]> git.ozlabs.org Git - ccan/blob - ccan/str/base32/base32.h
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / str / base32 / base32.h
1 /* CC0 (Public domain) - see LICENSE file for details */
2 #ifndef CCAN_STR_BASE32_H
3 #define CCAN_STR_BASE32_H
4 #include "config.h"
5 #include <stdbool.h>
6 #include <stdlib.h>
7
8 /**
9  * base32_decode - Unpack a base32 string.
10  * @str: the base32 string
11  * @slen: the length of @str
12  * @buf: the buffer to write the data into
13  * @bufsize: the length of @buf
14  *
15  * Returns false if there are any characters which aren't valid encodings
16  * or the string wasn't the right length for @bufsize.
17  *
18  * Example:
19  *      unsigned char data[20];
20  *
21  *      if (!base32_decode(argv[1], strlen(argv[1]), data, 20))
22  *              printf("String is malformed!\n");
23  */
24 bool base32_decode(const char *str, size_t slen, void *buf, size_t bufsize);
25
26 /**
27  * base32_encode - Create a nul-terminated base32 string
28  * @buf: the buffer to read the data from
29  * @bufsize: the length of @buf
30  * @dest: the string to fill
31  * @destsize: the max size of the string
32  *
33  * Returns true if the string, including terminator, fits in @destsize;
34  *
35  * Example:
36  *      unsigned char buf[] = { 'f', 'o' };
37  *      char str[9];
38  *
39  *      if (!base32_encode(buf, sizeof(buf), str, sizeof(str)))
40  *              abort();
41  */
42 bool base32_encode(const void *buf, size_t bufsize, char *dest, size_t destsize);
43
44 /**
45  * base32_str_size - Calculate how big a nul-terminated base32 string is
46  * @bytes: bytes of data to represent
47  *
48  * Example:
49  *      unsigned char buf[] = { 'f', 'o' };
50  *      char str[base32_str_size(sizeof(buf))];
51  *
52  *      base32_encode(buf, sizeof(buf), str, sizeof(str));
53  */
54 size_t base32_str_size(size_t bytes);
55
56 /**
57  * base32_data_size - Calculate how many bytes of data in a base32 string
58  * @str: the string
59  * @strlen: the length of str to examine.
60  *
61  * Example:
62  *      const char str[] = "MZXQ====";
63  *      unsigned char buf[base32_data_size(str, strlen(str))];
64  *
65  *      base32_decode(str, strlen(str), buf, sizeof(buf));
66  */
67 size_t base32_data_size(const char *str, size_t strlen);
68
69 /**
70  * base32_chars - the encoding/decoding array to use.
71  *
72  * It must be at least 33 characters long, representing 32 values and
73  * the pad value.  The default array is "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=".
74  */
75 extern const char *base32_chars;
76
77 #endif /* CCAN_STR_BASE32_H */