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