/* kbase64.c * BASE64 の例 * BASE64: apr_base64_encode / apr_base64_encode_len / apr_base64_decode / apr_base64_decode_len */ #define USAGE "Usage: kbase64 \n" #include "apr_general.h" #include "apr_file_io.h" #include "apr_errno.h" #include "apr_md5.h" #include "apr_base64.h" #include "mystab.h" #define ERROR_BUF_SIZE 1024 int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) { char *input; int inputLen; char *encodedData=NULL; char *decodedData=NULL; if (ac < 2) { apr_file_printf(astderr, USAGE); goto _ERROR_; } input = av[1]; inputLen = strlen(input); /* apr_base64_encode / apr_base64_encode_len * INCLUDE: apr_base64.h * LIBS: libapr-1.lib libaprutil-1.lib */ if (input) { char *encoded; apr_size_t encLen=apr_base64_encode_len(inputLen); encoded = apr_palloc(pool, encLen); memset(encoded, 0, encLen); if ((apr_base64_encode(encoded, input, inputLen))<=0) { goto _ERROR_; } encodedData = encoded; } if (encodedData) apr_file_printf(astdout, "encoded: [%s][%dbytes]\n", encodedData, strlen(encodedData)); /* apr_base64_decode / apr_base64_decode_len * INCLUDE: apr_base64.h * LIBS: libapr-1.lib libaprutil-1.lib */ if (encodedData) { char *input = encodedData; unsigned char *decoded; apr_size_t decLen = apr_base64_decode_len(encodedData); decoded = apr_palloc(pool, decLen); memset(decoded, 0, decLen); if ((apr_base64_decode(decoded, input))<=0) { goto _ERROR_; } decodedData = decoded; } if (decodedData) { apr_file_printf(astdout, "decoded: [%s][%dbytes]\n", decodedData, strlen(decodedData)); } if (decodedData && !strcmp(input, decodedData)) { apr_file_printf(astdout, "Successfully decoded.\n"); } apr_file_printf(astdout, "\ndone.\n"); return 0; /* 正常終了 */ _ERROR_: apr_file_printf(astderr, "failed!\n"); return 1; /* 異常終了 */ }