view uptools/libcoding/hexdecode.c @ 965:2969032bdfac

fcup-smsend[mult]: fix buglet in K&R C NULL pointer passing The only 100% safe way to pass a NULL pointer as a function argument in K&R C is to cast 0 to a pointer type; failing to do so may cause mysterious bugs (invalid stack frames or garbage in argument registers) on 64-bit machines. This issue has already been fixed in most of FC host tools, but I just found some missed spots: passing of NULL UDH to PDU encoding functions in fcup-smsend[mult] in the case of single (not concatenated) SMS.
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 01 Sep 2023 07:33:51 +0000
parents 18c692984549
children
line wrap: on
line source

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

#include <sys/types.h>
#include <ctype.h>

decode_hex_line(inbuf, outbuf, outmax)
	char *inbuf;
	u_char *outbuf;
	unsigned outmax;
{
	char *inp = inbuf;
	u_char *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;
}