void ZipFileFree(ZipFile* file) { if(file->data) FreePatch(file->data); if(file) FreePatch(file); }
static unsigned long ZipLocateCentralDir(FILE *file) { unsigned char* buf; unsigned long usizefile; unsigned long ubackread; unsigned long umaxback = 0xffff; unsigned long uposfound = 0; if(fseek(file, 0, SEEK_END) != 0) return 0; usizefile = ftell(file); if(umaxback > usizefile) umaxback = usizefile; buf = (unsigned char*)MallocPatch(_ZIP_BUF_READ_COMMENT + 4); if(buf == NULL) return 0; ubackread = 4; while(ubackread < umaxback) { unsigned long ureadsize, ureadpos; int i; if(ubackread + _ZIP_BUF_READ_COMMENT > umaxback) ubackread = umaxback; else ubackread += _ZIP_BUF_READ_COMMENT; ureadpos = usizefile - ubackread; ureadsize = ((_ZIP_BUF_READ_COMMENT + 4) < (usizefile - ureadpos)) ? (_ZIP_BUF_READ_COMMENT+ 4) : (usizefile - ureadpos); if(fseek(file, ureadpos, SEEK_SET) != 0) break; if(fread(buf, (unsigned int)ureadsize, 1, file) != 1) break; for(i = (int)ureadsize - 3; (i--) > 0;) if(((*(buf + i)) == 0x50) && ((*(buf + i + 1)) == 0x4b) && ((*(buf + i + 2)) == 0x05) && ((*(buf + i + 3)) == 0x06)) { uposfound = ureadpos + i; break; } if (uposfound != 0) break; } FreePatch(buf); return uposfound; }
void TerrainPager::FreePatch(const PatchID &patchID) { if (patchID.GetI() < 0 || patchID.GetJ() < 0) // reject call return; // Check the working set to see if it is loaded Patch *pPatch = findPatch(patchID); assert(NULL != pPatch); FreePatch(pPatch); }
static int ZipCloseCurrentFile(Zip* file) { int err = _ZIP_OK; _zip* s; zipFileInfoInternal *pfileinzipreadinfo; if(file == NULL) return _ZIP_PARAM_ERROR; s = (_zip*)file; pfileinzipreadinfo = s->currentzipfileinfo; if(pfileinzipreadinfo == NULL) return _ZIP_PARAM_ERROR; if(pfileinzipreadinfo->restreaduncompressed == 0) { if(pfileinzipreadinfo->crc32 != pfileinzipreadinfo->crc32wait) err = _ZIP_CRC_ERROR; } FreePatch(pfileinzipreadinfo->buffer); pfileinzipreadinfo->buffer = NULL; if(pfileinzipreadinfo->streaminitialised) inflateEnd(&pfileinzipreadinfo->stream); pfileinzipreadinfo->streaminitialised = 0; FreePatch(pfileinzipreadinfo); s->currentzipfileinfo = NULL; return err; }
int ZipClose(Zip* zip) { _zip* s; if(zip == NULL) return 0; s = (_zip*)zip; if(s->currentzipfileinfo != NULL) ZipCloseCurrentFile(zip); fclose(s->file); FreePatch(s); return 1; }
static int ZipOpenCurrentFile(Zip* file, const char *password) { int err = _ZIP_OK; int store; unsigned int isizevar; _zip* s; zipFileInfoInternal* pfileinzipreadinfo; unsigned long localextrafieldoffset; unsigned int localextrafieldsize; char source[12]; if(file == NULL) return _ZIP_PARAM_ERROR; s = (_zip*)file; if(!s->currentfileok) return _ZIP_PARAM_ERROR; if(s->currentzipfileinfo != NULL) ZipCloseCurrentFile(file); if(ZipCheckCurrentFileCoherencyHeader(s, &isizevar, &localextrafieldoffset, &localextrafieldsize) != _ZIP_OK) return _ZIP_BAD_FILE; pfileinzipreadinfo = (zipFileInfoInternal*) MallocPatch(sizeof(zipFileInfoInternal)); if(pfileinzipreadinfo == NULL) return _ZIP_INTERNAL_ERROR; pfileinzipreadinfo->buffer = (char*)MallocPatch(_ZIP_BUFFER_SIZE); pfileinzipreadinfo->localextrafieldoffset = localextrafieldoffset; pfileinzipreadinfo->localextrafieldsize = localextrafieldsize; pfileinzipreadinfo->localextrafieldpos = 0; if(pfileinzipreadinfo->buffer == NULL) { FreePatch(pfileinzipreadinfo); return _ZIP_INTERNAL_ERROR; } pfileinzipreadinfo->streaminitialised = 0; if((s->currentfileinfo.compressionmethod != 0) && (s->currentfileinfo.compressionmethod != Z_DEFLATED)) err = _ZIP_BAD_FILE; store = s->currentfileinfo.compressionmethod == 0; pfileinzipreadinfo->crc32wait = s->currentfileinfo.crc; pfileinzipreadinfo->crc32 = 0; pfileinzipreadinfo->compressionmethod = s->currentfileinfo.compressionmethod; pfileinzipreadinfo->file = s->file; pfileinzipreadinfo->bytebeforezip = s->bytebeforezip; pfileinzipreadinfo->stream.total_out = 0; if(!store) { pfileinzipreadinfo->stream.zalloc = (alloc_func)0; pfileinzipreadinfo->stream.zfree = (free_func)0; pfileinzipreadinfo->stream.opaque = (voidpf)0; err = inflateInit2(&pfileinzipreadinfo->stream, -MAX_WBITS); if(err == Z_OK) pfileinzipreadinfo->streaminitialised = 1; } pfileinzipreadinfo->restreadcompressed = s->currentfileinfo.compressedsize; pfileinzipreadinfo->restreaduncompressed = s->currentfileinfo.uncompressedsize; pfileinzipreadinfo->posinzip = s->currentfileinfointernal.currentfileoffset + _ZIP_LOCALHEADER_SIZE + isizevar; pfileinzipreadinfo->stream.avail_in = (unsigned int)0; s->currentzipfileinfo = pfileinzipreadinfo; if(password != NULL) { int i; s->crc32tab = get_crc_table(); ZipInitKeys(password, s->keys, s->crc32tab); if(fseek(pfileinzipreadinfo->file, s->currentzipfileinfo->posinzip + s->currentzipfileinfo->bytebeforezip, SEEK_SET) != 0) { FreePatch(pfileinzipreadinfo->buffer); FreePatch(pfileinzipreadinfo); return _ZIP_INTERNAL_ERROR; } if(fread(source, 1, 12, pfileinzipreadinfo->file) < 12) { FreePatch(pfileinzipreadinfo->buffer); FreePatch(pfileinzipreadinfo); return _ZIP_INTERNAL_ERROR; } for(i = 0; i < 12; i++) zdecode(s->keys, s->crc32tab, source[i]); s->currentzipfileinfo->posinzip += 12; s->encrypted = 1; } return _ZIP_OK; }
ZipFile* ZipFileRead(Zip* zip, const char *filename, const char *password) { char filenameinzip[256]; int err = 0; ZipFile* zipfile = (ZipFile*) MallocPatch(sizeof(ZipFile)); if(!zipfile) return NULL; if(ZipLocateFile(zip, filename, 0) != 0) { FreePatch(zipfile); return NULL; } zipFileInfo fileinfo; err = ZitCurrentFileInfo(zip, &fileinfo, filenameinzip, sizeof(filenameinzip), NULL, 0, NULL, 0); if(err != 0) { printf("error %d with zipfile in ZitCurrentFileInfo\n", err); FreePatch(zipfile); return NULL; } err = ZipOpenCurrentFile(zip, password); if(err != 0) { printf("error %d with zipfile in ZipOpenCurrentFile\n", err); FreePatch(zipfile); return NULL; } zipfile->size = fileinfo.uncompressedsize; zipfile->data = (unsigned char*)MallocPatch(fileinfo.uncompressedsize); if(!zipfile->data) { printf("error allocating data for zipfile\n"); FreePatch(zipfile); return NULL; } unsigned int count = 0; err = 1; while(err > 0) { err = ZipReadCurrentFile(zip, &zipfile->data[count], fileinfo.uncompressedsize); if(err < 0) { printf("error %d with zipfile in ZipReadCurrentFile\n", err); break; } else count += err; } if(err == 0) { err = ZipCloseCurrentFile(zip); if(err != 0) { printf("error %d with zipfile in ZipCloseCurrentFile\n", err); FreePatch(zipfile->data); FreePatch(zipfile); return NULL; } return zipfile; } else { ZipCloseCurrentFile(zip); FreePatch(zipfile->data); FreePatch(zipfile); return NULL; } }
int ZipExtractCurrentFile(Zip *zip, int *nopath, const char *password) { //void(overwrite); char filenameinzip[256]; char *filenameWithoutPath; char *p; void *buffer; unsigned int buffersize = 64*1024; int err = 0; FILE *fout = NULL; zipFileInfo fileInfo; err = ZitCurrentFileInfo(zip, &fileInfo, filenameinzip, sizeof(filenameinzip), NULL, 0, NULL, 0); if(err != 0) { printf("error %d with zipfile in ZitCurrentFileInfo\n", err); return -1; } buffer = (void *)MallocPatch(buffersize); if(!buffer) { printf("Error allocating buffer\n"); return 0; } p = filenameWithoutPath = filenameinzip; while((*p) != '\0') { if(((*p) == '/') || ((*p) == '\\')) filenameWithoutPath = p + 1; p++; } if((*filenameWithoutPath) == '\0') { if((*nopath) == 0) { //printf("Creating directory: %s\n", filenameinzip); mkdir(filenameinzip, 0777); } } else { const char *writeFilename; if((*nopath) == 0) writeFilename = filenameinzip; else writeFilename = filenameWithoutPath; err = ZipOpenCurrentFile(zip, password); if(err != _ZIP_OK) printf("Error with zipfile in ZipOpenCurrentFile\n"); fout = fopen(writeFilename, "wb"); if((fout == NULL) && ((*nopath) == 0) && (filenameWithoutPath != (char *)filenameinzip)) { char c = *(filenameWithoutPath - 1); *(filenameWithoutPath - 1) = '\0'; mkdir(writeFilename, 0777); *(filenameWithoutPath - 1) = c; fout = fopen(writeFilename, "wb"); } if(fout == NULL) printf("Error opening file\n"); do { err = ZipReadCurrentFile(zip, buffer, buffersize); if(err < 0) { printf("Error with zipfile in ZipReadCurrentFile\n"); break; } if(err > 0) { fwrite(buffer, 1, err, fout); } } while (err > 0); fclose(fout); err = ZipCloseCurrentFile(zip); if(err != _ZIP_OK) printf("Error with zipfile in ZipCloseCurrentFile\n"); } if(buffer) FreePatch(buffer); return err; }