view smsc-sendmt/hexdecode.c @ 19:9ff041d85da5

proto-smsc-daemon: allow HLR GSUP server other than 127.0.0.1
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 16 Dec 2023 22:23:23 +0000
parents 7543aa173634
children
line wrap: on
line source

/*
 * This library module implements decoding of long hex strings,
 * such as SMS PDUs.
 */

#include <ctype.h>
#include <stdint.h>

int decode_hex_line(const char *inbuf, uint8_t *outbuf, unsigned outmax)
{
	const char *inp = inbuf;
	uint8_t *outp = outbuf;
	unsigned outcnt = 0;
	int c, d[2], i;

	while (*inp) {
		if (!isxdigit(inp[0]) || !isxdigit(inp[1]))
			return(-1);
		if (outcnt >= outmax)
			break;
		for (i = 0; i < 2; i++) {
			c = *inp++;
			if (isdigit(c))
				d[i] = c - '0';
			else if (isupper(c))
				d[i] = c - 'A' + 10;
			else
				d[i] = c - 'a' + 10;
		}
		*outp++ = (d[0] << 4) | d[1];
		outcnt++;
	}
	return outcnt;
}