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