Files
adsb-tools/adsbus/hex.c

88 lines
2.2 KiB
C
Raw Normal View History

#include <assert.h>
#include <limits.h>
#include <unistd.h>
2016-02-22 16:36:27 -08:00
#include "hex.h"
2016-02-26 14:09:37 -08:00
static uint8_t hex_table[256];
static uint8_t hex_upper_table[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', };
static uint8_t hex_lower_table[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', };
2016-02-22 16:36:27 -08:00
2016-02-26 14:09:37 -08:00
#define HEX_INVALID 0xff
2016-02-22 16:36:27 -08:00
void hex_init() {
2016-02-26 14:09:37 -08:00
for (size_t i = 0; i < sizeof(hex_table) / sizeof(*hex_table); i++) {
hex_table[i] = HEX_INVALID;
}
for (uint8_t i = '0'; i <= '9'; i++) {
2016-02-22 16:36:27 -08:00
hex_table[i] = i - '0';
}
for (uint8_t i = 'a'; i <= 'f'; i++) {
2016-02-22 16:36:27 -08:00
hex_table[i] = 10 + i - 'a';
}
for (uint8_t i = 'A'; i <= 'F'; i++) {
2016-02-22 16:36:27 -08:00
hex_table[i] = 10 + i - 'A';
}
}
2016-02-26 14:09:37 -08:00
bool hex_to_bin(uint8_t *out, const uint8_t *in, size_t bytes) {
2016-02-22 16:36:27 -08:00
for (size_t i = 0, j = 0; i < bytes; i++, j += 2) {
2016-02-26 14:09:37 -08:00
uint8_t val1 = hex_table[in[j]], val2 = hex_table[in[j + 1]];
if (val1 == HEX_INVALID || val2 == HEX_INVALID) {
return false;
}
out[i] = (uint8_t) (val1 << 4) | val2;
2016-02-22 16:36:27 -08:00
}
2016-02-26 14:09:37 -08:00
return true;
2016-02-22 16:36:27 -08:00
}
2016-02-26 14:09:37 -08:00
int64_t hex_to_int(const uint8_t *in, size_t bytes) {
const uint8_t *in2 = (const uint8_t *) in;
2016-02-22 16:36:27 -08:00
uint64_t ret = 0;
bytes *= 2;
for (size_t i = 0; i < bytes; i++) {
ret <<= 4;
2016-02-26 14:09:37 -08:00
uint8_t val = hex_table[in2[i]];
if (val == 0xff) {
return -1;
}
ret |= val;
}
if (ret > INT64_MAX) {
return -1;
2016-02-22 16:36:27 -08:00
}
2016-02-26 14:09:37 -08:00
return (int64_t) ret;
2016-02-22 16:36:27 -08:00
}
static void hex_from_bin(uint8_t *out, const uint8_t *in, size_t bytes, uint8_t table[]) {
2016-02-22 16:36:27 -08:00
for (size_t i = 0, j = 0; i < bytes; i++, j += 2) {
out[j] = table[in[i] >> 4];
out[j + 1] = table[in[i] & 0xf];
}
}
static void hex_from_int(uint8_t *out, uint64_t in, size_t bytes, uint8_t table[]) {
2016-02-22 16:36:27 -08:00
bytes *= 2;
assert(bytes < SSIZE_MAX);
for (ssize_t o = (ssize_t) bytes - 1; o >= 0; o--) {
2016-02-22 16:36:27 -08:00
out[o] = table[in & 0xf];
in >>= 4;
}
}
void hex_from_bin_upper(uint8_t *out, const uint8_t *in, size_t bytes) {
2016-02-22 16:36:27 -08:00
hex_from_bin(out, in, bytes, hex_upper_table);
}
void hex_from_bin_lower(uint8_t *out, const uint8_t *in, size_t bytes) {
2016-02-22 16:36:27 -08:00
hex_from_bin(out, in, bytes, hex_lower_table);
}
void hex_from_int_upper(uint8_t *out, uint64_t in, size_t bytes) {
2016-02-22 16:36:27 -08:00
hex_from_int(out, in, bytes, hex_upper_table);
}
void hex_from_int_lower(uint8_t *out, uint64_t in, size_t bytes) {
2016-02-22 16:36:27 -08:00
hex_from_int(out, in, bytes, hex_lower_table);
}