blob: fbe298372f3db8d6c75cfc729844318d0073a416 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001#ifndef BASE32_H
2#define BASE32_H
3
4/*
5 *
6 * Copyright (c) 2002 Bryce "Zooko" Wilcox-O'Hearn Permission is hereby
7 * granted, free of charge, to any person obtaining a copy of this software to
8 * deal in this software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of this software, and to permit persons to whom this software
11 * is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of this software.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THIS SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THIS SOFTWARE.
23 *
24 * Converted to C++ by:
25 * @author Werner Dittmann <Werner.Dittmann@t-online.de>
26 */
27
28/**
29 * @file Base32.h
30 * @brief C++ implmentation of the Base32 encoding and decoding
31 *
32 * ZRTP uses the base 32 encoding and decoding to generate the Short
33 * Authentication String (SAS).
34 *
35 * @ingroup GNU_ZRTP
36 * @{
37 */
38
39#include <iostream>
40#include <cstdlib>
41
42#include <string.h>
43#include <assert.h>
44#include <stddef.h>
45
46using namespace std;
47
48extern int divceil(int a, int b);
49
50class Base32 {
51
52 public:
53
54 /**
55 * A Constructor that decodes from base32 into binary.
56 *
57 * The constructor decodes the base32 encoded data back into binary
58 * data. Use <code>getDecoded(...)</code> to get the binary data.
59 *
60 * @param encoded
61 * The string that contains the base32 encoded data.
62 */
63 Base32(const string encoded);
64
65 /**
66 * A Constructor that decodes from base32 into binary.
67 *
68 * This constructor decodes the base32 encoded data back into
69 * binary data. Only the specified number of bits are decoded
70 * (should be a multiple of 5). Use
71 * <code>getDecoded(...)</code> to get the binary data.
72 *
73 * @param encoded
74 * The string that contains the base32 encoded data.
75 * @param noOfBits
76 * How many bits to decode into binary data.
77 */
78 Base32(const string encoded, int noOfBits);
79
80 /**
81 * A Constructor that encodes binary data.
82 *
83 * The constructor converts the first specified number of bits of
84 * the binary data into a base32 presentation. Use
85 * <code>getEncoded</code> to get the encoded data.
86 *
87 * @param data
88 * A pointer to the first bits (byte) of binary data
89 * @param noOfBits
90 * How many bits to use for encoding. Should be a
91 * multiple of 5.
92 */
93 Base32(const unsigned char* data, int noOfBits);
94
95 ~Base32();
96
97 /**
98 * Get the decoded binary data and its length.
99 *
100 * The method returns the decoded binary data if the appropriate
101 * Constructor was used. Otherwise we return <code>NULL</code>
102 * pointer and length zero.
103 *
104 * <em>Note:</em> This method returns a pointer to the decoded
105 * binary data. The Base32 object manages this pointer, thus you
106 * may need to copy the data to a save place before deleting this
107 * object. If the object is deleted this pointer is no longer
108 * valid.
109 *
110 * @param length
111 * A reference to an integer.
112 * @return
113 * A pointer to the decoded binary data.
114 */
115 const unsigned char* getDecoded(int &length);
116
117 /**
118 * Get the encoded base32 string.
119 *
120 * The method returns a string that contains the base32 encoded
121 * data if the appropriate constructor was used. Otherwise we
122 * return an empty string.
123 *
124 * @return
125 * The string containing the base32 encoded data.
126 */
127 const string getEncoded() { return encoded; };
128
129 /**
130 * Compute the number of base32 encoded characters given the
131 * number of bits.
132 *
133 * @param lengthInBits
134 * The length of the data in bits
135 * @return
136 * The length of the base-32 encoding of the data in characters
137 */
138 static size_t const b2alen(const size_t lengthInBits) {
139 return divceil(lengthInBits, 5); };
140
141 private:
142
143 /**
144 * Decodes a string with base32 presentation into binary data.
145 *
146 * a2b_l() will return a result big enough to hold lengthinbits bits. So
147 * for example if cs is 4 characters long (encoding at least 15 and up to
148 * 20 bits) and lengthinbits is 16, then a2b_l() will return a string of
149 * length 2 (since 2 bytes is sufficient to store 16 bits). If cs is 4
150 * characters long and lengthinbits is 20, then a2b_l() will return a
151 * string of length 3 (since 3 bytes is sufficient to store 20 bits). Note
152 * that `b2a_l()' does not mask off unused least-significant bits, so for
153 * example if cs is 4 characters long and lengthinbits is 17, then you
154 * must ensure that all three of the unused least-significant bits of cs
155 * are zero bits or you will get the wrong result. This precondition is
156 * tested by assertions if assertions are enabled. (Generally you just
157 * require the encoder to ensure this consistency property between the
158 * least significant zero bits and value of `lengthinbits', and reject
159 * strings that have a length-in-bits which isn't a multiple of 8 and yet
160 * don't have trailing zero bits, as improperly encoded.)
161 *
162 * @param cs
163 * The data to be decoded
164 * @param size
165 * The length of the input data buffer. Usually divceil(length in bits, 5).
166 * @param lengthinbits
167 * The number of bits of data in <code>cs</code> to be decoded
168 */
169 void a2b_l(const string cs, size_t size, const size_t lengthinbits);
170
171 /**
172 * Encodes binary to to base32 presentation.
173 *
174 * b2a_l() will generate a base-32 encoded string big enough to encode
175 * lengthinbits bits. So for example if os is 2 bytes long and
176 * lengthinbits is 15, then b2a_l() will generate a 3-character- long
177 * base-32 encoded string (since 3 quintets is sufficient to encode 15
178 * bits). If os is 2 bytes long and lengthinbits is 16 (or None), then
179 * b2a_l() will generate a 4-character string. Note that `b2a_l()' does
180 * not mask off unused least-significant bits, so for example if os is 2
181 * bytes long and lengthinbits is 15, then you must ensure that the unused
182 * least-significant bit of os is a zero bit or you will get the wrong
183 * result. This precondition is tested by assertions if assertions are
184 * enabled.
185 *
186 * Warning: if you generate a base-32 encoded string with `b2a_l()', and
187 * then someone else tries to decode it by calling `a2b()' instead of
188 * `a2b_l()', then they will (probably) get a different string than the
189 * one you encoded! So only use `b2a_l()' when you are sure that the
190 * encoding and decoding sides know exactly which `lengthinbits' to use.
191 * If you do not have a way for the encoder and the decoder to agree upon
192 * the lengthinbits, then it is best to use `b2a()' and `a2b()'. The only
193 * drawback to using `b2a()' over `b2a_l()' is that when you have a number
194 * of bits to encode that is not a multiple of 8, `b2a()' can sometimes
195 * generate a base-32 encoded string that is one or two characters longer
196 * than necessary.
197 *
198 * @param cs
199 * Pointer to binary data.
200 * @param len
201 * Length of the binary data buffer. Usually (noOfBits+7)/8.
202 * @param noOfBits
203 * The number of bits of data in encoded into `cs'
204 */
205 void b2a_l(const unsigned char* cs, int len, const size_t noOfBits);
206
207 /**
208 * Holds the pointer to decoded binary data
209 */
210 unsigned char *binaryResult;
211
212 /**
213 * Length of decoding result
214 */
215 int resultLength;
216
217 /**
218 * The string containing the base32 encoded data.
219 */
220 string encoded;
221
222 unsigned char smallBuffer[128];
223};
224
225/**
226 * @}
227 */
228#endif