iPXE
Functions | Variables
base16.h File Reference

Base16 encoding. More...

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

Go to the source code of this file.

Functions

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

Detailed Description

Base16 encoding.

Definition in file base16.h.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static size_t base16_encoded_len ( size_t  raw_len) [inline, static]

Calculate length of base16-encoded data.

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

Definition at line 21 of file base16.h.

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

                                                           {
        return ( 2 * raw_len );
}
static size_t base16_decoded_max_len ( const char *  encoded) [inline, static]

Calculate maximum length of base16-decoded string.

Parameters:
encodedEncoded string
max_raw_lenMaximum length of raw data

Definition at line 31 of file base16.h.

References strlen().

Referenced by base16_decode_okx().

                                                                    {
        return ( ( strlen ( encoded ) + 1 ) / 2 );
}
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 50 of file base16.c.

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

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

                                             {
        const uint8_t *bytes = raw;
        const char delimiter[2] = { separator, '\0' };
        size_t used = 0;
        unsigned int i;

        if ( len )
                data[0] = 0; /* Ensure that a terminating NUL exists */
        for ( i = 0 ; i < raw_len ; i++ ) {
                used += ssnprintf ( ( data + used ), ( len - used ),
                                    "%s%02x", ( used ? delimiter : "" ),
                                    bytes[i] );
        }
        return used;
}
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 76 of file base16.c.

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

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

                                                                               {
        uint8_t *out = data;
        unsigned int count = 0;
        unsigned int sixteens;
        unsigned int units;

        while ( *encoded ) {

                /* Check separator, if applicable */
                if ( count && separator && ( ( *(encoded++) != separator ) ) )
                        return -EINVAL;

                /* Extract digits.  Note that either digit may be NUL,
                 * which would be interpreted as an invalid value by
                 * digit_value(); there is therefore no need for an
                 * explicit end-of-string check.
                 */
                sixteens = digit_value ( *(encoded++) );
                if ( sixteens >= 16 )
                        return -EINVAL;
                units = digit_value ( *(encoded++) );
                if ( units >= 16 )
                        return -EINVAL;

                /* Store result */
                if ( count < len )
                        out[count] = ( ( sixteens << 4 ) | units );
                count++;

        }
        return count;
}
static __attribute__ ( (always_inline)  ) const [inline, static]

Base16-encode data.

Parameters:
rawRaw data
raw_lenLength of raw data
dataBuffer
lenLength of buffer
Return values:
lenEncoded length

Variable Documentation

size_t char* data

Definition at line 50 of file base16.h.