Blame src/lib/binhex.c

Packit 4f15d5
/*
Packit 4f15d5
    Copyright (C) 2011  ABRT team
Packit 4f15d5
    Copyright (C) 2011  RedHat Inc
Packit 4f15d5
Packit 4f15d5
    This program is free software; you can redistribute it and/or modify
Packit 4f15d5
    it under the terms of the GNU General Public License as published by
Packit 4f15d5
    the Free Software Foundation; either version 2 of the License, or
Packit 4f15d5
    (at your option) any later version.
Packit 4f15d5
Packit 4f15d5
    This program is distributed in the hope that it will be useful,
Packit 4f15d5
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4f15d5
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4f15d5
    GNU General Public License for more details.
Packit 4f15d5
Packit 4f15d5
    You should have received a copy of the GNU General Public License along
Packit 4f15d5
    with this program; if not, write to the Free Software Foundation, Inc.,
Packit 4f15d5
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 4f15d5
*/
Packit 4f15d5
#include "internal_libreport.h"
Packit 4f15d5
Packit 4f15d5
static const char hexdigits_locase[] = "0123456789abcdef";
Packit 4f15d5
Packit 4f15d5
/* Emit a string of hex representation of bytes */
Packit 4f15d5
char *bin2hex(char *dst, const char *str, int count)
Packit 4f15d5
{
Packit 4f15d5
	while (count) {
Packit 4f15d5
		unsigned char c = *str++;
Packit 4f15d5
		/* put lowercase hex digits */
Packit 4f15d5
		*dst++ = hexdigits_locase[c >> 4];
Packit 4f15d5
		*dst++ = hexdigits_locase[c & 0xf];
Packit 4f15d5
		count--;
Packit 4f15d5
	}
Packit 4f15d5
	return dst;
Packit 4f15d5
}
Packit 4f15d5
Packit 4f15d5
/* Convert "xxxxxxxx" hex string to binary, no more than COUNT bytes */
Packit 4f15d5
char *hex2bin(char *dst, const char *str, int count)
Packit 4f15d5
{
Packit 4f15d5
	/* Parts commented out with // allow parsing
Packit 4f15d5
	 * of strings like "xx:x:x:xx:xx:xx:xxxxxx"
Packit 4f15d5
	 * (IPv6, ethernet addresses and the like).
Packit 4f15d5
	 */
Packit 4f15d5
	errno = EINVAL;
Packit 4f15d5
	while (*str && count) {
Packit 4f15d5
		uint8_t val;
Packit 4f15d5
		uint8_t c;
Packit 4f15d5
Packit 4f15d5
		c = *str++;
Packit 4f15d5
		if (isdigit(c))
Packit 4f15d5
			val = c - '0';
Packit 4f15d5
		else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
Packit 4f15d5
			val = (c|0x20) - ('a' - 10);
Packit 4f15d5
		else
Packit 4f15d5
			return NULL;
Packit 4f15d5
		val <<= 4;
Packit 4f15d5
		c = *str;
Packit 4f15d5
		if (isdigit(c))
Packit 4f15d5
			val |= c - '0';
Packit 4f15d5
		else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
Packit 4f15d5
			val |= (c|0x20) - ('a' - 10);
Packit 4f15d5
		//else if (c == ':' || c == '\0')
Packit 4f15d5
		//	val >>= 4;
Packit 4f15d5
		else
Packit 4f15d5
			return NULL;
Packit 4f15d5
Packit 4f15d5
		*dst++ = val;
Packit 4f15d5
		//if (c != '\0')
Packit 4f15d5
			str++;
Packit 4f15d5
		//if (*str == ':')
Packit 4f15d5
		//	str++;
Packit 4f15d5
		count--;
Packit 4f15d5
	}
Packit 4f15d5
	errno = (*str ? ERANGE : 0);
Packit 4f15d5
	return dst;
Packit 4f15d5
}