int zfile_zuncompress (void *dst, int dstsize, struct zfile *src, int srcsize) { z_stream zs; int v; uae_u8 inbuf[4096]; int incnt; if (!zlib_test ()) return 0; memset (&zs, 0, sizeof(zs)); if (inflateInit (&zs) != Z_OK) return 0; zs.next_out = dst; zs.avail_out = dstsize; incnt = 0; v = Z_OK; while (v == Z_OK && zs.avail_out > 0) { if (zs.avail_in == 0) { int left = srcsize - incnt; if (left == 0) break; if (left > (int)sizeof (inbuf)) left = sizeof (inbuf); zs.next_in = inbuf; zs.avail_in = zfile_fread (inbuf, 1, left, src); incnt += left; } v = inflate (&zs, 0); } inflateEnd (&zs); return 0; }
int main(int argc, char *argv[]) { zlib_test(argc, argv); if(!glfwInit()) { exit(EXIT_FAILURE); } int width = 320; int height = 240; if(!glfwOpenWindow(width, height, 0, 0, 0, 0, 0, 0, GLFW_WINDOW)) { glfwTerminate(); exit(EXIT_FAILURE); } GLenum err = glewInit(); if(GLEW_OK != err) { /* Problem: glewInit failed, something is seriously wrong. */ fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); glfwTerminate(); exit(EXIT_FAILURE); } fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); ::testing::InitGoogleTest(&argc, argv); int result = RUN_ALL_TESTS(); glfwTerminate(); getchar(); return 0; }
static struct zfile *zuncompress (struct zfile *z) { char *name = z->name; char *ext = strrchr (name, '.'); uae_u8 header[4]; if (ext != NULL) { ext++; if (strcasecmp (ext, "zip") == 0 && zlib_test ()) return unzip (z); if (strcasecmp (ext, "gz") == 0) return gunzip (z); if (strcasecmp (ext, "adz") == 0) // thinkp adz-file support return unzip (z); // thinkp adz-file support if (strcasecmp (ext, "roz") == 0) return gunzip (z); if (strcasecmp (ext, "dms") == 0) return dms (z); if (strcasecmp (ext, "lha") == 0 || strcasecmp (ext, "lzh") == 0) return lha (z); memset (header, 0, sizeof (header)); zfile_fseek (z, 0, SEEK_SET); zfile_fread (header, sizeof (header), 1, z); zfile_fseek (z, 0, SEEK_SET); if (header[0] == 0x1f && header[1] == 0x8b) return gunzip (z); if (header[0] == 'P' && header[1] == 'K') return unzip (z); if (header[0] == 'D' && header[1] == 'M' && header[2] == 'S' && header[3] == '!') return dms (z); } return z; }
static struct zfile *unzip (struct zfile *z) { unzFile uz; unz_file_info file_info; char filename_inzip[2048]; struct zfile *zf; unsigned int err, zipcnt, i, we_have_file = 0; int select; char tmphist[MAX_DPATH]; int first = 1; if (!zlib_test ()) return z; zf = 0; uz = unzOpen (z); if (!uz) return z; if (unzGoToFirstFile (uz) != UNZ_OK) return z; zipcnt = 1; tmphist[0] = 0; for (;;) { err = unzGetCurrentFileInfo(uz,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); if (err != UNZ_OK) return z; if (file_info.uncompressed_size > 0) { i = 0; while (ignoreextensions[i]) { if (strlen(filename_inzip) > strlen (ignoreextensions[i]) && !strcasecmp (ignoreextensions[i], filename_inzip + strlen (filename_inzip) - strlen (ignoreextensions[i]))) break; i++; } if (!ignoreextensions[i]) { if (tmphist[0]) { DISK_history_add (tmphist, -1); tmphist[0] = 0; first = 0; } if (first) { if (isdiskimage (filename_inzip)) sprintf (tmphist,"%s/%s", z->name, filename_inzip); } else { sprintf (tmphist,"%s/%s", z->name, filename_inzip); DISK_history_add (tmphist, -1); tmphist[0] = 0; } select = 0; if (!z->zipname) select = 1; if (z->zipname && !strcasecmp (z->zipname, filename_inzip)) select = -1; if (z->zipname && z->zipname[0] == '#' && atol (z->zipname + 1) == (int)zipcnt) select = -1; if (select && !we_have_file) { unsigned int err = unzOpenCurrentFile (uz); if (err == UNZ_OK) { zf = zfile_fopen_empty (filename_inzip, file_info.uncompressed_size); if (zf) { err = unzReadCurrentFile (uz, zf->data, file_info.uncompressed_size); unzCloseCurrentFile (uz); if (err == 0 || err == file_info.uncompressed_size) { zf = zuncompress (zf); if (select < 0 || zfile_gettype (zf)) { we_have_file = 1; } } } if (!we_have_file) { zfile_fclose (zf); zf = 0; } } } } } zipcnt++; err = unzGoToNextFile (uz); if (err != UNZ_OK) break; } if (zf) { zfile_fclose (z); z = zf; } return z; }
static struct zfile *gunzip (struct zfile *z) { uae_u8 header[2 + 1 + 1 + 4 + 1 + 1]; z_stream zs; int i, size, ret, first; uae_u8 flags; long offset; char name[MAX_DPATH]; uae_u8 buffer[8192]; struct zfile *z2; uae_u8 b; if (!zlib_test ()) return z; strcpy (name, z->name); memset (&zs, 0, sizeof (zs)); memset (header, 0, sizeof (header)); zfile_fread (header, sizeof (header), 1, z); flags = header[3]; if (header[0] != 0x1f && header[1] != 0x8b) return z; if (flags & 2) /* multipart not supported */ return z; if (flags & 32) /* encryption not supported */ return z; if (flags & 4) { /* skip extra field */ zfile_fread (&b, 1, 1, z); size = b; zfile_fread (&b, 1, 1, z); size |= b << 8; zfile_fseek (z, size + 2, SEEK_CUR); } if (flags & 8) { /* get original file name */ i = 0; do { zfile_fread (name + i, 1, 1, z); } while (name[i++]); } if (flags & 16) { /* skip comment */ i = 0; do { zfile_fread (&b, 1, 1, z); } while (b); } offset = zfile_ftell (z); zfile_fseek (z, -4, SEEK_END); zfile_fread (&b, 1, 1, z); size = b; zfile_fread (&b, 1, 1, z); size |= b << 8; zfile_fread (&b, 1, 1, z); size |= b << 16; zfile_fread (&b, 1, 1, z); size |= b << 24; if (size < 8 || size > 10000000) /* safety check */ return z; zfile_fseek (z, offset, SEEK_SET); z2 = zfile_fopen_empty (name, size); if (!z2) return z; zs.next_out = z2->data; zs.avail_out = size; first = 1; do { zs.next_in = buffer; zs.avail_in = sizeof (buffer); zfile_fread (buffer, sizeof (buffer), 1, z); if (first) { if (inflateInit2 (&zs, -MAX_WBITS) != Z_OK) break; first = 0; } ret = inflate (&zs, 0); } while (ret == Z_OK); inflateEnd (&zs); if (ret != Z_STREAM_END || first != 0) { zfile_fclose (z2); return z; } zfile_fclose (z); return z2; }