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 #ifndef SMS_PDU_BUFFER_H 17 #define SMS_PDU_BUFFER_H 18 19 #include <vector> 20 21 namespace OHOS { 22 namespace Telephony { 23 24 enum BITS : uint8_t { BIT0 = 0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, BIT8 }; 25 26 class SmsPduBuffer { 27 public: 28 virtual ~SmsPduBuffer(); 29 virtual bool IsEmpty(); 30 uint16_t GetIndex(); 31 uint16_t GetSize(); 32 bool SetIndex(uint16_t index); 33 uint16_t MoveForward(uint16_t len = 1); 34 uint16_t MoveBack(uint16_t len = 1); 35 uint16_t SkipBits(); 36 std::unique_ptr<std::vector<uint8_t>> GetPduBuffer(); 37 38 public: 39 std::unique_ptr<uint8_t[]> data_ { nullptr }; 40 41 protected: 42 uint16_t index_ { 0 }; 43 uint16_t length_ { 0 }; 44 uint8_t bitIndex_ { 0 }; 45 }; 46 47 class SmsReadBuffer : public SmsPduBuffer { 48 public: 49 explicit SmsReadBuffer(const std::string &pdu); 50 bool ReadByte(uint8_t &v); 51 bool PickOneByte(uint8_t &v); 52 bool PickOneByteFromIndex(uint16_t index, uint8_t &v); 53 bool ReadWord(uint16_t &v); 54 bool ReadBits(uint8_t &v, uint8_t l = BIT1); 55 }; 56 57 class SmsWriteBuffer : public SmsPduBuffer { 58 public: 59 SmsWriteBuffer(); 60 bool WriteByte(uint8_t v); 61 bool GetTopValue(uint8_t &oneByte); 62 bool GetValueFromIndex(uint16_t index, uint8_t &v); 63 bool WriteWord(uint16_t v); 64 bool WriteBits(uint8_t v, uint8_t l = BIT1); 65 bool InsertByte(uint8_t v, uint16_t index); 66 }; 67 68 } // namespace Telephony 69 } // namespace OHOS 70 #endif 71