Mstdlib-1.24.0
Binary Conversion

Macros

#define M_BINCODEC_PEM_LINE_LEN   64
 

Enumerations

enum  M_bincodec_codec_t {
  M_BINCODEC_BASE64 ,
  M_BINCODEC_HEX ,
  M_BINCODEC_BASE64ORHEX ,
  M_BINCODEC_BASE32
}
 

Functions

size_t M_bincodec_encode_size (size_t inLen, size_t wrap, M_bincodec_codec_t codec)
 
char * M_bincodec_encode_alloc (const M_uint8 *in, size_t inLen, size_t wrap, M_bincodec_codec_t codec) M_WARN_UNUSED_RESULT M_MALLOC
 
size_t M_bincodec_encode (char *out, size_t outLen, const M_uint8 *in, size_t inLen, size_t wrap, M_bincodec_codec_t codec)
 
size_t M_bincodec_decode_size (size_t inLen, M_bincodec_codec_t codec)
 
M_uint8 * M_bincodec_decode_alloc (const char *in, size_t inLen, size_t *outLen, M_bincodec_codec_t codec) M_WARN_UNUSED_RESULT M_MALLOC
 
char * M_bincodec_decode_str_alloc (const char *in, M_bincodec_codec_t codec)
 
size_t M_bincodec_decode (M_uint8 *out, size_t outLen, const char *in, size_t inLen, M_bincodec_codec_t codec)
 
char * M_bincodec_convert_alloc (const char *in, size_t inLen, size_t wrap, M_bincodec_codec_t inCodec, M_bincodec_codec_t outCodec) M_WARN_UNUSED_RESULT M_MALLOC
 
size_t M_bincodec_convert (char *out, size_t outLen, size_t wrap, M_bincodec_codec_t outCodec, const char *in, size_t inLen, M_bincodec_codec_t inCodec)
 

Detailed Description

Text to binary and binary to text conversion. Supports conversion of binary to or from base64 or hex.

Example:

const char *data = "abcd";
char *out[16];
char *odata;
M_bincodec_encode(out, sizeof(out), (M_uint8 *)data, M_str_len(data), 0, M_BINCODEC_HEX);
M_printf("hex='%s'\n", out);
M_bincodec_encode(out, sizeof(out), (M_uint8 *)data, M_str_len(data), 0, M_BINCODEC_BASE64);
M_printf("b64='%s'\n", out);
odata = (char *)M_bincodec_decode_alloc(out, M_str_len(out), NULL, M_BINCODEC_BASE64ORHEX);
M_printf("original='%s'\n", odata);
M_free(odata);
M_uint8 * M_bincodec_decode_alloc(const char *in, size_t inLen, size_t *outLen, M_bincodec_codec_t codec) M_WARN_UNUSED_RESULT M_MALLOC
size_t M_bincodec_encode(char *out, size_t outLen, const M_uint8 *in, size_t inLen, size_t wrap, M_bincodec_codec_t codec)
@ M_BINCODEC_BASE64ORHEX
Definition: m_bincodec.h:70
@ M_BINCODEC_HEX
Definition: m_bincodec.h:69
@ M_BINCODEC_BASE64
Definition: m_bincodec.h:68
ssize_t M_printf(const char *fmt,...)
void M_free(void *ptr) M_FREE(1)
size_t M_str_len(const char *s) M_WARN_UNUSED_RESULT

Macro Definition Documentation

◆ M_BINCODEC_PEM_LINE_LEN

#define M_BINCODEC_PEM_LINE_LEN   64

Enumeration Type Documentation

◆ M_bincodec_codec_t

Binary conversion types.

Enumerator
M_BINCODEC_BASE64 

Base64 encoding.

M_BINCODEC_HEX 

Hex encoding.

M_BINCODEC_BASE64ORHEX 

Auto detected between base64 and hex encoding.

M_BINCODEC_BASE32 

Base32 encoding.

Function Documentation

◆ M_bincodec_encode_size()

size_t M_bincodec_encode_size ( size_t  inLen,
size_t  wrap,
M_bincodec_codec_t  codec 
)

The maximum number of bytes necessary to encode using the specified codec.

Parameters
[in]inLenNumber of bytes that will be passed to the encoder.
[in]wrapThe maximum length of a given line. Longer lines will be split with a new line. Pass 0 if line splitting is not desired.
[in]codecThe binary codec that will be used for encoding.
Returns
The maximum number of bytes needed to store the data after encoding.

◆ M_bincodec_encode_alloc()

char * M_bincodec_encode_alloc ( const M_uint8 *  in,
size_t  inLen,
size_t  wrap,
M_bincodec_codec_t  codec 
)

Encodes data passed into it using the specified binary codec.

Parameters
[in]inThe binary data to encode.
[in]inLenThe length of the input data.
[in]wrapThe maximum length of a given line. Longer lines will be split with a new line. Pass 0 if line splitting is not desired.
[in]codecThe binary codec to use for encoding.
Returns
A new NULL terminated string with the encoded data. Otherwise NULL on error.
See also
M_free

◆ M_bincodec_encode()

size_t M_bincodec_encode ( char *  out,
size_t  outLen,
const M_uint8 *  in,
size_t  inLen,
size_t  wrap,
M_bincodec_codec_t  codec 
)

Encodes data passed into it using the specified binary codec.

Parameters
[out]outA buffer large enough to hold the encoded data.
[in]outLenThe length of the output buffer.
[in]inThe binary data to encode.
[in]inLenThe length of the input data.
[in]wrapThe maximum length of a given line. Longer lines will be split with a new line. Pass 0 if line splitting is not desired.
[in]codecThe binary codec to use for encoding.
Returns
The number of bytes written into the output buffer.

◆ M_bincodec_decode_size()

size_t M_bincodec_decode_size ( size_t  inLen,
M_bincodec_codec_t  codec 
)

The maximum number of bytes necessary to encode using the specified codec.

Parameters
[in]inLenNumber of bytes that will be passed to the decoder.
[in]codecThe binary codec that will be used for decoding.
Returns
The maximum number of bytes needed to store the data after decoding.

◆ M_bincodec_decode_alloc()

M_uint8 * M_bincodec_decode_alloc ( const char *  in,
size_t  inLen,
size_t *  outLen,
M_bincodec_codec_t  codec 
)

Decodes data passed into it using the specified binary codec.

Parameters
[in]inThe string data to decode.
[in]inLenThe length of the input data.
[out]outLenThe length ouf the buffer.
[in]codecThe binary codec to use for decoding.
Returns
A new array with the decoded data. Otherwise NULL on error.
See also
M_free

◆ M_bincodec_decode_str_alloc()

char * M_bincodec_decode_str_alloc ( const char *  in,
M_bincodec_codec_t  codec 
)

Convenience function for validating decoded data is a string. Some protocols may encode string data even if it is not necessary, so this function helps validate that data is really in string form to prevent issues.

Parameters
[in]inThe string data to decode.
[in]codecThe binary codec to use for decoding.
Returns
A new buffer with decoded data, otherwise NULL on error.

◆ M_bincodec_decode()

size_t M_bincodec_decode ( M_uint8 *  out,
size_t  outLen,
const char *  in,
size_t  inLen,
M_bincodec_codec_t  codec 
)

Decodes data passed into it using the specified binary codec.

Parameters
[out]outA buffer large enought to hold the decoded data.
[in]outLenThe length of the output buffer.
[in]inThe string data to decode.
[in]inLenThe length of the input data.
[in]codecThe binary codec to use for decoding.
Returns
The number of bytes written into the output buffer, or 0 on error.

◆ M_bincodec_convert_alloc()

char * M_bincodec_convert_alloc ( const char *  in,
size_t  inLen,
size_t  wrap,
M_bincodec_codec_t  inCodec,
M_bincodec_codec_t  outCodec 
)

Convert a string from one binary encoding to another.

A conversion will always be performed even when using the same input and output codecs.

Parameters
[in]inThe data to convert.
[in]inLenThe length of the input data.
[in]wrapThe maximum length of a given line. Longer lines will be split with a new line. Pass 0 if line splitting is not desired.
[in]inCodecThe format the input data is encoded using.
[in]outCodecThe output format to convert into.
Returns
A new NULL terminated string with the converted data. Otherwise NULL on error.
See also
M_free

◆ M_bincodec_convert()

size_t M_bincodec_convert ( char *  out,
size_t  outLen,
size_t  wrap,
M_bincodec_codec_t  outCodec,
const char *  in,
size_t  inLen,
M_bincodec_codec_t  inCodec 
)

Convert a string from one binary encoding to another.

A conversion will always be performed even when using the same input and output codecs.

Parameters
[out]outA buffer large enought to hold the converted data.
[in]outLenThe length of the output buffer.
[in]wrapThe maximum length of a given line. Longer lines will be split with a new line. Pass 0 if line splitting is not desired.
[in]outCodecThe output format to convert into.
[in]inThe data to convert.
[in]inLenThe length of the input data.
[in]inCodecThe format the input data is encoded using.
Returns
The number of bytes written into the output buffer.