iPXE
base64.h
Go to the documentation of this file.
00001 #ifndef _IPXE_BASE64_H
00002 #define _IPXE_BASE64_H
00003 
00004 /** @file
00005  *
00006  * Base64 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 base64-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 base64_encoded_len ( size_t raw_len ) {
00022         return ( ( ( raw_len + 3 - 1 ) / 3 ) * 4 );
00023 }
00024 
00025 /**
00026  * Calculate maximum length of base64-decoded string
00027  *
00028  * @v encoded           Encoded string
00029  * @v max_raw_len       Maximum length of raw data
00030  *
00031  * Note that the exact length of the raw data cannot be known until
00032  * the string is decoded.
00033  */
00034 static inline size_t base64_decoded_max_len ( const char *encoded ) {
00035         return ( ( ( strlen ( encoded ) + 4 - 1 ) / 4 ) * 3 );
00036 }
00037 
00038 extern size_t base64_encode ( const void *raw, size_t raw_len, char *data,
00039                               size_t len );
00040 extern int base64_decode ( const char *encoded, void *data, size_t len );
00041 
00042 #endif /* _IPXE_BASE64_H */