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 // TS 24.008 Section 10.5.3.5a Network Name
54 enum {
55     NETWORK_NAME_IEI,
56     NETWORK_NAME_LENGTH,
57     NETWORK_NAME_CODING_SCHEME,
58     NETWORK_NAME_TEXT_STRING
59 };
60 // TS 31.102 Section 4.2.59.
61 enum {
62     BCD_PLMN_MCC2,
63     BCD_PLMN_MCC1,
64     BCD_PLMN_MNC3,
65     BCD_PLMN_MCC3,
66     BCD_PLMN_MNC2,
67     BCD_PLMN_MNC1,
68     MCCMNC_LEN
69 };
70 
71 class SIMUtils {
72 public:
73     static unsigned char HexCharConvertToInt(char c);
74     static std::shared_ptr<unsigned char> HexStringConvertToBytes(const std::string &s, int &byteslen);
75     static std::string BytesConvertToHexString(const unsigned char *bytes, int byteLen);
76     static void ArrayCopy(const unsigned char *src, int srcPos, unsigned char *dest, int destPos, int length);
77     static bool IsShowableAscii(char c);
78     static bool IsShowableAsciiOnly(const std::string &str);
79     static std::string BcdPlmnConvertToString(const std::string &data, int offset);
80     static std::string DiallingNumberStringFieldConvertToString(
81         std::shared_ptr<unsigned char> array, int offset, int length, int offPos);
82     static std::shared_ptr<char16_t> CharsConvertToChar16(
83         const unsigned char *charBytes, int charBytesLen, int &outChar16Len, bool bigEndian);
84     static std::string Trim(std::string& str);
85     static std::string Gsm7bitConvertToString(const unsigned char *bytes, int byteLen);
86 
87 private:
88     static std::u16string UcsConvertToString(unsigned char *data, int length, int offset);
89     static std::u16string UcsWideConvertToString(unsigned char *data, int length, int offset);
90     static std::string Decode8BitConvertToString(unsigned char *data, int length, int offset);
91 };
92 } // namespace Telephony
93 } // namespace OHOS
94 #endif // OHOS_SIM_UTILS_H