/* acq_luhn.c - Luhn (mod-10) card number check (leaf util). */
#include "acq_common.h"

int acq_luhn_ok(const char *pan)
{
    int sum = 0, alt = 0, i, n;
    if (pan == 0) return 0;
    for (n = 0; pan[n]; n++) ;           /* strlen */
    if (n == 0) return 0;
    for (i = n - 1; i >= 0; i--) {
        char c = pan[i];
        if (c < '0' || c > '9') return 0;
        int d = c - '0';
        if (alt) { d *= 2; if (d > 9) d -= 9; }
        sum += d;
        alt = !alt;
    }
    return (sum % 10) == 0;
}
