Mercurial > libbase64
comparison base64.h @ 33:01c29bee0c54
man: add libbase64.3 manual page
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 02 Jun 2021 16:37:59 +0200 |
parents | c96f2b26678a |
children | e4a6c3f3d0f2 |
comparison
equal
deleted
inserted
replaced
32:c96f2b26678a | 33:01c29bee0c54 |
---|---|
17 */ | 17 */ |
18 | 18 |
19 #ifndef BASE64_H | 19 #ifndef BASE64_H |
20 #define BASE64_H | 20 #define BASE64_H |
21 | 21 |
22 /** | |
23 * \file base64.h | |
24 * \brief Base64 encoding and decoding. | |
25 * \author David Demelier <markand@malikania.fr> | |
26 * \version 2.0.0-dev | |
27 */ | |
28 | |
29 #include <stddef.h> | 22 #include <stddef.h> |
30 | 23 |
31 #if defined(__cplusplus) | 24 #if defined(__cplusplus) |
32 extern "C" { | 25 extern "C" { |
33 #endif | 26 #endif |
34 | 27 |
35 /** | 28 #define B64_ENCODE_LENGTH(x) (4 * ((x) / 3 + 1)) |
36 * Check if the character is a %base64 character, A-Za-z0-9 and +/. | 29 #define B64_DECODE_LENGTH(x) (3 * ((x) / 4)) |
37 * | 30 |
38 * \param ch the character to test | |
39 * \return non-zero if valid | |
40 */ | |
41 int | 31 int |
42 b64_isbase64(unsigned char ch); | 32 b64_isbase64(unsigned char ch); |
43 | 33 |
44 /** | |
45 * Check if the given character is a valid character in %base64 string, | |
46 * including '='. | |
47 * | |
48 * \param ch the character | |
49 * \return non-zero if the character is valid | |
50 */ | |
51 int | 34 int |
52 b64_isvalid(unsigned char ch); | 35 b64_isvalid(unsigned char ch); |
53 | 36 |
54 /** | |
55 * Get the %base64 character from the 6-bits value. | |
56 * | |
57 * \pre value must be valid (< 64) | |
58 * \param value the value | |
59 * \return the %base64 character for value | |
60 */ | |
61 unsigned char | 37 unsigned char |
62 b64_lookup(unsigned char value); | 38 b64_lookup(unsigned char value); |
63 | 39 |
64 /** | |
65 * Get the integer value from the %base64 character. | |
66 * | |
67 * \pre b64_isvalid(ch) | |
68 * \param ch the %base64 character | |
69 * \return the integer value for the %base64 character ch | |
70 */ | |
71 unsigned char | 40 unsigned char |
72 b64_rlookup(unsigned char ch); | 41 b64_rlookup(unsigned char ch); |
73 | 42 |
74 /** | |
75 * Compute the original data size according to srcsz. | |
76 * | |
77 * \param srcsz the base64 input length | |
78 * \return The pessimist original data size. | |
79 */ | |
80 size_t | 43 size_t |
81 b64_decode_length(size_t srcsz); | 44 b64_encode(const char *src, size_t srcsz, char *dst, size_t dstsz); |
82 | 45 |
83 /** | |
84 * Compute the required size to encode this data. | |
85 * | |
86 * For performance reasons, this function may returns four unused bytes to | |
87 * avoid calculating padding. | |
88 * | |
89 * \param srcsz the input size | |
90 * \return The number of bytes required to encode the data. | |
91 */ | |
92 size_t | 46 size_t |
93 b64_encode_length(size_t srcsz); | 47 b64_decode(const char *src, size_t srcsz, char *dst, size_t dstsz); |
94 | |
95 /** | |
96 * Encode the given src into the destination buffer. | |
97 * | |
98 * This function will write at most dstsz bytes into the buffer, including the | |
99 * NUL terminator. | |
100 * | |
101 * If srcsz is -1 then strlen(src) is called to determine the number of bytes | |
102 * in src. Note that binary data may contain embedded 0 and therefore | |
103 * specifying the source size would be required in that case. | |
104 * | |
105 * \pre src != NULL | |
106 * \pre dst != NULL | |
107 * \param src the source to encode | |
108 * \param srcsz the source size or -1 to read until NUL terminator | |
109 * \param dst the destination buffer | |
110 * \param dstsz the destination size | |
111 * \return The number of bytes written or -1 on error and sets errno. | |
112 * \see \ref b64_encode_length | |
113 */ | |
114 size_t | |
115 b64_encode(const char src[], size_t srcsz, char dst[], size_t dstsz); | |
116 | |
117 /** | |
118 * Decode the given src into the destination buffer. | |
119 * | |
120 * This function will write at most dstsz bytes into the buffer, including the | |
121 * NUL terminator. | |
122 * | |
123 * If srcsz is -1 then strlen(src) is called to determine the number of bytes | |
124 * in src. | |
125 * | |
126 * \pre src != NULL | |
127 * \pre dst != NULL | |
128 * \param src the source to encode | |
129 * \param srcsz the source size or -1 to read until NUL terminator | |
130 * \param dst the destination buffer | |
131 * \param dstsz the destination size | |
132 * \return The number of bytes written or -1 on error and sets errno. | |
133 * \see \ref b64_decode_length | |
134 */ | |
135 size_t | |
136 b64_decode(const char src[], size_t srcsz, char dst[], size_t dstsz); | |
137 | 48 |
138 #if defined(__cplusplus) | 49 #if defined(__cplusplus) |
139 } | 50 } |
140 #endif | 51 #endif |
141 | 52 |
142 #endif // !BASE64_H | 53 #endif /* !BASE64_H */ |