fluffy

Network Archaeology tools for Unix
git clone https://git.woozle.org/neale/fluffy.git

Neale Pickett  ·  2021-10-15

undec.c

 1/*
 2 * Hex Decoder -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain.  I make no promises about the functionality 
 3 * of this program. 
 4 */
 5
 6#include <stdio.h>
 7
 8int
 9main(int argc, char *argv[])
10{
11	unsigned char acc = 0;
12	unsigned char digit = 0;
13	unsigned long int count = 0;
14
15	while (1) {
16		int c = getchar();
17
18		count += 1;
19
20		switch (c) {
21			case EOF:
22				return 0;
23			case ' ':
24			case '\r':
25			case '\n':
26			case '\t':
27				digit += 3;
28				break;
29			case '0' ... '9':
30				acc = (acc * 10) + c - '0';
31				digit += 1;
32				break;
33			default:
34				if (digit != 0) {
35					fprintf(stderr, "Warning: non-numeric character mid-octet at offset %lu\n", count);
36				}
37				break;
38		}
39
40		if (digit > 3) {
41			putchar(acc);
42			acc = 0;
43			digit = 0;
44		}
45	}
46
47	return 0;
48}