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
174 uint32_t endPostion = decodeBuffer.GetCurPosition();
175 uint8_t oneByte = 0;
176 if (dispLen > (endPostion - beginPos)) {
177 if (!decodeBuffer.GetOneByte(oneByte)) {
178 TELEPHONY_LOGE("Body part header decode get one byte fail.");
179 return false;
180 }
181 if (oneByte == pFileNameValue) {
182 std::string strTmp = "";
183 uint32_t tmpLen = 0;
184 if (!decodeBuffer.DecodeText(strTmp, tmpLen)) {
185 TELEPHONY_LOGE("Body part header decode text fail.");
186 return false;
187 }
188 strFileName_ = strTmp;
189 }
190 endPostion = decodeBuffer.GetCurPosition();
191 if (dispLen < (endPostion - beginPos)) {
192 TELEPHONY_LOGE("Body part header decode content disposition length err.");
193 return false;
194 }
195 if (!decodeBuffer.IncreasePointer(dispLen - (endPostion - beginPos))) {
196 TELEPHONY_LOGE("Body part header decode content disposition move pointer err.");
197 return false;
198 }
199 }
200 return true;
201 }
202
203 /**
204 * @brief DecodeWellKnownHeader
205 * wap-230-wsp-20010705-a section:8.4.2.6 Header
206 * Well-known-header = Well-known-field-name Wap-value
207 * Well-known-field-name = Short-integer
208 * Wap-value = Accept-value | Accept-charset-value |...
209 * @param decodeBuffer
210 * @param headerLen
211 * @return true
212 * @return false
213 */
DecodeWellKnownHeader(MmsDecodeBuffer & decodeBuffer,uint32_t & headerLen)214 bool MmsBodyPartHeader::DecodeWellKnownHeader(MmsDecodeBuffer &decodeBuffer, uint32_t &headerLen)
215 {
216 const uint8_t endStringZeroLen = 1;
217 uint8_t fieldCode = 0xff;
218 uint8_t oneByte = 0;
219 if (!decodeBuffer.GetOneByte(oneByte)) {
220 TELEPHONY_LOGE("Body part header decode get one byte fail.");
221 return false;
222 }
223 headerLen--;
224 uint32_t len = 0;
225 fieldCode = oneByte & 0x7f;
226 switch (static_cast<MmsHeaderParam>(fieldCode)) {
227 case MmsHeaderParam::P_CONTENT_LOCATION_V1: /* Content-Location */
228 case MmsHeaderParam::P_CONTENT_LOCATION_V2: { /* Content-Location */
229 if (!DecodeContentLocation(decodeBuffer, len)) {
230 TELEPHONY_LOGE("Body part header decode content location fail.");
231 return false;
232 }
233 break;
234 }
235 case MmsHeaderParam::P_CONTENT_ID: { /* Content-ID */
236 if (!DecodeContentId(decodeBuffer, len)) {
237 TELEPHONY_LOGE("Body part header decode contentId fail.");
238 return false;
239 }
240 break;
241 }
242 case MmsHeaderParam::P_CONTENT_DISPOSITION_V1: /* Content-Disposition */
243 case MmsHeaderParam::P_CONTENT_DISPOSITION_V2: {
244 if (!DecodeContentDisposition(decodeBuffer, len)) {
245 TELEPHONY_LOGE("Body part header decode content disposition fail.");
246 return false;
247 }
248 break;
249 }
250 default: {
251 std::string sTmp = "";
252 decodeBuffer.DecodeQuotedText(sTmp, len);
253 break;
254 }
255 }
256 if (headerLen > len + endStringZeroLen) {
257 headerLen -= len + endStringZeroLen;
258 } else {
259 headerLen = 0;
260 }
261 return true;
262 }
263
264 /**
265 * @brief DecodeApplicationHeader
266 * wap-230-wsp-20010705-a section:8.4.2.6 Header
267 * Application-header = Token-text Application-specific-value
268 * Application-specific-value = Text-string
269 * @param decodeBuffer
270 * @param headerLen
271 * @return true
272 * @return false
273 */
DecodeApplicationHeader(MmsDecodeBuffer & decodeBuffer,uint32_t & headerLen)274 bool MmsBodyPartHeader::DecodeApplicationHeader(MmsDecodeBuffer &decodeBuffer, uint32_t &headerLen)
275 {
276 const uint32_t endStringZeroLen = 2;
277 headerLen = 0;
278 std::string sField = "";
279 uint32_t fieldLen = 0;
280 if (!decodeBuffer.DecodeTokenText(sField, fieldLen)) {
281 TELEPHONY_LOGE("Body part header decode token text fail.");
282 return false;
283 }
284
285 std::string sValue = "";
286 uint32_t valueLen = 0;
287 if (!decodeBuffer.DecodeText(sValue, valueLen)) {
288 TELEPHONY_LOGE("Body part header decode text fail.");
289 return false;
290 }
291 if (sField == "Content-Transfer-Encoding") {
292 strContentTransferEncoding_ = sValue;
293 }
294 if (headerLen > fieldLen + valueLen + endStringZeroLen) {
295 headerLen -= fieldLen + valueLen + endStringZeroLen;
296 } else {
297 headerLen = 0;
298 }
299 return true;
300 }
301
GetContentId(std::string & contentId)302 bool MmsBodyPartHeader::GetContentId(std::string &contentId)
303 {
304 contentId.clear();
305 contentId = strContentID_;
306 return true;
307 }
308
SetContentId(std::string contentId)309 bool MmsBodyPartHeader::SetContentId(std::string contentId)
310 {
311 strContentID_ = contentId;
312 return true;
313 }
314
GetContentTransferEncoding(std::string & contentTransferEncoding)315 bool MmsBodyPartHeader::GetContentTransferEncoding(std::string &contentTransferEncoding)
316 {
317 contentTransferEncoding.clear();
318 contentTransferEncoding = strContentTransferEncoding_;
319 return true;
320 }
321
SetContentTransferEncoding(std::string contentTransferEncoding)322 bool MmsBodyPartHeader::SetContentTransferEncoding(std::string contentTransferEncoding)
323 {
324 strContentTransferEncoding_ = contentTransferEncoding;
325 return true;
326 }
327
GetContentLocation(std::string & contentLocation)328 bool MmsBodyPartHeader::GetContentLocation(std::string &contentLocation)
329 {
330 contentLocation.clear();
331 contentLocation.assign(strContentLocation_);
332 return true;
333 }
334
SetContentLocation(std::string contentLocation)335 bool MmsBodyPartHeader::SetContentLocation(std::string contentLocation)
336 {
337 strContentLocation_ = contentLocation;
338 return true;
339 }
340
GetContentDisposition(std::string & contentDisposition)341 bool MmsBodyPartHeader::GetContentDisposition(std::string &contentDisposition)
342 {
343 contentDisposition.clear();
344 contentDisposition.assign(strDisposition_);
345 return true;
346 }
347
SetContentDisposition(std::string contentDisposition)348 bool MmsBodyPartHeader::SetContentDisposition(std::string contentDisposition)
349 {
350 strDisposition_ = contentDisposition;
351 return true;
352 }
353
354 /**
355 * @brief EncodeContentLocation
356 * OMA-TS-MMS_ENC-V1_3-20110913-A section:7.3.10 X-Mms-Content-Location Field
357 * When used in a PDU other than M-Mbox-Delete.conf and M-Delete.conf:
358 * Content-location-value = Uri-value
359 * When used in the M-Mbox-Delete.conf and M-Delete.conf PDU:
360 * Content-location-Del-value = Value-length Status-count-value Content-location-value
361 * Status-count-value = Integer-value
362 * Uri-value = Text-string
363 * @param encodeBuffer
364 * @return true
365 * @return false
366 */
EncodeContentLocation(MmsEncodeBuffer & encodeBuffer)367 bool MmsBodyPartHeader::EncodeContentLocation(MmsEncodeBuffer &encodeBuffer)
368 {
369 const uint8_t setHighestBitOne = 0x80;
370 if (strContentLocation_.empty()) {
371 TELEPHONY_LOGI("Body part header encode content location is empty.");
372 return true;
373 }
374
375 if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsHeaderParam::P_CONTENT_LOCATION_V2) | setHighestBitOne)) {
376 TELEPHONY_LOGE("Body part header encode content location write byte fail.");
377 return false;
378 }
379 if (!encodeBuffer.EncodeText(strContentLocation_)) {
380 TELEPHONY_LOGE("Body part header encode content location fail.");
381 return false;
382 }
383 return true;
384 }
385
386 /**
387 * @brief EncodeContentId
388 * wap-230-wsp-20010705-a section:8.4.2.67 Content-ID field
389 * Content-ID-value = Quoted-string
390 * @param encodeBuffer
391 * @return true
392 * @return false
393 */
EncodeContentId(MmsEncodeBuffer & encodeBuffer)394 bool MmsBodyPartHeader::EncodeContentId(MmsEncodeBuffer &encodeBuffer)
395 {
396 const uint8_t setHighestBitOne = 0x80;
397 if (strContentID_.empty()) {
398 TELEPHONY_LOGI("Body part header encode content ID is empty.");
399 return true;
400 }
401
402 if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsHeaderParam::P_CONTENT_ID) | setHighestBitOne)) {
403 TELEPHONY_LOGE("Body part header encode content ID write byte fail.");
404 return false;
405 }
406 if (!encodeBuffer.EncodeQuotedText(strContentID_)) {
407 TELEPHONY_LOGE("Body part header encode content ID fail.");
408 return false;
409 }
410 return true;
411 }
412
413 /**
414 * @brief EncodeContentDisposition
415 * wap-230-wsp-20010705-a section:8.4.2.53 Content-disposition field
416 * Content-disposition-value = Value-length Disposition *(Parameter)
417 * Disposition = Form-data | Attachment | Inline | Token-text
418 * Form-data = <Octet 128>
419 * Attachment = <Octet 129>
420 * Inline = <Octet 130>
421 * @param encodeBuffer
422 * @return true
423 * @return false
424 */
EncodeContentDisposition(MmsEncodeBuffer & encodeBuffer)425 bool MmsBodyPartHeader::EncodeContentDisposition(MmsEncodeBuffer &encodeBuffer)
426 {
427 const uint8_t setHighestBitOne = 0x80;
428 std::vector<std::string> dispVec {DISPOSITION_FROM_DATA, DISPOSITION_ATTACHMENT, DISPOSITION_INLINE};
429 auto it = std::find(dispVec.begin(), dispVec.end(), strDisposition_);
430 if (it == dispVec.end()) {
431 return true;
432 }
433
434 TELEPHONY_LOGI("strDisposition = %{public}s", strDisposition_.c_str());
435 if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsHeaderParam::P_CONTENT_DISPOSITION_V1) | setHighestBitOne)) {
436 TELEPHONY_LOGE("Body part header encode content disposition write byte fail.");
437 return false;
438 }
439 if (!encodeBuffer.EncodeUintvar(0x01)) {
440 TELEPHONY_LOGE("EncodeContentDisposition EncodeUintvar Error.");
441 return false;
442 }
443 if (strDisposition_ == DISPOSITION_FROM_DATA) {
444 if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsDispositonParam::P_DISPOSITION_FROM_DATA))) {
445 TELEPHONY_LOGE("Body part header encode content disposition write byte fail.");
446 return false;
447 }
448 return true;
449 }
450 if (strDisposition_ == DISPOSITION_ATTACHMENT) {
451 if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsDispositonParam::P_DISPOSITION_ATTACHMENT))) {
452 TELEPHONY_LOGE("Body part header encode content disposition write byte fail.");
453 return false;
454 }
455 return true;
456 }
457 if (strDisposition_ == DISPOSITION_INLINE) {
458 if (!encodeBuffer.WriteByte(static_cast<uint8_t>(MmsDispositonParam::P_DISPOSITION_INLINE))) {
459 TELEPHONY_LOGE("Body part header encode content disposition write byte fail.");
460 return false;
461 }
462 return true;
463 }
464 return true;
465 }
466
467 /**
468 * @brief EncodeContentDisposition
469 * wap-230-wsp-20010705-a section:8.4.2.46 Transfer encoding field
470 * Transfer-encoding-values = Chunked | Token-text
471 * Chunked = <Octet 128>
472 * @param encodeBuffer
473 * @return true
474 * @return false
475 */
EncodeContentTransferEncoding(MmsEncodeBuffer & encodeBuffer)476 bool MmsBodyPartHeader::EncodeContentTransferEncoding(MmsEncodeBuffer &encodeBuffer)
477 {
478 if (strContentTransferEncoding_.empty()) {
479 TELEPHONY_LOGI("Body part header encode content transfer encoding is empty.");
480 return true;
481 }
482
483 if (!encodeBuffer.EncodeText("Content-Transfer-Encoding")) {
484 TELEPHONY_LOGE("Body part header encode content transfer encoding encode text fail.");
485 return false;
486 }
487 if (!encodeBuffer.EncodeText(strContentTransferEncoding_)) {
488 TELEPHONY_LOGE("Body part header encode content transfer encoding encode text fail.");
489 return false;
490 }
491 return true;
492 }
493
494 /**
495 * @brief EncodeMmsBodyPartHeader
496 * wap-230-wsp-20010705-a section:8.4.2.6 Header
497 * Well-known-header = Well-known-field-name Wap-value
498 * Well-known-field-name = Short-integer
499 * Wap-value = Accept-value | Accept-charset-value |...
500 * @param encodeBuffer
501 * @return true
502 * @return false
503 */
EncodeMmsBodyPartHeader(MmsEncodeBuffer & encodeBuffer)504 bool MmsBodyPartHeader::EncodeMmsBodyPartHeader(MmsEncodeBuffer &encodeBuffer)
505 {
506 if (!EncodeContentId(encodeBuffer)) {
507 TELEPHONY_LOGE("Body part header encode ContentId fail.");
508 return false;
509 }
510 if (!EncodeContentLocation(encodeBuffer)) {
511 TELEPHONY_LOGE("Body part header encode ContentLocation fail.");
512 return false;
513 }
514 if (!EncodeContentDisposition(encodeBuffer)) {
515 TELEPHONY_LOGE("Body part header encode ContentDisposition fail.");
516 return false;
517 }
518 if (!EncodeContentTransferEncoding(encodeBuffer)) {
519 TELEPHONY_LOGE("Body part header encode ContentTransferEncoding fail.");
520 return false;
521 }
522 return true;
523 }
524 } // namespace Telephony
525 } // namespace OHOS
526