1 /*
2 * Copyright (C) 2023 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 "gsm_cb_umts_codec.h"
17
18 #include "cdma_sms_common.h"
19 #include "gsm_pdu_hex_value.h"
20 #include "securec.h"
21 #include "sms_common_utils.h"
22 #include "string_utils.h"
23 #include "telephony_log_wrapper.h"
24 #include "text_coder.h"
25
26 namespace OHOS {
27 namespace Telephony {
28 static constexpr uint8_t SMS_BYTE_BIT = 8;
29 static constexpr uint8_t GSM_CODE_BIT = 7;
30 static constexpr uint8_t MAX_PAGE_PDU_LEN = 82;
31
GsmCbUmtsCodec(std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> header,std::shared_ptr<GsmCbPduDecodeBuffer> buffer,std::shared_ptr<GsmCbCodec> cbCodec)32 GsmCbUmtsCodec::GsmCbUmtsCodec(std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> header,
33 std::shared_ptr<GsmCbPduDecodeBuffer> buffer, std::shared_ptr<GsmCbCodec> cbCodec)
34 {
35 cbHeader_ = header;
36 cbPduBuffer_ = buffer;
37 cbCodec_ = cbCodec;
38 }
39
~GsmCbUmtsCodec()40 GsmCbUmtsCodec::~GsmCbUmtsCodec() {}
41
42 /**
43 * refer to 3GPP TS 23.041 V4.1.0 9.4.2.1 General Description
44 * refer to 3GPP TS 23.041 V4.1.0 9.4.2.2 Message Parameter
45 */
Decode3gHeader()46 bool GsmCbUmtsCodec::Decode3gHeader()
47 {
48 if (cbPduBuffer_ == nullptr || cbHeader_ == nullptr || cbPduBuffer_->GetSize() == 0) {
49 TELEPHONY_LOGE("CB pdu data error.");
50 return false;
51 }
52 cbPduBuffer_->IncreasePointer(1);
53 uint8_t oneByte = 0;
54 if (!cbPduBuffer_->GetOneByte(oneByte)) {
55 TELEPHONY_LOGE("get data error.");
56 return false;
57 }
58 uint8_t temp = oneByte;
59 if (!cbPduBuffer_->GetOneByte(oneByte)) {
60 TELEPHONY_LOGE("get data error.");
61 return false;
62 }
63 cbHeader_->msgId = (temp << HEX_VALUE_08) | oneByte;
64
65 if (!cbPduBuffer_->GetOneByte(oneByte)) {
66 TELEPHONY_LOGE("get data error.");
67 return false;
68 }
69 temp = oneByte;
70 cbHeader_->serialNum.geoScope = (temp & HEX_VALUE_C0) >> HEX_VALUE_06;
71 cbHeader_->serialNum.msgCode = (temp & HEX_VALUE_3F) << HEX_VALUE_04;
72
73 if (!cbPduBuffer_->GetOneByte(oneByte)) {
74 TELEPHONY_LOGE("get data error.");
75 return false;
76 }
77 temp = oneByte;
78 cbHeader_->serialNum.msgCode |= (temp & HEX_VALUE_F0) >> HEX_VALUE_04;
79 cbHeader_->serialNum.updateNum = temp & HEX_VALUE_0F;
80
81 if (!cbPduBuffer_->GetOneByte(oneByte)) {
82 TELEPHONY_LOGE("get data error.");
83 return false;
84 }
85 uint8_t dcs = oneByte;
86 return Decode3gHeaderPartData(dcs);
87 }
88
Decode3gHeaderPartData(uint8_t dcs)89 bool GsmCbUmtsCodec::Decode3gHeaderPartData(uint8_t dcs)
90 {
91 if (cbCodec_ == nullptr) {
92 TELEPHONY_LOGE("CB pdu data error.");
93 return false;
94 }
95 uint8_t oneByte = 0;
96 if (!cbPduBuffer_->GetOneByte(oneByte)) {
97 TELEPHONY_LOGE("get data error.");
98 return false;
99 }
100 cbHeader_->totalPages = oneByte;
101
102 if (!cbPduBuffer_->GetOneByte(oneByte)) {
103 TELEPHONY_LOGE("get data error.");
104 return false;
105 }
106 unsigned short iosTemp = oneByte;
107
108 if (!cbPduBuffer_->GetOneByte(oneByte)) {
109 TELEPHONY_LOGE("get data error.");
110 return false;
111 }
112 iosTemp |= (oneByte << SMS_BYTE_BIT);
113 cbCodec_->DecodeCbMsgDCS(dcs, iosTemp, cbHeader_->dcs);
114 cbHeader_->langType = cbHeader_->dcs.langType;
115 cbHeader_->recvTime = static_cast<time_t>(cbCodec_->GetRecvTime());
116 cbPduBuffer_->SetPointer(cbPduBuffer_->GetCurPosition() - HEX_VALUE_02);
117 return true;
118 }
119
120 /**
121 * refer to 3GPP TS 23.041 V4.1.0 9.4.2.2.5 CB Data
122 */
Decode3gCbMsg()123 bool GsmCbUmtsCodec::Decode3gCbMsg()
124 {
125 if (cbHeader_ == nullptr) {
126 TELEPHONY_LOGE("CB pdu data error.");
127 return false;
128 }
129
130 bool decodeResult = false;
131 switch (cbHeader_->dcs.codingScheme) {
132 case DATA_CODING_7BIT: {
133 decodeResult = Decode3g7Bit();
134 break;
135 }
136 case DATA_CODING_8BIT:
137 case DATA_CODING_UCS2: {
138 decodeResult = Decode3gUCS2();
139 break;
140 }
141 default:
142 break;
143 }
144 cbHeader_->totalPages = 1;
145 return decodeResult;
146 }
147
Decode3g7Bit()148 bool GsmCbUmtsCodec::Decode3g7Bit()
149 {
150 if (cbPduBuffer_ == nullptr || cbCodec_ == nullptr || cbPduBuffer_->GetSize() == 0) {
151 TELEPHONY_LOGE("CB pdu data error.");
152 return false;
153 }
154
155 std::vector<unsigned char> dataPdu;
156 cbCodec_->GetPduData(dataPdu);
157 if (dataPdu.size() == 0 || cbPduBuffer_->GetCurPosition() >= cbPduBuffer_->GetSize()) {
158 TELEPHONY_LOGE("dataPdu empty.");
159 return false;
160 }
161
162 uint16_t offset = 0;
163 uint16_t dataLen = 0;
164 uint16_t pduLen = cbPduBuffer_->GetSize() - cbPduBuffer_->GetCurPosition();
165
166 const uint8_t *tpdu = dataPdu.data();
167 for (uint8_t i = 0; i < cbHeader_->totalPages; ++i) {
168 uint8_t pageLenOffset = (i + 1) * MAX_PAGE_PDU_LEN + i;
169 if (pduLen <= pageLenOffset) {
170 TELEPHONY_LOGE("CB Msg Size err [%{pulbic}d]", pduLen);
171 messageRaw_.clear();
172 return false;
173 }
174 dataLen = tpdu[pageLenOffset];
175 offset = (i * MAX_PAGE_PDU_LEN) + i;
176 if (dataLen > MAX_PAGE_PDU_LEN) {
177 TELEPHONY_LOGE("CB Msg Size is over MAX [%{pulbic}d]", dataLen);
178 messageRaw_.clear();
179 return false;
180 }
181 uint16_t unpackLen = 0;
182 dataLen = (dataLen * SMS_BYTE_BIT) / GSM_CODE_BIT;
183 uint8_t pageData[MAX_PAGE_PDU_LEN * SMS_BYTE_BIT / GSM_CODE_BIT] = { 0 };
184 unpackLen = SmsCommonUtils::Unpack7bitChar(
185 &tpdu[offset], dataLen, 0x00, pageData, MAX_PAGE_PDU_LEN * SMS_BYTE_BIT / GSM_CODE_BIT);
186 for (uint16_t i = 0; i < unpackLen; i++) {
187 messageRaw_.push_back(pageData[i]);
188 }
189 }
190 cbCodec_->SetCbMessageRaw(messageRaw_);
191 return true;
192 }
193
Decode3gUCS2()194 bool GsmCbUmtsCodec::Decode3gUCS2()
195 {
196 if (cbPduBuffer_ == nullptr || cbCodec_ == nullptr || cbPduBuffer_->GetSize() == 0) {
197 TELEPHONY_LOGE("CB pdu data error.");
198 return false;
199 }
200
201 std::vector<unsigned char> dataPdu;
202 cbCodec_->GetPduData(dataPdu);
203 if (dataPdu.size() == 0 || cbPduBuffer_->GetCurPosition() >= cbPduBuffer_->GetSize()) {
204 TELEPHONY_LOGE("dataPdu empty.");
205 return false;
206 }
207
208 uint16_t dataLen = 0;
209 uint16_t offset = 0;
210 uint16_t pduLen = cbPduBuffer_->GetSize() - cbPduBuffer_->GetCurPosition();
211
212 uint8_t *tpdu = dataPdu.data();
213 uint16_t tpduLen = dataPdu.size();
214 for (uint8_t i = 0; i < cbHeader_->totalPages; ++i) {
215 TELEPHONY_LOGI("cbHeader_->totalPages:%{public}d", cbHeader_->totalPages);
216 uint8_t pageLenOffset = static_cast<uint8_t>((i + 1) * MAX_PAGE_PDU_LEN + i);
217 if (pduLen <= pageLenOffset) {
218 TELEPHONY_LOGE("pageLenOffset invalid.");
219 messageRaw_.clear();
220 return false;
221 }
222 if (cbHeader_->dcs.iso639Lang[0]) {
223 dataLen = tpdu[pageLenOffset] - HEX_VALUE_02;
224 offset = (i * MAX_PAGE_PDU_LEN) + i + HEX_VALUE_02;
225 } else {
226 dataLen = tpdu[pageLenOffset];
227 offset = (i * MAX_PAGE_PDU_LEN) + i;
228 }
229 if (dataLen > 0 && dataLen <= MAX_PAGE_PDU_LEN && dataLen < tpduLen) {
230 for (uint8_t i = offset; i < offset + dataLen; i++) {
231 messageRaw_.push_back(static_cast<char>(tpdu[i]));
232 }
233 }
234 }
235 cbCodec_->SetCbMessageRaw(messageRaw_);
236 return true;
237 }
238 } // namespace Telephony
239 } // namespace OHOS