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_pdu_hex_value.h"
17 #include "gsm_sms_param_codec.h"
18 #include "gsm_sms_tpdu_decode.h"
19 #include "gsm_sms_tpdu_encode.h"
20 #include "securec.h"
21 #include "telephony_log_wrapper.h"
22
23 namespace OHOS {
24 namespace Telephony {
25 using namespace std;
26 static constexpr uint8_t SLIDE_DATA_STEP = 2;
27 static constexpr uint16_t MAX_DECODE_LEN = 520;
28 static constexpr uint16_t MAX_ENCODE_LEN = 255;
29
GsmSmsTpduCodec()30 GsmSmsTpduCodec::GsmSmsTpduCodec()
31 {
32 uDataCodec_ = std::make_shared<GsmUserDataPdu>();
33 paramCodec_ = std::make_shared<GsmSmsParamCodec>();
34 }
35
~GsmSmsTpduCodec()36 GsmSmsTpduCodec::~GsmSmsTpduCodec() {}
37
EncodeSmsPdu(std::shared_ptr<SmsTpdu> sourceData,char * pdu,uint16_t pduLen,uint16_t & bufLen)38 bool GsmSmsTpduCodec::EncodeSmsPdu(std::shared_ptr<SmsTpdu> sourceData, char *pdu, uint16_t pduLen, uint16_t &bufLen)
39 {
40 if (pduLen == 0 || pduLen > MAX_ENCODE_LEN) {
41 TELEPHONY_LOGE("pduLen Error.");
42 return false;
43 }
44 if (sourceData == nullptr) {
45 TELEPHONY_LOGE("nullptr error.");
46 return false;
47 }
48
49 auto tpduEncode = std::make_shared<GsmSmsTpduEncode>(uDataCodec_, paramCodec_, shared_from_this());
50 if (tpduEncode == nullptr) {
51 TELEPHONY_LOGE("nullptr Error.");
52 return false;
53 }
54
55 bool encodeRet = 0;
56 SmsWriteBuffer pduBuffer;
57 switch (sourceData->tpduType) {
58 case SMS_TPDU_SUBMIT:
59 encodeRet = tpduEncode->EncodeSubmitPdu(pduBuffer, &(sourceData->data.submit));
60 break;
61 case SMS_TPDU_DELIVER:
62 encodeRet = tpduEncode->EncodeDeliverPdu(pduBuffer, &(sourceData->data.deliver));
63 break;
64 case SMS_TPDU_DELIVER_REP:
65 encodeRet = tpduEncode->EncodeDeliverReportPdu(pduBuffer, &(sourceData->data.deliverRep));
66 break;
67 case SMS_TPDU_STATUS_REP:
68 encodeRet = tpduEncode->EncodeStatusReportPdu(pduBuffer, &(sourceData->data.statusRep));
69 break;
70 default:
71 break;
72 }
73 bufLen = pduBuffer.GetIndex();
74 if (!encodeRet || bufLen > pduLen) {
75 TELEPHONY_LOGE("encode sms fail");
76 return false;
77 }
78 auto bufferRet = pduBuffer.GetPduBuffer();
79 if (bufferRet == nullptr) {
80 TELEPHONY_LOGE("bufferRet nullptr");
81 return false;
82 }
83 std::vector<uint8_t> buf = *(bufferRet);
84 for (uint16_t locate = 0; locate < bufLen; locate++) {
85 pdu[locate] = static_cast<char>(buf[locate]);
86 }
87 TELEPHONY_LOGI("encode sms success");
88 return true;
89 }
90
DecodeSmsPdu(const uint8_t * pTpdu,uint16_t TpduLen,struct SmsTpdu * pSmsTpdu)91 bool GsmSmsTpduCodec::DecodeSmsPdu(const uint8_t *pTpdu, uint16_t TpduLen, struct SmsTpdu *pSmsTpdu)
92 {
93 if (pTpdu == nullptr || pSmsTpdu == nullptr || TpduLen == 0 || TpduLen > MAX_DECODE_LEN) {
94 TELEPHONY_LOGE("nullptr error. or TpduLen Error.");
95 return false;
96 }
97
98 string pduData(TpduLen, '\0');
99 for (int locate = 0; locate < TpduLen; locate++) {
100 pduData[locate] = static_cast<char>(pTpdu[locate]);
101 }
102 SmsReadBuffer buffer(pduData);
103 std::shared_ptr<GsmSmsTpduDecode> tpduDecode =
104 std::make_shared<GsmSmsTpduDecode>(uDataCodec_, paramCodec_, shared_from_this());
105 if (tpduDecode == nullptr) {
106 TELEPHONY_LOGE("nullptr error.");
107 return false;
108 }
109
110 const char mti = pTpdu[0] & HEX_VALUE_03;
111 bool decodeResult = false;
112 switch (mti) {
113 case TYPE_INDICATOR_DELIVER:
114 pSmsTpdu->tpduType = SMS_TPDU_DELIVER;
115 decodeResult = tpduDecode->DecodeDeliver(buffer, &(pSmsTpdu->data.deliver));
116 break;
117 case TYPE_INDICATOR_SUBMIT:
118 pSmsTpdu->tpduType = SMS_TPDU_SUBMIT;
119 decodeResult = tpduDecode->DecodeSubmit(buffer, &(pSmsTpdu->data.submit));
120 break;
121 case TYPE_INDICATOR_STATUS_REP:
122 pSmsTpdu->tpduType = SMS_TPDU_STATUS_REP;
123 decodeResult = tpduDecode->DecodeStatusReport(buffer, &(pSmsTpdu->data.statusRep));
124 break;
125 default:
126 break;
127 }
128 TELEPHONY_LOGD("buffer index:%{public}d", buffer.GetIndex());
129 if (decodeResult && buffer.GetIndex() <= (TpduLen + 1)) {
130 TELEPHONY_LOGD("decode sms success");
131 return true;
132 }
133 TELEPHONY_LOGE("decode sms fail");
134 return false;
135 }
136
ParsePid(const uint8_t pid)137 enum SmsPid GsmSmsTpduCodec::ParsePid(const uint8_t pid)
138 {
139 return (enum SmsPid)pid;
140 }
141
DebugTpdu(SmsReadBuffer & buffer,const enum SmsParseType type)142 void GsmSmsTpduCodec::DebugTpdu(SmsReadBuffer &buffer, const enum SmsParseType type)
143 {
144 char tpduTmp[(MAX_ENCODE_LEN * SLIDE_DATA_STEP) + 1];
145 if (memset_s(tpduTmp, sizeof(tpduTmp), 0x00, sizeof(tpduTmp)) != EOK) {
146 TELEPHONY_LOGE("memset_s error.");
147 return;
148 }
149 uint8_t oneByte = 0;
150 uint8_t step = SLIDE_DATA_STEP;
151 uint16_t tpduLen = buffer.GetSize();
152 for (uint16_t i = 0; i < tpduLen; i++) {
153 if (sizeof(tpduTmp) <= (i * step)) {
154 TELEPHONY_LOGE("data error.");
155 return;
156 }
157 const uint16_t len = sizeof(tpduTmp) - (i * step);
158 if (!buffer.PickOneByteFromIndex(i, oneByte)) {
159 TELEPHONY_LOGE("buffer error.");
160 return;
161 }
162 if (snprintf_s(tpduTmp + (i * step), len - 1, len - 1, "%02X", oneByte) < 0) {
163 TELEPHONY_LOGE("DebugTpdu snprintf_s error");
164 return;
165 }
166 }
167 }
168 } // namespace Telephony
169 } // namespace OHOS