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 dataLength, 43 int offset, int length, int bcdExtType = BCD_TYPE_ADN); 44 45 static std::string ExtensionBCDConvertToString(const std::shared_ptr<unsigned char> bytesData, int dataLength, 46 int offset, int length, int bcdExtType = BCD_TYPE_ADN); 47 48 static bool IsValidNumberString(const std::string &number); 49 50 private: 51 static bool BCDSectionConvertToString(const std::vector<uint8_t>::const_iterator &codeBeg, 52 const std::vector<uint8_t>::const_iterator &codeEnd, std::string &number, const int bcdExtType); 53 54 static bool CharToBCD(const char c, uint8_t &result, const int bcdExtType); 55 56 static bool BcdToChar(const uint8_t bcdCode, char &result, const int bcdExtType); 57 58 static const std::string *chooseExtendedByType(const int bcdExtType); 59 60 static constexpr uint8_t FLAG_INTERNATIONAL = 0x91; 61 static constexpr uint8_t FLAG_UNKNOWN = 0x81; 62 inline const static std::string BCD_ADN_EXTENTION = "*#,N;"; 63 inline const static std::string BCD_CALLER_EXTENTION = "*#abc"; 64 inline constexpr static int INC_ONE = 1; 65 inline constexpr static size_t EVEN = 2; 66 inline constexpr static int INIT_VAL = 0; 67 inline constexpr static int FOUR_BIT = 4; 68 inline constexpr static uint8_t HI_FOUR = 0xf0; 69 inline constexpr static uint8_t LO_FOUR = 0x0f; 70 }; 71 } // namespace Telephony 72 } // namespace OHOS 73 #endif // OHOS_SIM_NUMBER_DECODE_H