base64.c File Reference

Base64 encoding. More...

#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <gpxe/base64.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
void base64_encode (const char *raw, char *encoded)
 Base64-encode a string.

Variables

static const char base64 [64]


Detailed Description

Base64 encoding.

Definition in file base64.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

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 }


Variable Documentation

const char base64[64] [static]

Initial value:

        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

Definition at line 32 of file base64.c.

Referenced by base64_encode().


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