]>
git.saurik.com Git - android/aapt.git/blob - StringPool.cpp
2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
7 #include "StringPool.h"
8 #include "ResourceTable.h"
10 #include <utils/ByteOrder.h>
11 #include <utils/SortedVector.h>
15 # define ZD_TYPE ssize_t
23 void strcpy16_htod(uint16_t* dst
, const uint16_t* src
)
26 char16_t s
= htods(*src
);
33 void printStringPool(const ResStringPool
* pool
)
35 SortedVector
<const void*> uniqueStrings
;
36 const size_t N
= pool
->size();
37 for (size_t i
=0; i
<N
; i
++) {
40 uniqueStrings
.add(pool
->string8At(i
, &len
));
42 uniqueStrings
.add(pool
->stringAt(i
, &len
));
46 printf("String pool of " ZD
" unique %s %s strings, " ZD
" entries and "
47 ZD
" styles using " ZD
" bytes:\n",
48 (ZD_TYPE
)uniqueStrings
.size(), pool
->isUTF8() ? "UTF-8" : "UTF-16",
49 pool
->isSorted() ? "sorted" : "non-sorted",
50 (ZD_TYPE
)N
, (ZD_TYPE
)pool
->styleCount(), (ZD_TYPE
)pool
->bytes());
52 const size_t NS
= pool
->size();
53 for (size_t s
=0; s
<NS
; s
++) {
54 String8 str
= pool
->string8ObjectAt(s
);
55 printf("String #" ZD
": %s\n", (ZD_TYPE
) s
, str
.string());
59 String8
StringPool::entry::makeConfigsString() const {
60 String8
configStr(configTypeName
);
61 if (configStr
.size() > 0) configStr
.append(" ");
62 if (configs
.size() > 0) {
63 for (size_t j
=0; j
<configs
.size(); j
++) {
64 if (j
> 0) configStr
.append(", ");
65 configStr
.append(configs
[j
].toString());
73 int StringPool::entry::compare(const entry
& o
) const {
74 // Strings with styles go first, to reduce the size of the
77 return o
.hasStyles
? 0 : -1;
82 int comp
= configTypeName
.compare(o
.configTypeName
);
86 const size_t LHN
= configs
.size();
87 const size_t RHN
= o
.configs
.size();
89 while (i
< LHN
&& i
< RHN
) {
90 comp
= configs
[i
].compareLogical(o
.configs
[i
]);
96 if (LHN
< RHN
) return -1;
97 else if (LHN
> RHN
) return 1;
101 StringPool::StringPool(bool sorted
, bool utf8
)
102 : mSorted(sorted
), mUTF8(utf8
), mValues(-1), mIdents(-1)
106 ssize_t
StringPool::add(const String16
& value
, bool mergeDuplicates
,
107 const String8
* configTypeName
, const ResTable_config
* config
)
109 return add(String16(), value
, mergeDuplicates
, configTypeName
, config
);
112 ssize_t
StringPool::add(const String16
& value
, const Vector
<entry_style_span
>& spans
,
113 const String8
* configTypeName
, const ResTable_config
* config
)
115 ssize_t res
= add(String16(), value
, false, configTypeName
, config
);
117 addStyleSpans(res
, spans
);
122 ssize_t
StringPool::add(const String16
& ident
, const String16
& value
,
123 bool mergeDuplicates
, const String8
* configTypeName
, const ResTable_config
* config
)
125 if (ident
.size() > 0) {
126 ssize_t idx
= mIdents
.valueFor(ident
);
128 fprintf(stderr
, "ERROR: Duplicate string identifier %s\n",
129 String8(mEntries
[idx
].value
).string());
130 return UNKNOWN_ERROR
;
134 ssize_t vidx
= mValues
.indexOfKey(value
);
135 ssize_t pos
= vidx
>= 0 ? mValues
.valueAt(vidx
) : -1;
136 ssize_t eidx
= pos
>= 0 ? mEntryArray
.itemAt(pos
) : -1;
138 eidx
= mEntries
.add(entry(value
));
140 fprintf(stderr
, "Failure adding string %s\n", String8(value
).string());
145 if (configTypeName
!= NULL
) {
146 entry
& ent
= mEntries
.editItemAt(eidx
);
147 NOISY(printf("*** adding config type name %s, was %s\n",
148 configTypeName
->string(), ent
.configTypeName
.string()));
149 if (ent
.configTypeName
.size() <= 0) {
150 ent
.configTypeName
= *configTypeName
;
151 } else if (ent
.configTypeName
!= *configTypeName
) {
152 ent
.configTypeName
= " ";
156 if (config
!= NULL
) {
157 // Add this to the set of configs associated with the string.
158 entry
& ent
= mEntries
.editItemAt(eidx
);
160 for (addPos
=0; addPos
<ent
.configs
.size(); addPos
++) {
161 int cmp
= ent
.configs
.itemAt(addPos
).compareLogical(*config
);
164 NOISY(printf("*** inserting config: %s\n", config
->toString().string()));
165 ent
.configs
.insertAt(*config
, addPos
);
170 if (addPos
>= ent
.configs
.size()) {
171 NOISY(printf("*** adding config: %s\n", config
->toString().string()));
172 ent
.configs
.add(*config
);
176 const bool first
= vidx
< 0;
177 if (first
|| !mergeDuplicates
) {
178 pos
= mEntryArray
.add(eidx
);
180 vidx
= mValues
.add(value
, pos
);
183 entry
& ent
= mEntries
.editItemAt(eidx
);
184 ent
.indices
.add(pos
);
188 if (ident
.size() > 0) {
189 mIdents
.add(ident
, vidx
);
192 NOISY(printf("Adding string %s to pool: pos=%d eidx=%d vidx=%d\n",
193 String8(value
).string(), pos
, eidx
, vidx
));
198 status_t
StringPool::addStyleSpan(size_t idx
, const String16
& name
,
199 uint32_t start
, uint32_t end
)
201 entry_style_span span
;
203 span
.span
.firstChar
= start
;
204 span
.span
.lastChar
= end
;
205 return addStyleSpan(idx
, span
);
208 status_t
StringPool::addStyleSpans(size_t idx
, const Vector
<entry_style_span
>& spans
)
210 const size_t N
=spans
.size();
211 for (size_t i
=0; i
<N
; i
++) {
212 status_t err
= addStyleSpan(idx
, spans
[i
]);
213 if (err
!= NO_ERROR
) {
220 status_t
StringPool::addStyleSpan(size_t idx
, const entry_style_span
& span
)
222 LOG_ALWAYS_FATAL_IF(mSorted
, "Can't use styles with sorted string pools.");
224 // Place blank entries in the span array up to this index.
225 while (mEntryStyleArray
.size() <= idx
) {
226 mEntryStyleArray
.add();
229 entry_style
& style
= mEntryStyleArray
.editItemAt(idx
);
230 style
.spans
.add(span
);
231 mEntries
.editItemAt(mEntryArray
[idx
]).hasStyles
= true;
235 size_t StringPool::size() const
237 return mSorted
? mValues
.size() : mEntryArray
.size();
240 const StringPool::entry
& StringPool::entryAt(size_t idx
) const
243 return mEntries
[mEntryArray
[idx
]];
245 return mEntries
[mEntryArray
[mValues
.valueAt(idx
)]];
249 size_t StringPool::countIdentifiers() const
251 return mIdents
.size();
254 int StringPool::config_sort(const size_t* lhs
, const size_t* rhs
, void* state
)
256 StringPool
* pool
= (StringPool
*)state
;
257 const entry
& lhe
= pool
->mEntries
[pool
->mEntryArray
[*lhs
]];
258 const entry
& rhe
= pool
->mEntries
[pool
->mEntryArray
[*rhs
]];
259 return lhe
.compare(rhe
);
262 void StringPool::sortByConfig()
264 LOG_ALWAYS_FATAL_IF(mSorted
, "Can't sort string pool containing identifiers.");
265 LOG_ALWAYS_FATAL_IF(mIdents
.size() > 0, "Can't sort string pool containing identifiers.");
266 LOG_ALWAYS_FATAL_IF(mOriginalPosToNewPos
.size() > 0, "Can't sort string pool after already sorted.");
268 const size_t N
= mEntryArray
.size();
270 // This is a vector that starts out with a 1:1 mapping to entries
271 // in the array, which we will sort to come up with the desired order.
272 // At that point it maps from the new position in the array to the
273 // original position the entry appeared.
274 Vector
<size_t> newPosToOriginalPos
;
275 for (size_t i
=0; i
<mEntryArray
.size(); i
++) {
276 newPosToOriginalPos
.add(i
);
280 NOISY(printf("SORTING STRINGS BY CONFIGURATION...\n"));
281 newPosToOriginalPos
.sort(config_sort
, this);
282 NOISY(printf("DONE SORTING STRINGS BY CONFIGURATION.\n"));
284 // Create the reverse mapping from the original position in the array
285 // to the new position where it appears in the sorted array. This is
286 // so that clients can re-map any positions they had previously stored.
287 mOriginalPosToNewPos
= newPosToOriginalPos
;
288 for (size_t i
=0; i
<N
; i
++) {
289 mOriginalPosToNewPos
.editItemAt(newPosToOriginalPos
[i
]) = i
;
293 SortedVector
<entry
> entries
;
295 for (size_t i
=0; i
<N
; i
++) {
296 printf("#%d was %d: %s\n", i
, newPosToOriginalPos
[i
],
297 mEntries
[mEntryArray
[newPosToOriginalPos
[i
]]].makeConfigsString().string());
298 entries
.add(mEntries
[mEntryArray
[i
]]);
301 for (size_t i
=0; i
<entries
.size(); i
++) {
302 printf("Sorted config #%d: %s\n", i
,
303 entries
[i
].makeConfigsString().string());
307 // Now we rebuild the arrays.
308 Vector
<entry
> newEntries
;
309 Vector
<size_t> newEntryArray
;
310 Vector
<entry_style
> newEntryStyleArray
;
311 DefaultKeyedVector
<size_t, size_t> origOffsetToNewOffset
;
313 for (size_t i
=0; i
<N
; i
++) {
314 // We are filling in new offset 'i'; oldI is where we can find it
315 // in the original data structure.
316 size_t oldI
= newPosToOriginalPos
[i
];
317 // This is the actual entry associated with the old offset.
318 const entry
& oldEnt
= mEntries
[mEntryArray
[oldI
]];
319 // This is the same entry the last time we added it to the
320 // new entry array, if any.
321 ssize_t newIndexOfOffset
= origOffsetToNewOffset
.indexOfKey(oldI
);
323 if (newIndexOfOffset
< 0) {
324 // This is the first time we have seen the entry, so add
326 newOffset
= newEntries
.add(oldEnt
);
327 newEntries
.editItemAt(newOffset
).indices
.clear();
329 // We have seen this entry before, use the existing one
330 // instead of adding it again.
331 newOffset
= origOffsetToNewOffset
.valueAt(newIndexOfOffset
);
333 // Update the indices to include this new position.
334 newEntries
.editItemAt(newOffset
).indices
.add(i
);
335 // And add the offset of the entry to the new entry array.
336 newEntryArray
.add(newOffset
);
337 // Add any old style to the new style array.
338 if (mEntryStyleArray
.size() > 0) {
339 if (oldI
< mEntryStyleArray
.size()) {
340 newEntryStyleArray
.add(mEntryStyleArray
[oldI
]);
342 newEntryStyleArray
.add(entry_style());
347 // Now trim any entries at the end of the new style array that are
349 for (ssize_t i
=newEntryStyleArray
.size()-1; i
>=0; i
--) {
350 const entry_style
& style
= newEntryStyleArray
[i
];
351 if (style
.spans
.size() > 0) {
355 // This one is not needed; remove.
356 newEntryStyleArray
.removeAt(i
);
359 // All done, install the new data structures and upate mValues with
360 // the new positions.
361 mEntries
= newEntries
;
362 mEntryArray
= newEntryArray
;
363 mEntryStyleArray
= newEntryStyleArray
;
365 for (size_t i
=0; i
<mEntries
.size(); i
++) {
366 const entry
& ent
= mEntries
[i
];
367 mValues
.add(ent
.value
, ent
.indices
[0]);
371 printf("FINAL SORTED STRING CONFIGS:\n");
372 for (size_t i
=0; i
<mEntries
.size(); i
++) {
373 const entry
& ent
= mEntries
[i
];
374 printf("#" ZD
" %s: %s\n", (ZD_TYPE
)i
, ent
.makeConfigsString().string(),
375 String8(ent
.value
).string());
380 sp
<AaptFile
> StringPool::createStringBlock()
382 sp
<AaptFile
> pool
= new AaptFile(String8(), AaptGroupEntry(),
384 status_t err
= writeStringBlock(pool
);
385 return err
== NO_ERROR
? pool
: NULL
;
388 #define ENCODE_LENGTH(str, chrsz, strSize) \
390 size_t maxMask = 1 << ((chrsz*8)-1); \
391 size_t maxSize = maxMask-1; \
392 if (strSize > maxSize) { \
393 *str++ = maxMask | ((strSize>>(chrsz*8))&maxSize); \
398 status_t
StringPool::writeStringBlock(const sp
<AaptFile
>& pool
)
400 // Allow appending. Sorry this is a little wacky.
401 if (pool
->getSize() > 0) {
402 sp
<AaptFile
> block
= createStringBlock();
404 return UNKNOWN_ERROR
;
406 ssize_t res
= pool
->writeData(block
->getData(), block
->getSize());
407 return (res
>= 0) ? (status_t
)NO_ERROR
: res
;
410 // First we need to add all style span names to the string pool.
411 // We do this now (instead of when the span is added) so that these
412 // will appear at the end of the pool, not disrupting the order
413 // our client placed their own strings in it.
415 const size_t STYLES
= mEntryStyleArray
.size();
418 for (i
=0; i
<STYLES
; i
++) {
419 entry_style
& style
= mEntryStyleArray
.editItemAt(i
);
420 const size_t N
= style
.spans
.size();
421 for (size_t i
=0; i
<N
; i
++) {
422 entry_style_span
& span
= style
.spans
.editItemAt(i
);
423 ssize_t idx
= add(span
.name
, true);
425 fprintf(stderr
, "Error adding span for style tag '%s'\n",
426 String8(span
.name
).string());
429 span
.span
.name
.index
= (uint32_t)idx
;
433 const size_t ENTRIES
= size();
435 // Now build the pool of unique strings.
437 const size_t STRINGS
= mEntries
.size();
438 const size_t preSize
= sizeof(ResStringPool_header
)
439 + (sizeof(uint32_t)*ENTRIES
)
440 + (sizeof(uint32_t)*STYLES
);
441 if (pool
->editData(preSize
) == NULL
) {
442 fprintf(stderr
, "ERROR: Out of memory for string pool\n");
446 const size_t charSize
= mUTF8
? sizeof(uint8_t) : sizeof(char16_t);
449 for (i
=0; i
<STRINGS
; i
++) {
450 entry
& ent
= mEntries
.editItemAt(i
);
451 const size_t strSize
= (ent
.value
.size());
452 const size_t lenSize
= strSize
> (size_t)(1<<((charSize
*8)-1))-1 ?
453 charSize
*2 : charSize
;
457 encStr
= String8(ent
.value
);
460 const size_t encSize
= mUTF8
? encStr
.size() : 0;
461 const size_t encLenSize
= mUTF8
?
462 (encSize
> (size_t)(1<<((charSize
*8)-1))-1 ?
463 charSize
*2 : charSize
) : 0;
467 const size_t totalSize
= lenSize
+ encLenSize
+
468 ((mUTF8
? encSize
: strSize
)+1)*charSize
;
470 void* dat
= (void*)pool
->editData(preSize
+ strPos
+ totalSize
);
472 fprintf(stderr
, "ERROR: Out of memory for string pool\n");
475 dat
= (uint8_t*)dat
+ preSize
+ strPos
;
477 uint8_t* strings
= (uint8_t*)dat
;
479 ENCODE_LENGTH(strings
, sizeof(uint8_t), strSize
)
481 ENCODE_LENGTH(strings
, sizeof(uint8_t), encSize
)
483 strncpy((char*)strings
, encStr
, encSize
+1);
485 uint16_t* strings
= (uint16_t*)dat
;
487 ENCODE_LENGTH(strings
, sizeof(uint16_t), strSize
)
489 strcpy16_htod(strings
, ent
.value
);
495 // Pad ending string position up to a uint32_t boundary.
498 size_t padPos
= ((strPos
+3)&~0x3);
499 uint8_t* dat
= (uint8_t*)pool
->editData(preSize
+ padPos
);
501 fprintf(stderr
, "ERROR: Out of memory padding string pool\n");
504 memset(dat
+preSize
+strPos
, 0, padPos
-strPos
);
508 // Build the pool of style spans.
510 size_t styPos
= strPos
;
511 for (i
=0; i
<STYLES
; i
++) {
512 entry_style
& ent
= mEntryStyleArray
.editItemAt(i
);
513 const size_t N
= ent
.spans
.size();
514 const size_t totalSize
= (N
*sizeof(ResStringPool_span
))
515 + sizeof(ResStringPool_ref
);
517 ent
.offset
= styPos
-strPos
;
518 uint8_t* dat
= (uint8_t*)pool
->editData(preSize
+ styPos
+ totalSize
);
520 fprintf(stderr
, "ERROR: Out of memory for string styles\n");
523 ResStringPool_span
* span
= (ResStringPool_span
*)(dat
+preSize
+styPos
);
524 for (size_t i
=0; i
<N
; i
++) {
525 span
->name
.index
= htodl(ent
.spans
[i
].span
.name
.index
);
526 span
->firstChar
= htodl(ent
.spans
[i
].span
.firstChar
);
527 span
->lastChar
= htodl(ent
.spans
[i
].span
.lastChar
);
530 span
->name
.index
= htodl(ResStringPool_span::END
);
536 // Add full terminator at the end (when reading we validate that
537 // the end of the pool is fully terminated to simplify error
539 size_t extra
= sizeof(ResStringPool_span
)-sizeof(ResStringPool_ref
);
540 uint8_t* dat
= (uint8_t*)pool
->editData(preSize
+ styPos
+ extra
);
542 fprintf(stderr
, "ERROR: Out of memory for string styles\n");
545 uint32_t* p
= (uint32_t*)(dat
+preSize
+styPos
);
547 *p
++ = htodl(ResStringPool_span::END
);
548 extra
-= sizeof(uint32_t);
555 ResStringPool_header
* header
=
556 (ResStringPool_header
*)pool
->padData(sizeof(uint32_t));
557 if (header
== NULL
) {
558 fprintf(stderr
, "ERROR: Out of memory for string pool\n");
561 memset(header
, 0, sizeof(*header
));
562 header
->header
.type
= htods(RES_STRING_POOL_TYPE
);
563 header
->header
.headerSize
= htods(sizeof(*header
));
564 header
->header
.size
= htodl(pool
->getSize());
565 header
->stringCount
= htodl(ENTRIES
);
566 header
->styleCount
= htodl(STYLES
);
568 header
->flags
|= htodl(ResStringPool_header::SORTED_FLAG
);
571 header
->flags
|= htodl(ResStringPool_header::UTF8_FLAG
);
573 header
->stringsStart
= htodl(preSize
);
574 header
->stylesStart
= htodl(STYLES
> 0 ? (preSize
+strPos
) : 0);
576 // Write string index array.
578 uint32_t* index
= (uint32_t*)(header
+1);
580 for (i
=0; i
<ENTRIES
; i
++) {
581 entry
& ent
= const_cast<entry
&>(entryAt(i
));
584 *index
++ = htodl(ent
.offset
);
587 for (i
=0; i
<ENTRIES
; i
++) {
588 entry
& ent
= mEntries
.editItemAt(mEntryArray
[i
]);
589 *index
++ = htodl(ent
.offset
);
590 NOISY(printf("Writing entry #%d: \"%s\" ent=%d off=%d\n", i
,
591 String8(ent
.value
).string(),
592 mEntryArray
[i
], ent
.offset
));
596 // Write style index array.
599 for (i
=0; i
<STYLES
; i
++) {
600 LOG_ALWAYS_FATAL("Shouldn't be here!");
603 for (i
=0; i
<STYLES
; i
++) {
604 *index
++ = htodl(mEntryStyleArray
[i
].offset
);
611 ssize_t
StringPool::offsetForString(const String16
& val
) const
613 const Vector
<size_t>* indices
= offsetsForString(val
);
614 ssize_t res
= indices
!= NULL
&& indices
->size() > 0 ? indices
->itemAt(0) : -1;
615 NOISY(printf("Offset for string %s: %d (%s)\n", String8(val
).string(), res
,
616 res
>= 0 ? String8(mEntries
[mEntryArray
[res
]].value
).string() : String8()));
620 const Vector
<size_t>* StringPool::offsetsForString(const String16
& val
) const
622 ssize_t pos
= mValues
.valueFor(val
);
626 return &mEntries
[mEntryArray
[pos
]].indices
;