rlm@1: /* unzip.c -- IO on .zip files using zlib rlm@1: Version 0.15 beta, Mar 19th, 1998, rlm@1: rlm@1: Read unzip.h for more info rlm@1: */ rlm@1: rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: #include "zlib.h" rlm@1: #include "unzip.h" rlm@1: rlm@1: #ifdef NO_ERRNO_H rlm@1: extern int errno; rlm@1: #else rlm@1: # include rlm@1: #endif rlm@1: rlm@1: #ifndef local rlm@1: # define local static rlm@1: #endif rlm@1: /* compile with -Dlocal if your debugger can't find static symbols */ rlm@1: rlm@1: #if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \ rlm@1: !defined(CASESENSITIVITYDEFAULT_NO) rlm@1: #define CASESENSITIVITYDEFAULT_NO rlm@1: #endif rlm@1: rlm@1: #ifndef UNZ_BUFSIZE rlm@1: #define UNZ_BUFSIZE (16384) rlm@1: #endif rlm@1: rlm@1: #ifndef UNZ_MAXFILENAMEINZIP rlm@1: #define UNZ_MAXFILENAMEINZIP (256) rlm@1: #endif rlm@1: rlm@1: #ifndef ALLOC rlm@1: # define ALLOC(size) (malloc(size)) rlm@1: #endif rlm@1: #ifndef TRYFREE rlm@1: # define TRYFREE(p) {if (p) \ rlm@1: free(p);} rlm@1: #endif rlm@1: rlm@1: #define SIZECENTRALDIRITEM (0x2e) rlm@1: #define SIZEZIPLOCALHEADER (0x1e) rlm@1: rlm@1: /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ rlm@1: rlm@1: #ifndef SEEK_CUR rlm@1: #define SEEK_CUR 1 rlm@1: #endif rlm@1: rlm@1: #ifndef SEEK_END rlm@1: #define SEEK_END 2 rlm@1: #endif rlm@1: rlm@1: #ifndef SEEK_SET rlm@1: #define SEEK_SET 0 rlm@1: #endif rlm@1: rlm@1: const char unz_copyright[] = rlm@1: " unzip 0.15 Copyright 1998 Gilles Vollant "; rlm@1: rlm@1: /* unz_file_info_interntal contain internal info about a file in zipfile*/ rlm@1: typedef struct unz_file_info_internal_s rlm@1: { rlm@1: uLong offset_curfile; /* relative offset of local header 4 bytes */ rlm@1: } unz_file_info_internal; rlm@1: rlm@1: /* file_in_zip_read_info_s contain internal information about a file in zipfile, rlm@1: when reading and decompress it */ rlm@1: typedef struct rlm@1: { rlm@1: char * read_buffer; /* internal buffer for compressed data */ rlm@1: z_stream stream; /* zLib stream structure for inflate */ rlm@1: rlm@1: uLong pos_in_zipfile; /* position in byte on the zipfile, for fseek*/ rlm@1: uLong stream_initialised; /* flag set if stream structure is initialised*/ rlm@1: rlm@1: uLong offset_local_extrafield; /* offset of the local extra field */ rlm@1: uInt size_local_extrafield; /* size of the local extra field */ rlm@1: uLong pos_local_extrafield; /* position in the local extra field in read*/ rlm@1: rlm@1: uLong crc32; /* crc32 of all data uncompressed */ rlm@1: uLong crc32_wait; /* crc32 we must obtain after decompress all */ rlm@1: uLong rest_read_compressed; /* number of byte to be decompressed */ rlm@1: uLong rest_read_uncompressed; /*number of byte to be obtained after decomp*/ rlm@1: FILE* file; /* io structore of the zipfile */ rlm@1: uLong compression_method; /* compression method (0==store) */ rlm@1: uLong byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx)*/ rlm@1: } file_in_zip_read_info_s; rlm@1: rlm@1: /* unz_s contain internal information about the zipfile rlm@1: */ rlm@1: typedef struct rlm@1: { rlm@1: FILE*file; /* io structore of the zipfile */ rlm@1: unz_global_info gi; /* public global information */ rlm@1: uLong byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx)*/ rlm@1: uLong num_file; /* number of the current file in the zipfile*/ rlm@1: uLong pos_in_central_dir; /* pos of the current file in the central dir*/ rlm@1: uLong current_file_ok; /* flag about the usability of the current file*/ rlm@1: uLong central_pos; /* position of the beginning of the central dir*/ rlm@1: rlm@1: uLong size_central_dir; /* size of the central directory */ rlm@1: uLong offset_central_dir; /* offset of start of central directory with rlm@1: respect to the starting disk number */ rlm@1: rlm@1: unz_file_info cur_file_info; /* public info about the current file in zip*/ rlm@1: unz_file_info_internal cur_file_info_internal; /* private info about it*/ rlm@1: file_in_zip_read_info_s*pfile_in_zip_read; /* structure about the current rlm@1: file if we are decompressing it */ rlm@1: } unz_s; rlm@1: rlm@1: /* =========================================================================== rlm@1: Read a byte from a gz_stream; update next_in and avail_in. Return EOF rlm@1: for end of file. rlm@1: IN assertion: the stream s has been sucessfully opened for reading. rlm@1: */ rlm@1: rlm@1: local int unzlocal_getByte(FILE *fin, int *pi) rlm@1: { rlm@1: unsigned char c; rlm@1: int err = fread(&c, 1, 1, fin); rlm@1: if (err == 1) rlm@1: { rlm@1: *pi = (int)c; rlm@1: return UNZ_OK; rlm@1: } rlm@1: else rlm@1: { rlm@1: if (ferror(fin)) rlm@1: return UNZ_ERRNO; rlm@1: else rlm@1: return UNZ_EOF; rlm@1: } rlm@1: } rlm@1: rlm@1: /* =========================================================================== rlm@1: Reads a long in LSB order from the given gz_stream. Sets rlm@1: */ rlm@1: local int unzlocal_getShort(FILE *fin, uLong *pX) rlm@1: { rlm@1: uLong x ; rlm@1: int i; rlm@1: int err; rlm@1: rlm@1: err = unzlocal_getByte(fin, &i); rlm@1: x = (uLong)i; rlm@1: rlm@1: if (err == UNZ_OK) rlm@1: err = unzlocal_getByte(fin, &i); rlm@1: x += ((uLong)i)<<8; rlm@1: rlm@1: if (err == UNZ_OK) rlm@1: *pX = x; rlm@1: else rlm@1: *pX = 0; rlm@1: return err; rlm@1: } rlm@1: rlm@1: local int unzlocal_getLong(FILE *fin, uLong *pX) rlm@1: { rlm@1: uLong x ; rlm@1: int i; rlm@1: int err; rlm@1: rlm@1: err = unzlocal_getByte(fin, &i); rlm@1: x = (uLong)i; rlm@1: rlm@1: if (err == UNZ_OK) rlm@1: err = unzlocal_getByte(fin, &i); rlm@1: x += ((uLong)i)<<8; rlm@1: rlm@1: if (err == UNZ_OK) rlm@1: err = unzlocal_getByte(fin, &i); rlm@1: x += ((uLong)i)<<16; rlm@1: rlm@1: if (err == UNZ_OK) rlm@1: err = unzlocal_getByte(fin, &i); rlm@1: x += ((uLong)i)<<24; rlm@1: rlm@1: if (err == UNZ_OK) rlm@1: *pX = x; rlm@1: else rlm@1: *pX = 0; rlm@1: return err; rlm@1: } rlm@1: rlm@1: /* My own strcmpi / strcasecmp */ rlm@1: local int strcmpcasenosensitive_internal(const char *fileName1, rlm@1: const char *fileName2) rlm@1: { rlm@1: for (;;) rlm@1: { rlm@1: char c1 = *(fileName1++); rlm@1: char c2 = *(fileName2++); rlm@1: if ((c1 >= 'a') && (c1 <= 'z')) rlm@1: c1 -= 0x20; rlm@1: if ((c2 >= 'a') && (c2 <= 'z')) rlm@1: c2 -= 0x20; rlm@1: if (c1 == '\0') rlm@1: return ((c2 == '\0') ? 0 : -1); rlm@1: if (c2 == '\0') rlm@1: return 1; rlm@1: if (c1 < c2) rlm@1: return -1; rlm@1: if (c1 > c2) rlm@1: return 1; rlm@1: } rlm@1: } rlm@1: rlm@1: #ifdef CASESENSITIVITYDEFAULT_NO rlm@1: #define CASESENSITIVITYDEFAULTVALUE 2 rlm@1: #else rlm@1: #define CASESENSITIVITYDEFAULTVALUE 1 rlm@1: #endif rlm@1: rlm@1: #ifndef STRCMPCASENOSENTIVEFUNCTION rlm@1: #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal rlm@1: #endif rlm@1: rlm@1: /* rlm@1: Compare two filename (fileName1,fileName2). rlm@1: If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) rlm@1: If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi rlm@1: or strcasecmp) rlm@1: If iCaseSenisivity = 0, case sensitivity is defaut of your operating system rlm@1: (like 1 on Unix, 2 on Windows) rlm@1: rlm@1: */ rlm@1: extern int ZEXPORT unzStringFileNameCompare(const char *fileName1, rlm@1: const char *fileName2, rlm@1: int iCaseSensitivity) rlm@1: { rlm@1: if (iCaseSensitivity == 0) rlm@1: iCaseSensitivity = CASESENSITIVITYDEFAULTVALUE; rlm@1: rlm@1: if (iCaseSensitivity == 1) rlm@1: return strcmp(fileName1, fileName2); rlm@1: rlm@1: return STRCMPCASENOSENTIVEFUNCTION(fileName1, fileName2); rlm@1: } rlm@1: rlm@1: #define BUFREADCOMMENT (0x400) rlm@1: rlm@1: /* rlm@1: Locate the Central directory of a zipfile (at the end, just before rlm@1: the global comment) rlm@1: */ rlm@1: local uLong unzlocal_SearchCentralDir(FILE *fin) rlm@1: { rlm@1: unsigned char*buf; rlm@1: uLong uSizeFile; rlm@1: uLong uBackRead; rlm@1: uLong uMaxBack = 0xffff; /* maximum size of global comment */ rlm@1: uLong uPosFound = 0; rlm@1: rlm@1: if (fseek(fin, 0, SEEK_END) != 0) rlm@1: return 0; rlm@1: rlm@1: uSizeFile = ftell(fin); rlm@1: rlm@1: if (uMaxBack > uSizeFile) rlm@1: uMaxBack = uSizeFile; rlm@1: rlm@1: buf = (unsigned char *)ALLOC(BUFREADCOMMENT+4); rlm@1: if (buf == NULL) rlm@1: return 0; rlm@1: rlm@1: uBackRead = 4; rlm@1: while (uBackRead < uMaxBack) rlm@1: { rlm@1: uLong uReadSize, uReadPos ; rlm@1: int i; rlm@1: if (uBackRead+BUFREADCOMMENT > uMaxBack) rlm@1: uBackRead = uMaxBack; rlm@1: else rlm@1: uBackRead += BUFREADCOMMENT; rlm@1: uReadPos = uSizeFile-uBackRead ; rlm@1: rlm@1: uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? rlm@1: (BUFREADCOMMENT+4) : (uSizeFile-uReadPos); rlm@1: if (fseek(fin, uReadPos, SEEK_SET) != 0) rlm@1: break; rlm@1: rlm@1: if (fread(buf, (uInt)uReadSize, 1, fin) != 1) rlm@1: break; rlm@1: rlm@1: for (i = (int)uReadSize-3; (i--) > 0;) rlm@1: if (((*(buf+i)) == 0x50) && ((*(buf+i+1)) == 0x4b) && rlm@1: ((*(buf+i+2)) == 0x05) && ((*(buf+i+3)) == 0x06)) rlm@1: { rlm@1: uPosFound = uReadPos+i; rlm@1: break; rlm@1: } rlm@1: rlm@1: if (uPosFound != 0) rlm@1: break; rlm@1: } rlm@1: TRYFREE(buf); rlm@1: return uPosFound; rlm@1: } rlm@1: rlm@1: /* rlm@1: Open a Zip file. path contain the full pathname (by example, rlm@1: on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer rlm@1: "zlib/zlib109.zip". rlm@1: If the zipfile cannot be opened (file don't exist or in not valid), the rlm@1: return value is NULL. rlm@1: Else, the return value is a unzFile Handle, usable with other function rlm@1: of this unzip package. rlm@1: */ rlm@1: extern unzFile ZEXPORT unzOpen(const char *path) rlm@1: { rlm@1: unz_s us; rlm@1: unz_s *s; rlm@1: uLong central_pos, uL; rlm@1: FILE * fin ; rlm@1: rlm@1: uLong number_disk; /* number of the current dist, used for rlm@1: spaning ZIP, unsupported, always 0*/ rlm@1: uLong number_disk_with_CD; /* number the the disk with central dir, used rlm@1: for spaning ZIP, unsupported, always 0*/ rlm@1: uLong number_entry_CD; /* total number of entries in rlm@1: the central dir rlm@1: (same than number_entry on nospan) */ rlm@1: rlm@1: int err = UNZ_OK; rlm@1: rlm@1: if (unz_copyright[0] != ' ') rlm@1: return NULL; rlm@1: rlm@1: fin = fopen(path, "rb"); rlm@1: if (fin == NULL) rlm@1: return NULL; rlm@1: rlm@1: central_pos = unzlocal_SearchCentralDir(fin); rlm@1: if (central_pos == 0) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (fseek(fin, central_pos, SEEK_SET) != 0) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: /* the signature, already checked */ rlm@1: if (unzlocal_getLong(fin, &uL) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: /* number of this disk */ rlm@1: if (unzlocal_getShort(fin, &number_disk) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: /* number of the disk with the start of the central directory */ rlm@1: if (unzlocal_getShort(fin, &number_disk_with_CD) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: /* total number of entries in the central dir on this disk */ rlm@1: if (unzlocal_getShort(fin, &us.gi.number_entry) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: /* total number of entries in the central dir */ rlm@1: if (unzlocal_getShort(fin, &number_entry_CD) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if ((number_entry_CD != us.gi.number_entry) || rlm@1: (number_disk_with_CD != 0) || rlm@1: (number_disk != 0)) rlm@1: err = UNZ_BADZIPFILE; rlm@1: rlm@1: /* size of the central directory */ rlm@1: if (unzlocal_getLong(fin, &us.size_central_dir) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: /* offset of start of central directory with respect to the rlm@1: starting disk number */ rlm@1: if (unzlocal_getLong(fin, &us.offset_central_dir) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: /* zipfile comment length */ rlm@1: if (unzlocal_getShort(fin, &us.gi.size_comment) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if ((central_pos < us.offset_central_dir+us.size_central_dir) && rlm@1: (err == UNZ_OK)) rlm@1: err = UNZ_BADZIPFILE; rlm@1: rlm@1: if (err != UNZ_OK) rlm@1: { rlm@1: fclose(fin); rlm@1: return NULL; rlm@1: } rlm@1: rlm@1: us.file = fin; rlm@1: us.byte_before_the_zipfile = central_pos - rlm@1: (us.offset_central_dir+us.size_central_dir); rlm@1: us.central_pos = central_pos; rlm@1: us.pfile_in_zip_read = NULL; rlm@1: rlm@1: s = (unz_s *)ALLOC(sizeof(unz_s)); rlm@1: *s = us; rlm@1: unzGoToFirstFile((unzFile)s); rlm@1: return (unzFile)s; rlm@1: } rlm@1: rlm@1: /* rlm@1: Close a ZipFile opened with unzipOpen. rlm@1: If there is files inside the .Zip opened with unzipOpenCurrentFile (see later), rlm@1: these files MUST be closed with unzipCloseCurrentFile before call unzipClose. rlm@1: return UNZ_OK if there is no problem. */ rlm@1: extern int ZEXPORT unzClose(unzFile file) rlm@1: { rlm@1: unz_s*s; rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: rlm@1: if (s->pfile_in_zip_read != NULL) rlm@1: unzCloseCurrentFile(file); rlm@1: rlm@1: fclose(s->file); rlm@1: TRYFREE(s); rlm@1: return UNZ_OK; rlm@1: } rlm@1: rlm@1: /* rlm@1: Write info about the ZipFile in the *pglobal_info structure. rlm@1: No preparation of the structure is needed rlm@1: return UNZ_OK if there is no problem. */ rlm@1: extern int ZEXPORT unzGetGlobalInfo(unzFile file, rlm@1: unz_global_info *pglobal_info) rlm@1: { rlm@1: unz_s*s; rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: *pglobal_info = s->gi; rlm@1: return UNZ_OK; rlm@1: } rlm@1: rlm@1: /* rlm@1: Translate date/time from Dos format to tm_unz (readable more easilty) rlm@1: */ rlm@1: local void unzlocal_DosDateToTmuDate(uLong ulDosDate, tm_unz *ptm) rlm@1: { rlm@1: uLong uDate; rlm@1: uDate = (uLong)(ulDosDate>>16); rlm@1: ptm->tm_mday = (uInt)(uDate&0x1f) ; rlm@1: ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ; rlm@1: ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ; rlm@1: rlm@1: ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800); rlm@1: ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ; rlm@1: ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ; rlm@1: } rlm@1: rlm@1: /* rlm@1: Get Info about the current file in the zipfile, with internal only info rlm@1: */ rlm@1: local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file, rlm@1: unz_file_info *pfile_info, rlm@1: unz_file_info_internal rlm@1: *pfile_info_internal, rlm@1: char *szFileName, rlm@1: uLong fileNameBufferSize, rlm@1: void *extraField, rlm@1: uLong extraFieldBufferSize, rlm@1: char *szComment, rlm@1: uLong commentBufferSize)); rlm@1: rlm@1: local int unzlocal_GetCurrentFileInfoInternal(unzFile file, rlm@1: unz_file_info *pfile_info, rlm@1: unz_file_info_internal *pfile_info_internal, rlm@1: char *szFileName, rlm@1: uLong fileNameBufferSize, rlm@1: void *extraField, rlm@1: uLong extraFieldBufferSize, rlm@1: char *szComment, rlm@1: uLong commentBufferSize) rlm@1: { rlm@1: unz_s* s; rlm@1: unz_file_info file_info; rlm@1: unz_file_info_internal file_info_internal; rlm@1: int err = UNZ_OK; rlm@1: uLong uMagic; rlm@1: long lSeek = 0; rlm@1: rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: if (fseek(s->file, s->pos_in_central_dir+s->byte_before_the_zipfile, SEEK_SET) != 0) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: /* we check the magic */ rlm@1: if (err == UNZ_OK) rlm@1: if (unzlocal_getLong(s->file, &uMagic) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: else if (uMagic != 0x02014b50) rlm@1: err = UNZ_BADZIPFILE; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &file_info.version) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &file_info.version_needed) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &file_info.flag) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &file_info.compression_method) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getLong(s->file, &file_info.dosDate) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: unzlocal_DosDateToTmuDate(file_info.dosDate, &file_info.tmu_date); rlm@1: rlm@1: if (unzlocal_getLong(s->file, &file_info.crc) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getLong(s->file, &file_info.compressed_size) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getLong(s->file, &file_info.uncompressed_size) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &file_info.size_filename) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &file_info.size_file_extra) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &file_info.size_file_comment) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &file_info.disk_num_start) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &file_info.internal_fa) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getLong(s->file, &file_info.external_fa) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getLong(s->file, &file_info_internal.offset_curfile) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: lSeek += file_info.size_filename; rlm@1: if ((err == UNZ_OK) && (szFileName != NULL)) rlm@1: { rlm@1: uLong uSizeRead ; rlm@1: if (file_info.size_filename < fileNameBufferSize) rlm@1: { rlm@1: *(szFileName+file_info.size_filename) = '\0'; rlm@1: uSizeRead = file_info.size_filename; rlm@1: } rlm@1: else rlm@1: uSizeRead = fileNameBufferSize; rlm@1: rlm@1: if ((file_info.size_filename > 0) && (fileNameBufferSize > 0)) rlm@1: if (fread(szFileName, (uInt)uSizeRead, 1, s->file) != 1) rlm@1: err = UNZ_ERRNO; rlm@1: lSeek -= uSizeRead; rlm@1: } rlm@1: rlm@1: if ((err == UNZ_OK) && (extraField != NULL)) rlm@1: { rlm@1: uLong uSizeRead ; rlm@1: if (file_info.size_file_extra < extraFieldBufferSize) rlm@1: uSizeRead = file_info.size_file_extra; rlm@1: else rlm@1: uSizeRead = extraFieldBufferSize; rlm@1: rlm@1: if (lSeek != 0) rlm@1: if (fseek(s->file, lSeek, SEEK_CUR) == 0) rlm@1: lSeek = 0; rlm@1: else rlm@1: err = UNZ_ERRNO; rlm@1: if ((file_info.size_file_extra > 0) && (extraFieldBufferSize > 0)) rlm@1: if (fread(extraField, (uInt)uSizeRead, 1, s->file) != 1) rlm@1: err = UNZ_ERRNO; rlm@1: lSeek += file_info.size_file_extra - uSizeRead; rlm@1: } rlm@1: else rlm@1: lSeek += file_info.size_file_extra; rlm@1: rlm@1: if ((err == UNZ_OK) && (szComment != NULL)) rlm@1: { rlm@1: uLong uSizeRead ; rlm@1: if (file_info.size_file_comment < commentBufferSize) rlm@1: { rlm@1: *(szComment+file_info.size_file_comment) = '\0'; rlm@1: uSizeRead = file_info.size_file_comment; rlm@1: } rlm@1: else rlm@1: uSizeRead = commentBufferSize; rlm@1: rlm@1: if (lSeek != 0) rlm@1: if (fseek(s->file, lSeek, SEEK_CUR) == 0) rlm@1: lSeek = 0; rlm@1: else rlm@1: err = UNZ_ERRNO; rlm@1: if ((file_info.size_file_comment > 0) && (commentBufferSize > 0)) rlm@1: if (fread(szComment, (uInt)uSizeRead, 1, s->file) != 1) rlm@1: err = UNZ_ERRNO; rlm@1: lSeek += file_info.size_file_comment - uSizeRead; rlm@1: } rlm@1: else rlm@1: lSeek += file_info.size_file_comment; rlm@1: rlm@1: if ((err == UNZ_OK) && (pfile_info != NULL)) rlm@1: *pfile_info = file_info; rlm@1: rlm@1: if ((err == UNZ_OK) && (pfile_info_internal != NULL)) rlm@1: *pfile_info_internal = file_info_internal; rlm@1: rlm@1: return err; rlm@1: } rlm@1: rlm@1: /* rlm@1: Write info about the ZipFile in the *pglobal_info structure. rlm@1: No preparation of the structure is needed rlm@1: return UNZ_OK if there is no problem. rlm@1: */ rlm@1: extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, rlm@1: unz_file_info *pfile_info, rlm@1: char *szFileName, rlm@1: uLong fileNameBufferSize, rlm@1: void *extraField, rlm@1: uLong extraFieldBufferSize, rlm@1: char *szComment, rlm@1: uLong commentBufferSize) rlm@1: { rlm@1: return unzlocal_GetCurrentFileInfoInternal(file, pfile_info, NULL, rlm@1: szFileName, fileNameBufferSize, rlm@1: extraField, extraFieldBufferSize, rlm@1: szComment, commentBufferSize); rlm@1: } rlm@1: rlm@1: /* rlm@1: Set the current file of the zipfile to the first file. rlm@1: return UNZ_OK if there is no problem rlm@1: */ rlm@1: extern int ZEXPORT unzGoToFirstFile(unzFile file) rlm@1: { rlm@1: int err = UNZ_OK; rlm@1: unz_s*s; rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: s->pos_in_central_dir = s->offset_central_dir; rlm@1: s->num_file = 0; rlm@1: err = unzlocal_GetCurrentFileInfoInternal(file, &s->cur_file_info, rlm@1: &s->cur_file_info_internal, rlm@1: NULL, 0, NULL, 0, NULL, 0); rlm@1: s->current_file_ok = (err == UNZ_OK); rlm@1: return err; rlm@1: } rlm@1: rlm@1: /* rlm@1: Set the current file of the zipfile to the next file. rlm@1: return UNZ_OK if there is no problem rlm@1: return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. rlm@1: */ rlm@1: extern int ZEXPORT unzGoToNextFile(unzFile file) rlm@1: { rlm@1: unz_s*s; rlm@1: int err; rlm@1: rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: if (!s->current_file_ok) rlm@1: return UNZ_END_OF_LIST_OF_FILE; rlm@1: if (s->num_file+1 == s->gi.number_entry) rlm@1: return UNZ_END_OF_LIST_OF_FILE; rlm@1: rlm@1: s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + rlm@1: s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ; rlm@1: s->num_file++; rlm@1: err = unzlocal_GetCurrentFileInfoInternal(file, &s->cur_file_info, rlm@1: &s->cur_file_info_internal, rlm@1: NULL, 0, NULL, 0, NULL, 0); rlm@1: s->current_file_ok = (err == UNZ_OK); rlm@1: return err; rlm@1: } rlm@1: rlm@1: /* rlm@1: Try locate the file szFileName in the zipfile. rlm@1: For the iCaseSensitivity signification, see unzipStringFileNameCompare rlm@1: rlm@1: return value : rlm@1: UNZ_OK if the file is found. It becomes the current file. rlm@1: UNZ_END_OF_LIST_OF_FILE if the file is not found rlm@1: */ rlm@1: extern int ZEXPORT unzLocateFile(unzFile file, rlm@1: const char *szFileName, rlm@1: int iCaseSensitivity) rlm@1: { rlm@1: unz_s*s; rlm@1: int err; rlm@1: rlm@1: uLong num_fileSaved; rlm@1: uLong pos_in_central_dirSaved; rlm@1: rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: rlm@1: if (strlen(szFileName) >= UNZ_MAXFILENAMEINZIP) rlm@1: return UNZ_PARAMERROR; rlm@1: rlm@1: s = (unz_s *)file; rlm@1: if (!s->current_file_ok) rlm@1: return UNZ_END_OF_LIST_OF_FILE; rlm@1: rlm@1: num_fileSaved = s->num_file; rlm@1: pos_in_central_dirSaved = s->pos_in_central_dir; rlm@1: rlm@1: err = unzGoToFirstFile(file); rlm@1: rlm@1: while (err == UNZ_OK) rlm@1: { rlm@1: char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1]; rlm@1: unzGetCurrentFileInfo(file, NULL, rlm@1: szCurrentFileName, sizeof(szCurrentFileName)-1, rlm@1: NULL, 0, NULL, 0); rlm@1: if (unzStringFileNameCompare(szCurrentFileName, rlm@1: szFileName, iCaseSensitivity) == 0) rlm@1: return UNZ_OK; rlm@1: err = unzGoToNextFile(file); rlm@1: } rlm@1: rlm@1: s->num_file = num_fileSaved ; rlm@1: s->pos_in_central_dir = pos_in_central_dirSaved ; rlm@1: return err; rlm@1: } rlm@1: rlm@1: /* rlm@1: Read the local header of the current zipfile rlm@1: Check the coherency of the local header and info in the end of central rlm@1: directory about this file rlm@1: store in *piSizeVar the size of extra info in local header rlm@1: (filename and size of extra field data) rlm@1: */ rlm@1: local int unzlocal_CheckCurrentFileCoherencyHeader(unz_s *s, rlm@1: uInt *piSizeVar, rlm@1: uLong *poffset_local_extrafield, rlm@1: uInt *psize_local_extrafield) rlm@1: { rlm@1: uLong uMagic, uData, uFlags; rlm@1: uLong size_filename; rlm@1: uLong size_extra_field; rlm@1: int err = UNZ_OK; rlm@1: rlm@1: *piSizeVar = 0; rlm@1: *poffset_local_extrafield = 0; rlm@1: *psize_local_extrafield = 0; rlm@1: rlm@1: if (fseek(s->file, s->cur_file_info_internal.offset_curfile + rlm@1: s->byte_before_the_zipfile, SEEK_SET) != 0) rlm@1: return UNZ_ERRNO; rlm@1: rlm@1: if (err == UNZ_OK) rlm@1: if (unzlocal_getLong(s->file, &uMagic) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: else if (uMagic != 0x04034b50) rlm@1: err = UNZ_BADZIPFILE; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &uData) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: /* rlm@1: else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion)) rlm@1: err=UNZ_BADZIPFILE; rlm@1: */ rlm@1: if (unzlocal_getShort(s->file, &uFlags) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &uData) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: else if ((err == UNZ_OK) && (uData != s->cur_file_info.compression_method)) rlm@1: err = UNZ_BADZIPFILE; rlm@1: rlm@1: if ((err == UNZ_OK) && (s->cur_file_info.compression_method != 0) && rlm@1: (s->cur_file_info.compression_method != Z_DEFLATED)) rlm@1: err = UNZ_BADZIPFILE; rlm@1: rlm@1: if (unzlocal_getLong(s->file, &uData) != UNZ_OK) /* date/time */ rlm@1: err = UNZ_ERRNO; rlm@1: rlm@1: if (unzlocal_getLong(s->file, &uData) != UNZ_OK) /* crc */ rlm@1: err = UNZ_ERRNO; rlm@1: else if ((err == UNZ_OK) && (uData != s->cur_file_info.crc) && rlm@1: ((uFlags & 8) == 0)) rlm@1: err = UNZ_BADZIPFILE; rlm@1: rlm@1: if (unzlocal_getLong(s->file, &uData) != UNZ_OK) /* size compr */ rlm@1: err = UNZ_ERRNO; rlm@1: else if ((err == UNZ_OK) && (uData != s->cur_file_info.compressed_size) && rlm@1: ((uFlags & 8) == 0)) rlm@1: err = UNZ_BADZIPFILE; rlm@1: rlm@1: if (unzlocal_getLong(s->file, &uData) != UNZ_OK) /* size uncompr */ rlm@1: err = UNZ_ERRNO; rlm@1: else if ((err == UNZ_OK) && (uData != s->cur_file_info.uncompressed_size) && rlm@1: ((uFlags & 8) == 0)) rlm@1: err = UNZ_BADZIPFILE; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &size_filename) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: else if ((err == UNZ_OK) && (size_filename != s->cur_file_info.size_filename)) rlm@1: err = UNZ_BADZIPFILE; rlm@1: rlm@1: *piSizeVar += (uInt)size_filename; rlm@1: rlm@1: if (unzlocal_getShort(s->file, &size_extra_field) != UNZ_OK) rlm@1: err = UNZ_ERRNO; rlm@1: *poffset_local_extrafield = s->cur_file_info_internal.offset_curfile + rlm@1: SIZEZIPLOCALHEADER + size_filename; rlm@1: *psize_local_extrafield = (uInt)size_extra_field; rlm@1: rlm@1: *piSizeVar += (uInt)size_extra_field; rlm@1: rlm@1: return err; rlm@1: } rlm@1: rlm@1: /* rlm@1: Open for reading data the current file in the zipfile. rlm@1: If there is no error and the file is opened, the return value is UNZ_OK. rlm@1: */ rlm@1: extern int ZEXPORT unzOpenCurrentFile(unzFile file) rlm@1: { rlm@1: int err = UNZ_OK; rlm@1: int Store; rlm@1: uInt iSizeVar; rlm@1: unz_s*s; rlm@1: file_in_zip_read_info_s*pfile_in_zip_read_info; rlm@1: uLong offset_local_extrafield; /* offset of the local extra field */ rlm@1: uInt size_local_extrafield; /* size of the local extra field */ rlm@1: rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: if (!s->current_file_ok) rlm@1: return UNZ_PARAMERROR; rlm@1: rlm@1: if (s->pfile_in_zip_read != NULL) rlm@1: unzCloseCurrentFile(file); rlm@1: rlm@1: if (unzlocal_CheckCurrentFileCoherencyHeader(s, &iSizeVar, rlm@1: &offset_local_extrafield, &size_local_extrafield) != UNZ_OK) rlm@1: return UNZ_BADZIPFILE; rlm@1: rlm@1: pfile_in_zip_read_info = (file_in_zip_read_info_s *) rlm@1: ALLOC(sizeof(file_in_zip_read_info_s)); rlm@1: if (pfile_in_zip_read_info == NULL) rlm@1: return UNZ_INTERNALERROR; rlm@1: rlm@1: pfile_in_zip_read_info->read_buffer = (char *)ALLOC(UNZ_BUFSIZE); rlm@1: pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield; rlm@1: pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield; rlm@1: pfile_in_zip_read_info->pos_local_extrafield = 0; rlm@1: rlm@1: if (pfile_in_zip_read_info->read_buffer == NULL) rlm@1: { rlm@1: TRYFREE(pfile_in_zip_read_info); rlm@1: return UNZ_INTERNALERROR; rlm@1: } rlm@1: rlm@1: pfile_in_zip_read_info->stream_initialised = 0; rlm@1: rlm@1: if ((s->cur_file_info.compression_method != 0) && rlm@1: (s->cur_file_info.compression_method != Z_DEFLATED)) rlm@1: err = UNZ_BADZIPFILE; rlm@1: Store = s->cur_file_info.compression_method == 0; rlm@1: rlm@1: pfile_in_zip_read_info->crc32_wait = s->cur_file_info.crc; rlm@1: pfile_in_zip_read_info->crc32 = 0; rlm@1: pfile_in_zip_read_info->compression_method = rlm@1: s->cur_file_info.compression_method; rlm@1: pfile_in_zip_read_info->file = s->file; rlm@1: pfile_in_zip_read_info->byte_before_the_zipfile = s->byte_before_the_zipfile; rlm@1: rlm@1: pfile_in_zip_read_info->stream.total_out = 0; rlm@1: rlm@1: if (!Store) rlm@1: { rlm@1: pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; rlm@1: pfile_in_zip_read_info->stream.zfree = (free_func)0; rlm@1: pfile_in_zip_read_info->stream.opaque = (voidpf)0; rlm@1: rlm@1: err = inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS); rlm@1: if (err == Z_OK) rlm@1: pfile_in_zip_read_info->stream_initialised = 1; rlm@1: /* windowBits is passed < 0 to tell that there is no zlib header. rlm@1: * Note that in this case inflate *requires* an extra "dummy" byte rlm@1: * after the compressed stream in order to complete decompression and rlm@1: * return Z_STREAM_END. rlm@1: * In unzip, i don't wait absolutely Z_STREAM_END because I known the rlm@1: * size of both compressed and uncompressed data rlm@1: */ rlm@1: } rlm@1: pfile_in_zip_read_info->rest_read_compressed = rlm@1: s->cur_file_info.compressed_size ; rlm@1: pfile_in_zip_read_info->rest_read_uncompressed = rlm@1: s->cur_file_info.uncompressed_size ; rlm@1: rlm@1: pfile_in_zip_read_info->pos_in_zipfile = rlm@1: s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + rlm@1: iSizeVar; rlm@1: rlm@1: pfile_in_zip_read_info->stream.avail_in = (uInt)0; rlm@1: rlm@1: s->pfile_in_zip_read = pfile_in_zip_read_info; rlm@1: return UNZ_OK; rlm@1: } rlm@1: rlm@1: /* rlm@1: Read bytes from the current file. rlm@1: buf contain buffer where data must be copied rlm@1: len the size of buf. rlm@1: rlm@1: return the number of byte copied if somes bytes are copied rlm@1: return 0 if the end of file was reached rlm@1: return <0 with error code if there is an error rlm@1: (UNZ_ERRNO for IO error, or zLib error for uncompress error) rlm@1: */ rlm@1: extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len) rlm@1: { rlm@1: int err = UNZ_OK; rlm@1: uInt iRead = 0; rlm@1: unz_s*s; rlm@1: file_in_zip_read_info_s*pfile_in_zip_read_info; rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: pfile_in_zip_read_info = s->pfile_in_zip_read; rlm@1: rlm@1: if (pfile_in_zip_read_info == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: rlm@1: if ((pfile_in_zip_read_info->read_buffer == NULL)) rlm@1: return UNZ_END_OF_LIST_OF_FILE; rlm@1: if (len == 0) rlm@1: return 0; rlm@1: rlm@1: pfile_in_zip_read_info->stream.next_out = (Bytef *)buf; rlm@1: rlm@1: pfile_in_zip_read_info->stream.avail_out = (uInt)len; rlm@1: rlm@1: if (len > pfile_in_zip_read_info->rest_read_uncompressed) rlm@1: pfile_in_zip_read_info->stream.avail_out = rlm@1: (uInt)pfile_in_zip_read_info->rest_read_uncompressed; rlm@1: rlm@1: while (pfile_in_zip_read_info->stream.avail_out > 0) rlm@1: { rlm@1: if ((pfile_in_zip_read_info->stream.avail_in == 0) && rlm@1: (pfile_in_zip_read_info->rest_read_compressed > 0)) rlm@1: { rlm@1: uInt uReadThis = UNZ_BUFSIZE; rlm@1: if (pfile_in_zip_read_info->rest_read_compressed < uReadThis) rlm@1: uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed; rlm@1: if (uReadThis == 0) rlm@1: return UNZ_EOF; rlm@1: if (fseek(pfile_in_zip_read_info->file, rlm@1: pfile_in_zip_read_info->pos_in_zipfile + rlm@1: pfile_in_zip_read_info->byte_before_the_zipfile, SEEK_SET) != 0) rlm@1: return UNZ_ERRNO; rlm@1: if (fread(pfile_in_zip_read_info->read_buffer, uReadThis, 1, rlm@1: pfile_in_zip_read_info->file) != 1) rlm@1: return UNZ_ERRNO; rlm@1: pfile_in_zip_read_info->pos_in_zipfile += uReadThis; rlm@1: rlm@1: pfile_in_zip_read_info->rest_read_compressed -= uReadThis; rlm@1: rlm@1: pfile_in_zip_read_info->stream.next_in = rlm@1: (Bytef *)pfile_in_zip_read_info->read_buffer; rlm@1: pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis; rlm@1: } rlm@1: rlm@1: if (pfile_in_zip_read_info->compression_method == 0) rlm@1: { rlm@1: uInt uDoCopy, i ; rlm@1: if (pfile_in_zip_read_info->stream.avail_out < rlm@1: pfile_in_zip_read_info->stream.avail_in) rlm@1: uDoCopy = pfile_in_zip_read_info->stream.avail_out ; rlm@1: else rlm@1: uDoCopy = pfile_in_zip_read_info->stream.avail_in ; rlm@1: rlm@1: for (i = 0; i < uDoCopy; i++) rlm@1: *(pfile_in_zip_read_info->stream.next_out+i) = rlm@1: *(pfile_in_zip_read_info->stream.next_in+i); rlm@1: rlm@1: pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, rlm@1: pfile_in_zip_read_info->stream.next_out, rlm@1: uDoCopy); rlm@1: pfile_in_zip_read_info->rest_read_uncompressed -= uDoCopy; rlm@1: pfile_in_zip_read_info->stream.avail_in -= uDoCopy; rlm@1: pfile_in_zip_read_info->stream.avail_out -= uDoCopy; rlm@1: pfile_in_zip_read_info->stream.next_out += uDoCopy; rlm@1: pfile_in_zip_read_info->stream.next_in += uDoCopy; rlm@1: pfile_in_zip_read_info->stream.total_out += uDoCopy; rlm@1: iRead += uDoCopy; rlm@1: } rlm@1: else rlm@1: { rlm@1: uLong uTotalOutBefore, uTotalOutAfter; rlm@1: const Bytef *bufBefore; rlm@1: uLong uOutThis; rlm@1: int flush = Z_SYNC_FLUSH; rlm@1: rlm@1: uTotalOutBefore = pfile_in_zip_read_info->stream.total_out; rlm@1: bufBefore = pfile_in_zip_read_info->stream.next_out; rlm@1: rlm@1: /* rlm@1: if ((pfile_in_zip_read_info->rest_read_uncompressed == rlm@1: pfile_in_zip_read_info->stream.avail_out) && rlm@1: (pfile_in_zip_read_info->rest_read_compressed == 0)) rlm@1: flush = Z_FINISH; rlm@1: */ rlm@1: err = inflate(&pfile_in_zip_read_info->stream, flush); rlm@1: rlm@1: uTotalOutAfter = pfile_in_zip_read_info->stream.total_out; rlm@1: uOutThis = uTotalOutAfter-uTotalOutBefore; rlm@1: rlm@1: pfile_in_zip_read_info->crc32 = rlm@1: crc32(pfile_in_zip_read_info->crc32, bufBefore, rlm@1: (uInt)(uOutThis)); rlm@1: rlm@1: pfile_in_zip_read_info->rest_read_uncompressed -= rlm@1: uOutThis; rlm@1: rlm@1: iRead += (uInt)(uTotalOutAfter - uTotalOutBefore); rlm@1: rlm@1: if (err == Z_STREAM_END) rlm@1: return (iRead == 0) ? UNZ_EOF : iRead; rlm@1: if (err != Z_OK) rlm@1: break; rlm@1: } rlm@1: } rlm@1: rlm@1: if (err == Z_OK) rlm@1: return iRead; rlm@1: return err; rlm@1: } rlm@1: rlm@1: /* rlm@1: Give the current position in uncompressed data rlm@1: */ rlm@1: extern z_off_t ZEXPORT unztell(unzFile file) rlm@1: { rlm@1: unz_s*s; rlm@1: file_in_zip_read_info_s*pfile_in_zip_read_info; rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: pfile_in_zip_read_info = s->pfile_in_zip_read; rlm@1: rlm@1: if (pfile_in_zip_read_info == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: rlm@1: return (z_off_t)pfile_in_zip_read_info->stream.total_out; rlm@1: } rlm@1: rlm@1: /* rlm@1: return 1 if the end of file was reached, 0 elsewhere rlm@1: */ rlm@1: extern int ZEXPORT unzeof(unzFile file) rlm@1: { rlm@1: unz_s*s; rlm@1: file_in_zip_read_info_s*pfile_in_zip_read_info; rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: pfile_in_zip_read_info = s->pfile_in_zip_read; rlm@1: rlm@1: if (pfile_in_zip_read_info == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: rlm@1: if (pfile_in_zip_read_info->rest_read_uncompressed == 0) rlm@1: return 1; rlm@1: else rlm@1: return 0; rlm@1: } rlm@1: rlm@1: /* rlm@1: Read extra field from the current file (opened by unzOpenCurrentFile) rlm@1: This is the local-header version of the extra field (sometimes, there is rlm@1: more info in the local-header version than in the central-header) rlm@1: rlm@1: if buf==NULL, it return the size of the local extra field that can be read rlm@1: rlm@1: if buf!=NULL, len is the size of the buffer, the extra header is copied in rlm@1: buf. rlm@1: the return value is the number of bytes copied in buf, or (if <0) rlm@1: the error code rlm@1: */ rlm@1: extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, unsigned len) rlm@1: { rlm@1: unz_s*s; rlm@1: file_in_zip_read_info_s*pfile_in_zip_read_info; rlm@1: uInt read_now; rlm@1: uLong size_to_read; rlm@1: rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: pfile_in_zip_read_info = s->pfile_in_zip_read; rlm@1: rlm@1: if (pfile_in_zip_read_info == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: rlm@1: size_to_read = (pfile_in_zip_read_info->size_local_extrafield - rlm@1: pfile_in_zip_read_info->pos_local_extrafield); rlm@1: rlm@1: if (buf == NULL) rlm@1: return (int)size_to_read; rlm@1: rlm@1: if (len > size_to_read) rlm@1: read_now = (uInt)size_to_read; rlm@1: else rlm@1: read_now = (uInt)len ; rlm@1: rlm@1: if (read_now == 0) rlm@1: return 0; rlm@1: rlm@1: if (fseek(pfile_in_zip_read_info->file, rlm@1: pfile_in_zip_read_info->offset_local_extrafield + rlm@1: pfile_in_zip_read_info->pos_local_extrafield, SEEK_SET) != 0) rlm@1: return UNZ_ERRNO; rlm@1: rlm@1: if (fread(buf, (uInt)size_to_read, 1, pfile_in_zip_read_info->file) != 1) rlm@1: return UNZ_ERRNO; rlm@1: rlm@1: return (int)read_now; rlm@1: } rlm@1: rlm@1: /* rlm@1: Close the file in zip opened with unzipOpenCurrentFile rlm@1: Return UNZ_CRCERROR if all the file was read but the CRC is not good rlm@1: */ rlm@1: extern int ZEXPORT unzCloseCurrentFile(unzFile file) rlm@1: { rlm@1: int err = UNZ_OK; rlm@1: rlm@1: unz_s*s; rlm@1: file_in_zip_read_info_s*pfile_in_zip_read_info; rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: pfile_in_zip_read_info = s->pfile_in_zip_read; rlm@1: rlm@1: if (pfile_in_zip_read_info == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: rlm@1: if (pfile_in_zip_read_info->rest_read_uncompressed == 0) rlm@1: { rlm@1: if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait) rlm@1: err = UNZ_CRCERROR; rlm@1: } rlm@1: rlm@1: TRYFREE(pfile_in_zip_read_info->read_buffer); rlm@1: pfile_in_zip_read_info->read_buffer = NULL; rlm@1: if (pfile_in_zip_read_info->stream_initialised) rlm@1: inflateEnd(&pfile_in_zip_read_info->stream); rlm@1: rlm@1: pfile_in_zip_read_info->stream_initialised = 0; rlm@1: TRYFREE(pfile_in_zip_read_info); rlm@1: rlm@1: s->pfile_in_zip_read = NULL; rlm@1: rlm@1: return err; rlm@1: } rlm@1: rlm@1: /* rlm@1: Get the global comment string of the ZipFile, in the szComment buffer. rlm@1: uSizeBuf is the size of the szComment buffer. rlm@1: return the number of byte copied or an error code <0 rlm@1: */ rlm@1: extern int ZEXPORT unzGetGlobalComment(unzFile file, rlm@1: char *szComment, rlm@1: uLong uSizeBuf) rlm@1: { rlm@1: //int err=UNZ_OK; rlm@1: unz_s*s; rlm@1: uLong uReadThis ; rlm@1: if (file == NULL) rlm@1: return UNZ_PARAMERROR; rlm@1: s = (unz_s *)file; rlm@1: rlm@1: uReadThis = uSizeBuf; rlm@1: if (uReadThis > s->gi.size_comment) rlm@1: uReadThis = s->gi.size_comment; rlm@1: rlm@1: if (fseek(s->file, s->central_pos+22, SEEK_SET) != 0) rlm@1: return UNZ_ERRNO; rlm@1: rlm@1: if (uReadThis > 0) rlm@1: { rlm@1: *szComment = '\0'; rlm@1: if (fread(szComment, (uInt)uReadThis, 1, s->file) != 1) rlm@1: return UNZ_ERRNO; rlm@1: } rlm@1: rlm@1: if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment)) rlm@1: *(szComment+s->gi.size_comment) = '\0'; rlm@1: return (int)uReadThis; rlm@1: } rlm@1: