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