• 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_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     pFile = fopen(strPathName.c_str(), "rb");
97     if (pFile == nullptr) {
98         TELEPHONY_LOGE("Open File Error :%{public}s", strPathName.c_str());
99         return false;
100     }
101     (void)fseek(pFile, 0, SEEK_END);
102     long fileLen = ftell(pFile);
103     if (fileLen <= 0 || fileLen > (long)CODE_BUFFER_MAX_SIZE) {
104         (void)fclose(pFile);
105         TELEPHONY_LOGE("Mms Over Long Error .");
106         return false;
107     }
108     if (pduBuffer_) {
109         pduBuffer_.reset();
110     }
111 
112     pduBuffer_ = std::make_unique<char[]>(fileLen);
113     if (!pduBuffer_) {
114         (void)fclose(pFile);
115         TELEPHONY_LOGE("make unique pduBuffer nullptr Error .");
116         return false;
117     }
118     (void)fseek(pFile, 0, SEEK_SET);
119     totolLength_ = fread(pduBuffer_.get(), 1, CODE_BUFFER_MAX_SIZE, pFile);
120     (void)fclose(pFile);
121     return true;
122 }
123 
GetCurPosition() const124 uint32_t MmsBuffer::GetCurPosition() const
125 {
126     return curPosition_;
127 }
128 
GetSize() const129 uint32_t MmsBuffer::GetSize() const
130 {
131     return totolLength_;
132 }
133 } // namespace Telephony
134 } // namespace OHOS
135