From bedea4dbec8066cd864a2c3e10dac24223613239 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Wed, 16 Mar 2011 10:17:48 +1100 Subject: [PATCH] Consolidate cbc padding into a macro. --- stream.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/stream.c b/stream.c index cad1176..41e5840 100644 --- a/stream.c +++ b/stream.c @@ -58,6 +58,8 @@ #define STREAM_BUFSIZE (1024 * 1024 * 10) +#define CBC_PAD(LEN) ((LEN % CBC_LEN) ? (CBC_LEN - (LEN % CBC_LEN)) : 0) + static struct compress_thread{ uchar *s_buf; /* Uncompressed buffer -> Compressed buffer */ uchar c_type; /* Compression type */ @@ -1174,8 +1176,7 @@ retry: /* We must pad the block length to a mutliple of CBC_LEN to be * able to encrypt. We pad it with random data */ - if (cti->c_len % CBC_LEN) - encrypt_pad = CBC_LEN - (cti->c_len % CBC_LEN); + encrypt_pad = CBC_PAD(cti->c_len); padded_len = cti->c_len + encrypt_pad; if (encrypt_pad) get_rand(cti->s_buf + cti->c_len, encrypt_pad); @@ -1298,7 +1299,7 @@ static void clear_buffer(rzip_control *control, struct stream_info *sinfo, int s /* The stream buffer has been given to the thread, allocate a * new one. Allocate slightly more in case we need padding for * encryption */ - sinfo->s[streamno].buf = malloc(sinfo->bufsize + CBC_LEN); + sinfo->s[streamno].buf = malloc(sinfo->bufsize + CBC_PAD(sinfo->bufsize)); if (unlikely(!sinfo->s[streamno].buf)) fatal("Unable to malloc buffer of size %lld in flush_buffer\n", sinfo->bufsize); sinfo->s[streamno].buflen = 0; @@ -1423,15 +1424,15 @@ fill_another: fsync(control->fd_out); - s_buf = malloc(c_len + CBC_LEN); + s_buf = malloc(c_len + CBC_PAD(c_len)); if (unlikely(c_len && !s_buf)) fatal("Unable to malloc buffer of size %lld in fill_buffer\n", c_len); sinfo->ram_alloced += c_len; /* If the data was encrypted, we need to read the padded data * at the end and then discard it once it's decrypted */ - if (ENCRYPT && c_len % CBC_LEN) - padded_len = c_len + CBC_LEN - (c_len % CBC_LEN); + if (ENCRYPT) + padded_len = c_len + CBC_PAD(c_len); else padded_len = c_len;