|
1 /* |
|
2 * Copyright (c) 1997-1999 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 "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 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "resentry.h" |
|
21 #include "resdict.h" |
|
22 #include "rescomp.h" |
|
23 |
|
24 //TResComponent |
|
25 |
|
26 TInt TResComponent::Size(TInt aDictionaryBits) |
|
27 { |
|
28 if (iType == EResTypePlain) |
|
29 { |
|
30 if (iLength == 0) |
|
31 return 0; |
|
32 TInt bits = iLength * 8; |
|
33 bits++; // data flag |
|
34 if (iLength == 1) |
|
35 bits ++; // first bit zero if 1 byte of data |
|
36 else |
|
37 { |
|
38 bits++; // first bit 1 if >1 byte of data |
|
39 if (iLength == 2) |
|
40 { |
|
41 bits ++; // second bit zero if 2 bytes of data |
|
42 } |
|
43 else |
|
44 { |
|
45 bits ++; // second bit 1 if >2 bytes of data |
|
46 if (iLength < 11) |
|
47 { |
|
48 // write 4 bits of data + 1 zero bit flag |
|
49 bits += 4; |
|
50 } |
|
51 else |
|
52 { |
|
53 // write 8 bits of data + 1 '1' bit flag |
|
54 bits += 9; |
|
55 } |
|
56 } |
|
57 } |
|
58 |
|
59 // console->Printf(_L("Type Plain: %d\n"), bits); |
|
60 return bits; |
|
61 } |
|
62 if (iType == EResTypeToken) |
|
63 { |
|
64 return 1+aDictionaryBits; |
|
65 // console->Printf(_L("Type Token: %d\n"), iToken); |
|
66 // return 8; // dictionary entries are stored in 6 bits |
|
67 } |
|
68 return 0; |
|
69 } |
|
70 |
|
71 |
|
72 void TResComponent::WriteBitStreamL(TBitWriter& aWriter, TInt aDictionaryBits) |
|
73 { |
|
74 if (iType == EResTypePlain) |
|
75 { |
|
76 if (iLength == 0) |
|
77 return; |
|
78 // Debugging code |
|
79 /* |
|
80 RDebug::Print(_L("Data Length: %d"), iLength); |
|
81 TBuf<100> b; |
|
82 for (TInt xx=0; xx<iLength; xx++) |
|
83 { |
|
84 b.AppendFormat(_L("%02x "), iData[xx]); |
|
85 } |
|
86 RDebug::Print(b); |
|
87 */ |
|
88 // |
|
89 // Write 'data' flag |
|
90 aWriter.WriteL(1, 1); |
|
91 if (iLength == 1) |
|
92 { |
|
93 aWriter.WriteL(0, 1); // first bit zero if 1 byte of data |
|
94 } |
|
95 else |
|
96 { |
|
97 aWriter.WriteL(1, 1); // first bit 1 if >1 byte of data |
|
98 if (iLength == 2) |
|
99 { |
|
100 aWriter.WriteL(0, 1); // second bit zero if 2 bytes of data |
|
101 } |
|
102 else |
|
103 { |
|
104 aWriter.WriteL(1, 1); // second bit 1 if >2 bytes of data |
|
105 if (iLength < 11) |
|
106 { |
|
107 aWriter.WriteL(0, 1); |
|
108 aWriter.WriteL(iLength-3, 3); |
|
109 // write 4 bits of data + 1 zero bit flag |
|
110 } |
|
111 else |
|
112 { |
|
113 aWriter.WriteL(1, 1); |
|
114 aWriter.WriteL(iLength, 8); |
|
115 // write 8 bits of data + 1 '1' bit flag |
|
116 if (iLength > 255) |
|
117 { |
|
118 User::Panic(_L("Entry too long"),0); |
|
119 } |
|
120 } |
|
121 } |
|
122 } |
|
123 // Now write data to output |
|
124 for (TInt ii=0; ii<iLength; ii++) |
|
125 { |
|
126 aWriter.WriteL(iData[ii], 8); |
|
127 } |
|
128 } |
|
129 if (iType == EResTypeToken) |
|
130 { |
|
131 // Write 'token' flag |
|
132 aWriter.WriteL(0, 1); |
|
133 |
|
134 // Write token |
|
135 aWriter.WriteL(iToken, aDictionaryBits); |
|
136 // RDebug::Print(_L("Token [%d] "), iToken); |
|
137 } |
|
138 } |
|
139 |
|
140 |
|
141 |
|
142 // CResEntry |
|
143 CResEntry::CResEntry(CDictArray* aDict) |
|
144 :iDict(aDict) |
|
145 { |
|
146 iComponent = new(ELeave)CArrayFixFlat<TResComponent>(4); |
|
147 } |
|
148 |
|
149 CResEntry::~CResEntry() |
|
150 { |
|
151 delete iComponent; |
|
152 } |
|
153 |
|
154 |
|
155 void CResEntry::AddPlainDataL(TUint8* aData, TInt aLength) |
|
156 { |
|
157 TResComponent comp; |
|
158 comp.iType = EResTypePlain; |
|
159 comp.iData = aData; |
|
160 comp.iLength = aLength; |
|
161 iComponent->AppendL(comp); |
|
162 } |
|
163 |
|
164 void CResEntry::InsertTokenL(TInt aPosition, TInt aToken) |
|
165 { |
|
166 TResComponent comp; |
|
167 comp.iType = EResTypeToken; |
|
168 comp.iToken = aToken; |
|
169 comp.iLength=0; |
|
170 iComponent->InsertL(aPosition,comp); |
|
171 (*iDict)[aToken].iUses++; |
|
172 } |
|
173 |
|
174 void CResEntry::SplitPlainDataWithZeroSpacedL(TInt aComponent, TInt aZeroSpacedStart, TInt aLength) |
|
175 { |
|
176 TResComponent comp = (*iComponent)[aComponent]; |
|
177 |
|
178 TResComponent newComp1; |
|
179 newComp1.iType = EResTypePlain; |
|
180 newComp1.iData = comp.iData; |
|
181 newComp1.iLength = aZeroSpacedStart; |
|
182 |
|
183 TResComponent newComp2; |
|
184 newComp2.iType = EResTypePlain; |
|
185 newComp2.iData = comp.iData + aZeroSpacedStart + aLength; |
|
186 newComp2.iLength = comp.iLength-(aZeroSpacedStart+aLength); |
|
187 |
|
188 TResComponent newComp3; |
|
189 newComp3.iType = EResTypeZeroSpaced; |
|
190 newComp3.iData = comp.iData + aZeroSpacedStart; |
|
191 newComp3.iLength = aLength; |
|
192 |
|
193 |
|
194 iComponent->Delete(aComponent); |
|
195 TInt componentPos = aComponent; |
|
196 if (newComp1.iLength != 0) |
|
197 { |
|
198 iComponent->InsertL(componentPos, newComp1); |
|
199 componentPos++; |
|
200 } |
|
201 |
|
202 iComponent->InsertL(componentPos, newComp3); |
|
203 componentPos++; |
|
204 |
|
205 if (newComp2.iLength != 0) |
|
206 { |
|
207 iComponent->InsertL(componentPos, newComp2); |
|
208 } |
|
209 } |
|
210 |
|
211 |
|
212 void CResEntry::SplitPlainDataWithTokenL(TInt aComponent, TInt aToken, TInt aPosition) |
|
213 { |
|
214 TResComponent comp = (*iComponent)[aComponent]; |
|
215 TDesC8& dEntry = (*iDict)[aToken].Data(); |
|
216 TInt dictLength = dEntry.Length(); |
|
217 |
|
218 TResComponent newComp1; |
|
219 newComp1.iType = EResTypePlain; |
|
220 newComp1.iData = comp.iData; |
|
221 newComp1.iLength = aPosition; |
|
222 |
|
223 TResComponent newComp2; |
|
224 newComp2.iType = EResTypePlain; |
|
225 newComp2.iData = comp.iData + aPosition + dictLength; |
|
226 newComp2.iLength = comp.iLength - dictLength - aPosition; |
|
227 |
|
228 iComponent->Delete(aComponent); |
|
229 TInt componentPos = aComponent; |
|
230 if (newComp1.iLength != 0) |
|
231 { |
|
232 iComponent->InsertL(componentPos, newComp1); |
|
233 componentPos++; |
|
234 } |
|
235 InsertTokenL(componentPos, aToken); |
|
236 componentPos++; |
|
237 |
|
238 if (newComp2.iLength != 0) |
|
239 { |
|
240 iComponent->InsertL(componentPos, newComp2); |
|
241 } |
|
242 |
|
243 } |
|
244 |
|
245 |
|
246 TInt CResEntry::Size(TInt aDictionaryBits) |
|
247 { |
|
248 TInt size = 0; |
|
249 TInt count = iComponent->Count(); |
|
250 for (TInt ii=0; ii<count; ii++) |
|
251 { |
|
252 size += iComponent->At(ii).Size(aDictionaryBits); |
|
253 } |
|
254 return size; |
|
255 } |
|
256 |
|
257 void CResEntry::WriteBitStreamL(TBitWriter& aWriter, TInt aDictionaryBits) |
|
258 { |
|
259 TInt count = iComponent->Count(); |
|
260 for (TInt ii=0; ii<count; ii++) |
|
261 { |
|
262 iComponent->At(ii).WriteBitStreamL(aWriter, aDictionaryBits); |
|
263 } |
|
264 } |
|
265 |
|
266 |
|
267 |
|
268 TBool CResEntry::DoMatchDictL(TResComponent& aComp, TInt aOffset, TInt aDictIndex) |
|
269 { |
|
270 TInt dCount = iDict->Count(); |
|
271 TInt cSize = aComp.iLength; |
|
272 for (TInt ii=0; ii<dCount; ii++) |
|
273 { |
|
274 if (ii != aDictIndex) |
|
275 { |
|
276 TDesC8& dEntry = (*iDict)[ii].Data(); |
|
277 if (dEntry.Length() <= cSize) |
|
278 { |
|
279 TPtrC8 data(aComp.iData, cSize); |
|
280 TInt pos = data.Find(dEntry); |
|
281 if (pos != KErrNotFound) |
|
282 { |
|
283 // console->Printf(_L("Found %d Offset: %d\n"),ii, pos); |
|
284 SplitPlainDataWithTokenL(aOffset, ii, pos); |
|
285 return ETrue; |
|
286 } |
|
287 } |
|
288 } |
|
289 } |
|
290 return EFalse; |
|
291 } |
|
292 |
|
293 |
|
294 void CResEntry::MatchDictL(TInt aDictIndex) |
|
295 { |
|
296 // Match plain text components against dictionary |
|
297 TInt count = iComponent->Count(); |
|
298 for (TInt ii=0; ii<count; ii++) |
|
299 { |
|
300 TResComponent comp = (*iComponent)[ii]; |
|
301 if (comp.iType == EResTypePlain) |
|
302 { |
|
303 if (DoMatchDictL(comp, ii, aDictIndex)) |
|
304 { |
|
305 count = iComponent->Count(); |
|
306 ii--; |
|
307 } |
|
308 } |
|
309 } |
|
310 } |
|
311 |
|
312 |
|
313 /* |
|
314 void CResEntry::MatchZeroSpacedL() |
|
315 { |
|
316 // Find zero spaced data |
|
317 TInt count = iComponent->Count(); |
|
318 for (TInt ii=0; ii<count; ii++) |
|
319 { |
|
320 TResComponent comp = (*iComponent)[ii]; |
|
321 if (comp.iType == EResTypePlain) |
|
322 { |
|
323 if (DoMatchZeroSpacedL(comp, ii)) |
|
324 { |
|
325 count = iComponent->Count(); |
|
326 ii--; |
|
327 } |
|
328 } |
|
329 } |
|
330 } |
|
331 |
|
332 |
|
333 TBool CResEntry::DoMatchZeroSpacedL(TResComponent& aComp, TInt aOffset) |
|
334 { |
|
335 // Analyse data and see if there is a string of zero spaced data |
|
336 // Data, followed by zero |
|
337 TInt length = aComp.iLength; |
|
338 TInt pos = 0; |
|
339 TInt dataSize = 0; |
|
340 for (TInt ii=0; ii<length-1; ii++) |
|
341 { |
|
342 if (aComp.iData[ii+1] == 0) |
|
343 { |
|
344 // Found zero spaced data |
|
345 if (dataSize == 0) |
|
346 { |
|
347 // Start of zero spaced data |
|
348 pos = ii; |
|
349 dataSize = 2; |
|
350 } |
|
351 else |
|
352 { |
|
353 dataSize += 2; |
|
354 } |
|
355 ii++; |
|
356 } |
|
357 else |
|
358 { |
|
359 // End of zero spaced data |
|
360 if (dataSize > 2) |
|
361 { |
|
362 SplitPlainDataWithZeroSpacedL(aOffset, pos, dataSize); |
|
363 return ETrue; |
|
364 } |
|
365 dataSize = 0; |
|
366 } |
|
367 } |
|
368 return EFalse; |
|
369 } |
|
370 */ |
|
371 |
|
372 |
|
373 void CResEntry::MatchSelfL(TInt aRes,CResComp* aResComp) |
|
374 { |
|
375 // Find duplicates within resource |
|
376 TInt count = iComponent->Count(); |
|
377 for (TInt ii=0; ii<count; ii++) |
|
378 { |
|
379 TResComponent comp = (*iComponent)[ii]; |
|
380 if (comp.iType == EResTypePlain) |
|
381 { |
|
382 if (DoMatchSelfL(comp, ii, aRes, aResComp)) |
|
383 { |
|
384 count = iComponent->Count(); |
|
385 ii--; |
|
386 } |
|
387 } |
|
388 } |
|
389 } |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 /* |
|
395 TBool CResEntry::MatchPreviousComponent(TInt aCompIndex, TDesC8& aMatch) |
|
396 { |
|
397 TInt size = aMatch.Size(); |
|
398 for (TInt ii=0; ii<aCompIndex; ii++) |
|
399 { |
|
400 TResComponent comp = (*iComponent)[ii]; |
|
401 if (comp.iType == EResTypePlain) |
|
402 { |
|
403 console->Printf(_L("Matching: %d (Length: %d)\n"), aCompIndex, size); |
|
404 if (comp.iLength <= size) |
|
405 { |
|
406 TPtrC8 buf(comp.iData, comp.iLength); |
|
407 TInt found = buf.Find(aMatch); |
|
408 if (found != KErrNotFound) |
|
409 { |
|
410 console->Printf(_L("Found\n")); |
|
411 return ii; |
|
412 } |
|
413 } |
|
414 } |
|
415 } |
|
416 |
|
417 return KErrNotFound; |
|
418 } |
|
419 */ |
|
420 |
|
421 TBool CResEntry::DoMatchSelfL(TResComponent& aComp, TInt aOffset, TInt aRes, CResComp* aResComp) |
|
422 { |
|
423 TInt cSize = aComp.iLength; |
|
424 TInt matchSize = cSize/2; |
|
425 if (matchSize > KMaxDictEntry) |
|
426 matchSize = KMaxDictEntry; |
|
427 |
|
428 while (matchSize > 1) |
|
429 { |
|
430 TInt pos=matchSize; |
|
431 TInt positions = (cSize-(matchSize*2))+1; |
|
432 while (positions) |
|
433 { |
|
434 TPtr8 match(aComp.iData + pos, matchSize,matchSize); |
|
435 TPtr8 left(aComp.iData, pos, pos); |
|
436 TInt found = left.Find(match); |
|
437 if (found != KErrNotFound) |
|
438 { |
|
439 TInt token = iDict->AddEntryL(match); |
|
440 // Remove both matches and replace with token |
|
441 SplitPlainDataWithTokenL(aOffset, token, pos); |
|
442 SplitPlainDataWithTokenL(aOffset, token, found); |
|
443 return ETrue; |
|
444 } |
|
445 else |
|
446 { |
|
447 // Find in all other resources |
|
448 TInt find = aResComp->FindInResources(match, aRes); |
|
449 if (find != KErrNotFound) |
|
450 { |
|
451 TInt token = iDict->AddEntryL(match); |
|
452 SplitPlainDataWithTokenL(aOffset, token, pos); |
|
453 return ETrue; |
|
454 } |
|
455 |
|
456 } |
|
457 positions--; |
|
458 pos++; |
|
459 } |
|
460 matchSize--; |
|
461 } |
|
462 return EFalse; |
|
463 } |
|
464 |
|
465 |
|
466 TResType iType; |
|
467 TUint8* iData; |
|
468 TInt iLength; |
|
469 TInt iToken; |
|
470 |
|
471 |
|
472 void CResEntry::CheckForLongDataStringsL() |
|
473 { |
|
474 TInt components = iComponent->Count(); |
|
475 for (TInt ii=0; ii<components; ii++) |
|
476 { |
|
477 TResComponent comp = (*iComponent)[ii]; |
|
478 if (comp.iLength > 255) |
|
479 { |
|
480 TInt length = comp.iLength; |
|
481 RDebug::Print(_L("Component Too Long!")); |
|
482 TResComponent comp1; |
|
483 comp1.iType = comp.iType; |
|
484 comp1.iData = comp.iData; |
|
485 comp1.iLength = 255; |
|
486 comp1.iToken = comp.iToken; |
|
487 |
|
488 TResComponent comp2; |
|
489 comp2.iType = comp.iType; |
|
490 comp2.iData = comp.iData + 255; |
|
491 comp2.iLength = length - 255; |
|
492 comp2.iToken = comp.iToken; |
|
493 |
|
494 iComponent->Delete(ii); |
|
495 iComponent->InsertL(ii, comp1); |
|
496 iComponent->InsertL(ii+1, comp2); |
|
497 |
|
498 components = iComponent->Count(); |
|
499 } |
|
500 } |
|
501 } |
|
502 |