1 /* 2 * Copyright (c) 2022 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 BUFFER_JS_BUFFER_H 17 #define BUFFER_JS_BUFFER_H 18 19 #include <numeric> 20 #include <string> 21 #include <vector> 22 23 #include "converter.h" 24 #include "napi/native_api.h" 25 #include "napi/native_node_api.h" 26 #include "tools/log.h" 27 28 namespace OHOS::buffer { 29 class Buffer { 30 public: 31 Buffer() = default; 32 virtual ~Buffer(); 33 void Init(uint32_t size); 34 void Init(Buffer *buffer); 35 void Init(Buffer *pool, unsigned int poolOffset, unsigned int length); 36 void Init(uint8_t *buffer, unsigned int byteOffset, unsigned int length); 37 38 unsigned int GetLength(); 39 void SetLength(unsigned int len); 40 unsigned int GetByteOffset(); 41 int32_t Get(uint32_t index); 42 void Set(uint32_t index, uint8_t value); 43 44 void WriteInt32BE(int32_t value, uint32_t offset); 45 void WriteInt32LE(int32_t value, uint32_t offset); 46 void WriteUInt32BE(int32_t value, uint32_t offset); 47 void WriteUInt32LE(int32_t value, uint32_t offset); 48 49 void ReadBytes(uint8_t *data, uint32_t offset, uint32_t length); 50 void ReadBytesForArrayBuffer(void *data, uint32_t length); 51 int32_t ReadInt32BE(uint32_t offset); 52 int32_t ReadInt32LE(uint32_t offset); 53 uint32_t ReadUInt32BE(uint32_t offset); 54 uint32_t ReadUInt32LE(uint32_t offset); 55 56 unsigned int WriteString(std::string value, unsigned int size); 57 unsigned int WriteString(std::string value, unsigned int offset, unsigned int length); 58 unsigned int WriteString(std::u16string value, unsigned int offset, unsigned int length); 59 unsigned int WriteString(std::string value, unsigned int offset, unsigned int length, std::string encoding); 60 61 void SubBuffer(Buffer *tBuf, uint32_t start, uint32_t end); 62 uint32_t Copy(Buffer *tBuf, uint32_t tStart, uint32_t sStart, uint32_t sEnd); 63 int Compare(Buffer *tBuf, uint32_t targetStart, uint32_t sourceStart, uint32_t length); 64 int IndexOf(const char *data, uint32_t offset, uint32_t len, uint64_t &resultIndex); 65 int LastIndexOf(const char *data, uint32_t offset, uint32_t len); 66 std::string ToBase64(uint32_t start, uint32_t length); 67 std::string ToBase64Url(uint32_t start, uint32_t length); 68 static EncodingType GetEncodingType(std::string type); 69 void SetArray(std::vector<uint8_t> array, unsigned int offset = 0); 70 void FillBuffer(Buffer *buffer, unsigned int offset, unsigned int end); 71 void FillNumber(std::vector<uint8_t> numbers, unsigned int offset, unsigned int end); 72 void FillString(std::string value, unsigned int offset, unsigned int end, std::string encoding); GetNeedRelease()73 bool GetNeedRelease() const 74 { 75 return needRelease_; 76 } 77 78 private: 79 uint8_t *GetRaw(); 80 bool WriteBytes(uint8_t *src, unsigned int size, uint8_t *dest); 81 void WriteBE(int32_t value, uint32_t bytes); 82 void WriteLE(int32_t value, uint32_t bytes); 83 uint32_t ReadBE(uint32_t bytes); 84 uint32_t ReadLE(uint32_t bytes); 85 std::string Utf16StrToStr(std::u16string value); 86 void WriteByte(uint8_t number, uint32_t offset); 87 void WriteStringLoop(std::string value, unsigned int offset, unsigned int end, unsigned int length); 88 void WriteStringLoop(std::u16string value, unsigned int offset, unsigned int end); 89 std::string GetString(std::string value, EncodingType encodingType); 90 91 uint8_t *raw_ {nullptr}; 92 uint8_t data_[4] = {0}; 93 unsigned int byteOffset_ {}; 94 unsigned int length_ {}; 95 bool needRelease_ {true}; 96 }; 97 } // namespace OHOS::Buffer 98 #endif // BUFFER_JS_BUFFER_H 99