changeset 4:d88c5ab03447

libcoding/number_encode.c: import from fc-sim-tools/libutil
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 05 Aug 2023 03:17:09 +0000
parents 367dce9bf5a7
children c79d09284c5f
files libcoding/Makefile libcoding/number_encode.c
diffstat 2 files changed, 106 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libcoding/Makefile	Sat Aug 05 02:10:16 2023 +0000
+++ b/libcoding/Makefile	Sat Aug 05 03:17:09 2023 +0000
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2
-OBJS=	gsm7_encode.o gsm7_encode_table.o gsm7_pack.o hexout.o ucs2_bigend.o \
-	utf8_decode.o utf8_decode2.o
+OBJS=	gsm7_encode.o gsm7_encode_table.o gsm7_pack.o hexout.o number_encode.o \
+	ucs2_bigend.o utf8_decode.o utf8_decode2.o
 LIB=	libcoding.a
 
 all:	${LIB}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libcoding/number_encode.c	Sat Aug 05 03:17:09 2023 +0000
@@ -0,0 +1,104 @@
+/*
+ * This module implements functions for encoding phone numbers.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+digit_char_to_gsm(ch)
+{
+	switch (ch) {
+	case '0':
+	case '1':
+	case '2':
+	case '3':
+	case '4':
+	case '5':
+	case '6':
+	case '7':
+	case '8':
+	case '9':
+		return (ch - '0');
+	case '*':
+		return 0xA;
+	case '#':
+		return 0xB;
+	case 'a':
+	case 'b':
+	case 'c':
+		return (ch - 'a' + 0xC);
+	case 'A':
+	case 'B':
+	case 'C':
+		return (ch - 'A' + 0xC);
+	}
+	return (-1);
+}
+
+void
+pack_digit_bytes(digits, dest, num_digit_bytes)
+	u_char *digits, *dest;
+	unsigned num_digit_bytes;
+{
+	u_char *sp, *dp;
+	unsigned n;
+
+	sp = digits;
+	dp = dest;
+	for (n = 0; n < num_digit_bytes; n++) {
+		*dp++ = sp[0] | (sp[1] << 4);
+		sp += 2;
+	}
+}
+
+encode_phone_number_arg(arg, fixp, mode)
+	char *arg;
+	u_char *fixp;
+{
+	u_char digits[20];
+	unsigned ndigits, num_digit_bytes;
+	char *cp, *endp;
+	int c;
+
+	cp = arg;
+	if (*cp == '+') {
+		fixp[1] = 0x91;
+		cp++;
+	} else
+		fixp[1] = 0x81;
+	if (digit_char_to_gsm(*cp) < 0) {
+inv_arg:	fprintf(stderr, "error: invalid phone number argument\n");
+		return(-1);
+	}
+	for (ndigits = 0; ; ndigits++) {
+		c = digit_char_to_gsm(*cp);
+		if (c < 0)
+			break;
+		cp++;
+		if (ndigits >= 20) {
+			fprintf(stderr, "error: too many number digits\n");
+			return(-1);
+		}
+		digits[ndigits] = c;
+	}
+	if (mode)
+		fixp[0] = ndigits;
+	if (ndigits & 1)
+		digits[ndigits++] = 0xF;
+	num_digit_bytes = ndigits >> 1;
+	if (!mode)
+		fixp[0] = num_digit_bytes + 1;
+	pack_digit_bytes(digits, fixp + 2, num_digit_bytes);
+	if (*cp == ',') {
+		cp++;
+		if (!isdigit(*cp))
+			goto inv_arg;
+		fixp[1] = strtoul(cp, &endp, 0);
+		if (*endp)
+			goto inv_arg;
+	} else if (*cp)
+		goto inv_arg;
+	return(0);
+}