| 1 |
#!/usr/bin/env perl |
| 2 |
# |
| 3 |
# Dump a Palm PDB or PRC database. |
| 4 |
|
| 5 |
use 5.006; |
| 6 |
use strict; |
| 7 |
use Palm::PDB; |
| 8 |
use Palm::Raw; |
| 9 |
|
| 10 |
our $VERSION = '1.400'; # VERSION |
| 11 |
# This file is part of Palm-PDB 1.400 (March 7, 2015) |
| 12 |
|
| 13 |
our ( %PDBHandlers, %PRCHandlers, $hexdump ); |
| 14 |
|
| 15 |
*PDBHandlers = *Palm::PDB::PDBHandlers; |
| 16 |
*PRCHandlers = *Palm::PDB::PRCHandlers; |
| 17 |
|
| 18 |
&Palm::PDB::RegisterPRCHandlers("Palm::Raw", |
| 19 |
[ "", "" ] |
| 20 |
); |
| 21 |
|
| 22 |
$hexdump = 1; # By default, print hex dumps of everything |
| 23 |
|
| 24 |
# Parse command-line arguments |
| 25 |
my $arg; |
| 26 |
while (($arg = $ARGV[0]) =~ /^-/) |
| 27 |
{ |
| 28 |
$arg = shift; |
| 29 |
|
| 30 |
if (($arg eq "-h") || ($arg eq "-help") || ($arg eq "--help")) |
| 31 |
{ |
| 32 |
print <<EOT; |
| 33 |
Usage: $0 [options] pdb-file |
| 34 |
Options: |
| 35 |
-h, -help, --help This message. |
| 36 |
-nohex Don't print hex dumps |
| 37 |
-M<module> Load <module> (e.g., -MPalm::Address) |
| 38 |
EOT |
| 39 |
#' |
| 40 |
exit 0; |
| 41 |
} elsif ($arg =~ /^-M/) |
| 42 |
{ |
| 43 |
eval "use $';"; |
| 44 |
} elsif ($arg eq "-nohex") |
| 45 |
{ |
| 46 |
$hexdump = 0; |
| 47 |
} else { |
| 48 |
die "Unrecognized option: $arg\n"; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
my $fname = shift; |
| 53 |
|
| 54 |
die "No such file: $fname\n" if ! -f $fname; |
| 55 |
|
| 56 |
my $EPOCH_1904 = 2082844800; # Difference between Palm's |
| 57 |
# epoch (Jan. 1, 1904) and |
| 58 |
# Unix's epoch (Jan. 1, 1970), |
| 59 |
# in seconds. |
| 60 |
my $HeaderLen = 32+2+2+(9*4); # Size of database header |
| 61 |
my $RecIndexHeaderLen = 6; # Size of record index header |
| 62 |
my $IndexRecLen = 8; # Length of record index entry |
| 63 |
my $IndexRsrcLen = 10; # Length of resource index entry |
| 64 |
|
| 65 |
#%PDBHandlers = (); # Record handler map |
| 66 |
#%PRCHandlers = (); # Resource handler map |
| 67 |
|
| 68 |
|
| 69 |
#&Palm::PDB::rawread($fname); |
| 70 |
&rawread($fname); |
| 71 |
|
| 72 |
#package Palm::PDB; # XXX - Gross hack! |
| 73 |
sub rawread |
| 74 |
{ |
| 75 |
my $self = new Palm::Raw; |
| 76 |
my $fname = shift; # Filename to read from |
| 77 |
my $buf; # Buffer into which to read stuff |
| 78 |
|
| 79 |
# Open database file |
| 80 |
open PDB, "< $fname" or die "Can't open \"$fname\": $!\n"; |
| 81 |
binmode PDB; # Parse as binary file under MS-DOS |
| 82 |
|
| 83 |
# Get the size of the file. It'll be useful later |
| 84 |
seek PDB, 0, 2; # 2 == SEEK_END. Seek to the end. |
| 85 |
$self->{_size} = tell PDB; |
| 86 |
print "File size: $self->{_size}\n"; |
| 87 |
seek PDB, 0, 0; # 0 == SEEK_START. Rewind to the beginning. |
| 88 |
|
| 89 |
# Read header |
| 90 |
my $name; |
| 91 |
my $attributes; |
| 92 |
my $version; |
| 93 |
my $ctime; |
| 94 |
my $mtime; |
| 95 |
my $baktime; |
| 96 |
my $modnum; |
| 97 |
my $appinfo_offset; |
| 98 |
my $sort_offset; |
| 99 |
my $type; |
| 100 |
my $creator; |
| 101 |
my $uniqueIDseed; |
| 102 |
|
| 103 |
read PDB, $buf, $HeaderLen; # Read the PDB header |
| 104 |
print "Database header:\n"; |
| 105 |
if ($hexdump) |
| 106 |
{ |
| 107 |
&hexdump(" ", $buf); |
| 108 |
print "\n"; |
| 109 |
} |
| 110 |
|
| 111 |
# Split header into its component fields |
| 112 |
($name, $attributes, $version, $ctime, $mtime, $baktime, |
| 113 |
$modnum, $appinfo_offset, $sort_offset, $type, $creator, |
| 114 |
$uniqueIDseed) = |
| 115 |
unpack "a32 n n N N N N N N a4 a4 N", $buf; |
| 116 |
|
| 117 |
($self->{name} = $name) =~ s/\0*$//; |
| 118 |
$self->{attributes}{resource} = 1 if $attributes & 0x0001; |
| 119 |
$self->{attributes}{"read-only"} = 1 if $attributes & 0x0002; |
| 120 |
$self->{attributes}{"AppInfo dirty"} = 1 if $attributes & 0x0004; |
| 121 |
$self->{attributes}{backup} = 1 if $attributes & 0x0008; |
| 122 |
$self->{attributes}{"OK newer"} = 1 if $attributes & 0x0010; |
| 123 |
$self->{attributes}{reset} = 1 if $attributes & 0x0020; |
| 124 |
$self->{attributes}{open} = 1 if $attributes & 0x0040; |
| 125 |
$self->{attributes}{launchable} = 1 if $attributes & 0x0200; |
| 126 |
$self->{version} = $version; |
| 127 |
$self->{ctime} = $ctime - $EPOCH_1904; |
| 128 |
$self->{mtime} = $mtime - $EPOCH_1904; |
| 129 |
$self->{baktime} = $baktime - $EPOCH_1904; |
| 130 |
$self->{modnum} = $modnum; |
| 131 |
# _appinfo_offset and _sort_offset are private fields |
| 132 |
$self->{_appinfo_offset} = $appinfo_offset; |
| 133 |
$self->{_sort_offset} = $sort_offset; |
| 134 |
$self->{type} = $type; |
| 135 |
$self->{creator} = $creator; |
| 136 |
$self->{uniqueIDseed} = $uniqueIDseed; |
| 137 |
|
| 138 |
print <<EOT; |
| 139 |
Name: $name |
| 140 |
Attributes: @{[sprintf("0x%02x", $attributes)]} |
| 141 |
EOT |
| 142 |
print " "x19; |
| 143 |
print " LAUNCHABLE" if $self->{attributes}{launchable}; |
| 144 |
print " OPEN" if $self->{attributes}{open}; |
| 145 |
print " RESET" if $self->{attributes}{reset}; |
| 146 |
print " OKNEWER" if $self->{attributes}{"OK newer"}; |
| 147 |
print " BACKUP" if $self->{attributes}{backup}; |
| 148 |
print " APPINFO-DIRTY" if $self->{attributes}{"AppInfo dirty"}; |
| 149 |
print " READ-ONLY" if $self->{attributes}{"read-only"}; |
| 150 |
print " RESOURCE" if $self->{attributes}{resource}; |
| 151 |
print "\n"; |
| 152 |
print <<EOT; |
| 153 |
Version: $version |
| 154 |
Ctime: $ctime @{[scalar(localtime($ctime-$EPOCH_1904))]} |
| 155 |
Mtime: $mtime @{[scalar(localtime($mtime-$EPOCH_1904))]} |
| 156 |
Backup time: $baktime @{[scalar(localtime($baktime-$EPOCH_1904))]} |
| 157 |
Mod number: $modnum |
| 158 |
AppInfo offset: $appinfo_offset |
| 159 |
Sort offset: $sort_offset |
| 160 |
Type: $type |
| 161 |
Creator: $creator |
| 162 |
Unique ID seed: $uniqueIDseed |
| 163 |
|
| 164 |
EOT |
| 165 |
|
| 166 |
# Rebless this PDB object, depending on its type and/or |
| 167 |
# creator. This allows us to magically invoke the proper |
| 168 |
# &Parse*() function on the various parts of the database. |
| 169 |
|
| 170 |
# Look for most specific handlers first, least specific ones |
| 171 |
# last. That is, first look for a handler that deals |
| 172 |
# specifically with this database's creator and type, then for |
| 173 |
# one that deals with this database's creator and any type, |
| 174 |
# and finally for one that deals with anything. |
| 175 |
|
| 176 |
my $handler; |
| 177 |
if ($self->{attributes}{resource}) |
| 178 |
{ |
| 179 |
# Look among resource handlers |
| 180 |
$handler = $PRCHandlers{$self->{creator}}{$self->{type}} || |
| 181 |
$PRCHandlers{undef}{$self->{type}} || |
| 182 |
$PRCHandlers{$self->{creator}}{""} || |
| 183 |
$PRCHandlers{""}{""}; |
| 184 |
} else { |
| 185 |
# Look among record handlers |
| 186 |
$handler = $PDBHandlers{$self->{creator}}{$self->{type}} || |
| 187 |
$PDBHandlers{""}{$self->{type}} || |
| 188 |
$PDBHandlers{$self->{creator}}{""} || |
| 189 |
$PDBHandlers{""}{""}; |
| 190 |
} |
| 191 |
|
| 192 |
if (defined($handler)) |
| 193 |
{ |
| 194 |
bless $self, $handler; |
| 195 |
} else { |
| 196 |
# XXX - This should probably return 'undef' or something, |
| 197 |
# rather than die. |
| 198 |
die "No handler defined for creator \"$creator\", type \"$type\"\n"; |
| 199 |
} |
| 200 |
|
| 201 |
## Read record/resource index |
| 202 |
# Read index header |
| 203 |
read PDB, $buf, $RecIndexHeaderLen; |
| 204 |
print "Record/resource index header:\n"; |
| 205 |
if ($hexdump) |
| 206 |
{ |
| 207 |
&hexdump(" ", $buf); |
| 208 |
print "\n"; |
| 209 |
} |
| 210 |
|
| 211 |
my $next_index; |
| 212 |
my $numrecs; |
| 213 |
|
| 214 |
($next_index, $numrecs) = unpack "N n", $buf; |
| 215 |
$self->{_numrecs} = $numrecs; |
| 216 |
|
| 217 |
print <<EOT; |
| 218 |
Next index: $next_index |
| 219 |
# records: $numrecs |
| 220 |
|
| 221 |
EOT |
| 222 |
|
| 223 |
# Read the index itself |
| 224 |
if ($self->{attributes}{resource}) |
| 225 |
{ |
| 226 |
&_load_rsrc_index($self, \*PDB); |
| 227 |
} else { |
| 228 |
&_load_rec_index($self, \*PDB); |
| 229 |
} |
| 230 |
|
| 231 |
# Ignore the two NUL bytes that are usually here. We'll seek() |
| 232 |
# around them later. |
| 233 |
|
| 234 |
# Read AppInfo block, if it exists |
| 235 |
if ($self->{_appinfo_offset} != 0) |
| 236 |
{ |
| 237 |
&_load_appinfo_block($self, \*PDB); |
| 238 |
} |
| 239 |
|
| 240 |
# Read sort block, if it exists |
| 241 |
if ($self->{_sort_offset} != 0) |
| 242 |
{ |
| 243 |
&_load_sort_block($self, \*PDB); |
| 244 |
} |
| 245 |
|
| 246 |
# Read record/resource list |
| 247 |
if ($self->{attributes}{resource}) |
| 248 |
{ |
| 249 |
&_load_resources($self, \*PDB); |
| 250 |
} else { |
| 251 |
&_load_records($self, \*PDB); |
| 252 |
} |
| 253 |
|
| 254 |
# These keys were needed for parsing the file, but are not |
| 255 |
# needed any longer. Delete them. |
| 256 |
delete $self->{_index}; |
| 257 |
delete $self->{_numrecs}; |
| 258 |
delete $self->{_appinfo_offset}; |
| 259 |
delete $self->{_sort_offset}; |
| 260 |
delete $self->{_size}; |
| 261 |
|
| 262 |
close PDB; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
# _load_rec_index |
| 267 |
# Private function. Read the record index, for a record database |
| 268 |
sub _load_rec_index |
| 269 |
{ |
| 270 |
my $pdb = shift; |
| 271 |
my $fh = shift; # Input file handle |
| 272 |
my $i; |
| 273 |
my $lastoffset = 0; |
| 274 |
|
| 275 |
print "Record index:\n"; |
| 276 |
|
| 277 |
# Read each record index entry in turn |
| 278 |
for ($i = 0; $i < $pdb->{_numrecs}; $i++) |
| 279 |
{ |
| 280 |
my $buf; # Input buffer |
| 281 |
|
| 282 |
print " Record index entry $i\n"; |
| 283 |
|
| 284 |
# Read the next record index entry |
| 285 |
my $offset; |
| 286 |
my $attributes; |
| 287 |
my @id; # Raw ID |
| 288 |
my $id; # Numerical ID |
| 289 |
my $entry = {}; # Parsed index entry |
| 290 |
|
| 291 |
read $fh, $buf, $IndexRecLen; |
| 292 |
if ($hexdump) |
| 293 |
{ |
| 294 |
&hexdump(" ", $buf); |
| 295 |
print "\n"; |
| 296 |
} |
| 297 |
|
| 298 |
# The ID field is a bit weird: it's represented as 3 |
| 299 |
# bytes, but it's really a double word (long) value. |
| 300 |
|
| 301 |
($offset, $attributes, @id) = unpack "N C C3", $buf; |
| 302 |
if ($offset == $lastoffset) |
| 303 |
{ |
| 304 |
print STDERR "Record $i has same offset as previous one: $offset\n"; |
| 305 |
} |
| 306 |
$lastoffset = $offset; |
| 307 |
|
| 308 |
$entry->{offset} = $offset; |
| 309 |
$entry->{attributes}{expunged} = 1 if $attributes & 0x80; |
| 310 |
$entry->{attributes}{dirty} = 1 if $attributes & 0x40; |
| 311 |
$entry->{attributes}{deleted} = 1 if $attributes & 0x20; |
| 312 |
$entry->{attributes}{private} = 1 if $attributes & 0x10; |
| 313 |
$entry->{id} = ($id[0] << 16) | |
| 314 |
($id[1] << 8) | |
| 315 |
$id[2]; |
| 316 |
|
| 317 |
# The lower 4 bits of the attributes field are |
| 318 |
# overloaded: If the record has been deleted and/or |
| 319 |
# expunged, then bit 0x08 indicates whether the record |
| 320 |
# should be archived. Otherwise (if it's an ordinary, |
| 321 |
# non-deleted record), the lower 4 bits specify the |
| 322 |
# category that the record belongs in. |
| 323 |
if (($attributes & 0xa0) == 0) |
| 324 |
{ |
| 325 |
$entry->{category} = $attributes & 0x0f; |
| 326 |
} else { |
| 327 |
$entry->{attributes}{archive} = 1 |
| 328 |
if $attributes & 0x08; |
| 329 |
} |
| 330 |
|
| 331 |
print <<EOT; |
| 332 |
Offset: $offset |
| 333 |
Attributes: @{[sprintf("0x%02x", $attributes), keys %{$entry->{attributes}}]} |
| 334 |
Category: $entry->{category} |
| 335 |
ID: @{[sprintf("0x%02x%02x%02x", @id)]} |
| 336 |
|
| 337 |
EOT |
| 338 |
|
| 339 |
# Put this information on a temporary array |
| 340 |
push @{$pdb->{_index}}, $entry; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
# XXX - Make this print out debugging information |
| 345 |
# _load_rsrc_index |
| 346 |
# Private function. Read the resource index, for a resource database |
| 347 |
sub _load_rsrc_index |
| 348 |
{ |
| 349 |
my $pdb = shift; |
| 350 |
my $fh = shift; # Input file handle |
| 351 |
my $i; |
| 352 |
|
| 353 |
print "Resource index:\n"; |
| 354 |
|
| 355 |
# Read each resource index entry in turn |
| 356 |
for ($i = 0; $i < $pdb->{_numrecs}; $i++) |
| 357 |
{ |
| 358 |
my $buf; # Input buffer |
| 359 |
|
| 360 |
print " Resource index entry $i\n"; |
| 361 |
|
| 362 |
# Read the next resource index entry |
| 363 |
my $type; |
| 364 |
my $id; |
| 365 |
my $offset; |
| 366 |
my $entry = {}; # Parsed index entry |
| 367 |
|
| 368 |
read $fh, $buf, $IndexRsrcLen; |
| 369 |
if ($hexdump) |
| 370 |
{ |
| 371 |
&hexdump(" ", $buf); |
| 372 |
print "\n"; |
| 373 |
} |
| 374 |
|
| 375 |
($type, $id, $offset) = unpack "a4 n N", $buf; |
| 376 |
|
| 377 |
$entry->{type} = $type; |
| 378 |
$entry->{id} = $id; |
| 379 |
$entry->{offset} = $offset; |
| 380 |
|
| 381 |
print <<EOT; |
| 382 |
Offset: $offset |
| 383 |
ID: $id |
| 384 |
Type: $type |
| 385 |
|
| 386 |
EOT |
| 387 |
|
| 388 |
push @{$pdb->{_index}}, $entry; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
# _load_appinfo_block |
| 393 |
# Private function. Read the AppInfo block |
| 394 |
sub _load_appinfo_block |
| 395 |
{ |
| 396 |
my $pdb = shift; |
| 397 |
my $fh = shift; # Input file handle |
| 398 |
my $len; # Length of AppInfo block |
| 399 |
my $buf; # Input buffer |
| 400 |
|
| 401 |
print "AppInfo block:\n"; |
| 402 |
|
| 403 |
# Sanity check: make sure we're positioned at the beginning of |
| 404 |
# the AppInfo block |
| 405 |
if (tell($fh) > $pdb->{_appinfo_offset}) |
| 406 |
{ |
| 407 |
die "Bad AppInfo offset: expected ", |
| 408 |
sprintf("0x%08x", $pdb->{_appinfo_offset}), |
| 409 |
", but I'm at ", |
| 410 |
tell($fh), "\n"; |
| 411 |
} |
| 412 |
|
| 413 |
# Seek to the right place, if necessary |
| 414 |
if (tell($fh) != $pdb->{_appinfo_offset}) |
| 415 |
{ |
| 416 |
seek PDB, $pdb->{_appinfo_offset}, 0; |
| 417 |
} |
| 418 |
|
| 419 |
# There's nothing that explicitly gives the size of the |
| 420 |
# AppInfo block. Rather, it has to be inferred from the offset |
| 421 |
# of the AppInfo block (previously recorded in |
| 422 |
# $pdb->{_appinfo_offset}) and whatever's next in the file. |
| 423 |
# That's either the sort block, the first data record, or the |
| 424 |
# end of the file. |
| 425 |
|
| 426 |
if ($pdb->{_sort_offset}) |
| 427 |
{ |
| 428 |
# The next thing in the file is the sort block |
| 429 |
$len = $pdb->{_sort_offset} - $pdb->{_appinfo_offset}; |
| 430 |
} elsif ((defined $pdb->{_index}) && @{$pdb->{_index}}) |
| 431 |
{ |
| 432 |
# There's no sort block; the next thing in the file is |
| 433 |
# the first data record |
| 434 |
$len = $pdb->{_index}[0]{offset} - |
| 435 |
$pdb->{_appinfo_offset}; |
| 436 |
} else { |
| 437 |
# There's no sort block and there are no records. The |
| 438 |
# AppInfo block goes to the end of the file. |
| 439 |
$len = $pdb->{_size} - $pdb->{_appinfo_offset}; |
| 440 |
} |
| 441 |
|
| 442 |
# Read the AppInfo block |
| 443 |
read $fh, $buf, $len; |
| 444 |
if ($hexdump) |
| 445 |
{ |
| 446 |
&hexdump(" ", $buf); |
| 447 |
print "\n"; |
| 448 |
} |
| 449 |
|
| 450 |
# Tell the real class to parse the AppInfo block |
| 451 |
$pdb->{appinfo} = $pdb->ParseAppInfoBlock($buf); |
| 452 |
|
| 453 |
# Print out the parsed values |
| 454 |
if (ref($pdb->{appinfo}) ne "") |
| 455 |
{ |
| 456 |
&dumphash($pdb->{appinfo}, "\t"); |
| 457 |
print "\n"; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
# _load_sort_block |
| 462 |
# Private function. Read the sort block. |
| 463 |
sub _load_sort_block |
| 464 |
{ |
| 465 |
my $pdb = shift; |
| 466 |
my $fh = shift; # Input file handle |
| 467 |
my $len; # Length of sort block |
| 468 |
my $buf; # Input buffer |
| 469 |
|
| 470 |
print "Sort block:\n"; |
| 471 |
|
| 472 |
# Sanity check: make sure we're positioned at the beginning of |
| 473 |
# the sort block |
| 474 |
if (tell($fh) > $pdb->{_sort_offset}) |
| 475 |
{ |
| 476 |
die "Bad sort block offset: expected ", |
| 477 |
sprintf("0x%08x", $pdb->{_sort_offset}), |
| 478 |
", but I'm at ", |
| 479 |
tell($fh), "\n"; |
| 480 |
} |
| 481 |
|
| 482 |
# Seek to the right place, if necessary |
| 483 |
if (tell($fh) != $pdb->{_sort_offset}) |
| 484 |
{ |
| 485 |
seek PDB, $pdb->{_sort_offset}, 0; |
| 486 |
} |
| 487 |
|
| 488 |
# There's nothing that explicitly gives the size of the sort |
| 489 |
# block. Rather, it has to be inferred from the offset of the |
| 490 |
# sort block (previously recorded in $pdb->{_sort_offset}) |
| 491 |
# and whatever's next in the file. That's either the first |
| 492 |
# data record, or the end of the file. |
| 493 |
|
| 494 |
if (defined($pdb->{_index})) |
| 495 |
{ |
| 496 |
# The next thing in the file is the first data record |
| 497 |
$len = $pdb->{_index}[0]{offset} - |
| 498 |
$pdb->{_sort_offset}; |
| 499 |
} else { |
| 500 |
# There are no records. The sort block goes to the end |
| 501 |
# of the file. |
| 502 |
$len = $pdb->{_size} - $pdb->{_sort_offset}; |
| 503 |
} |
| 504 |
|
| 505 |
# Read the AppInfo block |
| 506 |
read $fh, $buf, $len; |
| 507 |
if ($hexdump) |
| 508 |
{ |
| 509 |
&hexdump(" ", $buf); |
| 510 |
print "\n"; |
| 511 |
} |
| 512 |
|
| 513 |
# XXX - Check to see if the sort block has some predefined |
| 514 |
# structure. If so, it might be a good idea to parse the sort |
| 515 |
# block here. |
| 516 |
|
| 517 |
# Tell the real class to parse the sort block |
| 518 |
$pdb->{sort} = $pdb->ParseSortBlock($buf); |
| 519 |
} |
| 520 |
|
| 521 |
# _load_records |
| 522 |
# Private function. Load the actual data records, for a record database |
| 523 |
# (PDB) |
| 524 |
sub _load_records |
| 525 |
{ |
| 526 |
my $pdb = shift; |
| 527 |
my $fh = shift; # Input file handle |
| 528 |
my $i; |
| 529 |
|
| 530 |
print "Records:\n"; |
| 531 |
# Read each record in turn |
| 532 |
for ($i = 0; $i < $pdb->{_numrecs}; $i++) |
| 533 |
{ |
| 534 |
my $len; # Length of record |
| 535 |
my $buf; # Input buffer |
| 536 |
|
| 537 |
print " Record $i\n"; |
| 538 |
|
| 539 |
# Sanity check: make sure we're where we think we |
| 540 |
# should be. |
| 541 |
if (tell($fh) > $pdb->{_index}[$i]{offset}) |
| 542 |
{ |
| 543 |
# XXX - The two NULs are really optional. |
| 544 |
# die "Bad offset for record $i: expected ", |
| 545 |
# sprintf("0x%08x", |
| 546 |
# $pdb->{_index}[$i]{offset}), |
| 547 |
# " but it's at ", |
| 548 |
# sprintf("[0x%08x]", tell($fh)), "\n"; |
| 549 |
} |
| 550 |
|
| 551 |
# Seek to the right place, if necessary |
| 552 |
if (tell($fh) != $pdb->{_index}[$i]{offset}) |
| 553 |
{ |
| 554 |
seek PDB, $pdb->{_index}[$i]{offset}, 0; |
| 555 |
} |
| 556 |
|
| 557 |
# Compute the length of the record: the last record |
| 558 |
# extends to the end of the file. The others extend to |
| 559 |
# the beginning of the next record. |
| 560 |
if ($i == $pdb->{_numrecs} - 1) |
| 561 |
{ |
| 562 |
# This is the last record |
| 563 |
$len = $pdb->{_size} - |
| 564 |
$pdb->{_index}[$i]{offset}; |
| 565 |
} else { |
| 566 |
# This is not the last record |
| 567 |
$len = $pdb->{_index}[$i+1]{offset} - |
| 568 |
$pdb->{_index}[$i]{offset}; |
| 569 |
} |
| 570 |
|
| 571 |
# Read the record |
| 572 |
read $fh, $buf, $len; |
| 573 |
if ($hexdump) |
| 574 |
{ |
| 575 |
&hexdump(" ", $buf); |
| 576 |
print "\n"; |
| 577 |
} |
| 578 |
|
| 579 |
# Tell the real class to parse the record data. Pass |
| 580 |
# &ParseRecord all of the information from the index, |
| 581 |
# plus a "data" field with the raw record data. |
| 582 |
my $record; |
| 583 |
|
| 584 |
$record = $pdb->ParseRecord( |
| 585 |
%{$pdb->{_index}[$i]}, |
| 586 |
"data" => $buf, |
| 587 |
); |
| 588 |
push @{$pdb->{records}}, $record; |
| 589 |
|
| 590 |
# Print out the parsed values |
| 591 |
&dumphash($record, "\t"); |
| 592 |
print "\n"; |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
# _load_resources |
| 597 |
# Private function. Load the actual data resources, for a resource database |
| 598 |
# (PRC) |
| 599 |
sub _load_resources |
| 600 |
{ |
| 601 |
my $pdb = shift; |
| 602 |
my $fh = shift; # Input file handle |
| 603 |
my $i; |
| 604 |
|
| 605 |
print "Resources:\n"; |
| 606 |
# Read each resource in turn |
| 607 |
for ($i = 0; $i < $pdb->{_numrecs}; $i++) |
| 608 |
{ |
| 609 |
my $len; # Length of record |
| 610 |
my $buf; # Input buffer |
| 611 |
|
| 612 |
print " Resource $i\n"; |
| 613 |
|
| 614 |
# Sanity check: make sure we're where we think we |
| 615 |
# should be. |
| 616 |
if (tell($fh) > $pdb->{_index}[$i]{offset}) |
| 617 |
{ |
| 618 |
die "Bad offset for resource $i: expected ", |
| 619 |
sprintf("0x%08x", |
| 620 |
$pdb->{_index}[$i]{offset}), |
| 621 |
" but it's at ", |
| 622 |
sprintf("0x%08x", tell($fh)), "\n"; |
| 623 |
} |
| 624 |
|
| 625 |
# Seek to the right place, if necessary |
| 626 |
if (tell($fh) != $pdb->{_index}[$i]{offset}) |
| 627 |
{ |
| 628 |
seek PDB, $pdb->{_index}[$i]{offset}, 0; |
| 629 |
} |
| 630 |
|
| 631 |
# Compute the length of the resource: the last |
| 632 |
# resource extends to the end of the file. The others |
| 633 |
# extend to the beginning of the next resource. |
| 634 |
if ($i == $pdb->{_numrecs} - 1) |
| 635 |
{ |
| 636 |
# This is the last resource |
| 637 |
$len = $pdb->{_size} - |
| 638 |
$pdb->{_index}[$i]{offset}; |
| 639 |
} else { |
| 640 |
# This is not the last resource |
| 641 |
$len = $pdb->{_index}[$i+1]{offset} - |
| 642 |
$pdb->{_index}[$i]{offset}; |
| 643 |
} |
| 644 |
|
| 645 |
# Read the resource |
| 646 |
read $fh, $buf, $len; |
| 647 |
if ($hexdump) |
| 648 |
{ |
| 649 |
&hexdump(" ", $buf); |
| 650 |
print "\n"; |
| 651 |
} |
| 652 |
|
| 653 |
# Tell the real class to parse the resource data. Pass |
| 654 |
# &ParseResource all of the information from the |
| 655 |
# index, plus a "data" field with the raw resource |
| 656 |
# data. |
| 657 |
my $resource; |
| 658 |
|
| 659 |
$resource = $pdb->ParseResource( |
| 660 |
%{$pdb->{_index}[$i]}, |
| 661 |
"data" => $buf, |
| 662 |
); |
| 663 |
push @{$pdb->{resources}}, $resource; |
| 664 |
|
| 665 |
# Print out the parsed values |
| 666 |
&dumphash($resource, "\t"); |
| 667 |
print "\n"; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
sub hexdump |
| 672 |
{ |
| 673 |
my $prefix = shift; # What to print in front of each line |
| 674 |
my $data = shift; # The data to dump |
| 675 |
my $maxlines = shift; # Max # of lines to dump |
| 676 |
my $offset; # Offset of current chunk |
| 677 |
|
| 678 |
for ($offset = 0; $offset < length($data); $offset += 16) |
| 679 |
{ |
| 680 |
my $hex; # Hex values of the data |
| 681 |
my $ascii; # ASCII values of the data |
| 682 |
my $chunk; # Current chunk of data |
| 683 |
|
| 684 |
last if defined($maxlines) && ($offset >= ($maxlines * 16)); |
| 685 |
|
| 686 |
$chunk = substr($data, $offset, 16); |
| 687 |
|
| 688 |
($hex = $chunk) =~ s/./sprintf "%02x ", ord($&)/ges; |
| 689 |
|
| 690 |
($ascii = $chunk) =~ y/\040-\176/./c; |
| 691 |
|
| 692 |
printf "%s %-48s|%-16s|\n", $prefix, $hex, $ascii; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
# XXX - Ought to have a &dumparray as well. The two can call each other |
| 697 |
# recursively. |
| 698 |
|
| 699 |
sub dumphash |
| 700 |
{ |
| 701 |
my $hash = shift; |
| 702 |
my $indent = shift; |
| 703 |
my $key; |
| 704 |
my $value; |
| 705 |
|
| 706 |
while (($key, $value) = each %{$hash}) |
| 707 |
{ |
| 708 |
if (ref($value) eq "HASH") |
| 709 |
{ |
| 710 |
print $indent, $key, ":\n"; |
| 711 |
&dumphash($value, $indent . "\t"); |
| 712 |
} elsif (ref($value) eq "ARRAY") |
| 713 |
{ |
| 714 |
my($i,$j); |
| 715 |
|
| 716 |
print $indent, $key, ":\n"; |
| 717 |
for ($i = 0; $i <= $#{$value}; $i++) |
| 718 |
{ |
| 719 |
if (ref($value->[$i]) eq "HASH") |
| 720 |
{ |
| 721 |
print $indent, " $i:\n"; |
| 722 |
&dumphash($value->[$i], |
| 723 |
$indent . "\t"); |
| 724 |
} elsif (ref($value->[$i]) eq "ARRAY") |
| 725 |
{ |
| 726 |
my @v2 = @{$value->[$i]}; |
| 727 |
for ($j = 0; $j <= $#v2; $j++) |
| 728 |
{ |
| 729 |
print $indent, |
| 730 |
"\t$i-$j: [$v2[$j]]\n"; |
| 731 |
} |
| 732 |
}else { |
| 733 |
print $indent, |
| 734 |
"\t$i: [$value->[$i]]\n"; |
| 735 |
} |
| 736 |
} |
| 737 |
} else { |
| 738 |
print $indent, $key, " -> [", $value, "]\n"; |
| 739 |
} |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
__END__ |
| 744 |
|
| 745 |
=head1 NAME |
| 746 |
|
| 747 |
pdbdump - Print the contents of a Palm PDB file |
| 748 |
|
| 749 |
=head1 VERSION |
| 750 |
|
| 751 |
This document describes version 1.400 of |
| 752 |
main, released March 7, 2015 |
| 753 |
as part of Palm-PDB version 1.400. |
| 754 |
|
| 755 |
=head1 SYNOPSIS |
| 756 |
|
| 757 |
C<pdbdump> I<[options]> F<filename> |
| 758 |
|
| 759 |
=head1 DESCRIPTION |
| 760 |
|
| 761 |
C<pdbdump> reads a PalmOS F<.pdb> file, parses it, and prints its |
| 762 |
contents. This includes both a hex dump of the raw data of each piece, |
| 763 |
and a human-readable list of the various values, insofar as possible. |
| 764 |
The aim of C<pdbdump> is to allow one to verify whether a particular |
| 765 |
file is a well-formed PalmOS database file and if not, where the error |
| 766 |
lies. |
| 767 |
|
| 768 |
If the database is of a known type, C<pdbdump> parses the AppInfo |
| 769 |
block and records. Otherwise, it simply prints out a hex dump of their |
| 770 |
contents. C<pdbdump> includes, by default, support for most of the |
| 771 |
built-in applications. Other helper modules may be loaded with the |
| 772 |
C<-M> option. |
| 773 |
|
| 774 |
=head1 OPTIONS |
| 775 |
|
| 776 |
=over 4 |
| 777 |
|
| 778 |
=item -h -help --help |
| 779 |
|
| 780 |
Print a usage message and exit. |
| 781 |
|
| 782 |
=item -nohex |
| 783 |
|
| 784 |
Don't print the hex dump of the various parts. |
| 785 |
|
| 786 |
=item -MI<module> |
| 787 |
|
| 788 |
C<use> the named module. This can be useful for loading additional |
| 789 |
helper modules. |
| 790 |
|
| 791 |
=back |
| 792 |
|
| 793 |
=head1 SEE ALSO |
| 794 |
|
| 795 |
L<Palm::PDB> |
| 796 |
|
| 797 |
=head1 CONFIGURATION AND ENVIRONMENT |
| 798 |
|
| 799 |
main requires no configuration files or environment variables. |
| 800 |
|
| 801 |
=head1 INCOMPATIBILITIES |
| 802 |
|
| 803 |
None reported. |
| 804 |
|
| 805 |
=head1 BUGS AND LIMITATIONS |
| 806 |
|
| 807 |
C<pdbdump> only recognizes record databases (C<.pdb> files), not |
| 808 |
resource databases (C<.prc> files). |
| 809 |
|
| 810 |
=head1 AUTHORS |
| 811 |
|
| 812 |
Andrew Arensburger C<< <arensb AT ooblick.com> >> |
| 813 |
|
| 814 |
Currently maintained by Christopher J. Madsen C<< <perl AT cjmweb.net> >> |
| 815 |
|
| 816 |
Please report any bugs or feature requests |
| 817 |
to S<C<< <bug-Palm-PDB AT rt.cpan.org> >>> |
| 818 |
or through the web interface at |
| 819 |
L<< http://rt.cpan.org/Public/Bug/Report.html?Queue=Palm-PDB >>. |
| 820 |
|
| 821 |
You can follow or contribute to Palm-PDB's development at |
| 822 |
L<< https://github.com/madsen/Palm-PDB >>. |
| 823 |
|
| 824 |
=head1 COPYRIGHT AND LICENSE |
| 825 |
|
| 826 |
This software is copyright (c) 2000 by Andrew Arensburger. |
| 827 |
|
| 828 |
This is free software; you can redistribute it and/or modify it under |
| 829 |
the same terms as the Perl 5 programming language system itself. |
| 830 |
|
| 831 |
=head1 DISCLAIMER OF WARRANTY |
| 832 |
|
| 833 |
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
| 834 |
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
| 835 |
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
| 836 |
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
| 837 |
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 838 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
| 839 |
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
| 840 |
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
| 841 |
NECESSARY SERVICING, REPAIR, OR CORRECTION. |
| 842 |
|
| 843 |
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
| 844 |
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
| 845 |
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE |
| 846 |
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
| 847 |
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
| 848 |
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
| 849 |
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
| 850 |
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
| 851 |
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
| 852 |
SUCH DAMAGES. |
| 853 |
|
| 854 |
=cut |