kbase64.c

最終更新:2009/10/10

kbase64.c

001: /* kbase64.c 
002:  * BASE64 の例
003:  * BASE64: apr_base64_encode / apr_base64_encode_len / apr_base64_decode / apr_base64_decode_len
004: */
005: 
006: #define USAGE "Usage: kbase64 <keyword>\n"
007: 
008: #include "apr_general.h"
009: #include "apr_file_io.h"
010: #include "apr_errno.h"
011: 
012: #include "apr_md5.h"
013: #include "apr_base64.h"
014: 
015: #include "mystab.h"
016: 
017: #define ERROR_BUF_SIZE 1024
018:  
019: int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) {
020: 
021:   char *input;
022:   int inputLen;
023:   char *encodedData;
024:   char *decodedData;
025:   
026:   
027:   if (ac < 2) {
028:     apr_file_printf(astderr, USAGE);
029:     goto _ERROR_;
030:   }
031: 
032:   input = av[1];
033:   inputLen = strlen(input);
034: 
035:   /* apr_base64_encode / apr_base64_encode_len
036:    * INCLUDE: apr_base64.h
037:    * LIBS: libapr-1.lib libaprutil-1.lib
038:    */
039: 
040:   if (input) {
041:     char *encoded;
042:     apr_size_t encLen=apr_base64_encode_len(inputLen);
043: 
044:     encoded = apr_palloc(pool, encLen);
045:     memset(encoded, 0, encLen);
046: 
047:     if ((apr_base64_encode(encoded, input, inputLen))<=0) {
048:       goto _ERROR_;
049:     }
050: 
051:     encodedData = encoded;
052:   }
053: 
054:   if (encodedData)
055:     apr_file_printf(astdout, "encoded: [%s][%dbytes]\n", encodedData, strlen(encodedData));
056: 
057: 
058:   /* apr_base64_decode / apr_base64_decode_len
059:    * INCLUDE: apr_base64.h
060:    * LIBS: libapr-1.lib libaprutil-1.lib
061:    */
062: 
063:   if (encodedData) {
064:     char *input = encodedData;
065:     unsigned char *decoded;
066:     apr_size_t decLen = apr_base64_decode_len(encodedData);
067: 
068:     decoded = apr_palloc(pool, decLen);
069:     memset(decoded, 0, decLen);
070:     
071:     if ((apr_base64_decode(decoded, input))<=0) {
072:       goto _ERROR_;
073:     }
074:     
075:     decodedData = decoded;
076:   }
077: 
078:   if (decodedData) {
079:     apr_file_printf(astdout, "decoded: [%s][%dbytes]\n", decodedData, strlen(decodedData));
080:   }
081: 
082:   if (decodedData && !strcmp(input, decodedData)) {
083:     apr_file_printf(astdout, "Successfully decoded.\n");
084:   }
085: 
086: 
087:   apr_file_printf(astdout, "\ndone.\n");
088: 
089:   return 0; /* 正常終了 */
090:   
091:   _ERROR_:
092:   apr_file_printf(astderr, "failed!\n");
093:   return 1; /* 異常終了 */
094: }
095: 
Copyright (C) KAKU PROJECT (2009)KAKU PROJECT (2009)