iPXE
Functions
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)
 
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) More...
 
int hex_decode (char separator, const char *encoded, void *data, size_t len)
 Decode hexadecimal string (with optional byte separator character) More...
 

Detailed Description

Base16 encoding.

Definition in file base16.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ 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 50 of file base16.c.

51  {
52  const uint8_t *bytes = raw;
53  const char delimiter[2] = { separator, '\0' };
54  size_t used = 0;
55  unsigned int i;
56 
57  if ( len )
58  data[0] = 0; /* Ensure that a terminating NUL exists */
59  for ( i = 0 ; i < raw_len ; i++ ) {
60  used += ssnprintf ( ( data + used ), ( len - used ),
61  "%s%02x", ( used ? delimiter : "" ),
62  bytes[i] );
63  }
64  return used;
65 }
int ssnprintf(char *buf, ssize_t ssize, const char *fmt,...)
Version of vsnprintf() that accepts a signed buffer size.
Definition: vsprintf.c:420
static size_t raw_len
Definition: base16.h:50
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Length.
Definition: ena.h:14
__be32 raw[7]
Definition: CIB_PRM.h:28
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
uint8_t bytes[64]
Definition: ib_mad.h:16

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 76 of file base16.c.

76  {
77  uint8_t *out = data;
78  unsigned int count = 0;
79  unsigned int sixteens;
80  unsigned int units;
81 
82  while ( *encoded ) {
83 
84  /* Check separator, if applicable */
85  if ( count && separator && ( ( *(encoded++) != separator ) ) )
86  return -EINVAL;
87 
88  /* Extract digits. Note that either digit may be NUL,
89  * which would be interpreted as an invalid value by
90  * digit_value(); there is therefore no need for an
91  * explicit end-of-string check.
92  */
93  sixteens = digit_value ( *(encoded++) );
94  if ( sixteens >= 16 )
95  return -EINVAL;
96  units = digit_value ( *(encoded++) );
97  if ( units >= 16 )
98  return -EINVAL;
99 
100  /* Store result */
101  if ( count < len )
102  out[count] = ( ( sixteens << 4 ) | units );
103  count++;
104 
105  }
106  return count;
107 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
unsigned int digit_value(unsigned int character)
Calculate digit value.
Definition: string.c:383
__be32 out[4]
Definition: CIB_PRM.h:36
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Length.
Definition: ena.h:14
uint16_t count
Number of entries.
Definition: ena.h:22
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

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

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