|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * byte_pair.cpp |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include <malloc.h> |
|
21 #include <string.h> |
|
22 #include <stdlib.h> |
|
23 #include "h_utl.h" |
|
24 #include "h_ver.h" |
|
25 #include <stdio.h> |
|
26 |
|
27 #include "byte_pair.h" |
|
28 |
|
29 #define PAGE_SIZE 4096 |
|
30 |
|
31 typedef struct IndexTableItemTag |
|
32 { |
|
33 TUint16 iSizeOfCompressedPageData; // pointer to an array TUint16[NumberOfPages] |
|
34 TUint8 *iCompressedPageData; // pointer to an array TUint8*. Each elemet of |
|
35 // this array point a compressed Page data |
|
36 }IndexTableItem; |
|
37 |
|
38 |
|
39 typedef struct IndexTableHeaderTag |
|
40 { |
|
41 TInt iSizeOfData; // Includes the index and compressed pages |
|
42 TInt iDecompressedSize; |
|
43 TUint16 iNumberOfPages; |
|
44 } IndexTableHeader; |
|
45 |
|
46 |
|
47 class CBytePairCompressedImage |
|
48 { |
|
49 public: |
|
50 static CBytePairCompressedImage* NewLC(TUint16 aNumberOfPages, TInt aSize); |
|
51 |
|
52 ~CBytePairCompressedImage(); |
|
53 |
|
54 void AddPage(TUint16 aPageNum, TUint8 * aPageData, TUint16 aPageSize, CBytePair *aBPE); |
|
55 int GetPage(TUint16 aPageNum, TUint8 * aPageData, CBytePair *aBPE); |
|
56 void WriteOutTable(ostream &os); |
|
57 int ReadInTable(istream &is, TUint & aNumberOfPages); |
|
58 |
|
59 private: |
|
60 TInt ConstructL( TUint16 aNumberOfPages, TInt aSize); |
|
61 CBytePairCompressedImage(); |
|
62 |
|
63 private: |
|
64 IndexTableHeader iHeader; |
|
65 IndexTableItem* iPages; |
|
66 TUint8* iOutBuffer; |
|
67 |
|
68 }; |
|
69 |
|
70 |
|
71 CBytePairCompressedImage::CBytePairCompressedImage() |
|
72 { |
|
73 |
|
74 } |
|
75 |
|
76 |
|
77 CBytePairCompressedImage* CBytePairCompressedImage::NewLC(TUint16 aNumberOfPages, TInt aSize) |
|
78 { |
|
79 CBytePairCompressedImage* self = new CBytePairCompressedImage; |
|
80 if( NULL == self) |
|
81 { |
|
82 return self; |
|
83 } |
|
84 |
|
85 if( KErrNone == self->ConstructL(aNumberOfPages, aSize)) |
|
86 { |
|
87 return self; |
|
88 } |
|
89 |
|
90 if(self != NULL) |
|
91 delete self; |
|
92 |
|
93 return NULL; |
|
94 } |
|
95 |
|
96 |
|
97 TInt CBytePairCompressedImage::ConstructL(TUint16 aNumberOfPages, TInt aSize) |
|
98 { |
|
99 #ifdef __DEBUG_MSG__ |
|
100 Print(EWarning,"Start ofCBytePairCompressedImage::ConstructL(%d, %d)\n", aNumberOfPages, aSize ); |
|
101 #endif |
|
102 |
|
103 iHeader.iNumberOfPages = aNumberOfPages; |
|
104 iHeader.iDecompressedSize = aSize; |
|
105 |
|
106 if( 0 != aNumberOfPages) |
|
107 { |
|
108 iPages = (IndexTableItem *) calloc(aNumberOfPages, sizeof(IndexTableItem)); |
|
109 |
|
110 if( NULL == iPages ) |
|
111 { |
|
112 return KErrNoMemory; |
|
113 } |
|
114 } |
|
115 |
|
116 iHeader.iSizeOfData = sizeof(iHeader.iSizeOfData) + |
|
117 sizeof(iHeader.iDecompressedSize) + |
|
118 sizeof(iHeader.iNumberOfPages) + |
|
119 aNumberOfPages * sizeof(TUint16); |
|
120 |
|
121 iOutBuffer = (TUint8 *) calloc(4 * PAGE_SIZE, sizeof(TUint8) ); |
|
122 |
|
123 if ( NULL == iOutBuffer) |
|
124 { |
|
125 return KErrNoMemory; |
|
126 } |
|
127 return KErrNone; |
|
128 } // End of ConstructL() |
|
129 |
|
130 CBytePairCompressedImage::~CBytePairCompressedImage() |
|
131 { |
|
132 #ifdef __DEBUG_MSG__ |
|
133 Print(EWarning,"Destructor calling."); |
|
134 #endif |
|
135 |
|
136 for( int i = 0; i < iHeader.iNumberOfPages; i++) |
|
137 { |
|
138 free(iPages[i].iCompressedPageData); |
|
139 iPages[i].iCompressedPageData = NULL; |
|
140 } |
|
141 |
|
142 free( iPages ); |
|
143 iPages = NULL; |
|
144 |
|
145 free( iOutBuffer ); |
|
146 iOutBuffer = NULL; |
|
147 } |
|
148 |
|
149 |
|
150 void CBytePairCompressedImage::AddPage(TUint16 aPageNum, TUint8 * aPageData, TUint16 aPageSize, CBytePair *aBPE) |
|
151 { |
|
152 #ifdef __DEBUG_MSG__ |
|
153 Print(EWarning,"Start of AddPage(aPageNum:%d, ,aPageSize:%d)\n",aPageNum, aPageSize ); |
|
154 #endif |
|
155 |
|
156 #ifdef __TEST_ONLY__ |
|
157 |
|
158 iPages[aPageNum].iSizeOfCompressedPageData = 2; |
|
159 |
|
160 iPages[aPageNum].iCompressedPageData = (TUint8 *) calloc(iPages[aPageNum].iSizeOfCompressedPageData, sizeof(TUint8) ); |
|
161 |
|
162 memcpy(iPages[aPageNum].iCompressedPageData, (TUint8 *) &aPageNum, iPages[aPageNum].iSizeOfCompressedPageData); |
|
163 |
|
164 |
|
165 #else |
|
166 |
|
167 TUint16 compressedSize; |
|
168 if(aBPE != NULL) |
|
169 compressedSize = (TUint16) aBPE->Compress(iOutBuffer,aPageData,aPageSize); |
|
170 else { |
|
171 CBytePair bpe(EFalse); |
|
172 compressedSize = (TUint16) bpe.Compress(iOutBuffer,aPageData,aPageSize); |
|
173 } |
|
174 iPages[aPageNum].iSizeOfCompressedPageData = compressedSize; |
|
175 |
|
176 #ifdef __DEBUG_MSG__ |
|
177 Print(EWarning,"Compressed page size:%d\n", iPages[aPageNum].iSizeOfCompressedPageData ); |
|
178 #endif |
|
179 |
|
180 iPages[aPageNum].iCompressedPageData = (TUint8 *) calloc(iPages[aPageNum].iSizeOfCompressedPageData, sizeof(TUint8) ); |
|
181 |
|
182 if( NULL == iPages[aPageNum].iCompressedPageData ) |
|
183 { |
|
184 return; |
|
185 } |
|
186 |
|
187 memcpy(iPages[aPageNum].iCompressedPageData, iOutBuffer, iPages[aPageNum].iSizeOfCompressedPageData ); |
|
188 |
|
189 #endif |
|
190 |
|
191 iHeader.iSizeOfData += iPages[aPageNum].iSizeOfCompressedPageData; |
|
192 } |
|
193 |
|
194 void CBytePairCompressedImage::WriteOutTable(ostream &os) |
|
195 { |
|
196 // Write out IndexTableHeader |
|
197 os.write((const char *) &iHeader.iSizeOfData, sizeof(iHeader.iSizeOfData)); |
|
198 os.write((const char *) &iHeader.iDecompressedSize, sizeof(iHeader.iDecompressedSize)); |
|
199 os.write((const char *) &iHeader.iNumberOfPages, sizeof(iHeader.iNumberOfPages)); |
|
200 |
|
201 TInt i; |
|
202 // Write out IndexTableItems (size of each compressed page) |
|
203 for(i = 0; i < iHeader.iNumberOfPages; i++) |
|
204 { |
|
205 os.write((const char *) &(iPages[i].iSizeOfCompressedPageData), sizeof(TUint16)); |
|
206 } |
|
207 |
|
208 // Write out compressed pages |
|
209 for(i = 0; i < iHeader.iNumberOfPages; i++) |
|
210 { |
|
211 os.write((const char*)iPages[i].iCompressedPageData, iPages[i].iSizeOfCompressedPageData); |
|
212 } |
|
213 |
|
214 } |
|
215 |
|
216 |
|
217 int CBytePairCompressedImage::ReadInTable(istream &is, TUint & aNumberOfPages) |
|
218 { |
|
219 // Read page index table header |
|
220 is.read((char *)&iHeader, (sizeof(iHeader.iSizeOfData)+sizeof(iHeader.iDecompressedSize)+sizeof(iHeader.iNumberOfPages))); |
|
221 |
|
222 // Allocatin place to Page index table entries |
|
223 iPages = (IndexTableItem *) calloc(iHeader.iNumberOfPages, sizeof(IndexTableItem)); |
|
224 |
|
225 if( NULL == iPages ) |
|
226 { |
|
227 return KErrNoMemory; |
|
228 } |
|
229 TInt i; |
|
230 // Read whole Page index table |
|
231 |
|
232 for(i = 0; i < iHeader.iNumberOfPages; i++) |
|
233 { |
|
234 is.read((char *) &(iPages[i].iSizeOfCompressedPageData), sizeof(TUint16)); |
|
235 } |
|
236 |
|
237 // Read compressed data pages page by page, decompress and store them |
|
238 for(i = 0; i < iHeader.iNumberOfPages; i++) |
|
239 { |
|
240 |
|
241 iPages[i].iCompressedPageData = (TUint8 *) calloc(iPages[i].iSizeOfCompressedPageData, sizeof(TUint8) ); |
|
242 |
|
243 if( NULL == iPages[i].iCompressedPageData ) |
|
244 { |
|
245 return KErrNoMemory; |
|
246 } |
|
247 |
|
248 is.read((char*)iPages[i].iCompressedPageData, iPages[i].iSizeOfCompressedPageData); |
|
249 } |
|
250 |
|
251 aNumberOfPages = iHeader.iNumberOfPages; |
|
252 |
|
253 return KErrNone; |
|
254 } |
|
255 |
|
256 int CBytePairCompressedImage::GetPage(TUint16 aPageNum, TUint8 * aPageData, CBytePair *aBPE) |
|
257 { |
|
258 TUint8* pakEnd; |
|
259 |
|
260 const TInt MaxBlockSize = 0x1000; |
|
261 |
|
262 TUint16 uncompressedSize; |
|
263 if(aBPE != NULL) |
|
264 uncompressedSize = (TUint16) aBPE->Decompress( aPageData, |
|
265 MaxBlockSize, |
|
266 iPages[aPageNum].iCompressedPageData, |
|
267 iPages[aPageNum].iSizeOfCompressedPageData, |
|
268 pakEnd ); |
|
269 else { |
|
270 CBytePair bpe(EFalse); |
|
271 uncompressedSize = (TUint16) bpe.Decompress( aPageData, |
|
272 MaxBlockSize, |
|
273 iPages[aPageNum].iCompressedPageData, |
|
274 iPages[aPageNum].iSizeOfCompressedPageData, |
|
275 pakEnd ); |
|
276 } |
|
277 |
|
278 return uncompressedSize; |
|
279 } |
|
280 |
|
281 |
|
282 void CompressPages(TUint8 * bytes, TInt size, ostream& os, CBytePair *aBPE) |
|
283 { |
|
284 // Build a list of compressed pages |
|
285 TUint16 numOfPages = (TUint16) (size / PAGE_SIZE); |
|
286 |
|
287 #ifdef __DEBUG_MSG__ |
|
288 Print(EWarning,"numOfPages:%d, (size %% PAGE_SIZE:%d)\n", numOfPages, size % PAGE_SIZE); |
|
289 #endif |
|
290 |
|
291 if ( size % PAGE_SIZE > 0) |
|
292 ++numOfPages; |
|
293 |
|
294 CBytePairCompressedImage *comprImage = CBytePairCompressedImage::NewLC(numOfPages, size); |
|
295 if( NULL == comprImage) |
|
296 { |
|
297 Print(EError," NULL == comprImage\n"); |
|
298 return; |
|
299 } |
|
300 |
|
301 TUint8* iPageStart; |
|
302 TUint16 iPageLen; |
|
303 TUint16 iPage = 0; |
|
304 |
|
305 while(iPage * PAGE_SIZE < size ) |
|
306 { |
|
307 iPageStart = &bytes[iPage * PAGE_SIZE]; |
|
308 iPageLen = (TUint16)( (iPage + 1) * PAGE_SIZE < size ? PAGE_SIZE : size - iPage * PAGE_SIZE); |
|
309 |
|
310 comprImage->AddPage(iPage, iPageStart, iPageLen, aBPE); |
|
311 |
|
312 ++iPage; |
|
313 } |
|
314 |
|
315 // Write out index table and compressed pages |
|
316 comprImage->WriteOutTable(os); |
|
317 |
|
318 |
|
319 delete comprImage; |
|
320 comprImage = NULL; |
|
321 |
|
322 } |
|
323 |
|
324 int DecompressPages(TUint8 * bytes, istream& is, CBytePair *aBPE) |
|
325 { |
|
326 TUint decompressedSize = 0; |
|
327 CBytePairCompressedImage *comprImage = CBytePairCompressedImage::NewLC(0, 0); |
|
328 if( NULL == comprImage) |
|
329 { |
|
330 Print(EError," NULL == comprImage\n"); |
|
331 return KErrNoMemory; |
|
332 } |
|
333 |
|
334 TUint numberOfPages = 0; |
|
335 comprImage->ReadInTable(is, numberOfPages); |
|
336 |
|
337 |
|
338 TUint8* iPageStart; |
|
339 TUint16 iPage = 0; |
|
340 |
|
341 while(iPage < numberOfPages ) |
|
342 { |
|
343 iPageStart = &bytes[iPage * PAGE_SIZE]; |
|
344 |
|
345 decompressedSize += comprImage->GetPage(iPage, iPageStart, aBPE); |
|
346 |
|
347 ++iPage; |
|
348 } |
|
349 |
|
350 delete comprImage; |
|
351 return decompressedSize; |
|
352 |
|
353 } |