This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dex_utf8.cc
executable file
·70 lines (63 loc) · 2.06 KB
/
dex_utf8.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "slicer/dex_format.h"
namespace dex {
// Retrieve the next UTF-16 character from a UTF-8 string.
// Advances "*pUtf8Ptr" to the start of the next character.
//
// NOTE: If a string is corrupted by dropping a '\0' in the middle
// of a 3-byte sequence, you can end up overrunning the buffer with
// reads (and possibly with the writes if the length was computed and
// cached before the damage). For performance reasons, this function
// assumes that the string being parsed is known to be valid (e.g., by
// already being verified).
static u2 GetUtf16FromUtf8(const char** pUtf8Ptr) {
u4 one = *(*pUtf8Ptr)++;
if ((one & 0x80) != 0) {
// two- or three-byte encoding
u4 two = *(*pUtf8Ptr)++;
if ((one & 0x20) != 0) {
// three-byte encoding
u4 three = *(*pUtf8Ptr)++;
return ((one & 0x0f) << 12) | ((two & 0x3f) << 6) | (three & 0x3f);
} else {
// two-byte encoding
return ((one & 0x1f) << 6) | (two & 0x3f);
}
} else {
// one-byte encoding
return one;
}
}
int Utf8Cmp(const char* s1, const char* s2) {
for (;;) {
if (*s1 == '\0') {
if (*s2 == '\0') {
return 0;
}
return -1;
} else if (*s2 == '\0') {
return 1;
}
int utf1 = GetUtf16FromUtf8(&s1);
int utf2 = GetUtf16FromUtf8(&s2);
int diff = utf1 - utf2;
if (diff != 0) {
return diff;
}
}
}
} // namespace dex