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