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 "tag_service.h"
17
18 #include "telephony_common_utils.h"
19
20 namespace OHOS {
21 namespace Telephony {
22 static const int CONTINUE = 0;
23 static const int FINISH = 1;
24 static const int ERROR = -1;
25 static const size_t FIRST = 0;
26 static const size_t SECOND = 1;
27 static const uint8_t BYTE_ZERO = 0;
28 static const uint8_t WORD_LEN = 2;
29 constexpr size_t BUFF_SIZE = 3;
30 constexpr size_t CHAR_LEN = 2;
31
TagService(const std::string & data)32 TagService::TagService(const std::string &data)
33 {
34 if (data.empty() || data.size() % CHINESE_POS) {
35 return;
36 }
37 for (auto it = data.begin(); it != data.end(); it += CHINESE_POS) {
38 uint8_t res = 0;
39 std::from_chars(std::addressof(*it), std::addressof(*(it + CHINESE_POS)), res, HEX_TYPE);
40 data_.push_back(res);
41 }
42 }
43
TagService(const std::vector<uint8_t> & data)44 TagService::TagService(const std::vector<uint8_t> &data) : data_(data) {}
45
~TagService()46 TagService::~TagService() {}
47
GetTagCode() const48 int TagService::GetTagCode() const
49 {
50 TELEPHONY_LOGI("GetTagCode : %{public}s", tag_.c_str());
51 if (!IsValidHexValue(tag_)) {
52 return ERR;
53 }
54 int i = std::stoi(tag_, nullptr, HEX_TYPE);
55 return i;
56 }
57
GetValue(std::vector<uint8_t> & result) const58 void TagService::GetValue(std::vector<uint8_t> &result) const
59 {
60 result.clear();
61 for (uint8_t i = 0; i < length_; ++i) {
62 result.push_back(data_.at(dataOffset_ + i));
63 }
64 }
65
GetLength() const66 uint8_t TagService::GetLength() const
67 {
68 return length_;
69 }
70
TagFunc(const uint8_t arg,const size_t order,std::string & tag)71 static int TagFunc(const uint8_t arg, const size_t order, std::string &tag)
72 {
73 switch (order) {
74 case FIRST:
75 if (arg == BYTE_ZERO || arg == UINT8_MAX) {
76 return ERROR;
77 } else {
78 char buff[BUFF_SIZE] = {BYTE_ZERO, BYTE_ZERO, BYTE_ZERO};
79 std::to_chars(buff, buff + CHAR_LEN, arg, TagService::HEX_TYPE);
80 tag.append(std::begin(buff), std::end(buff));
81 return FINISH;
82 }
83 default:
84 break;
85 }
86 return ERROR;
87 }
88
LengthFunc(const uint8_t arg,const size_t order,uint8_t & len)89 static int LengthFunc(const uint8_t arg, const size_t order, uint8_t &len)
90 {
91 switch (order) {
92 case FIRST:
93 if (arg < CHINESE_FLAG) {
94 len = arg;
95 return FINISH;
96 } else if (arg == UCS_FLAG) {
97 len = BYTE_ZERO;
98 return CONTINUE;
99 }
100 break;
101 case SECOND:
102 len = arg;
103 return FINISH;
104 default:
105 break;
106 }
107 return ERROR;
108 }
109
HexVecToHexStr(const std::vector<uint8_t> & arr)110 static std::string HexVecToHexStr(const std::vector<uint8_t> &arr)
111 {
112 std::stringstream ss;
113 for (auto it = arr.begin(); it != arr.end(); it++) {
114 ss << std::setiosflags(std::ios::uppercase) << std::hex << std::setw(WORD_LEN) << std::setfill('0') << int(*it);
115 }
116 return ss.str();
117 }
118
Next()119 bool TagService::Next()
120 {
121 TELEPHONY_LOGI("TagService::Next begin!!");
122 if (!hasNext_ || offset_ >= data_.size()) {
123 hasNext_ = false;
124 return false;
125 }
126 constexpr int INT_ZERO = 0;
127 /* parse tag */
128 tag_.clear();
129 size_t order = INT_ZERO;
130 for (; offset_ < data_.size(); ++offset_, ++order) {
131 const auto res = TagFunc(data_.at(offset_), order, tag_);
132 if (res < INT_ZERO) {
133 hasNext_ = false;
134 return hasNext_;
135 } else if (res > INT_ZERO) {
136 ++offset_;
137 break;
138 }
139 }
140 TELEPHONY_LOGI("TagService::Next for tag : %{public}s", tag_.c_str());
141 /* parse length */
142 if (offset_ >= data_.size()) {
143 hasNext_ = false;
144 return false;
145 }
146 length_ = INT_ZERO;
147 order = INT_ZERO;
148 for (; offset_ < data_.size(); ++offset_, ++order) {
149 const auto res = LengthFunc(data_.at(offset_), order, length_);
150 if (res < INT_ZERO) {
151 hasNext_ = false;
152 return hasNext_;
153 } else if (res > INT_ZERO) {
154 ++offset_;
155 break;
156 }
157 }
158 TELEPHONY_LOGI("TagService::Next for length : %{public}d", length_);
159 /* parse value */
160 dataOffset_ = offset_;
161 offset_ += static_cast<size_t>(length_);
162 if (offset_ > data_.size()) {
163 hasNext_ = false;
164 }
165 TELEPHONY_LOGI("TagService::Next for value : %{public}s",
166 HexVecToHexStr(std::vector<uint8_t>(data_.begin() + dataOffset_, data_.begin() + dataOffset_ + length_))
167 .c_str());
168 return hasNext_;
169 }
170 } // namespace Telephony
171 } // namespace OHOS
172