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