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