#!/usr/bin/perl
#Copyright (C) 2000 Chrylis under the GNU GPL
#
#This decoder is for use with Azalea's Qtools and the "free or low-cost
#barcode scanners" out there.  Since scanning an Azalea-based barcode will
#return an encoded string, this program decodes it into that number you typed
#into their software in the first place.
#
#Input: one swipe from a keyboard-based "free or low cost barcode scanner"
#Output: the barcode decoded into decimal numeric form

%decodeList = (chr(194) => "00", #Set up the translation tables
               chr(184) => "--", #I'm sure you could use a tr///, but I really
               chr(32)  => "--");#don't know how.
for($i=33; $i<=41; $i++) {$decodeList{chr($i)} = "0".$i-32}
for($i=42; $i<=125; $i++) {$decodeList{chr($i)} = $i-32}
for($i=185; $i<=190; $i++) {$decodeList{chr($i)} = $i-91}
for($i=191; $i<=193; $i++) {$decodeList{chr($i)} = "--"}


print "Swipe barcode: ";
#Mostly courtesy of Larry Wall, but with modifications
<STDIN> =~ /\.([^.]+)\.$/;
$a = $1;
@i =
    map {
        tr/a-zA-Z0-9+-/ -_/;
        $_ = unpack 'u', chr(32 + length()*3/4) . $_;
        s/\0+$//;
        $_ ^= "C" x length;
    } ($a);
$i = $i[0];

$number = "";
while(true) {last unless $i =~ /(.)(.*)/; $number .= "$decodeList{$1}"; $i=$2;}
print "The barcode ID is: $number";
