diff libutil/plmncodes.c @ 157:f064dbcc5f41

libutil split from libcommon
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 26 Feb 2021 20:19:58 +0000
parents libcommon/plmncodes.c@d2e800abd257
children 3ddbc3fea5f0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libutil/plmncodes.c	Fri Feb 26 20:19:58 2021 +0000
@@ -0,0 +1,60 @@
+/*
+ * This module implements some functions for working with MCC-MNC PLMN codes.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+
+decode_plmn_3bytes(bin, asc, space_pad)
+	u_char *bin;
+	char *asc;
+{
+	asc[0] = encode_hex_digit(bin[0] & 0xF);
+	asc[1] = encode_hex_digit(bin[0] >> 4);
+	asc[2] = encode_hex_digit(bin[1] & 0xF);
+	asc[3] = '-';
+	asc[4] = encode_hex_digit(bin[2] & 0xF);
+	asc[5] = encode_hex_digit(bin[2] >> 4);
+	asc[6] = encode_hex_digit(bin[1] >> 4);
+	asc[7] = '\0';
+	if (asc[6] == 'F') {
+		if (space_pad)
+			asc[6] = ' ';
+		else
+			asc[6] = '\0';
+	}
+}
+
+encode_plmn_3bytes(asc, bin)
+	char *asc;
+	u_char *bin;
+{
+	u_char mcc[3], mnc[3];
+
+	if (!isxdigit(asc[0]) || !isxdigit(asc[1]) || !isxdigit(asc[2])) {
+inv:		fprintf(stderr, "error: invalid MCC-MNC argument\n");
+		return(-1);
+	}
+	mcc[0] = decode_hex_digit(asc[0]);
+	mcc[1] = decode_hex_digit(asc[1]);
+	mcc[2] = decode_hex_digit(asc[2]);
+	asc += 3;
+	if (*asc == '-')
+		asc++;
+	if (!isxdigit(asc[0]) || !isxdigit(asc[1]))
+		goto inv;
+	mnc[0] = decode_hex_digit(asc[0]);
+	mnc[1] = decode_hex_digit(asc[1]);
+	asc += 2;
+	if (*asc == '\0')
+		mnc[2] = 0xF;
+	else if (isxdigit(asc[0]) && asc[1] == '\0')
+		mnc[2] = decode_hex_digit(*asc);
+	else
+		goto inv;
+	bin[0] = (mcc[1] << 4) | mcc[0];
+	bin[1] = (mnc[2] << 4) | mcc[2];
+	bin[2] = (mnc[1] << 4) | mnc[0];
+	return(0);
+}