]>
Commit | Line | Data |
---|---|---|
5c193dec | 1 | /* |
77b1029d | 2 | * Copyright (C) 1996-2020 The Squid Software Foundation and contributors |
5c193dec AJ |
3 | * |
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. | |
7 | */ | |
8 | ||
25f98340 AJ |
9 | #ifndef _SQUID_BASE64_H |
10 | #define _SQUID_BASE64_H | |
11 | ||
1d11e9b3 | 12 | #if HAVE_NETTLE_BASE64_H && HAVE_NETTLE34_BASE64 |
aadbbd7d AJ |
13 | #include <nettle/base64.h> |
14 | ||
1d11e9b3 | 15 | #else /* Base64 functions copied from Nettle 3.4 under GPLv2, with adjustments */ |
aadbbd7d | 16 | |
1d11e9b3 | 17 | /* base64.h |
25f98340 | 18 | |
1d11e9b3 | 19 | Base-64 encoding and decoding. |
f53969cc | 20 | |
1d11e9b3 | 21 | Copyright (C) 2002 Niels Möller, Dan Egnor |
aadbbd7d | 22 | |
1d11e9b3 | 23 | This file is part of GNU Nettle. |
aadbbd7d | 24 | |
1d11e9b3 AJ |
25 | GNU Nettle is free software: you can redistribute it and/or |
26 | modify it under the terms of either: | |
aadbbd7d | 27 | |
1d11e9b3 AJ |
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. | |
f53969cc | 31 | |
1d11e9b3 | 32 | or |
aadbbd7d | 33 | |
1d11e9b3 AJ |
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. | |
37 | ||
38 | or both in parallel, as here. | |
39 | ||
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. | |
44 | ||
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/. | |
48 | */ | |
49 | ||
50 | #ifdef __cplusplus | |
51 | extern "C" { | |
52 | #endif | |
f53969cc | 53 | |
1d11e9b3 | 54 | /* Base64 encoding */ |
f53969cc | 55 | |
aadbbd7d AJ |
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. */ | |
1d11e9b3 | 59 | #define BASE64_ENCODE_LENGTH(length) (((length) * 8 + 4)/6) |
f53969cc | 60 | |
aadbbd7d | 61 | /* Maximum length of output generated by base64_encode_final. */ |
1d11e9b3 | 62 | #define BASE64_ENCODE_FINAL_LENGTH 3 |
f53969cc | 63 | |
aadbbd7d | 64 | /* Exact length of output generated by base64_encode_raw, including |
1d11e9b3 AJ |
65 | * padding. */ |
66 | #define BASE64_ENCODE_RAW_LENGTH(length) ((((length) + 2)/3)*4) | |
f53969cc | 67 | |
aadbbd7d AJ |
68 | struct base64_encode_ctx |
69 | { | |
1d11e9b3 AJ |
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. */ | |
aadbbd7d | 73 | }; |
f53969cc | 74 | |
1d11e9b3 AJ |
75 | /* Initialize encoding context for base-64 */ |
76 | void | |
77 | base64_encode_init(struct base64_encode_ctx *ctx); | |
78 | ||
79 | /* Initialize encoding context for URL safe alphabet, RFC 4648. */ | |
80 | void | |
81 | base64url_encode_init(struct base64_encode_ctx *ctx); | |
aadbbd7d | 82 | |
1d11e9b3 AJ |
83 | /* Encodes a single byte. Returns amount of output (always 1 or 2). */ |
84 | size_t | |
85 | base64_encode_single(struct base64_encode_ctx *ctx, | |
86 | char *dst, | |
87 | uint8_t src); | |
aadbbd7d AJ |
88 | |
89 | /* Returns the number of output characters. DST should point to an | |
1d11e9b3 AJ |
90 | * area of size at least BASE64_ENCODE_LENGTH(length). */ |
91 | size_t | |
92 | base64_encode_update(struct base64_encode_ctx *ctx, | |
93 | char *dst, | |
94 | size_t length, | |
95 | const uint8_t *src); | |
96 | ||
97 | /* DST should point to an area of size at least | |
98 | * BASE64_ENCODE_FINAL_LENGTH */ | |
99 | size_t | |
100 | base64_encode_final(struct base64_encode_ctx *ctx, | |
101 | char *dst); | |
102 | ||
103 | /* Lower level functions */ | |
104 | ||
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. FIXME: Use of overlap | |
108 | * is deprecated, if needed there should be a separate public fucntion | |
109 | * to do that.*/ | |
110 | void | |
111 | base64_encode_raw(char *dst, size_t length, const uint8_t *src); | |
112 | ||
113 | void | |
114 | base64_encode_group(char *dst, uint32_t group); | |
115 | ||
116 | /* Base64 decoding */ | |
117 | ||
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) | |
121 | ||
122 | struct base64_decode_ctx | |
123 | { | |
124 | const signed char *table; /* Decoding table */ | |
125 | unsigned short word; /* Leftover bits */ | |
126 | unsigned char bits; /* Number buffered bits */ | |
127 | ||
128 | /* Number of padding characters encountered */ | |
129 | unsigned char padding; | |
130 | }; | |
aadbbd7d | 131 | |
1d11e9b3 AJ |
132 | /* Initialize decoding context for base-64 */ |
133 | void | |
134 | base64_decode_init(struct base64_decode_ctx *ctx); | |
135 | ||
136 | /* Initialize encoding context for URL safe alphabet, RFC 4648. */ | |
137 | void | |
138 | base64url_decode_init(struct base64_decode_ctx *ctx); | |
139 | ||
140 | /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on | |
141 | * errors. */ | |
142 | int | |
143 | base64_decode_single(struct base64_decode_ctx *ctx, | |
144 | uint8_t *dst, | |
145 | char src); | |
146 | ||
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. */ | |
150 | int | |
151 | base64_decode_update(struct base64_decode_ctx *ctx, | |
152 | size_t *dst_length, | |
153 | uint8_t *dst, | |
154 | size_t src_length, | |
155 | const char *src); | |
156 | ||
157 | /* Returns 1 on success. */ | |
158 | int | |
159 | base64_decode_final(struct base64_decode_ctx *ctx); | |
8bdd0cec | 160 | |
25f98340 AJ |
161 | #ifdef __cplusplus |
162 | } | |
163 | #endif | |
aadbbd7d | 164 | |
1d11e9b3 | 165 | #endif /* HAVE_NETTLE_BASE64_H && HAVE_NETTLE34_BASE64 */ |
aadbbd7d AJ |
166 | |
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) | |
170 | ||
25f98340 | 171 | #endif /* _SQUID_BASE64_H */ |
7d5ae477 | 172 |