changeset 162:274bfd8bccd0

libutil: ICCID Luhn function implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 26 Feb 2021 21:16:45 +0000
parents 884b93362449
children 4cd2023f56a6
files libutil/Makefile libutil/iccid_luhn.c
diffstat 2 files changed, 28 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libutil/Makefile	Fri Feb 26 21:10:26 2021 +0000
+++ b/libutil/Makefile	Fri Feb 26 21:16:45 2021 +0000
@@ -2,7 +2,7 @@
 CFLAGS=	-O2
 OBJS=	alpha_decode.o alpha_fromfile.o alpha_valid.o decimal_str.o \
 	filesearch.o gsm7_decode.o gsm7_encode.o gsm7_encode_table.o \
-	gsm7_unpack.o hexdigits.o hexread.o hexstr.o nibbles2asc.o \
+	gsm7_unpack.o hexdigits.o hexread.o hexstr.o iccid_luhn.o nibbles2asc.o\
 	number_decode.o number_encode.o pinentry.o plmncodes.o revnibbles.o \
 	shorthand.o
 LIB=	libutil.a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libutil/iccid_luhn.c	Fri Feb 26 21:16:45 2021 +0000
@@ -0,0 +1,27 @@
+/*
+ * This module implements a function for computing the Luhn check digit
+ * for ICCIDs that follow the 18+1 convention.
+ */
+
+#include <sys/types.h>
+
+compute_iccid_luhn(digits)
+	u_char *digits;
+{
+	int i, dig, sum;
+
+	sum = 0;
+	for (i = 0; i < 18; i++) {
+		dig = digits[i];
+		if (i & 1) {
+			dig *= 2;
+			if (dig > 9)
+				dig -= 9;
+		}
+		sum += dig;
+	}
+	dig = sum % 10;
+	if (dig)
+		dig = 10 - dig;
+	return dig;
+}