1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "codec_cov.h"
17
18 #include <memory>
19 #ifdef _WIN32
20 #include <windows.h>
21 #endif
22
23 namespace SysTuning {
24 namespace base {
PreNum(unsigned char byte)25 int PreNum(unsigned char byte)
26 {
27 constexpr uint32_t BITS = 8;
28 unsigned char mask = 0x80;
29 int num = 0;
30 for (uint32_t i = 0; i < BITS; i++) {
31 if ((byte & mask) == mask) {
32 mask = mask >> 1;
33 num++;
34 } else {
35 break;
36 }
37 }
38 return num;
39 }
40
IsUTF8(const uint8_t * data,int len)41 bool IsUTF8(const uint8_t* data, int len)
42 {
43 constexpr uint8_t MASK = 0x80;
44 constexpr uint8_t FIRST_BYTE = 0xc0;
45 constexpr int TARGET = 2;
46 int num = 0;
47 int i = 0;
48 while (i < len) {
49 if ((data[i] & MASK) == 0x00) {
50 i++;
51 continue;
52 }
53 if ((num = PreNum(data[i])) > TARGET) {
54 i++;
55 for (int j = 0; j < num - 1; j++) {
56 if ((data[i] & FIRST_BYTE) != MASK) {
57 return false;
58 }
59 i++;
60 }
61 } else {
62 return false;
63 }
64 }
65 return true;
66 }
67
IsGBK(const uint8_t * data,int len)68 bool IsGBK(const uint8_t* data, int len)
69 {
70 constexpr int STEP = 2;
71 constexpr uint8_t ASCII_END = 0x7f;
72 constexpr uint8_t FIRST_BYTE = 0x81;
73 constexpr uint8_t FIRST_BYTE_END = 0xfe;
74 constexpr uint8_t SECOND_BYTE_ONE = 0x40;
75 constexpr uint8_t SECOND_BYTE_TWO_END = 0xfe;
76 constexpr uint8_t GBK_MASK = 0xf7;
77 int i = 0;
78 while (i < len) {
79 if (data[i] <= ASCII_END) {
80 i++;
81 continue;
82 } else {
83 if (data[i] >= FIRST_BYTE && data[i] <= FIRST_BYTE_END && data[i + 1] >= SECOND_BYTE_ONE &&
84 data[i + 1] <= SECOND_BYTE_TWO_END && data[i + 1] != GBK_MASK) {
85 i += STEP;
86 continue;
87 } else {
88 return false;
89 }
90 }
91 }
92 return true;
93 }
94
GetCoding(const uint8_t * data,int len)95 CODING GetCoding(const uint8_t* data, int len)
96 {
97 CODING coding;
98 if (IsUTF8(data, len)) {
99 coding = UTF8;
100 } else if (IsGBK(data, len)) {
101 coding = GBK;
102 } else {
103 coding = UNKOWN;
104 }
105 return coding;
106 }
107
108 #ifdef _WIN32
GbkToUtf8(const char * srcStr)109 std::string GbkToUtf8(const char* srcStr)
110 {
111 int len = MultiByteToWideChar(CP_ACP, 0, srcStr, -1, NULL, 0);
112 std::unique_ptr<wchar_t[]> wstr = std::make_unique<wchar_t[]>(len + 1);
113 MultiByteToWideChar(CP_ACP, 0, srcStr, -1, wstr.get(), len);
114 len = WideCharToMultiByte(CP_UTF8, 0, wstr.get(), -1, NULL, 0, NULL, NULL);
115 std::unique_ptr<char[]> str = std::make_unique<char[]>(len + 1);
116 WideCharToMultiByte(CP_UTF8, 0, wstr.get(), -1, str.get(), len, NULL, NULL);
117 return std::string(str.get());
118 }
119 #endif
120 } // namespace base
121 } // namespace SysTuning
122