/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* tcpip720 src/tcpip/usr/sbin/dhcp_dlls/samples/dhcputil.c 1.1           */
/*                                                                        */
/* Licensed Materials - Property of IBM                                   */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 1999                   */
/* All Rights Reserved                                                    */
/*                                                                        */
/* US Government Users Restricted Rights - Use, duplication or            */
/* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.      */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
static char sccsid[] = "@(#)23	1.1  src/tcpip/usr/sbin/dhcp_dlls/samples/dhcputil.c, dhcp, tcpip720 6/9/99 23:14:42";

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dhcp_api.h>

extern char *
dhcputil_cid2str(const struct dhcpclientid *c, char *buf)
{
	int printhex = 0;
	char *ocp;
	const uint8_t *cp;

	if (!c || !buf) return NULL;

	if (c->type == 0)
	{
		buf[0] = '0';
		buf[1] = '-';
		buf[2] = '\0';

	/* We have to verify that the whole client identifier is printable */

		for (cp = c->id; cp < c->id + c->len && isprint(*cp); cp++) ;
		if (cp < c->id + c->len)
		{
			/* Cannot be printed as text. */
			/* add 0x so that it starts "0-0x" */
			buf[2] = '0';
			buf[3] = 'x';
			buf[4] = '\0';
			printhex++;
		}
	}
	else
	{
		sprintf(buf, "%d-", c->type);
	}
		
	if (c->type == 0 && !printhex)
	{
		ocp = &buf[2];
		strncpy(ocp, (const char *) c->id, c->len);
		buf[c->len + 2] = '\0';
	}
	else
	{
		for (cp = c->id, ocp = buf + strlen(buf) ;
		     cp < c->id + c->len ;
		     cp++, ocp += 2 )
		{
	
			sprintf(ocp, "%02x", *cp);
	
		}
	}
	return buf;
}

extern struct dhcpclientid *
dhcputil_str2cid(const char *buf, struct dhcpclientid *c)
{
	char tmp_str[3];
	const char *cp = NULL;
	uint8_t	*ocp;

	if (!buf || !c || !strchr(buf, '-')) return NULL;

	bzero(c, sizeof(struct dhcpclientid));

	c->type = strtoul(buf, NULL, 10); /* convert number before the dash */
	cp = strchr(buf, '-') + 1;

	/* first, the easy, type zero, non "0-0x" case */
	if (c->type == 0 && strncmp(cp, "0x", 2))
	{
		strcpy((char *) c->id, (char *) cp);
		c->len = strlen(cp);
		return(c);
	}
	else if (c->type == 0 && !strncmp(cp, "0x", 2))
	{
		cp += 2;
	}

	tmp_str[2] = 0;

	if ((strlen(cp) % 2) != 0) {
		tmp_str[0] = 0;
		tmp_str[1] = *cp++;
	}
	else {
		strncpy(tmp_str, cp, 2);
	}

	ocp = c->id;
	*ocp = (uint8_t) strtoul(tmp_str, NULL, 16);

	while (ocp++ && (cp += 2) && *cp)
	{
		strncpy(tmp_str, cp, 2);
		*ocp = (uint8_t) strtoul(tmp_str, NULL, 16);
	}

	c->len = ocp - c->id;

	return(c);
}
