1 /*
2 * Copyright (C) 2015 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 "util/Util.h"
18
19 #include <algorithm>
20 #include <ostream>
21 #include <string>
22 #include <vector>
23
24 #include "androidfw/StringPiece.h"
25 #include "utils/Unicode.h"
26
27 #include "text/Unicode.h"
28 #include "text/Utf8Iterator.h"
29 #include "util/BigBuffer.h"
30 #include "util/Maybe.h"
31
32 using ::aapt::text::Utf8Iterator;
33 using ::android::StringPiece;
34 using ::android::StringPiece16;
35
36 namespace aapt {
37 namespace util {
38
SplitAndTransform(const StringPiece & str,char sep,const std::function<char (char)> & f)39 static std::vector<std::string> SplitAndTransform(
40 const StringPiece& str, char sep, const std::function<char(char)>& f) {
41 std::vector<std::string> parts;
42 const StringPiece::const_iterator end = std::end(str);
43 StringPiece::const_iterator start = std::begin(str);
44 StringPiece::const_iterator current;
45 do {
46 current = std::find(start, end, sep);
47 parts.emplace_back(str.substr(start, current).to_string());
48 if (f) {
49 std::string& part = parts.back();
50 std::transform(part.begin(), part.end(), part.begin(), f);
51 }
52 start = current + 1;
53 } while (current != end);
54 return parts;
55 }
56
Split(const StringPiece & str,char sep)57 std::vector<std::string> Split(const StringPiece& str, char sep) {
58 return SplitAndTransform(str, sep, nullptr);
59 }
60
SplitAndLowercase(const StringPiece & str,char sep)61 std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) {
62 return SplitAndTransform(str, sep, ::tolower);
63 }
64
StartsWith(const StringPiece & str,const StringPiece & prefix)65 bool StartsWith(const StringPiece& str, const StringPiece& prefix) {
66 if (str.size() < prefix.size()) {
67 return false;
68 }
69 return str.substr(0, prefix.size()) == prefix;
70 }
71
EndsWith(const StringPiece & str,const StringPiece & suffix)72 bool EndsWith(const StringPiece& str, const StringPiece& suffix) {
73 if (str.size() < suffix.size()) {
74 return false;
75 }
76 return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
77 }
78
TrimLeadingWhitespace(const StringPiece & str)79 StringPiece TrimLeadingWhitespace(const StringPiece& str) {
80 if (str.size() == 0 || str.data() == nullptr) {
81 return str;
82 }
83
84 const char* start = str.data();
85 const char* end = start + str.length();
86
87 while (start != end && isspace(*start)) {
88 start++;
89 }
90 return StringPiece(start, end - start);
91 }
92
TrimTrailingWhitespace(const StringPiece & str)93 StringPiece TrimTrailingWhitespace(const StringPiece& str) {
94 if (str.size() == 0 || str.data() == nullptr) {
95 return str;
96 }
97
98 const char* start = str.data();
99 const char* end = start + str.length();
100
101 while (end != start && isspace(*(end - 1))) {
102 end--;
103 }
104 return StringPiece(start, end - start);
105 }
106
TrimWhitespace(const StringPiece & str)107 StringPiece TrimWhitespace(const StringPiece& str) {
108 if (str.size() == 0 || str.data() == nullptr) {
109 return str;
110 }
111
112 const char* start = str.data();
113 const char* end = str.data() + str.length();
114
115 while (start != end && isspace(*start)) {
116 start++;
117 }
118
119 while (end != start && isspace(*(end - 1))) {
120 end--;
121 }
122
123 return StringPiece(start, end - start);
124 }
125
IsJavaNameImpl(const StringPiece & str)126 static int IsJavaNameImpl(const StringPiece& str) {
127 int pieces = 0;
128 for (const StringPiece& piece : Tokenize(str, '.')) {
129 pieces++;
130 if (!text::IsJavaIdentifier(piece)) {
131 return -1;
132 }
133 }
134 return pieces;
135 }
136
IsJavaClassName(const StringPiece & str)137 bool IsJavaClassName(const StringPiece& str) {
138 return IsJavaNameImpl(str) >= 2;
139 }
140
IsJavaPackageName(const StringPiece & str)141 bool IsJavaPackageName(const StringPiece& str) {
142 return IsJavaNameImpl(str) >= 1;
143 }
144
IsAndroidNameImpl(const StringPiece & str)145 static int IsAndroidNameImpl(const StringPiece& str) {
146 int pieces = 0;
147 for (const StringPiece& piece : Tokenize(str, '.')) {
148 if (piece.empty()) {
149 return -1;
150 }
151
152 const char first_character = piece.data()[0];
153 if (!::isalpha(first_character)) {
154 return -1;
155 }
156
157 bool valid = std::all_of(piece.begin() + 1, piece.end(), [](const char c) -> bool {
158 return ::isalnum(c) || c == '_';
159 });
160
161 if (!valid) {
162 return -1;
163 }
164 pieces++;
165 }
166 return pieces;
167 }
168
IsAndroidPackageName(const StringPiece & str)169 bool IsAndroidPackageName(const StringPiece& str) {
170 return IsAndroidNameImpl(str) > 1 || str == "android";
171 }
172
IsAndroidSplitName(const StringPiece & str)173 bool IsAndroidSplitName(const StringPiece& str) {
174 return IsAndroidNameImpl(str) > 0;
175 }
176
GetFullyQualifiedClassName(const StringPiece & package,const StringPiece & classname)177 Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package,
178 const StringPiece& classname) {
179 if (classname.empty()) {
180 return {};
181 }
182
183 if (util::IsJavaClassName(classname)) {
184 return classname.to_string();
185 }
186
187 if (package.empty()) {
188 return {};
189 }
190
191 std::string result = package.to_string();
192 if (classname.data()[0] != '.') {
193 result += '.';
194 }
195
196 result.append(classname.data(), classname.size());
197 if (!IsJavaClassName(result)) {
198 return {};
199 }
200 return result;
201 }
202
ConsumeDigits(const char * start,const char * end)203 static size_t ConsumeDigits(const char* start, const char* end) {
204 const char* c = start;
205 for (; c != end && *c >= '0' && *c <= '9'; c++) {
206 }
207 return static_cast<size_t>(c - start);
208 }
209
VerifyJavaStringFormat(const StringPiece & str)210 bool VerifyJavaStringFormat(const StringPiece& str) {
211 const char* c = str.begin();
212 const char* const end = str.end();
213
214 size_t arg_count = 0;
215 bool nonpositional = false;
216 while (c != end) {
217 if (*c == '%' && c + 1 < end) {
218 c++;
219
220 if (*c == '%' || *c == 'n') {
221 c++;
222 continue;
223 }
224
225 arg_count++;
226
227 size_t num_digits = ConsumeDigits(c, end);
228 if (num_digits > 0) {
229 c += num_digits;
230 if (c != end && *c != '$') {
231 // The digits were a size, but not a positional argument.
232 nonpositional = true;
233 }
234 } else if (*c == '<') {
235 // Reusing last argument, bad idea since positions can be moved around
236 // during translation.
237 nonpositional = true;
238
239 c++;
240
241 // Optionally we can have a $ after
242 if (c != end && *c == '$') {
243 c++;
244 }
245 } else {
246 nonpositional = true;
247 }
248
249 // Ignore size, width, flags, etc.
250 while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' ||
251 *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) {
252 c++;
253 }
254
255 /*
256 * This is a shortcut to detect strings that are going to Time.format()
257 * instead of String.format()
258 *
259 * Comparison of String.format() and Time.format() args:
260 *
261 * String: ABC E GH ST X abcdefgh nost x
262 * Time: DEFGHKMS W Za d hkm s w yz
263 *
264 * Therefore we know it's definitely Time if we have:
265 * DFKMWZkmwyz
266 */
267 if (c != end) {
268 switch (*c) {
269 case 'D':
270 case 'F':
271 case 'K':
272 case 'M':
273 case 'W':
274 case 'Z':
275 case 'k':
276 case 'm':
277 case 'w':
278 case 'y':
279 case 'z':
280 return true;
281 }
282 }
283 }
284
285 if (c != end) {
286 c++;
287 }
288 }
289
290 if (arg_count > 1 && nonpositional) {
291 // Multiple arguments were specified, but some or all were non positional.
292 // Translated
293 // strings may rearrange the order of the arguments, which will break the
294 // string.
295 return false;
296 }
297 return true;
298 }
299
Utf8ToUtf16(const StringPiece & utf8)300 std::u16string Utf8ToUtf16(const StringPiece& utf8) {
301 ssize_t utf16_length = utf8_to_utf16_length(
302 reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
303 if (utf16_length <= 0) {
304 return {};
305 }
306
307 std::u16string utf16;
308 utf16.resize(utf16_length);
309 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
310 &*utf16.begin(), utf16_length + 1);
311 return utf16;
312 }
313
Utf16ToUtf8(const StringPiece16 & utf16)314 std::string Utf16ToUtf8(const StringPiece16& utf16) {
315 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
316 if (utf8_length <= 0) {
317 return {};
318 }
319
320 std::string utf8;
321 utf8.resize(utf8_length);
322 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1);
323 return utf8;
324 }
325
WriteAll(std::ostream & out,const BigBuffer & buffer)326 bool WriteAll(std::ostream& out, const BigBuffer& buffer) {
327 for (const auto& b : buffer) {
328 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
329 return false;
330 }
331 }
332 return true;
333 }
334
Copy(const BigBuffer & buffer)335 std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
336 std::unique_ptr<uint8_t[]> data =
337 std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
338 uint8_t* p = data.get();
339 for (const auto& block : buffer) {
340 memcpy(p, block.buffer.get(), block.size);
341 p += block.size;
342 }
343 return data;
344 }
345
operator ++()346 typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
347 const char* start = token_.end();
348 const char* end = str_.end();
349 if (start == end) {
350 end_ = true;
351 token_.assign(token_.end(), 0);
352 return *this;
353 }
354
355 start += 1;
356 const char* current = start;
357 while (current != end) {
358 if (*current == separator_) {
359 token_.assign(start, current - start);
360 return *this;
361 }
362 ++current;
363 }
364 token_.assign(start, end - start);
365 return *this;
366 }
367
operator ==(const iterator & rhs) const368 bool Tokenizer::iterator::operator==(const iterator& rhs) const {
369 // We check equality here a bit differently.
370 // We need to know that the addresses are the same.
371 return token_.begin() == rhs.token_.begin() &&
372 token_.end() == rhs.token_.end() && end_ == rhs.end_;
373 }
374
operator !=(const iterator & rhs) const375 bool Tokenizer::iterator::operator!=(const iterator& rhs) const {
376 return !(*this == rhs);
377 }
378
iterator(const StringPiece & s,char sep,const StringPiece & tok,bool end)379 Tokenizer::iterator::iterator(const StringPiece& s, char sep, const StringPiece& tok, bool end)
380 : str_(s), separator_(sep), token_(tok), end_(end) {}
381
Tokenizer(const StringPiece & str,char sep)382 Tokenizer::Tokenizer(const StringPiece& str, char sep)
383 : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
384 end_(str, sep, StringPiece(str.end(), 0), true) {}
385
ExtractResFilePathParts(const StringPiece & path,StringPiece * out_prefix,StringPiece * out_entry,StringPiece * out_suffix)386 bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
387 StringPiece* out_entry, StringPiece* out_suffix) {
388 const StringPiece res_prefix("res/");
389 if (!StartsWith(path, res_prefix)) {
390 return false;
391 }
392
393 StringPiece::const_iterator last_occurence = path.end();
394 for (auto iter = path.begin() + res_prefix.size(); iter != path.end();
395 ++iter) {
396 if (*iter == '/') {
397 last_occurence = iter;
398 }
399 }
400
401 if (last_occurence == path.end()) {
402 return false;
403 }
404
405 auto iter = std::find(last_occurence, path.end(), '.');
406 *out_suffix = StringPiece(iter, path.end() - iter);
407 *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1);
408 *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1);
409 return true;
410 }
411
GetString16(const android::ResStringPool & pool,size_t idx)412 StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) {
413 size_t len;
414 const char16_t* str = pool.stringAt(idx, &len);
415 if (str != nullptr) {
416 return StringPiece16(str, len);
417 }
418 return StringPiece16();
419 }
420
GetString(const android::ResStringPool & pool,size_t idx)421 std::string GetString(const android::ResStringPool& pool, size_t idx) {
422 size_t len;
423 const char* str = pool.string8At(idx, &len);
424 if (str != nullptr) {
425 return std::string(str, len);
426 }
427 return Utf16ToUtf8(GetString16(pool, idx));
428 }
429
430 } // namespace util
431 } // namespace aapt
432