[barcode] Command line barcode validation utility

Ciaran Mac Lochlainn ciaran@meritsolutions.ie
Wed Aug 31 18:20:31 CEST 2005


I have adapted code from GNU Barcode into a small command line utility 
which tells the user if the barcode is valid.

Enjoy :)

-- 
Ciarán Mac Lochlainn                      Senior Software Developer
Merit Solutions                    Claregalway, Co. Galway, Ireland
http://www.meritsolutions.ie                         +353 91 738055

"Awk: a strange programming language used by UNIX nerds to perform
 an amazing array of tasks."  (Unix for Dummies, p.18)

-------------- next part --------------
/*
 * checkbc.c - a commandline barcode validator
 *
 * Copyright (c) 2005 Merit Solutions (info@meritsolutions.ie)
 * 
 * Developed from functions in GNU Barcode.
 * 
 * Copyright (c) 1999 Michele Comitini (mcm@glisco.it)
 * Copyright (c) 1999 Alessandro Rubini (rubini@gnu.org)
 * Copyright (c) 1999 Prosa Srl. (prosa@prosa.it)
 *
 *   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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
	
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

char correct_digit;

static int ean_make_checksum(char *text, int mode)
{
    int esum = 0, osum = 0, i;
    int even=1; /* last char is even */

    if (strchr(text, ' '))
	i = strchr(text, ' ') - text; /* end of first part */
    else 
	i = strlen(text); /* end of all */

    while (i-- > 0) {
	if (even) esum += text[i]-'0';
	else      osum += text[i]-'0';
	even = !even;
    }
    if (!mode) { /* standard upc/ean checksum */
	i = (3*esum + osum) % 10;
	return (10-i) % 10; /* complement to 10 */
    } else { /* add-5 checksum */
	i = (3*esum + 9*osum);
	return i%10;
    }
}

/*
 * Check that the text can be encoded. Returns 0 or -1.
 * Accept:
 *   13 or 12 digits: EAN-13 w/ or w/o checksum
 * or
 *   8 or 7 digits: EAN-8 w/ or w/o checksum.
 * For both EAN-13 and EAN-8, accept an addon of 2 or 5 digits,
 * separated by ' '
 */
int Barcode_ean_verify(unsigned char *text)
{
    int i, len0, len, addon;
    unsigned char tmp[24], *spc;

    len = strlen(text);
    spc = strchr(text, ' ');
    if (spc) {
		len0 = spc - text;
		addon = len - len0 - 1;
		if (addon != 2 && addon != 5)
			return -1;
		for (i=len0+1; i<len; i++)
		    if (!isdigit(text[i]))
				return -1;
    } else
		len0 = len;

    for (i=0; i<len0; i++)
        if (!isdigit(text[i]))
            return -2;

    switch (len0) {
    case 8:
		strncpy(tmp, text, 7);
		tmp[7] = '\0';
		if (text[7] != (correct_digit=ean_make_checksum(tmp, 0) + '0'))
			return -1;
    case 7:
		strcpy(tmp,text);
		correct_digit=ean_make_checksum(tmp, 0) + '0';
    	break;
    case 13:
		strncpy(tmp, text, 12);
		tmp[12] = '\0';
		if (text[12] != (correct_digit=ean_make_checksum(tmp, 0) + '0'))
			return -1;
    case 12:
		strcpy(tmp,text);
		correct_digit=ean_make_checksum(tmp, 0) + '0';
    	break;
    default:
		return -3;
    }
    return 0;
}

/*
 * The main function
 */
int main(int argc, char **argv)
{

	int status;

    if (argc != 2) {
		fprintf(stderr,"Usage: %s <barcode>\n", argv[0]);
		exit(1);
	} else {
		if (status=Barcode_ean_verify(argv[1])) {
			//printf("status=%d\n",status);
			if (status==-2) {
				fprintf(stderr,"Barcode is not numeric.\n");
				exit(1);
			}
			if (status==-3) {
				fprintf(stderr,"Barcode is the wrong length.\n");
				exit(1);
			}
			if (correct_digit >= '0' && correct_digit <= '9')
				fprintf(stderr,"Barcode checksum does not match. (should be %c)\n",correct_digit);
			else
				fprintf(stderr,"Barcode is the wrong format.\n");
			exit(1);
		} else {
			if (strlen(argv[1])==7 || strlen(argv[1])==12)
				printf("Barcode checksum %c is omitted.\n",correct_digit);
			else
				printf("Barcode checksum matches.\n");
			exit(0);
		}
	}
}

-------------- next part --------------
No virus found in this outgoing message.
This is NOT a guarantee - you should scan all incoming
messages yourself with an up-to-date virus scanner.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.10.17/85 - Release Date: 30/08/2005


More information about the barcode mailing list