1 | /*␊ |
2 | Copyright 2021 Grégory Soutadé␊ |
3 | ␊ |
4 | This file is part of libgourou.␊ |
5 | ␊ |
6 | libgourou is free software: you can redistribute it and/or modify␊ |
7 | it under the terms of the GNU Lesser General Public License as published by␊ |
8 | the Free Software Foundation, either version 3 of the License, or␊ |
9 | (at your option) any later version.␊ |
10 | ␊ |
11 | libgourou is distributed in the hope that it will be useful,␊ |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
14 | GNU Lesser General Public License for more details.␊ |
15 | ␊ |
16 | You should have received a copy of the GNU Lesser General Public License␊ |
17 | along with libgourou. If not, see <http://www.gnu.org/licenses/>.␊ |
18 | */␊ |
19 | ␊ |
20 | #ifndef _BYTEARRAY_H_␊ |
21 | #define _BYTEARRAY_H_␊ |
22 | ␊ |
23 | #include <map>␊ |
24 | #include <string>␊ |
25 | ␊ |
26 | namespace gourou␊ |
27 | {␊ |
28 | /**␊ |
29 | * @brief Utility class for byte array management.␊ |
30 | *␊ |
31 | * It's an equivalent of QByteArray␊ |
32 | *␊ |
33 | * Data handled is first copied in a newly allocated buffer␊ |
34 | * and then shared between all copies until last object is destroyed␊ |
35 | * (internal reference counter == 0)␊ |
36 | */␊ |
37 | class ByteArray␊ |
38 | {␊ |
39 | public:␊ |
40 | ␊ |
41 | ␉/**␊ |
42 | ␉ * @brief Create an empty byte array␊ |
43 | ␉ *␊ |
44 | ␉ * @param useMalloc If true, use malloc() instead of new[] for allocation␊ |
45 | ␉ */␊ |
46 | ␉ByteArray(bool useMalloc=false);␊ |
47 | ␊ |
48 | ␉/**␊ |
49 | ␉ * @brief Create an empty byte array of length bytes␊ |
50 | ␉ *␊ |
51 | ␉ * @param length Length of data␊ |
52 | ␉ * @param useMalloc If true, use malloc() instead of new[] for allocation␊ |
53 | ␉ */␊ |
54 | ␉ByteArray(unsigned int length, bool useMalloc=false);␊ |
55 | ␊ |
56 | ␉/**␊ |
57 | ␉ * @brief Initialize ByteArray with a copy of data␊ |
58 | ␉ *␊ |
59 | ␉ * @param data Data to be copied␊ |
60 | ␉ * @param length Length of data␊ |
61 | ␉ */␊ |
62 | ␉ByteArray(const unsigned char* data, unsigned int length);␊ |
63 | ␊ |
64 | ␉/**␊ |
65 | ␉ * @brief Initialize ByteArray with a copy of data␊ |
66 | ␉ *␊ |
67 | ␉ * @param data Data to be copied␊ |
68 | ␉ * @param length Optional length of data. If length == -1, it use strlen(data) as length␊ |
69 | ␉ */␊ |
70 | ␉ByteArray(const char* data, int length=-1);␊ |
71 | ␊ |
72 | ␉/**␊ |
73 | ␉ * @brief Initialize ByteArray with a copy of str␊ |
74 | ␉ *␊ |
75 | ␉ * @param str Use internal data of str␊ |
76 | ␉ */␊ |
77 | ␉ByteArray(const std::string& str);␊ |
78 | ␊ |
79 | ␉ByteArray(const ByteArray& other);␊ |
80 | ␉~ByteArray();␊ |
81 | ␊ |
82 | ␉/**␊ |
83 | ␉ * @brief Encode "other" data into base64 and put it into a ByteArray␊ |
84 | ␉ */␊ |
85 | ␉static ByteArray fromBase64(const ByteArray& other);␊ |
86 | ␊ |
87 | ␉/**␊ |
88 | ␉ * @brief Encode data into base64 and put it into a ByteArray␊ |
89 | ␉ *␊ |
90 | ␉ * @param data Data to be encoded␊ |
91 | ␉ * @param length Optional length of data. If length == -1, it use strlen(data) as length␊ |
92 | ␉ */␊ |
93 | ␉static ByteArray fromBase64(const char* data, int length=-1);␊ |
94 | ␊ |
95 | ␉/**␊ |
96 | ␉ * @brief Encode str into base64 and put it into a ByteArray␊ |
97 | ␉ *␊ |
98 | ␉ * @param str Use internal data of str␊ |
99 | ␉ */␊ |
100 | ␉static ByteArray fromBase64(const std::string& str);␊ |
101 | ␊ |
102 | ␉/**␊ |
103 | ␉ * @brief Return a string with base64 encoded internal data␊ |
104 | ␉ */␊ |
105 | ␉std::string toBase64();␊ |
106 | ␊ |
107 | ␉/**␊ |
108 | ␉ * @brief Convert hex string into bytes␊ |
109 | ␉ *␊ |
110 | ␉ * @param str Hex string␊ |
111 | ␉ */␊ |
112 | ␉static ByteArray fromHex(const std::string& str);␊ |
113 | ␊ |
114 | ␉/**␊ |
115 | ␉ * @brief Return a string with human readable hex encoded internal data␊ |
116 | ␉ */␊ |
117 | ␉std::string toHex();␊ |
118 | ␊ |
119 | ␉/**␊ |
120 | ␉ * @brief Append a byte to internal data␊ |
121 | ␉ */␊ |
122 | ␉void append(unsigned char c);␊ |
123 | ␊ |
124 | ␉/**␊ |
125 | ␉ * @brief Append data to internal data␊ |
126 | ␉ */␊ |
127 | ␉void append(const unsigned char* data, unsigned int length);␊ |
128 | ␊ |
129 | ␉/**␊ |
130 | ␉ * @brief Append str to internal data␊ |
131 | ␉ */␊ |
132 | ␉void append(const char* str);␊ |
133 | ␊ |
134 | ␉/**␊ |
135 | ␉ * @brief Append str to internal data␊ |
136 | ␉ */␊ |
137 | ␉void append(const std::string& str);␊ |
138 | ␊ |
139 | ␉/**␊ |
140 | ␉ * @brief Get internal data. Must not be freed␊ |
141 | ␉ */␊ |
142 | ␉unsigned char* data() {return _data;}␊ |
143 | ␊ |
144 | ␉/**␊ |
145 | ␉ * @brief Get internal data and increment internal reference counter.␊ |
146 | ␉ * Must bot be freed␊ |
147 | ␉ */␊ |
148 | ␉unsigned char* takeShadowData() {addRef() ; return _data;}␊ |
149 | ␊ |
150 | ␉/**␊ |
151 | ␉ * @brief Release shadow data. It can now be freed by ByteArray␊ |
152 | ␉ */␊ |
153 | ␉void releaseShadowData() {delRef();}␊ |
154 | ␊ |
155 | ␉/**␊ |
156 | ␉ * @brief Get internal data length␊ |
157 | ␉ */␊ |
158 | ␉unsigned int length() const {return _length;}␊ |
159 | ␊ |
160 | ␉/**␊ |
161 | ␉ * @brief Get internal data length␊ |
162 | ␉ */␊ |
163 | ␉unsigned int size() const {return length();}␊ |
164 | ␊ |
165 | ␉/**␊ |
166 | ␉ * @brief Increase or decrease internal buffer␊ |
167 | ␉ * @param length New length of internal buffer␊ |
168 | ␉ * @param keepData If true copy old data on new buffer, if false,␊ |
169 | ␉ * create a new buffer with random data␊ |
170 | ␉ */␊ |
171 | ␉void resize(unsigned int length, bool keepData=true);␊ |
172 | ␊ |
173 | ␉ByteArray& operator=(const ByteArray& other);␊ |
174 | ␉␊ |
175 | private:␊ |
176 | ␉void initData(const unsigned char* data, unsigned int length);␊ |
177 | ␉void addRef();␊ |
178 | ␉void delRef();␊ |
179 | ␊ |
180 | ␉bool _useMalloc;␊ |
181 | ␉unsigned char* _data;␊ |
182 | ␉unsigned int _length;␊ |
183 | ␉static std::map<unsigned char*, int> refCounter;␊ |
184 | };␊ |
185 | }␊ |
186 | #endif␊ |