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 #include "mms_msg.h"
16
17 #include "mms_content_type.h"
18 #include "telephony_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Telephony {
DumpMms()22 void MmsMsg::DumpMms()
23 {
24 mmsHeader_.DumpMmsHeader();
25 mmsBody_.DumpMmsBody();
26 }
27
DecodeMsg(std::string mmsFilePathName)28 bool MmsMsg::DecodeMsg(std::string mmsFilePathName)
29 {
30 MmsDecodeBuffer decodeBuffer;
31 if (!decodeBuffer.WriteBufferFromFile(mmsFilePathName)) {
32 TELEPHONY_LOGE("Read File To Buffer Error.");
33 return false;
34 }
35 if (!mmsHeader_.DecodeMmsHeader(decodeBuffer)) {
36 TELEPHONY_LOGE("Decode Mms Header Error.");
37 return false;
38 }
39 if (!mmsBody_.DecodeMmsBody(decodeBuffer, mmsHeader_)) {
40 TELEPHONY_LOGE("Decode Mms Body Error.");
41 return false;
42 }
43 return true;
44 }
45
DecodeMsg(std::unique_ptr<char[]> inBuff,uint32_t inLen)46 bool MmsMsg::DecodeMsg(std::unique_ptr<char[]> inBuff, uint32_t inLen)
47 {
48 MmsDecodeBuffer decodeBuffer;
49 if (!decodeBuffer.WriteDataBuffer(std::move(inBuff), inLen)) {
50 TELEPHONY_LOGE("Write To Buffer Error.");
51 return false;
52 }
53 if (!mmsHeader_.DecodeMmsHeader(decodeBuffer)) {
54 TELEPHONY_LOGE("Decode Mms Header Error.");
55 return false;
56 }
57 if (!mmsBody_.DecodeMmsBody(decodeBuffer, mmsHeader_)) {
58 TELEPHONY_LOGE("Decode Mms Body Error.");
59 return false;
60 }
61 return true;
62 }
63
EncodeMsg(uint32_t & outLen)64 std::unique_ptr<char[]> MmsMsg::EncodeMsg(uint32_t &outLen)
65 {
66 MmsEncodeBuffer encodeBuffer;
67 if (!mmsHeader_.EncodeMmsHeader(encodeBuffer)) {
68 TELEPHONY_LOGE("Encode Mms Header Error.");
69 return nullptr;
70 }
71 if (mmsHeader_.IsHaveBody()) {
72 if (!mmsBody_.EncodeMmsHeaderContentType(mmsHeader_, encodeBuffer)) {
73 TELEPHONY_LOGE("Encode Mms Header ContentType Error.");
74 return nullptr;
75 }
76 if (!mmsBody_.EncodeMmsBody(encodeBuffer)) {
77 TELEPHONY_LOGE("Encode Mms Body Error.");
78 return nullptr;
79 }
80 }
81 outLen = encodeBuffer.GetCurPosition();
82 TELEPHONY_LOGI("NativeEncodeMms length:%{public}d", outLen);
83 return encodeBuffer.ReadDataBuffer(0, outLen);
84 }
85
GetMmsVersion()86 uint16_t MmsMsg::GetMmsVersion()
87 {
88 return GetHeaderLongValue(MMS_MMS_VERSION);
89 }
90
SetMmsVersion(uint16_t value)91 bool MmsMsg::SetMmsVersion(uint16_t value)
92 {
93 return SetHeaderLongValue(MMS_MMS_VERSION, value);
94 }
95
GetMmsMessageType()96 uint8_t MmsMsg::GetMmsMessageType()
97 {
98 return GetHeaderOctetValue(MMS_MESSAGE_TYPE);
99 }
100
SetMmsMessageType(uint8_t value)101 bool MmsMsg::SetMmsMessageType(uint8_t value)
102 {
103 return mmsHeader_.SetOctetValue(MMS_MESSAGE_TYPE, value);
104 }
105
GetMmsTransactionId()106 std::string MmsMsg::GetMmsTransactionId()
107 {
108 return GetHeaderStringValue(MMS_TRANSACTION_ID);
109 }
110
SetMmsTransactionId(std::string value)111 bool MmsMsg::SetMmsTransactionId(std::string value)
112 {
113 return mmsHeader_.SetTextValue(MMS_TRANSACTION_ID, value);
114 }
115
GetMmsDate()116 int64_t MmsMsg::GetMmsDate()
117 {
118 int64_t date = 0;
119 mmsHeader_.GetLongValue(MMS_DATE, date);
120 return date;
121 }
122
SetMmsDate(int64_t value)123 bool MmsMsg::SetMmsDate(int64_t value)
124 {
125 return mmsHeader_.SetLongValue(MMS_DATE, value);
126 }
127
GetMmsSubject()128 std::string MmsMsg::GetMmsSubject()
129 {
130 std::string value;
131 mmsHeader_.GetStringValue(MMS_SUBJECT, value);
132 return value;
133 }
134
SetMmsSubject(std::string value)135 bool MmsMsg::SetMmsSubject(std::string value)
136 {
137 return mmsHeader_.SetEncodeStringValue(MMS_SUBJECT, 0x6A, value);
138 }
139
GetMmsFrom()140 MmsAddress MmsMsg::GetMmsFrom()
141 {
142 std::vector<MmsAddress> address;
143 GetHeaderAllAddressValue(MMS_FROM, address);
144 MmsAddress fromAddress;
145 if (address.size() == 1) {
146 fromAddress = address[0];
147 }
148 return fromAddress;
149 }
150
SetMmsFrom(MmsAddress address)151 bool MmsMsg::SetMmsFrom(MmsAddress address)
152 {
153 return AddHeaderAddressValue(MMS_FROM, address);
154 }
155
SetMmsTo(std::vector<MmsAddress> & toAddrs)156 bool MmsMsg::SetMmsTo(std::vector<MmsAddress> &toAddrs)
157 {
158 bool ret = false;
159 for (auto it : toAddrs) {
160 ret = AddHeaderAddressValue(MMS_TO, it);
161 if (!ret) {
162 return ret;
163 }
164 }
165 return ret;
166 }
167
GetMmsTo(std::vector<MmsAddress> & toAddrs)168 bool MmsMsg::GetMmsTo(std::vector<MmsAddress> &toAddrs)
169 {
170 return GetHeaderAllAddressValue(MMS_TO, toAddrs);
171 }
172
GetHeaderOctetValue(uint8_t fieldId)173 uint8_t MmsMsg::GetHeaderOctetValue(uint8_t fieldId)
174 {
175 uint8_t value = 0;
176 mmsHeader_.GetOctetValue(fieldId, value);
177 return value;
178 }
179
SetHeaderOctetValue(uint8_t fieldId,uint8_t value)180 bool MmsMsg::SetHeaderOctetValue(uint8_t fieldId, uint8_t value)
181 {
182 return mmsHeader_.SetOctetValue(fieldId, value);
183 }
184
GetHeaderIntegerValue(uint8_t fieldId)185 int32_t MmsMsg::GetHeaderIntegerValue(uint8_t fieldId)
186 {
187 int64_t value = 0;
188 mmsHeader_.GetLongValue(fieldId, value);
189 return static_cast<int32_t>(value);
190 }
191
SetHeaderIntegerValue(uint8_t fieldId,int32_t value)192 bool MmsMsg::SetHeaderIntegerValue(uint8_t fieldId, int32_t value)
193 {
194 return mmsHeader_.SetLongValue(fieldId, static_cast<int64_t>(value));
195 }
196
GetHeaderLongValue(uint8_t fieldId)197 int64_t MmsMsg::GetHeaderLongValue(uint8_t fieldId)
198 {
199 int64_t value = 0;
200 mmsHeader_.GetLongValue(fieldId, value);
201 return value;
202 }
203
SetHeaderLongValue(uint8_t fieldId,int64_t value)204 bool MmsMsg::SetHeaderLongValue(uint8_t fieldId, int64_t value)
205 {
206 return mmsHeader_.SetLongValue(fieldId, value);
207 }
208
GetHeaderStringValue(uint8_t fieldId)209 std::string MmsMsg::GetHeaderStringValue(uint8_t fieldId)
210 {
211 std::string value;
212 if (mmsHeader_.GetTextValue(fieldId, value)) {
213 return value;
214 }
215
216 value.clear();
217 MmsEncodeString eValue;
218 if (!mmsHeader_.GetEncodeStringValue(fieldId, eValue)) {
219 return value;
220 }
221 eValue.GetEncodeString(value);
222 return value;
223 }
224
225 // this fun must combit tow type string
SetHeaderStringValue(uint8_t fieldId,std::string & value)226 bool MmsMsg::SetHeaderStringValue(uint8_t fieldId, std::string &value)
227 {
228 return mmsHeader_.SetTextValue(fieldId, value);
229 }
230
SetHeaderEncodedStringValue(uint8_t fieldId,const std::string & value,uint32_t charset)231 bool MmsMsg::SetHeaderEncodedStringValue(uint8_t fieldId, const std::string &value, uint32_t charset)
232 {
233 return mmsHeader_.SetEncodeStringValue(fieldId, charset, value);
234 }
235
AddHeaderAddressValue(uint8_t fieldId,MmsAddress & address)236 bool MmsMsg::AddHeaderAddressValue(uint8_t fieldId, MmsAddress &address)
237 {
238 return mmsHeader_.AddHeaderAddressValue(fieldId, address);
239 }
240
GetHeaderAllAddressValue(uint8_t fieldId,std::vector<MmsAddress> & addressValue)241 bool MmsMsg::GetHeaderAllAddressValue(uint8_t fieldId, std::vector<MmsAddress> &addressValue)
242 {
243 return mmsHeader_.GetHeaderAllAddressValue(fieldId, addressValue);
244 }
245
SetHeaderContentType(std::string strType)246 bool MmsMsg::SetHeaderContentType(std::string strType)
247 {
248 return mmsHeader_.GetHeaderContentType().SetContentType(strType);
249 }
250
GetHeaderContentType()251 std::string MmsMsg::GetHeaderContentType()
252 {
253 std::string contentType = "";
254 mmsHeader_.GetHeaderContentType().GetContentType(contentType);
255 return contentType;
256 }
257
GetHeaderContentTypeStart()258 std::string MmsMsg::GetHeaderContentTypeStart()
259 {
260 std::string start = "";
261 uint8_t fieldCode = static_cast<uint8_t>(ContentParam::CT_P_START);
262 mmsHeader_.GetHeaderContentType().GetContentParam().GetNormalField(fieldCode, start);
263 if (start.empty()) {
264 fieldCode = static_cast<uint8_t>(ContentParam::CT_P_START_VALUE);
265 mmsHeader_.GetHeaderContentType().GetContentParam().GetNormalField(fieldCode, start);
266 return start;
267 }
268 return start;
269 }
270
AddAttachment(MmsAttachment & attachment)271 bool MmsMsg::AddAttachment(MmsAttachment &attachment)
272 {
273 MmsBodyPart bodyPart;
274 if (!bodyPart.SetAttachment(attachment)) {
275 TELEPHONY_LOGE("BodyPart Set AttachmentPath Error!");
276 return false;
277 }
278 /** Check Body Part for duplication **/
279 if (mmsBody_.IsBodyPartExist(bodyPart)) {
280 TELEPHONY_LOGE("MmsBody BodyPart Has Exist Error!");
281 return false;
282 }
283 /** Add Body Part To Mms Body **/
284 if (!mmsBody_.AddMmsBodyPart(bodyPart)) {
285 TELEPHONY_LOGE("MmsBody Add BodyPart error!");
286 return false;
287 }
288 /** Check Body Part is Smil Part **/
289 if (bodyPart.IsSmilFile()) {
290 std::string contentId;
291 std::string contentType;
292 bodyPart.GetContentId(contentId);
293 bodyPart.GetContentType(contentType);
294
295 mmsHeader_.GetHeaderContentType().GetContentParam().SetStart(contentId);
296 mmsHeader_.GetHeaderContentType().GetContentParam().SetType(contentType);
297 }
298 return true;
299 }
300
GetAllAttachment(std::vector<MmsAttachment> & attachments)301 bool MmsMsg::GetAllAttachment(std::vector<MmsAttachment> &attachments)
302 {
303 attachments.clear();
304 if (mmsBody_.GetBodyPartCount() <= 0) {
305 TELEPHONY_LOGE("MmsBody Not Has BodyPart Error.");
306 return false;
307 }
308 std::string smilFileName;
309 std::vector<MmsBodyPart> bodyParts;
310 mmsHeader_.GetSmilFileName(smilFileName);
311 mmsBody_.GetMmsBodyPart(bodyParts);
312
313 for (auto part : bodyParts) {
314 MmsAttachment attachment;
315 std::string contentId = "";
316 part.GetPartHeader().GetContentId(contentId);
317 if (!contentId.empty()) {
318 attachment.SetContentId(contentId);
319 bool isSmil = (smilFileName == contentId) ? true : false;
320 attachment.SetIsSmilFile(isSmil);
321 }
322 std::string fileName = "";
323 fileName = part.GetPartFileName();
324 if (fileName.empty()) {
325 TELEPHONY_LOGE("Body Part File Name Empty Error.");
326 return false;
327 }
328 attachment.SetFileName(fileName);
329
330 std::string contentType = "";
331 part.GetContentType().GetContentType(contentType);
332 attachment.SetContentType(contentType);
333 attachment.SetCharSet(part.GetContentType().GetContentParam().GetCharSet());
334
335 std::string contentLocation = "";
336 part.GetPartHeader().GetContentLocation(contentLocation);
337 attachment.SetContentLocation(contentLocation);
338
339 std::string contentDispositon = "";
340 part.GetPartHeader().GetContentDisposition(contentDispositon);
341 attachment.SetContentDisposition(contentDispositon);
342
343 std::string strContentTransferEncoding = "";
344 part.GetPartHeader().GetContentTransferEncoding(strContentTransferEncoding);
345 attachment.SetContentTransferEncoding(strContentTransferEncoding);
346
347 uint32_t partBodyLen = 0;
348 std::unique_ptr<char[]> partBodyBuffer = part.ReadBodyPartBuffer(partBodyLen);
349 if (partBodyBuffer == nullptr) {
350 TELEPHONY_LOGE("partBodyBuffer nullptr Error.");
351 return false;
352 }
353 attachment.SetDataBuffer(std::move(partBodyBuffer), partBodyLen);
354 attachments.push_back(attachment);
355 }
356 return true;
357 }
358
GetHeader()359 MmsHeader& MmsMsg::GetHeader()
360 {
361 return mmsHeader_;
362 }
363 } // namespace Telephony
364 } // namespace OHOS
365