Libgourou

Libgourou Git Source Tree

Root/include/bytearray.h

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
26namespace 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 */
36 class ByteArray
37 {
38 public:
39
40/**
41 * @brief Create an empty byte array
42 */
43ByteArray();
44
45/**
46 * @brief Initialize ByteArray with a copy of data
47 *
48 * @param data Data to be copied
49 * @param length Length of data
50 */
51ByteArray(const unsigned char* data, unsigned int length);
52
53/**
54 * @brief Initialize ByteArray with a copy of data
55 *
56 * @param data Data to be copied
57 * @param length Optional length of data. If length == -1, it use strlen(data) as length
58 */
59ByteArray(const char* data, int length=-1);
60
61/**
62 * @brief Initialize ByteArray with a copy of str
63 *
64 * @param str Use internal data of str
65 */
66ByteArray(const std::string& str);
67
68ByteArray(const ByteArray& other);
69~ByteArray();
70
71/**
72 * @brief Encode "other" data into base64 and put it into a ByteArray
73 */
74static ByteArray fromBase64(const ByteArray& other);
75
76/**
77 * @brief Encode data into base64 and put it into a ByteArray
78 *
79 * @param data Data to be encoded
80 * @param length Optional length of data. If length == -1, it use strlen(data) as length
81 */
82static ByteArray fromBase64(const char* data, int length=-1);
83
84/**
85 * @brief Encode str into base64 and put it into a ByteArray
86 *
87 * @param str Use internal data of str
88 */
89static ByteArray fromBase64(const std::string& str);
90
91/**
92 * @brief Return a string with base64 encoded internal data
93 */
94std::string toBase64();
95
96/**
97 * @brief Return a string with human readable hex encoded internal data
98 */
99std::string toHex();
100
101/**
102 * @brief Append a byte to internal data
103 */
104void append(unsigned char c);
105
106/**
107 * @brief Append data to internal data
108 */
109void append(const unsigned char* data, unsigned int length);
110
111/**
112 * @brief Append str to internal data
113 */
114void append(const char* str);
115
116/**
117 * @brief Append str to internal data
118 */
119void append(const std::string& str);
120
121/**
122 * @brief Get internal data. Must bot be modified nor freed
123 */
124const unsigned char* data() {return _data;}
125
126/**
127 * @brief Get internal data length
128 */
129unsigned int length() {return _length;}
130
131ByteArray& operator=(const ByteArray& other);
132
133 private:
134void initData(const unsigned char* data, unsigned int length);
135void addRef();
136void delRef();
137
138const unsigned char* _data;
139unsigned int _length;
140static std::map<const unsigned char*, int> refCounter;
141 };
142}
143#endif

Archive Download this file