mkp224o/base16_from.c

83 lines
2.5 KiB
C
Raw Permalink Normal View History

2017-09-24 21:13:16 +02:00
#include <stddef.h>
#include <stdint.h>
#include "types.h"
#include "base16.h"
static const u8 base16f[256] = {
2017-09-25 19:49:47 +02:00
//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, 0xFF, // 0x68
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x70
0xFF, 0xFF, 0xFF, 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,
2017-09-24 21:13:16 +02:00
};
2017-09-25 19:49:47 +02:00
size_t base16_from(u8 *dst,u8 *dmask,const char *src)
2017-09-24 21:13:16 +02:00
{
2017-09-25 19:49:47 +02:00
size_t i, j, k = (size_t)-1, l, sk = 0;
u8 mask = 0, cmask = 0;
2017-09-24 21:13:16 +02:00
for (i = 0;;i += 4) {
j = i/4;
if (!src[j]) {
2017-09-25 19:49:47 +02:00
if (k != (size_t)-1)
dst[k] &= cmask;
if (dmask)
*dmask = cmask;
2017-09-25 19:49:47 +02:00
return k + 1;
2017-09-24 21:13:16 +02:00
}
2017-09-25 19:49:47 +02:00
l = i%8;
2017-09-24 21:13:16 +02:00
k = i/8;
2017-09-25 19:49:47 +02:00
if (k != sk)
cmask = 0;
sk = k;
2017-09-24 21:13:16 +02:00
mask = (0x0F << 4) >> l;
2017-09-25 19:49:47 +02:00
cmask |= mask;
2017-09-24 21:13:16 +02:00
dst[k] &= ~mask;
dst[k] |= (base16f[(u8)src[j]] << 4) >> l;
}
}
2017-09-25 19:49:47 +02:00
int base16_valid(const char *src,size_t *count)
{
const char *p;
2017-10-23 09:06:59 +02:00
for (p = src;base16f[(u8)*p] != 0xFF;++p)
;
2017-09-25 19:49:47 +02:00
if (count)
2018-02-23 01:11:20 +01:00
*count = (size_t) (p - src);
2017-09-25 19:49:47 +02:00
return !*p;
}