• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Copyright (C) 2014 Samsung Electronics Co., Ltd. All rights reserved
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gsm_sms_udata_codec.h"
18 
19 #include <cstdio>
20 #include <cstring>
21 #include <memory>
22 
23 #include "securec.h"
24 #include "sms_common_utils.h"
25 #include "telephony_log_wrapper.h"
26 
27 namespace OHOS {
28 namespace Telephony {
29 using namespace std;
30 template<typename T>
UniquePtrDeleterOneDimension(T ** (& ptr))31 inline void UniquePtrDeleterOneDimension(T **(&ptr))
32 {
33     if (ptr && *ptr) {
34         delete[] *ptr;
35         *ptr = nullptr;
36     }
37 }
38 
EncodeUserData(const struct SmsUserData * (& pUserData),SmsCodingScheme CodingScheme,char * (& pEncodeData))39 int GsmSmsUDataCodec::EncodeUserData(
40     const struct SmsUserData *(&pUserData), SmsCodingScheme CodingScheme, char *(&pEncodeData))
41 {
42     int encodeSize = 0;
43     switch (CodingScheme) {
44         case SMS_CODING_7BIT:
45             encodeSize = EncodeGSMData(pUserData, pEncodeData);
46             break;
47         case SMS_CODING_8BIT:
48             encodeSize = Encode8bitData(pUserData, pEncodeData);
49             break;
50         case SMS_CODING_UCS2:
51             encodeSize = EncodeUCS2Data(pUserData, pEncodeData);
52             break;
53         default:
54             break;
55     }
56     return encodeSize;
57 }
58 
DecodeUserData(const unsigned char * pTpdu,const int tpduLen,bool bHeaderInd,SmsCodingScheme CodingScheme,struct SmsUserData * pUserData)59 int GsmSmsUDataCodec::DecodeUserData(const unsigned char *pTpdu, const int tpduLen, bool bHeaderInd,
60     SmsCodingScheme CodingScheme, struct SmsUserData *pUserData)
61 {
62     int decodeSize = 0;
63     if (memset_s(pUserData, sizeof(struct SmsUserData), 0x00, sizeof(struct SmsUserData)) != EOK) {
64         TELEPHONY_LOGE("memset_s error.");
65         return decodeSize;
66     }
67     switch (CodingScheme) {
68         case SMS_CODING_7BIT:
69             decodeSize = DecodeGSMData(pTpdu, tpduLen, bHeaderInd, pUserData, NULL);
70             break;
71         case SMS_CODING_8BIT:
72             decodeSize = Decode8bitData(pTpdu, bHeaderInd, pUserData, NULL);
73             break;
74         case SMS_CODING_UCS2:
75             decodeSize = DecodeUCS2Data(pTpdu, tpduLen, bHeaderInd, pUserData, NULL);
76             break;
77         default:
78             break;
79     }
80 
81     return decodeSize;
82 }
83 
DecodeUserData(const unsigned char * pTpdu,const int tpduLen,bool bHeaderInd,SmsCodingScheme CodingScheme,struct SmsUserData * pUserData,struct SmsTpud * pTPUD)84 int GsmSmsUDataCodec::DecodeUserData(const unsigned char *pTpdu, const int tpduLen, bool bHeaderInd,
85     SmsCodingScheme CodingScheme, struct SmsUserData *pUserData, struct SmsTpud *pTPUD)
86 {
87     int decodeSize = 0;
88     if (memset_s(pUserData, sizeof(struct SmsUserData), 0x00, sizeof(struct SmsUserData)) != EOK) {
89         TELEPHONY_LOGE("memset_s error.");
90         return decodeSize;
91     }
92     switch (CodingScheme) {
93         case SMS_CODING_7BIT:
94             decodeSize = DecodeGSMData(pTpdu, tpduLen, bHeaderInd, pUserData, pTPUD);
95             break;
96         case SMS_CODING_8BIT:
97             decodeSize = Decode8bitData(pTpdu, bHeaderInd, pUserData, pTPUD);
98             break;
99         case SMS_CODING_UCS2:
100             decodeSize = DecodeUCS2Data(pTpdu, tpduLen, bHeaderInd, pUserData, pTPUD);
101             break;
102         case SMS_CODING_EUCKR:
103             decodeSize = DecodeUCS2Data(pTpdu, tpduLen, bHeaderInd, pUserData, pTPUD);
104             break;
105         default:
106             break;
107     }
108     return decodeSize;
109 }
110 
EncodeGSMData(const struct SmsUserData * pUserData,char * pEncodeData)111 int GsmSmsUDataCodec::EncodeGSMData(const struct SmsUserData *pUserData, char *pEncodeData)
112 {
113     int headerLen = 0;
114     int offset = 0;
115     int fillBits = 0;
116     int packSize = 0;
117     int encodeLen = 0;
118     unsigned char udhl = 0x00;
119 
120     if (pUserData->headerCnt > 0) {
121         offset = HEX_BYTE_STEP;
122     } else {
123         offset = 0x01;
124     }
125     TELEPHONY_LOGI("pUserData->headerCnt [%{public}d]", pUserData->headerCnt);
126     for (int i = 0; i < pUserData->headerCnt; i++) {
127         headerLen = EncodeHeader(pUserData->header[i], &(pEncodeData[offset]));
128         TELEPHONY_LOGI("headerLen [%{public}d]", headerLen);
129         udhl += headerLen;
130         offset += headerLen;
131     }
132     TELEPHONY_LOGI("udhl [%{public}u]", udhl);
133     if (udhl > 0) {
134         fillBits = ((udhl + 1) * BYTE_BIT) % ENCODE_BYTE_BIT; /* + UDHL */
135     }
136     if (fillBits > 0) {
137         fillBits = ENCODE_BYTE_BIT - fillBits;
138     }
139     TELEPHONY_LOGI("fillBits [%{public}d] dataLen [%{public}d]", fillBits, pUserData->length);
140     /* Set UDL, UDHL */
141     if (udhl > 0) {
142         pEncodeData[0] = (((udhl + 1) * 0x08) + fillBits + (pUserData->length * 0x07)) / 0x07;
143         pEncodeData[1] = udhl;
144     } else {
145         pEncodeData[0] = (char)pUserData->length;
146     }
147 
148     packSize = SmsCommonUtils::Pack7bitChar(
149         (unsigned char *)pUserData->data, pUserData->length, fillBits, (unsigned char *)&pEncodeData[offset]);
150     encodeLen = offset + packSize;
151     TELEPHONY_LOGI("packSize [%{public}d] encodeLen [%{public}d]", packSize, encodeLen);
152     return encodeLen;
153 }
154 
Encode8bitData(const struct SmsUserData * pUserData,char * pEncodeData)155 int GsmSmsUDataCodec::Encode8bitData(const struct SmsUserData *pUserData, char *pEncodeData)
156 {
157     int headerLen = 0;
158     int offset = HEX_BYTE_STEP;
159     int fillBits = 0;
160     int encodeLen = 0;
161     unsigned char udhl = 0x00;
162 
163     if (pUserData->headerCnt > 0) {
164         offset = 0x02;
165     } else {
166         offset = 0x01;
167     }
168     /* Encode User Data Header */
169     for (int i = 0; i < pUserData->headerCnt; i++) {
170         headerLen = EncodeHeader(pUserData->header[i], &(pEncodeData[offset]));
171         udhl += headerLen;
172         offset += headerLen;
173     }
174     TELEPHONY_LOGI("fillBits [%{public}d]", fillBits);
175     TELEPHONY_LOGI("dataLen [%{public}d]", pUserData->length);
176     /* Set UDL, UDHL */
177     if (udhl > 0) {
178         pEncodeData[0] = (udhl + 1) + fillBits + pUserData->length;
179         pEncodeData[1] = udhl;
180     } else {
181         pEncodeData[0] = (char)pUserData->length;
182     }
183     if (memcpy_s(&(pEncodeData[offset]), MAX_TPDU_DATA_LEN - offset, pUserData->data, pUserData->length) != EOK) {
184         TELEPHONY_LOGE("Encode8bitData memcpy_s error");
185         return encodeLen;
186     }
187     encodeLen = offset + pUserData->length;
188     return encodeLen;
189 }
190 
EncodeUCS2Data(const struct SmsUserData * pUserData,char * pEncodeData)191 int GsmSmsUDataCodec::EncodeUCS2Data(const struct SmsUserData *pUserData, char *pEncodeData)
192 {
193     int headerLen = 0;
194     int offset = HEX_BYTE_STEP;
195     int fillBits = 0;
196     int encodeLen = 0;
197     unsigned char udhl = 0x00;
198 
199     if (pUserData->headerCnt > 0) {
200         offset = HEX_BYTE_STEP;
201     } else {
202         offset = 1;
203     }
204     /* Encode User Data Header */
205     for (int i = 0; i < pUserData->headerCnt; i++) {
206         headerLen = EncodeHeader(pUserData->header[i], &(pEncodeData[offset]));
207         udhl += headerLen;
208         offset += headerLen;
209     }
210     TELEPHONY_LOGI("fillBits [%{public}d] dataLen [%{public}d]", fillBits, pUserData->length);
211     /* Set UDL, UDHL */
212     if (udhl > 0) {
213         pEncodeData[0] = (udhl + 1) + fillBits + pUserData->length;
214         pEncodeData[1] = udhl;
215     } else {
216         pEncodeData[0] = (char)pUserData->length;
217     }
218     if (memcpy_s(&(pEncodeData[offset]), MAX_TPDU_DATA_LEN - offset, pUserData->data, pUserData->length) != EOK) {
219         TELEPHONY_LOGE("EncodeUCS2Data memcpy_s error");
220         return encodeLen;
221     }
222     encodeLen = offset + pUserData->length;
223     return encodeLen;
224 }
225 
DecodeGSMData(const unsigned char * pTpdu,const int tpduLen,bool bHeaderInd,struct SmsUserData * pUserData,struct SmsTpud * pTPUD)226 int GsmSmsUDataCodec::DecodeGSMData(const unsigned char *pTpdu, const int tpduLen, bool bHeaderInd,
227     struct SmsUserData *pUserData, struct SmsTpud *pTPUD)
228 {
229     int offset = 0;
230     int udl = 0;
231     int udhl = 0;
232     int headerLen = 0;
233     int fillBits = 0;
234     int octetUdl = 0;
235     /* UDL */
236     udl = pTpdu[offset++];
237     octetUdl = (udl * ENCODE_BYTE_BIT) / BYTE_BIT;
238     TELEPHONY_LOGI("udl= %{public}d, tpdulen= %{public}d, octetUdl= %{public}d, bHeaderInd= %{public}d", udl,
239         tpduLen, octetUdl, bHeaderInd);
240     if (udl > MAX_GSM_7BIT_DATA_LEN || octetUdl > tpduLen) {
241         pUserData->length = 0;
242         pUserData->headerCnt = 0;
243         return 0;
244     }
245     /* Setting for Wap Push */
246     if (pTPUD != NULL) {
247         pTPUD->udl = udl;
248         if (memcpy_s(pTPUD->ud, sizeof(pTPUD->ud), &(pTpdu[offset]), udl) != EOK) {
249             TELEPHONY_LOGE("DecodeGSMData memcpy_s error");
250             return 0;
251         }
252         pTPUD->ud[udl] = '\0';
253     }
254     /* Decode User Data Header */
255     if (bHeaderInd == true) {
256         /* UDHL */
257         udhl = pTpdu[offset++];
258         TELEPHONY_LOGI("udhl = %{public}d", udhl);
259         pUserData->headerCnt = 0;
260         for (int i = 0; offset < udhl && i < MAX_UD_HEADER_NUM; i++) {
261             headerLen = DecodeHeader(&(pTpdu[offset]), &(pUserData->header[i]));
262             if (headerLen <= 0) {
263                 TELEPHONY_LOGE("Error to Header. headerLen [%{public}d]", headerLen);
264                 ResetUserData(*pUserData);
265                 return 0;
266             }
267             offset += headerLen;
268             if (offset > (udhl + HEX_BYTE_STEP)) {
269                 TELEPHONY_LOGE("Error to Header. offset [%{public}d] > (udhl [%{public}d] + 2)", offset, udhl);
270                 ResetUserData(*pUserData);
271                 return 0;
272             }
273             pUserData->headerCnt++;
274         }
275     } else {
276         pUserData->headerCnt = 0;
277     }
278 
279     TELEPHONY_LOGI("headerCnt = %{public}d", pUserData->headerCnt);
280     if (udhl > 0) {
281         fillBits = ((udl * ENCODE_BYTE_BIT) - ((udhl + 1) * BYTE_BIT)) % ENCODE_BYTE_BIT;
282         udl = ((udl * ENCODE_BYTE_BIT) - ((udhl + 1) * BYTE_BIT)) / ENCODE_BYTE_BIT;
283     }
284     TELEPHONY_LOGI("fillBits = %{public}d udhl = %{public}d udl = %{public}d offset = %{public}d", fillBits, udhl,
285         udl, offset);
286     pUserData->length = SmsCommonUtils::Unpack7bitChar(&(pTpdu[offset]), udl, fillBits,
287         (unsigned char *)pUserData->data, MAX_USER_DATA_LEN + 1);
288     return pUserData->length;
289 }
290 
ResetUserData(struct SmsUserData & userData)291 void GsmSmsUDataCodec::ResetUserData(struct SmsUserData &userData)
292 {
293     userData.length = 0;
294     if (memset_s(userData.data, sizeof(userData.data), 0x00, sizeof(userData.data)) != EOK) {
295         TELEPHONY_LOGE("ResetUserData memset_s error!");
296         return;
297     }
298     TELEPHONY_LOGI("ResetUserData memset_s ok!");
299 }
300 
Decode8bitData(const unsigned char * pTpdu,bool bHeaderInd,struct SmsUserData * pUserData,struct SmsTpud * pTPUD)301 int GsmSmsUDataCodec::Decode8bitData(
302     const unsigned char *pTpdu, bool bHeaderInd, struct SmsUserData *pUserData, struct SmsTpud *pTPUD)
303 {
304     int offset = 0;
305     int udl = 0;
306     int udhl = 0;
307     int headerLen = 0;
308     /* UDL */
309     udl = pTpdu[offset++];
310     if (udl > MAX_UCS2_DATA_LEN) {
311         pUserData->length = 0;
312         return 0;
313     }
314     /* Setting for Wap Push */
315     if (pTPUD != nullptr) {
316         pTPUD->udl = udl;
317         if (memcpy_s(pTPUD->ud, sizeof(pTPUD->ud), &(pTpdu[offset]), udl) != EOK) {
318             TELEPHONY_LOGE("memcpy_s error.");
319             return 0;
320         }
321         pTPUD->ud[udl] = '\0';
322     }
323     TELEPHONY_LOGI("udl = %{public}d  bHeaderInd = %{public}d", udl, bHeaderInd);
324     /* Decode User Data Header */
325     if (bHeaderInd == true) {
326         /* UDHL */
327         udhl = pTpdu[offset++];
328         TELEPHONY_LOGI("udhl = %{public}d", udhl);
329         pUserData->headerCnt = 0;
330         for (int i = 0; offset < udhl && i < MAX_UD_HEADER_NUM; i++) {
331             headerLen = DecodeHeader(&(pTpdu[offset]), &(pUserData->header[i]));
332             if (headerLen <= 0) {
333                 pUserData->length = 0;
334                 if (memset_s(pUserData->data, sizeof(pUserData->data), 0x00, sizeof(pUserData->data)) != EOK) {
335                     TELEPHONY_LOGE("memset_s fail.");
336                 }
337                 return 0;
338             }
339             offset += headerLen;
340             if (offset > (udhl + HEX_BYTE_STEP)) {
341                 pUserData->length = 0;
342                 if (memset_s(pUserData->data, sizeof(pUserData->data), 0x00, sizeof(pUserData->data)) != EOK) {
343                     TELEPHONY_LOGE("memset_s fail.");
344                 }
345                 return 0;
346             }
347             pUserData->headerCnt++;
348         }
349     } else {
350         pUserData->headerCnt = 0;
351     }
352     if (udhl > 0) {
353         pUserData->length = (udl) - (udhl + 1);
354     } else {
355         pUserData->length = udl;
356     }
357     TELEPHONY_LOGI("pUserData->length= %{public}d  offset= %{public}d", pUserData->length, offset);
358     if (memcpy_s(pUserData->data, sizeof(pUserData->data), &(pTpdu[offset]), pUserData->length) != EOK) {
359         return 0;
360     }
361     return pUserData->length;
362 }
363 
DecodeUCS2Data(const unsigned char * pTpdu,const int tpduLen,bool bHeaderInd,struct SmsUserData * pUserData,struct SmsTpud * pTPUD)364 int GsmSmsUDataCodec::DecodeUCS2Data(const unsigned char *pTpdu, const int tpduLen, bool bHeaderInd,
365     struct SmsUserData *pUserData, struct SmsTpud *pTPUD)
366 {
367     int offset = 0;
368     int udl = 0;
369     int udhl = 0;
370     int headerLen = 0;
371     /* UDL */
372     udl = pTpdu[offset++];
373     if (udl > MAX_UCS2_DATA_LEN || udl > tpduLen) {
374         pUserData->length = 0;
375         pUserData->headerCnt = 0;
376         return 0;
377     }
378     /* Setting for Wap Push */
379     if (pTPUD != nullptr) {
380         pTPUD->udl = udl;
381         if (memcpy_s(pTPUD->ud, sizeof(pTPUD->ud), &(pTpdu[offset]), udl) != EOK) {
382             TELEPHONY_LOGE("memcpy_s error.");
383             return 0;
384         }
385         pTPUD->ud[udl] = '\0';
386     }
387     /* Decode User Data Header */
388     if (bHeaderInd == true) {
389         /* UDHL */
390         udhl = pTpdu[offset++];
391         pUserData->headerCnt = 0;
392         for (int i = 0; offset < udhl && i < MAX_UD_HEADER_NUM; i++) {
393             headerLen = DecodeHeader(&(pTpdu[offset]), &(pUserData->header[i]));
394             if (headerLen <= 0) {
395                 ResetUserData(*pUserData);
396                 return 0;
397             }
398             offset += headerLen;
399             if (offset > (udhl + HEX_BYTE_STEP)) {
400                 ResetUserData(*pUserData);
401                 return 0;
402             }
403             pUserData->headerCnt++;
404         }
405     } else {
406         pUserData->headerCnt = 0;
407     }
408 
409     if (udhl > 0) {
410         pUserData->length = (udl) - (udhl + 1);
411     } else {
412         pUserData->length = udl;
413     }
414     TELEPHONY_LOGI("pUserData->length= %{public}d", pUserData->length);
415     if (memcpy_s(pUserData->data, sizeof(pUserData->data), &(pTpdu[offset]), pUserData->length) != EOK) {
416         TELEPHONY_LOGE("memcpy_s error.");
417         return 0;
418     }
419     pUserData->data[pUserData->length] = 0;
420     return pUserData->length;
421 }
422 
EncodeHeader(const struct SmsUDH header,char * pHeader)423 int GsmSmsUDataCodec::EncodeHeader(const struct SmsUDH header, char *pHeader)
424 {
425     int index = 0;
426     int addrLen = 0;
427     char *encodedAddr = nullptr;
428     unique_ptr<char *, void (*)(char **(&))> addressBuf(&encodedAddr, UniquePtrDeleterOneDimension);
429     switch (header.udhType) {
430         case SMS_UDH_CONCAT_8BIT:
431         case SMS_UDH_CONCAT_16BIT:
432             index += EncodeHeaderConcat(header, pHeader);
433             break;
434         case SMS_UDH_APP_PORT_8BIT:
435             /* IEI */
436             pHeader[index++] = SMS_UDH_APP_PORT_8BIT;
437             /* IEDL */
438             pHeader[index++] = 0x02;
439             /* Dest Port */
440             pHeader[index++] = header.udh.appPort8bit.destPort;
441             /* Origin Port */
442             pHeader[index++] = header.udh.appPort8bit.originPort;
443             break;
444         case SMS_UDH_APP_PORT_16BIT:
445             /* IEI */
446             pHeader[index++] = SMS_UDH_APP_PORT_16BIT;
447             /* IEDL */
448             pHeader[index++] = 0x04;
449             /* Dest Port */
450             pHeader[index++] = (char)(header.udh.appPort16bit.destPort >> BYTE_BIT);
451             pHeader[index++] = header.udh.appPort16bit.destPort & 0x00FF;
452             /* Origin Port */
453             pHeader[index++] = (char)(header.udh.appPort16bit.originPort >> BYTE_BIT);
454             pHeader[index++] = header.udh.appPort16bit.originPort & 0x00FF;
455             break;
456         case SMS_UDH_ALTERNATE_REPLY_ADDRESS:
457             /* IEI */
458             pHeader[index++] = SMS_UDH_ALTERNATE_REPLY_ADDRESS;
459             addrLen = GsmSmsParamCodec::EncodeAddress(&(header.udh.alternateAddress), &encodedAddr);
460             /* IEDL */
461             pHeader[index++] = addrLen;
462             /* Alternate Reply Address */
463             if (memcpy_s(&pHeader[index], addrLen, encodedAddr, addrLen) != EOK) {
464                 TELEPHONY_LOGE("EncodeHeader memcpy_s error");
465                 return index;
466             }
467             index += addrLen;
468             break;
469         case SMS_UDH_SINGLE_SHIFT:
470             /* IEI */
471             pHeader[index++] = SMS_UDH_SINGLE_SHIFT;
472             /* IEDL */
473             pHeader[index++] = 0x01;
474             /* National Language Identifier */
475             pHeader[index++] = header.udh.singleShift.langId;
476             break;
477         case SMS_UDH_LOCKING_SHIFT:
478             /* IEI */
479             pHeader[index++] = SMS_UDH_LOCKING_SHIFT;
480             /* IEDL */
481             pHeader[index++] = 0x01;
482             /* National Language Identifier */
483             pHeader[index++] = header.udh.lockingShift.langId;
484             break;
485         case SMS_UDH_NONE:
486         default:
487             break;
488     }
489     return index;
490 }
491 
EncodeHeaderConcat(const struct SmsUDH header,char * pHeader)492 int GsmSmsUDataCodec::EncodeHeaderConcat(const struct SmsUDH header, char *pHeader)
493 {
494     int index = 0;
495     switch (header.udhType) {
496         case SMS_UDH_CONCAT_8BIT:
497             /* IEI */
498             pHeader[index++] = SMS_UDH_CONCAT_8BIT;
499             /* IEDL */
500             pHeader[index++] = 0x03;
501             /* Reference Number */
502             pHeader[index++] = header.udh.concat8bit.msgRef;
503             /* Number of Segments */
504             pHeader[index++] = header.udh.concat8bit.totalSeg;
505             /* Sequence Number */
506             pHeader[index++] = header.udh.concat8bit.seqNum;
507             break;
508         case SMS_UDH_CONCAT_16BIT:
509             /* IEI */
510             pHeader[index++] = SMS_UDH_CONCAT_16BIT;
511             /* IEDL */
512             pHeader[index++] = 0x04;
513             /* Reference Number */
514             pHeader[index++] = (char)(header.udh.concat16bit.msgRef >> BYTE_BIT);
515             pHeader[index++] = header.udh.concat16bit.msgRef & 0x00FF;
516             /* Number of Segments */
517             pHeader[index++] = header.udh.concat16bit.totalSeg;
518             /* Sequence Number */
519             pHeader[index++] = header.udh.concat16bit.seqNum;
520             break;
521         case SMS_UDH_NONE:
522         default:
523             break;
524     }
525     return index;
526 }
527 
DecodeHeader(const unsigned char * pTpdu,struct SmsUDH * pHeader)528 int GsmSmsUDataCodec::DecodeHeader(const unsigned char *pTpdu, struct SmsUDH *pHeader)
529 {
530     int offset = 0;
531     unsigned char IEDL = 0;
532     pHeader->udhType = pTpdu[offset++];
533     switch (pHeader->udhType) {
534         case SMS_UDH_CONCAT_8BIT: {
535             IEDL = pTpdu[offset++];
536             if (IEDL == 0) {
537                 return 0;
538             }
539             pHeader->udh.concat8bit.msgRef = pTpdu[offset++];
540             pHeader->udh.concat8bit.totalSeg = pTpdu[offset++];
541             pHeader->udh.concat8bit.seqNum = pTpdu[offset++];
542             DebugDecodeHeader(pHeader);
543             break;
544         }
545         case SMS_UDH_CONCAT_16BIT: {
546             IEDL = pTpdu[offset++];
547             if (IEDL == 0) {
548                 return 0;
549             }
550             pHeader->udh.concat16bit.msgRef = pTpdu[offset++];
551             pHeader->udh.concat16bit.msgRef =
552                 (unsigned short)((pHeader->udh.concat16bit.msgRef << BYTE_BIT) | pTpdu[offset++]);
553             pHeader->udh.concat16bit.totalSeg = pTpdu[offset++];
554             pHeader->udh.concat16bit.seqNum = pTpdu[offset++];
555             DebugDecodeHeader(pHeader);
556             break;
557         }
558         case SMS_UDH_APP_PORT_8BIT: {
559             IEDL = pTpdu[offset++];
560             if (IEDL == 0) {
561                 return 0;
562             }
563             pHeader->udh.appPort8bit.destPort = pTpdu[offset++];
564             pHeader->udh.appPort8bit.originPort = pTpdu[offset++];
565             DebugDecodeHeader(pHeader);
566             break;
567         }
568         case SMS_UDH_APP_PORT_16BIT: {
569             IEDL = pTpdu[offset++];
570             if (IEDL == 0) {
571                 return 0;
572             }
573             pHeader->udh.appPort16bit.destPort = pTpdu[offset++];
574             pHeader->udh.appPort16bit.destPort =
575                 (unsigned short)((pHeader->udh.appPort16bit.destPort << BYTE_BIT) | pTpdu[offset++]);
576             pHeader->udh.appPort16bit.originPort = pTpdu[offset++];
577             pHeader->udh.appPort16bit.originPort =
578                 (unsigned short)((pHeader->udh.appPort16bit.originPort << BYTE_BIT) | pTpdu[offset++]);
579             DebugDecodeHeader(pHeader);
580             break;
581         }
582         case SMS_UDH_SPECIAL_SMS: {
583             IEDL = pTpdu[offset++];
584             if (IEDL != 0x02) {
585                 return 0;
586             }
587             TELEPHONY_LOGI("Decoding special sms udh.");
588             pHeader->udh.specialInd.bStore = (bool)(pTpdu[offset] & 0x80);
589             pHeader->udh.specialInd.msgInd = (unsigned short)(pTpdu[offset++] & 0x7F);
590             pHeader->udh.specialInd.waitMsgNum = (unsigned short)pTpdu[offset];
591             break;
592         }
593         case SMS_UDH_ALTERNATE_REPLY_ADDRESS: {
594             IEDL = pTpdu[offset++];
595             if (IEDL == 0) {
596                 return 0;
597             }
598             offset += GsmSmsParamCodec::DecodeAddress(&pTpdu[offset], &(pHeader->udh.alternateAddress));
599             TELEPHONY_LOGI("alternate reply address [%{public}s]", pHeader->udh.alternateAddress.address);
600             break;
601         }
602         case SMS_UDH_SINGLE_SHIFT: {
603             IEDL = pTpdu[offset++];
604             if (IEDL == 0) {
605                 return 0;
606             }
607             pHeader->udh.singleShift.langId = pTpdu[offset++];
608             TELEPHONY_LOGI("singleShift.langId [%{public}02x]", pHeader->udh.singleShift.langId);
609             break;
610         }
611         case SMS_UDH_LOCKING_SHIFT: {
612             IEDL = pTpdu[offset++];
613             if (IEDL == 0) {
614                 return 0;
615             }
616             pHeader->udh.lockingShift.langId = pTpdu[offset++];
617             TELEPHONY_LOGI("lockingShift.langId [%{public}02x]", pHeader->udh.lockingShift.langId);
618             break;
619         }
620         default: {
621             TELEPHONY_LOGI("Not Supported Header Type [%{public}02x]", pHeader->udhType);
622             IEDL = pTpdu[offset++];
623             TELEPHONY_LOGI("IEDL [%{public}u]", IEDL);
624             return (offset + IEDL);
625             break;
626         }
627     }
628     return offset;
629 }
630 
DebugDecodeHeader(const struct SmsUDH * pHeader)631 void GsmSmsUDataCodec::DebugDecodeHeader(const struct SmsUDH *pHeader)
632 {
633     if (pHeader == nullptr) {
634         TELEPHONY_LOGE("DebugDecodeHeader pHeader nullptr");
635         return;
636     }
637     switch (pHeader->udhType) {
638         case SMS_UDH_CONCAT_8BIT: {
639             TELEPHONY_LOGI("concat8bit.seqNum [%{public}02x]", pHeader->udh.concat8bit.seqNum);
640             break;
641         }
642         case SMS_UDH_CONCAT_16BIT: {
643             TELEPHONY_LOGI("concat16bit.seqNum [%{public}02x]", pHeader->udh.concat16bit.seqNum);
644             break;
645         }
646         case SMS_UDH_APP_PORT_8BIT: {
647             TELEPHONY_LOGI("appPort8bit.destPort [%{public}02x]", pHeader->udh.appPort8bit.destPort);
648             TELEPHONY_LOGI("appPort8bit.originPort [%{public}02x]", pHeader->udh.appPort8bit.originPort);
649             break;
650         }
651         case SMS_UDH_APP_PORT_16BIT: {
652             TELEPHONY_LOGI("appPort16bit.destPort [%{public}04x]", pHeader->udh.appPort16bit.destPort);
653             TELEPHONY_LOGI("appPort16bit.originPort [%{public}04x]", pHeader->udh.appPort16bit.originPort);
654             break;
655         }
656         case SMS_UDH_SPECIAL_SMS: {
657             TELEPHONY_LOGI("Decoding special sms udh.");
658             break;
659         }
660         case SMS_UDH_ALTERNATE_REPLY_ADDRESS:
661             break;
662         case SMS_UDH_SINGLE_SHIFT: {
663             TELEPHONY_LOGI("singleShift.langId [%{public}02x]", pHeader->udh.singleShift.langId);
664             break;
665         }
666         case SMS_UDH_LOCKING_SHIFT: {
667             TELEPHONY_LOGI("lockingShift.langId [%{public}02x]", pHeader->udh.lockingShift.langId);
668             break;
669         }
670         default:
671             break;
672     }
673 }
674 } // namespace Telephony
675 } // namespace OHOS