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