Skip to content

Commit

Permalink
minor code quality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jun 22, 2024
1 parent 20b340d commit 4f5af3c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
14 changes: 14 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
comment: false

coverage:
status:
project:
default:
target: auto
threshold: 1%
informational: true
patch:
default:
target: auto
threshold: 1%
informational: true
27 changes: 14 additions & 13 deletions src/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,

if (n > (SIZE_MAX - 1) / 4) {
*olen = SIZE_MAX;
return -1;
return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
}

n *= 4;

if ((dlen < n + 1) || (NULL == dst)) {
*olen = n + 1;
return -1;
return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
}

n = (slen / 3) * 3;
Expand Down Expand Up @@ -174,21 +174,21 @@ int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,
}

if (spaces_present) {
return -2;
return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
}

if (src[i] > 127) { return -2; }
if (src[i] > 127) { return MBEDTLS_ERR_BASE64_INVALID_CHARACTER; }

if (src[i] == '=') {
if (++equals > 2) {
return -2;
return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
}
} else {
if (equals != 0) {
return -2;
return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
}
if (mbedtls_ct_base64_dec_value(src[i]) < 0) {
return -2;
return MBEDTLS_ERR_BASE64_INVALID_CHARACTER;
}
}
n++;
Expand All @@ -204,7 +204,7 @@ int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,

if (dst == NULL || dlen < n) {
*olen = n;
return -1;
return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
}

equals = 0;
Expand Down Expand Up @@ -341,18 +341,19 @@ static nano_buf nano_any_buf(const SEXP x) {
if (XLENGTH(x) == 1 && ATTRIB(x) == R_NilValue) {
const char *s = CHAR(STRING_ELT(x, 0));
NANO_INIT(&buf, (unsigned char *) s, strlen(s));
return buf;
goto resume;
}
break;
case RAWSXP:
if (ATTRIB(x) == R_NilValue) {
NANO_INIT(&buf, (unsigned char *) DATAPTR_RO(x), XLENGTH(x));
return buf;
goto resume;
}
}

nano_serialize_xdr(&buf, x);

resume:
return buf;

}
Expand All @@ -370,7 +371,7 @@ SEXP secretbase_base64enc(SEXP x, SEXP convert) {
unsigned char *buf = R_Calloc(olen, unsigned char);
xc = mbedtls_base64_encode(buf, olen, &olen, hash.buf, hash.cur);
NANO_FREE(hash);
CHECK_ERROR(xc);
CHECK_ERROR(xc, buf);

if (*(int *) DATAPTR_RO(convert)) {
out = rawToChar(buf, olen);
Expand Down Expand Up @@ -406,11 +407,11 @@ SEXP secretbase_base64dec(SEXP x, SEXP convert) {
}

xc = mbedtls_base64_decode(NULL, 0, &olen, inbuf, inlen);
if (xc == -2)
if (xc == MBEDTLS_ERR_BASE64_INVALID_CHARACTER)
Rf_error("input is not valid base64");
unsigned char *buf = R_Calloc(olen, unsigned char);
xc = mbedtls_base64_decode(buf, olen, &olen, inbuf, inlen);
CHECK_ERROR(xc);
CHECK_ERROR(xc, buf);

switch (*(int *) DATAPTR_RO(convert)) {
case 0:
Expand Down
5 changes: 4 additions & 1 deletion src/secret.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
#define SB_SIPH_SIZE 8
#define SB_SKEY_SIZE 16

#define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -1
#define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -2

#if defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
__ARMCC_VERSION >= 6000000)
#define MBEDTLS_CT_ASM
Expand Down Expand Up @@ -104,7 +107,7 @@ typedef struct nano_buf_s {
(x)->len = 0; \
(x)->cur = sz
#define NANO_FREE(x) if (x.len) R_Free(x.buf)
#define CHECK_ERROR(x) if (x) { R_Free(buf); \
#define CHECK_ERROR(x, buf) if (x) { R_Free(buf); \
Rf_error("write buffer insufficient"); }
#define ERROR_OUT(x) if (x->len) R_Free(x->buf); \
Rf_error("serialization exceeds max length of raw vector")
Expand Down

0 comments on commit 4f5af3c

Please sign in to comment.