commit e401f4cd871cf10bbcaf31eee682c3ae34454b5d
parent 481193d0d350c006ac751b4d36eb3a27baafb0a0
Author: sin <sin@2f30.org>
Date: Thu, 16 May 2019 13:51:28 +0300
compress: Use a switch instead of an if/elseif construct
Do not check for invalid compression type in put()/get() as this is
already checked in creat()/open().
Diffstat:
M | bcompress.c | | | 47 | +++++++++++++++++++++++------------------------ |
1 file changed, 23 insertions(+), 24 deletions(-)
diff --git a/bcompress.c b/bcompress.c
@@ -163,18 +163,20 @@ bcput(struct bctx *bctx, void *buf, size_t n, unsigned char *md)
struct cd cd;
char *cbuf;
size_t cn;
+ int r;
/* Calculate compressed block size */
cctx = bctx->cctx;
- if (cctx->type == CDNONETYPE) {
+ switch (cctx->type) {
+ case CDNONETYPE:
cn = n;
- } else if (cctx->type == CDSNAPPYTYPE) {
+ break;
+ case CDSNAPPYTYPE:
cn = snappy_max_compressed_length(n);
- } else if (cctx->type == CDLZ4TYPE) {
+ break;
+ case CDLZ4TYPE:
cn = LZ4_compressBound(n);
- } else {
- seterr("invalid compression type: %d", cctx->type);
- return -1;
+ break;
}
cbuf = malloc(CDSIZE + cn);
@@ -184,17 +186,18 @@ bcput(struct bctx *bctx, void *buf, size_t n, unsigned char *md)
}
/* Compress block */
- if (cctx->type == CDNONETYPE) {
+ switch (cctx->type) {
+ case CDNONETYPE:
memcpy(&cbuf[CDSIZE], buf, cn);
- } else if (cctx->type == CDSNAPPYTYPE) {
+ break;
+ case CDSNAPPYTYPE:
if (snappy_compress(buf, n, &cbuf[CDSIZE], &cn) != SNAPPY_OK) {
free(cbuf);
seterr("snappy_compress: failed");
return -1;
}
- } else if (cctx->type == CDLZ4TYPE) {
- int r;
-
+ break;
+ case CDLZ4TYPE:
r = LZ4_compress_default(buf, &cbuf[CDSIZE], n, cn);
if (r < 0) {
free(cbuf);
@@ -202,10 +205,7 @@ bcput(struct bctx *bctx, void *buf, size_t n, unsigned char *md)
return -1;
}
cn = r;
- } else {
- free(cbuf);
- seterr("invalid compression type: %d", cctx->type);
- return -1;
+ break;
}
/* Prepend compression descriptor */
@@ -229,6 +229,7 @@ bcget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n)
struct cd cd;
char *cbuf;
size_t cn, un, size;
+ int r;
/* Calculate maximum compressed block size */
size = *n;
@@ -254,7 +255,8 @@ bcget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n)
unpackcd(cbuf, &cd);
/* Decompress block */
- if (cd.type == CDNONETYPE) {
+ switch (cd.type) {
+ case CDNONETYPE:
un = cd.size;
if (*n < un) {
free(cbuf);
@@ -262,7 +264,8 @@ bcget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n)
return -1;
}
memcpy(buf, &cbuf[CDSIZE], un);
- } else if (cd.type == CDSNAPPYTYPE) {
+ break;
+ case CDSNAPPYTYPE:
if (snappy_uncompressed_length(&cbuf[CDSIZE], cd.size,
&un) != SNAPPY_OK) {
free(cbuf);
@@ -282,9 +285,8 @@ bcget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n)
seterr("snappy_uncompress: failed");
return -1;
}
- } else if (cd.type == CDLZ4TYPE) {
- int r;
-
+ break;
+ case CDLZ4TYPE:
r = LZ4_decompress_safe(&cbuf[CDSIZE], buf, cd.size, *n);
if (r < 0) {
free(cbuf);
@@ -292,10 +294,7 @@ bcget(struct bctx *bctx, unsigned char *md, void *buf, size_t *n)
return -1;
}
un = r;
- } else {
- free(cbuf);
- seterr("invalid compression type: %d", cd.type);
- return -1;
+ break;
}
free(cbuf);