1 /*
2  * Copyright (C) 2005 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <utils/String16.h>
18 
19 #include <utils/Log.h>
20 
21 #include <ctype.h>
22 
23 #include "SharedBuffer.h"
24 
25 namespace android {
26 
27 static const StaticString16 emptyString(u"");
getEmptyString()28 static inline char16_t* getEmptyString() {
29     return const_cast<char16_t*>(emptyString.string());
30 }
31 
32 // ---------------------------------------------------------------------------
33 
alloc(size_t size)34 void* String16::alloc(size_t size)
35 {
36     SharedBuffer* buf = SharedBuffer::alloc(size);
37     buf->mClientMetadata = kIsSharedBufferAllocated;
38     return buf;
39 }
40 
allocFromUTF8(const char * u8str,size_t u8len)41 char16_t* String16::allocFromUTF8(const char* u8str, size_t u8len)
42 {
43     if (u8len == 0) return getEmptyString();
44 
45     const uint8_t* u8cur = (const uint8_t*) u8str;
46 
47     const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len);
48     if (u16len < 0) {
49         return getEmptyString();
50     }
51 
52     SharedBuffer* buf = static_cast<SharedBuffer*>(alloc(sizeof(char16_t) * (u16len + 1)));
53     if (buf) {
54         u8cur = (const uint8_t*) u8str;
55         char16_t* u16str = (char16_t*)buf->data();
56 
57         utf8_to_utf16(u8cur, u8len, u16str, ((size_t) u16len) + 1);
58 
59         //printf("Created UTF-16 string from UTF-8 \"%s\":", in);
60         //printHexData(1, str, buf->size(), 16, 1);
61         //printf("\n");
62 
63         return u16str;
64     }
65 
66     return getEmptyString();
67 }
68 
allocFromUTF16(const char16_t * u16str,size_t u16len)69 char16_t* String16::allocFromUTF16(const char16_t* u16str, size_t u16len) {
70     if (u16len >= SIZE_MAX / sizeof(char16_t)) {
71         android_errorWriteLog(0x534e4554, "73826242");
72         abort();
73     }
74 
75     SharedBuffer* buf = static_cast<SharedBuffer*>(alloc((u16len + 1) * sizeof(char16_t)));
76     ALOG_ASSERT(buf, "Unable to allocate shared buffer");
77     if (buf) {
78         char16_t* str = (char16_t*)buf->data();
79         memcpy(str, u16str, u16len * sizeof(char16_t));
80         str[u16len] = 0;
81         return str;
82     }
83     return getEmptyString();
84 }
85 
86 // ---------------------------------------------------------------------------
87 
String16()88 String16::String16()
89     : mString(getEmptyString())
90 {
91 }
92 
String16(const String16 & o)93 String16::String16(const String16& o)
94     : mString(o.mString)
95 {
96     acquire();
97 }
98 
String16(String16 && o)99 String16::String16(String16&& o) noexcept
100     : mString(o.mString)
101 {
102     o.mString = getEmptyString();
103 }
104 
String16(const String16 & o,size_t len,size_t begin)105 String16::String16(const String16& o, size_t len, size_t begin)
106     : mString(getEmptyString())
107 {
108     setTo(o, len, begin);
109 }
110 
String16(const char16_t * o)111 String16::String16(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o))) {}
112 
String16(const char16_t * o,size_t len)113 String16::String16(const char16_t* o, size_t len) : mString(allocFromUTF16(o, len)) {}
114 
String16(const String8 & o)115 String16::String16(const String8& o)
116     : mString(allocFromUTF8(o.string(), o.size()))
117 {
118 }
119 
String16(const char * o)120 String16::String16(const char* o)
121     : mString(allocFromUTF8(o, strlen(o)))
122 {
123 }
124 
String16(const char * o,size_t len)125 String16::String16(const char* o, size_t len)
126     : mString(allocFromUTF8(o, len))
127 {
128 }
129 
~String16()130 String16::~String16()
131 {
132     release();
133 }
134 
operator =(String16 && other)135 String16& String16::operator=(String16&& other) noexcept {
136     release();
137     mString = other.mString;
138     other.mString = getEmptyString();
139     return *this;
140 }
141 
size() const142 size_t String16::size() const
143 {
144     if (isStaticString()) {
145         return staticStringSize();
146     } else {
147         return SharedBuffer::sizeFromData(mString) / sizeof(char16_t) - 1;
148     }
149 }
150 
setTo(const String16 & other)151 void String16::setTo(const String16& other)
152 {
153     release();
154     mString = other.mString;
155     acquire();
156 }
157 
setTo(const String16 & other,size_t len,size_t begin)158 status_t String16::setTo(const String16& other, size_t len, size_t begin)
159 {
160     const size_t N = other.size();
161     if (begin >= N) {
162         release();
163         mString = getEmptyString();
164         return OK;
165     }
166     if ((begin+len) > N) len = N-begin;
167     if (begin == 0 && len == N) {
168         setTo(other);
169         return OK;
170     }
171 
172     if (&other == this) {
173         LOG_ALWAYS_FATAL("Not implemented");
174     }
175 
176     return setTo(other.string()+begin, len);
177 }
178 
setTo(const char16_t * other)179 status_t String16::setTo(const char16_t* other)
180 {
181     return setTo(other, strlen16(other));
182 }
183 
setTo(const char16_t * other,size_t len)184 status_t String16::setTo(const char16_t* other, size_t len)
185 {
186     if (len >= SIZE_MAX / sizeof(char16_t)) {
187         android_errorWriteLog(0x534e4554, "73826242");
188         abort();
189     }
190 
191     SharedBuffer* buf = static_cast<SharedBuffer*>(editResize((len + 1) * sizeof(char16_t)));
192     if (buf) {
193         char16_t* str = (char16_t*)buf->data();
194         memmove(str, other, len*sizeof(char16_t));
195         str[len] = 0;
196         mString = str;
197         return OK;
198     }
199     return NO_MEMORY;
200 }
201 
append(const String16 & other)202 status_t String16::append(const String16& other) {
203     return append(other.string(), other.size());
204 }
205 
append(const char16_t * chrs,size_t otherLen)206 status_t String16::append(const char16_t* chrs, size_t otherLen) {
207     const size_t myLen = size();
208 
209     if (myLen == 0) return setTo(chrs, otherLen);
210 
211     if (otherLen == 0) return OK;
212 
213     size_t size = myLen;
214     if (__builtin_add_overflow(size, otherLen, &size) ||
215         __builtin_add_overflow(size, 1, &size) ||
216         __builtin_mul_overflow(size, sizeof(char16_t), &size)) return NO_MEMORY;
217 
218     SharedBuffer* buf = static_cast<SharedBuffer*>(editResize(size));
219     if (!buf) return NO_MEMORY;
220 
221     char16_t* str = static_cast<char16_t*>(buf->data());
222     memcpy(str + myLen, chrs, otherLen * sizeof(char16_t));
223     str[myLen + otherLen] = 0;
224     mString = str;
225     return OK;
226 }
227 
insert(size_t pos,const char16_t * chrs)228 status_t String16::insert(size_t pos, const char16_t* chrs) {
229     return insert(pos, chrs, strlen16(chrs));
230 }
231 
insert(size_t pos,const char16_t * chrs,size_t otherLen)232 status_t String16::insert(size_t pos, const char16_t* chrs, size_t otherLen) {
233     const size_t myLen = size();
234 
235     if (myLen == 0) return setTo(chrs, otherLen);
236 
237     if (otherLen == 0) return OK;
238 
239     if (pos > myLen) pos = myLen;
240 
241     size_t size = myLen;
242     if (__builtin_add_overflow(size, otherLen, &size) ||
243         __builtin_add_overflow(size, 1, &size) ||
244         __builtin_mul_overflow(size, sizeof(char16_t), &size)) return NO_MEMORY;
245 
246     SharedBuffer* buf = static_cast<SharedBuffer*>(editResize(size));
247     if (!buf) return NO_MEMORY;
248 
249     char16_t* str = static_cast<char16_t*>(buf->data());
250     if (pos < myLen) memmove(str + pos + otherLen, str + pos, (myLen - pos) * sizeof(char16_t));
251     memcpy(str + pos, chrs, otherLen * sizeof(char16_t));
252     str[myLen + otherLen] = 0;
253     mString = str;
254     return OK;
255 }
256 
findFirst(char16_t c) const257 ssize_t String16::findFirst(char16_t c) const
258 {
259     const char16_t* str = string();
260     const char16_t* p = str;
261     const char16_t* e = p + size();
262     while (p < e) {
263         if (*p == c) {
264             return p-str;
265         }
266         p++;
267     }
268     return -1;
269 }
270 
findLast(char16_t c) const271 ssize_t String16::findLast(char16_t c) const
272 {
273     const char16_t* str = string();
274     const char16_t* p = str;
275     const char16_t* e = p + size();
276     while (p < e) {
277         e--;
278         if (*e == c) {
279             return e-str;
280         }
281     }
282     return -1;
283 }
284 
startsWith(const String16 & prefix) const285 bool String16::startsWith(const String16& prefix) const
286 {
287     const size_t ps = prefix.size();
288     if (ps > size()) return false;
289     return strzcmp16(mString, ps, prefix.string(), ps) == 0;
290 }
291 
startsWith(const char16_t * prefix) const292 bool String16::startsWith(const char16_t* prefix) const
293 {
294     const size_t ps = strlen16(prefix);
295     if (ps > size()) return false;
296     return strncmp16(mString, prefix, ps) == 0;
297 }
298 
contains(const char16_t * chrs) const299 bool String16::contains(const char16_t* chrs) const
300 {
301     return strstr16(mString, chrs) != nullptr;
302 }
303 
edit()304 void* String16::edit() {
305     SharedBuffer* buf;
306     if (isStaticString()) {
307         buf = static_cast<SharedBuffer*>(alloc((size() + 1) * sizeof(char16_t)));
308         if (buf) {
309             memcpy(buf->data(), mString, (size() + 1) * sizeof(char16_t));
310         }
311     } else {
312         buf = SharedBuffer::bufferFromData(mString)->edit();
313         buf->mClientMetadata = kIsSharedBufferAllocated;
314     }
315     return buf;
316 }
317 
editResize(size_t newSize)318 void* String16::editResize(size_t newSize) {
319     SharedBuffer* buf;
320     if (isStaticString()) {
321         size_t copySize = (size() + 1) * sizeof(char16_t);
322         if (newSize < copySize) {
323             copySize = newSize;
324         }
325         buf = static_cast<SharedBuffer*>(alloc(newSize));
326         if (buf) {
327             memcpy(buf->data(), mString, copySize);
328         }
329     } else {
330         buf = SharedBuffer::bufferFromData(mString)->editResize(newSize);
331         buf->mClientMetadata = kIsSharedBufferAllocated;
332     }
333     return buf;
334 }
335 
acquire()336 void String16::acquire()
337 {
338     if (!isStaticString()) {
339         SharedBuffer::bufferFromData(mString)->acquire();
340     }
341 }
342 
release()343 void String16::release()
344 {
345     if (!isStaticString()) {
346         SharedBuffer::bufferFromData(mString)->release();
347     }
348 }
349 
isStaticString() const350 bool String16::isStaticString() const {
351     // See String16.h for notes on the memory layout of String16::StaticData and
352     // SharedBuffer.
353     static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4);
354     const uint32_t* p = reinterpret_cast<const uint32_t*>(mString);
355     return (*(p - 1) & kIsSharedBufferAllocated) == 0;
356 }
357 
staticStringSize() const358 size_t String16::staticStringSize() const {
359     // See String16.h for notes on the memory layout of String16::StaticData and
360     // SharedBuffer.
361     static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4);
362     const uint32_t* p = reinterpret_cast<const uint32_t*>(mString);
363     return static_cast<size_t>(*(p - 1));
364 }
365 
replaceAll(char16_t replaceThis,char16_t withThis)366 status_t String16::replaceAll(char16_t replaceThis, char16_t withThis)
367 {
368     const size_t N = size();
369     const char16_t* str = string();
370     char16_t* edited = nullptr;
371     for (size_t i=0; i<N; i++) {
372         if (str[i] == replaceThis) {
373             if (!edited) {
374                 SharedBuffer* buf = static_cast<SharedBuffer*>(edit());
375                 if (!buf) {
376                     return NO_MEMORY;
377                 }
378                 edited = (char16_t*)buf->data();
379                 mString = str = edited;
380             }
381             edited[i] = withThis;
382         }
383     }
384     return OK;
385 }
386 
387 }; // namespace android
388