1 /*
2  * Copyright (C) 2021 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 "mms_body_part_header.h"
17 
18 #include "telephony_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace Telephony {
operator =(const MmsBodyPartHeader & srcHeader)22 MmsBodyPartHeader &MmsBodyPartHeader::operator=(const MmsBodyPartHeader &srcHeader)
23 {
24     if (this != &srcHeader) {
25         strContentTransferEncoding_ = srcHeader.strContentTransferEncoding_;
26         strFileName_ = srcHeader.strFileName_;
27         strContentLocation_ = srcHeader.strContentLocation_;
28         strContentID_ = srcHeader.strContentID_;
29         strDisposition_ = srcHeader.strDisposition_;
30         textMap_ = srcHeader.textMap_;
31     }
32     return *this;
33 }
34 
MmsBodyPartHeader(const MmsBodyPartHeader & obj)35 MmsBodyPartHeader::MmsBodyPartHeader(const MmsBodyPartHeader &obj)
36 {
37     *this = obj;
38 }
39 
DumpBodyPartHeader()40 void MmsBodyPartHeader::DumpBodyPartHeader() {}
41 
42 /**
43  * @brief DecodeContentLocation
44  * OMA-TS-MMS_ENC-V1_3-20110913-A   section:7.3.10 X-Mms-Content-Location Field
45  * When used in a PDU other than M-Mbox-Delete.conf and M-Delete.conf:
46  * Content-location-value = Uri-value
47  * When used in the M-Mbox-Delete.conf and M-Delete.conf PDU:
48  * Content-location-Del-value = Value-length Status-count-value Content-location-value
49  * Status-count-value = Integer-value
50  * Uri-value = Text-string
51  * @param decodeBuffer
52  * @param Len
53  * @return true
54  * @return false
55  */
DecodeContentLocation(MmsDecodeBuffer & decodeBuffer,uint32_t & Len)56 bool MmsBodyPartHeader::DecodeContentLocation(MmsDecodeBuffer &decodeBuffer, uint32_t &Len)
57 {
58     std::string sTmp = "";
59     Len = 0;
60     if (!decodeBuffer.DecodeText(sTmp, Len)) {
61         TELEPHONY_LOGE("Body part header decode text fail.");
62         return false;
63     }
64     strContentLocation_ = sTmp;
65     return true;
66 }
67 
68 /**
69  * @brief DecodeContentId
70  * wap-230-wsp-20010705-a   section:8.4.2.67 Content-ID field
71  * Content-ID-value = Quoted-string
72  * @param decodeBuffer
73  * @param Len
74  * @return true
75  * @return false
76  */
DecodeContentId(MmsDecodeBuffer & decodeBuffer,uint32_t & Len)77 bool MmsBodyPartHeader::DecodeContentId(MmsDecodeBuffer &decodeBuffer, uint32_t &Len)
78 {
79     std::string sTmp = "";
80     Len = 0;
81     if (!decodeBuffer.DecodeQuotedText(sTmp, Len)) {
82         TELEPHONY_LOGE("Body part header decode quoted text fail.");
83         return false;
84     }
85     strContentID_ = sTmp;
86     return true;
87 }
88 
89 /**
90  * @brief DecodeContentDisposition
91  * wap-230-wsp-20010705-a   section:8.4.2.53 Content-disposition field
92  * Content-disposition-value = Value-length Disposition *(Parameter)
93  * Disposition = Form-data | Attachment | Inline | Token-text
94  * Form-data = <Octet 128>
95  * Attachment = <Octet 129>
96  * Inline = <Octet 130>
97  * @param decodeBuffer
98  * @param Len
99  * @return true
100  * @return false
101  */
DecodeContentDisposition(MmsDecodeBuffer & decodeBuffer,uint32_t & Len)102 bool MmsBodyPartHeader::DecodeContentDisposition(MmsDecodeBuffer &decodeBuffer, uint32_t &Len)
103 {
104     uint32_t dispositionLength = 0;
105     uint32_t count = 0;
106     if (!decodeBuffer.DecodeUintvar(dispositionLength, count)) {
107         TELEPHONY_LOGE("Body part header decode uintvar fail.");
108         return false;
109     }
110 
111     uint32_t beginPostion = decodeBuffer.GetCurPosition();
112     uint8_t oneByte = 0;
113     if (!decodeBuffer.GetOneByte(oneByte)) {
114         TELEPHONY_LOGE("Body part header decode get one byte fail.");
115         return false;
116     }
117 
118     switch (static_cast<MmsDispositonParam>(oneByte)) {
119         case MmsDispositonParam::P_DISPOSITION_FROM_DATA: {
120             strDisposition_ = DISPOSITION_FROM_DATA;
121             break;
122         }
123         case MmsDispositonParam::P_DISPOSITION_ATTACHMENT: {
124             strDisposition_ = DISPOSITION_ATTACHMENT;
125             break;
126         }
127         case MmsDispositonParam::P_DISPOSITION_INLINE: {
128             strDisposition_ = DISPOSITION_INLINE;
129             break;
130         }
131         default: {
132             if (!decodeBuffer.DecreasePointer(1)) {
133                 TELEPHONY_LOGE("Body part header move pointer fail.");
134                 return false;
135             }
136             std::string strTmp = "";
137             uint32_t tmpLen = 0;
138             if (!decodeBuffer.DecodeText(strTmp, tmpLen)) {
139                 TELEPHONY_LOGE("Body part header decode text fail.");
140                 return false;
141             }
142             strDisposition_ = strTmp;
143             break;
144         }
145     }
146     TELEPHONY_LOGI("strDisposition_ == %{public}s", strDisposition_.c_str());
147     if (!DecodeDispositionParameter(decodeBuffer, dispositionLength, beginPostion)) {
148         TELEPHONY_LOGE("Decode Disposition Parameter error.");
149         return false;
150     }
151     Len = dispositionLength + count + 1;
152     return true;
153 }
154 
155 /**
156  * @brief DecodeContentDispositionParameter
157  * wap-230-wsp-20010705-a   section:8.4.2.53 Content-disposition field
158  * Content-disposition-value = Value-length Disposition *(Parameter)
159  * Disposition = Form-data | Attachment | Inline | Token-text
160  * Form-data = <Octet 128>
161  * Attachment = <Octet 129>
162  * Inline = <Octet 130>
163  * @param decodeBuffer
164  * @param dispLen
165  * @param beginPos
166  * @return true
167  * @return false
168  */
DecodeDispositionParameter(MmsDecodeBuffer & decodeBuffer,uint32_t dispLen,uint32_t beginPos)169 bool MmsBodyPartHeader::DecodeDispositionParameter(
170     MmsDecodeBuffer &decodeBuffer, uint32_t dispLen, uint32_t beginPos)
171 {
172     const uint8_t pFileNameValue = 0x98;
173     uint32_t endPostion = decodeBuffer.GetCurPosition();
174     uint8_t oneByte = 0;
175     if ((endPostion < beginPos) || dispLen <= (endPostion - beginPos)) {
176         TELEPHONY_LOGI("Body part header data.");
177         return true;
178     }
179 
180     if (!decodeBuffer.GetOneByte(oneByte)) {
181         TELEPHONY_LOGE("Body part header decode get one byte fail.");
182         return false;
183     }
184     if (oneByte == pFileNameValue) {
185         std::string strTmp = "";
186         uint32_t tmpLen = 0;
187         if (!decodeBuffer.DecodeText(strTmp, tmpLen)) {
188             TELEPHONY_LOGE("Body part header decode text fail.");
189             return false;
190         }
191         strFileName_ = strTmp;
192     }
193     endPostion = decodeBuffer.GetCurPosition();
194     if ((endPostion < beginPos) || dispLen < (endPostion - beginPos)) {
195         TELEPHONY_LOGE("Body part header data error.");
196         return false;
197     }
198     if (!decodeBuffer.IncreasePointer(dispLen - (endPostion - beginPos))) {
199         TELEPHONY_LOGE("Body part header decode content disposition move pointer err.");
200         return false;
201     }
202     return true;
203 }
204 
205 /**
206  * @brief DecodeWellKnownHeader
207  * wap-230-wsp-20010705-a   section:8.4.2.6 Header
208  * Well-known-header = Well-known-field-name Wap-value
209  * Well-known-field-name = Short-integer
210  * Wap-value = Accept-value | Accept-charset-value |...
211  * @param decodeBuffer
212  * @param headerLen
213  * @return true
214  * @return false
215  */
DecodeWellKnownHeader(MmsDecodeBuffer & decodeBuffer,uint32_t & headerLen)216 bool MmsBodyPartHeader::DecodeWellKnownHeader(MmsDecodeBuffer &decodeBuffer, uint32_t &headerLen)
217 {
218     const uint8_t endStringZeroLen = 1;
219     uint8_t fieldCode = 0xff;
220     uint8_t oneByte = 0;
221     if (!decodeBuffer.GetOneByte(oneByte)) {
222         TELEPHONY_LOGE("Body part header decode get one byte fail.");
223         return false;
224     }
225     headerLen--;
226     uint32_t len = 0;
227     fieldCode = oneByte & 0x7f;
228     switch (static_cast<MmsHeaderParam>(fieldCode)) {
229         case MmsHeaderParam::P_CONTENT_LOCATION_V1: /* Content-Location */
230         case MmsHeaderParam::P_CONTENT_LOCATION_V2: { /* Content-Location */
231             if (!DecodeContentLocation(decodeBuffer, len)) {
232                 TELEPHONY_LOGE("Body part header decode content location fail.");
233                 return false;
234             }
235             break;
236         }
237         case MmsHeaderParam::P_CONTENT_ID: { /* Content-ID */
238             if (!DecodeContentId(decodeBuffer, len)) {
239                 TELEPHONY_LOGE("Body part header decode contentId fail.");
240                 return false;
241             }
242             break;
243         }
244         case MmsHeaderParam::P_CONTENT_DISPOSITION_V1: /* Content-Disposition */
245         case MmsHeaderParam::P_CONTENT_DISPOSITION_V2: {
246             if (!DecodeContentDisposition(decodeBuffer, len)) {
247                 TELEPHONY_LOGE("Body part header decode content disposition fail.");
248                 return false;
249             }
250             break;
251         }
252         default: {
253             std::string sTmp = "";
254             decodeBuffer.DecodeQuotedText(sTmp, len);
255             break;
256         }
257     }
258     if (headerLen > len + endStringZeroLen) {
259         headerLen -= len + endStringZeroLen;
260     } else {
261         headerLen = 0;
262     }
263     return true;
264 }
265 
266 /**
267  * @brief DecodeApplicationHeader
268  * wap-230-wsp-20010705-a   section:8.4.2.6 Header
269  * Application-header		  = Token-text Application-specific-value
270  * Application-specific-value = Text-string
271  * @param decodeBuffer
272  * @param headerLen
273  * @return true
274  * @return false
275  */
DecodeApplicationHeader(MmsDecodeBuffer & decodeBuffer,uint32_t & headerLen)276 bool MmsBodyPartHeader::DecodeApplicationHeader(MmsDecodeBuffer &decodeBuffer, uint32_t &headerLen)
277 {
278     const uint32_t endStringZeroLen = 2;
279     headerLen = 0;
280     std::string sField = "";
281     uint32_t fieldLen = 0;
282     if (!decodeBuffer.DecodeTokenText(sField, fieldLen)) {
283         TELEPHONY_LOGE("Body part header decode token text fail.");
284         return false;
285     }
286 
287     std::string sValue = "";
288     uint32_t valueLen = 0;
289     if (!decodeBuffer.DecodeText(sValue, valueLen)) {
290         TELEPHONY_LOGE("Body part header decode text fail.");
291         return false;
292     }
293     if (sField == "Content-Transfer-Encoding") {
294         strContentTransferEncoding_ = sValue;
295     }
296     if (headerLen > fieldLen + valueLen + endStringZeroLen) {
297         headerLen -= fieldLen + valueLen + endStringZeroLen;
298     } else {
299         headerLen = 0;
300     }
301     return true;
302 }
303 
GetContentId(std::string & contentId)304 bool MmsBodyPartHeader::GetContentId(std::string &contentId)
305 {
306     contentId.clear();
307     contentId = strContentID_;
308     return true;
309 }
310 
SetContentId(std::string contentId)311 bool MmsBodyPartHeader::SetContentId(std::string contentId)
312 {
313     strContentID_ = contentId;
314     return true;
315 }
316 
GetContentTransferEncoding(std::string & contentTransferEncoding)317 bool MmsBodyPartHeader::GetContentTransferEncoding(std::string &contentTransferEncoding)
318 {
319     contentTransferEncoding.clear();
320     contentTransferEncoding = strContentTransferEncoding_;
321     return true;
322 }
323 
SetContentTransferEncoding(std::string contentTransferEncoding)324 bool MmsBodyPartHeader::SetContentTransferEncoding(std::string contentTransferEncoding)
325 {
326     strContentTransferEncoding_ = contentTransferEncoding;
327     return true;
328 }
329 
GetContentLocation(std::string & contentLocation)330 bool MmsBodyPartHeader::GetContentLocation(std::string &contentLocation)
331 {
332     contentLocation.clear();
333     contentLocation.assign(strContentLocation_);
334     return true;
335 }
336 
SetContentLocation(std::string contentLocation)337 bool MmsBodyPartHeader::SetContentLocation(std::string contentLocation)
338 {
339     strContentLocation_ = contentLocation;
340     return true;
341 }
342 
GetContentDisposition(std::string & contentDisposition)343 bool MmsBodyPartHeader::GetContentDisposition(std::string &contentDisposition)
344 {
345     contentDisposition.clear();
346     contentDisposition.assign(strDisposition_);
347     return true;
348 }
349 
SetContentDisposition(std::string contentDisposition)350 bool MmsBodyPartHeader::SetContentDisposition(std::string contentDisposition)
351 {
352     strDisposition_ = contentDisposition;
353     return true;
354 }
355 
356 /**
357  * @brief EncodeContentLocation
358  * OMA-TS-MMS_ENC-V1_3-20110913-A   section:7.3.10 X-Mms-Content-Location Field
359  * When used in a PDU other than M-Mbox-Delete.conf and M-Delete.conf:
360  * Content-location-value = Uri-value
361  * When used in the M-Mbox-Delete.conf and M-Delete.conf PDU:
362  * Content-location-Del-value = Value-length Status-count-value Content-location-value
363  * Status-count-value = Integer-value
364  * Uri-value = Text-string
365  * @param encodeBuffer
366  * @return true
367  * @return false
368  */
EncodeContentLocation(MmsEncodeBuffer & encodeBuffer)369 bool MmsBodyPartHeader::EncodeContentLocation(MmsEncodeBuffer &encodeBuffer)
370 {
371     const uint8_t setHighestBitOne = 0x80;
372     if (strContentLocation_.empty()) {
373         TELEPHONY_LOGI("Body part header encode content location is empty.");
374         return true;
375     }
376 
377     if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsHeaderParam::P_CONTENT_LOCATION_V2) | setHighestBitOne)) {
378         TELEPHONY_LOGE("Body part header encode content location write byte fail.");
379         return false;
380     }
381     if (!encodeBuffer.EncodeText(strContentLocation_)) {
382         TELEPHONY_LOGE("Body part header encode content location fail.");
383         return false;
384     }
385     return true;
386 }
387 
388 /**
389  * @brief EncodeContentId
390  * wap-230-wsp-20010705-a   section:8.4.2.67 Content-ID field
391  * Content-ID-value = Quoted-string
392  * @param encodeBuffer
393  * @return true
394  * @return false
395  */
EncodeContentId(MmsEncodeBuffer & encodeBuffer)396 bool MmsBodyPartHeader::EncodeContentId(MmsEncodeBuffer &encodeBuffer)
397 {
398     const uint8_t setHighestBitOne = 0x80;
399     if (strContentID_.empty()) {
400         TELEPHONY_LOGI("Body part header encode content ID is empty.");
401         return true;
402     }
403 
404     if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsHeaderParam::P_CONTENT_ID) | setHighestBitOne)) {
405         TELEPHONY_LOGE("Body part header encode content ID write byte fail.");
406         return false;
407     }
408     if (!encodeBuffer.EncodeQuotedText(strContentID_)) {
409         TELEPHONY_LOGE("Body part header encode content ID fail.");
410         return false;
411     }
412     return true;
413 }
414 
415 /**
416  * @brief EncodeContentDisposition
417  * wap-230-wsp-20010705-a   section:8.4.2.53 Content-disposition field
418  * Content-disposition-value = Value-length Disposition *(Parameter)
419  * Disposition = Form-data | Attachment | Inline | Token-text
420  * Form-data = <Octet 128>
421  * Attachment = <Octet 129>
422  * Inline = <Octet 130>
423  * @param encodeBuffer
424  * @return true
425  * @return false
426  */
EncodeContentDisposition(MmsEncodeBuffer & encodeBuffer)427 bool MmsBodyPartHeader::EncodeContentDisposition(MmsEncodeBuffer &encodeBuffer)
428 {
429     const uint8_t setHighestBitOne = 0x80;
430     std::vector<std::string> dispVec {DISPOSITION_FROM_DATA, DISPOSITION_ATTACHMENT, DISPOSITION_INLINE};
431     auto it = std::find(dispVec.begin(), dispVec.end(), strDisposition_);
432     if (it == dispVec.end()) {
433         return true;
434     }
435 
436     TELEPHONY_LOGI("strDisposition = %{public}s", strDisposition_.c_str());
437     if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsHeaderParam::P_CONTENT_DISPOSITION_V1) | setHighestBitOne)) {
438         TELEPHONY_LOGE("Body part header encode content disposition write byte fail.");
439         return false;
440     }
441     if (!encodeBuffer.EncodeUintvar(0x01)) {
442         TELEPHONY_LOGE("EncodeContentDisposition EncodeUintvar Error.");
443         return false;
444     }
445     if (strDisposition_ == DISPOSITION_FROM_DATA) {
446         if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsDispositonParam::P_DISPOSITION_FROM_DATA))) {
447             TELEPHONY_LOGE("Body part header encode content disposition write byte fail.");
448             return false;
449         }
450         return true;
451     }
452     if (strDisposition_ == DISPOSITION_ATTACHMENT) {
453         if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsDispositonParam::P_DISPOSITION_ATTACHMENT))) {
454             TELEPHONY_LOGE("Body part header encode content disposition write byte fail.");
455             return false;
456         }
457         return true;
458     }
459     if (strDisposition_ == DISPOSITION_INLINE) {
460         if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsDispositonParam::P_DISPOSITION_INLINE))) {
461             TELEPHONY_LOGE("Body part header encode content disposition write byte fail.");
462             return false;
463         }
464         return true;
465     }
466     return true;
467 }
468 
469 /**
470  * @brief EncodeContentDisposition
471  * wap-230-wsp-20010705-a   section:8.4.2.46 Transfer encoding field
472  * Transfer-encoding-values = Chunked | Token-text
473  * Chunked = <Octet 128>
474  * @param encodeBuffer
475  * @return true
476  * @return false
477  */
EncodeContentTransferEncoding(MmsEncodeBuffer & encodeBuffer)478 bool MmsBodyPartHeader::EncodeContentTransferEncoding(MmsEncodeBuffer &encodeBuffer)
479 {
480     if (strContentTransferEncoding_.empty()) {
481         TELEPHONY_LOGI("Body part header encode content transfer encoding is empty.");
482         return true;
483     }
484 
485     if (!encodeBuffer.EncodeText("Content-Transfer-Encoding")) {
486         TELEPHONY_LOGE("Body part header encode content transfer encoding encode text fail.");
487         return false;
488     }
489     if (!encodeBuffer.EncodeText(strContentTransferEncoding_)) {
490         TELEPHONY_LOGE("Body part header encode content transfer encoding encode text fail.");
491         return false;
492     }
493     return true;
494 }
495 
496 /**
497  * @brief EncodeMmsBodyPartHeader
498  * wap-230-wsp-20010705-a   section:8.4.2.6 Header
499  * Well-known-header = Well-known-field-name Wap-value
500  * Well-known-field-name = Short-integer
501  * Wap-value = Accept-value | Accept-charset-value |...
502  * @param encodeBuffer
503  * @return true
504  * @return false
505  */
EncodeMmsBodyPartHeader(MmsEncodeBuffer & encodeBuffer)506 bool MmsBodyPartHeader::EncodeMmsBodyPartHeader(MmsEncodeBuffer &encodeBuffer)
507 {
508     if (!EncodeContentId(encodeBuffer)) {
509         TELEPHONY_LOGE("Body part header encode ContentId fail.");
510         return false;
511     }
512     if (!EncodeContentLocation(encodeBuffer)) {
513         TELEPHONY_LOGE("Body part header encode ContentLocation fail.");
514         return false;
515     }
516     if (!EncodeContentDisposition(encodeBuffer)) {
517         TELEPHONY_LOGE("Body part header encode ContentDisposition fail.");
518         return false;
519     }
520     if (!EncodeContentTransferEncoding(encodeBuffer)) {
521         TELEPHONY_LOGE("Body part header encode ContentTransferEncoding fail.");
522         return false;
523     }
524     return true;
525 }
526 } // namespace Telephony
527 } // namespace OHOS
528