aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_Base64.h
blob: 7d60797acc9eeae4df38ad6a8097172f28bdcf09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_Base64.h
 * @ingroup inputoutput
 * @brief Encode/decode base64
 *
 * Encoding goes to one long line, no line breaks.
 * Decoding will accept CRLF linebreaks in the data and skip them.
 *
 * Following the language of RFC 4648, encoding proceeds in a "quantum" of
 * 3 bytes of plaintext to 4 bytes of encoded data.  Decoding goes in
 * a 4-byte quantum to 3-byte decoded data.
 *
 * If decoding fails (e.g. there's a non-base64 character), then the output
 * buffer is rewound to the starting position and a failure is indicated.
 *
 * Decoding using a 256 byte table.  Each byte of the 4-byte quantum is looked
 * up and if its a valid character -- it resolves to a value 0..63, then that
 * value is shifted to the right position in the output.  Values CR and LF have
 * the special token "_" in the table, which means "skip".  That token has value
 * ascii value 95, is we can detect it as outside base64.  Similarly, all the
 * invalid characters have the symbol "~", which is ascii 127.
 *
 */
#ifndef libparc_parc_Base64_h
#define libparc_parc_Base64_h

#include <parc/algol/parc_Buffer.h>
#include <parc/algol/parc_BufferComposer.h>

/**
 * Encode the plaintext buffer to base64, as per RFC 4648, Section 4.
 *
 * The base64 encoding is appended to `output` at the current position, and `output` is returned.
 *
 * @param [in,out] output The instance of {@link PARCBufferComposer} to which the encoded @plainText is appended
 * @param [in] plainText The text to encode and append to @p output
 *
 * @return  A pointer to the @p output
 *
 * Example:
 * @code
 * {
 *     <#example#>
 * }
 * @endcode
 *
 */
PARCBufferComposer *parcBase64_Encode(PARCBufferComposer *output, PARCBuffer *plainText);

/**
 * Encode the array to base64.
 *
 * @param [in,out] output The instance of {@link PARCBufferComposer} to which the encoded @p array is appended
 * @param [in] length The length of the Array to be encoded
 * @param array The array to be encoded and appended to @p output
 *
 * @return  A pointer to the @p output
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
PARCBufferComposer *parcBase64_EncodeArray(PARCBufferComposer *output, size_t length, const uint8_t *array);

/**
 * Base64 decode the @p encodedText and append the result to @p output,
 * which is returned by the function.
 *
 * If the @p encodedText cannot be base64 decoded,
 * @p output is reset to the starting position and the function returns NULL.
 *
 * @param [in,out] output The instance of {@link PARCBufferComposer} to which the decoded @p encodedText is appended
 * @param [in] encodedText The array to be decoded and appended to @p output
 *
 * @return  A pointer to the @p output, or NULL if the @p encodedText cannot be base64 decoded
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
PARCBufferComposer *parcBase64_Decode(PARCBufferComposer *output, PARCBuffer *encodedText);

/**
 * Base64 decode the string, using strlen() to get the length.
 *
 * @param [in,out] output The instance of {@link PARCBufferComposer} to which the decoded @p encodedText is appended
 * @param [in] encodedString The string to be decoded and appended to @p output
 *
 * @return  A pointer to the @p output, or NULL if the @p encodedString cannot be base64 decoded
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
PARCBufferComposer *parcBase64_DecodeString(PARCBufferComposer *output, const char *encodedString);

/**
 * Base64 decode the array.
 *
 * @param [in,out] output The instance of {@link PARCBufferComposer} to which the decoded @p array is appended
 * @param [in] length The size of the array.
 * @param [in] array The array to be decoded and appended to @p output
 *
 * @return  A pointer to the @p output, or NULL if the @p encodedString cannot be base64 decoded
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
PARCBufferComposer *parcBase64_DecodeArray(PARCBufferComposer *output, size_t length, const uint8_t *array);
#endif // libparc_parc_Base64_h