/* * Copyright (c) 1995, 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "pg_config.h" IDENT("@(#) checkdigit.c (Gunther Schadow) 12/19/96"); #include /* * A simple check digit method used by HP CareVue as their M10 or * M11 algorithm (n = 10 or 11 respectively). */ int cdmod(const char *id, int n) { int S_e = 0; int S_o = 0; const char *p = id; if(p == NULL) return 0; /* skip leading text */ while(*p != 0 && !isdigit(*p)) p++; /* goto end of digit string */ while(isdigit(*p)) p++; /* now go back again, adding and stop when the leading text or the beginning of the string is reached */ while(TRUE) { p--; if(p < id || !isdigit(*p)) break; S_o += ( *p & ~'0' ); p--; if(p < id || !isdigit(*p)) break; S_e += ( *p & ~'0' ); } return ( 2 * S_o + S_e ) % n; } /* This is the standard HL7 M10 algorithm */ int cdM10(unsigned long x) { int odd = 0, even = 0, sum = 0; while(x != 0) { odd = odd * 10 + x % 10; x = x / 10uL; if(x == 0) break; even = even * 10 + x % 10; x = x / 10uL; } odd = odd * 2; while(odd != 0 || even != 0) { sum = sum + odd % 10 + even % 10; odd = odd / 10; even = even / 10; } return (10 - (sum % 10)) % 10; }