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