blob: 54b335190dc97925bd6c84a492a2341613e635a9 (
plain)
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
|
#include "std.h"
#include "dat.h"
static int
unhex(char c)
{
if('0' <= c && c <= '9')
return c-'0';
if('a' <= c && c <= 'f')
return c-'a'+10;
if('A' <= c && c <= 'F')
return c-'A'+10;
abort();
return -1;
}
int
hexparse(char *hex, uchar *dat, int ndat)
{
int i, n;
n = strlen(hex);
if(n%2)
return -1;
n /= 2;
if(n > ndat)
return -1;
if(hex[strspn(hex, "0123456789abcdefABCDEF")] != '\0')
return -1;
for(i=0; i<n; i++)
dat[i] = (unhex(hex[2*i])<<4)|unhex(hex[2*i+1]);
return n;
}
char*
estrappend(char *s, char *fmt, ...)
{
char *t;
va_list arg;
va_start(arg, fmt);
t = vsmprint(fmt, arg);
if(t == nil)
sysfatal("out of memory");
va_end(arg);
s = erealloc(s, strlen(s)+strlen(t)+1);
strcat(s, t);
free(t);
return s;
}
|