From 4591cafd27d0afb56581db0d6fb8b96b6c2ca0bd Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Sat, 11 Mar 2017 15:37:56 -0800 Subject: [PATCH] encoding: simplify {places,eats}_dots Why not using constant bools? Much simpler than complex function calls, that eventually return constant values. Signed-off-by: Ralf Ramsauer --- src/base128.c | 9 ++------- src/base32.c | 9 ++------- src/base64.c | 9 ++------- src/encoding.c | 6 +++--- src/encoding.h | 6 ++++-- src/iodined.c | 16 ++++++++-------- 6 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/base128.c b/src/base128.c index fbf83d4..12bc19f 100644 --- a/src/base128.c +++ b/src/base128.c @@ -51,11 +51,6 @@ static const unsigned char cb128[] = static unsigned char rev128[256]; static int reverse_init = 0; -static int base128_handles_dots(void) -{ - return 0; -} - inline static void base128_reverse_init(void) { int i; @@ -258,8 +253,8 @@ const struct encoder base128_ops = { .encode = base128_encode, .decode = base128_decode, - .places_dots = base128_handles_dots, - .eats_dots = base128_handles_dots, + .places_dots = false, + .eats_dots = false, .blocksize_raw = BASE128_BLKSIZE_RAW, .blocksize_encoded = BASE128_BLKSIZE_ENC, diff --git a/src/base32.c b/src/base32.c index 2662729..cb68193 100644 --- a/src/base32.c +++ b/src/base32.c @@ -32,11 +32,6 @@ static const char cb32_ucase[] = static unsigned char rev32[256]; static int reverse_init = 0; -static int base32_handles_dots(void) -{ - return 0; -} - inline static void base32_reverse_init(void) { int i; @@ -235,8 +230,8 @@ const struct encoder base32_ops = { .encode = base32_encode, .decode = base32_decode, - .places_dots = base32_handles_dots, - .eats_dots = base32_handles_dots, + .places_dots = false, + .eats_dots = false, .blocksize_raw = BASE32_BLKSIZE_RAW, .blocksize_encoded = BASE32_BLKSIZE_ENC, diff --git a/src/base64.c b/src/base64.c index abad891..abf548d 100644 --- a/src/base64.c +++ b/src/base64.c @@ -32,11 +32,6 @@ static const char cb64[] = static unsigned char rev64[256]; static int reverse_init = 0; -static int base64_handles_dots(void) -{ - return 0; -} - inline static void base64_reverse_init(void) { int i; @@ -173,8 +168,8 @@ const struct encoder base64_ops = { .encode = base64_encode, .decode = base64_decode, - .places_dots = base64_handles_dots, - .eats_dots = base64_handles_dots, + .places_dots = false, + .eats_dots = false, .blocksize_raw = BASE64_BLKSIZE_RAW, .blocksize_encoded = BASE64_BLKSIZE_ENC, diff --git a/src/encoding.c b/src/encoding.c index 1c7f249..9de0560 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -29,14 +29,14 @@ int build_hostname(char *buf, size_t buflen, const char *data, space = MIN((size_t)maxlen, buflen) - strlen(topdomain) - 8; /* 8 = 5 max header length + 1 dot before topdomain + 2 safety */ - if (!encoder->places_dots()) + if (!encoder->places_dots) space -= (space / 57); /* space for dots */ memset(buf, 0, buflen); encoder->encode(buf, &space, data, datalen); - if (!encoder->places_dots()) + if (!encoder->places_dots) inline_dotify(buf, buflen); b = buf; @@ -57,7 +57,7 @@ int build_hostname(char *buf, size_t buflen, const char *data, int unpack_data(char *buf, size_t buflen, char *data, size_t datalen, const struct encoder *enc) { - if (!enc->eats_dots()) + if (!enc->eats_dots) datalen = inline_undotify(data, datalen); return enc->decode(buf, &buflen, data, datalen); } diff --git a/src/encoding.h b/src/encoding.h index 5d4c65c..8d8caef 100644 --- a/src/encoding.h +++ b/src/encoding.h @@ -24,6 +24,8 @@ #ifndef _ENCODING_H_ #define _ENCODING_H_ +#include + /* All-0, all-1, 01010101, 10101010: each 4 times to make sure the pattern spreads across multiple encoded chars -> 16 bytes total. Followed by 32 bytes from my /dev/random; should be enough. @@ -36,8 +38,8 @@ struct encoder { int (*encode)(char *dst, size_t *dstlen, const void *src, size_t srclen); int (*decode)(void *dst, size_t *dstlen, const char *src, size_t srclen); - int (*places_dots)(void); - int (*eats_dots)(void); + const bool places_dots; + const bool eats_dots; const int blocksize_raw; const int blocksize_encoded; diff --git a/src/iodined.c b/src/iodined.c index 4ba20fd..76b3dcf 100644 --- a/src/iodined.c +++ b/src/iodined.c @@ -2136,31 +2136,31 @@ write_dns_nameenc(char *buf, size_t buflen, const char *data, int datalen, char if (downenc == 'S') { buf[0] = 'i'; - if (!base64_ops.places_dots()) + if (!base64_ops.places_dots) space -= (space / 57); /* space for dots */ base64_ops.encode(buf+1, &space, data, datalen); - if (!base64_ops.places_dots()) + if (!base64_ops.places_dots) inline_dotify(buf, buflen); } else if (downenc == 'U') { buf[0] = 'j'; - if (!base64u_ops.places_dots()) + if (!base64u_ops.places_dots) space -= (space / 57); /* space for dots */ base64u_ops.encode(buf+1, &space, data, datalen); - if (!base64u_ops.places_dots()) + if (!base64u_ops.places_dots) inline_dotify(buf, buflen); } else if (downenc == 'V') { buf[0] = 'k'; - if (!base128_ops.places_dots()) + if (!base128_ops.places_dots) space -= (space / 57); /* space for dots */ base128_ops.encode(buf+1, &space, data, datalen); - if (!base128_ops.places_dots()) + if (!base128_ops.places_dots) inline_dotify(buf, buflen); } else { buf[0] = 'h'; - if (!base32_ops.places_dots()) + if (!base32_ops.places_dots) space -= (space / 57); /* space for dots */ base32_ops.encode(buf+1, &space, data, datalen); - if (!base32_ops.places_dots()) + if (!base32_ops.places_dots) inline_dotify(buf, buflen); }