forked from mist-devel/mist-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
62 lines (51 loc) · 1.26 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <ctype.h>
#include "utils.h"
#include "attrs.h"
unsigned char bin2bcd(unsigned char in) {
return 16*(in/10) + (in % 10);
}
unsigned char bcd2bin(unsigned char in) {
return 10*(in >> 4) + (in & 0x0f);
}
unsigned char decval(unsigned char in, unsigned char min, unsigned char max) {
return (in == min) ? max : in-1;
}
unsigned char incval(unsigned char in, unsigned char min, unsigned char max) {
return (in == max) ? min : in+1;
}
FAST int _strnicmp(const char *s1, const char *s2, size_t n)
{
char c1, c2;
int v;
do
{
c1 = *s1++;
c2 = *s2++;
if (!c1) break;
v = (unsigned int)tolower(c1) - (unsigned int)tolower(c2);
}
while (v == 0 && --n > 0);
if (!c1 && c2) v = -1;
return v;
}
void hexdump(void *data, uint16_t size, uint16_t offset) {
uint8_t i, b2c;
uint16_t n=0;
char *ptr = data;
if(!size) return;
while(size>0) {
iprintf("%04x: ", n + offset);
b2c = (size>16)?16:size;
for(i=0;i<b2c;i++) iprintf("%02x ", 0xff&ptr[i]);
iprintf(" ");
for(i=0;i<(16-b2c);i++) iprintf(" ");
for(i=0;i<b2c;i++) iprintf("%c", isprint(ptr[i])?ptr[i]:'.');
iprintf("\n");
ptr += b2c;
size -= b2c;
n += b2c;
}
}