I have now decided to implement a (maybe temporary) solution, and it isn't that hard or kludgy as I thought :
sub RecSize { # Returns the length of the data required by a string for unpack() # Variable length (with "*") will result in an error # Platform specific stuff like "i" and "I" also result in an error my( $Data ) = @_; my( $Result, $Len, $Repeat ) = 0; my %Size = ( "a"=>1, # A string with arbitrary binary data, will be n +ull padded. "A"=>1, # An ascii string, will be space padded. "b"=>1, # A bit string (ascending bit order, like vec()) +. "B"=>1, # A bit string (descending bit order). "h"=>1, # A hex string (low nybble first). "H"=>1, # A hex string (high nybble first). "c"=>1, # A signed char value. "C"=>1, # An unsigned char value. "s"=>2, # A signed short value. "S"=>2, # An unsigned short value. "n"=>2, # A short in "network" (big-endian) order. "N"=>4, # A long in "network" (big-endian) order. "l"=>4, # A signed long value. "L"=>4, # An unsigned long value. "v"=>2, # A short in "VAX" (little-endian) order. "V"=>4, # A long in "VAX" (little-endian) order. "q"=>8, # A signed quad (64-bit) value. "Q"=>8, # An unsigned quad value. "x"=>1, # A null byte. "Z"=>1, # A zero terminated string (will need byte count + !) ); while ( $Data =~ s/([aAbBhHcCsSlLnNvVqQxZ])(\d*)// ) { $Repeat = $2 || 1; $Len = $Size{$1}; if ($Len) { $Result += $Len * $Repeat; } else { $Result = undef; last; }; }; return $Result; }; sub decodeRecord { my ($Value, $Records, @Names) = @_; my (@Values) = unpack( $Records, $Value ); my (%Result); foreach my $Name (@Names) { $Result{$Name} = $Values[0] if ($Name); shift @Values; }; return \%Result; };
This code is then used as follows :
$strMacBinaryHeader = "CZ64A4A4AAvvvaaNNVVvaA4A8VVaaN"; @nameMacBinaryHeader = ( "ID", "filename", "filetype", "filecreator", "fileflags", undef, "yoffs", "xoffs", "fileid", "fileflags2", undef, "dataforklength", "resourceforklength", "creationdate", "lastmodified", "infolength", "fileflags3", "macbinary3id", undef, "totalunpackedlength", "secondaryheaderlength", "macbinary3requiredversion", "CRC" );sub decodeMacBinaryHeader { my ($Buffer) = @_; my $Result = &decodeRecord($Buffer, $strMacBinaryHeader, @nameMacBin +aryHeader ); roundUp( \$Result->{dataforklength}, 128 ); return $Result; }; $HeaderSize = &RecSize($strMacBinaryHeader); $Filename = shift || "../macicon/test/icon"; open FILE, "< $Filename" or die "Error opening \"$Filename\" : $!\n"; binmode FILE; read FILE, $MacBinHeader, $HeaderSize; $Header = decodeMacBinaryHeader($MacBinHeader); $ResourceForkStart = $HeaderSize + $Header->{secondaryheaderlength} + $Header->{dataforklength}; &dump( $Header );
where &dump() is a simple routine that dumps a hash...

In reply to My current solution by Anonymous Monk
in thread How to organize file records by Corion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.