diff --git a/DESCRIPTION b/DESCRIPTION index d7b88f6..97ed55f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,4 +23,4 @@ URL: https://shikokuchuo.net/secretbase/, Encoding: UTF-8 Depends: R (>= 3.5) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 diff --git a/src/base.c b/src/base.c index 4a7c439..f7d1fbf 100644 --- a/src/base.c +++ b/src/base.c @@ -361,8 +361,9 @@ static nano_buf nano_any_buf(const SEXP x) { SEXP secretbase_base64enc(SEXP x, SEXP convert) { - int xc, conv; - SB_LOGICAL(conv, convert); + SB_ASSERT_LOGICAL(convert); + const int conv = SB_LOGICAL(convert); + int xc; SEXP out; size_t olen; @@ -371,7 +372,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, buf); + CHECK_ERROR(xc); if (conv) { out = rawToChar(buf, olen); @@ -388,8 +389,9 @@ SEXP secretbase_base64enc(SEXP x, SEXP convert) { SEXP secretbase_base64dec(SEXP x, SEXP convert) { - int xc, conv; - SB_LOGICAL(conv, convert); + SB_ASSERT_LOGICAL(convert); + const int conv = SB_LOGICAL(convert); + int xc; unsigned char *inbuf; SEXP out; size_t inlen, olen; @@ -412,7 +414,7 @@ SEXP secretbase_base64dec(SEXP x, SEXP convert) { 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, buf); + CHECK_ERROR(xc); switch (conv) { case 0: diff --git a/src/secret.c b/src/secret.c index 1dbbe67..33203d8 100644 --- a/src/secret.c +++ b/src/secret.c @@ -249,7 +249,7 @@ static inline void hash_bytes(R_outpstream_t stream, void *src, int len) { static void hash_file(mbedtls_sha3_context *ctx, const SEXP x) { - SB_CHK_STR(x); + SB_ASSERT_STR(x); const char *file = R_ExpandFileName(SB_STRING(x)); unsigned char buf[SB_BUF_SIZE]; FILE *f; @@ -336,8 +336,8 @@ static SEXP secretbase_sha3_impl(const SEXP x, const SEXP bits, const SEXP conve void (*const hash_func)(mbedtls_sha3_context *, SEXP), const int offset) { - int conv; - SB_LOGICAL(conv, convert); + SB_ASSERT_LOGICAL(convert); + const int conv = SB_LOGICAL(convert); const int bt = nano_integer(bits); mbedtls_sha3_id id; diff --git a/src/secret.h b/src/secret.h index db254cb..9a5107b 100644 --- a/src/secret.h +++ b/src/secret.h @@ -28,9 +28,10 @@ #define SB_DATAPTR(x) (void *) DATAPTR_RO(x) #define SB_STRING(x) CHAR(*((const SEXP *) DATAPTR_RO(x))) -#define SB_LOGICAL(v, x) if (TYPEOF(x) != LGLSXP) \ -Rf_error("'convert' must be a logical value"); else v = *(int *) DATAPTR_RO(x) -#define SB_CHK_STR(x) if (TYPEOF(x) != STRSXP) \ +#define SB_LOGICAL(x) *(int *) DATAPTR_RO(x) +#define SB_ASSERT_LOGICAL(x) if (TYPEOF(x) != LGLSXP) \ +Rf_error("'convert' must be a logical value") +#define SB_ASSERT_STR(x) if (TYPEOF(x) != STRSXP) \ Rf_error("'file' must be a character string") #define SB_R_SERIAL_VER 3 @@ -114,7 +115,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, buf) if (x) { R_Free(buf); \ +#define CHECK_ERROR(x) 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") diff --git a/src/secret2.c b/src/secret2.c index d06ec54..c96f101 100644 --- a/src/secret2.c +++ b/src/secret2.c @@ -384,7 +384,7 @@ static inline void hash_bytes(R_outpstream_t stream, void *src, int len) { static void hash_file(mbedtls_sha256_context *ctx, const SEXP x) { - SB_CHK_STR(x); + SB_ASSERT_STR(x); const char *file = R_ExpandFileName(SB_STRING(x)); unsigned char buf[SB_BUF_SIZE]; FILE *f; @@ -447,8 +447,8 @@ static void hash_object(mbedtls_sha256_context *ctx, const SEXP x) { static SEXP secretbase_sha256_impl(const SEXP x, const SEXP key, const SEXP convert, void (*const hash_func)(mbedtls_sha256_context *, SEXP)) { - int conv; - SB_LOGICAL(conv, convert); + SB_ASSERT_LOGICAL(convert); + const int conv = SB_LOGICAL(convert); unsigned char buf[SB_SHA256_SIZE]; if (key == R_NilValue) { diff --git a/src/secret3.c b/src/secret3.c index a8c3a80..410f69b 100644 --- a/src/secret3.c +++ b/src/secret3.c @@ -191,7 +191,7 @@ static inline void hash_bytes(R_outpstream_t stream, void *src, int len) { static void hash_file(CSipHash *ctx, const SEXP x) { - SB_CHK_STR(x); + SB_ASSERT_STR(x); const char *file = R_ExpandFileName(SB_STRING(x)); unsigned char buf[SB_BUF_SIZE]; FILE *f; @@ -252,8 +252,8 @@ static void hash_object(CSipHash *ctx, const SEXP x) { static SEXP secretbase_siphash_impl(const SEXP x, const SEXP key, const SEXP convert, void (*const hash_func)(CSipHash *, SEXP)) { - int conv; - SB_LOGICAL(conv, convert); + SB_ASSERT_LOGICAL(convert); + const int conv = SB_LOGICAL(convert); uint64_t hash; CSipHash ctx;