Botan 3.3.0
Crypto and TLS for C&
base64.h
Go to the documentation of this file.
1/*
2* Base64 Encoding and Decoding
3* (C) 2010 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_BASE64_CODEC_H_
9#define BOTAN_BASE64_CODEC_H_
10
11#include <botan/secmem.h>
12#include <span>
13#include <string>
14#include <string_view>
15
16namespace Botan {
17
18/**
19* Perform base64 encoding
20* @param output an array of at least base64_encode_max_output bytes
21* @param input is some binary data
22* @param input_length length of input in bytes
23* @param input_consumed is an output parameter which says how many
24* bytes of input were actually consumed. If less than
25* input_length, then the range input[consumed:length]
26* should be passed in later along with more input.
27* @param final_inputs true iff this is the last input, in which case
28 padding chars will be applied if needed
29* @return number of bytes written to output
30*/
31size_t BOTAN_PUBLIC_API(2, 0)
32 base64_encode(char output[], const uint8_t input[], size_t input_length, size_t& input_consumed, bool final_inputs);
33
34/**
35* Perform base64 encoding
36* @param input some input
37* @param input_length length of input in bytes
38* @return base64adecimal representation of input
39*/
40std::string BOTAN_PUBLIC_API(2, 0) base64_encode(const uint8_t input[], size_t input_length);
41
42/**
43* Perform base64 encoding
44* @param input some input
45* @return base64adecimal representation of input
46*/
47inline std::string base64_encode(std::span<const uint8_t> input) {
48 return base64_encode(input.data(), input.size());
49}
50
51/**
52* Perform base64 decoding
53* @param output an array of at least base64_decode_max_output bytes
54* @param input some base64 input
55* @param input_length length of input in bytes
56* @param input_consumed is an output parameter which says how many
57* bytes of input were actually consumed. If less than
58* input_length, then the range input[consumed:length]
59* should be passed in later along with more input.
60* @param final_inputs true iff this is the last input, in which case
61 padding is allowed
62* @param ignore_ws ignore whitespace on input; if false, throw an
63 exception if whitespace is encountered
64* @return number of bytes written to output
65*/
66size_t BOTAN_PUBLIC_API(2, 0) base64_decode(uint8_t output[],
67 const char input[],
68 size_t input_length,
69 size_t& input_consumed,
70 bool final_inputs,
71 bool ignore_ws = true);
72
73/**
74* Perform base64 decoding
75* @param output an array of at least base64_decode_max_output bytes
76* @param input some base64 input
77* @param input_length length of input in bytes
78* @param ignore_ws ignore whitespace on input; if false, throw an
79 exception if whitespace is encountered
80* @return number of bytes written to output
81*/
82size_t BOTAN_PUBLIC_API(2, 0)
83 base64_decode(uint8_t output[], const char input[], size_t input_length, bool ignore_ws = true);
84
85/**
86* Perform base64 decoding
87* @param output an array of at least base64_decode_max_output bytes
88* @param input some base64 input
89* @param ignore_ws ignore whitespace on input; if false, throw an
90 exception if whitespace is encountered
91* @return number of bytes written to output
92*/
93size_t BOTAN_PUBLIC_API(3, 0) base64_decode(uint8_t output[], std::string_view input, bool ignore_ws = true);
94
95/**
96* Perform base64 decoding
97* @param output a contiguous byte buffer of at least base64_decode_max_output bytes
98* @param input some base64 input
99* @param ignore_ws ignore whitespace on input; if false, throw an
100 exception if whitespace is encountered
101* @return number of bytes written to output
102*/
103size_t BOTAN_PUBLIC_API(3, 0) base64_decode(std::span<uint8_t> output, std::string_view input, bool ignore_ws = true);
104
105/**
106* Perform base64 decoding
107* @param input some base64 input
108* @param input_length the length of input in bytes
109* @param ignore_ws ignore whitespace on input; if false, throw an
110 exception if whitespace is encountered
111* @return decoded base64 output
112*/
113secure_vector<uint8_t> BOTAN_PUBLIC_API(2, 0)
114 base64_decode(const char input[], size_t input_length, bool ignore_ws = true);
115
116/**
117* Perform base64 decoding
118* @param input some base64 input
119* @param ignore_ws ignore whitespace on input; if false, throw an
120 exception if whitespace is encountered
121* @return decoded base64 output
122*/
123secure_vector<uint8_t> BOTAN_PUBLIC_API(3, 0) base64_decode(std::string_view input, bool ignore_ws = true);
124
125/**
126* Calculate the size of output buffer for base64_encode
127* @param input_length the length of input in bytes
128* @return the size of output buffer in bytes
129*/
130size_t BOTAN_PUBLIC_API(2, 1) base64_encode_max_output(size_t input_length);
131
132/**
133* Calculate the size of output buffer for base64_decode
134* @param input_length the length of input in bytes
135* @return the size of output buffer in bytes
136*/
137size_t BOTAN_PUBLIC_API(2, 1) base64_decode_max_output(size_t input_length);
138
139} // namespace Botan
140
141#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base64.cpp:146
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition base64.cpp:154
size_t base64_encode_max_output(size_t input_length)
Definition base64.cpp:182
size_t base64_decode_max_output(size_t input_length)
Definition base64.cpp:186
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61