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 ; 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_write(text.c_str(), text.length(), info);
ops_teardown_file_write(info, fd); ops_teardown_file_write(info, fd);

View file

@ -25,7 +25,6 @@
#include <zlib.h> #include <zlib.h>
#include <bzlib.h> #include <bzlib.h>
#include <assert.h>
#include <string.h> #include <string.h>
#include <openpgpsdk/compress.h> #include <openpgpsdk/compress.h>
@ -79,7 +78,12 @@ static int zlib_compressed_data_reader(void *dest,size_t length,
ops_parse_cb_info_t *cbinfo) ops_parse_cb_info_t *cbinfo)
{ {
z_decompress_arg_t *arg=ops_reader_get_arg(rinfo); 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; //ops_parser_content_t content;
int saved=length; int saved=length;
@ -148,7 +152,12 @@ static int zlib_compressed_data_reader(void *dest,size_t length,
} }
arg->inflate_ret=ret; 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]; len=arg->zstream.next_out-&arg->out[arg->offset];
if(len > length) if(len > length)
len=length; len=length;
@ -167,7 +176,12 @@ static int bzip2_compressed_data_reader(void *dest,size_t length,
ops_parse_cb_info_t *cbinfo) ops_parse_cb_info_t *cbinfo)
{ {
bz_decompress_arg_t *arg=ops_reader_get_arg(rinfo); 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; //ops_parser_content_t content;
int saved=length; int saved=length;
@ -231,7 +245,11 @@ static int bzip2_compressed_data_reader(void *dest,size_t length,
} }
arg->inflate_ret=ret; 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]; len=arg->bzstream.next_out-&arg->out[arg->offset];
if(len > length) if(len > length)
len=length; len=length;
@ -388,13 +406,24 @@ ops_boolean_t ops_write_compressed(const unsigned char *data,
if (deflateInit(&compress->stream,level) != Z_OK) if (deflateInit(&compress->stream,level) != Z_OK)
{ {
// can't initialise // can't initialise
assert(0);
fprintf(stderr,"ops_write_compressed: can't initialise compression engine\n") ;
return ops_false ;
} }
// do necessary transformation // do necessary transformation
// copy input to maintain const'ness of src // 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_in=len * sizeof (unsigned char);
sz_out= (sz_in * 1.01) + 12; // from zlib webpage sz_out= (sz_in * 1.01) + 12; // from zlib webpage
@ -412,7 +441,12 @@ ops_boolean_t ops_write_compressed(const unsigned char *data,
compress->stream.total_out=0; compress->stream.total_out=0;
r=deflate(&compress->stream, Z_FINISH); 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 // write it out
return (ops_write_ptag(OPS_PTAG_CT_COMPRESSED, cinfo) return (ops_write_ptag(OPS_PTAG_CT_COMPRESSED, cinfo)
@ -545,7 +579,7 @@ static void stream_compress_destroyer(ops_writer_info_t *winfo)
will be encoded as a compressed packet. will be encoded as a compressed packet.
\param cinfo Write settings \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 // This is a streaming writer, so we don't know the length in
// advance. Use a partial writer to handle the partial body // advance. Use a partial writer to handle the partial body
@ -568,11 +602,13 @@ void ops_writer_push_compressed(ops_create_info_t *cinfo)
if (deflateInit(&compress->stream, level) != Z_OK) if (deflateInit(&compress->stream, level) != Z_OK)
// can't initialise. Is there a better way to handle this? // can't initialise. Is there a better way to handle this?
assert(0); return ops_false ;
// And push writer on stack // And push writer on stack
ops_writer_push(cinfo, stream_compress_writer, stream_compress_finaliser, ops_writer_push(cinfo, stream_compress_writer, stream_compress_finaliser,
stream_compress_destroyer, compress); stream_compress_destroyer, compress);
return ops_true ;
} }
// EOF // EOF

View file

@ -31,4 +31,4 @@ ops_boolean_t ops_write_compressed(const unsigned char* data,
const unsigned int len, const unsigned int len,
ops_create_info_t *cinfo); 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,7 +253,7 @@ ops_boolean_t ops_encrypt_file(const char* input_filename,
ops_create_info_delete(info); ops_create_info_delete(info);
\endcode \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_keydata_t* public_key,
const ops_secret_key_t* secret_key, const ops_secret_key_t* secret_key,
const ops_boolean_t compress, const ops_boolean_t compress,
@ -261,13 +261,18 @@ extern void ops_encrypt_stream(ops_create_info_t* cinfo,
{ {
if (use_armour) if (use_armour)
ops_writer_push_armoured_message(cinfo); ops_writer_push_armoured_message(cinfo);
ops_writer_push_stream_encrypt_se_ip(cinfo, public_key); 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) if (secret_key != NULL)
ops_writer_push_signed(cinfo, OPS_SIG_BINARY, secret_key); ops_writer_push_signed(cinfo, OPS_SIG_BINARY, secret_key);
else else
ops_writer_push_literal(cinfo); ops_writer_push_literal(cinfo);
return ops_true ;
} }
/** /**

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_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); 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) ; 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 // Keys
ops_boolean_t ops_rsa_generate_keypair(const int numbits, const unsigned long e, ops_keydata_t* keydata); ops_boolean_t ops_rsa_generate_keypair(const int numbits, const unsigned long e, ops_keydata_t* keydata);