summaryrefslogtreecommitdiffstats
path: root/src/util.c
blob: df1f83627e83ccdc09a2e9f1aa1c5e42c6a7b300 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* $Id$ */

/*
 * This file is part of aeswepd.
 *
 * aeswepd is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * aeswepd is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aeswepd; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include <unistd.h>
#include <sys/socket.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <libdaemon/dlog.h>

#include "util.h"

void print_hex(FILE *f, uint8_t *w, int l) {
    while (l > 0) {
        fprintf(f, "%02x", *(w++));
        l--;
    }
}


int parse_hex(char *s, uint8_t *b, int l) {
    int n = 0;
    char *p;
    int nibble = 0;
    
    for (p = s; *p && l >= 0; p++) {
        int c;

        if (*p >= '0' && *p <= '9')
            c = *p - '0';
        else if (*p >= 'A' && *p <= 'F')
            c = *p - 'A' + 10;
        else if (*p >= 'a' && *p <= 'f')
            c = *p - 'a' + 10;
        else if (*p == ':' || *p == '.')
            continue;
        else
            return -(p-s)-1;

        if (!nibble)
            *b = c << 4;
        else {
            *b |= c & 15;
            b++;
            l--;
            n++;
        }

        nibble = !nibble;
    }

    if (*p)
        return -(p-s)-1;
    
    return n;
}