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