b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync/** @file
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * IPRT - Base64, MIME content transfer encoding.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync */
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync/*
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2009-2010 Oracle Corporation
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * available from http://www.virtualbox.org. This file is free software;
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * General Public License (GPL) as published by the Free Software
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * The contents of this file may alternatively be used under the terms
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * of the Common Development and Distribution License Version 1.0
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * VirtualBox OSE distribution, in which case the provisions of the
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * CDDL are applicable instead of those of the GPL.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * You may elect to license modified versions of this file under the
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * terms and conditions of either the GPL or the CDDL or both.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync */
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync#ifndef ___iprt_base64_h
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync#define ___iprt_base64_h
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync#include <iprt/types.h>
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsyncRT_C_DECLS_BEGIN
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync/** @defgroup grp_rt_base64 RTBase64 - Base64, MIME content transfer encoding.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @ingroup grp_rt
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @{
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync */
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync/** @def RTBASE64_EOL_SIZE
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * The size of the end-of-line marker. */
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync# define RTBASE64_EOL_SIZE (sizeof("\r\n") - 1)
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync#else
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync# define RTBASE64_EOL_SIZE (sizeof("\n") - 1)
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync#endif
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync/**
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * Calculates the decoded data size for a Base64 encoded string.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @returns The length in bytes. -1 if the encoding is bad.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszString The Base64 encoded string.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param ppszEnd If not NULL, this will point to the first char
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * following the Base64 encoded text block. If
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NULL the entire string is assumed to be Base64.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync */
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsyncRTDECL(ssize_t) RTBase64DecodedSize(const char *pszString, char **ppszEnd);
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/**
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Calculates the decoded data size for a Base64 encoded string.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns The length in bytes. -1 if the encoding is bad.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszString The Base64 encoded string.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param cchStringMax The max length to decode, use RTSTR_MAX if the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * length of @a pszString is not known and it is
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * really zero terminated.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param ppszEnd If not NULL, this will point to the first char
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * following the Base64 encoded text block. If
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * NULL the entire string is assumed to be Base64.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(ssize_t) RTBase64DecodedSizeEx(const char *pszString, size_t cchStringMax, char **ppszEnd);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync/**
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * Decodes a Base64 encoded string into the buffer supplied by the caller.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @returns IPRT status code.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @retval VERR_BUFFER_OVERFLOW if the buffer is too small. pcbActual will not
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * be set, nor will ppszEnd.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @retval VERR_INVALID_BASE64_ENCODING if the encoding is wrong.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @param pszString The Base64 string. Whether the entire string or
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * just the start of the string is in Base64 depends
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * on whether ppszEnd is specified or not.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @param pvData Where to store the decoded data.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @param cbData The size of the output buffer that pvData points to.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @param pcbActual Where to store the actual number of bytes returned.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * Optional.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * @param ppszEnd Indicates that the string may contain other stuff
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * after the Base64 encoded data when not NULL. Will
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * be set to point to the first char that's not part of
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * the encoding. If NULL the entire string must be part
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * of the Base64 encoded data.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync */
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsyncRTDECL(int) RTBase64Decode(const char *pszString, void *pvData, size_t cbData, size_t *pcbActual, char **ppszEnd);
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/**
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Decodes a Base64 encoded string into the buffer supplied by the caller.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns IPRT status code.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @retval VERR_BUFFER_OVERFLOW if the buffer is too small. pcbActual will not
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * be set, nor will ppszEnd.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @retval VERR_INVALID_BASE64_ENCODING if the encoding is wrong.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pszString The Base64 string. Whether the entire string or
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * just the start of the string is in Base64 depends
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * on whether ppszEnd is specified or not.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param cchStringMax The max length to decode, use RTSTR_MAX if the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * length of @a pszString is not known and it is
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * really zero terminated.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pvData Where to store the decoded data.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param cbData The size of the output buffer that pvData points to.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pcbActual Where to store the actual number of bytes returned.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Optional.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param ppszEnd Indicates that the string may contain other stuff
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * after the Base64 encoded data when not NULL. Will
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * be set to point to the first char that's not part of
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * the encoding. If NULL the entire string must be part
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * of the Base64 encoded data.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBase64DecodeEx(const char *pszString, size_t cchStringMax, void *pvData, size_t cbData,
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync size_t *pcbActual, char **ppszEnd);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync/**
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * Calculates the length of the Base64 encoding of a given number of bytes of
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * data.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * This will assume line breaks every 64 chars. A RTBase64EncodedLengthEx
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * function can be added if closer control over the output is found to be
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * required.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @returns The Base64 string length.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @param cbData The number of bytes to encode.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync */
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsyncRTDECL(size_t) RTBase64EncodedLength(size_t cbData);
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync/**
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * Encodes the specifed data into a Base64 string, the caller supplies the
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * output buffer.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * This will make the same assumptions about line breaks and EOL size as
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * RTBase64EncodedLength() does. A RTBase64EncodeEx function can be added if
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * more strict control over the output formatting is found necessary.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @returns IRPT status code.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * may contain an invalid Base64 string.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync *
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @param pvData The data to encode.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync * @param cbData The number of bytes to encode.
df5f1b4303be82a000284349e9392beb9f1b6f11vboxsync * @param pszBuf Where to put the Base64 string.
df5f1b4303be82a000284349e9392beb9f1b6f11vboxsync * @param cbBuf The size of the output buffer, including the terminator.
df5f1b4303be82a000284349e9392beb9f1b6f11vboxsync * @param pcchActual The actual number of characters returned.
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync */
df5f1b4303be82a000284349e9392beb9f1b6f11vboxsyncRTDECL(int) RTBase64Encode(const void *pvData, size_t cbData, char *pszBuf, size_t cbBuf, size_t *pcchActual);
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync/** @} */
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsyncRT_C_DECLS_END
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync#endif
b0ab7e3ff45cca3c9755432dde464562cdde61f0vboxsync