Esempio n. 1
0
File: l.c Progetto: krasm/coursera
void from_hex(const char * in, unsigned char * out) {
    while(*in != 0) {
        unsigned char c = hc2b(*in++);
        c = (c << 4) | hc2b(*in++);
        *out++ = c;
    }
}
Esempio n. 2
0
static int
hex2bin(unsigned char *out, unsigned char *in)
{
	unsigned char *out_start = out;
	unsigned char *end = in + strlen((char *)in);
	int is_low = 0;
	int c;

	/* Clamp to maximum packet size */
	if (end - in > MAX_PACKET_LEN*2) /* As we're reading nibbles */
		end = in + MAX_PACKET_LEN*2;

	while (in < end)
	{
		c = hc2b(in[0]);
		if (c < 0)
		{
			in++;
			continue;
		}
		if (is_low == 0)
		{
			out[0] = c << 4;
			is_low = 1;
		} else {
			out[0] |= (c & 0x0f);
			is_low = 0;
			out++;
		}
		in++;
	}

	return (int)(out - out_start);
}
Esempio n. 3
0
static int
hex2bin(guint8 *out, guint8 *out_end, char *in)
{
	guint8 *out_start = out;
	int is_low = 0;
	int c;

	while (*in != '\0')
	{
		c = hc2b(*(unsigned char *)in);
		if (c < 0)
		{
			in++;
			continue;
		}
		if (out == out_end)
		{
			/* Too much data */
			return -1;
		}
		if (is_low == 0)
		{
			*out = c << 4;
			is_low = 1;
		} else {
			*out |= (c & 0x0f);
			is_low = 0;
			out++;
		}
		in++;
	}

	return (int)(out - out_start);
}
Esempio n. 4
0
File: l.c Progetto: krasm/coursera
static void test_hc2b() {
    assert(hc2b('0') == 0);
    assert(hc2b('1') == 1);
    assert(hc2b('2') == 2);
    assert(hc2b('3') == 3);
    assert(hc2b('4') == 4);
    assert(hc2b('5') == 5);
    assert(hc2b('6') == 6);
    assert(hc2b('7') == 7);
    assert(hc2b('8') == 8);
    assert(hc2b('9') == 9);
    assert(hc2b('A') == 10);
    assert(hc2b('b') == 11);
    assert(hc2b('C') == 12);
    assert(hc2b('d') == 13);
    assert(hc2b('e') == 14);
    assert(hc2b('f') == 0x0f);
}