view libutil/iccid_luhn.c @ 103:3477438b5706 default tip

new fc-simtool command script: oper-sim-test
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 Aug 2022 16:34:43 +0000
parents 34bbb0585cab
children
line wrap: on
line source

/*
 * 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;
}