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 <ralf@ramses-pyramidenbau.de>
This commit is contained in:
Ralf Ramsauer 2017-03-11 15:37:56 -08:00
parent 0eb3b65158
commit 4591cafd27
6 changed files with 21 additions and 34 deletions

View file

@ -51,11 +51,6 @@ static const unsigned char cb128[] =
static unsigned char rev128[256]; static unsigned char rev128[256];
static int reverse_init = 0; static int reverse_init = 0;
static int base128_handles_dots(void)
{
return 0;
}
inline static void base128_reverse_init(void) inline static void base128_reverse_init(void)
{ {
int i; int i;
@ -258,8 +253,8 @@ const struct encoder base128_ops = {
.encode = base128_encode, .encode = base128_encode,
.decode = base128_decode, .decode = base128_decode,
.places_dots = base128_handles_dots, .places_dots = false,
.eats_dots = base128_handles_dots, .eats_dots = false,
.blocksize_raw = BASE128_BLKSIZE_RAW, .blocksize_raw = BASE128_BLKSIZE_RAW,
.blocksize_encoded = BASE128_BLKSIZE_ENC, .blocksize_encoded = BASE128_BLKSIZE_ENC,

View file

@ -32,11 +32,6 @@ static const char cb32_ucase[] =
static unsigned char rev32[256]; static unsigned char rev32[256];
static int reverse_init = 0; static int reverse_init = 0;
static int base32_handles_dots(void)
{
return 0;
}
inline static void base32_reverse_init(void) inline static void base32_reverse_init(void)
{ {
int i; int i;
@ -235,8 +230,8 @@ const struct encoder base32_ops = {
.encode = base32_encode, .encode = base32_encode,
.decode = base32_decode, .decode = base32_decode,
.places_dots = base32_handles_dots, .places_dots = false,
.eats_dots = base32_handles_dots, .eats_dots = false,
.blocksize_raw = BASE32_BLKSIZE_RAW, .blocksize_raw = BASE32_BLKSIZE_RAW,
.blocksize_encoded = BASE32_BLKSIZE_ENC, .blocksize_encoded = BASE32_BLKSIZE_ENC,

View file

@ -32,11 +32,6 @@ static const char cb64[] =
static unsigned char rev64[256]; static unsigned char rev64[256];
static int reverse_init = 0; static int reverse_init = 0;
static int base64_handles_dots(void)
{
return 0;
}
inline static void base64_reverse_init(void) inline static void base64_reverse_init(void)
{ {
int i; int i;
@ -173,8 +168,8 @@ const struct encoder base64_ops = {
.encode = base64_encode, .encode = base64_encode,
.decode = base64_decode, .decode = base64_decode,
.places_dots = base64_handles_dots, .places_dots = false,
.eats_dots = base64_handles_dots, .eats_dots = false,
.blocksize_raw = BASE64_BLKSIZE_RAW, .blocksize_raw = BASE64_BLKSIZE_RAW,
.blocksize_encoded = BASE64_BLKSIZE_ENC, .blocksize_encoded = BASE64_BLKSIZE_ENC,

View file

@ -29,14 +29,14 @@ int build_hostname(char *buf, size_t buflen, const char *data,
space = MIN((size_t)maxlen, buflen) - strlen(topdomain) - 8; space = MIN((size_t)maxlen, buflen) - strlen(topdomain) - 8;
/* 8 = 5 max header length + 1 dot before topdomain + 2 safety */ /* 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 */ space -= (space / 57); /* space for dots */
memset(buf, 0, buflen); memset(buf, 0, buflen);
encoder->encode(buf, &space, data, datalen); encoder->encode(buf, &space, data, datalen);
if (!encoder->places_dots()) if (!encoder->places_dots)
inline_dotify(buf, buflen); inline_dotify(buf, buflen);
b = buf; 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, int unpack_data(char *buf, size_t buflen, char *data, size_t datalen,
const struct encoder *enc) const struct encoder *enc)
{ {
if (!enc->eats_dots()) if (!enc->eats_dots)
datalen = inline_undotify(data, datalen); datalen = inline_undotify(data, datalen);
return enc->decode(buf, &buflen, data, datalen); return enc->decode(buf, &buflen, data, datalen);
} }

View file

@ -24,6 +24,8 @@
#ifndef _ENCODING_H_ #ifndef _ENCODING_H_
#define _ENCODING_H_ #define _ENCODING_H_
#include <stdbool.h>
/* All-0, all-1, 01010101, 10101010: each 4 times to make sure the pattern /* All-0, all-1, 01010101, 10101010: each 4 times to make sure the pattern
spreads across multiple encoded chars -> 16 bytes total. spreads across multiple encoded chars -> 16 bytes total.
Followed by 32 bytes from my /dev/random; should be enough. 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 (*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 (*decode)(void *dst, size_t *dstlen, const char *src, size_t srclen);
int (*places_dots)(void); const bool places_dots;
int (*eats_dots)(void); const bool eats_dots;
const int blocksize_raw; const int blocksize_raw;
const int blocksize_encoded; const int blocksize_encoded;

View file

@ -2136,31 +2136,31 @@ write_dns_nameenc(char *buf, size_t buflen, const char *data, int datalen, char
if (downenc == 'S') { if (downenc == 'S') {
buf[0] = 'i'; buf[0] = 'i';
if (!base64_ops.places_dots()) if (!base64_ops.places_dots)
space -= (space / 57); /* space for dots */ space -= (space / 57); /* space for dots */
base64_ops.encode(buf+1, &space, data, datalen); base64_ops.encode(buf+1, &space, data, datalen);
if (!base64_ops.places_dots()) if (!base64_ops.places_dots)
inline_dotify(buf, buflen); inline_dotify(buf, buflen);
} else if (downenc == 'U') { } else if (downenc == 'U') {
buf[0] = 'j'; buf[0] = 'j';
if (!base64u_ops.places_dots()) if (!base64u_ops.places_dots)
space -= (space / 57); /* space for dots */ space -= (space / 57); /* space for dots */
base64u_ops.encode(buf+1, &space, data, datalen); base64u_ops.encode(buf+1, &space, data, datalen);
if (!base64u_ops.places_dots()) if (!base64u_ops.places_dots)
inline_dotify(buf, buflen); inline_dotify(buf, buflen);
} else if (downenc == 'V') { } else if (downenc == 'V') {
buf[0] = 'k'; buf[0] = 'k';
if (!base128_ops.places_dots()) if (!base128_ops.places_dots)
space -= (space / 57); /* space for dots */ space -= (space / 57); /* space for dots */
base128_ops.encode(buf+1, &space, data, datalen); base128_ops.encode(buf+1, &space, data, datalen);
if (!base128_ops.places_dots()) if (!base128_ops.places_dots)
inline_dotify(buf, buflen); inline_dotify(buf, buflen);
} else { } else {
buf[0] = 'h'; buf[0] = 'h';
if (!base32_ops.places_dots()) if (!base32_ops.places_dots)
space -= (space / 57); /* space for dots */ space -= (space / 57); /* space for dots */
base32_ops.encode(buf+1, &space, data, datalen); base32_ops.encode(buf+1, &space, data, datalen);
if (!base32_ops.places_dots()) if (!base32_ops.places_dots)
inline_dotify(buf, buflen); inline_dotify(buf, buflen);
} }