Beispiel #1
0
int ipsw_extract_to_memory(const char* ipsw, const char* infile, unsigned char** pbuffer, unsigned int* psize) {
	ipsw_archive* archive = ipsw_open(ipsw);
	if (archive == NULL || archive->zip == NULL) {
		error("ERROR: Invalid archive\n");
		return -1;
	}

	int zindex = zip_name_locate(archive->zip, infile, 0);
	if (zindex < 0) {
		error("ERROR: zip_name_locate: %s\n", infile);
		return -1;
	}

	struct zip_stat zstat;
	zip_stat_init(&zstat);
	if (zip_stat_index(archive->zip, zindex, 0, &zstat) != 0) {
		error("ERROR: zip_stat_index: %s\n", infile);
		return -1;
	}

	struct zip_file* zfile = zip_fopen_index(archive->zip, zindex, 0);
	if (zfile == NULL) {
		error("ERROR: zip_fopen_index: %s\n", infile);
		return -1;
	}

	int size = zstat.size;
	unsigned char* buffer = (unsigned char*) malloc(size+1);
	if (buffer == NULL) {
		error("ERROR: Out of memory\n");
		zip_fclose(zfile);
		return -1;
	}

	if (zip_fread(zfile, buffer, size) != size) {
		error("ERROR: zip_fread: %s\n", infile);
		zip_fclose(zfile);
		free(buffer);
		return -1;
	}

	buffer[size] = '\0';

	zip_fclose(zfile);
	ipsw_close(archive);

	*pbuffer = buffer;
	*psize = size;
	return 0;
}
Beispiel #2
0
static void zip_read(struct zip_file *file, const char *filename)
{
	int size = 1024, n, read = 0;
	char *mem = malloc(size);

	while ((n = zip_fread(file, mem + read, size - read)) > 0) {
		read += n;
		size = read * 3 / 2;
		mem = realloc(mem, size);
	}
	mem[read] = 0;
	(void) parse_xml_buffer(filename, mem, read, &dive_table, NULL);
	free(mem);
}
Beispiel #3
0
/**
 * Read the contents of the first file of the ZIP file and load
 * it into a ModPlugFile.
**/
static ModPlugFile* loadZipFile(const char *path)
{
	ModPlugFile *self_ = NULL;
	int err = 0;
	struct zip *zf = zip_open(path, 0, &err);

	if (!zf) return NULL;

	struct zip_stat sb;

	// FIXME: Assumes the first file is the mod-file
	if (zip_stat_index(zf, 0, 0, &sb)) {
		MPSP_EPRINTF("failed to stat ZIP member: %s\n", zip_strerror(zf));
		goto exit1;
	}

	// FIXME: Assumes the first file is the mod-file
	struct zip_file *file = zip_fopen_index(zf, 0, 0);

	if (!file) {
		MPSP_EPRINTF("failed to open ZIP member: %s\n", zip_strerror(zf));
		goto exit1;
	}

	void *data = malloc(sb.size);

	if (!data) {
		MPSP_EPRINTF("failed to allocate memory: %s\n", strerror(errno));
		goto exit2;
	}

	if (zip_fread(file, data, sb.size) != sb.size) {
		MPSP_EPRINTF("failed to read ZIP member: %s\n", zip_file_strerror(file));
		goto exit3;
	}

	self_ = ModPlug_Load(data, sb.size);

exit3:
	free(data);

exit2:
	(void) zip_fclose(file);

exit1:
	(void) zip_close(zf);

	return self_;
}
Beispiel #4
0
cv::Mat ZipByteReader::Read(size_t seqId, const std::string& path, bool grayscale)
{
    // Find index of the file in .zip file.
    auto r = m_seqIdToIndex.find(seqId);
    if (r == m_seqIdToIndex.end())
        RuntimeError("Could not find file %s in the zip file, sequence id = %lu", path.c_str(), (long)seqId);

    zip_uint64_t index = std::get<0>((*r).second);
    zip_uint64_t size = std::get<1>((*r).second);

    auto contents = m_workspace.pop_or_create([size]() { return vector<unsigned char>(size); });
    if (contents.size() < size)
        contents.resize(size);
    auto zipFile = m_zips.pop_or_create([this]() { return OpenZip(); });
    {
        std::unique_ptr<zip_file_t, void(*)(zip_file_t*)> file(
            zip_fopen_index(zipFile.get(), index, 0),
            [](zip_file_t* f)
            {
                assert(f != nullptr);
                int err = zip_fclose(f);
                assert(ZIP_ER_OK == err);
#ifdef NDEBUG
                UNUSED(err);
#endif
            });
        assert(nullptr != file);
        if (nullptr == file)
        {
            RuntimeError("Could not open file %s in the zip file, sequence id = %lu, zip library error: %s",
                         path.c_str(), (long)seqId, GetZipError(zip_error_code_zip(zip_get_error(zipFile.get()))).c_str());
        }
        assert(contents.size() >= size);
        zip_uint64_t bytesRead = zip_fread(file.get(), contents.data(), size);
        assert(bytesRead == size);
        if (bytesRead != size)
        {
            RuntimeError("Bytes read %lu != expected %lu while reading file %s",
                         (long)bytesRead, (long)size, path.c_str());
        }
    }
    m_zips.push(std::move(zipFile));

    cv::Mat img; 
    img = cv::imdecode(cv::Mat(1, (int)size, CV_8UC1, contents.data()), grayscale ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
    assert(nullptr != img.data);
    m_workspace.push(std::move(contents));
    return img;
}
Beispiel #5
0
static int
do_read(struct zip *z, const char *name, int flags,
	enum when when_ex, int ze_ex, int se_ex)
{
    struct zip_file *zf;
    enum when when_got;
    int err, ze_got, se_got;
    char b[8192];
    zip_int64_t n;
    char expected[80];
    char got[80];

    when_got = WHEN_NEVER;
    ze_got = se_got = 0;
    
    if ((zf=zip_fopen(z, name, flags)) == NULL) {
	when_got = WHEN_OPEN;
	zip_error_get(z, &ze_got, &se_got);
    }
    else {
	while ((n=zip_fread(zf, b, sizeof(b))) > 0)
	    ;
	if (n < 0) {
	    when_got = WHEN_READ;
	    zip_file_error_get(zf, &ze_got, &se_got);
	}
	err = zip_fclose(zf);
	if (when_got == WHEN_NEVER && err != 0) {
	    when_got = WHEN_CLOSE;
	    ze_got = err;
	    se_got = 0;
	}
    }

    if (when_got != when_ex || ze_got != ze_ex || se_got != se_ex) {
	zip_error_to_str(expected, sizeof(expected), ze_ex, se_ex);
	zip_error_to_str(got, sizeof(got), ze_got, se_got);
	printf("%s: %s: got %s error (%s), expected %s error (%s)\n",
	       prg, name,
	       when_name[when_got], got, 
	       when_name[when_ex], expected);
	return 1;
    }
    else if (verbose)
	printf("%s: %s: passed\n", prg, name);

    return 0;
}
const uint CCZipFile::read(void *dest, const uint size)
{
	ASSERT( m_File != NULL && m_apkArchive != NULL );

	// Regular file handle
	const uint sz = zip_fread( m_File, dest, size );
	ASSERT( sz == size );
	if( sz > 0 )
	{
		m_Position += size;
		return size;
	}
	ASSERT_MESSAGE( false, "File::Read(...) : ERROR! Error reading" );

    return 0;
}
Beispiel #7
0
void ZipFileReader::initialize(ZipArchive& archive, const struct zip_stat& st) {
    _path.assign(utf8::decode(st.name));

    zip_file* file = zip_fopen_index(archive.c_obj(), st.index, 0);
    if (file == NULL) {
        throw Exception(format(
                    "{0}: {1}: {2}", archive.path(), _path, zip_error(archive.c_obj())));
    }
    AutoCloseZipFile close(file);

    _data.resize(st.size);
    int bytes_read = zip_fread(file, _data.data(), _data.size());
    if ((bytes_read < 0) || (bytes_read - _data.size() != 0)) {
        throw Exception(format("{0}: {1}: {2}", archive.path(), _path, zip_error(file)));
    }
}
Beispiel #8
0
/* {{{ php_zip_ops_read */
static size_t php_zip_ops_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
{
	int n = 0;
	STREAM_DATA_FROM_STREAM();

	if (self->za && self->zf) {
		n = (size_t)zip_fread(self->zf, buf, (int)count);

		if (n == 0) {
			stream->eof = 1;
		} else {
			self->cursor += n;
		}
	}
	return n<1 ? 0 : n;
}
Beispiel #9
0
/** @private */
SR_PRIV int sr_sessionfile_check(const char *filename)
{
    struct stat st;
    struct zip *archive;
    struct zip_file *zf;
    struct zip_stat zs;
    int version, ret;
    char s[11];

    if (!filename)
        return SR_ERR_ARG;

    if (stat(filename, &st) == -1) {
        sr_err("Couldn't stat %s: %s", filename, strerror(errno));
        return SR_ERR;
    }

    if (!(archive = zip_open(filename, 0, &ret)))
        /* No logging: this can be used just to check if it's
         * a sigrok session file or not. */
        return SR_ERR;

    /* check "version" */
    version = 0;
    if (!(zf = zip_fopen(archive, "version", 0))) {
        sr_dbg("Not a sigrok session file: no version found.");
        return SR_ERR;
    }
    if ((ret = zip_fread(zf, s, 10)) == -1)
        return SR_ERR;
    zip_fclose(zf);
    s[ret] = 0;
    version = strtoull(s, NULL, 10);
    if (version > 2) {
        sr_dbg("Cannot handle sigrok session file version %d.", version);
        return SR_ERR;
    }
    sr_spew("Detected sigrok session file version %d.", version);

    /* read "metadata" */
    if (zip_stat(archive, "metadata", 0, &zs) == -1) {
        sr_dbg("Not a valid sigrok session file.");
        return SR_ERR;
    }

    return SR_OK;
}
Beispiel #10
0
void ZipFile::Work_ReadFile(uv_work_t* req) {
    closure_t *closure = static_cast<closure_t *>(req->data);

    struct zip_file *zf_ptr = NULL;

    int idx = -1;

    std::vector<std::string>::iterator it = std::find(closure->zf->names_.begin(),
                                                      closure->zf->names_.end(),
                                                      closure->name);
    if (it != closure->zf->names_.end()) {
        idx = distance(closure->zf->names_.begin(), it);
    }

    if (idx == -1) {
        std::stringstream s;
        s << "No file found by the name of: '" << closure->name << "\n";
        closure->error = true;
        closure->error_name = s.str();
    } else {
        if ((zf_ptr = zip_fopen_index(closure->za, idx, 0)) == NULL) {
            std::stringstream s;
            s << "cannot open file #" << idx << " in "
              << closure->name << ": archive error: " << zip_strerror(closure->za) << "\n";
            closure->error = true;
            closure->error_name = s.str();
        } else {
            struct zip_stat st;
            zip_stat_index(closure->za, idx, 0, &st);
            closure->data.clear();
            closure->data.resize(st.size);

            int result =  0;
            result = static_cast<int>(zip_fread(zf_ptr, reinterpret_cast<void*> (&closure->data[0]), closure->data.size()));

            if (result < 0) {
                std::stringstream s;
                s << "error reading file #" << idx << " in "
                  << closure->name << ": archive error: " << zip_file_strerror(zf_ptr) << "\n";
                closure->error = true;
                closure->error_name = s.str();
            }
        }
    }
    zip_fclose(zf_ptr);
}
Beispiel #11
0
uint8_t *ReadFromZip(zip *archive, const char* filename, size_t *size) {
	// Figure out the file size first.
	struct zip_stat zstat;
	zip_file *file = zip_fopen(archive, filename, ZIP_FL_NOCASE|ZIP_FL_UNCHANGED);
	if (!file) {
		ELOG("Error opening %s from ZIP", filename);
		return 0;
	}
	zip_stat(archive, filename, ZIP_FL_NOCASE|ZIP_FL_UNCHANGED, &zstat);

	uint8_t *contents = new uint8_t[zstat.size + 1];
	zip_fread(file, contents, zstat.size);
	zip_fclose(file);
	contents[zstat.size] = 0;

	*size = zstat.size;
	return contents;
}
ByteStream::size_type ZipFileByteStream::ReadBytes(void *buf, size_type len)
{
    if (len == 0) return 0;

    if ( _file == nullptr )
        return 0;
    
    ssize_t numRead = zip_fread(_file, buf, len);
    if ( numRead < 0 )
    {
        Close();
        return 0;
    }

	_eof = (_file->bytes_left == 0);
    
    return numRead;
}
Beispiel #13
0
extern size_t FileRead( void *handle, void *buffer, size_t length )
{
    data_source     *ds = handle;
    size_t          amt;

    switch( ds->type ) {
    case DS_FILE:
        amt = read( ds->fhandle, buffer, length );
        break;
    case DS_ZIP:
        amt = zip_fread( ds->zf, buffer, length );
        break;
    default:
        amt = 0;
    }

    return( amt );
}
Beispiel #14
0
int _zip_fseek_by_reading(struct zip_file* zf, size_t toread)
{
    char bytes[1024];
    while (toread > 0) {
        ssize_t numRead = zip_fread(zf, bytes, (toread < 1024 ? toread : 1024));
        if (numRead < 0 )
            return -1;      /* error already set */
        if (numRead == 0) {
            /* avoid infinite loops */
            _zip_error_set(&zf->error, ZIP_ER_INCONS, 0);
            return -1;
        }
        
        toread -= numRead;
    }
    
    /* zf has been updated for us by zip_fread() already */
    return 0;
}
Beispiel #15
0
char *get_contents_zip(char *path, char *name, time_t *last_modified) {
	zip_t *archive = zip_open(path, ZIP_RDONLY, NULL);
	if (archive == NULL) {
		print_zip_err("zip_open", archive);
		return NULL;
	}

	zip_stat_t stat;
	if (zip_stat(archive, name, 0, &stat) < 0) {
		print_zip_err("zip_stat", archive);
		goto close_archive;
	}

	zip_file_t *f = zip_fopen(archive, name, 0);
	if (f == NULL) {
		print_zip_err("zip_fopen", archive);
		goto close_archive;
	}

	if (last_modified != NULL) {
		*last_modified = stat.mtime;
	}

	char *buf = malloc(stat.size + 1);
	if (zip_fread(f, buf, stat.size) < 0) {
		print_zip_err("zip_fread", archive);
		goto free_buf;
	}
	buf[stat.size] = '\0';

	zip_fclose(f);
	zip_close(archive);

	return buf;

free_buf:
	free(buf);
	zip_fclose(f);
close_archive:
	zip_close(archive);

	return NULL;
}
Beispiel #16
0
QByteArray readZipFile(const char *archiveName, const char *fileName)
{
    int error = 0;
    zip *archive;
    zip_file *file;
    char buffer[1024];
    int readsize = 0;
    QByteArray ret;

    archive = zip_open(archiveName, 0, &error);

    if (!archive)
    {
        qDebug() << "Error when opening archive" << archiveName;
        return ret;
    }

    file = zip_fopen(archive, fileName, 0);

    if (!file)
    {
        qDebug() << "Error when opening file "<< fileName <<" in archive: " << archiveName <<" : " << zip_strerror(archive);
        zip_close(archive);
        return ret;
    }

    do
    {
        ret.append(buffer, readsize);

        readsize = zip_fread(file, buffer, 1024);
    } while (readsize > 0) ;

    if (readsize < 0)
    {
        qDebug() << "Error when reading file "<< fileName <<" in archive: " << archiveName <<" : " << zip_file_strerror(file);
    }

    zip_fclose(file);
    zip_close(archive);

    return ret;
}
static int zip_get_contents(struct zip *zf, const char *filename, int locate_flags, char **buffer, uint32_t *len)
{
	struct zip_stat zs;
	struct zip_file *zfile;
	int zindex = zip_name_locate(zf, filename, locate_flags);

	*buffer = NULL;
	*len = 0;

	if (zindex < 0) {
		return -1;
	}

	zip_stat_init(&zs);

	if (zip_stat_index(zf, zindex, 0, &zs) != 0) {
		fprintf(stderr, "ERROR: zip_stat_index '%s' failed!\n", filename);
		return -2;
	}

	if (zs.size > 10485760) {
		fprintf(stderr, "ERROR: file '%s' is too large!\n", filename);
		return -3;
	}

	zfile = zip_fopen_index(zf, zindex, 0);
	if (!zfile) {
		fprintf(stderr, "ERROR: zip_fopen '%s' failed!\n", filename);
		return -4;
	}

	*buffer = malloc(zs.size);
	if (zs.size > LLONG_MAX || zip_fread(zfile, *buffer, zs.size) != (zip_int64_t)zs.size) {
		fprintf(stderr, "ERROR: zip_fread %" PRIu64 " bytes from '%s'\n", (uint64_t)zs.size, filename);
		free(*buffer);
		*buffer = NULL;
		zip_fclose(zfile);
		return -5;
	}
	*len = zs.size;
	zip_fclose(zfile);
	return 0;
}
Beispiel #18
0
bool get_jar_entry(
  const std::string &jar_file,
  std::size_t index,
  std::string &dest)
{
  #ifdef HAVE_LIBZIP
  int zip_error;
  
  struct zip *zip=
    zip_open(jar_file.c_str(), 0, &zip_error);
    
  if(zip==NULL)
    return true; // error
    
  struct zip_file *zip_file=
    zip_fopen_index(zip, index, 0);
  
  if(zip_file==NULL)
  {
    zip_close(zip);
    return true; // error
  }

  std::vector<char> buffer;
  buffer.resize(ZIP_READ_SIZE);
  
  while(true)
  {
    int bytes_read=
      zip_fread(zip_file, buffer.data(), ZIP_READ_SIZE);
    assert(bytes_read<=ZIP_READ_SIZE);
    if(bytes_read<=0) break;
    dest.insert(dest.end(), buffer.begin(), buffer.begin()+bytes_read);
  }

  zip_fclose(zip_file);    
  zip_close(zip);
  
  return false;
  #else
  return true;
  #endif
}
Beispiel #19
0
/**
 * Read metadata entries from a session archive.
 *
 * @param[in] archive An open ZIP archive.
 * @param[in] entry Stat buffer filled in for the metadata archive member.
 *
 * @return A new key/value store containing the session metadata.
 *
 * @private
 */
SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
			const struct zip_stat *entry)
{
	GKeyFile *keyfile;
	GError *error;
	struct zip_file *zf;
	char *metabuf;
	int metalen;

	if (entry->size > G_MAXINT || !(metabuf = g_try_malloc(entry->size))) {
		sr_err("Metadata buffer allocation failed.");
		return NULL;
	}
	zf = zip_fopen_index(archive, entry->index, 0);
	if (!zf) {
		sr_err("Failed to open metadata: %s", zip_strerror(archive));
		g_free(metabuf);
		return NULL;
	}
	metalen = zip_fread(zf, metabuf, entry->size);
	if (metalen < 0) {
		sr_err("Failed to read metadata: %s", zip_file_strerror(zf));
		zip_fclose(zf);
		g_free(metabuf);
		return NULL;
	}
	zip_fclose(zf);

	keyfile = g_key_file_new();
	error = NULL;
	g_key_file_load_from_data(keyfile, metabuf, metalen,
			G_KEY_FILE_NONE, &error);
	g_free(metabuf);

	if (error) {
		sr_err("Failed to parse metadata: %s", error->message);
		g_error_free(error);
		g_key_file_free(keyfile);
		return NULL;
	}
	return keyfile;
}
Beispiel #20
0
wxImage ZipEntry::LoadImage() {
  mutex->Lock();
  if (IsDirectory())
    return wxImage();

  struct zip_stat stat;
  zip_stat(zipFile, innerPath, 0, &stat);

  auto file = zip_fopen(zipFile, innerPath, 0);
  auto size = stat.size;
  auto buffer = new unsigned char[size];
  auto read = zip_fread(file, buffer, size);

  wxMemoryInputStream stream(buffer, size);
  wxImage output(stream);

  delete[] buffer;
  mutex->Unlock();
  return output;
}
Beispiel #21
0
//-------------------------------------------------------------------------------------------------------
void Unpack::Decompress(const std::string& from, const std::string& to)
{
	struct zip_stat zs;
	zip_stat(m_pAPKArchive, from.c_str(), ZIP_FL_UNCHANGED , &zs);//读取zip属性
	DEBUGLOG("%s size is %d ", from.c_str(), zs.size);

	byte* data = NEW byte[zs.size];

	zip_file* zipfile;
	zipfile = zip_fopen(m_pAPKArchive, from.c_str(), 0); //打开文件流
	zip_fread(zipfile, data, zs.size); //读取文件

	DEBUGLOG("save to %s", to.c_str());

	FilePtr writeto;
	File::Instance().OpenFile(to.c_str(), File::FILE_WRITE, writeto);
	File::Instance().WriteFile(data, zs.size, 1, writeto);
	File::Instance().CloseFile(writeto);

	zip_fclose(zipfile);//关闭文件
}
Beispiel #22
0
/*************************************************
 Function:		readDexFile
 Descroption:
 Input:
	1.zip* z
	2.filename
	3.char* buf
 Output:
 Return:
 Other:
*************************************************/
long readDexFile(struct zip* z, char* filename, unsigned char** buf)
{
    int i;
    long ReadNum = 0;
    struct zip_stat fstat;

    if(z != NULL && NULL != buf)
    {
        zip_stat_init(&fstat);
        int c = zip_get_num_files(z);
        if(c > 0)
        {
            for (i = 0 ; i < c; i++)
            {
                const char* name = zip_get_name(z, i, 0);
                if(0 == strcmp(name,filename))
                {
                    zip_stat(z, name,0,&fstat);
                    //LOGI("File %i:%s Size1: %lld Size2: %lld\n", i,fstat.name,fstat.size ,fstat.comp_size);

                    struct zip_file* file = zip_fopen(z, filename, 0);
                    if (file)
                    {
                        *buf =(unsigned char *)malloc(fstat.size+1);
                        memset(*buf,0,(fstat.size+1));
                        ReadNum = zip_fread(file, *buf,fstat.size);
                        zip_fclose(file);
                    }
                    break;
                }
            }
        }
    }
    else
    {
        return 0;
    }

    return ReadNum;
}
Beispiel #23
0
void *ResourceManager::loadFile(const char *filename, int &fsize) {
#ifdef _DEBUG
	if(fileExists(filename)) 
		return ::loadFile(filename, fsize);
#endif

	int fidx = zip_name_locate(archive, filename, 0);
	if( fidx == -1)
		return 0;

	struct zip_stat st;
	if( zip_stat_index(archive, fidx, 0, &st) == -1 )
		return 0;

	zip_file *zfile = zip_fopen_index(archive, fidx, 0);
	if (!zfile)
		return 0;

	char *buffer = new char[ st.size+1 ];
	if(!buffer)
		return 0;

	int size = st.size;
	char *buf = buffer;
	while(size > 0) {
		ssize_t rs = zip_fread(zfile, buf, std::min(size, 0x8000) );
		if(rs <= 0) {
			zip_fclose(zfile);
			delete buffer;
			return 0;
		}
		buf += rs;
		size -= rs;
	}
	buffer[st.size] = 0;
	zip_fclose(zfile);

	fsize = st.size;
	return buffer;
}
Beispiel #24
0
static int
cat(int argc, char *argv[]) {
    /* output file contents to stdout */
    zip_uint64_t idx;
    zip_int64_t n;
    zip_file_t *zf;
    char buf[8192];
    int err;
    idx = strtoull(argv[0], NULL, 10);

#ifdef _WIN32
    /* Need to set stdout to binary mode for Windows */
    setmode(fileno(stdout), _O_BINARY);
#endif
    if ((zf=zip_fopen_index(za, idx, 0)) == NULL) {
	fprintf(stderr, "can't open file at index '%" PRIu64 "': %s\n", idx, zip_strerror(za));
	return -1;
    }
    while ((n=zip_fread(zf, buf, sizeof(buf))) > 0) {
	if (fwrite(buf, (size_t)n, 1, stdout) != 1) {
	    zip_fclose(zf);
	    fprintf(stderr, "can't write file contents to stdout: %s\n", strerror(errno));
	    return -1;
	}
    }
    if (n == -1) {
	zip_fclose(zf);
	fprintf(stderr, "can't read file at index '%" PRIu64 "': %s\n", idx, zip_file_strerror(zf));
	return -1;
    }
    if ((err = zip_fclose(zf)) != 0) {
	zip_error_t error;

	zip_error_init_with_code(&error, err);
	fprintf(stderr, "can't close file at index '%" PRIu64 "': %s\n", idx, zip_error_strerror(&error));
	return -1;
    }

    return 0;
}
Beispiel #25
0
bool ZipFileManager::GetFileBuffer(const str_type::string &fileName, FileBuffer &out)
{
	if (!IsLoaded())
		return false;

	str_type::string fixedPath = fileName;
	FixSlashesForUnix(fixedPath);

	zip_file *file = zip_fopen(m_archive, fixedPath.c_str(), 0);

	if (file == NULL)
		return false;

	struct zip_stat stat;
	zip_stat(m_archive, fixedPath.c_str(), 0, &stat);

	out = FileBuffer(new _FileBuffer<unsigned char>(static_cast<unsigned long>(stat.size)));
	zip_fread(file, out->GetAddress(), stat.size);

	zip_fclose(file);
	return true;
}
Beispiel #26
0
/* {{{ php_zip_ops_read */
static size_t php_zip_ops_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
{
	int n = 0;
	STREAM_DATA_FROM_STREAM();

	if (self->za && self->zf) {
		n = (size_t)zip_fread(self->zf, buf, (int)count);
		if (n < 0) {
			int ze, se;
			zip_file_error_get(self->zf, &ze, &se);
			stream->eof = 1;
			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Zip stream error: %s", zip_file_strerror(self->zf));
			return 0;
		}
		if (n == 0 || n < count) {
			stream->eof = 1;
		} else {
			self->cursor += n;
		}
	}
	return n<1 ? 0 : n;
}
Beispiel #27
0
static int S_archive_file_read(lua_State* L) {
    struct zip_file** file = check_archive_file(L, 1);
    int               len  = luaL_checkint(L, 2);
    char*             buff;

    if ( len <= 0 ) luaL_argerror(L, 2, "Must be > 0");

    if ( ! *file ) return 0;

    buff = (char*)lua_newuserdata(L, len);

    len = zip_fread(*file, buff, len);

    if ( -1 == len ) {
        lua_pushnil(L);
        lua_pushstring(L, zip_file_strerror(*file));
        return 2;
    }

    lua_pushlstring(L, buff, len);
    return 1;
}
Beispiel #28
0
std::string jar_filet::get_entry(std::size_t i)
{
  if(zip==nullptr)
    return std::string("");

  assert(i<index.size());

  std::string dest;

  #ifdef HAVE_LIBZIP
  struct zip_file *zip_file=
    zip_fopen_index(static_cast<struct zip *>(zip), i, 0);

  if(zip_file==NULL)
  {
    zip_close(static_cast<struct zip *>(zip));
    zip=nullptr;
    return std::string(""); // error
  }

  std::vector<char> buffer;
  buffer.resize(ZIP_READ_SIZE);

  while(true)
  {
    int bytes_read=
      zip_fread(zip_file, buffer.data(), ZIP_READ_SIZE);
    assert(bytes_read<=ZIP_READ_SIZE);
    if(bytes_read<=0) break;
    dest.insert(dest.end(), buffer.begin(), buffer.begin()+bytes_read);
  }

  zip_fclose(zip_file);
  #endif

  return dest;
}
Beispiel #29
0
int r_io_zip_slurp_file(RIOZipFileObj *zip_file_obj) {
	struct zip_file *zFile = NULL;
	int result = R_FALSE;
	struct zip * zipArch = r_io_zip_open_archive(zip_file_obj->archivename, zip_file_obj->flags, zip_file_obj->mode, zip_file_obj->rw);
	struct zip_stat sb; 
	//eprintf("Slurping file");
	if (zip_file_obj && zip_file_obj->entry != -1) {

		zFile = zip_fopen_index(zipArch, zip_file_obj->entry, 0);
		if (!zip_file_obj->b) {
			zip_file_obj->b = r_buf_new();
		}
		zip_stat_init(&sb);

		if (zFile && zip_file_obj->b && !zip_stat_index(zipArch, zip_file_obj->entry, 0, &sb) ) {

			ut8 *buf = malloc(sb.size);
			memset(buf, 0, sb.size);

			if (buf) {			
				zip_fread(zFile, buf, sb.size);
				r_buf_set_bytes(zip_file_obj->b, buf, sb.size);
				zip_file_obj->opened = 1;
				result = R_TRUE;
			}
			if (buf)
				free(buf);
		}
		if (zFile) {
			zip_fclose(zFile);
		}
	}
	if (zipArch)
		zip_close(zipArch);

	return result;
}
Beispiel #30
0
AbstractFSProvider::status_t ZIPProvider::ZIPHandle::readFile(const FileName & file, std::vector<uint8_t> & data) {
	if (file.getFile().empty()) {
		return FAILURE;
	}

	const size_t size = fileSize(file);
	if (size == 0) {
		return FAILURE;
	}

	zip_file * fileHandle = zip_fopen(handle, file.getPath().c_str(), 0);
	if (fileHandle == nullptr) {
		WARN(zip_strerror(handle));
		return FAILURE;
	}

	data.resize(size);
	const int bytesRead = zip_fread(fileHandle, data.data(), data.size());
	if (bytesRead == -1) {
		WARN(zip_strerror(handle));
		zip_fclose(fileHandle);
		return FAILURE;
	}
	if (static_cast<size_t>(bytesRead) != size) {
		WARN("Sizes differ during read.");
		zip_fclose(fileHandle);
		return FAILURE;
	}

	if (zip_fclose(fileHandle) == -1) {
		WARN(zip_strerror(handle));
		return FAILURE;
	}

	return OK;
}