7 // Decompress a 'deflate' compressed buffer
\r
8 static int Inflate(unsigned char *dest,int destLen, unsigned char *src,int srcLen)
\r
12 memset(&stream,0,sizeof(stream));
\r
14 stream.next_in =src;
\r
15 stream.avail_in =srcLen;
\r
16 stream.next_out =dest;
\r
17 stream.avail_out=destLen;
\r
18 inflateInit2(&stream,-15);
\r
19 inflate(&stream,Z_FINISH);
\r
20 inflateEnd(&stream);
\r
25 static int Get32(unsigned char *src)
\r
27 return src[0] | (src[1]<<8) | (src[2]<<16) | (src[3]<<24);
\r
30 // --------------------------------------------------------------
\r
33 memset(this,0,sizeof(*this));
\r
36 int Unzip::gotoFirstFile()
\r
42 int Unzip::fileOpen()
\r
46 fseek(file,headerPos,SEEK_SET);
\r
48 // Read in file entry header:
\r
49 ret=fread(head,1,sizeof(head),file);
\r
50 if (ret!=sizeof(head)) return 1;
\r
53 if (head[0]=='P' && head[1]=='K' && head[2]==3 && head[3]==4) okay=1;
\r
54 if (okay==0) return 1;
\r
56 // Get compressed and uncompressed sizes:
\r
57 srcLen =Get32(head+0x12);
\r
58 dataLen=Get32(head+0x16);
\r
60 // Get size of name and extra fields:
\r
61 nameLen=Get32(head+0x1a);
\r
62 extraLen=nameLen>>16; nameLen&=0xffff;
\r
65 name=(char *)malloc(nameLen+1); if (name==NULL) return 1;
\r
66 memset(name,0,nameLen+1);
\r
67 fread(name,1,nameLen,file);
\r
69 // Find position of compressed data in the file
\r
70 compPos=headerPos+sizeof(head);
\r
71 compPos+=nameLen+extraLen;
\r
76 int Unzip::fileClose()
\r
78 free(name); name=NULL;
\r
80 // Go to next header:
\r
81 headerPos=compPos+srcLen;
\r
89 int Unzip::fileDecode(unsigned char *data)
\r
91 unsigned char *src=NULL;
\r
93 // Go to compressed data:
\r
94 fseek(file,compPos,SEEK_SET);
\r
97 src=(unsigned char *)malloc(srcLen);
\r
98 if (src==NULL) { fclose(file); return 1; }
\r
99 memset(src,0,srcLen);
\r
101 // Read in compressed version and decompress
\r
102 fread(src,1,srcLen,file);
\r
104 Inflate(data,dataLen, src,srcLen);
\r
106 free(src); src=NULL; srcLen=0;
\r