VirtualBox

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

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

IPRT/Base64: Allow the end-of-line style to be specified via the fFlags parameter when encoding. bugref:9699

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 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.
146 * @param cbData The number of bytes to encode.
147 */
148RTDECL(size_t) RTBase64EncodedLength(size_t cbData);
149
150/**
151 * Calculates the length of the Base64 encoding of a given number of bytes of
152 * data produced by RTBase64EncodeEx() with the same @a fFlags.
153 *
154 * @returns The Base64 string length.
155 * @param cbData The number of bytes to encode.
156 * @param fFlags Flags, any combination of the RTBASE64_FLAGS \#defines.
157 */
158RTDECL(size_t) RTBase64EncodedLengthEx(size_t cbData, uint32_t fFlags);
159
160/**
161 * Encodes the specifed data into a Base64 string, the caller supplies the
162 * output buffer.
163 *
164 * This is equivalent to calling RTBase64EncodeEx() with no flags.
165 *
166 * @returns IRPT status code.
167 * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
168 * may contain an invalid Base64 string.
169 *
170 * @param pvData The data to encode.
171 * @param cbData The number of bytes to encode.
172 * @param pszBuf Where to put the Base64 string.
173 * @param cbBuf The size of the output buffer, including the terminator.
174 * @param pcchActual The actual number of characters returned.
175 */
176RTDECL(int) RTBase64Encode(const void *pvData, size_t cbData, char *pszBuf, size_t cbBuf, size_t *pcchActual);
177
178/**
179 * Encodes the specifed data into a Base64 string, the caller supplies the
180 * output buffer.
181 *
182 * @returns IRPT status code.
183 * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
184 * may contain an invalid Base64 string.
185 *
186 * @param pvData The data to encode.
187 * @param cbData The number of bytes to encode.
188 * @param fFlags Flags, any combination of the RTBASE64_FLAGS \#defines.
189 * @param pszBuf Where to put the Base64 string.
190 * @param cbBuf The size of the output buffer, including the terminator.
191 * @param pcchActual The actual number of characters returned.
192 */
193RTDECL(int) RTBase64EncodeEx(const void *pvData, size_t cbData, uint32_t fFlags,
194 char *pszBuf, size_t cbBuf, size_t *pcchActual);
195
196/** @} */
197
198RT_C_DECLS_END
199
200#endif /* !IPRT_INCLUDED_base64_h */
201
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