kdechunk.c
最終更新:2009/10/10
001: /* kdechunk.c 002: * チャンク形式データ用デコーダ 003: */ 004: 005: #define USAGE "Usage: kdechunk <chunk-encoded-filename> <out-filename>" 006: 007: #include <stdlib.h> /* strtol */ 008: 009: #include "apr_general.h" 010: #include "apr_file_io.h" 011: 012: #include "apr_strings.h" 013: #include "apr_buckets.h" 014: 015: #include "mystab.h" 016: #include "mybb.h" 017: 018: int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) { 019: apr_status_t rv; 020: char error_buf[256]; 021: 022: apr_finfo_t finfo; 023: 024: apr_file_t *fpin = NULL; 025: apr_file_t *fpout = NULL; 026: 027: char *infilename = NULL; 028: char *outfilename = NULL; 029: 030: apr_bucket_alloc_t *ba = NULL; 031: apr_bucket_brigade *bb = NULL; 032: 033: if (ac < 2) { 034: apr_file_printf(astderr, "%s\n", USAGE); 035: goto _ERROR_; 036: } 037: 038: infilename = av[1]; 039: outfilename = av[2]; 040: 041: ba = apr_bucket_alloc_create(pool); 042: bb = apr_brigade_create(pool, ba); 043: 044: if (APR_SUCCESS != 045: (rv = apr_file_open(&fpin, infilename, APR_FOPEN_READ|APR_FOPEN_BINARY, APR_OS_DEFAULT, pool))) { 046: apr_file_printf(astderr, "ERROR: apr_file_open: %s\n", apr_strerror(rv, error_buf, sizeof(error_buf))); 047: goto _ERROR_; 048: } 049: 050: if (APR_SUCCESS != (rv = apr_file_info_get(&finfo, APR_FINFO_MIN, fpin))) { 051: apr_file_printf(astderr, "ERROR: apr_file_info_get: %s\n", apr_strerror(rv, error_buf, sizeof(error_buf))); 052: goto _ERROR_; 053: } 054: 055: if (APR_SUCCESS != 056: (rv = apr_file_open(&fpout, outfilename, 057: APR_FOPEN_WRITE|APR_FOPEN_CREATE|APR_FOPEN_TRUNCATE|APR_FOPEN_BINARY, APR_OS_DEFAULT, pool))) { 058: apr_file_printf(astderr, "ERROR: apr_file_open: %s\n", apr_strerror(rv, error_buf, sizeof(error_buf))); 059: goto _ERROR_; 060: } 061: 062: /* 入力ファイルをストリームに挿入 */ 063: 064: if (fpin && bb) { 065: apr_brigade_insert_file(bb, fpin, 0, finfo.size, pool); 066: } 067: 068: if (bb && ba) { 069: 070: apr_bucket_brigade *bb2 = apr_brigade_create(pool, ba); 071: 072: /* ヘッダの読み出し。読み出すのみ。 */ 073: 074: if (read_entity_header_CRLF(bb) == 0) { 075: apr_file_printf(astderr, "ERROR: read_entity_header_CRLF\n"); 076: goto _ERROR_; 077: } 078: 079: /* チャンク形式のボディをデコード。デコード結果はストリーム bb2 に格納。 */ 080: 081: if (decode_chunked_body (bb, bb2) < 0) { 082: apr_file_printf(astderr, "ERROR: decode_chunked_body\n"); 083: goto _ERROR_; 084: } 085: 086: /* デコード結果のストリームをファイルに書き込み。 */ 087: 088: write_from_bb_to_fp (bb2, fpout); 089: } 090: 091: apr_file_printf(astdout, "\ndone.\n"); 092: 093: if (bb) { 094: apr_brigade_destroy(bb); 095: bb = NULL; 096: } 097: 098: if (ba) { 099: apr_bucket_alloc_destroy(ba); 100: ba = NULL; 101: } 102: 103: if (fpin) { 104: apr_file_close(fpin); 105: fpin = NULL; 106: } 107: 108: if (fpout) { 109: apr_file_close(fpout); 110: fpout = NULL; 111: } 112: 113: return 0; /* 正常終了 */ 114: 115: _ERROR_: 116: 117: if (bb) { 118: apr_brigade_destroy(bb); 119: bb = NULL; 120: } 121: 122: if (ba) { 123: apr_bucket_alloc_destroy(ba); 124: ba = NULL; 125: } 126: 127: if (fpin) { 128: apr_file_close(fpin); 129: fpin = NULL; 130: } 131: 132: if (fpout) { 133: apr_file_close(fpout); 134: fpout = NULL; 135: } 136: 137: apr_file_printf(astderr, "\nfailed.\n"); 138: return 1; /* 異常終了 */ 139: }
KAKU PROJECT (2009) |