Changeset 553

Show
Ignore:
Timestamp:
03/05/08 13:54:36
Author:
rachel
Message:

Added support for BZIP2 decompression

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/configure

    r544 r553  
    2525our %Subst=( 
    2626            CRYPTO_LIBS => '-lcrypto', 
    27             ZLIB => '-lz', 
     27            ZLIB => '-lz -lbz2', 
    2828            INCLUDES => '', 
    2929            CFLAGS => '', 
  • openpgpsdk/trunk/include/openpgpsdk/errors.h

    r543 r553  
    3636    OPS_E_P_MPI_FORMAT_ERROR    =OPS_E_P+4, 
    3737    OPS_E_P_PACKET_NOT_CONSUMED =OPS_E_P+5, 
     38    OPS_E_P_DECOMPRESSION_ERROR =OPS_E_P+6, 
    3839 
    3940    /* creator errors */ 
  • openpgpsdk/trunk/src/lib/adv_compress.c

    r549 r553  
    33    
    44#include <zlib.h> 
     5#include <bzlib.h> 
    56#include <assert.h> 
    67#include <string.h> 
     
    1718typedef struct 
    1819    { 
     20    ops_compression_type_t type; 
    1921    ops_region_t *region; 
    2022    unsigned char in[DECOMPRESS_BUFFER]; 
    2123    unsigned char out[DECOMPRESS_BUFFER]; 
    22     z_stream stream; 
     24    z_stream zstream; // ZIP and ZLIB 
    2325    size_t offset; 
    2426    int inflate_ret; 
    25     } decompress_arg_t; 
     27    } z_decompress_arg_t; 
     28 
     29typedef struct 
     30    { 
     31    ops_compression_type_t type; 
     32    ops_region_t *region; 
     33    char in[DECOMPRESS_BUFFER]; 
     34    char out[DECOMPRESS_BUFFER]; 
     35    bz_stream bzstream; // BZIP2 
     36    size_t offset; 
     37    int inflate_ret; 
     38    } bz_decompress_arg_t; 
    2639 
    2740typedef struct 
     
    3447#define ERR(err)        do { content.content.error.error=err; content.tag=OPS_PARSER_ERROR; ops_parse_cb(&content,cbinfo); return -1; } while(0) 
    3548 
    36 static int compressed_data_reader(void *dest,size_t length, 
     49// \todo remove code duplication between this and bzip2_compressed_data_reader 
     50static int zlib_compressed_data_reader(void *dest,size_t length, 
    3751                                  ops_error_t **errors, 
    3852                                  ops_reader_info_t *rinfo, 
    3953                                  ops_parse_cb_info_t *cbinfo) 
    4054    { 
    41     decompress_arg_t *arg=ops_reader_get_arg(rinfo); 
     55    z_decompress_arg_t *arg=ops_reader_get_arg(rinfo); 
     56    assert(arg->type==OPS_C_ZIP || arg->type==OPS_C_ZLIB); 
     57 
    4258    ops_parser_content_t content; 
    4359    int saved=length; 
    4460 
    4561    if(/*arg->region->indeterminate && */ arg->inflate_ret == Z_STREAM_END 
    46        && arg->stream.next_out == &arg->out[arg->offset]) 
    47        return 0; 
     62       && arg->zstream.next_out == &arg->out[arg->offset]) 
     63        return 0; 
    4864 
    4965    if(arg->region->length_read == arg->region->length) 
    50         { 
    51         if(arg->inflate_ret != Z_STREAM_END) 
    52             ERR("Compressed data didn't end when region ended."); 
    53     /* 
    54         else 
    55             return 0; 
    56     */ 
    57         } 
     66        { 
     67        if(arg->inflate_ret != Z_STREAM_END) 
     68            ERR("Compressed data didn't end when region ended."); 
     69        /* 
     70          else 
     71          return 0; 
     72          www.zlib.org 
     73        */ 
     74        } 
    5875 
    5976    while(length > 0) 
     
    6178        unsigned len; 
    6279 
    63         if(&arg->out[arg->offset] == arg->stream.next_out) 
     80        if(&arg->out[arg->offset] == arg->zstream.next_out) 
    6481            { 
    6582            int ret; 
    6683 
    67             arg->stream.next_out=arg->out; 
    68             arg->stream.avail_out=sizeof arg->out; 
     84            arg->zstream.next_out=arg->out; 
     85            arg->zstream.avail_out=sizeof arg->out; 
    6986            arg->offset=0; 
    70             if(arg->stream.avail_in == 0) 
     87            if(arg->zstream.avail_in == 0) 
    7188                { 
    7289                unsigned n=arg->region->length; 
     
    85102                    return -1; 
    86103 
    87                 arg->stream.next_in=arg->in; 
    88                 arg->stream.avail_in=arg->region->indeterminate 
     104                arg->zstream.next_in=arg->in; 
     105                arg->zstream.avail_in=arg->region->indeterminate 
    89106                    ? arg->region->last_read : n; 
    90107                } 
    91108 
    92             ret=inflate(&arg->stream,Z_SYNC_FLUSH); 
     109            ret=inflate(&arg->zstream,Z_SYNC_FLUSH); 
    93110            if(ret == Z_STREAM_END) 
    94111                { 
     
    100117                { 
    101118                fprintf(stderr,"ret=%d\n",ret); 
    102                 ERR(arg->stream.msg); 
     119                ERR(arg->zstream.msg); 
    103120                } 
    104121            arg->inflate_ret=ret; 
    105122            } 
    106         assert(arg->stream.next_out > &arg->out[arg->offset]); 
    107         len=arg->stream.next_out-&arg->out[arg->offset]; 
     123        assert(arg->zstream.next_out > &arg->out[arg->offset]); 
     124        len=arg->zstream.next_out-&arg->out[arg->offset]; 
    108125        if(len > length) 
    109126            len=length; 
     
    116133    } 
    117134 
     135// \todo remove code duplication between this and zlib_compressed_data_reader 
     136static int bzip2_compressed_data_reader(void *dest,size_t length, 
     137                                  ops_error_t **errors, 
     138                                  ops_reader_info_t *rinfo, 
     139                                  ops_parse_cb_info_t *cbinfo) 
     140    { 
     141    bz_decompress_arg_t *arg=ops_reader_get_arg(rinfo); 
     142    assert(arg->type==OPS_C_BZIP2); 
     143 
     144    ops_parser_content_t content; 
     145    int saved=length; 
     146 
     147    if(arg->inflate_ret == BZ_STREAM_END 
     148       && arg->bzstream.next_out == &arg->out[arg->offset]) 
     149        return 0; 
     150 
     151    if(arg->region->length_read == arg->region->length) 
     152        { 
     153        if(arg->inflate_ret != BZ_STREAM_END) 
     154            ERR("Compressed data didn't end when region ended."); 
     155        } 
     156 
     157    while(length > 0) 
     158        { 
     159        unsigned len; 
     160 
     161        if(&arg->out[arg->offset] == arg->bzstream.next_out) 
     162            { 
     163            int ret; 
     164 
     165            arg->bzstream.next_out=(char *) arg->out; 
     166            arg->bzstream.avail_out=sizeof arg->out; 
     167            arg->offset=0; 
     168            if(arg->bzstream.avail_in == 0) 
     169                { 
     170                unsigned n=arg->region->length; 
     171 
     172                if(!arg->region->indeterminate) 
     173                    { 
     174                    n-=arg->region->length_read; 
     175                    if(n > sizeof arg->in) 
     176                        n=sizeof arg->in; 
     177                    } 
     178                else 
     179                    n=sizeof arg->in; 
     180 
     181                if(!ops_stacked_limited_read((unsigned char *)arg->in,n,arg->region, 
     182                                             errors,rinfo,cbinfo)) 
     183                    return -1; 
     184 
     185                arg->bzstream.next_in=arg->in; 
     186                arg->bzstream.avail_in=arg->region->indeterminate 
     187                    ? arg->region->last_read : n; 
     188                } 
     189 
     190            ret=BZ2_bzDecompress(&arg->bzstream); 
     191            if(ret == BZ_STREAM_END) 
     192                { 
     193                if(!arg->region->indeterminate 
     194                   && arg->region->length_read != arg->region->length) 
     195                    ERR("Compressed stream ended before packet end."); 
     196                } 
     197            else if(ret != BZ_OK) 
     198                { 
     199                fprintf(stderr,"ret=%d\n",ret); 
     200        //              ERR(arg->bzstream.msg); //\todo add error handling 
     201                } 
     202            arg->inflate_ret=ret; 
     203            } 
     204        assert(arg->bzstream.next_out > &arg->out[arg->offset]); 
     205        len=arg->bzstream.next_out-&arg->out[arg->offset]; 
     206        if(len > length) 
     207            len=length; 
     208        memcpy(dest,&arg->out[arg->offset],len); 
     209        arg->offset+=len; 
     210        length-=len; 
     211        } 
     212 
     213    return saved; 
     214    } 
     215 
    118216/** 
    119217 * \ingroup Utils 
     
    126224                   ops_compression_type_t type) 
    127225    { 
    128     decompress_arg_t arg; 
     226    z_decompress_arg_t z_arg; 
     227    bz_decompress_arg_t bz_arg; 
    129228    int ret; 
    130229 
    131     memset(&arg,'\0',sizeof arg); 
    132  
    133     arg.region=region; 
    134  
    135     arg.stream.next_in=Z_NULL; 
    136     arg.stream.avail_in=0; 
    137     arg.stream.next_out=arg.out; 
    138     arg.offset=0; 
    139     arg.stream.zalloc=Z_NULL; 
    140     arg.stream.zfree=Z_NULL; 
    141  
    142     if(type == OPS_C_ZIP) 
    143         ret=inflateInit2(&arg.stream,-15); 
    144     else if(type == OPS_C_ZLIB) 
    145         ret=inflateInit(&arg.stream); 
    146     else if (type == OPS_C_BZIP2) 
    147         { 
     230    switch (type) 
     231        { 
     232    case OPS_C_ZIP: 
     233    case OPS_C_ZLIB: 
     234        memset(&z_arg,'\0',sizeof z_arg); 
     235 
     236        z_arg.region=region; 
     237        z_arg.offset=0; 
     238        z_arg.type=type; 
     239 
     240        z_arg.zstream.next_in=Z_NULL; 
     241        z_arg.zstream.avail_in=0; 
     242        z_arg.zstream.next_out=z_arg.out; 
     243        z_arg.zstream.zalloc=Z_NULL; 
     244        z_arg.zstream.zfree=Z_NULL; 
     245        z_arg.zstream.opaque=Z_NULL; 
     246        break; 
     247 
     248    case OPS_C_BZIP2: 
     249        memset(&bz_arg,'\0',sizeof bz_arg); 
     250 
     251        bz_arg.region=region; 
     252        bz_arg.offset=0; 
     253        bz_arg.type=type; 
     254 
     255        bz_arg.bzstream.next_in=NULL; 
     256        bz_arg.bzstream.avail_in=0; 
     257        bz_arg.bzstream.next_out=bz_arg.out; 
     258        bz_arg.bzstream.bzalloc=NULL; 
     259        bz_arg.bzstream.bzfree=NULL; 
     260        bz_arg.bzstream.opaque=NULL; 
     261        break; 
     262 
     263    default: 
     264        OPS_ERROR_1(&parse_info->errors, OPS_E_ALG_UNSUPPORTED_COMPRESS_ALG, "Compression algorithm %d is not yet supported", type); 
     265        return 0; 
     266        } 
     267 
     268    switch(type) 
     269        { 
     270    case OPS_C_ZIP: 
     271        ret=inflateInit2(&z_arg.zstream,-15); 
     272        break; 
     273 
     274    case OPS_C_ZLIB: 
     275        ret=inflateInit(&z_arg.zstream); 
     276        break; 
     277 
     278    case OPS_C_BZIP2: 
     279        /* 
    148280        OPS_ERROR_1(&parse_info->errors, OPS_E_ALG_UNSUPPORTED_COMPRESS_ALG, "Compression algorithm %s is not yet supported", "BZIP2"); 
    149281        return 0; 
    150         } 
    151     else 
    152         { 
     282        */ 
     283        ret=BZ2_bzDecompressInit(&bz_arg.bzstream, 1, 0); 
     284        break; 
     285 
     286    default: 
    153287        OPS_ERROR_1(&parse_info->errors, OPS_E_ALG_UNSUPPORTED_COMPRESS_ALG, "Compression algorithm %d is not yet supported", type); 
    154288        return 0; 
    155289        } 
    156290 
    157     if(ret != Z_OK) 
    158         { 
    159         fprintf(stderr,"ret=%d\n",ret); 
    160         return 0; 
    161         } 
    162  
    163     ops_reader_push(parse_info,compressed_data_reader,NULL,&arg); 
     291    switch (type) 
     292        { 
     293    case OPS_C_ZIP: 
     294    case OPS_C_ZLIB: 
     295        if(ret != Z_OK) 
     296            { 
     297            OPS_ERROR_1(&parse_info->errors, OPS_E_P_DECOMPRESSION_ERROR, "Cannot initialise ZIP or ZLIB stream for decompression: error=%d", ret); 
     298            return 0; 
     299            } 
     300        ops_reader_push(parse_info,zlib_compressed_data_reader,NULL,&z_arg); 
     301        break; 
     302 
     303    case OPS_C_BZIP2: 
     304        if (ret != BZ_OK) 
     305            { 
     306            OPS_ERROR_1(&parse_info->errors, OPS_E_P_DECOMPRESSION_ERROR, "Cannot initialise BZIP2 stream for decompression: error=%d", ret); 
     307            return 0; 
     308            } 
     309        ops_reader_push(parse_info,bzip2_compressed_data_reader,NULL,&bz_arg); 
     310        break; 
     311 
     312    default: 
     313        OPS_ERROR_1(&parse_info->errors, OPS_E_ALG_UNSUPPORTED_COMPRESS_ALG, "Compression algorithm %d is not yet supported", type); 
     314        return 0; 
     315        } 
    164316 
    165317    ret=ops_parse(parse_info); 
  • openpgpsdk/trunk/tests/test_rsa_decrypt.c

    r547 r553  
    2222static char *current_passphrase=NULL; 
    2323 
    24 /* \todo add support for bzip2 
    2524static char *algos[]={ "zip", "zlib", "bzip2" }; 
    2625static int n_algos=3; 
    27 */ 
    28  
    29 static char *algos[]={ "zip", "zlib" }; 
    30 static int n_algos=2; 
    3126 
    3227static ops_parse_cb_return_t