VirtualBox

source: vbox/trunk/include/iprt/base64.h@ 84286

Last change on this file since 84286 was 84286, checked in by vboxsync, 5 years ago

IPRT/base64: Added UTF-16 versions of the encoding functions. bugref:9224

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1/** @file
2 * IPRT - Base64, MIME content transfer encoding.
3 */
4
5/*
6 * Copyright (C) 2009-2020 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_base64_h
27#define IPRT_INCLUDED_base64_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/types.h>
33
34RT_C_DECLS_BEGIN
35
36/** @defgroup grp_rt_base64 RTBase64 - Base64, MIME content transfer encoding.
37 * @ingroup grp_rt
38 * @{
39 */
40
41/** @def RTBASE64_EOL_SIZE
42 * The size of the end-of-line marker. */
43#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
44# define RTBASE64_EOL_SIZE (sizeof("\r\n") - 1)
45#else
46# define RTBASE64_EOL_SIZE (sizeof("\n") - 1)
47#endif
48
49
50/** @name Flags for RTBase64EncodeEx() and RTBase64EncodedLengthEx().
51 * @{ */
52/** Insert line breaks into encoded string.
53 * The size of the end-of-line marker is that that of the host platform.
54 */
55#define RTBASE64_FLAGS_EOL_NATIVE UINT32_C(0) /**< Use native newlines. */
56#define RTBASE64_FLAGS_NO_LINE_BREAKS UINT32_C(1) /**< No newlines. */
57#define RTBASE64_FLAGS_EOL_LF UINT32_C(2) /**< Use UNIX-style newlines. */
58#define RTBASE64_FLAGS_EOL_CRLF UINT32_C(3) /**< Use DOS-style newlines. */
59#define RTBASE64_FLAGS_EOL_STYLE_MASK UINT32_C(3) /**< End-of-line style mask. */
60/** @} */
61
62
63/**
64 * Calculates the decoded data size for a Base64 encoded string.
65 *
66 * @returns The length in bytes. -1 if the encoding is bad.
67 *
68 * @param pszString The Base64 encoded string.
69 * @param ppszEnd If not NULL, this will point to the first char
70 * following the Base64 encoded text block. If
71 * NULL the entire string is assumed to be Base64.
72 */
73RTDECL(ssize_t) RTBase64DecodedSize(const char *pszString, char **ppszEnd);
74
75/**
76 * Calculates the decoded data size for a Base64 encoded string.
77 *
78 * @returns The length in bytes. -1 if the encoding is bad.
79 *
80 * @param pszString The Base64 encoded string.
81 * @param cchStringMax The max length to decode, use RTSTR_MAX if the
82 * length of @a pszString is not known and it is
83 * really zero terminated.
84 * @param ppszEnd If not NULL, this will point to the first char
85 * following the Base64 encoded text block. If
86 * NULL the entire string is assumed to be Base64.
87 */
88RTDECL(ssize_t) RTBase64DecodedSizeEx(const char *pszString, size_t cchStringMax, char **ppszEnd);
89
90/**
91 * Decodes a Base64 encoded string into the buffer supplied by the caller.
92 *
93 * @returns IPRT status code.
94 * @retval VERR_BUFFER_OVERFLOW if the buffer is too small. pcbActual will not
95 * be set, nor will ppszEnd.
96 * @retval VERR_INVALID_BASE64_ENCODING if the encoding is wrong.
97 *
98 * @param pszString The Base64 string. Whether the entire string or
99 * just the start of the string is in Base64 depends
100 * on whether ppszEnd is specified or not.
101 * @param pvData Where to store the decoded data.
102 * @param cbData The size of the output buffer that pvData points to.
103 * @param pcbActual Where to store the actual number of bytes returned.
104 * Optional.
105 * @param ppszEnd Indicates that the string may contain other stuff
106 * after the Base64 encoded data when not NULL. Will
107 * be set to point to the first char that's not part of
108 * the encoding. If NULL the entire string must be part
109 * of the Base64 encoded data.
110 */
111RTDECL(int) RTBase64Decode(const char *pszString, void *pvData, size_t cbData, size_t *pcbActual, char **ppszEnd);
112
113/**
114 * Decodes a Base64 encoded string into the buffer supplied by the caller.
115 *
116 * @returns IPRT status code.
117 * @retval VERR_BUFFER_OVERFLOW if the buffer is too small. pcbActual will not
118 * be set, nor will ppszEnd.
119 * @retval VERR_INVALID_BASE64_ENCODING if the encoding is wrong.
120 *
121 * @param pszString The Base64 string. Whether the entire string or
122 * just the start of the string is in Base64 depends
123 * on whether ppszEnd is specified or not.
124 * @param cchStringMax The max length to decode, use RTSTR_MAX if the
125 * length of @a pszString is not known and it is
126 * really zero terminated.
127 * @param pvData Where to store the decoded data.
128 * @param cbData The size of the output buffer that pvData points to.
129 * @param pcbActual Where to store the actual number of bytes returned.
130 * Optional.
131 * @param ppszEnd Indicates that the string may contain other stuff
132 * after the Base64 encoded data when not NULL. Will
133 * be set to point to the first char that's not part of
134 * the encoding. If NULL the entire string must be part
135 * of the Base64 encoded data.
136 */
137RTDECL(int) RTBase64DecodeEx(const char *pszString, size_t cchStringMax, void *pvData, size_t cbData,
138 size_t *pcbActual, char **ppszEnd);
139
140
141/**
142 * Calculates the length of the Base64 encoding of a given number of bytes of
143 * data produced by RTBase64Encode().
144 *
145 * @returns The Base64 string length, excluding the terminator.
146 * @param cbData The number of bytes to encode.
147 */
148RTDECL(size_t) RTBase64EncodedLength(size_t cbData);
149
150/**
151 * Calculates the UTF-16 length of the Base64 encoding of a given number of
152 * bytes of data produced by RTBase64EncodeUtf16().
153 *
154 * @returns The Base64 UTF-16 string length (in RTUTF16 units), excluding the
155 * terminator.
156 * @param cbData The number of bytes to encode.
157 */
158RTDECL(size_t) RTBase64EncodedUtf16Length(size_t cbData);
159
160/**
161 * Calculates the length of the Base64 encoding of a given number of bytes of
162 * data produced by RTBase64EncodeEx() with the same @a fFlags.
163 *
164 * @returns The Base64 string length, excluding the terminator.
165 * @param cbData The number of bytes to encode.
166 * @param fFlags Flags, any combination of the RTBASE64_FLAGS \#defines.
167 */
168RTDECL(size_t) RTBase64EncodedLengthEx(size_t cbData, uint32_t fFlags);
169
170/**
171 * Calculates the UTF-16 length of the Base64 encoding of a given number of
172 * bytes of data produced by RTBase64EncodeUtf16Ex() with the same @a fFlags.
173 *
174 * @returns The Base64 UTF-16 string length (in RTUTF16 units), excluding the
175 * terminator.
176 * @param cbData The number of bytes to encode.
177 * @param fFlags Flags, any combination of the RTBASE64_FLAGS \#defines.
178 */
179RTDECL(size_t) RTBase64EncodedUtf16LengthEx(size_t cbData, uint32_t fFlags);
180
181/**
182 * Encodes the specifed data into a Base64 string, the caller supplies the
183 * output buffer.
184 *
185 * This is equivalent to calling RTBase64EncodeEx() with no flags.
186 *
187 * @returns IRPT status code.
188 * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
189 * may contain an invalid Base64 string.
190 *
191 * @param pvData The data to encode.
192 * @param cbData The number of bytes to encode.
193 * @param pszBuf Where to put the Base64 string.
194 * @param cbBuf The size of the output buffer, including the terminator.
195 * @param pcchActual The actual number of characters returned.
196 */
197RTDECL(int) RTBase64Encode(const void *pvData, size_t cbData, char *pszBuf, size_t cbBuf, size_t *pcchActual);
198
199/**
200 * Encodes the specifed data into a Base64 UTF-16 string, the caller supplies
201 * the output buffer.
202 *
203 * This is equivalent to calling RTBase64EncodeUtf16Ex() with no flags.
204 *
205 * @returns IRPT status code.
206 * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
207 * may contain an invalid Base64 string.
208 *
209 * @param pvData The data to encode.
210 * @param cbData The number of bytes to encode.
211 * @param pwszBuf Where to put the Base64 UTF-16 string.
212 * @param cwcBuf The size of the output buffer in RTUTF16 units,
213 * including the terminator.
214 * @param pcwcActual The actual number of characters returned (excluding the
215 * terminator). Optional.
216 */
217RTDECL(int) RTBase64EncodeUtf16(const void *pvData, size_t cbData, PRTUTF16 pwszBuf, size_t cwcBuf, size_t *pcwcActual);
218
219/**
220 * Encodes the specifed data into a Base64 string, the caller supplies the
221 * output buffer.
222 *
223 * @returns IRPT status code.
224 * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
225 * may contain an invalid Base64 string.
226 *
227 * @param pvData The data to encode.
228 * @param cbData The number of bytes to encode.
229 * @param fFlags Flags, any combination of the RTBASE64_FLAGS \#defines.
230 * @param pszBuf Where to put the Base64 string.
231 * @param cbBuf The size of the output buffer, including the terminator.
232 * @param pcchActual The actual number of characters returned (excluding the
233 * terminator). Optional.
234 */
235RTDECL(int) RTBase64EncodeEx(const void *pvData, size_t cbData, uint32_t fFlags,
236 char *pszBuf, size_t cbBuf, size_t *pcchActual);
237
238/**
239 * Encodes the specifed data into a Base64 UTF-16 string, the caller supplies
240 * the output buffer.
241 *
242 * @returns IRPT status code.
243 * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
244 * may contain an invalid Base64 string.
245 *
246 * @param pvData The data to encode.
247 * @param cbData The number of bytes to encode.
248 * @param fFlags Flags, any combination of the RTBASE64_FLAGS \#defines.
249 * @param pwszBuf Where to put the Base64 UTF-16 string.
250 * @param cwcBuf The size of the output buffer in RTUTF16 units,
251 * including the terminator.
252 * @param pcwcActual The actual number of characters returned (excluding the
253 * terminator). Optional.
254 */
255RTDECL(int) RTBase64EncodeUtf16Ex(const void *pvData, size_t cbData, uint32_t fFlags,
256 PRTUTF16 pwszBuf, size_t cwcBuf, size_t *pcwcActual);
257
258
259/** @} */
260
261RT_C_DECLS_END
262
263#endif /* !IPRT_INCLUDED_base64_h */
264
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette