/****************************************** * base64 example with apr * Sylvain Marechal * 25/10/2005 *****************************************/ #include "apr_general.h" #include "apr_getopt.h" #include "apr_uuid.h" /* args */ static apr_getopt_option_t s_opts[] = { { "help", 'h', 0, "display this help" }, { 0, 0, 0, 0 } }; /********************************************* * usage ********************************************/ static void usage() { int i; printf( "Usage:\n" ); for( i = 0; i < sizeof(s_opts)/sizeof(apr_getopt_option_t) -1; i ++ ) { printf( "\t--%s\t-%c\t%s\n", s_opts[i].name, s_opts[i].optch, s_opts[i].description ); } exit(1); } /********************************************* * get_args ********************************************/ static int get_args( apr_pool_t *pool, int argc, char * argv[] ) { apr_getopt_t *os; apr_status_t ret; apr_getopt_init( &os, pool, argc, (const char * const *)argv ); do { int opt_ch; const char * opt_arg; ret = apr_getopt_long( os, s_opts, &opt_ch, &opt_arg ); if( ret == APR_SUCCESS ) { switch(opt_ch) { case 'h': { usage(); break; } } } } while( ret == APR_SUCCESS ); return 0; } /****************************************** * test_base64() *****************************************/ void test_base64( apr_pool_t *pool ) { apr_uuid_t uuid; char szuuid[APR_UUID_FORMATTED_LENGTH + 1] = {0}; char szencoded[2*(APR_UUID_FORMATTED_LENGTH + 1)] = {0}; char szdecoded[APR_UUID_FORMATTED_LENGTH + 1] = {0}; int lenencoded, lendecoded; /* Create a string */ apr_uuid_get( &uuid ); apr_uuid_format( szuuid, &uuid ); printf( "uuid string to encode into base64: '%s'\n", szuuid ); /* Encode into base64 */ lenencoded = apr_base64_encode_len( APR_UUID_FORMATTED_LENGTH + 1 ); if( lenencoded > sizeof(szencoded) ) { printf( "test_base64() error with lenencoded ...\n" ); } lenencoded = apr_base64_encode_binary( szencoded, szuuid, strlen(szuuid) ); /* Decode from base64 */ lendecoded = apr_base64_decode_len( szencoded ); if( lendecoded > sizeof(szdecoded) ) { printf( "test_base64() error with lendecoded ...\n" ); } apr_base64_decode_binary( szdecoded, szencoded, lenencoded ); /* printf */ printf( "base64: '%s'\ndecoded base64: '%s'\n", szencoded, szdecoded ); } /****************************************** * main() *****************************************/ int main( int argc, char * argv[] ) { apr_pool_t *pool; apr_status_t ret; /* Init apr */ ret = apr_initialize(); if( ret != APR_SUCCESS) { printf( "apr_initialize() failed (%d)\n", ret ); exit(-1); } /* Create pool */ ret = apr_pool_create(&pool, NULL); if( ret != APR_SUCCESS) { printf( "apr_pool_create() failed %d\n", ret ); exit( -1); } /* Parse args */ get_args( pool, argc, argv ); /* base64 tests */ test_base64( pool ); /* Destroy pool */ apr_pool_destroy( pool ); return 0; }