#!/usr/bin/perl -w use Cwd; use warnings; use strict; use Getopt::Long; use Switch; use constant FILEHDR => 4; use constant CDRLEN => 286; my ($trace, $help, $infile); my $swap = ''; my $indir = getcwd; my $outdir = getcwd; GetOptions ( "h|help" => \$help, "filename|f=s" => \$infile, "swap|s" => \$swap, "input|i=s" => \$indir, "output|o=s" => \$outdir, "trace|t" => \$trace ) or usage(); sub usage { exit; } my @tt = ("Flags", "Record Seq.", "LAC Length", "Directory No", "Others"); my @dtf = ("Year","Month","Day","Hour","Minute","Second", "Reserved","Duration"); my $outfile = $infile; my $data; my $i; sub inttohex { my $int = shift; return sprintf ("%02X", $int); } sub hextoint { my $hex = shift; return sprintf ("%d", hex($hex)); } ## Convert each ASCII character to a two-digit hex number. sub asctohex () { (my $hex = shift) =~ s/(.|\n)/sprintf("%02lx", ord $1)/eg; return $hex; } # Convert each two-digit hex number back to an ASCII character. sub hextoasc { (my $asc = shift) =~ s/([a-fA-F0-9]{2})/chr(hex $1)/eg; return $asc; } sub strtoasc { my $str = shift; my $asc = ""; my $loc = 0; my $i; for ($i = 0; $i < length($str); $i+=2) { $asc = $asc . hextoasc(substr($str, $loc, 2)); $loc = $loc + 2; } return $asc; } my ($partnerdir_no); if ($infile) { #open (OUTPUT, ">$outdir/$outfile"); open (DATA, "$indir/$infile"); binmode DATA; my @rec; until (eof DATA) { read (DATA, $data, 2); my $tag = unpack "H2", substr $data,0,1,''; my $length = unpack "C", substr $data,0,1,''; $length -= 2; if ($length == "81") { $length = unpack "C", substr $data,0,1,''; $length -= 1; } my $loc = 0; while (read (DATA, $data, $length)) { my @header = unpack "H6 H2 H2 H12", substr $data,0,11,''; if ($trace) { printf ("%-20s : %-5s >%s<\n", "TAG" , $tag, inttohex($tag)); printf ("%-20s : %-5s >%s<\n", "RECORD LENGTH", $length+2, inttohex($length+2)); printf ("HEADER\n"); for ($i=0; $i<@header; $i++) { printf (" |- %-16s : %s\n", $tt[$i],$header[$i]); } } printf ("\nDATA PACKAGE\n"); while ($data) { $tag = unpack "C", substr $data,$loc,1,''; printf ("%-20s : %-5s >%s<\n", "TAG", $tag, inttohex($tag)); switch ($tag) { # x64 - length fix 11 bytes case 100 { @rec = unpack "C C C C C C C C3", substr $data,0,10; printf " |- DateTime/Duration\n"; for (my $i=0; $i<8; $i++) { printf " |- %-20s : %02s >%s<\n", $dtf[$i], $rec[$i], inttohex($rec[$i]); } } # x65 case 101 { my $length = unpack "C", substr $data,0,1,''; $length += 1; if ($length == "81") { $length = unpack "C", substr $data,0,1,''; $length += 1; } $partnerdir_no = unpack "H*", substr $data,0,$length; printf ("PARTNER DIRECTORY NO. : %s\n", $partnerdir_no); } } } } } close(DATA); #close(OUTPUT); }