|
1 // Copyright (c) 1995-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 // Accenture - minor tweaks to build in to fshell chkdeps command |
|
13 // |
|
14 // Description: |
|
15 // f32\sfile\sf_pgcompr.cpp |
|
16 // |
|
17 // |
|
18 |
|
19 #include <f32file.h> |
|
20 //#include "sf_std.h" |
|
21 //#include "sf_ldr.h" |
|
22 #include <f32image.h> |
|
23 //#include "sf_image.h" |
|
24 #include <e32uid.h> |
|
25 #include <e32rom.h> |
|
26 //#include "sf_cache.h" |
|
27 |
|
28 #include "sf_pgcompr.h" |
|
29 |
|
30 extern TInt BytePairDecompress(TUint8* /*dst*/, TInt /*dstSize*/, TUint8* /*src*/, TInt /*srcSize*/, TUint8*& /*srcNext*/); |
|
31 |
|
32 //BEGIN TOMSCI |
|
33 #include <fshell/ioutils.h> |
|
34 #define LEAVE_FAILURE(err) StaticLeaveIfErr(err, _L("Byte pair decompress failed at line %d"), __LINE__) |
|
35 #define __IF_DEBUG(x) |
|
36 //END TOMSCI |
|
37 |
|
38 |
|
39 // CBytePairReader - reading from in-memory buffer |
|
40 |
|
41 |
|
42 CBytePairReader* CBytePairReader::NewLC(TUint8* aBuffer, TUint32 aLength) |
|
43 { |
|
44 CBytePairReader* reader = new (ELeave) CBytePairReader(aBuffer, aLength); |
|
45 CleanupStack::PushL(reader); |
|
46 return reader; |
|
47 } |
|
48 |
|
49 |
|
50 CBytePairReader::CBytePairReader(TUint8* aBuffer, TUint32 aLength) |
|
51 : iNextPage(aBuffer), iBytesLeft(aLength) |
|
52 { |
|
53 } |
|
54 |
|
55 |
|
56 void CBytePairReader::SeekForwardL(TUint aBytes) |
|
57 { |
|
58 if (iBytesLeft < aBytes) |
|
59 LEAVE_FAILURE(KErrCorrupt); |
|
60 iNextPage += aBytes; |
|
61 iBytesLeft -= aBytes; |
|
62 } |
|
63 |
|
64 |
|
65 void CBytePairReader::ReadInTableL() |
|
66 { |
|
67 if (KIndexTableHeaderSize > iBytesLeft) |
|
68 LEAVE_FAILURE(KErrCorrupt); |
|
69 Mem::Copy(&iHeader, iNextPage, KIndexTableHeaderSize); |
|
70 iNextPage += KIndexTableHeaderSize; |
|
71 iBytesLeft -= KIndexTableHeaderSize; |
|
72 |
|
73 __IF_DEBUG(Printf("numberOfPages:%d", iHeader.iNumberOfPages)); |
|
74 |
|
75 TUint size = iHeader.iNumberOfPages * sizeof(TUint16); |
|
76 if (size > iBytesLeft) |
|
77 LEAVE_FAILURE(KErrCorrupt); |
|
78 // coverity[buffer_alloc] |
|
79 iIndexTable = new (ELeave) TUint16[size/sizeof(TUint16)]; |
|
80 Mem::Copy(iIndexTable, iNextPage, size); |
|
81 iNextPage += size; |
|
82 iBytesLeft -= size; |
|
83 } |
|
84 |
|
85 |
|
86 void CBytePairReader::ReleaseTable() |
|
87 { |
|
88 delete[] iIndexTable; |
|
89 iIndexTable = NULL; |
|
90 } |
|
91 |
|
92 |
|
93 TUint CBytePairReader::GetPageL(TUint aPageNum, TUint8* aTarget, TInt aLength, TMemoryMoveFunction aMemMoveFn) |
|
94 { |
|
95 if (iIndexTable[aPageNum] > iBytesLeft) |
|
96 LEAVE_FAILURE(KErrCorrupt); |
|
97 iBytesLeft -= iIndexTable[aPageNum]; |
|
98 aLength = Min(aLength, KBytePairPageSize); |
|
99 |
|
100 TInt size; |
|
101 TUint8* nextPage; |
|
102 |
|
103 if (aMemMoveFn) |
|
104 size = BytePairDecompress(iPageBuf, aLength, iNextPage, iIndexTable[aPageNum], nextPage); |
|
105 else |
|
106 size = BytePairDecompress(aTarget, aLength, iNextPage, iIndexTable[aPageNum], nextPage); |
|
107 |
|
108 User::LeaveIfError(size); |
|
109 if (size != aLength) |
|
110 LEAVE_FAILURE(KErrCorrupt); |
|
111 if (iNextPage + iIndexTable[aPageNum] != nextPage) |
|
112 StaticLeaveIfErr(KErrCorrupt, _L("nextPage is %x, expected %x for pagenum %d"), nextPage, iNextPage + iIndexTable[aPageNum], aPageNum); |
|
113 // LEAVE_FAILURE(KErrCorrupt); |
|
114 |
|
115 //TOMSCI IoUtils::CCommandBase::Static().Printf(_L("nextPage is 0x%08x, iNextPage+%d\r\n"), nextPage, nextPage - iNextPage); |
|
116 |
|
117 // If a memmove() was provided, use that to copy the data to its final target |
|
118 if (aMemMoveFn) |
|
119 aMemMoveFn(aTarget, iPageBuf, size); |
|
120 |
|
121 iNextPage = nextPage; |
|
122 return size; |
|
123 } |
|
124 |
|
125 |
|
126 TUint CBytePairReader::DecompressPagesL(TUint8* aTarget, TInt aLength, TMemoryMoveFunction aMemMoveFn) |
|
127 { |
|
128 TUint decompressedSize = 0; |
|
129 |
|
130 ReadInTableL(); |
|
131 |
|
132 for (TUint curPage = 0; curPage < iHeader.iNumberOfPages; ++curPage) |
|
133 { |
|
134 TUint size = GetPageL(curPage, aTarget, aLength, aMemMoveFn); |
|
135 |
|
136 decompressedSize += size; |
|
137 aTarget += size; |
|
138 aLength -= size; |
|
139 |
|
140 __IF_DEBUG(Printf("decomp page size:%d\n", size )); |
|
141 } |
|
142 |
|
143 __IF_DEBUG(Printf("decompressedSize:%d", decompressedSize)); |
|
144 |
|
145 ReleaseTable(); |
|
146 |
|
147 return decompressedSize; |
|
148 } |
|
149 |
|
150 |
|
151 void CBytePairReader::GetPageOffsetsL(TInt32 aInitialOffset, TInt& aPageCount, TInt32*& aPageOffsets) |
|
152 { |
|
153 ReadInTableL(); |
|
154 aPageCount = iHeader.iNumberOfPages; |
|
155 aPageOffsets = new (ELeave) TInt32[aPageCount+1]; |
|
156 |
|
157 TInt bytes = aInitialOffset + KIndexTableHeaderSize + aPageCount * sizeof(TUint16); |
|
158 for (TInt i = 0; i < aPageCount; ++i) |
|
159 { |
|
160 aPageOffsets[i] = bytes; |
|
161 bytes += iIndexTable[i]; |
|
162 } |
|
163 aPageOffsets[aPageCount] = bytes; |
|
164 |
|
165 ReleaseTable(); |
|
166 } |
|
167 |
|
168 |
|
169 // CBytePairFileReader - reading from file |
|
170 |
|
171 |
|
172 CBytePairFileReader* CBytePairFileReader::NewLC(RFile& aFile) |
|
173 { |
|
174 CBytePairFileReader* reader = new (ELeave) CBytePairFileReader(aFile); |
|
175 CleanupStack::PushL(reader); |
|
176 return reader; |
|
177 } |
|
178 |
|
179 |
|
180 CBytePairFileReader::CBytePairFileReader(RFile& aFile) |
|
181 : CBytePairReader(NULL, 0), iFile(aFile) |
|
182 { |
|
183 } |
|
184 |
|
185 |
|
186 CBytePairFileReader::~CBytePairFileReader() |
|
187 { |
|
188 ReleaseTable(); |
|
189 } |
|
190 |
|
191 |
|
192 void CBytePairFileReader::SeekForwardL(TUint aBytes) |
|
193 { |
|
194 TInt bytes = aBytes; |
|
195 User::LeaveIfError(iFile.Seek(ESeekCurrent, bytes)); |
|
196 } |
|
197 |
|
198 |
|
199 void CBytePairFileReader::ReadInTableL() |
|
200 { |
|
201 TPtr8 header((TUint8*)&iHeader, KIndexTableHeaderSize); |
|
202 User::LeaveIfError(iFile.Read(header, KIndexTableHeaderSize)); |
|
203 if (header.Length() != (TInt)KIndexTableHeaderSize) |
|
204 LEAVE_FAILURE(KErrCorrupt); |
|
205 |
|
206 __IF_DEBUG(Printf("numberOfPages:%d", iHeader.iNumberOfPages)); |
|
207 |
|
208 TInt size = iHeader.iNumberOfPages * sizeof(TUint16); |
|
209 iIndexTable = new (ELeave) TUint16[size/sizeof(TUint16)]; |
|
210 TPtr8 indexTable((TUint8*)iIndexTable, size); |
|
211 User::LeaveIfError(iFile.Read(indexTable, size)); |
|
212 if (indexTable.Length() != size) |
|
213 LEAVE_FAILURE(KErrCorrupt); |
|
214 } |
|
215 |
|
216 |
|
217 TUint CBytePairFileReader::DecompressPagesL(TUint8* aTarget, TInt aLength, TMemoryMoveFunction aMemMoveFn) |
|
218 { |
|
219 TUint decompressedSize = 0; |
|
220 |
|
221 ReadInTableL(); |
|
222 |
|
223 TUint curPage = 0; |
|
224 while (curPage < iHeader.iNumberOfPages) |
|
225 { |
|
226 TUint bytes = 0; |
|
227 TUint pages = 0; |
|
228 while (curPage + pages < iHeader.iNumberOfPages && |
|
229 bytes + iIndexTable[curPage+pages] < sizeof(iBuffer)) |
|
230 { |
|
231 bytes += iIndexTable[curPage+pages]; |
|
232 ++pages; |
|
233 } |
|
234 if(!bytes) |
|
235 LEAVE_FAILURE(KErrCorrupt); |
|
236 TPtr8 data(iBuffer, bytes); |
|
237 User::LeaveIfError(iFile.Read(data, bytes)); |
|
238 if (data.Length() != (TInt)bytes) |
|
239 LEAVE_FAILURE(KErrCorrupt); |
|
240 iNextPage = iBuffer; |
|
241 iBytesLeft = bytes; |
|
242 |
|
243 for (; pages; ++curPage, --pages) |
|
244 { |
|
245 TUint size = GetPageL(curPage, aTarget, aLength, aMemMoveFn); |
|
246 |
|
247 decompressedSize += size; |
|
248 aTarget += size; |
|
249 aLength -= size; |
|
250 |
|
251 __IF_DEBUG(Printf("decomp page size:%d\n", size )); |
|
252 } |
|
253 } |
|
254 |
|
255 __IF_DEBUG(Printf("decompressedSize:%d", decompressedSize)); |
|
256 |
|
257 ReleaseTable(); |
|
258 |
|
259 return decompressedSize; |
|
260 } |