]> mj.ucw.cz Git - libucw.git/blob - ucw/base64.h
Compatibility with GCC < 4.0 is not needed any longer
[libucw.git] / ucw / base64.h
1 /*
2  *      UCW Library -- Base 64 Encoding & Decoding
3  *
4  *      (c) 2002, Robert Spalek <robert@ucw.cz>
5  *      (c) 2017, Pavel Charvat <pchar@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifdef CONFIG_UCW_CLEAN_ABI
12 #define base64_decode ucw_base64_decode
13 #define base64_encode ucw_base64_encode
14 #define base64_enc_table ucw_base64_enc_table
15 #define base64_dec_table ucw_base64_dec_table
16 #endif
17
18 /**
19  * Encodes @len bytes of data pointed to by @src by base64 encoding.
20  * Stores them in @dest and returns the number of bytes the output
21  * takes.
22  */
23 uint base64_encode(byte *dest, const byte *src, uint len);
24 /**
25  * Decodes @len bytes of data pointed to by @src from base64 encoding.
26  * All invalid characters are ignored. The result is stored into @dest
27  * and length of the result is returned. It is allowed to use the
28  * same buffer for both input and output.
29  */
30 uint base64_decode(byte *dest, const byte *src, uint len);
31
32 /**
33  * Use this macro to calculate @base64_encode() output buffer size.
34  */
35 #define BASE64_ENC_LENGTH(x) (((x)+2)/3 *4)
36
37 /*
38  * When called for BASE64_IN_CHUNK-byte chunks, the result will be
39  * always BASE64_OUT_CHUNK bytes long. If a longer block is split
40  * to such chunks, the result will be identical.
41  */
42 #define BASE64_IN_CHUNK 3 /** Size of chunk on the un-encoded side. **/
43 #define BASE64_OUT_CHUNK 4 /** Size of chunk on the encoded side. **/
44
45 /*
46  * Lookup table for fast encoding.
47  * For each 6bit value contains corresponding base64 character.
48  */
49 extern const byte base64_enc_table[65];
50 #define BASE64_PADDING '=' /* Padding character */
51
52 /*
53  * Lookup table for fast decoding:
54  * -- for valid base64 characters contains their 6bit values
55  * -- for BASE64_PADDING character contains special value BASE64_DEC_PADDING
56  * -- for all other characters contains BASE64_DEC_INVALID
57  *
58  * Note that BASE64_DEC_INVALID is greater than BASE64_DEC_PADDING
59  * (can be useful to know for some optimizations).
60  */
61 extern const byte base64_dec_table[256];
62 #define BASE64_DEC_PADDING 0x40
63 #define BASE64_DEC_INVALID 0x80