iPXE
base16.c File Reference

Base16 encoding. More...

#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/string.h>
#include <ipxe/vsprintf.h>
#include <ipxe/base16.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
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)

Detailed Description

Base16 encoding.

Definition in file base16.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ hex_encode()

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)

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
static size_t raw_len
Definition base16.h:54
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 )

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