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