• 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_NUMBER_DECODE_H
17 #define OHOS_SIM_NUMBER_DECODE_H
18 
19 #include <vector>
20 #include <regex>
21 #include "telephony_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 constexpr int32_t NEW_WIDTH = 2;
26 const int MAX_NUMBER_CHAR = 20;
27 enum SectionPosition { POS_START = 1, POS_BRACE = 2, POS_BRACKET = 3, POS_PARENTHESIS = 4, POS_END = 5 };
28 enum CommonConstant { MIN_LENGTH = 2, SHIFT_FLAG = 4, CHAR_START = 0xa, HALF_BYTE = 0xf, BYTE_HIGH = 0xf0 };
29 
30 class SimNumberDecode final {
31 public:
32     const static int32_t BCD_TYPE_ADN = 1;
33     const static int32_t BCD_TYPE_CALLER = 2;
34 
35     static bool NumberConvertToBCD(const std::string &number, std::vector<uint8_t> &bcdCodes,
36         const bool includeLen = false, const int bcdExtType = BCD_TYPE_ADN);
37 
38     static bool BCDConvertToString(const std::vector<uint8_t>::const_iterator &codeBeg,
39         const std::vector<uint8_t>::const_iterator &codeEnd, std::string &number,
40         const int bcdExtType = BCD_TYPE_ADN);
41 
42     static std::string BCDConvertToString(const std::shared_ptr<unsigned char> bytesData, int offset, int length,
43         int bcdExtType = BCD_TYPE_ADN);
44 
45     static bool IsValidNumberString(const std::string &number);
46 
47 private:
48     static bool BCDSectionConvertToString(const std::vector<uint8_t>::const_iterator &codeBeg,
49         const std::vector<uint8_t>::const_iterator &codeEnd, std::string &number, const int bcdExtType);
50 
51     static bool CharToBCD(const char c, uint8_t &result, const int bcdExtType);
52 
53     static bool BcdToChar(const uint8_t bcdCode, char &result, const int bcdExtType);
54 
55     static const std::string *chooseExtendedByType(const int bcdExtType);
56 
57     static constexpr uint8_t FLAG_INTERNATIONAL = 0x91;
58     static constexpr uint8_t FLAG_UNKNOWN = 0x81;
59     inline const static std::string BCD_ADN_EXTENTION = "*#,N;";
60     inline const static std::string BCD_CALLER_EXTENTION = "*#abc";
61     inline constexpr static int INC_ONE = 1;
62     inline constexpr static size_t EVEN = 2;
63     inline constexpr static int INIT_VAL = 0;
64     inline constexpr static int FOUR_BIT = 4;
65     inline constexpr static uint8_t HI_FOUR = 0xf0;
66     inline constexpr static uint8_t LO_FOUR = 0x0f;
67 };
68 } // namespace Telephony
69 } // namespace OHOS
70 #endif // OHOS_SIM_NUMBER_DECODE_H