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_sms_param_encode.h"
17
18 #include "gsm_pdu_hex_value.h"
19 #include "gsm_sms_common_utils.h"
20 #include "gsm_user_data_pdu.h"
21 #include "securec.h"
22 #include "sms_common_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 MAX_ADD_PARAM_LEN = 12;
29 static constexpr uint8_t MAX_SMSC_LEN = 20;
30 static constexpr uint8_t SLIDE_DATA_STEP = 2;
31 static constexpr uint8_t DIGIT_TEN = 10;
32 static constexpr uint8_t REL_TIME_LEN = 1;
33 static constexpr uint8_t MIN_SMSC_LEN = 2;
34
EncodeAddressPdu(const struct AddressNumber * num,std::string & resultNum)35 bool GsmSmsParamEncode::EncodeAddressPdu(const struct AddressNumber *num, std::string &resultNum)
36 {
37 if (num == nullptr) {
38 TELEPHONY_LOGE("nullptr error!");
39 return false;
40 }
41 const char *temp = static_cast<const char *>(num->address);
42 size_t tempLen = strlen(temp);
43 if (tempLen > MAX_SMSC_LEN) {
44 TELEPHONY_LOGE("temp over size!");
45 return false;
46 }
47 unsigned char ton = 0;
48 if (temp[0] == '+' && tempLen > 1) {
49 resultNum.push_back(tempLen - 1);
50 temp++;
51 tempLen--;
52 ton = TYPE_INTERNATIONAL;
53 } else {
54 resultNum.push_back(tempLen);
55 ton = num->ton;
56 }
57
58 /* Set TON, NPI */
59 resultNum.push_back(HEX_VALUE_80 + (ton << HEX_VALUE_04) + num->npi);
60 GsmSmsCommonUtils utils;
61 uint8_t length = 0;
62 uint8_t bcd[MAX_ADD_PARAM_LEN] = { 0 };
63 if (!utils.DigitToBcd(temp, tempLen, bcd, MAX_ADD_PARAM_LEN, length)) {
64 TELEPHONY_LOGE("DigitToBcd fail!");
65 return false;
66 }
67 if (resultNum.size() + length > MAX_ADD_PARAM_LEN) {
68 TELEPHONY_LOGE("length over size.");
69 return false;
70 }
71 for (uint8_t index = 0; index < length; index++) {
72 resultNum.push_back(static_cast<char>(bcd[index]));
73 }
74 return true;
75 }
76
EncodeSmscPdu(const char * num,uint8_t * resultNum)77 uint8_t GsmSmsParamEncode::EncodeSmscPdu(const char *num, uint8_t *resultNum)
78 {
79 if (num == nullptr || resultNum == nullptr) {
80 TELEPHONY_LOGE("Address or EncodeAddr is null.");
81 return 0;
82 }
83
84 char newNum[MAX_SMSC_LEN + 1];
85 if (memset_s(newNum, sizeof(newNum), 0x00, sizeof(newNum)) != EOK) {
86 TELEPHONY_LOGE("memset_s error!");
87 return 0;
88 }
89 int32_t ret = 0;
90 if (num[0] == '+') {
91 ret = strncpy_s(newNum, sizeof(newNum), num + 1, MAX_SMSC_LEN);
92 } else {
93 ret = strncpy_s(newNum, sizeof(newNum), num, MAX_SMSC_LEN);
94 }
95 if (ret != EOK) {
96 TELEPHONY_LOGE("EncodeSmscPdu error!");
97 return 0;
98 }
99 /* Set Address */
100 uint8_t encodeLen = 0;
101 GsmSmsCommonUtils utils;
102 if (!utils.DigitToBcd(newNum, strlen(newNum), resultNum, MAX_SMSC_LEN, encodeLen)) {
103 TELEPHONY_LOGE("digit to bcd error!");
104 return 0;
105 }
106 if (encodeLen == 0 || encodeLen >= MAX_SMSC_LEN) {
107 TELEPHONY_LOGE("encodeLen error!");
108 return 0;
109 }
110 resultNum[encodeLen] = '\0';
111 return encodeLen;
112 }
113
EncodeSmscPdu(const struct AddressNumber * num,uint8_t * smscNum,uint8_t smscLen)114 uint8_t GsmSmsParamEncode::EncodeSmscPdu(const struct AddressNumber *num, uint8_t *smscNum, uint8_t smscLen)
115 {
116 if (num == nullptr || smscNum == nullptr) {
117 TELEPHONY_LOGE("nullptr error.");
118 return 0;
119 }
120
121 char newNum[MAX_SMSC_LEN + 1];
122 if (memset_s(newNum, sizeof(newNum), 0x00, sizeof(newNum)) != EOK) {
123 TELEPHONY_LOGE("memset_s error!");
124 return 0;
125 }
126
127 int32_t ret = 0;
128 if (num->address[0] == '+' && strlen(num->address) > 1) {
129 ret = memcpy_s(newNum, sizeof(newNum), num->address + 1, strlen(num->address) - 1);
130 } else {
131 ret = memcpy_s(newNum, sizeof(newNum), num->address, sizeof(newNum));
132 }
133 if (ret != EOK) {
134 TELEPHONY_LOGE("memory copy error!");
135 return 0;
136 }
137 uint8_t addrLen = strlen(newNum);
138 uint8_t dataSize = 0;
139 if (addrLen % SLIDE_DATA_STEP == 0) {
140 dataSize = SLIDE_DATA_STEP + (addrLen / SLIDE_DATA_STEP);
141 } else {
142 dataSize = SLIDE_DATA_STEP + (addrLen / SLIDE_DATA_STEP) + 1;
143 }
144 if (dataSize > MAX_SMSC_LEN || dataSize >= smscLen || dataSize <= 1 || smscLen <= HEX_VALUE_02) {
145 TELEPHONY_LOGE("DataSize error!");
146 return 0;
147 }
148
149 smscNum[0] = static_cast<char>(dataSize - 1);
150 smscNum[1] = HEX_VALUE_80 + (static_cast<unsigned char>(num->ton << HEX_VALUE_04)) + num->npi;
151 GsmSmsCommonUtils utils;
152 uint8_t len = 0;
153 if (smscLen > MIN_SMSC_LEN &&
154 !utils.DigitToBcd(newNum, addrLen, &(smscNum[MIN_SMSC_LEN]), smscLen - MIN_SMSC_LEN, len)) {
155 TELEPHONY_LOGE("digit to bcd error!");
156 return 0;
157 }
158 smscNum[dataSize] = '\0';
159 return dataSize;
160 }
161
EncodeDCS(const struct SmsDcs * dcsData,std::string & returnValue)162 void GsmSmsParamEncode::EncodeDCS(const struct SmsDcs *dcsData, std::string &returnValue)
163 {
164 if (dcsData == nullptr) {
165 TELEPHONY_LOGE("dcsData is null.");
166 return;
167 }
168 switch (dcsData->codingGroup) {
169 case CODING_DELETION_GROUP:
170 case CODING_DISCARD_GROUP:
171 case CODING_STORE_GROUP:
172 /* not supported */
173 break;
174 case CODING_GENERAL_GROUP:
175 if (dcsData->msgClass != SMS_CLASS_UNKNOWN) {
176 returnValue.push_back(HEX_VALUE_10 + dcsData->msgClass);
177 }
178 if (dcsData->bCompressed) {
179 char value = returnValue.front();
180 returnValue.pop_back();
181 returnValue.push_back(value | HEX_VALUE_20);
182 }
183 break;
184 case SMS_CLASS_GROUP:
185 returnValue.push_back(HEX_VALUE_F0 + dcsData->msgClass);
186 break;
187 default:
188 return;
189 }
190
191 char value = returnValue.front();
192 if (!returnValue.empty()) {
193 returnValue.pop_back();
194 }
195 switch (dcsData->codingScheme) {
196 case DATA_CODING_7BIT:
197 break;
198 case DATA_CODING_UCS2:
199 returnValue.push_back(value | HEX_VALUE_08);
200 break;
201 case DATA_CODING_8BIT:
202 returnValue.push_back(value | HEX_VALUE_04);
203 break;
204 default:
205 break;
206 }
207 }
208
EncodeTimePdu(const struct SmsTimeStamp * timeStamp,std::string & resultValue)209 void GsmSmsParamEncode::EncodeTimePdu(const struct SmsTimeStamp *timeStamp, std::string &resultValue)
210 {
211 if (timeStamp == nullptr) {
212 TELEPHONY_LOGE("TimeStamp is null.");
213 return;
214 }
215 if (timeStamp->format == SMS_TIME_ABSOLUTE) {
216 int32_t timeZone = timeStamp->time.absolute.timeZone;
217 resultValue.push_back(((timeStamp->time.absolute.year % DIGIT_TEN) << HEX_VALUE_04) +
218 (timeStamp->time.absolute.year / DIGIT_TEN));
219 resultValue.push_back(((timeStamp->time.absolute.month % DIGIT_TEN) << HEX_VALUE_04) +
220 (timeStamp->time.absolute.month / DIGIT_TEN));
221 resultValue.push_back(
222 ((timeStamp->time.absolute.day % DIGIT_TEN) << HEX_VALUE_04) + (timeStamp->time.absolute.day / DIGIT_TEN));
223 resultValue.push_back(((timeStamp->time.absolute.hour % DIGIT_TEN) << HEX_VALUE_04) +
224 (timeStamp->time.absolute.hour / DIGIT_TEN));
225 resultValue.push_back(((timeStamp->time.absolute.minute % DIGIT_TEN) << HEX_VALUE_04) +
226 (timeStamp->time.absolute.minute / DIGIT_TEN));
227 resultValue.push_back(((timeStamp->time.absolute.second % DIGIT_TEN) << HEX_VALUE_04) +
228 (timeStamp->time.absolute.second / DIGIT_TEN));
229
230 if (timeZone < 0) {
231 resultValue.push_back(HEX_VALUE_08);
232 }
233 char value = (static_cast<uint8_t>(timeStamp->time.absolute.timeZone % DIGIT_TEN) << HEX_VALUE_04) +
234 (timeStamp->time.absolute.timeZone / DIGIT_TEN) + resultValue.front();
235 resultValue.pop_back();
236 resultValue.push_back(value);
237 return;
238 } else if (timeStamp->format == SMS_TIME_RELATIVE) {
239 std::string tempStr(REL_TIME_LEN + 1, '\0');
240 int ret = memcpy_s(tempStr.data(), REL_TIME_LEN + 1, &(timeStamp->time.relative.time), REL_TIME_LEN);
241 if (ret != EOK) {
242 TELEPHONY_LOGE("memcpy_s error!");
243 }
244 resultValue = tempStr;
245 }
246 }
247 } // namespace Telephony
248 } // namespace OHOS
249