summaryrefslogtreecommitdiffstats
path: root/security/nss/cmd/bltest/tests/aes_gcm/hex.c
blob: cdf583da2fc5df5e3a03e1bd2948a61834e496f3 (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
78
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int
tohex(int c)
{
    if ((c >= '0') && (c <= '9')) {
        return c - '0';
    }
    if ((c >= 'a') && (c <= 'f')) {
        return c - 'a' + 10;
    }
    if ((c >= 'A') && (c <= 'F')) {
        return c - 'A' + 10;
    }
    return 0;
}

int
isspace(int c)
{
    if (c <= ' ')
        return 1;
    if (c == '\n')
        return 1;
    if (c == '\t')
        return 1;
    if (c == ':')
        return 1;
    if (c == ';')
        return 1;
    if (c == ',')
        return 1;
    return 0;
}

void
verify_nibble(int nibble, int current)
{
    if (nibble != 0) {
        fprintf(stderr, "count mismatch %d (nibbles=0x%x)\n", nibble, current);
        fflush(stderr);
    }
}

int
main(int argc, char **argv)
{
    int c;
    int current = 0;
    int nibble = 0;
    int skip = 0;

    if (argv[1]) {
        skip = atoi(argv[1]);
    }

#define NIBBLE_COUNT 2
    while ((c = getchar()) != EOF) {
        if (isspace(c)) {
            verify_nibble(nibble, current);
            continue;
        }
        if (skip) {
            skip--;
            continue;
        }
        current = current << 4 | tohex(c);
        nibble++;
        if (nibble == NIBBLE_COUNT) {
            putchar(current);
            nibble = 0;
            current = 0;
        }
    }
    return 0;
}