Example #1
0
int ipsw_get_file_size(const char* ipsw, const char* infile, off_t* size) {
	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;
	}

	*size = zstat.size;

	ipsw_close(archive);
	return 0;
}
Example #2
0
FileNode *FileNode::createNodeForZipEntry(struct zip *zip,
        const char *fname, zip_int64_t id) {
    FileNode *n = new FileNode(zip, fname, id);
    if (n == NULL) {
        return NULL;
    }
    n->is_dir = false;
    n->open_count = 0;
    n->state = CLOSED;

    struct zip_stat stat;
    zip_stat_index(zip, id, 0, &stat);
    // check that all used fields are valid
    zip_uint64_t needValid = ZIP_STAT_NAME | ZIP_STAT_INDEX |
        ZIP_STAT_SIZE | ZIP_STAT_MTIME;
    // required fields are always valid for existing items or newly added
    // directories (see zip_stat_index.c from libzip)
    assert((stat.valid & needValid) == needValid);
    n->m_mtime = n->m_atime = n->m_ctime = stat.mtime;
    n->has_cretime = false;
    n->m_size = stat.size;

    n->parse_name();

    n->processExternalAttributes();
    n->processExtraFields();
    return n;
}
Example #3
0
static int r_io_zip_slurp_file(RIOZipFileObj *zfo) {
	struct zip_file *zFile = NULL;
	struct zip *zipArch;
	struct zip_stat sb;
	bool res = false;
	if (!zfo) {
		return res;
	}
	zipArch = r_io_zip_open_archive (
		zfo->archivename, zfo->perm,
		zfo->mode, zfo->rw);

	if (zipArch && zfo && zfo->entry != -1) {
		zFile = zip_fopen_index (zipArch, zfo->entry, 0);
		if (!zfo->b) {
			zfo->b = r_buf_new ();
		}
		zip_stat_init (&sb);
		if (zFile && zfo->b && !zip_stat_index (zipArch, zfo->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 (zfo->b, buf, sb.size);
				res = true;
				zfo->opened = true;
				free (buf);
			}
		}
		zip_fclose (zFile);
	}
	zip_close (zipArch);
	return res;
}
Example #4
0
RList * r_io_zip_get_files(char *archivename, ut32 flags, int mode, int rw) {
	ut64 num_entries = 0, i = 0;
	struct zip * zipArch = r_io_zip_open_archive(archivename, flags, mode, rw);
	struct zip_stat sb; 
	RList *files = NULL; 
	//eprintf("Slurping file");
	if (zipArch) {
		files = r_list_new();
		num_entries = zip_get_num_files(zipArch);

		for (i=0; i < num_entries; i++) {
			char *name = NULL;	
			zip_stat_init(&sb );
			zip_stat_index(zipArch, i, 0, &sb );	
			//eprintf("Comparing %s == %s = %d\n", sb.name, filename, strcmp(sb.name, filename));
			name = strdup(sb.name);
			if (name) {
				r_list_append(files, name);
			}

		}
	}
	if (zipArch)
		zip_close(zipArch);

	return files;
}
Example #5
0
static int zip_enumerate(arch_enum_t e,struct arch_enum_callbacks *cb,void*arg){
    if (cb->data!=NULL) Abort("cannot enumerate data");
    while(e->next<e->count){
        struct zip_stat sb;
        int res=zip_stat_index(e->archive->archive,e->next,0,&sb);
        if (res<0) {
            Abort("cannot stat zip archive: %s\n",zip_strerror(e->archive->archive));
        }
        if(cb->new_item) {
            res=cb->new_item(arg,e->next,sb.name);
            if (res) {
                e->next++;
                return res;
            }
        }
        if(cb->stat) {
            struct archive_item_s item;
            item.name=sb.name;
            item.code=zip_get_file_comment(e->archive->archive,e->next,NULL,0);
            item.length=sb.size;
            item.compressed=sb.comp_size;
            res=cb->stat(arg,e->next,&item);
            if (res) {
                e->next++;
                return res;
            }
            
        }
        e->next++;
    }
    return 0;
}
Example #6
0
/* The file can be a file in the archive or ::[num].  */
RIOZipFileObj* r_io_zip_alloc_zipfileobj(const char *archivename, const char *filename, ut32 perm, int mode, int rw) {
	RIOZipFileObj *zfo = NULL;
	ut64 i, num_entries;
	struct zip_stat sb;
	struct zip *zipArch = r_io_zip_open_archive (archivename, perm, mode, rw);
	if (!zipArch) {
		return NULL;
	}
	num_entries = zip_get_num_files (zipArch);

	for (i = 0; i < num_entries; i++) {
		zip_stat_init (&sb);
		zip_stat_index (zipArch, i, 0, &sb);
		if (sb.name != NULL) {
			if (strcmp (sb.name, filename) == 0) {
				zfo = r_io_zip_create_new_file (
					archivename, filename, &sb,
					perm, mode, rw);
				r_io_zip_slurp_file (zfo);
				break;
			}
		}
	}
	if (!zfo) {
		zfo = r_io_zip_create_new_file (archivename,
			filename, NULL, perm, mode, rw);
	}
	zip_close (zipArch);
	return zfo;
}
Example #7
0
archive_t arch_zip_read(const char* name,int buf){
    archive_t arch=(archive_t)HREmalloc(NULL,sizeof(struct archive_s));
    arch_init(arch);
    int err;
    arch->archive=zip_open(name,ZIP_CHECKCONS,&err);
    if (arch->archive==NULL){
        char errstr[1024];
        zip_error_to_str(errstr, sizeof(errstr), err, errno);
        Abort("cannot open zip archive `%s': %s\n",name , errstr);
    }
    arch->stream_index=SIcreate();
#ifdef LIBZIP_VERSION
    int count=zip_get_num_entries(arch->archive,0);
#else
    int count=zip_get_num_files(arch->archive);
#endif
    for(int i=0;i<count;i++){
        struct zip_stat sb;
        int res=zip_stat_index(arch->archive,i,0,&sb);
        if (res<0) {
            Abort("cannot stat zip archive: %s\n",zip_strerror(arch->archive));
        }
        SIputAt(arch->stream_index,sb.name,i);
        Print(infoShort,"stream %d is %s",i,sb.name);
    }
    arch->procs.contains=zip_contains;
    arch->procs.read=hre_zip_read;
    arch->procs.read_raw=hre_zip_read_raw;
    arch->procs.enumerator=zip_enum;
    arch->procs.close=hre_zip_close;
    arch->buf=buf;
    return arch;
}
Example #8
0
size_t Unzip(const char *dst_path_, const char *archive, const UnzipFileHandler& handler)
{
    struct zip *za;
    struct zip_file *zf;
    struct zip_stat sb;
    char buf[1024];
    size_t ret = 0;
    std::string dst_path = dst_path_;

    int err;
    if ((za = zip_open(archive, 0, &err)) == nullptr) {
        zip_error_to_str(buf, sizeof(buf), err, errno);
        return false;
    }

    for (int i = 0; i < zip_get_num_entries(za, 0); ++i) {
        if (zip_stat_index(za, i, 0, &sb) == 0) {
            std::string dstpath = dst_path + '/' + sb.name;

            if (dstpath.back() == '/') {
                std::experimental::filesystem::create_directories(dstpath.c_str());
            }
            else {
                zf = zip_fopen_index(za, i, 0);
                if (!zf) {
                    goto bail_out;
                }

                FILE *of = fopen(dstpath.c_str(), "wb");
                if (of == nullptr) {
                    goto bail_out;
                }

                size_t sum = 0;
                while (sum != sb.size) {
                    size_t len = (size_t)zip_fread(zf, buf, sizeof(buf));
                    if (len < 0) {
                        fclose(of);
                        goto bail_out;
                    }
                    fwrite(buf, 1, len, of);
                    sum += len;
                }
                fclose(of);
                zip_fclose(zf);
                if (handler) { handler(dstpath.c_str()); }
                ++ret;
            }
        }
    }

    if (zip_close(za) == -1) {
        return false;
    }
    return ret;

bail_out:
    zip_close(za);
    return ret;
}
Example #9
0
File: rom.c Project: k5g/emulika
static int fzip2buffer(struct zip *fzip, int index, byte **buffer)
{
    struct zip_stat fstat;
    struct zip_file *file = NULL;

    *buffer = NULL;

    zip_stat_index(fzip, index, 0, &fstat);
    *buffer = malloc(fstat.size);

    // Read zipped file
    if((file = zip_fopen(fzip, fstat.name, ZIP_FL_UNCHANGED))==NULL)
        goto error;
    if(zip_fread(file, *buffer, fstat.size)!=fstat.size)
        goto error;
    zip_fclose(file); file = NULL;

    return fstat.size;

error:
    free(*buffer); *buffer = NULL;
    if(file)
        zip_fclose(file);

    return -1;
}
Example #10
0
int r_io_zip_slurp_file(RIOZipFileObj *zfo) {
	int res = R_FALSE;
	struct zip_stat sb;
	struct zip_file *zFile = NULL;
	struct zip * zipArch ;

	if (!zfo) return res;
	zipArch = r_io_zip_open_archive (
		zfo->archivename, zfo->flags,
		zfo->mode, zfo->rw);
	//eprintf("Slurping file");

	if (zipArch && zfo && zfo->entry != -1) {
		zFile = zip_fopen_index (zipArch, zfo->entry, 0);
		if (!zfo->b)
			zfo->b = r_buf_new ();
		zip_stat_init (&sb);
		if (zFile && zfo->b && !zip_stat_index(zipArch,
				zfo->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 (zfo->b, buf, sb.size);
				res = zfo->opened = R_TRUE;
				free (buf);
			}
		}
		zip_fclose (zFile);
	}
	zip_close (zipArch);
	return res;
}
Example #11
0
QStringList KaraokePlayable_ZIP::enumerate()
{
    QStringList filelist;

    // http://www.nih.at/libzip/zip_get_num_entries.html
    int files = zip_get_num_entries( m_zip, ZIP_FL_UNCHANGED );

    if ( files == 0 )
    {
        m_errorMsg = "archive has no files";
        return filelist;
    }

    for ( int i = 0; i < files; i++ )
    {
        // Retrieve the file size
        struct zip_stat fileinfo;

        // http://www.nih.at/libzip/zip_stat_index.html
        if ( zip_stat_index( m_zip, i, 0, &fileinfo) != 0 )
        {
            m_errorMsg = "file info not found";
            return QStringList();
        }

        filelist.push_back( decodeFilename( fileinfo.name ) );
    }

    return filelist;
}
Example #12
0
QByteArray zipobject::readData(const QString& inZipPath)const{
    if( NULL == m_zip){
        qDebug()<<"zip unopened";
        return QByteArray();
    }
    int index = zip_name_locate(m_zip,getCharPtr(inZipPath),0);
    if( -1 == index ){
        qDebug()<<"no such file : "<<inZipPath;
        return QByteArray();
    }
    zip_file *pFile = zip_fopen_index(m_zip,index,0);
    if( NULL == pFile){
        qDebug()<<"fopen is NULL"<<zip_strerror(m_zip);
        return QByteArray();
    }
    struct zip_stat stat;
    int rStat = zip_stat_index(m_zip,index, 0, &stat);
    if( -1 == rStat ){
        qDebug()<<"stat failed : "<<zip_strerror(m_zip);
        return QByteArray();
    }
    const int length = stat.size;
    char buffer[length +1 ];
    int rRead = zip_fread(pFile,buffer,sizeof(buffer));
    if( -1 == rRead ){
        qDebug()<<"read failed : "<<zip_strerror(m_zip);
        return QByteArray();
    }
    return QByteArray (buffer,rRead);
}
Example #13
0
bool zipobject::extractTo(const QString& dirPath){
    int num_entries = zip_get_num_files(m_zip) ;
    for( int i = 0 ; i < num_entries ; ++i ){
        qDebug()<<zip_get_name(m_zip,i,0);
        zip_file* pFile = zip_fopen_index(m_zip,i,0);
        struct zip_stat stat ;
        zip_stat_index(m_zip,i,0,&stat);
        
        const int length = stat.size ;
        char buffer[length +1 ] ;
        int rRead = zip_fread(pFile,buffer,sizeof(buffer));
        if( -1 == rRead ){
            qDebug()<<"read failed : "<<zip_strerror(m_zip);
            return false;
        }
        
        QFileInfo fileInfo(dirPath + '/' + stat.name);
        QDir dir = fileInfo.absoluteDir() ;
        if(!dir.mkpath(".")){
            return false;
        }
        
        QFile file(dirPath + '/' + stat.name);
        file.open(QIODevice::WriteOnly);
        file.write(QByteArray(buffer,length));
        file.close();
    }
    return true;
}
Example #14
0
static int
confirm_replace(struct zip *za, const char *tname, int it,
		struct zip *zs, const char *sname, int is)
{
    char line[1024];
    struct zip_stat st, ss;

    if (confirm & CONFIRM_ALL_YES)
	return 1;
    else if (confirm & CONFIRM_ALL_NO)
	return 0;

    if (zip_stat_index(za, it, ZIP_FL_UNCHANGED, &st) < 0) {
	fprintf(stderr, "%s: cannot stat file %d in `%s': %s\n",
		prg, it, tname, zip_strerror(za));
	return -1;
    }
    if (zip_stat_index(zs, is, 0, &ss) < 0) {
	fprintf(stderr, "%s: cannot stat file %d in `%s': %s\n",
		prg, is, sname, zip_strerror(zs));
	return -1;
    }

    if (st.size == ss.size && st.crc == ss.crc) {
	if (confirm & CONFIRM_SAME_YES)
	    return 1;
	else if (confirm & CONFIRM_SAME_NO)
	    return 0;
    }

    printf("replace `%s' (%llu / %08x) in `%s'\n"
	   "   with `%s' (%llu / %08x) from `%s'? ",
	   st.name, st.size, st.crc, tname,
	   ss.name, ss.size, ss.crc, sname);
    fflush(stdout);

    if (fgets(line, sizeof(line), stdin) == NULL) {
	fprintf(stderr, "%s: read error from stdin: %s\n",
		prg, strerror(errno));
	return -1;
    }

    if (tolower((unsigned char)line[0]) == 'y')
	return 1;

    return 0;
}
Example #15
0
const char* _vdezName(struct VDirEntry* vde) {
	struct VDirEntryZip* vdez = (struct VDirEntryZip*) vde;
	struct zip_stat s;
	if (zip_stat_index(vdez->z, vdez->index, 0, &s) < 0) {
		return 0;
	}
	return s.name;
}
Example #16
0
ZipFileReader::ZipFileReader(ZipArchive& archive, int index) {
    struct zip_stat st;
    if (zip_stat_index(archive.c_obj(), index, 0, &st) != 0) {
        throw Exception(format(
                    "{0}: {1}: {2}", archive.path(), index, zip_error(archive.c_obj())));
    }
    initialize(archive, st);
}
bool Upgrader::unzipper(int targetfile)
{
    bfs::path target = path(DATA) / "upgrade";
    if (!verifyPath(target.c_str(), true)) {return false;}

    const char *targetzip = (target / targetswitch(targetfile)).string().c_str();

    struct zip *archive;
    struct zip_file *zipfile;
    struct zip_stat filestat;
    char buffer[1024*1024];
    FILE *file;
    int bufferlength, err;
    unsigned long long sum;

    printf("Extracting %s\n", targetzip);

    if ((archive = zip_open(targetzip, 0, &err)) == NULL) {
        printf("Failed to open archive %s\n", targetzip);
        return false;
    }
 
    for (unsigned int i = 0; i < zip_get_num_entries(archive, 0); i++) 
    {
        if (zip_stat_index(archive, i, 0, &filestat) == 0)
        {
            verifyPath((target / filestat.name).parent_path(), true);
            if (!is_directory(target / filestat.name))
            {
                zipfile = zip_fopen_index(archive, i, 0);
                if (!zipfile) {
                    printf("Could not open %s in archive\n", filestat.name);
                    continue;
                }

                file = fopen((target / filestat.name).string().c_str(), "w");
 
                sum = 0;
                while (sum != filestat.size) {
                    bufferlength = zip_fread(zipfile, buffer, 1024*1024);
                    fwrite(buffer, sizeof(char), bufferlength, file);
                    sum += bufferlength;
                }
                printf("Finished extracting %s\n", filestat.name);
                fclose(file);
                zip_fclose(zipfile);
            }
        }
    }
    if (zip_close(archive) == -1) 
    {
        printf("Can't close zip archive %s\n", targetzip);
        return false;
    }

    bfs::remove(target / targetswitch(targetfile));
    return true;
}
Example #18
0
ZIP_EXTERN int
zip_stat(struct zip *za, const char *fname, zip_flags_t flags, struct zip_stat *st)
{
    zip_int64_t idx;

    if ((idx=zip_name_locate(za, fname, flags)) < 0)
	return -1;

    return zip_stat_index(za, (zip_uint64_t)idx, flags, st);
}
Example #19
0
ZIP_EXTERN int
zip_stat(struct zip *za, const char *fname, int flags, struct zip_stat *st)
{
    int idx;

    if ((idx=zip_name_locate(za, fname, flags)) < 0)
	return -1;

    return zip_stat_index(za, idx, flags, st);
}
Example #20
0
static STRBUF *read_from_zip(const char *zipfile, const char *filename)
{
	int r = 0;
	STRBUF *content = NULL;

#ifdef HAVE_LIBZIP
	int zip_error;
	struct zip *zip = NULL;
	struct zip_stat stat;
	struct zip_file *unzipped = NULL;
	char *buf = NULL;

	if ( !(zip = zip_open(zipfile, 0, &zip_error)) ||
	     (r = zip_name_locate(zip, filename, 0)) < 0 ||
	     (zip_stat_index(zip, r, ZIP_FL_UNCHANGED, &stat) < 0) ||
	     !(unzipped = zip_fopen_index(zip, r, ZIP_FL_UNCHANGED)) ) {
		if (unzipped)
			zip_fclose(unzipped);
		if (zip)
			zip_close(zip);
		r = -1;
	}
#else
	r = kunzip_get_offset_by_name((char*)zipfile, (char*)filename, 3, -1);
#endif

	if(-1 == r) {
		fprintf(stderr,
			"Can't read from %s: Is it an OpenDocument Text?\n", zipfile);
		exit(EXIT_FAILURE);
	}

#ifdef HAVE_LIBZIP
	if ( !(buf = ymalloc(stat.size + 1)) ||
	     (zip_fread(unzipped, buf, stat.size) != stat.size) ||
	     !(content = strbuf_slurp_n(buf, stat.size)) ) {
		if (buf)
			yfree(buf);
		content = NULL;
	}
	zip_fclose(unzipped);
	zip_close(zip);
#else
	content = kunzip_next_tobuf((char*)zipfile, r);
#endif

	if (!content) {
		fprintf(stderr,
			"Can't extract %s from %s.  Maybe the file is corrupted?\n",
			filename, zipfile);
		exit(EXIT_FAILURE);
	}

	return content;
}
Example #21
0
Handle<Value> ZipFile::readFileSync(const Arguments& args) {
    HandleScope scope;

    if (args.Length() != 1 || !args[0]->IsString())
        return ThrowException(Exception::TypeError(
                                  String::New("first argument must be a file name inside the zip")));

    std::string name = TOSTR(args[0]);

    ZipFile* zf = ObjectWrap::Unwrap<ZipFile>(args.This());

    struct zip_file *zf_ptr;

    int idx = -1;

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

    if (idx == -1) {
        std::stringstream s;
        s << "No file found by the name of: '" << name << "\n";
        return ThrowException(Exception::Error(String::New(s.str().c_str())));
    }

    if ((zf_ptr=zip_fopen_index(zf->archive_, idx, 0)) == NULL) {
        zip_fclose(zf_ptr);
        std::stringstream s;
        s << "cannot open file #" << idx << " in " << name << ": archive error: " << zip_strerror(zf->archive_) << "\n";
        return ThrowException(Exception::Error(String::New(s.str().c_str())));
    }

    struct zip_stat st;
    zip_stat_index(zf->archive_, idx, 0, &st);

    std::vector<unsigned char> data;
    data.clear();
    data.resize(st.size);

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

    if (result < 0) {
        zip_fclose(zf_ptr);
        std::stringstream s;
        s << "error reading file #" << idx << " in " << name << ": archive error: " << zip_file_strerror(zf_ptr) << "\n";
        return ThrowException(Exception::Error(String::New(s.str().c_str())));
    }

    node::Buffer *retbuf = Buffer::New(reinterpret_cast<char *>(&data[0]), data.size());
    zip_fclose(zf_ptr);
    return scope.Close(retbuf->handle_);
}
Example #22
0
void ZipFile::GetNames()
{
    int num = zip_get_num_files(this->archive);
    this->names.clear();
    this->names.reserve(num);
    int i = 0;
    for (i=0; i<num; i++) {
        struct zip_stat st;
        zip_stat_index(this->archive, i, 0, &st);
        this->names.push_back(st.name);
    }
}
Example #23
0
 const uint64 GetFileSize(size_t index) const override
 {
     zip_stat_t zipFileStat;
     if (zip_stat_index(_zip, index, 0, &zipFileStat) == ZIP_ER_OK)
     {
         return zipFileStat.size;
     }
     else
     {
         return 0;
     }
 }
Example #24
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;
}
Example #25
0
static int S_archive_stat(lua_State* L) {
    struct zip**    ar        = check_archive(L, 1);
    const char*     path      = (lua_isnumber(L, 2)) ? NULL : luaL_checkstring(L, 2);
    int             path_idx  = (lua_isnumber(L, 2)) ? luaL_checkint(L, 2)-1 : -1;
    int             flags     = (lua_gettop(L) < 3)  ? 0    : luaL_checkint(L, 3);
    struct zip_stat stat;
    int             result;

    if ( ! *ar ) return 0;

    if ( NULL == path ) {
        result = zip_stat_index(*ar, path_idx, flags, &stat);
    } else {
        result = zip_stat(*ar, path, flags, &stat);
    }

    if ( result != 0 ) {
        lua_pushnil(L);
        lua_pushstring(L, zip_strerror(*ar));
        return 2;
    }

    lua_createtable(L, 0, 8);

    lua_pushstring(L, stat.name);
    lua_setfield(L, -2, "name");

    lua_pushinteger(L, stat.index+1);
    lua_setfield(L, -2, "index");

    lua_pushnumber(L, stat.crc);
    lua_setfield(L, -2, "crc");

    lua_pushnumber(L, stat.size);
    lua_setfield(L, -2, "size");

    lua_pushnumber(L, stat.mtime);
    lua_setfield(L, -2, "mtime");

    lua_pushnumber(L, stat.comp_size);
    lua_setfield(L, -2, "comp_size");

    lua_pushnumber(L, stat.comp_method);
    lua_setfield(L, -2, "comp_method");

    lua_pushnumber(L, stat.encryption_method);
    lua_setfield(L, -2, "encryption_method");

    return 1;
}
Example #26
0
	void iterateFiles(FunctorT Functor)
	{
		struct zip_stat Stat;
		auto NumEntries = zip_get_num_entries(mZip, 0);
		for (zip_uint64_t I = 0; I != NumEntries; ++I)
		{
			auto Error = zip_stat_index(mZip, I, 0, &Stat);
			if (Error != 0) throw std::runtime_error{"Error reading zip"};
			auto Length = std::char_traits<char>::length(Stat.name);
			if (Length == 0 || Stat.name[Length - 1] == '/') continue;
			auto File = ZipFileT{Stat.name, zip_fopen_index(mZip, I, 0)};
			Functor(File);
		}
	}
Example #27
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_;
}
Example #28
0
	PImportStream COpcPackageReader::openZIPEntryIndexed(_In_ nfUint64 nIndex)
	{

		zip_stat_t Stat;
		nfInt32 nResult = zip_stat_index(m_ZIParchive, nIndex, ZIP_FL_UNCHANGED, &Stat);
		if (nResult != 0)
			throw CNMRException(NMR_ERROR_COULDNOTSTATZIPENTRY);

		nfUint64 nSize = Stat.size;

		zip_file_t * pFile = zip_fopen_index(m_ZIParchive, nIndex, ZIP_FL_UNCHANGED);
		if (pFile == nullptr)
			throw CNMRException(NMR_ERROR_COULDNOTOPENZIPENTRY);

		return std::make_shared<CImportStream_ZIP>(pFile, nSize);

	}
Example #29
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);
}
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;
}