+ my $blocks = $sector < 32 ? 4 : 16;
+
+ last if $pos >= length($card);
+ next if substr($card,$pos,$blocks * 0x10) eq "\x00" x ($blocks * 0x10);
+
+ # General purpose byte (GPB)
+ my $GBP = ord(substr($card,0x39,1));
+
+ if ( $sector == 0 ) {
+ printf "manufacturer block\nSerial number: %s\nCB: %s\nmanufacturer data: %s\n"
+ , unpack('H*',substr($card,0,4))
+ , unpack('H*',substr($card,4,1))
+ , unpack('H*',substr($card,5,11))
+ ;
+
+ # MAD
+ $ADV = $GBP & 0b00000011;
+ $MA = $GBP & 0b01000000;
+ $DA = $GBP & 0b10000000;
+ printf "ADV (MAD version code): %d\n", $ADV;
+ printf "MA (multiapplication): %s\n", $MA ? 'yes' : 'monoaplication';
+ printf "DA (MAD available): %s%s\n", $DA ? 'yes' : 'no',
+ substr($card,$pos+0x30,6) eq "\xA0\xA1\xA2\xA3\xA4\xA5" ? ' public' : '';
+
+ printf "Info byte (publisher sector): %x\n", ord(substr($card,0x11,1));
+ } elsif ( $DA ) {
+ my $mad_offset = 0x10 + ( $sector * 2 );
+ my $v = unpack('v',(substr($card, $mad_offset, 2)));
+ my $cluster_id = unpack('HH', (( $v & 0xff00 ) >> 8) );
+ my $full_id = sprintf "%04x",$v;
+ printf "# sector %-2d MAD@%x %04x [%s]\n%s\n", $sector, $mad_offset, $v
+ , $function_clusters->{ $cluster_id }
+ , $mad_id->{$full_id} || "FIXME: add $full_id from MAD_overview.pdf to __DATA__ at end of $0"
+ ;
+
+ if ( $v == 0x0004 ) {
+ # RLE encoded card holder information
+ my $data = substr( $card, $pos, 0x30);
+ my $o = 0;
+ my $types = {
+ 0b00 => 'surname',
+ 0b01 => 'given name',
+ 0b10 => 'sex',
+ 0b11 => 'any',
+ };
+ while ( substr($data,$o,1) ne "\x00" ) {
+ my $len = ord(substr($data,$o,1));
+ my $type = ( $len & 0b11000000 ) >> 6;
+ $len = $len & 0b00111111;
+ my $dump = substr($data,$o+1,$len-1);
+ $dump = '0x' . unpack('H*', $dump) if $type == 0b11; # any
+ printf "%-10s %2d %s\n", $types->{$type}, $len, $dump;
+ $o += $len + 1;
+ }
+ } elsif ( $v == 0x0015 ) {
+ printf "Card number: %s\n", unpack('h*',substr($card,$pos + 0x04,6));
+ }
+
+ } else {
+ printf "# sector %-2d with %d blocks\n", $sector, $blocks;
+ }
+
+ my $trailer_pos = $pos + $blocks * 0x10 - 0x10;
+ my $c1 = ( ord(substr($card,$trailer_pos+7,1)) & 0xf0 ) >> 4;
+ my $c2 = ( ord(substr($card,$trailer_pos+8,1)) & 0x0f );
+ my $c3 = ( ord(substr($card,$trailer_pos+8,1)) & 0xf0 ) >> 4;
+
+ printf "# trailer @%x %016b c1:%08b c2:%08b c3:%08b\n"
+ , unpack('n',(substr($card,$trailer_pos+7,2)))
+ , $trailer_pos, $c1, $c2, $c3