| 1 | ############################################################################## | ||
| 2 | # | ||
| 3 | # ISO8583 parsing and construction ISO8583 message data stream | ||
| 4 | # Copyright (c) 2011-2024 Vladi Belperchinov-Shabanski "Cade" | ||
| 5 | # <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||
| 6 | # http://cade.noxrun.com/ | ||
| 7 | # https://github.com/cade-vs/perl-data-iso8583 | ||
| 8 | # | ||
| 9 | # GPL | ||
| 10 | # | ||
| 11 | ############################################################################## | ||
| 12 | package Data::ISO8583; | ||
| 13 | use strict; | ||
| 14 | use Exporter; | ||
| 15 | |||
| 16 | use Encode; | ||
| 17 | #use Data::Dumper; | ||
| 18 | #use Data::HexDump; | ||
| 19 | use Data::Tools 1.44; | ||
| 20 | |||
| 21 | our $VERSION = '2.44'; | ||
| 22 | |||
| 23 | our @ISA = qw( Exporter ); | ||
| 24 | our @EXPORT = qw( | ||
| 25 | |||
| 26 | parse_iso8583_fields | ||
| 27 | parse_iso8583_bitmap | ||
| 28 | |||
| 29 | ); | ||
| 30 | |||
| 31 | |||
| 32 | # ISO 8583:1987 | ||
| 33 | |||
| 34 | # parses fields, only handles bitmaps and fields data, returns parsed fields, possibly nested | ||
| 35 | sub parse_iso8583_fields | ||
| 36 | { | ||
| 37 | my $data = shift; | ||
| 38 | my $dict = shift; # hash ref with fields dictionary | ||
| 39 | my $opt = shift || {}; | ||
| 40 | |||
| 41 | #print HexDump $data; | ||
| 42 | |||
| 43 | my %data; # result data, will be returned as reference | ||
| 44 | use Tie::IxHash; | ||
| 45 | tie %data, 'Tie::IxHash'; | ||
| 46 | |||
| 47 | my ( $fmap, $pos ) = parse_iso8583_bitmap( $data, $opt->{ 'BML' }, $opt->{ 'BMO' }, $opt->{ 'BMB' } ); | ||
| 48 | |||
| 49 | #print Dumper( $fmap, $pos ); | ||
| 50 | |||
| 51 | for my $f ( @$fmap ) | ||
| 52 | { | ||
| 53 | my $d = $dict->{ $f }; | ||
| 54 | die "unknown dictionary field [$f]" unless $d; | ||
| 55 | |||
| 56 | my ( $n, $t, $tl, $l, $c ) = @$d{ qw( N T E L C ) }; # type, len, convert | ||
| 57 | my $v; | ||
| 58 | if( $t == 0 ) | ||
| 59 | { | ||
| 60 | # skip | ||
| 61 | next; | ||
| 62 | } | ||
| 63 | elsif( $t == 1 ) | ||
| 64 | { | ||
| 65 | #print "FIX: $f: \@$pos (\@x".int2hex($pos).") len=$l"; | ||
| 66 | $v = substr( $data, $pos, $l ); | ||
| 67 | $pos += $l; | ||
| 68 | } | ||
| 69 | elsif( $t == 2 ) | ||
| 70 | { | ||
| 71 | #print "VAR: $f: \@$pos (\@x".int2hex($pos).")"; | ||
| 72 | $tl ||= 1; # type len for variable data, bytes count | ||
| 73 | die "unsupported variable data length [$tl] for field [$f]" unless $tl == 1 or $tl == 2 or $tl == 4; | ||
| 74 | my $ld = substr( $data, $pos, $tl ); | ||
| 75 | $pos += $tl; | ||
| 76 | my $ll = unpack [undef,'C','S',undef,'L']->[$tl], $ld; | ||
| 77 | die "field $f has invalid length incoming from data [".str_hex($ld)."] expected $ll + $tl <= $l" unless $ll + $tl <= $l; | ||
| 78 | #my $ll = bcd2int( $ld ); | ||
| 79 | $v = substr( $data, $pos, $ll ); | ||
| 80 | #print " len=$tl byte(s)->".str_hex($ld)."->$ll "; | ||
| 81 | $pos += $ll; | ||
| 82 | } | ||
| 83 | else | ||
| 84 | { | ||
| 85 | die "not supported type [$t] for field [$f]"; | ||
| 86 | } | ||
| 87 | |||
| 88 | #print " HEX:V[".str_hex($v)."]"; | ||
| 89 | $v = bcd2str( $v ) if $c == 1; | ||
| 90 | $v = decode( 'posix-bc', $v ) if $c == 2; | ||
| 91 | $v = str_hex( $v ) if $c == 9; | ||
| 92 | #print "->[$v] || $n\n"; | ||
| 93 | $data{ $f } = $v; | ||
| 94 | } | ||
| 95 | |||
| 96 | return \%data; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub parse_iso8583_bitmap | ||
| 101 | { | ||
| 102 | my $data = shift; | ||
| 103 | my $len = shift || 8; # bitmap length in bytes, default is 8 | ||
| 104 | my $one = shift; # if true, no multiple bitmaps expected, bit 1 regular data | ||
| 105 | my $base = shift; # base, defaults to 2 | ||
| 106 | |||
| 107 | $base = $one ? 1 : 2 unless $base; # if single bitmap, and no base specified, set to 1 | ||
| 108 | |||
| 109 | my $fpos = $base - 1; | ||
| 110 | my @fmap; | ||
| 111 | my $skip = 0; | ||
| 112 | while(4) | ||
| 113 | { | ||
| 114 | my $bm = substr( $data, $skip, $len ); | ||
| 115 | |||
| 116 | #print "BITMAP: base: $base, one: [$one] ".str_hex($bm)." skip $skip, len $len\n"; | ||
| 117 | #print HexDump $bm; | ||
| 118 | |||
| 119 | return () unless length( $bm ) == $len; | ||
| 120 | my @bm = split //, unpack 'B' . ( $len * 8 ), $bm; | ||
| 121 | |||
| 122 | #print Dumper( $one, \@bm ); | ||
| 123 | |||
| 124 | my $next = shift @bm unless $one; | ||
| 125 | push @fmap, map { ++ $fpos; $_ ? $fpos : (); } @bm; | ||
| 126 | $skip += $len; | ||
| 127 | |||
| 128 | #print Dumper( $one, \@fmap ); | ||
| 129 | |||
| 130 | last unless $next; | ||
| 131 | } | ||
| 132 | |||
| 133 | return ( \@fmap, $skip ); | ||
| 134 | } | ||
| 135 | |||
| 136 | # ISO 8583:2003 | ||
| 137 | |||
| 138 | =pod | ||
| 139 | |||
| 140 | |||
| 141 | =head1 NAME | ||
| 142 | |||
| 143 | ISO8583 parsing and construction of message data stream | ||
| 144 | |||
| 145 | =head1 SYNOPSIS | ||
| 146 | |||
| 147 | use Data::ISO8583; | ||
| 148 | use Data::ISO8583::VISA; | ||
| 149 | |||
| 150 | my $msg_hash_ref = parse_iso8583_fields( $byte_data, $VISA_MESSAGE_FIELDS ); | ||
| 151 | |||
| 152 | my ( $fmap_arr_ref, $skip ) = parse_iso8583_bitmap( $byte_data, $len, $one, $base ); | ||
| 153 | |||
| 154 | my $field_62_hr = parse_iso8583_fields( $msg_hash_ref->{ 62 }, $VISA_MESSAGE_FIELD_62 ); | ||
| 155 | |||
| 156 | # field 61 has no bitmap, has only 3 fields, so to use the same parser, | ||
| 157 | # fake bitmap has to be passed with first 3 bits set and also bitmap size | ||
| 158 | # should be set to 1 (byte, BML) and flag raised that there is no chained bitmaps (BMO) | ||
| 159 | my $field_61_hr = parse_iso8583_fields( chr( 0b11100000 ) . $msg_hash_ref->{ 61 }, $VISA_MESSAGE_FIELD_61, { BML => 1, BMO => 1 } ); | ||
| 160 | |||
| 161 | # field 60 is similar to 61 but there are 10 fields so either larger bitmap is needed (2 bytes) | ||
| 162 | # or can be chained two 1-byte bitmaps: | ||
| 163 | my $field_60_hr = parse_iso8583_fields( chr( 0b11111111 ) . chr( 0b01110000 ) . $msg_hash_ref->{ 60 }, $VISA_MESSAGE_FIELD_60, { BML => 1 } ); | ||
| 164 | |||
| 165 | or: | ||
| 166 | |||
| 167 | my $field_60_hr = parse_iso8583_fields( pack( 'C2', 0b11111111, 0b01110000 ) . $msg_hash_ref->{ 60 }, $VISA_MESSAGE_FIELD_60, { BML => 1, BMB => 1 } ); | ||
| 168 | |||
| 169 | or with single bitmap of size 2 bytes without chaining: | ||
| 170 | |||
| 171 | my $field_60_hr = parse_iso8583_fields( pack( 'C2', 0b11111111, 0b11000000 ) . $msg_hash_ref->{ 60 }, $VISA_MESSAGE_FIELD_60, { BML => 2, BMO => 1 } ); | ||
| 172 | |||
| 173 | =head1 FUNCTIONS | ||
| 174 | |||
| 175 | =head2 parse_iso8583_fields( $byte_data, $msg_dictionary, \%options ); | ||
| 176 | |||
| 177 | This functions parses the incoming data stream, using the given message | ||
| 178 | dictionary and returns hash reference with parsed fields, keyed by field | ||
| 179 | number. | ||
| 180 | |||
| 181 | %options argument is optional and is used mostly to pass parameters to | ||
| 182 | parse_iso8583_bitmap() as: | ||
| 183 | |||
| 184 | { | ||
| 185 | BML => $bitmap_length, | ||
| 186 | BMO => $true_if_single_bitmap_only, | ||
| 187 | BMB => $field_base_index_ie_first_field_number, | ||
| 188 | } | ||
| 189 | |||
| 190 | =head2 parse_iso8583_bitmap( $byte_data, $len, $one ) | ||
| 191 | |||
| 192 | This is helper function, used by parse_iso8583_fields() byt can be useful | ||
| 193 | standalone so it is exported. It takes data stream looks for primary and | ||
| 194 | extended bitmaps (no limit for chained bitmaps) and returns array with | ||
| 195 | found fields' numbers. It also returns how many bytes are read from the | ||
| 196 | incoming byte data for the bitmaps. The second return value (skip) is used | ||
| 197 | to skip bitmap data in the source data: | ||
| 198 | |||
| 199 | my ( $fields_arr_ref, $skip ) = parse_iso8583_bitmap( $byte_data ); | ||
| 200 | my $fields_data = substr( $byte_data, $skip ); | ||
| 201 | |||
| 202 | This function has two optional arguments: | ||
| 203 | |||
| 204 | $len -- size of the bitmaps in bytes, defaults to 8 | ||
| 205 | $one -- if TRUE no chained bitmaps are searched and bit 1 is regular field | ||
| 206 | $base -- start index of the fields numbering | ||
| 207 | * defaults to 2 no bitmap chaining | ||
| 208 | * defaults to 1 if single bitmap | ||
| 209 | * if specified, given number will be used regardless bitmap count | ||
| 210 | |||
| 211 | |||
| 212 | =head1 TODO | ||
| 213 | |||
| 214 | (more docs) | ||
| 215 | |||
| 216 | =head1 DATA::ISO8583 SUB-MODULES | ||
| 217 | |||
| 218 | Data::Tools package includes several sub-modules: | ||
| 219 | |||
| 220 | * Data::ISO8583::VISA -- VISA-specific dictionaries and functions | ||
| 221 | |||
| 222 | =head1 GITHUB REPOSITORY | ||
| 223 | |||
| 224 | https://github.com/cade-vs/perl-data-iso8583 | ||
| 225 | |||
| 226 | git@github.com:cade-vs/perl-data-iso8583.git | ||
| 227 | |||
| 228 | git clone git@github.com:cade-vs/perl-data-iso8583.git | ||
| 229 | |||
| 230 | or | ||
| 231 | |||
| 232 | git clone https://github.com/cade-vs/perl-data-iso8583.git | ||
| 233 | |||
| 234 | =head1 AUTHOR | ||
| 235 | |||
| 236 | Copyright (c) 2011-2024 Vladi Belperchinov-Shabanski "Cade" | ||
| 237 | <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||
| 238 | http://cade.noxrun.com/ | ||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | 1; |