]>
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"
9 #include <utils/ByteOrder.h>
13 void strcpy16_htod(uint16_t* dst
, const uint16_t* src
)
16 char16_t s
= htods(*src
);
23 void printStringPool(const ResStringPool
* pool
)
25 const size_t NS
= pool
->size();
26 for (size_t s
=0; s
<NS
; s
++) {
28 printf("String #%d: %s\n", s
,
29 String8(pool
->stringAt(s
, &len
)).string());
33 StringPool::StringPool(bool sorted
, bool utf8
)
34 : mSorted(sorted
), mUTF8(utf8
), mValues(-1), mIdents(-1)
38 ssize_t
StringPool::add(const String16
& value
, bool mergeDuplicates
)
40 return add(String16(), value
, mergeDuplicates
);
43 ssize_t
StringPool::add(const String16
& value
, const Vector
<entry_style_span
>& spans
)
45 ssize_t res
= add(String16(), value
, false);
47 addStyleSpans(res
, spans
);
52 ssize_t
StringPool::add(const String16
& ident
, const String16
& value
,
55 if (ident
.size() > 0) {
56 ssize_t idx
= mIdents
.valueFor(ident
);
58 fprintf(stderr
, "ERROR: Duplicate string identifier %s\n",
59 String8(mEntries
[idx
].value
).string());
64 ssize_t vidx
= mValues
.indexOfKey(value
);
65 ssize_t pos
= vidx
>= 0 ? mValues
.valueAt(vidx
) : -1;
66 ssize_t eidx
= pos
>= 0 ? mEntryArray
.itemAt(pos
) : -1;
68 eidx
= mEntries
.add(entry(value
));
70 fprintf(stderr
, "Failure adding string %s\n", String8(value
).string());
75 const bool first
= vidx
< 0;
76 if (first
|| !mergeDuplicates
) {
77 pos
= mEntryArray
.add(eidx
);
79 vidx
= mValues
.add(value
, pos
);
80 const size_t N
= mEntryArrayToValues
.size();
81 for (size_t i
=0; i
<N
; i
++) {
82 size_t& e
= mEntryArrayToValues
.editItemAt(i
);
83 if ((ssize_t
)e
>= vidx
) {
88 mEntryArrayToValues
.add(vidx
);
90 entry
& ent
= mEntries
.editItemAt(eidx
);
95 if (ident
.size() > 0) {
96 mIdents
.add(ident
, vidx
);
99 NOISY(printf("Adding string %s to pool: pos=%d eidx=%d vidx=%d\n",
100 String8(value
).string(), pos
, eidx
, vidx
));
105 status_t
StringPool::addStyleSpan(size_t idx
, const String16
& name
,
106 uint32_t start
, uint32_t end
)
108 entry_style_span span
;
110 span
.span
.firstChar
= start
;
111 span
.span
.lastChar
= end
;
112 return addStyleSpan(idx
, span
);
115 status_t
StringPool::addStyleSpans(size_t idx
, const Vector
<entry_style_span
>& spans
)
117 const size_t N
=spans
.size();
118 for (size_t i
=0; i
<N
; i
++) {
119 status_t err
= addStyleSpan(idx
, spans
[i
]);
120 if (err
!= NO_ERROR
) {
127 status_t
StringPool::addStyleSpan(size_t idx
, const entry_style_span
& span
)
129 LOG_ALWAYS_FATAL_IF(mSorted
, "Can't use styles with sorted string pools.");
131 // Place blank entries in the span array up to this index.
132 while (mEntryStyleArray
.size() <= idx
) {
133 mEntryStyleArray
.add();
136 entry_style
& style
= mEntryStyleArray
.editItemAt(idx
);
137 style
.spans
.add(span
);
141 size_t StringPool::size() const
143 return mSorted
? mValues
.size() : mEntryArray
.size();
146 const StringPool::entry
& StringPool::entryAt(size_t idx
) const
149 return mEntries
[mEntryArray
[idx
]];
151 return mEntries
[mEntryArray
[mValues
.valueAt(idx
)]];
155 size_t StringPool::countIdentifiers() const
157 return mIdents
.size();
160 sp
<AaptFile
> StringPool::createStringBlock()
162 sp
<AaptFile
> pool
= new AaptFile(String8(), AaptGroupEntry(),
164 status_t err
= writeStringBlock(pool
);
165 return err
== NO_ERROR
? pool
: NULL
;
168 #define ENCODE_LENGTH(str, chrsz, strSize) \
170 size_t maxMask = 1 << ((chrsz*8)-1); \
171 size_t maxSize = maxMask-1; \
172 if (strSize > maxSize) { \
173 *str++ = maxMask | ((strSize>>(chrsz*8))&maxSize); \
178 status_t
StringPool::writeStringBlock(const sp
<AaptFile
>& pool
)
180 // Allow appending. Sorry this is a little wacky.
181 if (pool
->getSize() > 0) {
182 sp
<AaptFile
> block
= createStringBlock();
184 return UNKNOWN_ERROR
;
186 ssize_t res
= pool
->writeData(block
->getData(), block
->getSize());
187 return (res
>= 0) ? (status_t
)NO_ERROR
: res
;
190 // First we need to add all style span names to the string pool.
191 // We do this now (instead of when the span is added) so that these
192 // will appear at the end of the pool, not disrupting the order
193 // our client placed their own strings in it.
195 const size_t STYLES
= mEntryStyleArray
.size();
198 for (i
=0; i
<STYLES
; i
++) {
199 entry_style
& style
= mEntryStyleArray
.editItemAt(i
);
200 const size_t N
= style
.spans
.size();
201 for (size_t i
=0; i
<N
; i
++) {
202 entry_style_span
& span
= style
.spans
.editItemAt(i
);
203 ssize_t idx
= add(span
.name
, true);
205 fprintf(stderr
, "Error adding span for style tag '%s'\n",
206 String8(span
.name
).string());
209 span
.span
.name
.index
= (uint32_t)idx
;
213 const size_t ENTRIES
= size();
215 // Now build the pool of unique strings.
217 const size_t STRINGS
= mEntries
.size();
218 const size_t preSize
= sizeof(ResStringPool_header
)
219 + (sizeof(uint32_t)*ENTRIES
)
220 + (sizeof(uint32_t)*STYLES
);
221 if (pool
->editData(preSize
) == NULL
) {
222 fprintf(stderr
, "ERROR: Out of memory for string pool\n");
226 const size_t charSize
= mUTF8
? sizeof(uint8_t) : sizeof(char16_t);
229 for (i
=0; i
<STRINGS
; i
++) {
230 entry
& ent
= mEntries
.editItemAt(i
);
231 const size_t strSize
= (ent
.value
.size());
232 const size_t lenSize
= strSize
> (size_t)(1<<((charSize
*8)-1))-1 ?
233 charSize
*2 : charSize
;
237 encStr
= String8(ent
.value
);
240 const size_t encSize
= mUTF8
? encStr
.size() : 0;
241 const size_t encLenSize
= mUTF8
?
242 (encSize
> (size_t)(1<<((charSize
*8)-1))-1 ?
243 charSize
*2 : charSize
) : 0;
247 const size_t totalSize
= lenSize
+ encLenSize
+
248 ((mUTF8
? encSize
: strSize
)+1)*charSize
;
250 void* dat
= (void*)pool
->editData(preSize
+ strPos
+ totalSize
);
252 fprintf(stderr
, "ERROR: Out of memory for string pool\n");
255 dat
= (uint8_t*)dat
+ preSize
+ strPos
;
257 uint8_t* strings
= (uint8_t*)dat
;
259 ENCODE_LENGTH(strings
, sizeof(uint8_t), strSize
)
261 ENCODE_LENGTH(strings
, sizeof(uint8_t), encSize
)
263 strncpy((char*)strings
, encStr
, encSize
+1);
265 uint16_t* strings
= (uint16_t*)dat
;
267 ENCODE_LENGTH(strings
, sizeof(uint16_t), strSize
)
269 strcpy16_htod(strings
, ent
.value
);
275 // Pad ending string position up to a uint32_t boundary.
278 size_t padPos
= ((strPos
+3)&~0x3);
279 uint8_t* dat
= (uint8_t*)pool
->editData(preSize
+ padPos
);
281 fprintf(stderr
, "ERROR: Out of memory padding string pool\n");
284 memset(dat
+preSize
+strPos
, 0, padPos
-strPos
);
288 // Build the pool of style spans.
290 size_t styPos
= strPos
;
291 for (i
=0; i
<STYLES
; i
++) {
292 entry_style
& ent
= mEntryStyleArray
.editItemAt(i
);
293 const size_t N
= ent
.spans
.size();
294 const size_t totalSize
= (N
*sizeof(ResStringPool_span
))
295 + sizeof(ResStringPool_ref
);
297 ent
.offset
= styPos
-strPos
;
298 uint8_t* dat
= (uint8_t*)pool
->editData(preSize
+ styPos
+ totalSize
);
300 fprintf(stderr
, "ERROR: Out of memory for string styles\n");
303 ResStringPool_span
* span
= (ResStringPool_span
*)(dat
+preSize
+styPos
);
304 for (size_t i
=0; i
<N
; i
++) {
305 span
->name
.index
= htodl(ent
.spans
[i
].span
.name
.index
);
306 span
->firstChar
= htodl(ent
.spans
[i
].span
.firstChar
);
307 span
->lastChar
= htodl(ent
.spans
[i
].span
.lastChar
);
310 span
->name
.index
= htodl(ResStringPool_span::END
);
316 // Add full terminator at the end (when reading we validate that
317 // the end of the pool is fully terminated to simplify error
319 size_t extra
= sizeof(ResStringPool_span
)-sizeof(ResStringPool_ref
);
320 uint8_t* dat
= (uint8_t*)pool
->editData(preSize
+ styPos
+ extra
);
322 fprintf(stderr
, "ERROR: Out of memory for string styles\n");
325 uint32_t* p
= (uint32_t*)(dat
+preSize
+styPos
);
327 *p
++ = htodl(ResStringPool_span::END
);
328 extra
-= sizeof(uint32_t);
335 ResStringPool_header
* header
=
336 (ResStringPool_header
*)pool
->padData(sizeof(uint32_t));
337 if (header
== NULL
) {
338 fprintf(stderr
, "ERROR: Out of memory for string pool\n");
341 memset(header
, 0, sizeof(*header
));
342 header
->header
.type
= htods(RES_STRING_POOL_TYPE
);
343 header
->header
.headerSize
= htods(sizeof(*header
));
344 header
->header
.size
= htodl(pool
->getSize());
345 header
->stringCount
= htodl(ENTRIES
);
346 header
->styleCount
= htodl(STYLES
);
348 header
->flags
|= htodl(ResStringPool_header::SORTED_FLAG
);
351 header
->flags
|= htodl(ResStringPool_header::UTF8_FLAG
);
353 header
->stringsStart
= htodl(preSize
);
354 header
->stylesStart
= htodl(STYLES
> 0 ? (preSize
+strPos
) : 0);
356 // Write string index array.
358 uint32_t* index
= (uint32_t*)(header
+1);
360 for (i
=0; i
<ENTRIES
; i
++) {
361 entry
& ent
= const_cast<entry
&>(entryAt(i
));
364 *index
++ = htodl(ent
.offset
);
367 for (i
=0; i
<ENTRIES
; i
++) {
368 entry
& ent
= mEntries
.editItemAt(mEntryArray
[i
]);
369 *index
++ = htodl(ent
.offset
);
370 NOISY(printf("Writing entry #%d: \"%s\" ent=%d off=%d\n", i
,
371 String8(ent
.value
).string(),
372 mEntryArray
[i
], ent
.offset
));
376 // Write style index array.
379 for (i
=0; i
<STYLES
; i
++) {
380 LOG_ALWAYS_FATAL("Shouldn't be here!");
383 for (i
=0; i
<STYLES
; i
++) {
384 *index
++ = htodl(mEntryStyleArray
[i
].offset
);
391 ssize_t
StringPool::offsetForString(const String16
& val
) const
393 const Vector
<size_t>* indices
= offsetsForString(val
);
394 ssize_t res
= indices
!= NULL
&& indices
->size() > 0 ? indices
->itemAt(0) : -1;
395 NOISY(printf("Offset for string %s: %d (%s)\n", String8(val
).string(), res
,
396 res
>= 0 ? String8(mEntries
[mEntryArray
[res
]].value
).string() : String8()));
400 const Vector
<size_t>* StringPool::offsetsForString(const String16
& val
) const
402 ssize_t pos
= mValues
.valueFor(val
);
406 return &mEntries
[mEntryArray
[pos
]].indices
;