/* 
 * Convert a DHCPv6 DUID from hexadecimal to binary
 *
 * usage: ./duigen 000300060800270c5f2b > /var/db/dhcp6c_duid
 *
 */
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>

void
str2bin(char *s, uint8_t *d, size_t size)
{
        size_t i;
        for (i = 0; s && i < size; i++, s += 2)
                sscanf(s, "%2hhx", &d[i]);
}

int
main(int argc, char *argv[])
{
	char *s;
	char d[12];

	if (argc < 1)
		return -1;

	s = argv[1];

	/* force size to 10 (2 bytes little endian)*/
	d[0] = 10;
	d[1] = 0;

	/* convert DUID to binary */
	str2bin(s, d+2, strlen(s)/2);

	/* output length + duid to stdout */
	write(1, d, strlen(s)/2 + 2);
	
	return 0;
}