|
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // e32test\misc\unzip.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #include "unzip.h" |
|
19 #include "inflate.h" |
|
20 |
|
21 //#include <e32test.h> |
|
22 //GLREF_C RTest test; |
|
23 |
|
24 const TInt INBUFSIZE=0x2000; |
|
25 |
|
26 TZipInfo* TheZipInfo; |
|
27 |
|
28 #define Z (*TheZipInfo) |
|
29 |
|
30 extern "C" { |
|
31 |
|
32 extern int inflate(); |
|
33 |
|
34 TUint8 inbuf[INBUFSIZE]; |
|
35 TUint8* volatile inptr; /* index of next byte to be processed in inbuf */ |
|
36 TUint8* volatile inbuf_end; /* pointer to last valid input byte + 1 */ |
|
37 TUint8* volatile outptr; /* pointer to output data */ |
|
38 |
|
39 TAny* malloc(TUint aSize) |
|
40 { |
|
41 return MALLOC((TInt)aSize); |
|
42 } |
|
43 |
|
44 void free(TAny* aPtr) |
|
45 { |
|
46 FREE(aPtr); |
|
47 } |
|
48 |
|
49 TUint8 fill_inbuf() |
|
50 { |
|
51 WAIT_FOR_ANY_REQUEST(); // wait for a block from the file |
|
52 TUint w=Z.iFileBufW; |
|
53 TInt avail=(TInt)w-(TInt)Z.iFileBufR; |
|
54 TInt amount=(avail>(TInt)INBUFSIZE)?INBUFSIZE:avail; |
|
55 TInt rix=(TInt)(Z.iFileBufR & (Z.iFileBufSize-1)); |
|
56 memcpy(inbuf,Z.iFileBuf+rix,amount); |
|
57 Z.iFileBufR+=amount; |
|
58 inptr=inbuf; |
|
59 inbuf_end=inbuf+amount; |
|
60 return *inptr++; |
|
61 } |
|
62 |
|
63 void process_block(int error) |
|
64 { |
|
65 AcceptUnzippedBlock(Z, (TUint8*&)outptr, error); |
|
66 } |
|
67 } |
|
68 |
|
69 TInt ParseZipHeader(TZipInfo& a) |
|
70 { |
|
71 TInt avail=inbuf_end-inptr; |
|
72 if (avail<KZipLocalHeaderLen) |
|
73 return KErrCorrupt; |
|
74 TUint sig=*(TUint*)inptr; // OK since at beginning of buffer |
|
75 inptr+=6; |
|
76 if (sig!=KZipSignature) |
|
77 return KErrCorrupt; |
|
78 a.iFlags=*inptr++; |
|
79 ++inptr; |
|
80 a.iMethod=*inptr++; |
|
81 inptr+=5; |
|
82 memcpy(&a.iCrc,inptr,12); // crc, comp size, uncomp size |
|
83 inptr+=12; |
|
84 a.iFileNameLength=*inptr | (inptr[1]<<8); |
|
85 inptr+=2; |
|
86 a.iExtraLength=*inptr | (inptr[1]<<8); |
|
87 inptr+=2; |
|
88 if (a.iFlags & (CRPFLG|EXTFLG)) |
|
89 return KErrNotSupported; |
|
90 if (a.iMethod!=STORED && a.iMethod!=DEFLATED) |
|
91 return KErrNotSupported; |
|
92 if (avail<KZipLocalHeaderLen+a.iFileNameLength+a.iExtraLength) |
|
93 return KErrCorrupt; |
|
94 a.iNameOffset=30; |
|
95 a.iDataOffset=30+a.iFileNameLength+a.iExtraLength; |
|
96 TInt fnamelen=Min(a.iFileNameLength,a.iName.MaxLength()); |
|
97 TPtrC8 fileNamePtr(inptr,fnamelen); |
|
98 a.iName.Copy(fileNamePtr); |
|
99 return KErrNone; |
|
100 } |
|
101 |
|
102 TInt UnzipThread(TAny* aInfo) |
|
103 { |
|
104 TheZipInfo=(TZipInfo*)aInfo; |
|
105 Z.iProcessedHeader=KRequestPending; |
|
106 inptr=inbuf; |
|
107 inbuf_end=inbuf; |
|
108 fill_inbuf(); |
|
109 inptr=inbuf; |
|
110 TInt r=ParseZipHeader(Z); |
|
111 if (r!=KErrNone) |
|
112 return r; |
|
113 inptr=inbuf+Z.iDataOffset; |
|
114 Z.iHeaderDone=1; |
|
115 WAIT_FOR_REQUEST(Z.iProcessedHeader); |
|
116 outptr=Z.iOutBuf; |
|
117 r=inflate(); |
|
118 r=UnzipComplete(Z, outptr, r); |
|
119 return r; |
|
120 } |
|
121 |
|
122 TInt InitInfo(TZipInfo& a) |
|
123 { |
|
124 a.iInBufSize=INBUFSIZE; |
|
125 a.iFileBufR=0; |
|
126 a.iFileBufW=0; |
|
127 a.iFileBuf=NULL; |
|
128 a.iProcessedHeader=KRequestPending; |
|
129 a.iHeaderDone=0; |
|
130 a.iOutBuf=NULL; |
|
131 a.iThreadHandle=0; |
|
132 a.iThreadStatus=KRequestPending; |
|
133 return KErrNone; |
|
134 } |
|
135 |
|
136 TInt ReadBlockToBuffer(TZipInfo& a) |
|
137 { |
|
138 TInt n; |
|
139 for (n=0; n<10 && a.iFileBufW-a.iFileBufR==a.iFileBufSize; ++n) |
|
140 { |
|
141 // test.Printf(_L("FULL!")); |
|
142 DELAY(20000); // buffer full so wait a bit |
|
143 } |
|
144 if (a.iFileBufW-a.iFileBufR==a.iFileBufSize) |
|
145 return KErrTimedOut; |
|
146 // TInt avail=(TInt)a.iFileBufSize+(TInt)a.iFileBufR-(TInt)a.iFileBufW; |
|
147 // test(avail>=(TInt)INBUFSIZE); |
|
148 TInt req_len=Min(a.iRemain,INBUFSIZE); |
|
149 TInt len=req_len; |
|
150 TInt wix=(TInt)(a.iFileBufW & (a.iFileBufSize-1)); |
|
151 TInt r=ReadInputData(a.iFileBuf+wix,len); |
|
152 if (len>req_len) |
|
153 len=req_len; |
|
154 if (r==KErrNone) |
|
155 { |
|
156 a.iFileBufW+=len; |
|
157 a.iRemain-=len; |
|
158 } |
|
159 return r; |
|
160 } |
|
161 |