// // Copyright 2006 The Android Open Source Project // // Build resource files from raw assets. // #include "StringPool.h" #include #include #include #include "ResourceTable.h" // Set to true for noisy debug output. static const bool kIsDebug = false; #if __cplusplus >= 201103L void strcpy16_htod(char16_t* dst, const char16_t* src) { while (*src) { char16_t s = htods(*src); *dst++ = s; src++; } *dst = 0; } #endif void strcpy16_htod(uint16_t* dst, const char16_t* src) { while (*src) { uint16_t s = htods(static_cast(*src)); *dst++ = s; src++; } *dst = 0; } void printStringPool(const ResStringPool* pool) { if (pool->getError() == NO_INIT) { printf("String pool is unitialized.\n"); return; } else if (pool->getError() != NO_ERROR) { printf("String pool is corrupt/invalid.\n"); return; } SortedVector uniqueStrings; const size_t N = pool->size(); for (size_t i=0; iisUTF8()) { uniqueStrings.add(UnpackOptionalString(pool->string8At(i), &len)); } else { uniqueStrings.add(UnpackOptionalString(pool->stringAt(i), &len)); } } printf("String pool of " ZD " unique %s %s strings, " ZD " entries and " ZD " styles using " ZD " bytes:\n", (ZD_TYPE)uniqueStrings.size(), pool->isUTF8() ? "UTF-8" : "UTF-16", pool->isSorted() ? "sorted" : "non-sorted", (ZD_TYPE)N, (ZD_TYPE)pool->styleCount(), (ZD_TYPE)pool->bytes()); const size_t NS = pool->size(); for (size_t s=0; sstring8ObjectAt(s); printf("String #" ZD ": %s\n", (ZD_TYPE) s, (str.has_value() ? str->string() : "")); } } String8 StringPool::entry::makeConfigsString() const { String8 configStr(configTypeName); if (configStr.size() > 0) configStr.append(" "); if (configs.size() > 0) { for (size_t j=0; j 0) configStr.append(", "); configStr.append(configs[j].toString()); } } else { configStr = "(none)"; } return configStr; } int StringPool::entry::compare(const entry& o) const { // Strings with styles go first, to reduce the size of the styles array. // We don't care about the relative order of these strings. if (hasStyles) { return o.hasStyles ? 0 : -1; } if (o.hasStyles) { return 1; } // Sort unstyled strings by type, then by logical configuration. int comp = configTypeName.compare(o.configTypeName); if (comp != 0) { return comp; } const size_t LHN = configs.size(); const size_t RHN = o.configs.size(); size_t i=0; while (i < LHN && i < RHN) { comp = configs[i].compareLogical(o.configs[i]); if (comp != 0) { return comp; } i++; } if (LHN < RHN) return -1; else if (LHN > RHN) return 1; return 0; } StringPool::StringPool(bool utf8) : mUTF8(utf8), mValues(-1) { } ssize_t StringPool::add(const String16& value, const Vector& spans, const String8* configTypeName, const ResTable_config* config) { ssize_t res = add(value, false, configTypeName, config); if (res >= 0) { addStyleSpans(res, spans); } return res; } ssize_t StringPool::add(const String16& value, bool mergeDuplicates, const String8* configTypeName, const ResTable_config* config) { ssize_t vidx = mValues.indexOfKey(value); ssize_t pos = vidx >= 0 ? mValues.valueAt(vidx) : -1; ssize_t eidx = pos >= 0 ? mEntryArray.itemAt(pos) : -1; if (eidx < 0) { eidx = mEntries.add(entry(value)); if (eidx < 0) { fprintf(stderr, "Failure adding string %s\n", String8(value).string()); return eidx; } } if (configTypeName != NULL) { entry& ent = mEntries.editItemAt(eidx); if (kIsDebug) { printf("*** adding config type name %s, was %s\n", configTypeName->string(), ent.configTypeName.string()); } if (ent.configTypeName.size() <= 0) { ent.configTypeName = *configTypeName; } else if (ent.configTypeName != *configTypeName) { ent.configTypeName = " "; } } if (config != NULL) { // Add this to the set of configs associated with the string. entry& ent = mEntries.editItemAt(eidx); size_t addPos; for (addPos=0; addPos= 0) { if (cmp > 0) { if (kIsDebug) { printf("*** inserting config: %s\n", config->toString().string()); } ent.configs.insertAt(*config, addPos); } break; } } if (addPos >= ent.configs.size()) { if (kIsDebug) { printf("*** adding config: %s\n", config->toString().string()); } ent.configs.add(*config); } } const bool first = vidx < 0; const bool styled = (pos >= 0 && (size_t)pos < mEntryStyleArray.size()) ? mEntryStyleArray[pos].spans.size() : 0; if (first || styled || !mergeDuplicates) { pos = mEntryArray.add(eidx); if (first) { vidx = mValues.add(value, pos); } entry& ent = mEntries.editItemAt(eidx); ent.indices.add(pos); } if (kIsDebug) { printf("Adding string %s to pool: pos=%zd eidx=%zd vidx=%zd\n", String8(value).string(), pos, eidx, vidx); } return pos; } status_t StringPool::addStyleSpan(size_t idx, const String16& name, uint32_t start, uint32_t end) { entry_style_span span; span.name = name; span.span.firstChar = start; span.span.lastChar = end; return addStyleSpan(idx, span); } status_t StringPool::addStyleSpans(size_t idx, const Vector& spans) { const size_t N=spans.size(); for (size_t i=0; i 0, "Can't sort string pool after already sorted."); const size_t N = mEntryArray.size(); // This is a vector that starts out with a 1:1 mapping to entries // in the array, which we will sort to come up with the desired order. // At that point it maps from the new position in the array to the // original position the entry appeared. Vector newPosToOriginalPos; newPosToOriginalPos.setCapacity(N); for (size_t i=0; i < N; i++) { newPosToOriginalPos.add(i); } // Sort the array. if (kIsDebug) { printf("SORTING STRINGS BY CONFIGURATION...\n"); } ConfigSorter sorter(*this); std::sort(newPosToOriginalPos.begin(), newPosToOriginalPos.end(), sorter); if (kIsDebug) { printf("DONE SORTING STRINGS BY CONFIGURATION.\n"); } // Create the reverse mapping from the original position in the array // to the new position where it appears in the sorted array. This is // so that clients can re-map any positions they had previously stored. mOriginalPosToNewPos = newPosToOriginalPos; for (size_t i=0; i entries; for (size_t i=0; i newEntries; Vector newEntryArray; Vector newEntryStyleArray; DefaultKeyedVector origOffsetToNewOffset; for (size_t i=0; i 0) { if (oldI < mEntryStyleArray.size()) { newEntryStyleArray.add(mEntryStyleArray[oldI]); } else { newEntryStyleArray.add(entry_style()); } } } // Now trim any entries at the end of the new style array that are // not needed. for (ssize_t i=newEntryStyleArray.size()-1; i>=0; i--) { const entry_style& style = newEntryStyleArray[i]; if (style.spans.size() > 0) { // That's it. break; } // This one is not needed; remove. newEntryStyleArray.removeAt(i); } // All done, install the new data structures and upate mValues with // the new positions. mEntries = newEntries; mEntryArray = newEntryArray; mEntryStyleArray = newEntryStyleArray; mValues.clear(); for (size_t i=0; i StringPool::createStringBlock() { sp pool = new AaptFile(String8(), AaptGroupEntry(), String8()); status_t err = writeStringBlock(pool); return err == NO_ERROR ? pool : NULL; } #define ENCODE_LENGTH(str, chrsz, strSize) \ { \ size_t maxMask = 1 << (((chrsz)*8)-1); \ size_t maxSize = maxMask-1; \ if ((strSize) > maxSize) { \ *(str)++ = maxMask | (((strSize)>>((chrsz)*8))&maxSize); \ } \ *(str)++ = strSize; \ } status_t StringPool::writeStringBlock(const sp& pool) { // Allow appending. Sorry this is a little wacky. if (pool->getSize() > 0) { sp block = createStringBlock(); if (block == NULL) { return UNKNOWN_ERROR; } ssize_t res = pool->writeData(block->getData(), block->getSize()); return (res >= 0) ? (status_t)NO_ERROR : res; } // First we need to add all style span names to the string pool. // We do this now (instead of when the span is added) so that these // will appear at the end of the pool, not disrupting the order // our client placed their own strings in it. const size_t STYLES = mEntryStyleArray.size(); size_t i; for (i=0; ieditData(preSize) == NULL) { fprintf(stderr, "ERROR: Out of memory for string pool\n"); return NO_MEMORY; } const size_t charSize = mUTF8 ? sizeof(uint8_t) : sizeof(uint16_t); size_t strPos = 0; for (i=0; i (size_t)(1<<((charSize*8)-1))-1 ? charSize*2 : charSize; String8 encStr; if (mUTF8) { encStr = String8(ent.value); } const size_t encSize = mUTF8 ? encStr.size() : 0; const size_t encLenSize = mUTF8 ? (encSize > (size_t)(1<<((charSize*8)-1))-1 ? charSize*2 : charSize) : 0; ent.offset = strPos; const size_t totalSize = lenSize + encLenSize + ((mUTF8 ? encSize : strSize)+1)*charSize; void* dat = (void*)pool->editData(preSize + strPos + totalSize); if (dat == NULL) { fprintf(stderr, "ERROR: Out of memory for string pool\n"); return NO_MEMORY; } dat = (uint8_t*)dat + preSize + strPos; if (mUTF8) { uint8_t* strings = (uint8_t*)dat; ENCODE_LENGTH(strings, sizeof(uint8_t), strSize) ENCODE_LENGTH(strings, sizeof(uint8_t), encSize) strncpy((char*)strings, encStr, encSize+1); } else { char16_t* strings = (char16_t*)dat; ENCODE_LENGTH(strings, sizeof(char16_t), strSize) strcpy16_htod(strings, ent.value); } strPos += totalSize; } // Pad ending string position up to a uint32_t boundary. if (strPos&0x3) { size_t padPos = ((strPos+3)&~0x3); uint8_t* dat = (uint8_t*)pool->editData(preSize + padPos); if (dat == NULL) { fprintf(stderr, "ERROR: Out of memory padding string pool\n"); return NO_MEMORY; } memset(dat+preSize+strPos, 0, padPos-strPos); strPos = padPos; } // Build the pool of style spans. size_t styPos = strPos; for (i=0; ieditData(preSize + styPos + totalSize); if (dat == NULL) { fprintf(stderr, "ERROR: Out of memory for string styles\n"); return NO_MEMORY; } ResStringPool_span* span = (ResStringPool_span*)(dat+preSize+styPos); for (size_t i=0; iname.index = htodl(ent.spans[i].span.name.index); span->firstChar = htodl(ent.spans[i].span.firstChar); span->lastChar = htodl(ent.spans[i].span.lastChar); span++; } span->name.index = htodl(ResStringPool_span::END); styPos += totalSize; } if (STYLES > 0) { // Add full terminator at the end (when reading we validate that // the end of the pool is fully terminated to simplify error // checking). size_t extra = sizeof(ResStringPool_span)-sizeof(ResStringPool_ref); uint8_t* dat = (uint8_t*)pool->editData(preSize + styPos + extra); if (dat == NULL) { fprintf(stderr, "ERROR: Out of memory for string styles\n"); return NO_MEMORY; } uint32_t* p = (uint32_t*)(dat+preSize+styPos); while (extra > 0) { *p++ = htodl(ResStringPool_span::END); extra -= sizeof(uint32_t); } styPos += extra; } // Write header. ResStringPool_header* header = (ResStringPool_header*)pool->padData(sizeof(uint32_t)); if (header == NULL) { fprintf(stderr, "ERROR: Out of memory for string pool\n"); return NO_MEMORY; } memset(header, 0, sizeof(*header)); header->header.type = htods(RES_STRING_POOL_TYPE); header->header.headerSize = htods(sizeof(*header)); header->header.size = htodl(pool->getSize()); header->stringCount = htodl(ENTRIES); header->styleCount = htodl(STYLES); if (mUTF8) { header->flags |= htodl(ResStringPool_header::UTF8_FLAG); } header->stringsStart = htodl(preSize); header->stylesStart = htodl(STYLES > 0 ? (preSize+strPos) : 0); // Write string index array. uint32_t* index = (uint32_t*)(header+1); for (i=0; i* indices = offsetsForString(val); ssize_t res = indices != NULL && indices->size() > 0 ? indices->itemAt(0) : -1; if (kIsDebug) { printf("Offset for string %s: %zd (%s)\n", String8(val).string(), res, res >= 0 ? String8(mEntries[mEntryArray[res]].value).string() : String8()); } return res; } const Vector* StringPool::offsetsForString(const String16& val) const { ssize_t pos = mValues.valueFor(val); if (pos < 0) { return NULL; } return &mEntries[mEntryArray[pos]].indices; }