BSL v1.1.1 - 39.g9f43410
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
TextUtil.c File Reference
#include "bsl/front/TextUtil.h"
#include "bsl/BPSecLib_Private.h"
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
+ Include dependency graph for TextUtil.c:

Functions

int BSL_TextUtil_Base16_Encode (BSL_Data_t *out, const BSL_Data_t *in, bool uppercase)
 Encode to base16 text form.
 
static int base16_decode_char (uint8_t chr)
 Decode a single character.
 
int BSL_TextUtil_Base16_Decode (BSL_Data_t *out, const char *ptr, size_t len)
 Decode base16 text form.
 
int BSL_TextUtil_Base64_Encode (BSL_Data_t *out, const BSL_Data_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 BSL_TextUtil_Base64_Decode (BSL_Data_t *out, const char *ptr, size_t len)
 Decode base64 and base64url text forms.
 

Variables

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 BSL_TextUtil_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 BSL_TextUtil_Base64_Decode().

◆ BSL_TextUtil_Base16_Decode()

int BSL_TextUtil_Base16_Decode ( BSL_Data_t out,
const char *  ptr,
size_t  len 
)

Decode base16 text form.

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

Parameters
[out]outThe output buffer, which will be sized to its data.
[in]ptrThe input buffer to read, which may be null terminated. Whitespace in the input must have already been removed.
lenThe length from ptr to read, not including null terminator.
Returns
Zero upon success.

References ASSERT_ARG_NONNULL, base16_decode_char(), BSL_Data_Resize(), and BSL_Data_t::ptr.

Referenced by BSL_TestUtils_DecodeBase16_cstr(), BSLP_GetBytesHex(), test_BSL_TextUtil_Base16_Decode_invalid(), and test_BSL_TextUtil_Base16_Decode_valid().

◆ BSL_TextUtil_Base16_Encode()

int BSL_TextUtil_Base16_Encode ( BSL_Data_t out,
const BSL_Data_t in,
bool  uppercase 
)

Encode to base16 text form.

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

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

References ASSERT_ARG_NONNULL, BSL_Data_Resize(), BSL_Data_t::len, and BSL_Data_t::ptr.

Referenced by BSL_IdValPair_Print(), BSL_TestUtils_PrintHexToBuffer(), and test_BSL_TextUtil_Base16_Encode().

◆ BSL_TextUtil_Base64_Decode()

int BSL_TextUtil_Base64_Decode ( BSL_Data_t out,
const char *  ptr,
size_t  len 
)

Decode base64 and base64url text forms.

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

Parameters
[out]outThe output buffer, which will be sized to its data.
[in]ptrThe input buffer to read, which may be null terminated. Whitespace in the input must have already been removed.
lenThe length from ptr to read, not including null terminator.
Returns
Zero upon success.

References ASSERT_ARG_NONNULL, base64_decode_char(), BSL_Data_Resize(), BSL_Data_t::len, and BSL_Data_t::ptr.

Referenced by mock_bpa_key_registry_init_jwk(), test_BSL_TextUtil_Base64_Decode_invalid(), and test_BSL_TextUtil_Base64_Decode_valid().

◆ BSL_TextUtil_Base64_Encode()

int BSL_TextUtil_Base64_Encode ( BSL_Data_t out,
const BSL_Data_t in,
bool  useurl,
bool  usepad 
)

Encode base64 and base64url text forms.

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

Parameters
[out]outThe output buffer, which will be resized appropriately and null-terminated.
[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 ASSERT_ARG_NONNULL, base64_alphabet, base64url_alphabet, BSL_Data_Resize(), BSL_Data_t::len, and BSL_Data_t::ptr.

Referenced by TEST_CASE().

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 BSL_TextUtil_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 BSL_TextUtil_Base64_Encode().