base64.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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_BASE64_H
10#define _SQUID_BASE64_H
11
12#if HAVE_NETTLE_BASE64_H && HAVE_NETTLE34_BASE64
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#ifdef __cplusplus
51extern "C" {
52#endif
53
54/* Base64 encoding */
55
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)
60
61/* Maximum length of output generated by base64_encode_final. */
62#define BASE64_ENCODE_FINAL_LENGTH 3
63
64/* Exact length of output generated by base64_encode_raw, including
65 * padding. */
66#define BASE64_ENCODE_RAW_LENGTH(length) ((((length) + 2)/3)*4)
67
69{
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. */
73};
74
75/* Initialize encoding context for base-64 */
76void
78
79/* Initialize encoding context for URL safe alphabet, RFC 4648. */
80void
82
83/* Encodes a single byte. Returns amount of output (always 1 or 2). */
84size_t
86 char *dst,
87 uint8_t src);
88
89/* Returns the number of output characters. DST should point to an
90 * area of size at least BASE64_ENCODE_LENGTH(length). */
91size_t
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 */
99size_t
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.
108 * TODO: Use of overlap is deprecated, if needed there should be a separate public function
109 * to do that.*/
110void
111base64_encode_raw(char *dst, size_t length, const uint8_t *src);
112
113void
114base64_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
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};
131
132/* Initialize decoding context for base-64 */
133void
135
136/* Initialize encoding context for URL safe alphabet, RFC 4648. */
137void
139
140/* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
141 * errors. */
142int
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. */
150int
152 size_t *dst_length,
153 uint8_t *dst,
154 size_t src_length,
155 const char *src);
156
157/* Returns 1 on success. */
158int
160
161#ifdef __cplusplus
162}
163#endif
164
165#endif /* HAVE_NETTLE_BASE64_H && HAVE_NETTLE34_BASE64 */
166
169# define base64_encode_len(length) (BASE64_ENCODE_LENGTH(length)+BASE64_ENCODE_FINAL_LENGTH+1)
170
171#endif /* _SQUID_BASE64_H */
172
void base64_encode_raw(char *dst, size_t length, const uint8_t *src)
Definition: base64.c:217
void base64_encode_group(char *dst, uint32_t group)
Definition: base64.c:223
void base64url_encode_init(struct base64_encode_ctx *ctx)
int base64_decode_single(struct base64_decode_ctx *ctx, uint8_t *dst, char src)
Definition: base64.c:82
void base64_encode_init(struct base64_encode_ctx *ctx)
Definition: base64.c:232
size_t base64_encode_single(struct base64_encode_ctx *ctx, char *dst, uint8_t src)
Definition: base64.c:240
size_t base64_encode_update(struct base64_encode_ctx *ctx, char *dst, size_t length, const uint8_t *src)
Definition: base64.c:265
void base64_decode_init(struct base64_decode_ctx *ctx)
Definition: base64.c:54
void base64url_decode_init(struct base64_decode_ctx *ctx)
size_t base64_encode_final(struct base64_encode_ctx *ctx, char *dst)
Definition: base64.c:308
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.c:129
int base64_decode_final(struct base64_decode_ctx *ctx)
Definition: base64.c:159
unsigned char bits
Definition: base64.h:126
unsigned short word
Definition: base64.h:125
const signed char * table
Definition: base64.h:124
unsigned char padding
Definition: base64.h:129
const char * alphabet
Definition: base64.h:70
unsigned short word
Definition: base64.h:71
unsigned char bits
Definition: base64.h:72

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors