iPXE
Functions
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)
 
static size_t base64_encoded_len (size_t raw_len)
 Calculate length of base64-encoded data. More...
 
static size_t base64_decoded_max_len (const char *encoded)
 Calculate maximum length of base64-decoded string. More...
 
size_t base64_encode (const void *raw, size_t raw_len, char *data, size_t len)
 Base64-encode data. More...
 
int base64_decode (const char *encoded, void *data, size_t len)
 Base64-decode string. More...
 

Detailed Description

Base64 encoding.

Definition in file base64.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ base64_encoded_len()

static 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 21 of file base64.h.

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

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()

static 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 34 of file base64.h.

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

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 
)

Base64-encode data.

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

Definition at line 51 of file base64.c.

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

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 
)

Base64-decode string.

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

Definition at line 91 of file base64.c.

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

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().