iPXE
Functions | Variables
base64.c File Reference

Base64 encoding. More...

#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/base64.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FILE_SECBOOT (PERMITTED)
 
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...
 

Variables

static const char base64 [64+1]
 

Detailed Description

Base64 encoding.

Definition in file base64.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ 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 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 }
static unsigned int unsigned int bit
Definition: bigint.h:392
static const char base64[64+1]
Definition: base64.c:40
unsigned long tmp
Definition: linux_pci.h:65
ring len
Length.
Definition: dwmac.h:231
static size_t raw_len
Definition: base16.h:54
unsigned char uint8_t
Definition: stdint.h:10
unsigned char byte
Definition: smc9000.h:38
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 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 }
#define EINVAL
Invalid argument.
Definition: errno.h:429
__be32 in[4]
Definition: CIB_PRM.h:35
static unsigned int unsigned int bit
Definition: bigint.h:392
static const char base64[64+1]
Definition: base64.c:40
__be32 out[4]
Definition: CIB_PRM.h:36
ring len
Length.
Definition: dwmac.h:231
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:272
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:42
unsigned char uint8_t
Definition: stdint.h:10
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#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().

Variable Documentation

◆ base64

const char base64[64+1]
static
Initial value:
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

Definition at line 40 of file base64.c.

Referenced by base64_decode(), base64_encode(), and ocsp_uri_string().