BSL v1.0.0 - 16.g9d98179
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
text_util.c File Reference
#include "text_util.h"
#include <BPSecLib_Private.h>
#include <string.h>
#include <strings.h>
#include <inttypes.h>
#include <stdlib.h>
#include <math.h>
+ Include dependency graph for text_util.c:

Functions

static int take_hex_1byte (uint8_t *out, const char **curs, const char *end)
 
static int take_hex_2byte (uint16_t *out, const char **curs, const char *end)
 
int mock_bpa_uri_percent_encode (m_string_t out, const m_string_t in, const char *safe)
 Encode to URI percent-encoding text form.
 
int mock_bpa_uri_percent_decode (m_string_t out, const m_string_t in)
 Decode from URI percent-encoding text form.
 
int mock_bpa_slash_escape (m_string_t out, const m_string_t in, const char quote)
 Escape backslashes in tstr or bstr text form.
 
int mock_bpa_slash_unescape (m_string_t out, const m_string_t in)
 Unescape backslashes in tstr/bstr text form.
 
static void strip_chars (m_string_t out, const char *in, size_t in_len, const char *chars)
 
void mock_bpa_strip_space (m_string_t out, const char *in, size_t in_len)
 Remove whitespace characters from a text string.
 
void mock_bpa_string_tolower (m_string_t out)
 Convert a text string to lowercase.
 
void mock_bpa_string_toupper (m_string_t out)
 Convert a text string to uppercase.
 
int mock_bpa_base16_encode (m_string_t out, const m_bstring_t in, bool uppercase)
 Encode to base16 text form.
 
static int base16_decode_char (uint8_t chr)
 Decode a single character.
 
int mock_bpa_base16_decode (m_bstring_t out, const m_string_t in)
 Decode base16 text form.
 
int mock_bpa_base64_encode (m_string_t out, const m_bstring_t in, bool useurl, bool usepad)
 Encode base64 and base64url text forms.
 
static int base64_decode_char (uint8_t chr)
 Decode a single character.
 
int mock_bpa_base64_decode (m_bstring_t out, const m_string_t in)
 Decode base64 and base64url text forms.
 

Variables

static const char * unreserved = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~"
 Set of unreserved characters from Section 2.3 of RFC 3986 [1].
 
static const size_t base16_decode_lim = 0x80
 Size of the base16_decode_table.
 
static const int base16_decode_table [0x80]
 Decode table for base16.
 
static const char * base64_alphabet
 
static const char * base64url_alphabet
 
static const size_t base64_decode_lim = 0x80
 Size of the base16_decode_table.
 
static const int base64_decode_table [0x80]
 Decode table for base64 and base64uri.
 

Function Documentation

◆ base16_decode_char()

static int base16_decode_char ( uint8_t  chr)
static

Decode a single character.

Parameters
chrThe character to decode.
Returns
If positive, the decoded value. -1 to indicate error. -2 to indicate whitespace.

References base16_decode_lim, and base16_decode_table.

Referenced by mock_bpa_base16_decode().

◆ base64_decode_char()

static int base64_decode_char ( uint8_t  chr)
static

Decode a single character.

Parameters
chrThe character to decode.
Returns
If positive, the decoded value. -1 to indicate error. -2 to indicate whitespace.

References base64_decode_lim, and base64_decode_table.

Referenced by mock_bpa_base64_decode().

◆ mock_bpa_base16_decode()

int mock_bpa_base16_decode ( m_bstring_t  out,
const m_string_t  in 
)

Decode base16 text form.

This is defined in Section 8 of RFC 4648 [15].

Parameters
[out]outThe output buffer, which will be sized to its data.
[in]inThe input buffer to read, which must be null terminated. Whitespace in the input must have already been removed with strip_space().
Returns
Zero upon success.

References base16_decode_char(), and BSL_CHKERR1.

Referenced by test_mock_bpa_base16_decode_invalid(), and test_mock_bpa_base16_decode_valid().

◆ mock_bpa_base16_encode()

int mock_bpa_base16_encode ( m_string_t  out,
const m_bstring_t  in,
bool  uppercase 
)

Encode to base16 text form.

This is defined in Section 8 of RFC 4648 [15].

Parameters
[out]outThe output buffer, which will be appended to.
[in]inThe input buffer to read.
uppercaseTrue to use upper-case letters, false to use lower-case.
Returns
Zero upon success.

Referenced by test_mock_bpa_base16_encode().

◆ mock_bpa_base64_decode()

int mock_bpa_base64_decode ( m_bstring_t  out,
const m_string_t  in 
)

Decode base64 and base64url text forms.

These is defined in Section 4 and 5 of RFC 4648 [15].

Parameters
[out]outThe output buffer, which will be sized to its data.
[in]inThe input buffer to read, which must be null terminated. Whitespace in the input must have already been removed with strip_space().
Returns
Zero upon success.

References base64_decode_char(), and BSL_CHKERR1.

Referenced by mock_bpa_key_registry_init(), test_mock_bpa_base64_decode_invalid(), and test_mock_bpa_base64_decode_valid().

◆ mock_bpa_base64_encode()

int mock_bpa_base64_encode ( m_string_t  out,
const m_bstring_t  in,
bool  useurl,
bool  usepad 
)

Encode base64 and base64url text forms.

These is defined in Section 4 and 5 of RFC 4648 [15].

Parameters
[out]outThe output buffer, which will be appended to.
[in]inThe input buffer to read.
useurlTrue to use the base64url alphabet, false to use the base64 alphabet.
usepadTrue to include padding characters (=), false to not use padding.
Returns
Zero upon success.

References base64_alphabet, and base64url_alphabet.

Referenced by TEST_CASE().

◆ mock_bpa_slash_escape()

int mock_bpa_slash_escape ( m_string_t  out,
const m_string_t  in,
const char  quote 
)

Escape backslashes in tstr or bstr text form.

This is defined in Section G.2 of RFC 8610 [2] and Section 7 of RFC 8259 [5].

Parameters
[out]outThe output buffer, which will be appended to.
inThe input buffer to read, which must be null terminated.
quoteThe character used to quote the string.
Returns
Zero upon success.

References BSL_CHKERR1.

Referenced by test_mock_bpa_slash_escape_valid().

◆ mock_bpa_slash_unescape()

int mock_bpa_slash_unescape ( m_string_t  out,
const m_string_t  in 
)

Unescape backslashes in tstr/bstr text form.

This is defined in Section G.2 of RFC8610 [2].

Parameters
[out]outThe output buffer, which will be appended to.
inThe input buffer to read, which may be null terminated.
Returns
Zero upon success.

References BSL_CHKERR1, and take_hex_2byte().

Referenced by test_mock_bpa_slash_unescape_invalid(), and test_mock_bpa_slash_unescape_valid().

◆ mock_bpa_string_tolower()

void mock_bpa_string_tolower ( m_string_t  out)

Convert a text string to lowercase.

This is written to work on byte strings, not unicode.

Parameters
[out]outThe output buffer, which will be replaced.

References BSL_CHKVOID.

◆ mock_bpa_string_toupper()

void mock_bpa_string_toupper ( m_string_t  out)

Convert a text string to uppercase.

This is written to work on byte strings, not unicode.

Parameters
[out]outThe output buffer, which will be replaced.

References BSL_CHKVOID.

◆ mock_bpa_strip_space()

void mock_bpa_strip_space ( m_string_t  out,
const char *  in,
size_t  in_len 
)

Remove whitespace characters from a text string.

This is based on isspace() inspection.

Parameters
[out]outThe output buffer, which will be replaced.
[in]inThe input text to read.
in_lenThe length of text not including null terminator.

References strip_chars().

◆ mock_bpa_uri_percent_decode()

int mock_bpa_uri_percent_decode ( m_string_t  out,
const m_string_t  in 
)

Decode from URI percent-encoding text form.

This is defined in Section 2.1 of RFC 3986 [1].

Parameters
[out]outThe output buffer, which will be appended to.
[in]inThe input encoded text which may be null-terminated.
Returns
Zero upon success.

References BSL_CHKERR1, and take_hex_1byte().

Referenced by test_mock_bpa_uri_percent_decode_invalid(), and test_mock_bpa_uri_percent_decode_valid().

◆ mock_bpa_uri_percent_encode()

int mock_bpa_uri_percent_encode ( m_string_t  out,
const m_string_t  in,
const char *  safe 
)

Encode to URI percent-encoding text form.

This is defined in Section 2.1 of RFC 3986 [1]. The set of unreserved characters are alpha, digits, and _.-~ characters. in accordance with Section 2.3 of RFC 3986 [1].

Parameters
[out]outThe output buffer, which will be appended to.
inThe input encoded text which is null-terminated.
safeA set of additional safe characters to not be encoded, which is null-terminated.
Returns
Zero upon success.

References BSL_CHKERR1, and unreserved.

Referenced by test_mock_bpa_uri_percent_encode_valid().

◆ strip_chars()

static void strip_chars ( m_string_t  out,
const char *  in,
size_t  in_len,
const char *  chars 
)
static

Referenced by mock_bpa_strip_space().

◆ take_hex_1byte()

static int take_hex_1byte ( uint8_t *  out,
const char **  curs,
const char *  end 
)
static

References buf.

Referenced by mock_bpa_uri_percent_decode().

◆ take_hex_2byte()

static int take_hex_2byte ( uint16_t *  out,
const char **  curs,
const char *  end 
)
static

References buf.

Referenced by mock_bpa_slash_unescape().

Variable Documentation

◆ base16_decode_lim

const size_t base16_decode_lim = 0x80
static

Size of the base16_decode_table.

Referenced by base16_decode_char().

◆ base16_decode_table

const int base16_decode_table[0x80]
static
Initial value:
=
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
}

Decode table for base16.

Referenced by base16_decode_char().

◆ base64_alphabet

const char* base64_alphabet
static
Initial value:
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/"

Referenced by mock_bpa_base64_encode().

◆ base64_decode_lim

const size_t base64_decode_lim = 0x80
static

Size of the base16_decode_table.

Referenced by base64_decode_char().

◆ base64_decode_table

const int base64_decode_table[0x80]
static
Initial value:
= {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
}

Decode table for base64 and base64uri.

Referenced by base64_decode_char().

◆ base64url_alphabet

const char* base64url_alphabet
static
Initial value:
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789-_"

Referenced by mock_bpa_base64_encode().

◆ unreserved

const char* unreserved = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~"
static

Set of unreserved characters from Section 2.3 of RFC 3986 [1].

Referenced by mock_bpa_uri_percent_encode().