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_buffer.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 {
23 static constexpr const char *APP_SAND_ABSOLUTE_DIR = "/data/app";
24 static constexpr const char *APP_SAND_RELATIVE_DIR = "/data/storage";
25 
MmsBuffer()26 MmsBuffer::MmsBuffer()
27 {
28     pduBuffer_ = std::make_unique<char[]>(MMS_PDU_MAX_SIZE);
29     if (pduBuffer_ == nullptr) {
30         TELEPHONY_LOGE("MmsBuffer make_unique create pduBuffer error");
31         totolLength_ = 0;
32         return;
33     }
34     totolLength_ = MMS_PDU_MAX_SIZE;
35 }
36 
~MmsBuffer()37 MmsBuffer::~MmsBuffer()
38 {
39     if (pduBuffer_ != nullptr) {
40         pduBuffer_.reset();
41     }
42 }
43 
ReadDataBuffer(uint32_t desLen)44 std::unique_ptr<char[]> MmsBuffer::ReadDataBuffer(uint32_t desLen)
45 {
46     return ReadDataBuffer(curPosition_, desLen);
47 }
48 
ReadDataBuffer(uint32_t offset,uint32_t desLen)49 std::unique_ptr<char[]> MmsBuffer::ReadDataBuffer(uint32_t offset, uint32_t desLen)
50 {
51     if (desLen > MMS_PDU_MAX_SIZE) {
52         TELEPHONY_LOGE("desLen over size error");
53         return nullptr;
54     }
55     std::unique_ptr<char[]> result = std::make_unique<char[]>(desLen);
56     if (result == nullptr) {
57         TELEPHONY_LOGE("make unique result nullptr Error .");
58         return nullptr;
59     }
60 
61     if (offset + desLen > totolLength_) {
62         TELEPHONY_LOGE("read over buffer error .");
63         return nullptr;
64     }
65 
66     if (memcpy_s(result.get(), desLen, &pduBuffer_[offset], desLen) != EOK) {
67         TELEPHONY_LOGE("read memcpy_s error .");
68         return nullptr;
69     }
70     return result;
71 }
72 
WriteDataBuffer(std::unique_ptr<char[]> inBuff,uint32_t len)73 bool MmsBuffer::WriteDataBuffer(std::unique_ptr<char[]> inBuff, uint32_t len)
74 {
75     if (inBuff == nullptr) {
76         TELEPHONY_LOGE("InBuffer nullptr Error .");
77         return false;
78     }
79     if (len <= 0 || len > MMS_PDU_MAX_SIZE) {
80         TELEPHONY_LOGE("Data Len Over Error .");
81         return false;
82     }
83 
84     if (pduBuffer_) {
85         pduBuffer_.reset();
86     }
87 
88     pduBuffer_ = std::make_unique<char[]>(len);
89     if (!pduBuffer_) {
90         TELEPHONY_LOGE("make unique pduBuffer nullptr Error .");
91         return false;
92     }
93     if (memcpy_s(pduBuffer_.get(), len, inBuff.get(), len) != EOK) {
94         TELEPHONY_LOGE("MemCpy_s Error .");
95         return false;
96     }
97     totolLength_ = len;
98     return true;
99 }
100 
WriteBufferFromFile(std::string & strPathName)101 bool MmsBuffer::WriteBufferFromFile(std::string &strPathName)
102 {
103     FILE *pFile = nullptr;
104     char realPath[PATH_MAX] = {0};
105     if (strPathName.empty() || realpath(strPathName.c_str(), realPath) == NULL) {
106         TELEPHONY_LOGE("path or realPath is NULL");
107         return false;
108     }
109 
110     std::string filePath = realPath;
111     std::string absDir = APP_SAND_ABSOLUTE_DIR;
112     std::string relDir = APP_SAND_RELATIVE_DIR;
113     if ((absDir.compare(filePath.substr(0, absDir.size())) != 0) &&
114         (relDir.compare(filePath.substr(0, relDir.size())) != 0)) {
115         TELEPHONY_LOGE("filePath no app sand box.");
116         return false;
117     }
118 
119     pFile = fopen(realPath, "rb");
120     if (pFile == nullptr) {
121         TELEPHONY_LOGE("Open File Error");
122         return false;
123     }
124     (void)fseek(pFile, 0, SEEK_END);
125     long fileLen = ftell(pFile);
126     if (fileLen <= 0 || fileLen > static_cast<long>(MMS_PDU_MAX_SIZE)) {
127         (void)fclose(pFile);
128         TELEPHONY_LOGE("Mms Over Long Error .");
129         return false;
130     }
131     if (pduBuffer_) {
132         pduBuffer_.reset();
133     }
134 
135     pduBuffer_ = std::make_unique<char[]>(fileLen);
136     if (!pduBuffer_) {
137         (void)fclose(pFile);
138         TELEPHONY_LOGE("make unique pduBuffer nullptr Error .");
139         return false;
140     }
141     (void)fseek(pFile, 0, SEEK_SET);
142     totolLength_ = fread(pduBuffer_.get(), 1, fileLen, pFile);
143     (void)fclose(pFile);
144     return true;
145 }
146 
GetCurPosition() const147 uint32_t MmsBuffer::GetCurPosition() const
148 {
149     return curPosition_;
150 }
151 
GetSize() const152 uint32_t MmsBuffer::GetSize() const
153 {
154     return totolLength_;
155 }
156 } // namespace Telephony
157 } // namespace OHOS
158