]>
git.ipfire.org Git - thirdparty/squid.git/blob - include/base64.h
0abf202dcf95a77b024624aea11242641d3e70f1
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 #ifndef _SQUID_BASE64_H
10 #define _SQUID_BASE64_H
12 #if HAVE_NETTLE_BASE64_H && HAVE_NETTLE34_BASE64
13 #include <nettle/base64.h>
15 #else /* Base64 functions copied from Nettle 3.4 under GPLv2, with adjustments */
19 Base-64 encoding and decoding.
21 Copyright (C) 2002 Niels Möller, Dan Egnor
23 This file is part of GNU Nettle.
25 GNU Nettle is free software: you can redistribute it and/or
26 modify it under the terms of either:
28 * the GNU Lesser General Public License as published by the Free
29 Software Foundation; either version 3 of the License, or (at your
30 option) any later version.
34 * the GNU General Public License as published by the Free
35 Software Foundation; either version 2 of the License, or (at your
36 option) any later version.
38 or both in parallel, as here.
40 GNU Nettle is distributed in the hope that it will be useful,
41 but WITHOUT ANY WARRANTY; without even the implied warranty of
42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 General Public License for more details.
45 You should have received copies of the GNU General Public License and
46 the GNU Lesser General Public License along with this program. If
47 not, see http://www.gnu.org/licenses/.
56 /* Maximum length of output for base64_encode_update. NOTE: Doesn't
57 * include any padding that base64_encode_final may add. */
58 /* We have at most 4 buffered bits, and a total of (4 + length * 8) bits. */
59 #define BASE64_ENCODE_LENGTH(length) (((length) * 8 + 4)/6)
61 /* Maximum length of output generated by base64_encode_final. */
62 #define BASE64_ENCODE_FINAL_LENGTH 3
64 /* Exact length of output generated by base64_encode_raw, including
66 #define BASE64_ENCODE_RAW_LENGTH(length) ((((length) + 2)/3)*4)
68 struct base64_encode_ctx
70 const char *alphabet
; /* Alphabet to use for encoding */
71 unsigned short word
; /* Leftover bits */
72 unsigned char bits
; /* Number of bits, always 0, 2, or 4. */
75 /* Initialize encoding context for base-64 */
77 base64_encode_init(struct base64_encode_ctx
*ctx
);
79 /* Initialize encoding context for URL safe alphabet, RFC 4648. */
81 base64url_encode_init(struct base64_encode_ctx
*ctx
);
83 /* Encodes a single byte. Returns amount of output (always 1 or 2). */
85 base64_encode_single(struct base64_encode_ctx
*ctx
,
89 /* Returns the number of output characters. DST should point to an
90 * area of size at least BASE64_ENCODE_LENGTH(length). */
92 base64_encode_update(struct base64_encode_ctx
*ctx
,
97 /* DST should point to an area of size at least
98 * BASE64_ENCODE_FINAL_LENGTH */
100 base64_encode_final(struct base64_encode_ctx
*ctx
,
103 /* Lower level functions */
105 /* Encodes a string in one go, including any padding at the end.
106 * Generates exactly BASE64_ENCODE_RAW_LENGTH(length) bytes of output.
107 * Supports overlapped operation, if src <= dst.
108 * TODO: Use of overlap is deprecated, if needed there should be a separate public function
111 base64_encode_raw(char *dst
, size_t length
, const uint8_t *src
);
114 base64_encode_group(char *dst
, uint32_t group
);
116 /* Base64 decoding */
118 /* Maximum length of output for base64_decode_update. */
119 /* We have at most 6 buffered bits, and a total of (length + 1) * 6 bits. */
120 #define BASE64_DECODE_LENGTH(length) ((((length) + 1) * 6) / 8)
122 struct base64_decode_ctx
124 const signed char *table
; /* Decoding table */
125 unsigned short word
; /* Leftover bits */
126 unsigned char bits
; /* Number buffered bits */
128 /* Number of padding characters encountered */
129 unsigned char padding
;
132 /* Initialize decoding context for base-64 */
134 base64_decode_init(struct base64_decode_ctx
*ctx
);
136 /* Initialize encoding context for URL safe alphabet, RFC 4648. */
138 base64url_decode_init(struct base64_decode_ctx
*ctx
);
140 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
143 base64_decode_single(struct base64_decode_ctx
*ctx
,
147 /* Returns 1 on success, 0 on error. DST should point to an area of
148 * size at least BASE64_DECODE_LENGTH(length). The amount of data
149 * generated is returned in *DST_LENGTH. */
151 base64_decode_update(struct base64_decode_ctx
*ctx
,
157 /* Returns 1 on success. */
159 base64_decode_final(struct base64_decode_ctx
*ctx
);
165 #endif /* HAVE_NETTLE_BASE64_H && HAVE_NETTLE34_BASE64 */
167 /// Calculate the buffer size required to hold the encoded form of
168 /// a string of length 'decodedLen' including all terminator bytes.
169 # define base64_encode_len(length) (BASE64_ENCODE_LENGTH(length)+BASE64_ENCODE_FINAL_LENGTH+1)
171 #endif /* _SQUID_BASE64_H */