1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 20:38:10 +00:00

tests: cleanup fromhex function

This commit is contained in:
Pavol Rusnak 2016-10-20 12:04:05 +02:00
parent e6574f8eea
commit ca4057aca0
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

23
tests.c
View File

@ -46,18 +46,19 @@
#include "ed25519.h" #include "ed25519.h"
#include "script.h" #include "script.h"
uint8_t *fromhex(const char *str) #define FROMHEX_MAXLEN 256
const uint8_t *fromhex(const char *str)
{ {
static uint8_t buf[256]; static uint8_t buf[FROMHEX_MAXLEN];
uint8_t c; size_t len = strlen(str) / 2;
for (size_t i = 0; i < strlen(str) / 2; i++) { if (len > FROMHEX_MAXLEN) len = FROMHEX_MAXLEN;
c = 0; for (size_t i = 0; i < len; i++) {
if (str[i*2] >= '0' && str[i*2] <= '9') c += (str[i*2] - '0') << 4; uint8_t c = 0;
if (str[i*2] >= 'a' && str[i*2] <= 'f') c += (10 + str[i*2] - 'a') << 4; if (str[i * 2] >= '0' && str[i*2] <= '9') c += (str[i * 2] - '0') << 4;
if (str[i*2] >= 'A' && str[i*2] <= 'F') c += (10 + str[i*2] - 'A') << 4; if ((str[i * 2] & ~0x20) >= 'A' && (str[i*2] & ~0x20) <= 'F') c += (10 + (str[i * 2] & ~0x20) - 'A') << 4;
if (str[i*2+1] >= '0' && str[i*2+1] <= '9') c += (str[i*2+1] - '0'); if (str[i * 2 + 1] >= '0' && str[i * 2 + 1] <= '9') c += (str[i * 2 + 1] - '0');
if (str[i*2+1] >= 'a' && str[i*2+1] <= 'f') c += (10 + str[i*2+1] - 'a'); if ((str[i * 2 + 1] & ~0x20) >= 'A' && (str[i * 2 + 1] & ~0x20) <= 'F') c += (10 + (str[i * 2 + 1] & ~0x20) - 'A');
if (str[i*2+1] >= 'A' && str[i*2+1] <= 'F') c += (10 + str[i*2+1] - 'A');
buf[i] = c; buf[i] = c;
} }
return buf; return buf;