plan9port

[fork] Plan 9 from user space
git clone git://src.adamsgaard.dk/plan9port # fast
git clone https://src.adamsgaard.dk/plan9port.git # slow
Log | Files | Refs | README | LICENSE Back to index

util.c (786B)


      1 #include "std.h"
      2 #include "dat.h"
      3 
      4 static int
      5 unhex(char c)
      6 {
      7 	if('0' <= c && c <= '9')
      8 		return c-'0';
      9 	if('a' <= c && c <= 'f')
     10 		return c-'a'+10;
     11 	if('A' <= c && c <= 'F')
     12 		return c-'A'+10;
     13 	abort();
     14 	return -1;
     15 }
     16 
     17 int
     18 hexparse(char *hex, uchar *dat, int ndat)
     19 {
     20 	int i, n;
     21 
     22 	n = strlen(hex);
     23 	if(n%2)
     24 		return -1;
     25 	n /= 2;
     26 	if(n > ndat)
     27 		return -1;
     28 	if(hex[strspn(hex, "0123456789abcdefABCDEF")] != '\0')
     29 		return -1;
     30 	for(i=0; i<n; i++)
     31 		dat[i] = (unhex(hex[2*i])<<4)|unhex(hex[2*i+1]);
     32 	return n;
     33 }
     34 
     35 char*
     36 estrappend(char *s, char *fmt, ...)
     37 {
     38 	char *t;
     39 	int l;
     40 	va_list arg;
     41 
     42 	va_start(arg, fmt);
     43 	t = vsmprint(fmt, arg);
     44 	if(t == nil)
     45 		sysfatal("out of memory");
     46 	va_end(arg);
     47 	l = s ? strlen(s) : 0;
     48 	s = erealloc(s, l+strlen(t)+1);
     49 	strcpy(s+l, t);
     50 	free(t);
     51 	return s;
     52 }