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 #ifndef HAP_BYTEBUFFER_H 16 #define HAP_BYTEBUFFER_H 17 18 #include <memory> 19 #include <string> 20 21 #include "common/export_define.h" 22 23 namespace OHOS { 24 namespace Security { 25 namespace Verify { 26 enum 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 class HapByteBuffer { 36 public: 37 DLL_EXPORT HapByteBuffer(); 38 DLL_EXPORT explicit HapByteBuffer(int32_t bufferCapacity); 39 DLL_EXPORT HapByteBuffer(const HapByteBuffer& other); 40 DLL_EXPORT ~HapByteBuffer(); 41 DLL_EXPORT HapByteBuffer& operator=(const HapByteBuffer& other); 42 DLL_EXPORT bool GetInt64(long long& value); 43 DLL_EXPORT bool GetInt64(int32_t index, long long& value); 44 DLL_EXPORT bool GetUInt32(uint32_t& value); 45 DLL_EXPORT bool GetUInt32(int32_t index, uint32_t& value); 46 DLL_EXPORT bool GetInt32(int32_t& value); 47 DLL_EXPORT bool GetInt32(int32_t index, int32_t& value); 48 DLL_EXPORT bool GetUInt16(int32_t index, uint16_t& value); 49 DLL_EXPORT void PutInt32(int32_t offset, int32_t value); 50 DLL_EXPORT void PutByte(int32_t offset, char value); 51 DLL_EXPORT void PutData(int32_t offset, const char data[], int32_t len); 52 DLL_EXPORT int32_t GetCapacity() const; 53 DLL_EXPORT int32_t GetPosition() const; 54 DLL_EXPORT int32_t GetLimit() const; 55 DLL_EXPORT const char* GetBufferPtr() const; 56 DLL_EXPORT void SetPosition(int32_t pos); 57 DLL_EXPORT void SetLimit(int32_t lim); 58 DLL_EXPORT void SetCapacity(int32_t cap); 59 DLL_EXPORT void Slice(); 60 DLL_EXPORT int32_t Remaining() const; 61 DLL_EXPORT bool HasRemaining() const; 62 DLL_EXPORT void Clear(); 63 DLL_EXPORT bool IsEqual(const HapByteBuffer& other); 64 DLL_EXPORT bool IsEqual(const std::string& other); 65 66 private: 67 void Init(int32_t bufferCapacity); 68 bool CheckInputForGettingData(int32_t index, int32_t dataLen); 69 70 private: 71 static const int32_t MAX_PRINT_LENGTH; 72 static const int32_t HEX_PRINT_LENGTH; 73 std::unique_ptr<char[]> buffer; 74 int32_t position = 0; 75 int32_t limit = 0; 76 int32_t capacity = 0; 77 }; 78 } // namespace Verify 79 } // namespace Security 80 } // namespace OHOS 81 #endif // HAP_BYTEBUFFER_H 82