removed asserts and put proper error handling in compress.c

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6937 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2013-12-10 09:49:13 +00:00
parent 162ef94b70
commit 3e88b54ae3
5 changed files with 278 additions and 232 deletions

View File

@ -1139,7 +1139,12 @@ bool PGPHandler::encryptTextToFile(const PGPIdType& key_id,const std::string& te
return false ;
}
ops_encrypt_stream(info, public_key, NULL, ops_false, ops_true);
if(!ops_encrypt_stream(info, public_key, NULL, ops_false, ops_true))
{
std::cerr << "PGPHandler::encryptTextToFile(): ERROR: encryption failed." << std::endl;
return false ;
}
ops_write(text.c_str(), text.length(), info);
ops_teardown_file_write(info, fd);

View File

@ -25,7 +25,6 @@
#include <zlib.h>
#include <bzlib.h>
#include <assert.h>
#include <string.h>
#include <openpgpsdk/compress.h>
@ -77,9 +76,14 @@ static int zlib_compressed_data_reader(void *dest,size_t length,
ops_error_t **errors,
ops_reader_info_t *rinfo,
ops_parse_cb_info_t *cbinfo)
{
{
z_decompress_arg_t *arg=ops_reader_get_arg(rinfo);
assert(arg->type==OPS_C_ZIP || arg->type==OPS_C_ZLIB);
if(! (arg->type==OPS_C_ZIP || arg->type==OPS_C_ZLIB) ) // ASSERT(arg->type==OPS_C_ZIP || arg->type==OPS_C_ZLIB);
{
fprintf(stderr,"zlib_compressed_data_reader: unexpected compress format %d\n",arg->type) ;
return -1 ;
}
//ops_parser_content_t content;
int saved=length;
@ -148,7 +152,12 @@ static int zlib_compressed_data_reader(void *dest,size_t length,
}
arg->inflate_ret=ret;
}
assert(arg->zstream.next_out > &arg->out[arg->offset]);
if(!(arg->zstream.next_out > &arg->out[arg->offset])) // ASSERT(arg->zstream.next_out > &arg->out[arg->offset]);
{
fprintf(stderr,"decompression error: next packet is too large.\n");
return -1 ;
}
len=arg->zstream.next_out-&arg->out[arg->offset];
if(len > length)
len=length;
@ -158,16 +167,21 @@ static int zlib_compressed_data_reader(void *dest,size_t length,
}
return saved;
}
}
// \todo remove code duplication between this and zlib_compressed_data_reader
static int bzip2_compressed_data_reader(void *dest,size_t length,
ops_error_t **errors,
ops_reader_info_t *rinfo,
ops_parse_cb_info_t *cbinfo)
{
{
bz_decompress_arg_t *arg=ops_reader_get_arg(rinfo);
assert(arg->type==OPS_C_BZIP2);
if(!(arg->type==OPS_C_BZIP2)) // ASSERT(arg->type==OPS_C_BZIP2);
{
fprintf(stderr,"bzip2_compressed_data_reader: unexpected compress format %d\n",arg->type) ;
return -1 ;
}
//ops_parser_content_t content;
int saved=length;
@ -231,7 +245,11 @@ static int bzip2_compressed_data_reader(void *dest,size_t length,
}
arg->inflate_ret=ret;
}
assert(arg->bzstream.next_out > &arg->out[arg->offset]);
if(!(arg->bzstream.next_out > &arg->out[arg->offset])) // ASSERT(arg->bzstream.next_out > &arg->out[arg->offset]);
{
fprintf(stderr,"bzip2_compressed_data_reader: unexpected size in compressed packet.\n") ;
return -1 ;
}
len=arg->bzstream.next_out-&arg->out[arg->offset];
if(len > length)
len=length;
@ -241,7 +259,7 @@ static int bzip2_compressed_data_reader(void *dest,size_t length,
}
return saved;
}
}
/**
* \ingroup Core_Compress
@ -371,7 +389,7 @@ int ops_decompress(ops_region_t *region,ops_parse_info_t *parse_info,
ops_boolean_t ops_write_compressed(const unsigned char *data,
const unsigned int len,
ops_create_info_t *cinfo)
{
{
int r=0;
int sz_in=0;
int sz_out=0;
@ -388,13 +406,24 @@ ops_boolean_t ops_write_compressed(const unsigned char *data,
if (deflateInit(&compress->stream,level) != Z_OK)
{
// can't initialise
assert(0);
fprintf(stderr,"ops_write_compressed: can't initialise compression engine\n") ;
return ops_false ;
}
// do necessary transformation
// copy input to maintain const'ness of src
assert(compress->src==NULL);
assert(compress->dst==NULL);
if(!(compress->src==NULL)) // ASSERT(compress->src==NULL);
{
fprintf(stderr,"ops_write_compressed: compress->src should be NULL\n") ;
return ops_false ;
}
if(!(compress->dst==NULL)) // ASSERT(compress->dst==NULL);
{
fprintf(stderr,"ops_write_compressed: compress->dst should be NULL\n") ;
return ops_false ;
}
sz_in=len * sizeof (unsigned char);
sz_out= (sz_in * 1.01) + 12; // from zlib webpage
@ -412,14 +441,19 @@ ops_boolean_t ops_write_compressed(const unsigned char *data,
compress->stream.total_out=0;
r=deflate(&compress->stream, Z_FINISH);
assert(r==Z_STREAM_END); // need to loop if not
if(!(r==Z_STREAM_END)) // ASSERT(r==Z_STREAM_END); // need to loop if not
{
fprintf(stderr,"ops_write_compressed: compression failed.\n") ;
return ops_false ;
}
// write it out
return (ops_write_ptag(OPS_PTAG_CT_COMPRESSED, cinfo)
&& ops_write_length(1+compress->stream.total_out, cinfo)
&& ops_write_scalar(OPS_C_ZLIB,1,cinfo)
&& ops_write(compress->dst, compress->stream.total_out,cinfo));
}
}
// Writes out the header for the compressed packet. Invoked by the
@ -545,8 +579,8 @@ static void stream_compress_destroyer(ops_writer_info_t *winfo)
will be encoded as a compressed packet.
\param cinfo Write settings
*/
void ops_writer_push_compressed(ops_create_info_t *cinfo)
{
ops_boolean_t ops_writer_push_compressed(ops_create_info_t *cinfo)
{
// This is a streaming writer, so we don't know the length in
// advance. Use a partial writer to handle the partial body
// packet lengths.
@ -568,11 +602,13 @@ void ops_writer_push_compressed(ops_create_info_t *cinfo)
if (deflateInit(&compress->stream, level) != Z_OK)
// can't initialise. Is there a better way to handle this?
assert(0);
return ops_false ;
// And push writer on stack
ops_writer_push(cinfo, stream_compress_writer, stream_compress_finaliser,
stream_compress_destroyer, compress);
}
return ops_true ;
}
// EOF

View File

@ -31,4 +31,4 @@ ops_boolean_t ops_write_compressed(const unsigned char* data,
const unsigned int len,
ops_create_info_t *cinfo);
void ops_writer_push_compressed(ops_create_info_t *cinfo);
ops_boolean_t ops_writer_push_compressed(ops_create_info_t *cinfo);

View File

@ -253,22 +253,27 @@ ops_boolean_t ops_encrypt_file(const char* input_filename,
ops_create_info_delete(info);
\endcode
*/
extern void ops_encrypt_stream(ops_create_info_t* cinfo,
extern ops_boolean_t ops_encrypt_stream(ops_create_info_t* cinfo,
const ops_keydata_t* public_key,
const ops_secret_key_t* secret_key,
const ops_boolean_t compress,
const ops_boolean_t use_armour)
{
{
if (use_armour)
ops_writer_push_armoured_message(cinfo);
ops_writer_push_stream_encrypt_se_ip(cinfo, public_key);
if (compress)
ops_writer_push_compressed(cinfo);
if(! (compress && ops_writer_push_compressed(cinfo)))
return ops_false ;
if (secret_key != NULL)
ops_writer_push_signed(cinfo, OPS_SIG_BINARY, secret_key);
else
ops_writer_push_literal(cinfo);
}
return ops_true ;
}
/**
\ingroup HighLevel_Crypto

View File

@ -172,7 +172,7 @@ void ops_writer_push_encrypt(ops_create_info_t *info,
ops_boolean_t ops_encrypt_file(const char* input_filename, const char* output_filename, const ops_keydata_t *pub_key, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite);
ops_boolean_t ops_decrypt_file(const char* input_filename, const char* output_filename, ops_keyring_t *keyring, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite,ops_parse_cb_t* cb_get_passphrase);
extern void ops_encrypt_stream(ops_create_info_t* cinfo, const ops_keydata_t* public_key, const ops_secret_key_t* secret_key, const ops_boolean_t compress, const ops_boolean_t use_armour);
extern ops_boolean_t ops_encrypt_stream(ops_create_info_t* cinfo, const ops_keydata_t* public_key, const ops_secret_key_t* secret_key, const ops_boolean_t compress, const ops_boolean_t use_armour);
ops_boolean_t ops_decrypt_memory(const unsigned char *encrypted_memory,int em_length, unsigned char **decrypted_memory,int *out_length, ops_keyring_t* keyring, const ops_boolean_t use_armour, ops_parse_cb_t* cb_get_passphrase) ;
// Keys
ops_boolean_t ops_rsa_generate_keypair(const int numbits, const unsigned long e, ops_keydata_t* keydata);