Skip to content

Commit

Permalink
more idiomatic macros
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jul 2, 2024
1 parent 8cbe4f8 commit e1efa89
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ URL: https://shikokuchuo.net/secretbase/,
Encoding: UTF-8
Depends:
R (>= 3.5)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
14 changes: 8 additions & 6 deletions src/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions src/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
9 changes: 5 additions & 4 deletions src/secret.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions src/secret2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/secret3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e1efa89

Please sign in to comment.