base64.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
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 
9 #ifndef SQUID_INCLUDE_BASE64_H
10 #define SQUID_INCLUDE_BASE64_H
11 
12 #if HAVE_NETTLE_BASE64_H
13 #include <nettle/base64.h>
14 
15 #else /* Base64 functions copied from Nettle 3.4 under GPLv2, with adjustments */
16 
17 /* base64.h
18 
19  Base-64 encoding and decoding.
20 
21  Copyright (C) 2002 Niels Möller, Dan Egnor
22 
23  This file is part of GNU Nettle.
24 
25  GNU Nettle is free software: you can redistribute it and/or
26  modify it under the terms of either:
27 
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.
31 
32  or
33 
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 /* Base64 encoding */
51 
52 /* Maximum length of output for base64_encode_update. NOTE: Doesn't
53  * include any padding that base64_encode_final may add. */
54 /* We have at most 4 buffered bits, and a total of (4 + length * 8) bits. */
55 #define BASE64_ENCODE_LENGTH(length) (((length) * 8 + 4)/6)
56 
57 /* Maximum length of output generated by base64_encode_final. */
58 #define BASE64_ENCODE_FINAL_LENGTH 3
59 
60 /* Exact length of output generated by base64_encode_raw, including
61  * padding. */
62 #define BASE64_ENCODE_RAW_LENGTH(length) ((((length) + 2)/3)*4)
63 
65 {
66  const char *alphabet; /* Alphabet to use for encoding */
67  unsigned short word; /* Leftover bits */
68  unsigned char bits; /* Number of bits, always 0, 2, or 4. */
69 };
70 
71 /* Initialize encoding context for base-64 */
72 void
74 
75 /* Initialize encoding context for URL safe alphabet, RFC 4648. */
76 void
78 
79 /* Encodes a single byte. Returns amount of output (always 1 or 2). */
80 size_t
82  char *dst,
83  uint8_t src);
84 
85 /* Returns the number of output characters. DST should point to an
86  * area of size at least BASE64_ENCODE_LENGTH(length). */
87 size_t
89  char *dst,
90  size_t length,
91  const uint8_t *src);
92 
93 /* DST should point to an area of size at least
94  * BASE64_ENCODE_FINAL_LENGTH */
95 size_t
97  char *dst);
98 
99 /* Lower level functions */
100 
101 /* Encodes a string in one go, including any padding at the end.
102  * Generates exactly BASE64_ENCODE_RAW_LENGTH(length) bytes of output.
103  * Supports overlapped operation, if src <= dst.
104  * TODO: Use of overlap is deprecated, if needed there should be a separate public function
105  * to do that.*/
106 void
107 base64_encode_raw(char *dst, size_t length, const uint8_t *src);
108 
109 void
110 base64_encode_group(char *dst, uint32_t group);
111 
112 /* Base64 decoding */
113 
114 /* Maximum length of output for base64_decode_update. */
115 /* We have at most 6 buffered bits, and a total of (length + 1) * 6 bits. */
116 #define BASE64_DECODE_LENGTH(length) ((((length) + 1) * 6) / 8)
117 
119 {
120  const signed char *table; /* Decoding table */
121  unsigned short word; /* Leftover bits */
122  unsigned char bits; /* Number buffered bits */
123 
124  /* Number of padding characters encountered */
125  unsigned char padding;
126 };
127 
128 /* Initialize decoding context for base-64 */
129 void
131 
132 /* Initialize encoding context for URL safe alphabet, RFC 4648. */
133 void
135 
136 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
137  * errors. */
138 int
140  uint8_t *dst,
141  char src);
142 
143 /* Returns 1 on success, 0 on error. DST should point to an area of
144  * size at least BASE64_DECODE_LENGTH(length). The amount of data
145  * generated is returned in *DST_LENGTH. */
146 int
148  size_t *dst_length,
149  uint8_t *dst,
150  size_t src_length,
151  const char *src);
152 
153 /* Returns 1 on success. */
154 int
156 
157 #endif /* HAVE_NETTLE_BASE64_H */
158 
161 #define base64_encode_len(length) (BASE64_ENCODE_LENGTH(length)+BASE64_ENCODE_FINAL_LENGTH+1)
162 
163 #endif /* SQUID_INCLUDE_BASE64_H */
164 
const signed char * table
Definition: base64.h:120
unsigned short word
Definition: base64.h:121
size_t base64_encode_single(struct base64_encode_ctx *ctx, char *dst, uint8_t src)
Definition: base64.cc:240
void base64_decode_init(struct base64_decode_ctx *ctx)
Definition: base64.cc:54
unsigned char bits
Definition: base64.h:122
size_t base64_encode_final(struct base64_encode_ctx *ctx, char *dst)
Definition: base64.cc:308
unsigned char bits
Definition: base64.h:68
void base64url_decode_init(struct base64_decode_ctx *ctx)
void base64_encode_raw(char *dst, size_t length, const uint8_t *src)
Definition: base64.cc:217
int base64_decode_final(struct base64_decode_ctx *ctx)
Definition: base64.cc:159
void base64url_encode_init(struct base64_encode_ctx *ctx)
unsigned short word
Definition: base64.h:67
int base64_decode_update(struct base64_decode_ctx *ctx, size_t *dst_length, uint8_t *dst, size_t src_length, const char *src)
Definition: base64.cc:129
void base64_encode_group(char *dst, uint32_t group)
Definition: base64.cc:223
void base64_encode_init(struct base64_encode_ctx *ctx)
Definition: base64.cc:232
unsigned char padding
Definition: base64.h:125
size_t base64_encode_update(struct base64_encode_ctx *ctx, char *dst, size_t length, const uint8_t *src)
Definition: base64.cc:265
const char * alphabet
Definition: base64.h:66
int base64_decode_single(struct base64_decode_ctx *ctx, uint8_t *dst, char src)
Definition: base64.cc:82

 

Introduction

Documentation

Support

Miscellaneous