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_gsm_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 uint8_t MAX_ETWS_PDU_LEN = 56;
32 static constexpr uint8_t MAX_PAGE_NUM = 15;
33 static constexpr uint16_t GSM_ETWS_BASE_MASK = 0x1100;
34 
GsmCbGsmCodec(std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> header,std::shared_ptr<GsmCbPduDecodeBuffer> buffer,std::shared_ptr<GsmCbCodec> cbCodec)35 GsmCbGsmCodec::GsmCbGsmCodec(std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> header,
36     std::shared_ptr<GsmCbPduDecodeBuffer> buffer, std::shared_ptr<GsmCbCodec> cbCodec)
37 {
38     cbHeader_ = header;
39     cbPduBuffer_ = buffer;
40     cbCodec_ = cbCodec;
41 }
42 
~GsmCbGsmCodec()43 GsmCbGsmCodec::~GsmCbGsmCodec() {}
44 
45 /**
46  * refer to 3GPP TS 23.041 V4.1.0 9.4 Message Format on the Radio Network  – MS/UE Interface
47  * refer to 3GPP TS 23.041 V4.1.0 9.4.1.2 Message Parameter
48  */
Decode2gHeader()49 bool GsmCbGsmCodec::Decode2gHeader()
50 {
51     if (cbPduBuffer_ == nullptr || cbHeader_ == nullptr || cbPduBuffer_->GetSize() == 0) {
52         TELEPHONY_LOGE("CB pdu data error.");
53         return false;
54     }
55 
56     cbHeader_->bEtwsMessage = false;
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     cbHeader_->serialNum.geoScope = (temp & HEX_VALUE_C0) >> HEX_VALUE_06;
64     cbHeader_->serialNum.msgCode = (temp & HEX_VALUE_3F) << HEX_VALUE_04;
65 
66     if (!cbPduBuffer_->GetOneByte(oneByte)) {
67         TELEPHONY_LOGE("get data error.");
68         return false;
69     }
70     temp = oneByte;
71     cbHeader_->serialNum.msgCode |= (temp & HEX_VALUE_F0) >> HEX_VALUE_04;
72     cbHeader_->serialNum.updateNum = temp & HEX_VALUE_0F;
73 
74     if (!cbPduBuffer_->GetOneByte(oneByte)) {
75         TELEPHONY_LOGE("get data error.");
76         return false;
77     }
78     temp = oneByte;
79 
80     if (!cbPduBuffer_->GetOneByte(oneByte)) {
81         TELEPHONY_LOGE("get data error.");
82         return false;
83     }
84     cbHeader_->msgId = (temp << HEX_VALUE_08) | oneByte;
85     bool isEtws;
86     cbCodec_->IsEtwsMessage(isEtws);
87     if (isEtws && cbPduBuffer_->GetSize() <= MAX_ETWS_PDU_LEN) {
88         if (!Decode2gHeaderEtws()) {
89             TELEPHONY_LOGE("etws head error.");
90             return false;
91         }
92     } else {
93         if (!Decode2gHeaderCommonCb()) {
94             TELEPHONY_LOGE("common cb head error.");
95             return false;
96         }
97     }
98     return true;
99 }
100 
Decode2gHeaderEtws()101 bool GsmCbGsmCodec::Decode2gHeaderEtws()
102 {
103     uint8_t oneByte = 0;
104     cbHeader_->cbEtwsType = GsmCbCodec::ETWS_PRIMARY;
105     cbHeader_->bEtwsMessage = true;
106     if (!cbPduBuffer_->GetOneByte(oneByte)) {
107         TELEPHONY_LOGE("get data error.");
108         return false;
109     }
110     cbHeader_->warningType = (oneByte & HEX_VALUE_FE) >> 1;
111     if (cbHeader_->msgId >= GSM_ETWS_BASE_MASK) {
112         cbCodec_->GetWarningType(cbHeader_->warningType);
113     }
114     if (!cbPduBuffer_->GetOneByte(oneByte)) {
115         TELEPHONY_LOGE("get data error.");
116         return false;
117     }
118     cbHeader_->totalPages = 1;
119     bool activatePopup = (oneByte & HEX_VALUE_80) != 0;
120     TELEPHONY_LOGI("activatePopup:%{public}d.", activatePopup);
121     return true;
122 }
123 
Decode2gHeaderCommonCb()124 bool GsmCbGsmCodec::Decode2gHeaderCommonCb()
125 {
126     if (cbPduBuffer_ == nullptr || cbHeader_ == nullptr || cbCodec_ == nullptr || cbPduBuffer_->GetSize() == 0) {
127         TELEPHONY_LOGE("CB pdu data error.");
128         return false;
129     }
130     uint8_t oneByte = 0;
131     if (!cbPduBuffer_->GetOneByte(oneByte)) {
132         TELEPHONY_LOGE("get data error.");
133         return false;
134     }
135     uint8_t dcs = oneByte;
136 
137     if (!cbPduBuffer_->GetOneByte(oneByte)) {
138         TELEPHONY_LOGE("get data error.");
139         return false;
140     }
141     uint8_t temp = oneByte;
142     cbHeader_->totalPages = temp & HEX_VALUE_0F;
143     cbHeader_->page = (temp & HEX_VALUE_F0) >> HEX_VALUE_04;
144 
145     if (!cbPduBuffer_->GetOneByte(oneByte)) {
146         TELEPHONY_LOGE("get data error.");
147         return false;
148     }
149     unsigned short iosTemp = oneByte;
150 
151     if (!cbPduBuffer_->GetOneByte(oneByte)) {
152         TELEPHONY_LOGE("get data error.");
153         return false;
154     }
155     iosTemp |= (oneByte << SMS_BYTE_BIT);
156     cbCodec_->DecodeCbMsgDCS(dcs, iosTemp, cbHeader_->dcs);
157     cbHeader_->langType = cbHeader_->dcs.langType;
158     cbHeader_->recvTime = static_cast<time_t>(cbCodec_->GetRecvTime());
159     if (cbHeader_->totalPages > MAX_PAGE_NUM) {
160         TELEPHONY_LOGE("CB Page Count is over MAX[%{public}d]", cbHeader_->totalPages);
161         cbPduBuffer_->SetPointer(0);
162     }
163     bool isEtws;
164     cbCodec_->IsEtwsMessage(isEtws);
165     if (isEtws) {
166         cbHeader_->bEtwsMessage = true;
167         cbHeader_->cbEtwsType = GsmCbCodec::ETWS_GSM;
168         if (cbHeader_->msgId >= GSM_ETWS_BASE_MASK) {
169             cbCodec_->GetWarningType(cbHeader_->warningType);
170         }
171     }
172     if (cbPduBuffer_->GetCurPosition() >= HEX_VALUE_02) {
173         cbPduBuffer_->SetPointer(cbPduBuffer_->GetCurPosition() - HEX_VALUE_02);
174     }
175     return true;
176 }
177 
178 /**
179  * refer to 3GPP TS 23.041 V4.1.0 9.4.2.2 Message Parameter
180  */
Decode2gCbMsg()181 bool GsmCbGsmCodec::Decode2gCbMsg()
182 {
183     if (cbPduBuffer_ == nullptr || cbHeader_ == nullptr || cbCodec_ == nullptr ||
184         cbPduBuffer_->GetCurPosition() >= cbPduBuffer_->GetSize()) {
185         TELEPHONY_LOGE("CB pdu data error.");
186         return false;
187     }
188 
189     uint16_t dataLen = cbPduBuffer_->GetSize() - cbPduBuffer_->GetCurPosition();
190     switch (cbHeader_->dcs.codingScheme) {
191         case DATA_CODING_7BIT: {
192             if (!Decode2gCbMsg7bit(dataLen)) {
193                 TELEPHONY_LOGE("decode cb 7bit error.");
194                 return false;
195             }
196             break;
197         }
198         case DATA_CODING_8BIT:
199         case DATA_CODING_UCS2: {
200             if (dataLen < HEX_VALUE_02) {
201                 TELEPHONY_LOGE("dataLen error.");
202                 return false;
203             }
204             if (cbHeader_->dcs.iso639Lang[0]) {
205                 TELEPHONY_LOGI("dcs.iso639Lang");
206                 cbPduBuffer_->SetPointer(cbPduBuffer_->GetCurPosition() + HEX_VALUE_02);
207                 dataLen -= HEX_VALUE_02;
208             }
209 
210             if (dataLen == 0 || cbPduBuffer_->pduBuffer_ == nullptr ||
211                 (cbPduBuffer_->GetCurPosition() + dataLen) > cbPduBuffer_->GetSize()) {
212                 TELEPHONY_LOGE("CB pdu data error.");
213                 return false;
214             }
215             for (uint8_t i = cbPduBuffer_->GetCurPosition(); i < cbPduBuffer_->GetSize(); i++) {
216                 messageRaw_.push_back(cbPduBuffer_->pduBuffer_[i]);
217             }
218             cbCodec_->SetCbMessageRaw(messageRaw_);
219             break;
220         }
221         default:
222             break;
223     }
224     return true;
225 }
226 
Decode2gCbMsg7bit(uint16_t dataLen)227 bool GsmCbGsmCodec::Decode2gCbMsg7bit(uint16_t dataLen)
228 {
229     uint8_t pageData[MAX_PAGE_PDU_LEN * SMS_BYTE_BIT / GSM_CODE_BIT + 1] = { 0 };
230     std::vector<uint8_t> dataPdu;
231     cbCodec_->GetPduData(dataPdu);
232 
233     if (dataPdu.size() == 0) {
234         TELEPHONY_LOGE("dataPdu empty.");
235         return false;
236     }
237     uint16_t unpackLen = SmsCommonUtils::Unpack7bitCharForCBPdu(
238         dataPdu.data(), dataLen, 0x00, pageData, MAX_PAGE_PDU_LEN * SMS_BYTE_BIT / GSM_CODE_BIT + 1);
239 
240     uint16_t offset = 0;
241     if (cbHeader_->dcs.iso639Lang[0] && unpackLen >= GsmCbCodec::CB_IOS639_LANG_SIZE) {
242         unpackLen = unpackLen - GsmCbCodec::CB_IOS639_LANG_SIZE;
243         offset = GsmCbCodec::CB_IOS639_LANG_SIZE;
244     }
245     if (offset + unpackLen >= (MAX_PAGE_PDU_LEN * SMS_BYTE_BIT / GSM_CODE_BIT) + 1) {
246         TELEPHONY_LOGE("CB pdu data error.");
247         return false;
248     }
249     for (uint8_t i = 0; i < unpackLen; i++) {
250         messageRaw_.push_back(static_cast<char>(pageData[i]));
251     }
252     cbCodec_->SetCbMessageRaw(messageRaw_);
253     return true;
254 }
255 
DecodeEtwsMsg()256 bool GsmCbGsmCodec::DecodeEtwsMsg()
257 {
258     if (cbPduBuffer_ == nullptr || cbPduBuffer_->pduBuffer_ == nullptr || cbPduBuffer_->GetSize() == 0 ||
259         cbPduBuffer_->GetSize() > MAX_ETWS_PDU_LEN) {
260         TELEPHONY_LOGE("CB pdu data error.");
261         return false;
262     }
263     uint8_t total = cbPduBuffer_->GetSize() - cbPduBuffer_->GetCurPosition();
264     return Decode2gCbMsg7bit(total);
265 }
266 } // namespace Telephony
267 } // namespace OHOS