NCBI C++ ToolKit
Macros | Functions | Variables
ncbi_base64.c File Reference
#include "ncbi_base64.h"
+ Include dependency graph for ncbi_base64.c:

Go to the source code of this file.

Go to the SVN repository for this file.

Macros

#define XLAT_BASE64_CHAR(var)
 

Functions

void BASE64_Encode (const void *src_buf, size_t src_size, size_t *src_read, void *dst_buf, size_t dst_size, size_t *dst_written, size_t *line_len)
 BASE64-encode up to "src_size" symbols(bytes) from buffer "src_buf". More...
 
int BASE64_Decode (const void *src_buf, size_t src_size, size_t *src_read, void *dst_buf, size_t dst_size, size_t *dst_written)
 BASE64-decode up to "src_size" symbols(bytes) from buffer "src_buf". More...
 
EBase64_Result base64url_encode (const void *src_buf, size_t src_size, void *dst_buf, size_t dst_size, size_t *output_len)
 Encode binary data using the base64url variant of the Base64 family of encodings. More...
 
EBase64_Result base64url_decode (const void *src_buf, size_t src_size, void *dst_buf, size_t dst_size, size_t *output_len)
 Decode the base64url-encoded src_buf and store the result in dst_buf. More...
 

Variables

static const unsigned char base64url_alphabet []
 
static const unsigned char base64url_decode_table [256]
 

Macro Definition Documentation

◆ XLAT_BASE64_CHAR

#define XLAT_BASE64_CHAR (   var)
Value:
if ((signed char)(var = base64url_decode_table[*src++]) < 0) \
static const unsigned char base64url_decode_table[256]
Definition: ncbi_base64.c:255
@ eBase64_InvalidInput
Input contains characters outside alphabet.
Definition: ncbi_base64.h:102

Definition at line 291 of file ncbi_base64.c.

Function Documentation

◆ BASE64_Decode()

int BASE64_Decode ( const void *  src_buf,
size_t  src_size,
size_t *  src_read,
void *  dst_buf,
size_t  dst_size,
size_t *  dst_written 
)

BASE64-decode up to "src_size" symbols(bytes) from buffer "src_buf".

Write the decoded data to buffer "dst_buf", but no more than "dst_size" bytes. Assign "*src_read" with the # of bytes successfully decoded from "src_buf". Assign "*dst_written" with the # of bytes written to buffer "dst_buf". Return FALSE (0) only if this call cannot decode anything at all. The destination buffer size, as a worst case, equal to the source size will accommodate the entire output. As a rule, each 4 bytes of source (line breaks ignored) get converted into 3 bytes of decoded output.

Definition at line 109 of file ncbi_base64.c.

References _ASSERT, i, and ok.

◆ BASE64_Encode()

void BASE64_Encode ( const void *  src_buf,
size_t  src_size,
size_t *  src_read,
void *  dst_buf,
size_t  dst_size,
size_t *  dst_written,
size_t *  line_len 
)

BASE64-encode up to "src_size" symbols(bytes) from buffer "src_buf".

Write the encoded data to buffer "dst_buf", but no more than "dst_size" bytes. Assign "*src_read" with the # of bytes successfully encoded from "src_buf". Assign "*dst_written" with the # of bytes written to buffer "dst_buf". Resulting lines will not exceed "*line_len" (or the standard default if "line_len" is NULL) bytes; *line_len == 0 disables the line breaks. To estimate required destination buffer size, you can take into account that BASE64 encoding converts every 3 bytes of source into 4 bytes of encoded output, not including the additional line breaks ('
').

Definition at line 37 of file ncbi_base64.c.

References _ASSERT, i, and len.

◆ base64url_decode()

EBase64_Result base64url_decode ( const void *  src_buf,
size_t  src_size,
void *  dst_buf,
size_t  dst_size,
size_t *  output_len 
)

Decode the base64url-encoded src_buf and store the result in dst_buf.

This implementation reports the padding character ('=') as an error, so those symbols must be removed before calling this function. When called with a dst_size of zero, this function simply returns the required destination buffer size in output_len. Large inputs can be processed incrementally by dividing the input into chunks and calling this function for each chunk. Important: When large inputs are incrementally encoded in this way, the source buffer size for all but the last chunk must be a multiple of 4 bytes. For information about the base64url variant of the Base64 family of encodings, please refer to RFC 4648.

Parameters
src_bufBase64url-encoded data to decode. Ignored if dst_size is zero.
src_sizeThe size of src_buf.
dst_bufOutput buffer. Ignored if dst_size is zero.
dst_sizeThe size of the output buffer or zero.
output_lenVariable for storing the length of the decoded string. If more space than dst_size bytes is required, dst_buf is not written and the function returns eBase64_BufferTooSmall. Can be NULL.
Returns
eBase64_OK if the input string has been successfully decoded; eBase64_BufferTooSmall if the input buffer is too small to store the decoded string; eBase64_InvalidInput if there's a character in src_buf that's not from the base64url alphabet (alphanumeric, underscore, and dash characters).

Definition at line 295 of file ncbi_base64.c.

References eBase64_BufferTooSmall, eBase64_InvalidInput, eBase64_OK, NULL, result_len, and XLAT_BASE64_CHAR.

◆ base64url_encode()

EBase64_Result base64url_encode ( const void *  src_buf,
size_t  src_size,
void *  dst_buf,
size_t  dst_size,
size_t *  output_len 
)

Encode binary data using the base64url variant of the Base64 family of encodings.

The input data is read from src_buf and the result is stored in dst_buf. This implementation does not pad the output with '='. When called with a dst_size of zero, this function simply returns the required destination buffer size in output_len. Large inputs can be processed incrementally by dividing the input into chunks and calling this function for each chunk. Important: When large inputs are incrementally encoded in this way, the source buffer size for all but the last chunk must be a multiple of 3 bytes. For information about the base64url, please refer to RFC 4648.

Parameters
src_bufData to encode. Ignored if dst_size is zero.
src_sizeThe size of the input data.
dst_bufOutput buffer. Ignored if dst_size is zero.
dst_sizeThe size of the output buffer or zero.
output_lenVariable for storing the length of the encoded string. If it turns out to be greater than dst_size, dst_buf is not written and the function returns eBase64_BufferTooSmall. Can be NULL.
Returns
eBase64_OK if the input string has been successfully encoded; eBase64_BufferTooSmall if the input buffer is too small to store the encoded string.

Definition at line 213 of file ncbi_base64.c.

References base64url_alphabet, eBase64_BufferTooSmall, eBase64_OK, NULL, offset, and result_len.

Variable Documentation

◆ base64url_alphabet

const unsigned char base64url_alphabet[]
static
Initial value:
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"

Definition at line 210 of file ncbi_base64.c.

Referenced by base64url_encode().

◆ base64url_decode_table

const unsigned char base64url_decode_table[256]
static

Definition at line 255 of file ncbi_base64.c.

Modified on Mon May 27 04:36:24 2024 by modify_doxy.py rev. 669887