iPXE
base16.h
Go to the documentation of this file.
1 #ifndef _IPXE_BASE16_H
2 #define _IPXE_BASE16_H
3 
4 /** @file
5  *
6  * Base16 encoding
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <string.h>
14 
15 /**
16  * Calculate length of base16-encoded data
17  *
18  * @v raw_len Raw data length
19  * @ret encoded_len Encoded string length (excluding NUL)
20  */
21 static inline size_t base16_encoded_len ( size_t raw_len ) {
22  return ( 2 * raw_len );
23 }
24 
25 /**
26  * Calculate maximum length of base16-decoded string
27  *
28  * @v encoded Encoded string
29  * @v max_raw_len Maximum length of raw data
30  */
31 static inline size_t base16_decoded_max_len ( const char *encoded ) {
32  return ( ( strlen ( encoded ) + 1 ) / 2 );
33 }
34 
35 extern size_t hex_encode ( char separator, const void *raw, size_t raw_len,
36  char *data, size_t len );
37 extern int hex_decode ( char separator, const char *encoded, void *data,
38  size_t len );
39 
40 /**
41  * Base16-encode data
42  *
43  * @v raw Raw data
44  * @v raw_len Length of raw data
45  * @v data Buffer
46  * @v len Length of buffer
47  * @ret len Encoded length
48  */
49 static inline __attribute__ (( always_inline )) size_t
50 base16_encode ( const void *raw, size_t raw_len, char *data, size_t len ) {
51  return hex_encode ( 0, raw, raw_len, data, len );
52 }
53 
54 /**
55  * Base16-decode data
56  *
57  * @v encoded Encoded string
58  * @v data Buffer
59  * @v len Length of buffer
60  * @ret len Length of data, or negative error
61  */
62 static inline __attribute__ (( always_inline )) int
63 base16_decode ( const char *encoded, void *data, size_t len ) {
64  return hex_decode ( 0, encoded, data, len );
65 }
66 
67 #endif /* _IPXE_BASE16_H */
static size_t base16_decoded_max_len(const char *encoded)
Calculate maximum length of base16-decoded string.
Definition: base16.h:31
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:50
static __attribute__((always_inline)) size_t base16_encode(const void *raw
Base16-encode data.
static size_t base16_encoded_len(size_t raw_len)
Calculate length of base16-encoded data.
Definition: base16.h:21
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int hex_decode(char separator, const char *encoded, void *data, size_t len)
Decode hexadecimal string (with optional byte separator character)
Definition: base16.c:76
static size_t raw_len
Definition: base16.h:50
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
static size_t char size_t len
Definition: base16.h:50
static size_t char * data
Definition: base16.h:50
__be32 raw[7]
Definition: CIB_PRM.h:28
String functions.