iPXE
base16.h
Go to the documentation of this file.
00001 #ifndef _IPXE_BASE16_H
00002 #define _IPXE_BASE16_H
00003 
00004 /** @file
00005  *
00006  * Base16 encoding
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <stdint.h>
00013 #include <string.h>
00014 
00015 /**
00016  * Calculate length of base16-encoded data
00017  *
00018  * @v raw_len           Raw data length
00019  * @ret encoded_len     Encoded string length (excluding NUL)
00020  */
00021 static inline size_t base16_encoded_len ( size_t raw_len ) {
00022         return ( 2 * raw_len );
00023 }
00024 
00025 /**
00026  * Calculate maximum length of base16-decoded string
00027  *
00028  * @v encoded           Encoded string
00029  * @v max_raw_len       Maximum length of raw data
00030  */
00031 static inline size_t base16_decoded_max_len ( const char *encoded ) {
00032         return ( ( strlen ( encoded ) + 1 ) / 2 );
00033 }
00034 
00035 extern size_t hex_encode ( char separator, const void *raw, size_t raw_len,
00036                            char *data, size_t len );
00037 extern int hex_decode ( char separator, const char *encoded, void *data,
00038                         size_t len );
00039 
00040 /**
00041  * Base16-encode data
00042  *
00043  * @v raw               Raw data
00044  * @v raw_len           Length of raw data
00045  * @v data              Buffer
00046  * @v len               Length of buffer
00047  * @ret len             Encoded length
00048  */
00049 static inline __attribute__ (( always_inline )) size_t
00050 base16_encode ( const void *raw, size_t raw_len, char *data, size_t len ) {
00051         return hex_encode ( 0, raw, raw_len, data, len );
00052 }
00053 
00054 /**
00055  * Base16-decode data
00056  *
00057  * @v encoded           Encoded string
00058  * @v data              Buffer
00059  * @v len               Length of buffer
00060  * @ret len             Length of data, or negative error
00061  */
00062 static inline __attribute__ (( always_inline )) int
00063 base16_decode ( const char *encoded, void *data, size_t len ) {
00064         return hex_decode ( 0, encoded, data, len );
00065 }
00066 
00067 #endif /* _IPXE_BASE16_H */