1 /* 2 * Copyright (C) 2023 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 16 #include "gsm_cb_pdu_decode_buffer.h" 17 18 #include "securec.h" 19 #include "telephony_log_wrapper.h" 20 21 namespace OHOS { 22 namespace Telephony { 23 using namespace std; 24 static constexpr uint16_t MAX_CB_MSG_LEN = 4200; 25 GsmCbPduDecodeBuffer(uint32_t len)26GsmCbPduDecodeBuffer::GsmCbPduDecodeBuffer(uint32_t len) 27 { 28 if (len == 0 && len > MAX_CB_MSG_LEN) { 29 TELEPHONY_LOGE("pdu data error."); 30 return; 31 } 32 pduBuffer_ = std::make_unique<char[]>(len); 33 if (pduBuffer_ == nullptr) { 34 TELEPHONY_LOGE("pduBuffer_ nullptr error"); 35 totolLength_ = 0; 36 return; 37 } 38 totolLength_ = len; 39 } 40 ~GsmCbPduDecodeBuffer()41GsmCbPduDecodeBuffer::~GsmCbPduDecodeBuffer() {} 42 PickOneByte(uint8_t & oneByte)43bool GsmCbPduDecodeBuffer::PickOneByte(uint8_t &oneByte) 44 { 45 if (pduBuffer_ == nullptr || curPosition_ >= totolLength_) { 46 TELEPHONY_LOGE("curPosition_ over size."); 47 return false; 48 } 49 oneByte = pduBuffer_[curPosition_]; 50 return true; 51 } 52 GetOneByte(uint8_t & oneByte)53bool GsmCbPduDecodeBuffer::GetOneByte(uint8_t &oneByte) 54 { 55 if (pduBuffer_ == nullptr || curPosition_ >= totolLength_) { 56 return false; 57 } 58 oneByte = pduBuffer_[curPosition_++]; 59 return true; 60 } 61 IncreasePointer(uint32_t offset)62void GsmCbPduDecodeBuffer::IncreasePointer(uint32_t offset) 63 { 64 curPosition_ += offset; 65 } 66 SetPointer(uint32_t offset)67void GsmCbPduDecodeBuffer::SetPointer(uint32_t offset) 68 { 69 curPosition_ = offset; 70 } 71 GetCurPosition()72uint32_t GsmCbPduDecodeBuffer::GetCurPosition() 73 { 74 return curPosition_; 75 } 76 GetSize()77uint32_t GsmCbPduDecodeBuffer::GetSize() 78 { 79 return totolLength_; 80 } 81 } // namespace Telephony 82 } // namespace OHOS 83