annotate libutil/iccid_luhn.c @ 165:8a5c132fe871

simtool/grcard2.c comment update
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 26 Feb 2021 22:03:12 +0000
parents 274bfd8bccd0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
162
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements a function for computing the Luhn check digit
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * for ICCIDs that follow the 18+1 convention.
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 compute_iccid_luhn(digits)
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 u_char *digits;
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 {
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 int i, dig, sum;
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 sum = 0;
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 for (i = 0; i < 18; i++) {
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 dig = digits[i];
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 if (i & 1) {
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 dig *= 2;
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 if (dig > 9)
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 dig -= 9;
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 }
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 sum += dig;
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 }
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 dig = sum % 10;
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 if (dig)
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 dig = 10 - dig;
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 return dig;
274bfd8bccd0 libutil: ICCID Luhn function implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 }