base64.h File Reference

Base64 encoding. More...

#include <stdint.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static size_t base64_encoded_len (size_t raw_len)
 Calculate length of base64-encoded string.
void base64_encode (const char *raw, char *encoded)
 Base64-encode a string.


Detailed Description

Base64 encoding.

Definition in file base64.h.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

static size_t base64_encoded_len ( size_t  raw_len  )  [inline, static]

Calculate length of base64-encoded string.

Parameters:
raw_len Raw string length (excluding NUL)
Return values:
encoded_len Encoded string length (excluding NUL)

Definition at line 20 of file base64.h.

Referenced by base64_encode(), and http_step().

00020                                                            {
00021         return ( ( ( raw_len + 3 - 1 ) / 3 ) * 4 );
00022 }

void base64_encode ( const char *  raw,
char *  encoded 
)

Base64-encode a string.

Parameters:
raw Raw string
encoded Buffer for encoded string
The buffer must be the correct length for the encoded string. Use something like

char buf[ base64_encoded_len ( strlen ( raw ) ) + 1 ];

(the +1 is for the terminating NUL) to provide a buffer of the correct size.

Definition at line 49 of file base64.c.

References assert, base64, base64_encoded_len(), DBG, and strlen().

Referenced by http_step().

00049                                                       {
00050         const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
00051         uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
00052         size_t raw_bit_len = ( 8 * strlen ( raw ) );
00053         unsigned int bit;
00054         unsigned int tmp;
00055 
00056         for ( bit = 0 ; bit < raw_bit_len ; bit += 6 ) {
00057                 tmp = ( ( raw_bytes[ bit / 8 ] << ( bit % 8 ) ) |
00058                         ( raw_bytes[ bit / 8 + 1 ] >> ( 8 - ( bit % 8 ) ) ) );
00059                 tmp = ( ( tmp >> 2 ) & 0x3f );
00060                 *(encoded_bytes++) = base64[tmp];
00061         }
00062         for ( ; ( bit % 8 ) != 0 ; bit += 6 )
00063                 *(encoded_bytes++) = '=';
00064         *(encoded_bytes++) = '\0';
00065 
00066         DBG ( "Base64-encoded \"%s\" as \"%s\"\n", raw, encoded );
00067         assert ( strlen ( encoded ) == base64_encoded_len ( strlen ( raw ) ) );
00068 }


Generated on Tue Apr 6 20:01:48 2010 for gPXE by  doxygen 1.5.7.1