/* kcrypt.c * 暗号化・復号のサンプル */ #define USAGE "Usage: kcrypt " #include "apr_general.h" #include "apr_file_io.h" #include "apr_errno.h" #include "apr_strings.h" #include "apr_buckets.h" #include "mycipher.h" #include "mybb.h" #include "mystab.h" int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) { int error_flag = 0; apr_status_t rv; char error_buf[256]; char *cipher_alg = NULL; char *encdec = NULL; char *input_file = NULL; char *output_file = NULL; int enc = 0; if (ac < 5) { apr_file_printf(astderr, "%s\n", USAGE); error_flag = 1; goto _FINALLY_; } cipher_alg = av[1]; encdec = av[2]; input_file = av[3]; output_file = av[4]; if (!strcmp(encdec, "enc")) { enc = 1; } init_mycipher(); if (1) { apr_bucket_alloc_t *ba = NULL; apr_bucket_brigade *bbin = NULL; apr_bucket_brigade *bbout = NULL; apr_file_t *fpin = NULL; apr_file_t *fpout = NULL; apr_finfo_t finfo; if (APR_SUCCESS != (rv = apr_file_open(&fpin, input_file, APR_FOPEN_READ|APR_FOPEN_BINARY, APR_OS_DEFAULT, pool))) { apr_file_printf(astderr, "ERROR: apr_file_open: %s\n", apr_strerror(rv, error_buf, sizeof(error_buf))); error_flag = 1; goto _FINALLY_; } if (APR_SUCCESS != (rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, fpin))) { apr_file_printf(astderr, "ERROR: apr_file_info_get: %s ", apr_strerror(rv, error_buf, sizeof(error_buf))); apr_file_close(fpin); error_flag = 1; goto _FINALLY_; } if (APR_SUCCESS != (rv = apr_file_open(&fpout, output_file, APR_FOPEN_WRITE|APR_FOPEN_CREATE|APR_FOPEN_TRUNCATE|APR_FOPEN_BINARY, APR_OS_DEFAULT, pool))) { apr_file_close(fpin); apr_file_printf(astderr, "ERROR: apr_file_open: %s ", apr_strerror(rv, error_buf, sizeof(error_buf))); error_flag = 1; goto _FINALLY_; } ba = apr_bucket_alloc_create(pool); bbin = apr_brigade_create(pool, ba); bbout = apr_brigade_create(pool, ba); apr_brigade_insert_file(bbin, fpin, 0, finfo.size, pool); #ifdef DEBUG printf("#finfo.size=[%d]\n", finfo.size); #endif /* メモ:鍵やIVは乱数にするのが普通だけど…。 */ if(kencdec_from_bb_to_bb(cipher_alg, "0123456789ABCDEF", 16, "This is a pencil", 16, (apr_size_t)finfo.size, bbin, bbout, enc)) { write_from_bb_to_fp (bbout, fpout); } else { error_flag = 1; } apr_file_close(fpout); fpin = NULL; apr_file_close(fpout); fpout = NULL; apr_brigade_destroy(bbin); bbin = NULL; apr_brigade_destroy(bbout); bbout =NULL; apr_bucket_alloc_destroy(ba); ba = NULL; } _FINALLY_: if (error_flag) { apr_file_printf(astderr, "failed!\n"); return 1; /* 異常終了 */ } apr_file_printf(astdout, "\ndone.\n"); return 0; /* 正常終了 */ } /* end of main */