]> mj.ucw.cz Git - libucw.git/blob - ucw/base64.h
bgets_mp(): Added a non-intuitive warning to documentation.
[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.
28  */
29 uint base64_decode(byte *dest, const byte *src, uint len);
30
31 /**
32  * Use this macro to calculate @base64_encode() output buffer size.
33  */
34 #define BASE64_ENC_LENGTH(x) (((x)+2)/3 *4)
35
36 /*
37  * When called for BASE64_IN_CHUNK-byte chunks, the result will be
38  * always BASE64_OUT_CHUNK bytes long. If a longer block is split
39  * to such chunks, the result will be identical.
40  */
41 #define BASE64_IN_CHUNK 3 /** Size of chunk on the un-encoded side. **/
42 #define BASE64_OUT_CHUNK 4 /** Size of chunk on the encoded side. **/
43
44 /*
45  * Lookup table for fast encoding.
46  * For each 6bit value contains corresponding base64 character.
47  */
48 extern const byte base64_enc_table[65];
49 #define BASE64_PADDING '=' /* Padding character */
50
51 /*
52  * Lookup table for fast decoding:
53  * -- for valid base64 characters contains their 6bit values
54  * -- for BASE64_PADDING character contains special value BASE64_DEC_PADDING
55  * -- for all other characters contains BASE64_DEC_INVALID
56  *
57  * Note that BASE64_DEC_INVALID is greater than BASE64_DEC_PADDING
58  * (can be useful to know for some optimizations).
59  */
60 extern const byte base64_dec_table[256];
61 #define BASE64_DEC_PADDING 0x40
62 #define BASE64_DEC_INVALID 0x80