PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/aapt/StringPool.cpp

https://github.com/dbaeumges/android_platform_frameworks_base
C++ | 411 lines | 330 code | 61 blank | 20 comment | 64 complexity | ca034aefe7d210a4ee64b262296ce099 MD5 | raw file
  1. //
  2. // Copyright 2006 The Android Open Source Project
  3. //
  4. // Build resource files from raw assets.
  5. //
  6. #include "StringPool.h"
  7. #include <utils/ByteOrder.h>
  8. #define NOISY(x) //x
  9. void strcpy16_htod(uint16_t* dst, const uint16_t* src)
  10. {
  11. while (*src) {
  12. char16_t s = htods(*src);
  13. *dst++ = s;
  14. src++;
  15. }
  16. *dst = 0;
  17. }
  18. void printStringPool(const ResStringPool* pool)
  19. {
  20. const size_t NS = pool->size();
  21. for (size_t s=0; s<NS; s++) {
  22. size_t len;
  23. const char *str = (const char*)pool->string8At(s, &len);
  24. if (str == NULL) {
  25. str = String8(pool->stringAt(s, &len)).string();
  26. }
  27. printf("String #%ld: %s\n", s, str);
  28. }
  29. }
  30. StringPool::StringPool(bool sorted, bool utf8)
  31. : mSorted(sorted), mUTF8(utf8), mValues(-1), mIdents(-1)
  32. {
  33. }
  34. ssize_t StringPool::add(const String16& value, bool mergeDuplicates)
  35. {
  36. return add(String16(), value, mergeDuplicates);
  37. }
  38. ssize_t StringPool::add(const String16& value, const Vector<entry_style_span>& spans)
  39. {
  40. ssize_t res = add(String16(), value, false);
  41. if (res >= 0) {
  42. addStyleSpans(res, spans);
  43. }
  44. return res;
  45. }
  46. ssize_t StringPool::add(const String16& ident, const String16& value,
  47. bool mergeDuplicates)
  48. {
  49. if (ident.size() > 0) {
  50. ssize_t idx = mIdents.valueFor(ident);
  51. if (idx >= 0) {
  52. fprintf(stderr, "ERROR: Duplicate string identifier %s\n",
  53. String8(mEntries[idx].value).string());
  54. return UNKNOWN_ERROR;
  55. }
  56. }
  57. ssize_t vidx = mValues.indexOfKey(value);
  58. ssize_t pos = vidx >= 0 ? mValues.valueAt(vidx) : -1;
  59. ssize_t eidx = pos >= 0 ? mEntryArray.itemAt(pos) : -1;
  60. if (eidx < 0) {
  61. eidx = mEntries.add(entry(value));
  62. if (eidx < 0) {
  63. fprintf(stderr, "Failure adding string %s\n", String8(value).string());
  64. return eidx;
  65. }
  66. }
  67. const bool first = vidx < 0;
  68. if (first || !mergeDuplicates) {
  69. pos = mEntryArray.add(eidx);
  70. if (first) {
  71. vidx = mValues.add(value, pos);
  72. const size_t N = mEntryArrayToValues.size();
  73. for (size_t i=0; i<N; i++) {
  74. size_t& e = mEntryArrayToValues.editItemAt(i);
  75. if ((ssize_t)e >= vidx) {
  76. e++;
  77. }
  78. }
  79. }
  80. mEntryArrayToValues.add(vidx);
  81. if (!mSorted) {
  82. entry& ent = mEntries.editItemAt(eidx);
  83. ent.indices.add(pos);
  84. }
  85. }
  86. if (ident.size() > 0) {
  87. mIdents.add(ident, vidx);
  88. }
  89. NOISY(printf("Adding string %s to pool: pos=%d eidx=%d vidx=%d\n",
  90. String8(value).string(), pos, eidx, vidx));
  91. return pos;
  92. }
  93. status_t StringPool::addStyleSpan(size_t idx, const String16& name,
  94. uint32_t start, uint32_t end)
  95. {
  96. entry_style_span span;
  97. span.name = name;
  98. span.span.firstChar = start;
  99. span.span.lastChar = end;
  100. return addStyleSpan(idx, span);
  101. }
  102. status_t StringPool::addStyleSpans(size_t idx, const Vector<entry_style_span>& spans)
  103. {
  104. const size_t N=spans.size();
  105. for (size_t i=0; i<N; i++) {
  106. status_t err = addStyleSpan(idx, spans[i]);
  107. if (err != NO_ERROR) {
  108. return err;
  109. }
  110. }
  111. return NO_ERROR;
  112. }
  113. status_t StringPool::addStyleSpan(size_t idx, const entry_style_span& span)
  114. {
  115. LOG_ALWAYS_FATAL_IF(mSorted, "Can't use styles with sorted string pools.");
  116. // Place blank entries in the span array up to this index.
  117. while (mEntryStyleArray.size() <= idx) {
  118. mEntryStyleArray.add();
  119. }
  120. entry_style& style = mEntryStyleArray.editItemAt(idx);
  121. style.spans.add(span);
  122. return NO_ERROR;
  123. }
  124. size_t StringPool::size() const
  125. {
  126. return mSorted ? mValues.size() : mEntryArray.size();
  127. }
  128. const StringPool::entry& StringPool::entryAt(size_t idx) const
  129. {
  130. if (!mSorted) {
  131. return mEntries[mEntryArray[idx]];
  132. } else {
  133. return mEntries[mEntryArray[mValues.valueAt(idx)]];
  134. }
  135. }
  136. size_t StringPool::countIdentifiers() const
  137. {
  138. return mIdents.size();
  139. }
  140. sp<AaptFile> StringPool::createStringBlock()
  141. {
  142. sp<AaptFile> pool = new AaptFile(String8(), AaptGroupEntry(),
  143. String8());
  144. status_t err = writeStringBlock(pool);
  145. return err == NO_ERROR ? pool : NULL;
  146. }
  147. #define ENCODE_LENGTH(str, chrsz, strSize) \
  148. { \
  149. size_t maxMask = 1 << ((chrsz*8)-1); \
  150. size_t maxSize = maxMask-1; \
  151. if (strSize > maxSize) { \
  152. *str++ = maxMask | ((strSize>>(chrsz*8))&maxSize); \
  153. } \
  154. *str++ = strSize; \
  155. }
  156. status_t StringPool::writeStringBlock(const sp<AaptFile>& pool)
  157. {
  158. // Allow appending. Sorry this is a little wacky.
  159. if (pool->getSize() > 0) {
  160. sp<AaptFile> block = createStringBlock();
  161. if (block == NULL) {
  162. return UNKNOWN_ERROR;
  163. }
  164. ssize_t res = pool->writeData(block->getData(), block->getSize());
  165. return (res >= 0) ? (status_t)NO_ERROR : res;
  166. }
  167. // First we need to add all style span names to the string pool.
  168. // We do this now (instead of when the span is added) so that these
  169. // will appear at the end of the pool, not disrupting the order
  170. // our client placed their own strings in it.
  171. const size_t STYLES = mEntryStyleArray.size();
  172. size_t i;
  173. for (i=0; i<STYLES; i++) {
  174. entry_style& style = mEntryStyleArray.editItemAt(i);
  175. const size_t N = style.spans.size();
  176. for (size_t i=0; i<N; i++) {
  177. entry_style_span& span = style.spans.editItemAt(i);
  178. ssize_t idx = add(span.name, true);
  179. if (idx < 0) {
  180. fprintf(stderr, "Error adding span for style tag '%s'\n",
  181. String8(span.name).string());
  182. return idx;
  183. }
  184. span.span.name.index = (uint32_t)idx;
  185. }
  186. }
  187. const size_t ENTRIES = size();
  188. // Now build the pool of unique strings.
  189. const size_t STRINGS = mEntries.size();
  190. const size_t preSize = sizeof(ResStringPool_header)
  191. + (sizeof(uint32_t)*ENTRIES)
  192. + (sizeof(uint32_t)*STYLES);
  193. if (pool->editData(preSize) == NULL) {
  194. fprintf(stderr, "ERROR: Out of memory for string pool\n");
  195. return NO_MEMORY;
  196. }
  197. const size_t charSize = mUTF8 ? sizeof(uint8_t) : sizeof(char16_t);
  198. size_t strPos = 0;
  199. for (i=0; i<STRINGS; i++) {
  200. entry& ent = mEntries.editItemAt(i);
  201. const size_t strSize = (ent.value.size());
  202. const size_t lenSize = strSize > (size_t)(1<<((charSize*8)-1))-1 ?
  203. charSize*2 : charSize;
  204. String8 encStr;
  205. if (mUTF8) {
  206. encStr = String8(ent.value);
  207. }
  208. const size_t encSize = mUTF8 ? encStr.size() : 0;
  209. const size_t encLenSize = mUTF8 ?
  210. (encSize > (size_t)(1<<((charSize*8)-1))-1 ?
  211. charSize*2 : charSize) : 0;
  212. ent.offset = strPos;
  213. const size_t totalSize = lenSize + encLenSize +
  214. ((mUTF8 ? encSize : strSize)+1)*charSize;
  215. void* dat = (void*)pool->editData(preSize + strPos + totalSize);
  216. if (dat == NULL) {
  217. fprintf(stderr, "ERROR: Out of memory for string pool\n");
  218. return NO_MEMORY;
  219. }
  220. dat = (uint8_t*)dat + preSize + strPos;
  221. if (mUTF8) {
  222. uint8_t* strings = (uint8_t*)dat;
  223. ENCODE_LENGTH(strings, sizeof(uint8_t), strSize)
  224. ENCODE_LENGTH(strings, sizeof(uint8_t), encSize)
  225. strncpy((char*)strings, encStr, encSize+1);
  226. } else {
  227. uint16_t* strings = (uint16_t*)dat;
  228. ENCODE_LENGTH(strings, sizeof(uint16_t), strSize)
  229. strcpy16_htod(strings, ent.value);
  230. }
  231. strPos += totalSize;
  232. }
  233. // Pad ending string position up to a uint32_t boundary.
  234. if (strPos&0x3) {
  235. size_t padPos = ((strPos+3)&~0x3);
  236. uint8_t* dat = (uint8_t*)pool->editData(preSize + padPos);
  237. if (dat == NULL) {
  238. fprintf(stderr, "ERROR: Out of memory padding string pool\n");
  239. return NO_MEMORY;
  240. }
  241. memset(dat+preSize+strPos, 0, padPos-strPos);
  242. strPos = padPos;
  243. }
  244. // Build the pool of style spans.
  245. size_t styPos = strPos;
  246. for (i=0; i<STYLES; i++) {
  247. entry_style& ent = mEntryStyleArray.editItemAt(i);
  248. const size_t N = ent.spans.size();
  249. const size_t totalSize = (N*sizeof(ResStringPool_span))
  250. + sizeof(ResStringPool_ref);
  251. ent.offset = styPos-strPos;
  252. uint8_t* dat = (uint8_t*)pool->editData(preSize + styPos + totalSize);
  253. if (dat == NULL) {
  254. fprintf(stderr, "ERROR: Out of memory for string styles\n");
  255. return NO_MEMORY;
  256. }
  257. ResStringPool_span* span = (ResStringPool_span*)(dat+preSize+styPos);
  258. for (size_t i=0; i<N; i++) {
  259. span->name.index = htodl(ent.spans[i].span.name.index);
  260. span->firstChar = htodl(ent.spans[i].span.firstChar);
  261. span->lastChar = htodl(ent.spans[i].span.lastChar);
  262. span++;
  263. }
  264. span->name.index = htodl(ResStringPool_span::END);
  265. styPos += totalSize;
  266. }
  267. if (STYLES > 0) {
  268. // Add full terminator at the end (when reading we validate that
  269. // the end of the pool is fully terminated to simplify error
  270. // checking).
  271. size_t extra = sizeof(ResStringPool_span)-sizeof(ResStringPool_ref);
  272. uint8_t* dat = (uint8_t*)pool->editData(preSize + styPos + extra);
  273. if (dat == NULL) {
  274. fprintf(stderr, "ERROR: Out of memory for string styles\n");
  275. return NO_MEMORY;
  276. }
  277. uint32_t* p = (uint32_t*)(dat+preSize+styPos);
  278. while (extra > 0) {
  279. *p++ = htodl(ResStringPool_span::END);
  280. extra -= sizeof(uint32_t);
  281. }
  282. styPos += extra;
  283. }
  284. // Write header.
  285. ResStringPool_header* header =
  286. (ResStringPool_header*)pool->padData(sizeof(uint32_t));
  287. if (header == NULL) {
  288. fprintf(stderr, "ERROR: Out of memory for string pool\n");
  289. return NO_MEMORY;
  290. }
  291. memset(header, 0, sizeof(*header));
  292. header->header.type = htods(RES_STRING_POOL_TYPE);
  293. header->header.headerSize = htods(sizeof(*header));
  294. header->header.size = htodl(pool->getSize());
  295. header->stringCount = htodl(ENTRIES);
  296. header->styleCount = htodl(STYLES);
  297. if (mSorted) {
  298. header->flags |= htodl(ResStringPool_header::SORTED_FLAG);
  299. }
  300. if (mUTF8) {
  301. header->flags |= htodl(ResStringPool_header::UTF8_FLAG);
  302. }
  303. header->stringsStart = htodl(preSize);
  304. header->stylesStart = htodl(STYLES > 0 ? (preSize+strPos) : 0);
  305. // Write string index array.
  306. uint32_t* index = (uint32_t*)(header+1);
  307. if (mSorted) {
  308. for (i=0; i<ENTRIES; i++) {
  309. entry& ent = const_cast<entry&>(entryAt(i));
  310. ent.indices.clear();
  311. ent.indices.add(i);
  312. *index++ = htodl(ent.offset);
  313. }
  314. } else {
  315. for (i=0; i<ENTRIES; i++) {
  316. entry& ent = mEntries.editItemAt(mEntryArray[i]);
  317. *index++ = htodl(ent.offset);
  318. NOISY(printf("Writing entry #%d: \"%s\" ent=%d off=%d\n", i,
  319. String8(ent.value).string(),
  320. mEntryArray[i], ent.offset));
  321. }
  322. }
  323. // Write style index array.
  324. if (mSorted) {
  325. for (i=0; i<STYLES; i++) {
  326. LOG_ALWAYS_FATAL("Shouldn't be here!");
  327. }
  328. } else {
  329. for (i=0; i<STYLES; i++) {
  330. *index++ = htodl(mEntryStyleArray[i].offset);
  331. }
  332. }
  333. return NO_ERROR;
  334. }
  335. ssize_t StringPool::offsetForString(const String16& val) const
  336. {
  337. const Vector<size_t>* indices = offsetsForString(val);
  338. ssize_t res = indices != NULL && indices->size() > 0 ? indices->itemAt(0) : -1;
  339. NOISY(printf("Offset for string %s: %d (%s)\n", String8(val).string(), res,
  340. res >= 0 ? String8(mEntries[mEntryArray[res]].value).string() : String8()));
  341. return res;
  342. }
  343. const Vector<size_t>* StringPool::offsetsForString(const String16& val) const
  344. {
  345. ssize_t pos = mValues.valueFor(val);
  346. if (pos < 0) {
  347. return NULL;
  348. }
  349. return &mEntries[mEntryArray[pos]].indices;
  350. }