view libutil/decimal_str.c @ 157:f064dbcc5f41

libutil split from libcommon
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 26 Feb 2021 20:19:58 +0000
parents libcommon/decimal_str.c@3ef90bd13fbe
children 2557012666ea
line wrap: on
line source

/*
 * This module implements some functions for initial parsing of decimal
 * string arguments, intended for implementation of commands like
 * write-iccid and write-imsi.
 */

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

parse_decimal_string_arg(arg, dest, maxdigits)
	char *arg;
	u_char *dest;
	unsigned maxdigits;
{
	unsigned n, ndig;

	if (!*arg) {
		fprintf(stderr,
			"error: empty argument given for decimal string\n");
		return(-1);
	}
	for (n = 0; *arg; ) {
		if (!isdigit(*arg)) {
			fprintf(stderr,
			"error: non-digit char in decimal string argument\n");
			return(-1);
		}
		if (n >= maxdigits) {
			fprintf(stderr,
			"error: decimal string exceeds limit of %u digits\n",
				maxdigits);
			return(-1);
		}
		dest[n++] = *arg++ - '0';
	}
	ndig = n;
	while (n < maxdigits)
		dest[n++] = 0xF;
	return ndig;
}

pack_reversed_nibbles(nibbles, bytes, nbytes)
	u_char *nibbles, *bytes;
	unsigned nbytes;
{
	u_char *sp, *dp;
	unsigned n;

	sp = nibbles;
	dp = bytes;
	for (n = 0; n < nbytes; n++) {
		*dp++ = sp[0] | (sp[1] << 4);
		sp += 2;
	}
}