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 /** Treat separator as optional while decoding */
16 #define HEX_DECODE_OPTIONAL 0x80
17 
18 /**
19  * Calculate length of base16-encoded data
20  *
21  * @v raw_len Raw data length
22  * @ret encoded_len Encoded string length (excluding NUL)
23  */
24 static inline size_t base16_encoded_len ( size_t raw_len ) {
25  return ( 2 * raw_len );
26 }
27 
28 /**
29  * Calculate maximum length of base16-decoded string
30  *
31  * @v encoded Encoded string
32  * @v max_raw_len Maximum length of raw data
33  */
34 static inline size_t base16_decoded_max_len ( const char *encoded ) {
35  return ( ( strlen ( encoded ) + 1 ) / 2 );
36 }
37 
38 extern size_t hex_encode ( char separator, const void *raw, size_t raw_len,
39  char *data, size_t len );
40 extern int hex_decode ( char separator, const char *encoded, void *data,
41  size_t len );
42 
43 /**
44  * Base16-encode data
45  *
46  * @v raw Raw data
47  * @v raw_len Length of raw data
48  * @v data Buffer
49  * @v len Length of buffer
50  * @ret len Encoded length
51  */
52 static inline __attribute__ (( always_inline )) size_t
53 base16_encode ( const void *raw, size_t raw_len, char *data, size_t len ) {
54  return hex_encode ( 0, raw, raw_len, data, len );
55 }
56 
57 /**
58  * Base16-decode data
59  *
60  * @v encoded Encoded string
61  * @v data Buffer
62  * @v len Length of buffer
63  * @ret len Length of data, or negative error
64  */
65 static inline __attribute__ (( always_inline )) int
66 base16_decode ( const char *encoded, void *data, size_t len ) {
67  return hex_decode ( 0, encoded, data, len );
68 }
69 
70 #endif /* _IPXE_BASE16_H */
static size_t base16_decoded_max_len(const char *encoded)
Calculate maximum length of base16-decoded string.
Definition: base16.h:34
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:24
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:53
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
static size_t char size_t len
Definition: base16.h:53
static size_t char * data
Definition: base16.h:53
__be32 raw[7]
Definition: CIB_PRM.h:28
String functions.