iPXE
base64.h File Reference

Base64 encoding. More...

#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static size_t base64_encoded_len (size_t raw_len)
 Calculate length of base64-encoded data.
static size_t base64_decoded_max_len (const char *encoded)
 Calculate maximum length of base64-decoded string.
size_t base64_encode (const void *raw, size_t raw_len, char *data, size_t len)
 Base64-encode data.
int base64_decode (const char *encoded, void *data, size_t len)
 Base64-decode string.

Detailed Description

Base64 encoding.

Definition in file base64.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ base64_encoded_len()

size_t base64_encoded_len ( size_t raw_len)
inlinestatic

Calculate length of base64-encoded data.

Parameters
raw_lenRaw data length
Return values
encoded_lenEncoded string length (excluding NUL)

Definition at line 22 of file base64.h.

22 {
23 return ( ( ( raw_len + 3 - 1 ) / 3 ) * 4 );
24}
static size_t raw_len
Definition base16.h:54

References raw_len.

Referenced by base64_encode_okx(), http_format_ntlm_auth(), icert_encode(), ocsp_uri_string(), and validator_start_download().

◆ base64_decoded_max_len()

size_t base64_decoded_max_len ( const char * encoded)
inlinestatic

Calculate maximum length of base64-decoded string.

Parameters
encodedEncoded string
max_raw_lenMaximum length of raw data

Note that the exact length of the raw data cannot be known until the string is decoded.

Definition at line 35 of file base64.h.

35 {
36 return ( ( ( strlen ( encoded ) + 4 - 1 ) / 4 ) * 3 );
37}
size_t strlen(const char *src)
Get length of string.
Definition string.c:244

References strlen().

Referenced by base64_decode_okx(), ipair_rx_pubkey(), and pem_asn1().

◆ base64_encode()

size_t base64_encode ( const void * raw,
size_t raw_len,
char * data,
size_t len )
extern

Base64-encode data.

Parameters
rawRaw data
raw_lenLength of raw data
dataBuffer
lenLength of buffer
Return values
lenEncoded length

Definition at line 52 of file base64.c.

53 {
54 const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
55 size_t raw_bit_len = ( 8 * raw_len );
56 size_t used = 0;
57 unsigned int bit;
58 unsigned int byte;
59 unsigned int shift;
60 unsigned int tmp;
61
62 for ( bit = 0 ; bit < raw_bit_len ; bit += 6, used++ ) {
63 byte = ( bit / 8 );
64 shift = ( bit % 8 );
65 tmp = ( raw_bytes[byte] << shift );
66 if ( ( byte + 1 ) < raw_len )
67 tmp |= ( raw_bytes[ byte + 1 ] >> ( 8 - shift ) );
68 tmp = ( ( tmp >> 2 ) & 0x3f );
69 if ( used < len )
70 data[used] = base64[tmp];
71 }
72 for ( ; ( bit % 8 ) != 0 ; bit += 6, used++ ) {
73 if ( used < len )
74 data[used] = '=';
75 }
76 if ( used < len )
77 data[used] = '\0';
78 if ( len )
79 data[ len - 1 ] = '\0'; /* Ensure terminator exists */
80
81 return used;
82}
__be32 raw[7]
Definition CIB_PRM.h:0
unsigned char uint8_t
Definition stdint.h:10
static const char base64[64+1]
Definition base64.c:40
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
static unsigned int unsigned int bit
Definition bigint.h:392
unsigned long tmp
Definition linux_pci.h:65
unsigned char byte
Definition smc9000.h:38

References base64, bit, data, len, raw, raw_len, and tmp.

Referenced by base64_encode_okx(), format_base64_setting(), http_format_basic_auth(), http_format_ntlm_auth(), icert_encode(), ocsp_uri_string(), and validator_start_download().

◆ base64_decode()

int base64_decode ( const char * encoded,
void * data,
size_t len )
extern

Base64-decode string.

Parameters
encodedEncoded string
dataBuffer
lenLength of buffer
Return values
lenLength of data, or negative error

Definition at line 92 of file base64.c.

92 {
93 const char *in = encoded;
94 uint8_t *out = data;
95 uint8_t in_char;
96 char *match;
97 int in_bits;
98 unsigned int bit = 0;
99 unsigned int pad_count = 0;
100 size_t offset;
101
102 /* Zero the output buffer */
103 memset ( data, 0, len );
104
105 /* Decode string */
106 while ( ( in_char = *(in++) ) ) {
107
108 /* Ignore whitespace characters */
109 if ( isspace ( in_char ) )
110 continue;
111
112 /* Process pad characters */
113 if ( in_char == '=' ) {
114 if ( pad_count >= 2 ) {
115 DBG ( "Base64-encoded string \"%s\" has too "
116 "many pad characters\n", encoded );
117 return -EINVAL;
118 }
119 pad_count++;
120 bit -= 2; /* unused_bits = ( 2 * pad_count ) */
121 continue;
122 }
123 if ( pad_count ) {
124 DBG ( "Base64-encoded string \"%s\" has invalid pad "
125 "sequence\n", encoded );
126 return -EINVAL;
127 }
128
129 /* Process normal characters */
130 match = strchr ( base64, in_char );
131 if ( ! match ) {
132 DBG ( "Base64-encoded string \"%s\" contains invalid "
133 "character '%c'\n", encoded, in_char );
134 return -EINVAL;
135 }
136 in_bits = ( match - base64 );
137
138 /* Add to raw data */
139 in_bits <<= 2;
140 offset = ( bit / 8 );
141 if ( offset < len )
142 out[offset] |= ( in_bits >> ( bit % 8 ) );
143 offset++;
144 if ( offset < len )
145 out[offset] |= ( in_bits << ( 8 - ( bit % 8 ) ) );
146 bit += 6;
147 }
148
149 /* Check that we decoded a whole number of bytes */
150 if ( ( bit % 8 ) != 0 ) {
151 DBG ( "Base64-encoded string \"%s\" has invalid bit length "
152 "%d\n", encoded, bit );
153 return -EINVAL;
154 }
155
156 /* Return length in bytes */
157 return ( bit / 8 );
158}
__be32 in[4]
Definition CIB_PRM.h:7
__be32 out[4]
Definition CIB_PRM.h:8
uint16_t offset
Offset to command line.
Definition bzimage.h:3
int isspace(int character)
Check to see if character is a space.
Definition ctype.c:42
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
#define EINVAL
Invalid argument.
Definition errno.h:429
void * memset(void *dest, int character, size_t len) __nonnull
char * strchr(const char *src, int character)
Find character within a string.
Definition string.c:272

References base64, bit, data, DBG, EINVAL, in, isspace(), len, memset(), offset, out, and strchr().

Referenced by base64_decode_okx(), http_parse_ntlm_auth(), ipair_rx_pubkey(), iscsi_large_binary_decode(), parse_base64_setting(), and pem_asn1().