Add a few missing #include's of <string.h>.
[userv-utils] / ipif / hex.c
CommitLineData
f0e54a99 1/*
c07be359 2 * Copyright (C) 1997,2000,2003 Ian Jackson
3 * This file is part of ipif, part of userv-utils
f0e54a99 4 *
5 * This is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with userv-utils; if not, write to the Free Software
17 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
2dc68225 19
20#include <stdio.h>
21#include <stdlib.h>
22#include <assert.h>
dea61a77 23#include <string.h>
2dc68225 24#include <sysexits.h>
25
26#include "hex.h"
27
ed509ebd 28const char *tohex(uint8_t *data, int len, char *buf) {
2dc68225 29 char *p;
30
31 for (p= buf;
32 len>0;
33 len--, data++, p+=2)
34 sprintf(p,"%02x",*data);
35 return buf;
36}
37
ed509ebd 38void unhex(const char *what, const char *txt, uint8_t *datar, int *lenr,
2dc68225 39 int minlen, int maxlen) {
40 int l, v;
41 char buf[3], *ep;
42
43 l= strlen(txt);
44 if (l%1) { fprintf(stderr,"odd number of hex digits in %s\n",what); exit(EX_DATAERR); }
45 l>>=1;
46 if (l<minlen) { fprintf(stderr,"too few hex digits in %s\n",what); exit(EX_DATAERR); }
47 if (l>maxlen) { fprintf(stderr,"too many hex digits in %s\n",what); exit(EX_DATAERR); }
48
49 if (lenr) *lenr= l;
50 while (l) {
51 buf[0]= *txt++;
52 buf[1]= *txt++;
53 buf[2]= 0;
54 v= strtoul(buf,&ep,16);
55 if (*ep) { fprintf(stderr,"not hex digit in %s: %c\n",what,*ep); exit(EX_DATAERR); }
56 *datar++= v;
57 l--;
58 }
59}