Skip to content

Commit

Permalink
improvements all over the place
Browse files Browse the repository at this point in the history
  • Loading branch information
cathugger committed Sep 25, 2017
1 parent 59c07f0 commit 7887d08
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 70 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

CC= gcc
CFLAGS= -O3 -march=native -Wall
#CFLAGS= -O0 -g3 -fsanitize=address
CSTD= -std=c99 -Wall -D_POSIX_C_SOURCE=200112L
CFLAGS= $(CSTD) -O3 -march=native
#CFLAGS= $(CSTD) -O0 -g3 -fsanitize=address
MV= mv

ED25519OBJ= $(patsubst %.c,%.o,$(wildcard ed25519/ref10/*.c))
Expand Down Expand Up @@ -61,7 +62,7 @@ clean:
$(RM) $(EXE)

depend:
makedepend -Y -- $(CFLAGS) -- $(MAINOBJ:.o=.c) $(TEST_BASE16OBJ:.o=.c) $(TEST_BASE32OBJ:.o=.c) $(TEST_ED25519OBJ:.o=.c)
makedepend -Y -- $(CSTD) -- $(MAINOBJ:.o=.c) $(TEST_BASE16OBJ:.o=.c) $(TEST_BASE32OBJ:.o=.c) $(TEST_ED25519OBJ:.o=.c)

# DO NOT DELETE THIS LINE

Expand Down
14 changes: 11 additions & 3 deletions base16.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@

char *base16_to(char *dst, const u8 *src, size_t slen);
size_t base16_from(u8 *dst, u8 *dmask, const char *src);
// converts src[0:slen] to base16 string
char *base16_to(char *dst,const u8 *src,size_t slen);
// calculates length needed to store data converted to base16
#define BASE16_TO_LEN(l) (((l) * 8 + 3) / 4)
// converts src string from base16
size_t base16_from(u8 *dst,u8 *dmask,const char *src);
// calculates length needed to store data converted from base16
#define BASE16_FROM_LEN(l) (((l) * 4 + 7) / 8)
// validates base16 string and optionally stores length of valid data
// returns 1 if whole string is good, 0 if string contains invalid data
int base16_valid(const char *src,size_t *count);
77 changes: 61 additions & 16 deletions base16_from.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,77 @@
#include "base16.h"

static const u8 base16f[256] = {
['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3,
['4'] = 4, ['5'] = 5, ['6'] = 6, ['7'] = 7,
['8'] = 8, ['9'] = 9, ['A'] = 10, ['B'] = 11,
['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15,
['a'] = 10, ['b'] = 11,
['c'] = 12, ['d'] = 13, ['e'] = 14, ['f'] = 15,
//00 01 02 03 04 05 06 07
//08 09 0A 0B 0C 0D 0E 0F
// 0x00..0x3F
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x00
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x08
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x10
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x18
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x20
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x28
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 0x30
0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x38
// 0x40..0x7F
0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, // 0x40
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x48
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x50
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x58
0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, // 0x60
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,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ask = 0, cmask = 0;
for (i = 0;;i += 4) {
j = i/4;
l = i%8;
if (!src[j]) {
if (!l)
mask = 0xFF;
if (k >= 0)
dst[k] &= mask;
*dmask = mask;
return (size_t)(k+1);
if (k != (size_t)-1)
dst[k] &= cmask;
*dmask = cmask;
return k + 1;
}
l = i%8;
k = i/8;
if (k != sk)
cmask = 0;
sk = k;
mask = (0x0F << 4) >> l;
cmask |= mask;
dst[k] &= ~mask;
dst[k] |= (base16f[(u8)src[j]] << 4) >> l;
}
}

int base16_valid(const char *src,size_t *count)
{
const char *p;

for (p = src;base16f[(u8)*p] != 0xFF;++p) ;

if (count)
*count = p - src;
return !*p;
}
13 changes: 11 additions & 2 deletions base32.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
char *base32_to(char *dst, const u8 *src, size_t slen);
size_t base32_from(u8 *dst, u8 *dmask, const char *src);
// converts src[0:slen] to base32 string
char *base32_to(char *dst,const u8 *src,size_t slen);
// calculates length needed to store data converted to base32
#define BASE32_TO_LEN(l) (((l) * 8 + 4) / 5)
// converts src string from base32
size_t base32_from(u8 *dst,u8 *dmask,const char *src);
// calculates length needed to store data converted from base
#define BASE32_FROM_LEN(l) (((l) * 5 + 7) / 8)
// validates base32 string and optionally stores length of valid data
// returns 1 if whole string is good, 0 if string contains invalid data
int base32_valid(const char *src,size_t *count);
68 changes: 54 additions & 14 deletions base32_from.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,44 @@
#include "base32.h"

static const u8 base32f[256] = {
['a'] = 0, ['b'] = 1, ['c'] = 2, ['d'] = 3,
['e'] = 4, ['f'] = 5, ['g'] = 6, ['h'] = 7,
['i'] = 8, ['j'] = 9, ['k'] = 10, ['l'] = 11,
['m'] = 12, ['n'] = 13, ['o'] = 14, ['p'] = 15,
['q'] = 16, ['r'] = 17, ['s'] = 18, ['t'] = 19,
['u'] = 20, ['v'] = 21, ['w'] = 22, ['x'] = 23,
['y'] = 24, ['z'] = 25, ['2'] = 26, ['3'] = 27,
['4'] = 28, ['5'] = 29, ['6'] = 30, ['7'] = 31,
//00 01 02 03 04 05 06 07
//08 09 0A 0B 0C 0D 0E 0F
// 0x00..0x3F
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x00
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x08
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x10
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x18
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x20
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x28
0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, // 0x30
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x38
// 0x40..0x7F
0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 0x40
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 0x48
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 0x50
0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x58
0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 0x60
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 0x68
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 0x70
0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x78
// 0x80..0xBF
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
// 0xC0..0xFF
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
/*
+--first octet--+-second octet--+--third octet--+--forth octet--+--fifth octet--+
Expand All @@ -20,20 +50,19 @@ static const u8 base32f[256] = {
|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|
+-1.index-+-2.index-+-3.index-+-4.index-+-5.index-+-6.index-+-7.index-+-8.index-+
*/
size_t base32_from(u8 *dst, u8 *dmask, const char *src)
size_t base32_from(u8 *dst,u8 *dmask,const char *src)
{
int i, j, k = -1, l, sk = 0;
size_t i, j, k = (size_t)-1, l, sk = 0;
u8 mask = 0, cmask = 0;
for (i = 0;;i += 5) {
j = i/5;
l = i%8;
if (!src[j]) {
if (k >= 0)
if (k != (size_t)-1)
dst[k] &= cmask;
//printf("dst[k]:%02X mask:%02X l:%d\n", dst[k], (cmask & 0xFF), l);
*dmask = cmask;
return (size_t)(k + 1);
return k + 1;
}
l = i%8;
k = i/8;
if (k != sk)
cmask = 0;
Expand All @@ -57,3 +86,14 @@ size_t base32_from(u8 *dst, u8 *dmask, const char *src)
//printf("mask1: %02x\n", ((0x1F << 8) >> (i%8+5-8)) & 0xFF);
}
}

int base32_valid(const char *src,size_t *count)
{
const char *p;

for (p = src;base32f[(u8)*p] != 0xFF;++p) ;

if (count)
*count = p - src;
return !*p;
}
2 changes: 1 addition & 1 deletion base32_to.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static const char base32t[32] = {
// masks:
// 0xFF 0x7F 0x3F 0x1F 0x0F 0x07 0x03 0x01
// 255 127 63 31 15 7 3 1
char *base32_to(char *dst, const u8 *src, size_t slen)
char *base32_to(char *dst,const u8 *src,size_t slen)
{
//printf("slen = %d\n", slen);
//printhex(base32t, 32);
Expand Down
Loading

0 comments on commit 7887d08

Please sign in to comment.