1/* 2 * Copyright (c) 2024-2024 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#ifndef SIGNATRUETOOLS_BYTEBUFFER_H 16#define SIGNATRUETOOLS_BYTEBUFFER_H 17 18#include <memory> 19#include <string> 20 21#include "export_define.h" 22 23namespace OHOS { 24namespace SignatureTools { 25 26enum ReadFileErrorCode { 27 DEST_BUFFER_IS_NULL = -1, 28 FILE_IS_CLOSE = -2, 29 MMAP_COPY_FAILED = -3, 30 READ_OFFSET_OUT_OF_RANGE = -4, 31 MMAP_FAILED = -5, 32 MMAP_PARAM_INVALID = -6, 33}; 34/* 35* The underlying storage structure of ByteBuffer is an array, and all operations are based on that array. 36* ByteBuffer is in its initial state and also in a write state after creation 37* 38* 1. Several position related member variables in the ByteBuffer class 39* Mark: Read the starting position of the data to facilitate subsequent rollback to that position 40* Position: Where to start reading or writing 41* Limit: When reading the status, it indicates how much data is actually stored; 42* Equal to capacity when writing status 43* Capacity: represents the capacity of ByteBuffer, which is the maximum number of bytes that can be stored 44* 45* Bool hasRemaining(); Is there any data between position and limit 46* Uint32_t remaining() const; Return the number of bytes between position and limit 47* 48* 2. Set ByteBuffer from writer state to read state: limit is set to position value, position is set to zero 49* Void flip(); 50* 51* 3. If you want to write or overwrite again, you can call clear() or compact() 52* Void clear()// Reset to initial state: mark=-1, position=0, limit=capacity 53* Void compact()// Copy the data between position and limit to the starting position at index 0, 54* then move the position to the next position of this data segment and set the limit value to capacity 55*/ 56class ByteBuffer { 57public: 58 DLL_EXPORT ByteBuffer(); 59 DLL_EXPORT explicit ByteBuffer(int32_t bufferCapacity); 60 DLL_EXPORT ByteBuffer(const char* arr, int32_t length); 61 DLL_EXPORT ByteBuffer(const ByteBuffer& other); 62 DLL_EXPORT ~ByteBuffer(); 63 DLL_EXPORT ByteBuffer& operator=(const ByteBuffer& other); 64 DLL_EXPORT bool GetInt64(int64_t& value); 65 DLL_EXPORT bool GetInt64(int32_t index, int64_t& value); 66 DLL_EXPORT bool GetUInt32(uint32_t& value); 67 DLL_EXPORT bool GetUInt32(int32_t index, uint32_t& value); 68 DLL_EXPORT bool GetInt32(int32_t& value); 69 DLL_EXPORT bool GetInt32(int32_t index, int32_t& value); 70 DLL_EXPORT bool GetUInt16(uint16_t& value); 71 DLL_EXPORT bool GetUInt16(int32_t index, uint16_t& value); 72 DLL_EXPORT bool GetInt16(int16_t& value); 73 DLL_EXPORT bool GetInt16(int32_t index, int16_t& value); 74 DLL_EXPORT bool GetUInt8(uint8_t& value); 75 DLL_EXPORT bool GetUInt8(int32_t index, uint8_t& value); 76 DLL_EXPORT bool GetInt8(int8_t& value); 77 DLL_EXPORT bool GetInt8(int32_t index, int8_t& value); 78 DLL_EXPORT void PutUInt8(uint8_t value); 79 DLL_EXPORT void PutUInt16(uint16_t value); 80 DLL_EXPORT void PutUInt32(uint32_t value); 81 DLL_EXPORT void PutInt16(int16_t value); 82 DLL_EXPORT void PutInt16(int32_t offset, int16_t value); 83 DLL_EXPORT void PutInt32(int32_t value); 84 DLL_EXPORT void PutInt32(int32_t offset, int32_t value); 85 DLL_EXPORT void PutInt64(int64_t value); 86 DLL_EXPORT void PutByte(int32_t offset, char value); 87 DLL_EXPORT void PutData(const char data[], int32_t len); 88 DLL_EXPORT void PutData(int8_t data[], int32_t len); 89 DLL_EXPORT void PutData(int32_t offset, const char data[], int32_t len); 90 DLL_EXPORT void PutData(int32_t offset, const int8_t data[], int32_t len); 91 DLL_EXPORT void PutData(int32_t offset, const char data[], int32_t len, int32_t type); 92 DLL_EXPORT void PutByte(char value); 93 DLL_EXPORT void Put(const ByteBuffer& byteBuffer); 94 DLL_EXPORT void GetData(char data[], uint32_t len); 95 DLL_EXPORT void GetData(int32_t offset, int8_t data[], uint32_t len); 96 DLL_EXPORT void GetByte(int8_t data[], int32_t len); 97 DLL_EXPORT void ClearData(); 98 DLL_EXPORT int32_t GetCapacity() const; 99 DLL_EXPORT int32_t GetPosition() const; 100 DLL_EXPORT int32_t GetLimit() const; 101 DLL_EXPORT const char* GetBufferPtr() const; 102 DLL_EXPORT void SetPosition(int32_t pos); 103 DLL_EXPORT void SetLimit(int32_t lim); 104 DLL_EXPORT void SetCapacity(int32_t cap); 105 DLL_EXPORT ByteBuffer& Slice(); 106 DLL_EXPORT ByteBuffer& slice_for_codesigning(); 107 DLL_EXPORT ByteBuffer* Duplicate(); 108 DLL_EXPORT int32_t Remaining() const; 109 DLL_EXPORT bool HasRemaining() const; 110 DLL_EXPORT void Clear(); 111 DLL_EXPORT ByteBuffer& Flip(); 112 // to the beginning of the cache area; Switch to write state 113 DLL_EXPORT bool IsEqual(const ByteBuffer& other); 114 DLL_EXPORT bool IsEqual(const std::string& other); 115 DLL_EXPORT void Rewind(); 116 DLL_EXPORT ByteBuffer& RewindHap(); 117 DLL_EXPORT std::string ToString(); 118 119private: 120 void Init(int32_t bufferCapacity); 121 bool CheckInputForGettingData(int32_t index, int32_t dataLen); 122 123private: 124 static const int32_t MAX_PRINT_LENGTH; 125 static const int32_t HEX_PRINT_LENGTH; 126 std::shared_ptr<char> buffer; 127 int32_t position = 0; 128 int32_t limit = 0; 129 int32_t capacity = 0; 130}; 131} // namespace SignatureTools 132} // namespace OHOS 133#endif // SIGNATRUETOOLS_BYTEBUFFER_H 134