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_attachment.h"
16
17 #include "securec.h"
18 #include "telephony_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Telephony {
~MmsAttachment()22 MmsAttachment::~MmsAttachment()
23 {
24 if (pAttachmentBuffer_ != nullptr) {
25 pAttachmentBuffer_.reset();
26 dataLength_ = 0;
27 }
28 }
29
SetAttachmentFilePath(std::string strPath,bool isSmil)30 bool MmsAttachment::SetAttachmentFilePath(std::string strPath, bool isSmil)
31 {
32 if (strPath.empty()) {
33 TELEPHONY_LOGE("Attachment file path is empty!");
34 return false;
35 }
36 strPathName_ = strPath;
37 isSmilFile_ = isSmil;
38 return true;
39 }
40
MmsAttachment(const MmsAttachment & srcAttachment)41 MmsAttachment::MmsAttachment(const MmsAttachment &srcAttachment)
42 {
43 if (srcAttachment.dataLength_ > MAX_MMS_ATTACHMENT_LEN) {
44 TELEPHONY_LOGE("srcAttachment.dataLength_ over size error");
45 return;
46 }
47 pAttachmentBuffer_ = std::make_unique<char[]>(srcAttachment.dataLength_);
48 if (pAttachmentBuffer_ == nullptr) {
49 TELEPHONY_LOGE("make unique attachment buffer nullptr error.");
50 return;
51 }
52 if (memcpy_s(pAttachmentBuffer_.get(), srcAttachment.dataLength_, srcAttachment.pAttachmentBuffer_.get(),
53 srcAttachment.dataLength_) != EOK) {
54 TELEPHONY_LOGE("memcpy_s attachment buffer error.");
55 return;
56 }
57
58 dataLength_ = srcAttachment.dataLength_;
59 contentId_ = srcAttachment.contentId_;
60 contentLocation_ = srcAttachment.contentLocation_;
61 contentDispositon_ = srcAttachment.contentDispositon_;
62 contentType_ = srcAttachment.contentType_;
63 contenTransferEncoding_ = srcAttachment.contenTransferEncoding_;
64 strFileName_ = srcAttachment.strFileName_;
65 isSmilFile_ = srcAttachment.isSmilFile_;
66 charset_ = srcAttachment.charset_;
67 }
68
GetAttachmentFilePath()69 std::string MmsAttachment::GetAttachmentFilePath()
70 {
71 return strPathName_;
72 }
73
SetContentId(std::string contentId)74 bool MmsAttachment::SetContentId(std::string contentId)
75 {
76 if (contentId.empty()) {
77 TELEPHONY_LOGE("Attachment contentId is empty!");
78 return false;
79 }
80 if (contentId.length() > 1 && contentId.at(0) == '<' && contentId.at(contentId.length() - 1)) {
81 contentId_.assign(contentId);
82 return true;
83 }
84 contentId = '<' + contentId + '>';
85 contentId_.assign(contentId);
86 return true;
87 }
88
GetContentId()89 std::string MmsAttachment::GetContentId()
90 {
91 return contentId_;
92 }
93
SetContentLocation(std::string contentLocation)94 bool MmsAttachment::SetContentLocation(std::string contentLocation)
95 {
96 if (contentLocation.empty()) {
97 TELEPHONY_LOGE("Attachment ContentLocation is empty!");
98 return false;
99 }
100 contentLocation_.assign(contentLocation);
101 return true;
102 }
103
GetContentLocation()104 std::string MmsAttachment::GetContentLocation()
105 {
106 return contentLocation_;
107 }
108
SetContentDisposition(std::string contentDisposition)109 bool MmsAttachment::SetContentDisposition(std::string contentDisposition)
110 {
111 if (contentDisposition.empty()) {
112 TELEPHONY_LOGE("Attachment contentDisposition is empty!");
113 return false;
114 }
115 contentDispositon_.assign(contentDisposition);
116 return true;
117 }
118
GetContentDisposition()119 std::string MmsAttachment::GetContentDisposition()
120 {
121 return contentDispositon_;
122 }
123
SetContentTransferEncoding(std::string contentTransferEncoding)124 bool MmsAttachment::SetContentTransferEncoding(std::string contentTransferEncoding)
125 {
126 if (contentTransferEncoding.empty()) {
127 TELEPHONY_LOGE("Attachment ContentTransferEncoding is empty!");
128 return false;
129 }
130 contenTransferEncoding_.assign(contentTransferEncoding);
131 return true;
132 }
133
GetContentTransferEncoding()134 std::string MmsAttachment::GetContentTransferEncoding()
135 {
136 return contenTransferEncoding_;
137 }
138
SetContentType(std::string strContentType)139 bool MmsAttachment::SetContentType(std::string strContentType)
140 {
141 if (strContentType.empty()) {
142 TELEPHONY_LOGE("Attachment ContentType is empty!");
143 return false;
144 }
145 contentType_.assign(strContentType);
146 return true;
147 }
148
GetContentType()149 std::string MmsAttachment::GetContentType()
150 {
151 return contentType_;
152 }
153
SetCharSet(uint32_t charset)154 void MmsAttachment::SetCharSet(uint32_t charset)
155 {
156 charset_ = charset;
157 }
158
GetCharSet()159 uint32_t MmsAttachment::GetCharSet()
160 {
161 return charset_;
162 }
163
SetFileName(std::string strFileName)164 bool MmsAttachment::SetFileName(std::string strFileName)
165 {
166 if (strFileName.empty()) {
167 TELEPHONY_LOGE("Attachment file name is empty!");
168 return false;
169 }
170 strFileName_.assign(strFileName);
171 return true;
172 }
173
GetFileName()174 std::string MmsAttachment::GetFileName()
175 {
176 if (strFileName_.length() > 0) {
177 return strFileName_;
178 }
179 return "";
180 }
181
IsSmilFile()182 bool MmsAttachment::IsSmilFile()
183 {
184 return isSmilFile_;
185 }
186
SetIsSmilFile(bool isSmilFile)187 void MmsAttachment::SetIsSmilFile(bool isSmilFile)
188 {
189 isSmilFile_ = isSmilFile;
190 }
191
GetDataBuffer(uint32_t & len)192 std::unique_ptr<char[]> MmsAttachment::GetDataBuffer(uint32_t &len)
193 {
194 if (dataLength_ > MAX_MMS_ATTACHMENT_LEN) {
195 TELEPHONY_LOGE("dataLength_ over size error");
196 return nullptr;
197 }
198 len = dataLength_;
199 std::unique_ptr<char[]> result = std::make_unique<char[]>(len);
200 if (result == nullptr) {
201 TELEPHONY_LOGE("make unique buffer result nullptr error.");
202 return nullptr;
203 }
204 if (memcpy_s(result.get(), len, pAttachmentBuffer_.get(), len) != EOK) {
205 TELEPHONY_LOGE("memcpy_s from attachment buffer error.");
206 return nullptr;
207 }
208 return result;
209 }
210
SetDataBuffer(std::unique_ptr<char[]> inBuff,uint32_t len)211 bool MmsAttachment::SetDataBuffer(std::unique_ptr<char[]> inBuff, uint32_t len)
212 {
213 if (inBuff == nullptr) {
214 TELEPHONY_LOGE("input buffer pointer nullptr error.");
215 return false;
216 }
217
218 if (len > MAX_MMS_ATTACHMENT_LEN) {
219 TELEPHONY_LOGE("input buffer over max lenght error.");
220 return false;
221 }
222 pAttachmentBuffer_ = std::make_unique<char[]>(len);
223 if (pAttachmentBuffer_ == nullptr) {
224 TELEPHONY_LOGE("make unique attachment buffer nullptr error.");
225 return false;
226 }
227 if (memcpy_s(pAttachmentBuffer_.get(), len, inBuff.get(), len) != EOK) {
228 TELEPHONY_LOGE("memcpy_s to attachment buffer error.");
229 return false;
230 }
231 dataLength_ = len;
232 return true;
233 }
234 } // namespace Telephony
235 } // namespace OHOS
236