dedup

deduplicating backup program
git clone git://git.2f30.org/dedup
Log | Files | Refs | README | LICENSE

commit 5bb4a9db996f3e7265fe0588fa03125a28cd9051
parent e401f4cd871cf10bbcaf31eee682c3ae34454b5d
Author: sin <sin@2f30.org>
Date:   Thu, 16 May 2019 13:58:55 +0300

encrypt: Use a switch instead of an if/elseif construct

Diffstat:
Mbcompress.c | 3++-
Mbencrypt.c | 58++++++++++++++++++++++++++++------------------------------
2 files changed, 30 insertions(+), 31 deletions(-)

diff --git a/bcompress.c b/bcompress.c @@ -208,10 +208,11 @@ bcput(struct bctx *bctx, void *buf, size_t n, unsigned char *md) break; } - /* Prepend compression descriptor */ + /* Prepare compression descriptor */ cd.type = cctx->type; memset(cd.reserved, 0, sizeof(cd.reserved)); cd.size = cn; + /* Prepend compression descriptor */ packcd(cbuf, &cd); if (bencryptops()->put(bctx, cbuf, CDSIZE + cn, md) < 0) { diff --git a/bencrypt.c b/bencrypt.c @@ -181,17 +181,18 @@ beput(struct bctx *bctx, void *buf, size_t n, unsigned char *md) struct ectx *ectx; struct ed ed; unsigned char *ebuf; + unsigned long long elen; size_t en; /* Calculate size of encrypted block */ ectx = bctx->ectx; - if (ectx->type == EDNONETYPE) { + switch (ectx->type) { + case EDNONETYPE: en = n; - } else if (ectx->type == EDCHACHATYPE) { + break; + case EDCHACHATYPE: en = n + crypto_aead_xchacha20poly1305_ietf_ABYTES; - } else { - seterr("invalid encryption type: %d", ectx->type); - return -1; + break; } ebuf = malloc(EDSIZE + en); @@ -200,36 +201,35 @@ beput(struct bctx *bctx, void *buf, size_t n, unsigned char *md) return -1; } - /* Prepend the encryption descriptor */ + /* Prepare encryption descriptor */ ed.type = ectx->type; memset(ed.reserved, 0, sizeof(ed.reserved)); ed.size = en; + /* Fill nonce buffer */ - if (ectx->type == EDNONETYPE) { + switch (ectx->type) { + case EDNONETYPE: memset(ed.nonce, 0, sizeof(ed.nonce)); - } else if (ectx->type == EDCHACHATYPE) { + break; + case EDCHACHATYPE: randombytes_buf(ed.nonce, sizeof(ed.nonce)); - } else { - free(ebuf); - seterr("invalid encryption type: %d", ectx->type); - return -1; + break; } + + /* Prepend encryption descriptor */ packed(ebuf, &ed); /* Encrypt block */ - if (ectx->type == EDNONETYPE) { + switch (ectx->type) { + case EDNONETYPE: memcpy(&ebuf[EDSIZE], buf, en); - } else if (ectx->type == EDCHACHATYPE) { - unsigned long long elen; - + break; + case EDCHACHATYPE: crypto_aead_xchacha20poly1305_ietf_encrypt(&ebuf[EDSIZE], &elen, buf, n, ebuf, EDSIZE, NULL, ed.nonce, ectx->key); assert(elen == en); - } else { - free(ebuf); - seterr("invalid encryption type: %d", ectx->type); - return -1; + break; } if (bstorageops()->put(bctx, ebuf, EDSIZE + en, md) < 0) { @@ -245,7 +245,9 @@ static int beget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) { struct ed ed; + struct ectx *ectx; unsigned char *ebuf; + unsigned long long dlen; size_t dn, size; /* Calculate maximum size of encrypted block */ @@ -265,7 +267,9 @@ beget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) unpacked(ebuf, &ed); /* Decrypt block */ - if (ed.type == EDNONETYPE) { + ectx = bctx->ectx; + switch (ed.type) { + case EDNONETYPE: dn = ed.size; if (*n < dn) { free(ebuf); @@ -273,10 +277,8 @@ beget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) return -1; } memcpy(buf, &ebuf[EDSIZE], dn); - } else if (ed.type == EDCHACHATYPE) { - struct ectx *ectx; - unsigned long long dlen; - + break; + case EDCHACHATYPE: dn = ed.size - crypto_aead_xchacha20poly1305_ietf_ABYTES; if (*n < dn) { free(ebuf); @@ -284,7 +286,6 @@ beget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) return -1; } - ectx = bctx->ectx; if (crypto_aead_xchacha20poly1305_ietf_decrypt(buf, &dlen, NULL, &ebuf[EDSIZE], ed.size, @@ -296,10 +297,7 @@ beget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n) } assert(dn == dlen); - } else { - free(ebuf); - seterr("invalid encryption type: %d", ed.type); - return -1; + break; } free(ebuf);