StringPool.cpp revision 6c997a9e880e08c354ffd809bd62df9e25e9c4d4
1//
2// Copyright 2006 The Android Open Source Project
3//
4// Build resource files from raw assets.
5//
6
7#include "StringPool.h"
8#include "ResourceTable.h"
9
10#include <utils/ByteOrder.h>
11#include <utils/SortedVector.h>
12
13#if HAVE_PRINTF_ZD
14#  define ZD "%zd"
15#  define ZD_TYPE ssize_t
16#else
17#  define ZD "%ld"
18#  define ZD_TYPE long
19#endif
20
21#define NOISY(x) //x
22
23void strcpy16_htod(uint16_t* dst, const uint16_t* src)
24{
25    while (*src) {
26        char16_t s = htods(*src);
27        *dst++ = s;
28        src++;
29    }
30    *dst = 0;
31}
32
33void printStringPool(const ResStringPool* pool)
34{
35    SortedVector<const void*> uniqueStrings;
36    const size_t N = pool->size();
37    for (size_t i=0; i<N; i++) {
38        size_t len;
39        if (pool->isUTF8()) {
40            uniqueStrings.add(pool->string8At(i, &len));
41        } else {
42            uniqueStrings.add(pool->stringAt(i, &len));
43        }
44    }
45
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());
51
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());
56    }
57}
58
59String8 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());
66        }
67    } else {
68        configStr = "(none)";
69    }
70    return configStr;
71}
72
73int StringPool::entry::compare(const entry& o) const {
74    // Strings with styles go first, to reduce the size of the
75    // styles array.
76    if (hasStyles) {
77        return o.hasStyles ? 0 : -1;
78    }
79    if (o.hasStyles) {
80        return 1;
81    }
82    int comp = configTypeName.compare(o.configTypeName);
83    if (comp != 0) {
84        return comp;
85    }
86    const size_t LHN = configs.size();
87    const size_t RHN = o.configs.size();
88    size_t i=0;
89    while (i < LHN && i < RHN) {
90        comp = configs[i].compareLogical(o.configs[i]);
91        if (comp != 0) {
92            return comp;
93        }
94        i++;
95    }
96    if (LHN < RHN) return -1;
97    else if (LHN > RHN) return 1;
98    return 0;
99}
100
101StringPool::StringPool(bool sorted, bool utf8)
102    : mSorted(sorted), mUTF8(utf8), mValues(-1), mIdents(-1)
103{
104}
105
106ssize_t StringPool::add(const String16& value, bool mergeDuplicates,
107        const String8* configTypeName, const ResTable_config* config)
108{
109    return add(String16(), value, mergeDuplicates, configTypeName, config);
110}
111
112ssize_t StringPool::add(const String16& value, const Vector<entry_style_span>& spans,
113        const String8* configTypeName, const ResTable_config* config)
114{
115    ssize_t res = add(String16(), value, false, configTypeName, config);
116    if (res >= 0) {
117        addStyleSpans(res, spans);
118    }
119    return res;
120}
121
122ssize_t StringPool::add(const String16& ident, const String16& value,
123        bool mergeDuplicates, const String8* configTypeName, const ResTable_config* config)
124{
125    if (ident.size() > 0) {
126        ssize_t idx = mIdents.valueFor(ident);
127        if (idx >= 0) {
128            fprintf(stderr, "ERROR: Duplicate string identifier %s\n",
129                    String8(mEntries[idx].value).string());
130            return UNKNOWN_ERROR;
131        }
132    }
133
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;
137    if (eidx < 0) {
138        eidx = mEntries.add(entry(value));
139        if (eidx < 0) {
140            fprintf(stderr, "Failure adding string %s\n", String8(value).string());
141            return eidx;
142        }
143    }
144
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 = " ";
153        }
154    }
155
156    if (config != NULL) {
157        // Add this to the set of configs associated with the string.
158        entry& ent = mEntries.editItemAt(eidx);
159        size_t addPos;
160        for (addPos=0; addPos<ent.configs.size(); addPos++) {
161            int cmp = ent.configs.itemAt(addPos).compareLogical(*config);
162            if (cmp >= 0) {
163                if (cmp > 0) {
164                    NOISY(printf("*** inserting config: %s\n", config->toString().string()));
165                    ent.configs.insertAt(*config, addPos);
166                }
167                break;
168            }
169        }
170        if (addPos >= ent.configs.size()) {
171            NOISY(printf("*** adding config: %s\n", config->toString().string()));
172            ent.configs.add(*config);
173        }
174    }
175
176    const bool first = vidx < 0;
177    if (first || !mergeDuplicates) {
178        pos = mEntryArray.add(eidx);
179        if (first) {
180            vidx = mValues.add(value, pos);
181        }
182        if (!mSorted) {
183            entry& ent = mEntries.editItemAt(eidx);
184            ent.indices.add(pos);
185        }
186    }
187
188    if (ident.size() > 0) {
189        mIdents.add(ident, vidx);
190    }
191
192    NOISY(printf("Adding string %s to pool: pos=%d eidx=%d vidx=%d\n",
193            String8(value).string(), pos, eidx, vidx));
194
195    return pos;
196}
197
198status_t StringPool::addStyleSpan(size_t idx, const String16& name,
199                                  uint32_t start, uint32_t end)
200{
201    entry_style_span span;
202    span.name = name;
203    span.span.firstChar = start;
204    span.span.lastChar = end;
205    return addStyleSpan(idx, span);
206}
207
208status_t StringPool::addStyleSpans(size_t idx, const Vector<entry_style_span>& spans)
209{
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) {
214            return err;
215        }
216    }
217    return NO_ERROR;
218}
219
220status_t StringPool::addStyleSpan(size_t idx, const entry_style_span& span)
221{
222    LOG_ALWAYS_FATAL_IF(mSorted, "Can't use styles with sorted string pools.");
223
224    // Place blank entries in the span array up to this index.
225    while (mEntryStyleArray.size() <= idx) {
226        mEntryStyleArray.add();
227    }
228
229    entry_style& style = mEntryStyleArray.editItemAt(idx);
230    style.spans.add(span);
231    mEntries.editItemAt(mEntryArray[idx]).hasStyles = true;
232    return NO_ERROR;
233}
234
235size_t StringPool::size() const
236{
237    return mSorted ? mValues.size() : mEntryArray.size();
238}
239
240const StringPool::entry& StringPool::entryAt(size_t idx) const
241{
242    if (!mSorted) {
243        return mEntries[mEntryArray[idx]];
244    } else {
245        return mEntries[mEntryArray[mValues.valueAt(idx)]];
246    }
247}
248
249size_t StringPool::countIdentifiers() const
250{
251    return mIdents.size();
252}
253
254int StringPool::config_sort(const size_t* lhs, const size_t* rhs, void* state)
255{
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);
260}
261
262void StringPool::sortByConfig()
263{
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.");
267
268    const size_t N = mEntryArray.size();
269
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);
277    }
278
279    // Sort the array.
280    NOISY(printf("SORTING STRINGS BY CONFIGURATION...\n"));
281    newPosToOriginalPos.sort(config_sort, this);
282    NOISY(printf("DONE SORTING STRINGS BY CONFIGURATION.\n"));
283
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;
290    }
291
292#if 0
293    SortedVector<entry> entries;
294
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]]);
299    }
300
301    for (size_t i=0; i<entries.size(); i++) {
302        printf("Sorted config #%d: %s\n", i,
303                entries[i].makeConfigsString().string());
304    }
305#endif
306
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;
312
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);
322        size_t newOffset;
323        if (newIndexOfOffset < 0) {
324            // This is the first time we have seen the entry, so add
325            // it.
326            newOffset = newEntries.add(oldEnt);
327            newEntries.editItemAt(newOffset).indices.clear();
328        } else {
329            // We have seen this entry before, use the existing one
330            // instead of adding it again.
331            newOffset = origOffsetToNewOffset.valueAt(newIndexOfOffset);
332        }
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]);
341            } else {
342                newEntryStyleArray.add(entry_style());
343            }
344        }
345    }
346
347    // Now trim any entries at the end of the new style array that are
348    // not needed.
349    for (ssize_t i=newEntryStyleArray.size()-1; i>=0; i--) {
350        const entry_style& style = newEntryStyleArray[i];
351        if (style.spans.size() > 0) {
352            // That's it.
353            break;
354        }
355        // This one is not needed; remove.
356        newEntryStyleArray.removeAt(i);
357    }
358
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;
364    mValues.clear();
365    for (size_t i=0; i<mEntries.size(); i++) {
366        const entry& ent = mEntries[i];
367        mValues.add(ent.value, ent.indices[0]);
368    }
369
370#if 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());
376    }
377#endif
378}
379
380sp<AaptFile> StringPool::createStringBlock()
381{
382    sp<AaptFile> pool = new AaptFile(String8(), AaptGroupEntry(),
383                                     String8());
384    status_t err = writeStringBlock(pool);
385    return err == NO_ERROR ? pool : NULL;
386}
387
388#define ENCODE_LENGTH(str, chrsz, strSize) \
389{ \
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); \
394    } \
395    *str++ = strSize; \
396}
397
398status_t StringPool::writeStringBlock(const sp<AaptFile>& pool)
399{
400    // Allow appending.  Sorry this is a little wacky.
401    if (pool->getSize() > 0) {
402        sp<AaptFile> block = createStringBlock();
403        if (block == NULL) {
404            return UNKNOWN_ERROR;
405        }
406        ssize_t res = pool->writeData(block->getData(), block->getSize());
407        return (res >= 0) ? (status_t)NO_ERROR : res;
408    }
409
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.
414
415    const size_t STYLES = mEntryStyleArray.size();
416    size_t i;
417
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);
424            if (idx < 0) {
425                fprintf(stderr, "Error adding span for style tag '%s'\n",
426                        String8(span.name).string());
427                return idx;
428            }
429            span.span.name.index = (uint32_t)idx;
430        }
431    }
432
433    const size_t ENTRIES = size();
434
435    // Now build the pool of unique strings.
436
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");
443        return NO_MEMORY;
444    }
445
446    const size_t charSize = mUTF8 ? sizeof(uint8_t) : sizeof(char16_t);
447
448    size_t strPos = 0;
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;
454
455        String8 encStr;
456        if (mUTF8) {
457            encStr = String8(ent.value);
458        }
459
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;
464
465        ent.offset = strPos;
466
467        const size_t totalSize = lenSize + encLenSize +
468            ((mUTF8 ? encSize : strSize)+1)*charSize;
469
470        void* dat = (void*)pool->editData(preSize + strPos + totalSize);
471        if (dat == NULL) {
472            fprintf(stderr, "ERROR: Out of memory for string pool\n");
473            return NO_MEMORY;
474        }
475        dat = (uint8_t*)dat + preSize + strPos;
476        if (mUTF8) {
477            uint8_t* strings = (uint8_t*)dat;
478
479            ENCODE_LENGTH(strings, sizeof(uint8_t), strSize)
480
481            ENCODE_LENGTH(strings, sizeof(uint8_t), encSize)
482
483            strncpy((char*)strings, encStr, encSize+1);
484        } else {
485            uint16_t* strings = (uint16_t*)dat;
486
487            ENCODE_LENGTH(strings, sizeof(uint16_t), strSize)
488
489            strcpy16_htod(strings, ent.value);
490        }
491
492        strPos += totalSize;
493    }
494
495    // Pad ending string position up to a uint32_t boundary.
496
497    if (strPos&0x3) {
498        size_t padPos = ((strPos+3)&~0x3);
499        uint8_t* dat = (uint8_t*)pool->editData(preSize + padPos);
500        if (dat == NULL) {
501            fprintf(stderr, "ERROR: Out of memory padding string pool\n");
502            return NO_MEMORY;
503        }
504        memset(dat+preSize+strPos, 0, padPos-strPos);
505        strPos = padPos;
506    }
507
508    // Build the pool of style spans.
509
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);
516
517        ent.offset = styPos-strPos;
518        uint8_t* dat = (uint8_t*)pool->editData(preSize + styPos + totalSize);
519        if (dat == NULL) {
520            fprintf(stderr, "ERROR: Out of memory for string styles\n");
521            return NO_MEMORY;
522        }
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);
528            span++;
529        }
530        span->name.index = htodl(ResStringPool_span::END);
531
532        styPos += totalSize;
533    }
534
535    if (STYLES > 0) {
536        // Add full terminator at the end (when reading we validate that
537        // the end of the pool is fully terminated to simplify error
538        // checking).
539        size_t extra = sizeof(ResStringPool_span)-sizeof(ResStringPool_ref);
540        uint8_t* dat = (uint8_t*)pool->editData(preSize + styPos + extra);
541        if (dat == NULL) {
542            fprintf(stderr, "ERROR: Out of memory for string styles\n");
543            return NO_MEMORY;
544        }
545        uint32_t* p = (uint32_t*)(dat+preSize+styPos);
546        while (extra > 0) {
547            *p++ = htodl(ResStringPool_span::END);
548            extra -= sizeof(uint32_t);
549        }
550        styPos += extra;
551    }
552
553    // Write header.
554
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");
559        return NO_MEMORY;
560    }
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);
567    if (mSorted) {
568        header->flags |= htodl(ResStringPool_header::SORTED_FLAG);
569    }
570    if (mUTF8) {
571        header->flags |= htodl(ResStringPool_header::UTF8_FLAG);
572    }
573    header->stringsStart = htodl(preSize);
574    header->stylesStart = htodl(STYLES > 0 ? (preSize+strPos) : 0);
575
576    // Write string index array.
577
578    uint32_t* index = (uint32_t*)(header+1);
579    if (mSorted) {
580        for (i=0; i<ENTRIES; i++) {
581            entry& ent = const_cast<entry&>(entryAt(i));
582            ent.indices.clear();
583            ent.indices.add(i);
584            *index++ = htodl(ent.offset);
585        }
586    } else {
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));
593        }
594    }
595
596    // Write style index array.
597
598    if (mSorted) {
599        for (i=0; i<STYLES; i++) {
600            LOG_ALWAYS_FATAL("Shouldn't be here!");
601        }
602    } else {
603        for (i=0; i<STYLES; i++) {
604            *index++ = htodl(mEntryStyleArray[i].offset);
605        }
606    }
607
608    return NO_ERROR;
609}
610
611ssize_t StringPool::offsetForString(const String16& val) const
612{
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()));
617    return res;
618}
619
620const Vector<size_t>* StringPool::offsetsForString(const String16& val) const
621{
622    ssize_t pos = mValues.valueFor(val);
623    if (pos < 0) {
624        return NULL;
625    }
626    return &mEntries[mEntryArray[pos]].indices;
627}
628