82 lines
2.5 KiB
C
82 lines
2.5 KiB
C
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include "types.h"
|
|
#include "base16.h"
|
|
|
|
static const u8 base16f[256] = {
|
|
//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
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for (i = 0;;i += 4) {
|
|
j = i/4;
|
|
if (!src[j]) {
|
|
if (k != (size_t)-1)
|
|
dst[k] &= cmask;
|
|
if (dmask)
|
|
*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;
|
|
}
|