#!/usr/bin/perl # NewtPackageFlags for Perl # written by Victor Rehorst http://www.chuma.org/code/newton/pkgflags/ # created 2001 July 16 # last modified 2004 Aug 16 my $version = "0.2"; if (@ARGV < 1) { print "NewtPackageFlags $version - http://www.chuma.org/code/newton/pkgflags/\n"; print "Usage: newtpackageflags \n"; } open INFILE, "<$ARGV[0]" or die "Couldn't open file for input: $ARGV[1]\n"; my $pkgheader; read INFILE, $pkgheader, 8; if ($pkgheader =~ /package[01]+/) { print "Now checking file: $ARGV[0]\n"; # print "File looks like a Newton Package to me...\n"; } else { die "File does not look like a Newton Package file\n"; } if (substr($pkgheader,7,1) == 0) { print "File is a 1.x package / does not contain relocation information\n"; } elsif (substr($pkgheader,7,1) == 1) { print "File is a 2.x package / contains relocation information\n"; } # read number of parts in this file my $numParts; seek INFILE, 50, 0; read INFILE, $numParts, 2; $numParts = unpack("n", $numParts); print "Number of parts in package: $numParts\n"; # read infoRef to package name my $nameOffset = undef; my $nameLen = undef; seek INFILE, 24, 0; read INFILE, $nameOffset, 2; $nameOffset = unpack("n", $nameOffset); seek INFILE, 26, 0; read INFILE, $nameLen, 2; $nameLen = unpack("n", $nameLen); #print "Raw package name Offset: '$nameOffset' Len:'$nameLen'\n"; my $pkgname; seek INFILE, ($nameOffset + 52) + ($numParts * 32), 0; read INFILE, $pkgname, ($nameLen); $pkgname =~ s/\000//g; print "Package Name: $pkgname\n"; # read infoRef to copyright info my $copyrightOffset; my $copyrightLen; seek INFILE, 20, 0; read INFILE, $copyrightOffset, 2; $copyrightOffset = unpack("n", $copyrightOffset); seek INFILE, 22, 0; read INFILE, $copyrightLen, 2; $copyrightLen = unpack("n", $copyrightLen); my $copyright; seek INFILE, ($copyrightOffset + 52) + ($numParts * 32), 0; read INFILE, $copyright, $copyrightLen; $copyright =~ s/\000//g; print "Copyright info: $copyright\n"; # find part information # let's start over... # $numParts contains the number of parts to look for # each part entry is 84 bytes long in total, first one starts at byte 52 my $foundBook = 0; for (my $i = 0; $i < $numParts; $i++) { seek INFILE, (52+($i*32)), 0; my $rawPartInfo; read INFILE, $rawPartInfo,32; my ($pOffset, $pSize1, $pSize2, $partType, $pRes1, $pFlags, $pInfo, $pRes2) = unpack("N3a4N4", $rawPartInfo); print "\nInformation for Part $i:\n"; print "\tOffset: $pOffset\n"; print "\tSize: $pSize1\n"; print "\tType: $partType\n"; if ($partType eq "book") { $foundBook = 1; } } exit; if (($foundBook == 1) && ($numParts == 1)) { # look for a PICT graphic in the book # read a byte at a time # first find 0x04, then if 0xF1 follows it... we've found the beginning of a PICT # the next 16 bytes are its size # then read $size bytes into a buffer, spit it out to /tmp/pict.pict my $PICTsize = 0; my ($tmp1, $tmp2); FINDBYTE: while (read (INFILE, $tmp1, 1)) { if ($tmp1 eq chr(4)) { # read the next byte read INFILE, $tmp2, 1; if ($tmp2 eq chr(241)) { # DINGDINGDING Found it... print "FOUND THE PICT START AT " . tell(INFILE) . "\n"; last FINDBYTE; } } } if ($tmp2 eq chr(241)) { print "Writing PICT data...\n"; # read the next two bytes as the size read INFILE, $PICTsize, 2; my ($PICTdata, $PICTlen); $PICTlen = hex(unpack("H*", $PICTsize)); #print "$PICTlen \n"; read INFILE, $PICTdata, $PICTlen-2; # write the file size and data to a new file open OUTFILE, ">/tmp/pict.pict"; print OUTFILE pack("x512", 0); print OUTFILE $PICTsize . $PICTdata; close OUTFILE; } }