• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef OHOS_SIM_UTILS_H
17 #define OHOS_SIM_UTILS_H
18 
19 #include <stdlib.h>
20 #include <string>
21 
22 #include "event_handler.h"
23 #include "event_runner.h"
24 #include "string_ex.h"
25 #include "telephony_log_wrapper.h"
26 #include "sim_char_decode.h"
27 
28 namespace OHOS {
29 namespace Telephony {
30 static const int DECIMAL_MAX = 10;
31 static const int HALF_LEN = 2;
32 static const int HALF_BYTE_LEN = 4;
33 static const int LAC_RANGE_LEN = 6;
34 static const int ZERO_DATA = 0x00;
35 static const int SEVENF_DATA = 0x7F;
36 static const int F0_DATA = 0x80;
37 static const int FF_DATA = 0xFF;
38 static const int UCS_FLAG = 0x81;
39 static const int UCS_WIDE_FLAG = 0x82;
40 static const int HEX_HUNDRE = 0x100;
41 static const int UCS_OFFSET = 3;
42 static const int UCS_WIDE_OFFSET = 4;
43 // Full Name IEI from TS 24.008
44 static const int LONG_NAME_FLAG = 0x43;
45 // Short Name IEI from TS 24.008
46 static const int SHORT_NAME_FLAG = 0x45;
47 static const int CHAR_GSM_7BIT = 7;
48 static const int UCS_BASE_POS = 2;
49 static const int START_POS = 3;
50 static const int END_POS = 4;
51 static const int POS_NOT_BLANK = 1;
52 static char HEX_CHARS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
53 //GSM 03.38
54 static wchar_t LANGUAGE_TABLE[] = {
55     L'@', L'\u00a3', L'$', L'\u00a5', L'\u00e8', L'\u00e9', L'\u00f9', L'\u00ec', L'\u00f2', L'\u00c7',
56     L'\n', L'\u00d8', L'\u00f8', L'\r', L'\u00c5', L'\u00e5', L'\u0394', L'_', L'\u03a6', L'\u0393',
57     L'\u039b', L'\u03a9', L'\u03a0', L'\u03a8', L'\u03a3', L'\u0398', L'\u039e', L'\uffff', L'\u00c6',
58     L'\u00e6', L'\u00df', L'\u00c9', L' ', L'!', L'"', L'#', L'\u00a4', L'%', L'&', L'\'', L'(',
59     L')', L'*', L'+', L',', L'-', L'.', L'/', L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7', L'8', L'9', L':',
60     L';', L'<', L'=', L'>', L'?', L'\u00a1', L'A', L'B', L'C', L'D', L'E', L'F', L'G', L'H', L'I',
61     L'J', L'K', L'L', L'M', L'N', L'O', L'P', L'Q', L'R', L'S', L'T', L'U', L'V', L'W', L'X', L'Y', L'Z',
62     L'\u00c4', L'\u00d6', L'\u00d1', L'\u00dc', L'\u00a7', L'\u00bf', L'a', L'b', L'c', L'd', L'e',
63     L'f', L'g', L'h', L'i', L'j', L'k', L'l', L'm', L'n', L'o', L'p', L'q', L'r', L's', L't', L'u',
64     L'v', L'w', L'x', L'y', L'z', L'\u00e4', L'\u00f6', L'\u00f1', L'\u00fc', L'\u00e0'
65 };
66 static const std::map<int, wchar_t> LANGUAGE_EXT_TABLE_MAP = {
67     { 0x0A, L'\u000c' },
68     { 0x14, '^' },
69     { 0x28, '{' },
70     { 0x29, '}' },
71     { 0x2F, '\\' },
72     { 0x3C, '[' },
73     { 0x3D, '~' },
74     { 0x3E, ']' },
75     { 0x40, '|' },
76     { 0x65, L'\u00ac' },
77 };
78 // TS 24.008 Section 10.5.3.5a Network Name
79 enum {
80     NETWORK_NAME_IEI,
81     NETWORK_NAME_LENGTH,
82     NETWORK_NAME_CODING_SCHEME,
83     NETWORK_NAME_TEXT_STRING
84 };
85 // TS 31.102 Section 4.2.59.
86 enum {
87     BCD_PLMN_MCC2,
88     BCD_PLMN_MCC1,
89     BCD_PLMN_MNC3,
90     BCD_PLMN_MCC3,
91     BCD_PLMN_MNC2,
92     BCD_PLMN_MNC1,
93     MCCMNC_LEN
94 };
95 
96 class SIMUtils {
97 public:
98     static unsigned char HexCharConvertToInt(char c);
99     static std::shared_ptr<unsigned char> HexStringConvertToBytes(const std::string &s, int &byteslen);
100     static std::string BytesConvertToHexString(const unsigned char *bytes, int byteLen);
101     static void ArrayCopy(const unsigned char *src, int srcPos, unsigned char *dest, int destPos, int length);
102     static bool IsShowableAscii(char c);
103     static bool IsShowableAsciiOnly(const std::string &str);
104     static std::string BcdPlmnConvertToString(const std::string &data, int offset);
105     static std::string DiallingNumberStringFieldConvertToString(
106         std::shared_ptr<unsigned char> array, int offset, int length, int offPos);
107     static std::shared_ptr<char16_t> CharsConvertToChar16(
108         const unsigned char *charBytes, int charBytesLen, int &outChar16Len, bool bigEndian);
109     static std::string Trim(std::string& str);
110     static std::string Gsm7bitConvertToString(const unsigned char *bytes, int byteLen);
111     static std::string Cphs7bitConvertToString(const std::string &rawData);
112     static std::string HexVecToHexStr(const std::vector<uint8_t> &arr);
113 
114 private:
115     static std::u16string UcsConvertToString(unsigned char *data, int length, int offset);
116     static std::u16string UcsWideConvertToString(unsigned char *data, int length, int offset);
117     static std::string Decode8BitConvertToString(unsigned char *data, int length, int offset);
118 };
119 } // namespace Telephony
120 } // namespace OHOS
121 #endif // OHOS_SIM_UTILS_H