comparison 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
comparison
equal deleted inserted replaced
156:5f1f3f6fd865 157:f064dbcc5f41
1 /*
2 * This module implements some functions for initial parsing of decimal
3 * string arguments, intended for implementation of commands like
4 * write-iccid and write-imsi.
5 */
6
7 #include <sys/types.h>
8 #include <ctype.h>
9 #include <stdio.h>
10
11 parse_decimal_string_arg(arg, dest, maxdigits)
12 char *arg;
13 u_char *dest;
14 unsigned maxdigits;
15 {
16 unsigned n, ndig;
17
18 if (!*arg) {
19 fprintf(stderr,
20 "error: empty argument given for decimal string\n");
21 return(-1);
22 }
23 for (n = 0; *arg; ) {
24 if (!isdigit(*arg)) {
25 fprintf(stderr,
26 "error: non-digit char in decimal string argument\n");
27 return(-1);
28 }
29 if (n >= maxdigits) {
30 fprintf(stderr,
31 "error: decimal string exceeds limit of %u digits\n",
32 maxdigits);
33 return(-1);
34 }
35 dest[n++] = *arg++ - '0';
36 }
37 ndig = n;
38 while (n < maxdigits)
39 dest[n++] = 0xF;
40 return ndig;
41 }
42
43 pack_reversed_nibbles(nibbles, bytes, nbytes)
44 u_char *nibbles, *bytes;
45 unsigned nbytes;
46 {
47 u_char *sp, *dp;
48 unsigned n;
49
50 sp = nibbles;
51 dp = bytes;
52 for (n = 0; n < nbytes; n++) {
53 *dp++ = sp[0] | (sp[1] << 4);
54 sp += 2;
55 }
56 }