Changeset 553
- Timestamp:
- 03/05/08 13:54:36
- Files:
-
- openpgpsdk/trunk/configure (modified) (1 diff)
- openpgpsdk/trunk/include/openpgpsdk/errors.h (modified) (1 diff)
- openpgpsdk/trunk/src/lib/adv_compress.c (modified) (8 diffs)
- openpgpsdk/trunk/tests/test_rsa_decrypt.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/configure
r544 r553 25 25 our %Subst=( 26 26 CRYPTO_LIBS => '-lcrypto', 27 ZLIB => '-lz ',27 ZLIB => '-lz -lbz2', 28 28 INCLUDES => '', 29 29 CFLAGS => '', openpgpsdk/trunk/include/openpgpsdk/errors.h
r543 r553 36 36 OPS_E_P_MPI_FORMAT_ERROR =OPS_E_P+4, 37 37 OPS_E_P_PACKET_NOT_CONSUMED =OPS_E_P+5, 38 OPS_E_P_DECOMPRESSION_ERROR =OPS_E_P+6, 38 39 39 40 /* creator errors */ openpgpsdk/trunk/src/lib/adv_compress.c
r549 r553 3 3 4 4 #include <zlib.h> 5 #include <bzlib.h> 5 6 #include <assert.h> 6 7 #include <string.h> … … 17 18 typedef struct 18 19 { 20 ops_compression_type_t type; 19 21 ops_region_t *region; 20 22 unsigned char in[DECOMPRESS_BUFFER]; 21 23 unsigned char out[DECOMPRESS_BUFFER]; 22 z_stream stream;24 z_stream zstream; // ZIP and ZLIB 23 25 size_t offset; 24 26 int inflate_ret; 25 } decompress_arg_t; 27 } z_decompress_arg_t; 28 29 typedef 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; 26 39 27 40 typedef struct … … 34 47 #define ERR(err) do { content.content.error.error=err; content.tag=OPS_PARSER_ERROR; ops_parse_cb(&content,cbinfo); return -1; } while(0) 35 48 36 static int compressed_data_reader(void *dest,size_t length, 49 // \todo remove code duplication between this and bzip2_compressed_data_reader 50 static int zlib_compressed_data_reader(void *dest,size_t length, 37 51 ops_error_t **errors, 38 52 ops_reader_info_t *rinfo, 39 53 ops_parse_cb_info_t *cbinfo) 40 54 { 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 42 58 ops_parser_content_t content; 43 59 int saved=length; 44 60 45 61 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; 48 64 49 65 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 } 58 75 59 76 while(length > 0) … … 61 78 unsigned len; 62 79 63 if(&arg->out[arg->offset] == arg-> stream.next_out)80 if(&arg->out[arg->offset] == arg->zstream.next_out) 64 81 { 65 82 int ret; 66 83 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; 69 86 arg->offset=0; 70 if(arg-> stream.avail_in == 0)87 if(arg->zstream.avail_in == 0) 71 88 { 72 89 unsigned n=arg->region->length; … … 85 102 return -1; 86 103 87 arg-> stream.next_in=arg->in;88 arg-> stream.avail_in=arg->region->indeterminate104 arg->zstream.next_in=arg->in; 105 arg->zstream.avail_in=arg->region->indeterminate 89 106 ? arg->region->last_read : n; 90 107 } 91 108 92 ret=inflate(&arg-> stream,Z_SYNC_FLUSH);109 ret=inflate(&arg->zstream,Z_SYNC_FLUSH); 93 110 if(ret == Z_STREAM_END) 94 111 { … … 100 117 { 101 118 fprintf(stderr,"ret=%d\n",ret); 102 ERR(arg-> stream.msg);119 ERR(arg->zstream.msg); 103 120 } 104 121 arg->inflate_ret=ret; 105 122 } 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]; 108 125 if(len > length) 109 126 len=length; … … 116 133 } 117 134 135 // \todo remove code duplication between this and zlib_compressed_data_reader 136 static 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 118 216 /** 119 217 * \ingroup Utils … … 126 224 ops_compression_type_t type) 127 225 { 128 decompress_arg_t arg; 226 z_decompress_arg_t z_arg; 227 bz_decompress_arg_t bz_arg; 129 228 int ret; 130 229 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 /* 148 280 OPS_ERROR_1(&parse_info->errors, OPS_E_ALG_UNSUPPORTED_COMPRESS_ALG, "Compression algorithm %s is not yet supported", "BZIP2"); 149 281 return 0; 150 } 151 else 152 { 282 */ 283 ret=BZ2_bzDecompressInit(&bz_arg.bzstream, 1, 0); 284 break; 285 286 default: 153 287 OPS_ERROR_1(&parse_info->errors, OPS_E_ALG_UNSUPPORTED_COMPRESS_ALG, "Compression algorithm %d is not yet supported", type); 154 288 return 0; 155 289 } 156 290 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 } 164 316 165 317 ret=ops_parse(parse_info); openpgpsdk/trunk/tests/test_rsa_decrypt.c
r547 r553 22 22 static char *current_passphrase=NULL; 23 23 24 /* \todo add support for bzip225 24 static char *algos[]={ "zip", "zlib", "bzip2" }; 26 25 static int n_algos=3; 27 */28 29 static char *algos[]={ "zip", "zlib" };30 static int n_algos=2;31 26 32 27 static ops_parse_cb_return_t
