iPXE
base16.h File Reference

Base16 encoding. More...

#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Macros

#define HEX_DECODE_OPTIONAL   0x80
 Treat separator as optional while decoding.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static size_t base16_encoded_len (size_t raw_len)
 Calculate length of base16-encoded data.
static size_t base16_decoded_max_len (const char *encoded)
 Calculate maximum length of base16-decoded string.
size_t hex_encode (char separator, const void *raw, size_t raw_len, char *data, size_t len)
 Encode hexadecimal string (with optional byte separator character)
int hex_decode (char separator, const char *encoded, void *data, size_t len)
 Decode hexadecimal string (with optional byte separator character)
static __attribute__ ((always_inline)) size_t base16_encode(const void *raw
 Base16-encode data.

Variables

static size_t raw_len
static size_t char * data
static size_t char size_t len

Detailed Description

Base16 encoding.

Definition in file base16.h.

Macro Definition Documentation

◆ HEX_DECODE_OPTIONAL

#define HEX_DECODE_OPTIONAL   0x80

Treat separator as optional while decoding.

Definition at line 17 of file base16.h.

Referenced by hex_decode(), and uuid_aton().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ base16_encoded_len()

size_t base16_encoded_len ( size_t raw_len)
inlinestatic

Calculate length of base16-encoded data.

Parameters
raw_lenRaw data length
Return values
encoded_lenEncoded string length (excluding NUL)

Definition at line 25 of file base16.h.

25 {
26 return ( 2 * raw_len );
27}
static size_t raw_len
Definition base16.h:54

References raw_len.

Referenced by base16_encode_okx(), certstat(), ecm_fetch_mac(), http_digest_authenticate(), iscsi_build_login_request_strings(), peerdisc_open(), and peerdist_info_hash_ntoa().

◆ base16_decoded_max_len()

size_t base16_decoded_max_len ( const char * encoded)
inlinestatic

Calculate maximum length of base16-decoded string.

Parameters
encodedEncoded string
max_raw_lenMaximum length of raw data

Definition at line 35 of file base16.h.

35 {
36 return ( ( strlen ( encoded ) + 1 ) / 2 );
37}
size_t strlen(const char *src)
Get length of string.
Definition string.c:244

References strlen().

Referenced by base16_decode_okx().

◆ hex_encode()

size_t hex_encode ( char separator,
const void * raw,
size_t raw_len,
char * data,
size_t len )
extern

Encode hexadecimal string (with optional byte separator character)

Parameters
separatorByte separator character, or 0 for no separator
rawRaw data
raw_lenLength of raw data
dataBuffer
lenLength of buffer
Return values
lenEncoded length

Definition at line 51 of file base16.c.

52 {
53 const uint8_t *bytes = raw;
54 const char delimiter[2] = { separator, '\0' };
55 size_t used = 0;
56 unsigned int i;
57
58 if ( len )
59 data[0] = 0; /* Ensure that a terminating NUL exists */
60 for ( i = 0 ; i < raw_len ; i++ ) {
61 used += ssnprintf ( ( data + used ), ( len - used ),
62 "%s%02x", ( used ? delimiter : "" ),
63 bytes[i] );
64 }
65 return used;
66}
__be32 raw[7]
Definition CIB_PRM.h:0
unsigned char uint8_t
Definition stdint.h:10
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
uint8_t bytes[64]
Definition ib_mad.h:5
int ssnprintf(char *buf, ssize_t ssize, const char *fmt,...)
Version of vsnprintf() that accepts a signed buffer size.
Definition vsprintf.c:421

References bytes, data, len, raw, raw_len, and ssnprintf().

Referenced by format_hex_colon_setting(), format_hex_hyphen_setting(), and format_hex_raw_setting().

◆ hex_decode()

int hex_decode ( char separator,
const char * encoded,
void * data,
size_t len )
extern

Decode hexadecimal string (with optional byte separator character)

Parameters
separatorByte separator character, or 0 for no separator
encodedEncoded string
dataBuffer
lenLength of buffer
Return values
lenLength of data, or negative error

Definition at line 77 of file base16.c.

77 {
78 uint8_t *out = data;
79 unsigned int count = 0;
80 unsigned int sixteens;
81 unsigned int units;
82 int optional;
83
84 /* Strip out optionality flag from separator character */
85 optional = ( separator & HEX_DECODE_OPTIONAL );
86 separator &= ~HEX_DECODE_OPTIONAL;
87
88 /* Decode string */
89 while ( *encoded ) {
90
91 /* Check separator, if applicable */
92 if ( count && separator ) {
93 if ( *encoded == separator ) {
94 encoded++;
95 } else if ( ! optional ) {
96 return -EINVAL;
97 }
98 }
99
100 /* Extract digits. Note that either digit may be NUL,
101 * which would be interpreted as an invalid value by
102 * digit_value(); there is therefore no need for an
103 * explicit end-of-string check.
104 */
105 sixteens = digit_value ( *(encoded++) );
106 if ( sixteens >= 16 )
107 return -EINVAL;
108 units = digit_value ( *(encoded++) );
109 if ( units >= 16 )
110 return -EINVAL;
111
112 /* Store result */
113 if ( count < len )
114 out[count] = ( ( sixteens << 4 ) | units );
115 count++;
116
117 }
118 return count;
119}
__be32 out[4]
Definition CIB_PRM.h:8
#define HEX_DECODE_OPTIONAL
Treat separator as optional while decoding.
Definition base16.h:17
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define EINVAL
Invalid argument.
Definition errno.h:429
unsigned int digit_value(unsigned int character)
Calculate digit value.
Definition string.c:427

References count, data, digit_value(), EINVAL, HEX_DECODE_OPTIONAL, len, and out.

Referenced by netfront_read_mac(), parse_hex_hyphen_setting(), parse_hex_raw_setting(), parse_hex_setting(), and uuid_aton().

◆ __attribute__()

__attribute__ ( (always_inline) ) const
inlinestatic

Base16-encode data.

Base16-decode data.

Parameters
rawRaw data
raw_lenLength of raw data
dataBuffer
lenLength of buffer
Return values
lenEncoded length
Parameters
encodedEncoded string
dataBuffer
lenLength of buffer
Return values
lenLength of data, or negative error

Variable Documentation

◆ raw_len

◆ data

void* data

Definition at line 54 of file base16.h.

◆ len

void size_t len
Initial value:
{
return hex_encode ( 0, raw, raw_len, data, len )
size_t hex_encode(char separator, const void *raw, size_t raw_len, char *data, size_t len)
Encode hexadecimal string (with optional byte separator character)
Definition base16.c:51

Definition at line 54 of file base16.h.