PDB.pm

filter: Statementstmt 60.5% 282 / 466 Branchbran 41.2% 109 / 264 Conditioncond 30.2% 23 / 76 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 54.1% 13 / 24 Podpod 100% 13 / 13 totaltotal 49.6% 440 / 887 SCAR 81.7
CC
171
Cov
51%
Write
270.5
Load
92.3
is_Dirty
90.0
CRAP 3534.9
↑ Summary Raw.pm »
1 package Palm::PDB;
2 #
3 # Perl module for reading and writing Palm databases (both PDB and PRC).
4 #
5 # Copyright (C) 1999, 2000, Andrew Arensburger.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the same terms as Perl itself.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
13 # GNU General Public License or the Artistic License for more details.
14 #
15 # A Palm database file (either .pdb or .prc) has the following overall
16 # structure:
17 # Header
18 # Index header
19 # Record/resource index
20 # Two NUL(?) bytes
21 # Optional AppInfo block
22 # Optional sort block
23 # Records/resources
24 # See http://www.palmos.com/dev/tech/docs/fileformats.zip
25 # for details.
26  
27 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 our $VERSION = '1.016';
31 # This file is part of Palm-PDB 1.016 (January 24, 2015)
32  
33 # ABSTRACT: Parse Palm database files
34  
35  
36 use constant 1.03 { # accepts hash reference
37 3 dmRecordIDReservedRange => 1, # The range of upper bits in the database's
38 # uniqueIDSeed from 0 to this number are
39 # reserved and not randomly picked when a
40 #database is created.
41  
42 EPOCH_1904 => 2082844800, # Difference between Palm's
43 # epoch (Jan. 1, 1904) and
44 # Unix's epoch (Jan. 1, 1970),
45 # in seconds.
46 HeaderLen => 32+2+2+(9*4), # Size of database header
47 RecIndexHeaderLen => 6, # Size of record index header
48 IndexRecLen => 8, # Length of record index entry
49 IndexRsrcLen => 10, # Length of resource index entry
50 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 3 our %PDBHandlers = (); # Record handler map
53 3 our %PRCHandlers = (); # Resource handler map
54  
55  
56 sub new
57 {
58 5 ▶ my $class = shift;
Subroutine new: called CC=13 SCAR=28.2
Pod documented
59 5 my $params = shift;
60  
61 5 my $self = {};
62  
63  
64 # Initialize the PDB. These values are just defaults, of course.
65 5 ▶ $self->{'name'} = $params->{'name'} || "";
Condition50%1/2
$params->{'name'} || ''
l!l
05
MC/DC0%0/1
$params->{'name'} || ''
$params->{'name'}
Truth table50%1/2
A
A= $params->{'name'}
Aresult
11
00
66 5 ▶ $self->{'attributes'} = $params->{'attributes'} || {};
Condition50%1/2
$params->{'attributes'} || {}
l!l
05
MC/DC0%0/1
$params->{'attributes'} || {}
$params->{'attributes'}
Truth table50%1/2
A
A= $params->{'attributes'}
Aresult
11
00
67 5 ▶ $self->{'version'} = $params->{'version'} || 0;
Condition50%1/2
$params->{'version'} || 0
l!l
05
MC/DC0%0/1
$params->{'version'} || 0
$params->{'version'}
Truth table50%1/2
A
A= $params->{'version'}
Aresult
11
00
68  
69 5 my $now = time;
70  
71 5 ▶ $self->{'ctime'} = $params->{'ctime'} || $now;
Condition33%1/3
$params->{'ctime'} || $now
l!l&&r!l&&!r
050
MC/DC0%0/2
$params->{'ctime'} || $now
$params->{'ctime'} $now
Truth table33%1/3
A || B
A= $params->{'ctime'}B= $now
ABresult
1X1
011
000
72 5 ▶ $self->{'mtime'} = $params->{'mtime'} || $now;
Condition33%1/3
$params->{'mtime'} || $now
l!l&&r!l&&!r
050
MC/DC0%0/2
$params->{'mtime'} || $now
$params->{'mtime'} $now
Truth table33%1/3
A || B
A= $params->{'mtime'}B= $now
ABresult
1X1
011
000
73 5 ▶ $self->{'baktime'} = $params->{'baktime'} || -(EPOCH_1904);
Condition50%1/2
$params->{'baktime'} || -2082844800
l!l
05
MC/DC0%0/1
$params->{'baktime'} || -2082844800
$params->{'baktime'}
Truth table50%1/2
A
A= $params->{'baktime'}
Aresult
11
00
74  
75 5 ▶ $self->{'modnum'} = $params->{'modnum'} || 0;
Condition50%1/2
$params->{'modnum'} || 0
l!l
05
MC/DC0%0/1
$params->{'modnum'} || 0
$params->{'modnum'}
Truth table50%1/2
A
A= $params->{'modnum'}
Aresult
11
00
76 5 ▶ $self->{'type'} = $params->{'type'} || "\0\0\0\0";
Condition50%1/2
$params->{'type'} || "\000\000\000\000"
l!l
05
MC/DC0%0/1
$params->{'type'} || "\000\000\000\000"
$params->{'type'}
Truth table50%1/2
A
A= $params->{'type'}
Aresult
11
00
77 5 ▶ $self->{'creator'} = $params->{'creator'} || "\0\0\0\0";
Condition50%1/2
$params->{'creator'} || "\000\000\000\000"
l!l
05
MC/DC0%0/1
$params->{'creator'} || "\000\000\000\000"
$params->{'creator'}
Truth table50%1/2
A
A= $params->{'creator'}
Aresult
11
00
78 5 ▶ $self->{'uniqueIDseed'} = $params->{'uniqueIDseed'} || 0;
Condition50%1/2
$params->{'uniqueIDseed'} || 0
l!l
05
MC/DC0%0/1
$params->{'uniqueIDseed'} || 0
$params->{'uniqueIDseed'}
Truth table50%1/2
A
A= $params->{'uniqueIDseed'}
Aresult
11
00
79  
80 5 $self->{"2NULs"} = "\0\0";
81  
82 # This will be set when any elements of the object are changed
83 5 $self->{'dirty'} = 0;
84  
85  
86 # Calculate a proper uniqueIDseed if the user has not provided
87 # a correct one.
88 5 ▶ if ($self->{'uniqueIDseed'} <= ((dmRecordIDReservedRange + 1) << 12))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"uniqueIDseed"} <= 8192) 5 0 5
89 {
90 5 my $uniqueIDseed = 0;
91  
92 do
93 5 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$uniqueIDseed <= 1 and do { do { $uniqueIDseed = int rand 4095 } } 0 5 5
94 5 $uniqueIDseed = int(rand(0x0FFF));
95  
96 } while ($uniqueIDseed <= dmRecordIDReservedRange);
97  
98 5 $self->{'uniqueIDseed'} = $uniqueIDseed << 12;
99 5 $self->{'uniqueIDseed'} &= 0x00FFF000; # Isolate the upper 12 seed bits.
100 }
101  
102 5 bless $self, $class;
103 5 return $self;
104 }
105  
106 #' <-- For Emacs.
107  
108 sub RegisterPDBHandlers
109 {
110 3 ▶ my $handler = shift; # Name of class that'll handle
Subroutine RegisterPDBHandlers: called CC=3 SCAR=11.3
Pod documented
111 # these databases
112 3 my @types = @_;
113 3 my $item;
114  
115 3 foreach $item (@types)
116 {
117 3 ▶ if (ref($item) eq "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 3 0 3
118 {
119 3 $PDBHandlers{$item->[0]}{$item->[1]} = $handler;
120 } else {
121 0 $PDBHandlers{$item}{""} = $handler;
122 }
123 }
124 }
125  
126  
127 sub RegisterPRCHandlers
128 {
129 0 ▶ my $handler = shift; # Name of class that'll handle
Subroutine RegisterPRCHandlers: not called CC=3 SCAR=24.8
Pod documented
130 # these databases
131 0 my @types = @_;
132 0 my $item;
133  
134 0 foreach $item (@types)
135 {
136 0 ▶ if (ref($item) eq "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 0 0 0
137 {
138 0 $PRCHandlers{$item->[0]}{$item->[1]} = $handler;
139 } else {
140 0 $PRCHandlers{$item}{""} = $handler;
141 }
142 }
143 }
144  
145 #'
146  
147 # _open
148 sub _open
149 {
150 5 ▶ my($self, $mode, $fname) = @_;
Subroutine _open: called CC=8 SCAR=27.2
151  
152 5 my $handle;
153  
154 5 ▶ if (ref($fname))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fname) { } 1 4 5
155 {
156 # Already a filehandle
157 1 ▶ if (ref($fname) eq 'GLOB'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fname eq 'GLOB' or UNIVERSAL::isa($fname, 'IO::Seekable')) { } 1 0 1
Condition33%1/3
ref $fname eq "GLOB" or UNIVERSAL::isa($fname, "IO::Seekable")
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $fname eq "GLOB" or UNIVERSAL::isa($fname, "IO::Seekable")
ref $fname eq "GLOB" UNIVERSAL::isa($fname, "IO::Seekable")
Truth table33%1/3
A or B
A= ref $fname eq "GLOB"B= UNIVERSAL::isa($fname, "IO::Seekable")
ABresult
1X1
011
000
158 or UNIVERSAL::isa($fname,"IO::Seekable"))
159 {
160 1 $handle = $fname;
161 }
162 # Probably a reference to a SCALAR
163 else
164 {
165 0 ▶ unless (eval 'open $handle, $mode, $fname')
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "open \$handle, \$mode, \$fname") 0 0 0
166 {
167 0 ▶ if ($@ ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($@ ne '') { } 0 0 0
168 {
169 0 die "Open of \"$fname\" unsupported: $@\n";
170 }
171 else
172 {
173 0 die "Can't open \"$fname\": $!\n";
174 }
175 }
176 }
177 }
178 else
179 {
180 # Before 5.6.0 "autovivified file handles" don't exist
181 4 ▶ eval 'use IO::File; $handle = new IO::File' if $] < 5.006;
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.006" 0 4 4
182 4 ▶ open $handle, "$mode $fname"
Branch: 1 branch
BranchTrueFalseTotal
open $handle, "$mode $fname" or die qq[Can't open "$fname": $!\n] 0 4 4
183 or die "Can't open \"$fname\": $!\n";
184 }
185  
186 5 return $handle;
187 }
188  
189 # Load
190 sub Load
191 {
192 4 ▶ my $self = shift;
Subroutine Load: called CC=41 SCAR=45.3
Pod documented
193 4 my $fname = shift; # Filename to read from
194 4 my $buf; # Buffer into which to read stuff
195  
196 4 my $handle = $self->_open('<', $fname);
197 4 ▶ return undef unless defined $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle 0 4 4
198  
199 4 binmode $handle; # Read as binary file under MS-DOS
200  
201 # Get the size of the file. It'll be useful later
202 4 seek $handle, 0, 2; # 2 == SEEK_END. Seek to the end.
203 4 $self->{_size} = tell $handle;
204 4 seek $handle, 0, 0; # 0 == SEEK_START. Rewind to the beginning.
205  
206 # Read header
207 4 my $name;
208 my $attributes;
209 4 my $version;
210 4 my $ctime;
211 4 my $mtime;
212 4 my $baktime;
213 4 my $modnum;
214 4 my $appinfo_offset;
215 4 my $sort_offset;
216 4 my $type;
217 4 my $creator;
218 4 my $uniqueIDseed;
219  
220 4 read $handle, $buf, HeaderLen; # Read the PDB header
221  
222 # Split header into its component fields
223 4 ($name, $attributes, $version, $ctime, $mtime, $baktime,
224 $modnum, $appinfo_offset, $sort_offset, $type, $creator,
225 $uniqueIDseed) =
226 unpack "a32 n n N N N N N N a4 a4 N", $buf;
227  
228 # database names must include a terminating NUL.
229 4 ▶ die "bogus database name! is this really a PalmOS file?" unless $name =~ /.+\0/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /.+\0/ 2 2 4
230  
231 2 ($self->{name} = $name) =~ s/\0.*$//;
232 2 ▶ $self->{attributes}{resource} = 1 if $attributes & 0x0001;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 1 0 2 2
233 2 ▶ $self->{attributes}{"read-only"} = 1 if $attributes & 0x0002;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 2 0 2 2
234 2 ▶ $self->{attributes}{"AppInfo dirty"} = 1 if $attributes & 0x0004;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 4 0 2 2
235 2 ▶ $self->{attributes}{backup} = 1 if $attributes & 0x0008;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 8 1 1 2
236 2 ▶ $self->{attributes}{"OK newer"} = 1 if $attributes & 0x0010;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 16 0 2 2
237 2 ▶ $self->{attributes}{reset} = 1 if $attributes & 0x0020;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 32 0 2 2
238 2 ▶ $self->{attributes}{open} = 1 if $attributes & 0x8000;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 32768 0 2 2
239 2 ▶ $self->{attributes}{launchable} = 1 if $attributes & 0x0200;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 512 0 2 2
240  
241 # Attribute names as of PalmOS 5.0 ( see /Core/System/DataMgr.h )
242  
243 2 ▶ $self->{'attributes'}{'ResDB'} = 1 if $attributes & 0x0001;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 1 0 2 2
244 2 ▶ $self->{'attributes'}{'ReadOnly'} = 1 if $attributes & 0x0002;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 2 0 2 2
245 2 ▶ $self->{'attributes'}{'AppInfoDirty'} = 1 if $attributes & 0x0004;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 4 0 2 2
246 2 ▶ $self->{'attributes'}{'Backup'} = 1 if $attributes & 0x0008;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 8 1 1 2
247 2 ▶ $self->{'attributes'}{'OKToInstallNewer'} = 1 if $attributes & 0x0010;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 16 0 2 2
248 2 ▶ $self->{'attributes'}{'ResetAfterInstall'} = 1 if $attributes & 0x0020;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 32 0 2 2
249 2 ▶ $self->{'attributes'}{'CopyPrevention'} = 1 if $attributes & 0x0040;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 64 0 2 2
250 2 ▶ $self->{'attributes'}{'Stream'} = 1 if $attributes & 0x0080;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 128 0 2 2
251 2 ▶ $self->{'attributes'}{'Hidden'} = 1 if $attributes & 0x0100;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 256 0 2 2
252 2 ▶ $self->{'attributes'}{'LaunchableData'} = 1 if $attributes & 0x0200;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 512 0 2 2
253 2 ▶ $self->{'attributes'}{'Recyclable'} = 1 if $attributes & 0x0400;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 1024 0 2 2
254 2 ▶ $self->{'attributes'}{'Bundle'} = 1 if $attributes & 0x0800;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 2048 0 2 2
255 2 ▶ $self->{'attributes'}{'Open'} = 1 if $attributes & 0x8000;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 32768 0 2 2
256  
257  
258 2 $self->{version} = $version;
259 2 $self->{ctime} = $ctime - EPOCH_1904;
260 2 $self->{mtime} = $mtime - EPOCH_1904;
261 2 $self->{baktime} = $baktime - EPOCH_1904;
262 2 $self->{modnum} = $modnum;
263 # _appinfo_offset and _sort_offset are private fields
264 2 $self->{_appinfo_offset} = $appinfo_offset;
265 2 $self->{_sort_offset} = $sort_offset;
266 2 $self->{type} = $type;
267 2 $self->{creator} = $creator;
268 2 $self->{uniqueIDseed} = $uniqueIDseed;
269  
270 # XXX strictly speaking, ctime/mtime/baktime values before 1990 are quite
271 # unlikely. Palm was founded in 1992, so even allowing for some prototypes.
272 # This is another way one could detect bogus databases.
273  
274 2 ▶ if( $self->{_appinfo_offset} > $self->{_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_appinfo_offset"} > $self->{"_size"}) 1 1 2
275 1 die "AppInfo block offset beyond end of file!";
276 }
277 1 ▶ if( $self->{_sort_offset} > $self->{_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_sort_offset"} > $self->{"_size"}) 0 1 1
278 0 die "Sort block offset beyond end of file!";
279 }
280  
281 # Rebless this PDB object, depending on its type and/or
282 # creator. This allows us to magically invoke the proper
283 # &Parse*() function on the various parts of the database.
284  
285 # Look for most specific handlers first, least specific ones
286 # last. That is, first look for a handler that deals
287 # specifically with this database's creator and type, then for
288 # one that deals with this database's creator and any type,
289 # and finally for one that deals with anything.
290  
291 1 my $handler;
292 1 ▶ if ($self->{attributes}{resource} || $self->{'attributes'}{'ResDB'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attributes'}{'resource'} or $self->{'attributes'}{'ResDB'}) { } 0 1 1
Condition33%1/3
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
$self->{"attributes"}{"resource"} $self->{"attributes"}{"ResDB"}
Truth table33%1/3
A or B
A= $self->{"attributes"}{"resource"}B= $self->{"attributes"}{"ResDB"}
ABresult
1X1
011
000
293 {
294 # Look among resource handlers
295 $handler = $PRCHandlers{$self->{creator}}{$self->{type}} ||
296 $PRCHandlers{undef}{$self->{type}} ||
297 $PRCHandlers{$self->{creator}}{""} ||
298 0 ▶ $PRCHandlers{""}{""};
Condition0%0/9
$PRCHandlers{$self->{'creator'}}{$self->{'type'}} || $PRCHandlers{'undef'}{$self->{'type'}} || $PRCHandlers{$self->{'creator'}}{''} || $PRCHandlers{''}{''}
l!l&&r!l&&!r
000
$PRCHandlers{$self->{"creator"}}{$self->{"type"}} or $PRCHandlers{"undef"}{$self->{"type"}} or $PRCHandlers{$self->{"creator"}}{""}
l!l&&r!l&&!r
000
$PRCHandlers{$self->{"creator"}}{$self->{"type"}} or $PRCHandlers{"undef"}{$self->{"type"}}
l!l&&r!l&&!r
000
MC/DC0%0/4
$PRCHandlers{$self->{'creator'}}{$self->{'type'}} || $PRCHandlers{'undef'}{$self->{'type'}} || $PRCHandlers{$self->{'creator'}}{''} || $PRCHandlers{''}{''}
$PRCHandlers{$self->{"creator"}}{$self->{"type"}} $PRCHandlers{"undef"}{$self->{"type"}} $PRCHandlers{$self->{"creator"}}{""} $PRCHandlers{''}{''}
Truth table0%0/5
A or B or C || D
A= $PRCHandlers{$self->{"creator"}}{$self->{"type"}}B= $PRCHandlers{"undef"}{$self->{"type"}}C= $PRCHandlers{$self->{"creator"}}{""}D= $PRCHandlers{''}{''}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
299 } else {
300 # Look among record handlers
301 $handler = $PDBHandlers{$self->{creator}}{$self->{type}} ||
302 $PDBHandlers{""}{$self->{type}} ||
303 $PDBHandlers{$self->{creator}}{""} ||
304 1 ▶ $PDBHandlers{""}{""};
Condition33%3/9
$PDBHandlers{$self->{'creator'}}{$self->{'type'}} || $PDBHandlers{''}{$self->{'type'}} || $PDBHandlers{$self->{'creator'}}{''} || $PDBHandlers{''}{''}
l!l&&r!l&&!r
010
$PDBHandlers{$self->{"creator"}}{$self->{"type"}} or $PDBHandlers{""}{$self->{"type"}} or $PDBHandlers{$self->{"creator"}}{""}
l!l&&r!l&&!r
001
$PDBHandlers{$self->{"creator"}}{$self->{"type"}} or $PDBHandlers{""}{$self->{"type"}}
l!l&&r!l&&!r
001
MC/DC0%0/4
$PDBHandlers{$self->{'creator'}}{$self->{'type'}} || $PDBHandlers{''}{$self->{'type'}} || $PDBHandlers{$self->{'creator'}}{''} || $PDBHandlers{''}{''}
$PDBHandlers{$self->{"creator"}}{$self->{"type"}} $PDBHandlers{""}{$self->{"type"}} $PDBHandlers{$self->{"creator"}}{""} $PDBHandlers{''}{''}
Truth table20%1/5
A or B or C || D
A= $PDBHandlers{$self->{"creator"}}{$self->{"type"}}B= $PDBHandlers{""}{$self->{"type"}}C= $PDBHandlers{$self->{"creator"}}{""}D= $PDBHandlers{''}{''}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
305 }
306  
307 1 ▶ if (defined($handler))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handler) { } 1 0 1
308 {
309 1 bless $self, $handler;
310 } else {
311 # XXX - This should probably return 'undef' or something,
312 # rather than die.
313 0 die "No handler defined for creator \"$creator\", type \"$type\"\n";
314 }
315  
316 ## Read record/resource index
317 # Read index header
318 1 read $handle, $buf, RecIndexHeaderLen;
319  
320 1 my $next_index;
321 my $numrecs;
322  
323 1 ($next_index, $numrecs) = unpack "N n", $buf;
324 1 $self->{_numrecs} = $numrecs;
325  
326 # Read the index itself
327 1 ▶ if ($self->{attributes}{resource} || $self->{'attributes'}{'ResDB'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attributes'}{'resource'} or $self->{'attributes'}{'ResDB'}) { } 0 1 1
Condition33%1/3
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
$self->{"attributes"}{"resource"} $self->{"attributes"}{"ResDB"}
Truth table33%1/3
A or B
A= $self->{"attributes"}{"resource"}B= $self->{"attributes"}{"ResDB"}
ABresult
1X1
011
000
328 {
329 0 &_load_rsrc_index($self, $handle);
330 } else {
331 1 &_load_rec_index($self, $handle);
332 }
333  
334 # Read the two NUL bytes
335 # XXX - Actually, these are bogus. They don't appear in the
336 # spec. The Right Thing to do is to ignore them, and use the
337 # specified or calculated offsets, if they're sane. Sane ==
338 # appears later than the current position.
339 # read $handle, $buf, 2;
340 # $self->{"2NULs"} = $buf;
341  
342 # Read AppInfo block, if it exists
343 1 ▶ if ($self->{_appinfo_offset} != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_appinfo_offset"} != 0) 0 1 1
344 {
345 0 &_load_appinfo_block($self, $handle);
346 }
347  
348 # Read sort block, if it exists
349 1 ▶ if ($self->{_sort_offset} != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_sort_offset"} != 0) 0 1 1
350 {
351 0 &_load_sort_block($self, $handle);
352 }
353  
354 # Read record/resource list
355 1 ▶ if ($self->{attributes}{resource} || $self->{'attributes'}{'ResDB'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attributes'}{'resource'} or $self->{'attributes'}{'ResDB'}) { } 0 1 1
Condition33%1/3
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
$self->{"attributes"}{"resource"} $self->{"attributes"}{"ResDB"}
Truth table33%1/3
A or B
A= $self->{"attributes"}{"resource"}B= $self->{"attributes"}{"ResDB"}
ABresult
1X1
011
000
356 {
357 0 &_load_resources($self, $handle);
358 } else {
359 1 &_load_records($self, $handle);
360 }
361  
362 # These keys were needed for parsing the file, but are not
363 # needed any longer. Delete them.
364 1 delete $self->{_index};
365 1 delete $self->{_numrecs};
366 1 delete $self->{_appinfo_offset};
367 1 delete $self->{_sort_offset};
368 1 delete $self->{_size};
369  
370 1 $self->{'dirty'} = 0;
371  
372 1 return $self;
373 }
374  
375 # _load_rec_index
376 # Private function. Read the record index, for a record database
377 sub _load_rec_index
378 {
379 1 ▶ my $pdb = shift;
Subroutine _load_rec_index: called CC=13 SCAR=27.7
380 1 my $fh = shift; # Input file handle
381 1 my $i;
382 1 my $lastoffset = 0;
383  
384 # Read each record index entry in turn
385 1 ▶ for ($i = 0; $i < $pdb->{_numrecs}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pdb->{"_numrecs"}) 3 1 4
386 {
387 3 my $buf; # Input buffer
388  
389 # Read the next record index entry
390 my $offset;
391 3 my $attributes;
392 3 my @id; # Raw ID
393 3 my $id; # Numerical ID
394 3 my $entry = {}; # Parsed index entry
395  
396 3 read $fh, $buf, IndexRecLen;
397  
398 # The ID field is a bit weird: it's represented as 3
399 # bytes, but it's really a double word (long) value.
400  
401 3 ($offset, $attributes, @id) = unpack "N C C3", $buf;
402  
403 3 ▶ if ($offset == $lastoffset)
Branch: 1 branch
BranchTrueFalseTotal
if ($offset == $lastoffset) 0 3 3
404 {
405 0 print STDERR "Record $i has same offset as previous one: $offset\n";
406 }
407  
408 3 $lastoffset = $offset;
409  
410 3 $entry->{offset} = $offset;
411  
412 3 ▶ $entry->{attributes}{expunged} = 1 if $attributes & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 128 0 3 3
413 3 ▶ $entry->{attributes}{dirty} = 1 if $attributes & 0x40;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 64 3 0 3
414 3 ▶ $entry->{attributes}{deleted} = 1 if $attributes & 0x20;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 32 0 3 3
415 3 ▶ $entry->{attributes}{private} = 1 if $attributes & 0x10;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 16 0 3 3
416  
417 # Attribute names as of PalmOS 5.0 ( see /Core/System/DataMgr.h )
418  
419 3 ▶ $entry->{'attributes'}{'Delete'} = 1 if $attributes & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 128 0 3 3
420 3 ▶ $entry->{'attributes'}{'Dirty'} = 1 if $attributes & 0x40;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 64 3 0 3
421 3 ▶ $entry->{'attributes'}{'Busy'} = 1 if $attributes & 0x20;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 32 0 3 3
422 3 ▶ $entry->{'attributes'}{'Secret'} = 1 if $attributes & 0x10;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 16 0 3 3
423  
424 3 $entry->{id} = ($id[0] << 16) |
425 ($id[1] << 8) |
426 $id[2];
427  
428 # The lower 4 bits of the attributes field are
429 # overloaded: If the record has been deleted and/or
430 # expunged, then bit 0x08 indicates whether the record
431 # should be archived. Otherwise (if it's an ordinary,
432 # non-deleted record), the lower 4 bits specify the
433 # category that the record belongs in.
434 3 ▶ if (($attributes & 0xa0) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (($attributes & 160) == 0) { } 3 0 3
435 {
436 3 $entry->{category} = $attributes & 0x0f;
437 } else {
438 0 ▶ $entry->{attributes}{archive} = 1
Branch: 1 branch
BranchTrueFalseTotal
if $attributes & 8 0 0 0
439 if $attributes & 0x08;
440 }
441  
442 # Put this information on a temporary array
443 3 push @{$pdb->{_index}}, $entry;
444 }
445 }
446  
447 # _load_rsrc_index
448 # Private function. Read the resource index, for a resource database
449 sub _load_rsrc_index
450 {
451 0 ▶ my $pdb = shift;
Subroutine _load_rsrc_index: not called CC=2 SCAR=17.9
452 0 my $fh = shift; # Input file handle
453 0 my $i;
454  
455 # Read each resource index entry in turn
456 0 ▶ for ($i = 0; $i < $pdb->{_numrecs}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pdb->{"_numrecs"}) 0 0 0
457 {
458 0 my $buf; # Input buffer
459  
460 # Read the next resource index entry
461 my $type;
462 0 my $id;
463 0 my $offset;
464 0 my $entry = {}; # Parsed index entry
465  
466 0 read $fh, $buf, IndexRsrcLen;
467  
468 0 ($type, $id, $offset) = unpack "a4 n N", $buf;
469  
470 0 $entry->{type} = $type;
471 0 $entry->{id} = $id;
472 0 $entry->{offset} = $offset;
473  
474 0 push @{$pdb->{_index}}, $entry;
475 }
476 }
477  
478 # _load_appinfo_block
479 # Private function. Read the AppInfo block
480 sub _load_appinfo_block
481 {
482 0 ▶ my $pdb = shift;
Subroutine _load_appinfo_block: not called CC=6 SCAR=37.4
483 0 my $fh = shift; # Input file handle
484 0 my $len; # Length of AppInfo block
485 my $buf; # Input buffer
486  
487 # Sanity check: make sure we're positioned at the beginning of
488 # the AppInfo block
489 0 ▶ if (tell($fh) > $pdb->{_appinfo_offset})
Branch: 1 branch
BranchTrueFalseTotal
if (tell $fh > $pdb->{"_appinfo_offset"}) 0 0 0
490 {
491 die "Bad AppInfo offset: expected ",
492 0 sprintf("0x%08x", $pdb->{_appinfo_offset}),
493 ", but I'm at ",
494 tell($fh), "\n";
495 }
496  
497 # Seek to the right place, if necessary
498 0 ▶ if (tell($fh) != $pdb->{_appinfo_offset})
Branch: 1 branch
BranchTrueFalseTotal
if (tell $fh != $pdb->{"_appinfo_offset"}) 0 0 0
499 {
500 0 seek $fh, $pdb->{_appinfo_offset}, 0;
501 }
502  
503 # There's nothing that explicitly gives the size of the
504 # AppInfo block. Rather, it has to be inferred from the offset
505 # of the AppInfo block (previously recorded in
506 # $pdb->{_appinfo_offset}) and whatever's next in the file.
507 # That's either the sort block, the first data record, or the
508 # end of the file.
509  
510 0 ▶ if ($pdb->{_sort_offset})
Branch: 2 branches
BranchTrueFalseTotal
if ($pdb->{'_sort_offset'}) { } 0 0 0
elsif (defined $pdb->{'_index'} and @{$pdb->{'_index'};}) { } 0 0 0
511 {
512 # The next thing in the file is the sort block
513 0 $len = $pdb->{_sort_offset} - $pdb->{_appinfo_offset};
514 0 ▶ } elsif ((defined $pdb->{_index}) && @{$pdb->{_index}})
Condition0%0/3
defined $pdb->{"_index"} and @{$pdb->{"_index"};}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pdb->{"_index"} and @{$pdb->{"_index"};}
defined $pdb->{"_index"} @{$pdb->{"_index"};}
Truth table0%0/3
A and B
A= defined $pdb->{"_index"}B= @{$pdb->{"_index"};}
ABresult
0X0
100
111
515 {
516 # There's no sort block; the next thing in the file is
517 # the first data record
518 $len = $pdb->{_index}[0]{offset} -
519 0 $pdb->{_appinfo_offset};
520 } else {
521 # There's no sort block and there are no records. The
522 # AppInfo block goes to the end of the file.
523 0 $len = $pdb->{_size} - $pdb->{_appinfo_offset};
524 }
525  
526 # Read the AppInfo block
527 0 read $fh, $buf, $len;
528  
529 # Tell the real class to parse the AppInfo block
530 0 $pdb->{appinfo} = $pdb->ParseAppInfoBlock($buf);
531 }
532  
533 # _load_sort_block
534 # Private function. Read the sort block.
535 sub _load_sort_block
536 {
537 0 ▶ my $pdb = shift;
Subroutine _load_sort_block: not called CC=4 SCAR=30.0
538 0 my $fh = shift; # Input file handle
539 0 my $len; # Length of sort block
540 my $buf; # Input buffer
541  
542 # Sanity check: make sure we're positioned at the beginning of
543 # the sort block
544 0 ▶ if (tell($fh) > $pdb->{_sort_offset})
Branch: 1 branch
BranchTrueFalseTotal
if (tell $fh > $pdb->{"_sort_offset"}) 0 0 0
545 {
546 die "Bad sort block offset: expected ",
547 0 sprintf("0x%08x", $pdb->{_sort_offset}),
548 ", but I'm at ",
549 tell($fh), "\n";
550 }
551  
552 # Seek to the right place, if necessary
553 0 ▶ if (tell($fh) != $pdb->{_sort_offset})
Branch: 1 branch
BranchTrueFalseTotal
if (tell $fh != $pdb->{"_sort_offset"}) 0 0 0
554 {
555 0 seek $fh, $pdb->{_sort_offset}, 0;
556 }
557  
558 # There's nothing that explicitly gives the size of the sort
559 # block. Rather, it has to be inferred from the offset of the
560 # sort block (previously recorded in $pdb->{_sort_offset})
561 # and whatever's next in the file. That's either the first
562 # data record, or the end of the file.
563  
564 0 ▶ if (defined($pdb->{_index}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pdb->{'_index'}) { } 0 0 0
565 {
566 # The next thing in the file is the first data record
567 $len = $pdb->{_index}[0]{offset} -
568 0 $pdb->{_sort_offset};
569 } else {
570 # There are no records. The sort block goes to the end
571 # of the file.
572 0 $len = $pdb->{_size} - $pdb->{_sort_offset};
573 }
574  
575 # Read the AppInfo block
576 0 read $fh, $buf, $len;
577  
578 # XXX - Check to see if the sort block has some predefined
579 # structure. If so, it might be a good idea to parse the sort
580 # block here.
581  
582 # Tell the real class to parse the sort block
583 0 $pdb->{sort} = $pdb->ParseSortBlock($buf);
584 }
585  
586 # _load_records
587 # Private function. Load the actual data records, for a record database
588 # (PDB)
589 sub _load_records
590 {
591 1 ▶ my $pdb = shift;
Subroutine _load_records: called CC=6 SCAR=18.1
592 1 my $fh = shift; # Input file handle
593 1 my $i;
594  
595 # Read each record in turn
596 1 ▶ for ($i = 0; $i < $pdb->{_numrecs}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pdb->{"_numrecs"}) 3 1 4
597 {
598 3 my $len; # Length of record
599 my $buf; # Input buffer
600  
601 # Sanity check: make sure we're where we think we
602 # should be.
603 3 ▶ if (tell($fh) > $pdb->{_index}[$i]{offset})
Branch: 1 branch
BranchTrueFalseTotal
if (tell $fh > $pdb->{"_index"}[$i]{"offset"}) 0 3 3
604 {
605 die "Bad offset for record $i: expected ",
606 sprintf("0x%08x",
607 0 $pdb->{_index}[$i]{offset}),
608 " but it's at ",
609 sprintf("[0x%08x]", tell($fh)), "\n";
610 }
611  
612 3 ▶ if( $pdb->{_index}[$i]{offset} > $pdb->{_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdb->{"_index"}[$i]{"offset"} > $pdb->{"_size"}) 0 3 3
613 0 die "corruption: Record $i beyond end of database!";
614 }
615  
616 # Seek to the right place, if necessary
617 3 ▶ if (tell($fh) != $pdb->{_index}[$i]{offset})
Branch: 1 branch
BranchTrueFalseTotal
if (tell $fh != $pdb->{"_index"}[$i]{"offset"}) 1 2 3
618 {
619 1 seek $fh, $pdb->{_index}[$i]{offset}, 0;
620 }
621  
622 # Compute the length of the record: the last record
623 # extends to the end of the file. The others extend to
624 # the beginning of the next record.
625 3 ▶ if ($i == $pdb->{_numrecs} - 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $pdb->{'_numrecs'} - 1) { } 1 2 3
626 {
627 # This is the last record
628 $len = $pdb->{_size} -
629 1 $pdb->{_index}[$i]{offset};
630 } else {
631 # This is not the last record
632 $len = $pdb->{_index}[$i+1]{offset} -
633 2 $pdb->{_index}[$i]{offset};
634 }
635  
636 # Read the record
637 3 read $fh, $buf, $len;
638  
639 # Tell the real class to parse the record data. Pass
640 # &ParseRecord all of the information from the index,
641 # plus a "data" field with the raw record data.
642 3 my $record;
643  
644 $record = $pdb->ParseRecord(
645 3 %{$pdb->{_index}[$i]},
646 "data" => $buf,
647 );
648 3 push @{$pdb->{records}}, $record;
649 }
650 }
651  
652 # _load_resources
653 # Private function. Load the actual data resources, for a resource database
654 # (PRC)
655 sub _load_resources
656 {
657 0 ▶ my $pdb = shift;
Subroutine _load_resources: not called CC=6 SCAR=37.4
658 0 my $fh = shift; # Input file handle
659 0 my $i;
660  
661 # Read each resource in turn
662 0 ▶ for ($i = 0; $i < $pdb->{_numrecs}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pdb->{"_numrecs"}) 0 0 0
663 {
664 0 my $len; # Length of record
665 my $buf; # Input buffer
666  
667 # Sanity check: make sure we're where we think we
668 # should be.
669 0 ▶ if (tell($fh) > $pdb->{_index}[$i]{offset})
Branch: 1 branch
BranchTrueFalseTotal
if (tell $fh > $pdb->{"_index"}[$i]{"offset"}) 0 0 0
670 {
671 die "Bad offset for resource $i: expected ",
672 sprintf("0x%08x",
673 0 $pdb->{_index}[$i]{offset}),
674 " but it's at ",
675 sprintf("0x%08x", tell($fh)), "\n";
676 }
677  
678 0 ▶ if( $pdb->{_index}[$i]{offset} > $pdb->{_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdb->{"_index"}[$i]{"offset"} > $pdb->{"_size"}) 0 0 0
679 0 die "corruption: Resource $i beyond end of database!";
680 }
681  
682 # Seek to the right place, if necessary
683 0 ▶ if (tell($fh) != $pdb->{_index}[$i]{offset})
Branch: 1 branch
BranchTrueFalseTotal
if (tell $fh != $pdb->{"_index"}[$i]{"offset"}) 0 0 0
684 {
685 0 seek $fh, $pdb->{_index}[$i]{offset}, 0;
686 }
687  
688 # Compute the length of the resource: the last
689 # resource extends to the end of the file. The others
690 # extend to the beginning of the next resource.
691 0 ▶ if ($i == $pdb->{_numrecs} - 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $pdb->{'_numrecs'} - 1) { } 0 0 0
692 {
693 # This is the last resource
694 $len = $pdb->{_size} -
695 0 $pdb->{_index}[$i]{offset};
696 } else {
697 # This is not the last resource
698 $len = $pdb->{_index}[$i+1]{offset} -
699 0 $pdb->{_index}[$i]{offset};
700 }
701  
702 # Read the resource
703 0 read $fh, $buf, $len;
704  
705 # Tell the real class to parse the resource data. Pass
706 # &ParseResource all of the information from the
707 # index, plus a "data" field with the raw resource
708 # data.
709 0 my $resource;
710  
711 $resource = $pdb->ParseResource(
712 0 %{$pdb->{_index}[$i]},
713 "data" => $buf,
714 );
715 0 push @{$pdb->{resources}}, $resource;
716 }
717 }
718  
719 #' <-- For Emacs
720  
721 sub Write
722 {
723 1 ▶ my $self = shift;
Subroutine Write: called CC=57 SCAR=56.0
Pod documented
724 1 my $fname = shift; # Output file name
725 1 my @record_data;
726 my @deleted_records;
727  
728 die "Can't write a database with no name\n"
729 1 ▶ unless $self->{name} ne "";
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"name"} ne "" 0 1 1
730  
731 1 my $handle = $self->_open('>', $fname);
732 1 ▶ return undef unless defined $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle 0 1 1
733  
734 # Open file
735 1 binmode $handle; # Write as binary file under MS-DOS
736  
737 # Get AppInfo block
738 1 my $appinfo_block = $self->PackAppInfoBlock;
739  
740 # Get sort block
741 1 my $sort_block = $self->PackSortBlock;
742  
743 1 my $index_len;
744  
745 # Get records or resources
746 1 ▶ if ($self->{attributes}{resource} || $self->{'attributes'}{'ResDB'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attributes'}{'resource'} or $self->{'attributes'}{'ResDB'}) { } 0 1 1
Condition33%1/3
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
$self->{"attributes"}{"resource"} $self->{"attributes"}{"ResDB"}
Truth table33%1/3
A or B
A= $self->{"attributes"}{"resource"}B= $self->{"attributes"}{"ResDB"}
ABresult
1X1
011
000
747 {
748 # Resource database
749 0 my $resource;
750  
751 0 foreach $resource (@{$self->{resources}})
752 {
753 0 my $type;
754 my $id;
755 0 my $data;
756  
757 # Get all the stuff that goes in the index, as
758 # well as the resource data.
759 0 $type = $resource->{type};
760 0 $id = $resource->{id};
761 0 $data = $self->PackResource($resource);
762  
763 0 push @record_data, [ $type, $id, $data ];
764 }
765 # Figure out size of index
766 0 $index_len = RecIndexHeaderLen +
767 @record_data * IndexRsrcLen;
768 } else {
769 1 my $record;
770  
771 1 foreach $record (@{$self->{records}})
772 {
773 3 my $attributes;
774 my $id;
775 3 my $data;
776  
777 # XXX - Should probably check the length of this
778 # record and not add it to the record if it's 0.
779  
780 # Get all the stuff that goes in the index, as
781 # well as the record data.
782 3 $attributes = 0;
783 3 ▶ if ($record->{attributes}{expunged} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{'attributes'}{'expunged'} or $record->{'attributes'}{'deleted'}) { } 0 3 3
Condition33%1/3
$record->{"attributes"}{"expunged"} or $record->{"attributes"}{"deleted"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$record->{"attributes"}{"expunged"} or $record->{"attributes"}{"deleted"}
$record->{"attributes"}{"expunged"} $record->{"attributes"}{"deleted"}
Truth table33%1/3
A or B
A= $record->{"attributes"}{"expunged"}B= $record->{"attributes"}{"deleted"}
ABresult
1X1
011
000
784 $record->{attributes}{deleted})
785 {
786 $attributes |= 0x08
787 0 ▶ if $record->{attributes}{archive};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"archive"} 0 0 0
788 } else {
789 3 $attributes = ($record->{category} & 0x0f);
790 }
791 $attributes |= 0x80
792 3 ▶ if $record->{attributes}{expunged};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"expunged"} 0 3 3
793 $attributes |= 0x40
794 3 ▶ if $record->{attributes}{dirty};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"dirty"} 3 0 3
795 $attributes |= 0x20
796 3 ▶ if $record->{attributes}{deleted};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"deleted"} 0 3 3
797 $attributes |= 0x10
798 3 ▶ if $record->{attributes}{private};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"private"} 0 3 3
799  
800 3 ▶ $attributes |= 0x80 if $record->{'attributes'}{'Delete'};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"Delete"} 0 3 3
801 3 ▶ $attributes |= 0x40 if $record->{'attributes'}{'Dirty'};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"Dirty"} 3 0 3
802 3 ▶ $attributes |= 0x20 if $record->{'attributes'}{'Busy'};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"Busy"} 0 3 3
803 3 ▶ $attributes |= 0x10 if $record->{'attributes'}{'Secret'};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"Secret"} 0 3 3
804  
805 3 $id = $record->{id};
806  
807 3 $data = $self->PackRecord($record);
808 3 ▶ if ($attributes & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes & 128) { } 0 3 3
809 0 push @deleted_records, [ $attributes, $id, $data ];
810 }
811 else {
812 3 push @record_data, [ $attributes, $id, $data ];
813 }
814  
815 }
816 # put deleted records at end (RT#101666)
817 1 push @record_data, @deleted_records;
818 # Figure out size of index
819 1 $index_len = RecIndexHeaderLen +
820 @record_data * IndexRecLen;
821 }
822  
823 1 my $header;
824 1 my $attributes = 0x0000;
825 1 my $appinfo_offset;
826 my $sort_offset;
827  
828 # Build attributes field
829 $attributes =
830 ($self->{attributes}{resource} ? 0x0001 : 0) |
831 ($self->{attributes}{"read-only"} ? 0x0002 : 0) |
832 ($self->{attributes}{"AppInfo dirty"} ? 0x0004 : 0) |
833 ($self->{attributes}{backup} ? 0x0008 : 0) |
834 ($self->{attributes}{"OK newer"} ? 0x0010 : 0) |
835 ($self->{attributes}{reset} ? 0x0020 : 0) |
836 1 ▶ ($self->{attributes}{open} ? 0x8000 : 0);
Branch: 7 branches
BranchTrueFalseTotal
$self->{'attributes'}{'resource'} ? : 0 1 1
$self->{'attributes'}{'read-only'} ? : 0 1 1
$self->{'attributes'}{'AppInfo dirty'} ? : 0 1 1
$self->{'attributes'}{'backup'} ? : 0 1 1
$self->{'attributes'}{'OK newer'} ? : 0 1 1
$self->{'attributes'}{'reset'} ? : 0 1 1
$self->{'attributes'}{'open'} ? : 0 1 1
837  
838 1 ▶ $attributes |= 0x0001 if $self->{'attributes'}{'ResDB'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"ResDB"} 0 1 1
839 1 ▶ $attributes |= 0x0002 if $self->{'attributes'}{'ReadOnly'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"ReadOnly"} 0 1 1
840 1 ▶ $attributes |= 0x0004 if $self->{'attributes'}{'AppInfoDirty'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"AppInfoDirty"} 0 1 1
841 1 ▶ $attributes |= 0x0008 if $self->{'attributes'}{'Backup'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"Backup"} 0 1 1
842 1 ▶ $attributes |= 0x0010 if $self->{'attributes'}{'OKToInstallNewer'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"OKToInstallNewer"} 0 1 1
843 1 ▶ $attributes |= 0x0020 if $self->{'attributes'}{'ResetAfterInstall'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"ResetAfterInstall"} 0 1 1
844 1 ▶ $attributes |= 0x0040 if $self->{'attributes'}{'CopyPrevention'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"CopyPrevention"} 0 1 1
845 1 ▶ $attributes |= 0x0080 if $self->{'attributes'}{'Stream'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"Stream"} 0 1 1
846 1 ▶ $attributes |= 0x0100 if $self->{'attributes'}{'Hidden'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"Hidden"} 0 1 1
847 1 ▶ $attributes |= 0x0200 if $self->{'attributes'}{'LaunchableData'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"LaunchableData"} 0 1 1
848 1 ▶ $attributes |= 0x0400 if $self->{'attributes'}{'Recyclable'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"Recyclable"} 0 1 1
849 1 ▶ $attributes |= 0x0800 if $self->{'attributes'}{'Bundle'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"Bundle"} 0 1 1
850 1 ▶ $attributes |= 0x8000 if $self->{'attributes'}{'Open'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"Open"} 0 1 1
851  
852  
853 # Calculate AppInfo block offset
854 1 ▶ if ((!defined($appinfo_block)) || ($appinfo_block eq ""))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $appinfo_block or $appinfo_block eq '') { } 1 0 1
Condition33%1/3
not defined $appinfo_block or $appinfo_block eq ""
l!l&&r!l&&!r
100
MC/DC0%0/2
not defined $appinfo_block or $appinfo_block eq ""
not defined $appinfo_block $appinfo_block eq ""
Truth table33%1/3
not(A) or B
A= not defined $appinfo_blockB= $appinfo_block eq ""
ABresult
1X1
011
000
855 {
856 # There's no AppInfo block
857 1 $appinfo_offset = 0;
858 } else {
859 # Offset of AppInfo block from start of file
860 0 $appinfo_offset = HeaderLen + $index_len + 2;
861 }
862  
863 # Calculate sort block offset
864 1 ▶ if ((!defined($sort_block)) || ($sort_block eq ""))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sort_block or $sort_block eq '') { } 1 0 1
Condition33%1/3
not defined $sort_block or $sort_block eq ""
l!l&&r!l&&!r
100
MC/DC0%0/2
not defined $sort_block or $sort_block eq ""
not defined $sort_block $sort_block eq ""
Truth table33%1/3
not(A) or B
A= not defined $sort_blockB= $sort_block eq ""
ABresult
1X1
011
000
865 {
866 # There's no sort block
867 1 $sort_offset = 0;
868 } else {
869 # Offset of sort block...
870 0 ▶ if ($appinfo_offset == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($appinfo_offset == 0) { } 0 0 0
871 {
872 # ...from start of file
873 0 $sort_offset = HeaderLen + $index_len + 2;
874 } else {
875 # ...or just from start of AppInfo block
876 0 $sort_offset = $appinfo_offset +
877 length($appinfo_block);
878 }
879 }
880  
881 # Write header
882 $header = pack "a32 n n N N N N N N a4 a4 N",
883 $self->{name},
884 $attributes,
885 $self->{version},
886 $self->{ctime} + EPOCH_1904,
887 $self->{mtime} + EPOCH_1904,
888 $self->{baktime} + EPOCH_1904,
889 $self->{modnum},
890 $appinfo_offset,
891 $sort_offset,
892 $self->{type},
893 $self->{creator},
894 1 $self->{uniqueIDseed};
895 ;
896  
897 1 print $handle "$header";
898  
899 # Write index header
900 1 my $index_header;
901  
902 1 $index_header = pack "N n", 0, scalar @record_data;
903 1 print $handle "$index_header";
904  
905 # Write index
906 1 my $rec_offset; # Offset of next record/resource
907  
908 # Calculate offset of first record/resource
909 1 ▶ if ($sort_offset != 0)
Branch: 2 branches
BranchTrueFalseTotal
if ($sort_offset != 0) { } 0 1 1
elsif ($appinfo_offset != 0) { } 0 1 1
910 {
911 0 $rec_offset = $sort_offset + length($sort_block);
912 } elsif ($appinfo_offset != 0)
913 {
914 0 $rec_offset = $appinfo_offset + length($appinfo_block);
915 } else {
916 1 $rec_offset = HeaderLen + $index_len + 2;
917 }
918  
919 1 ▶ if ($self->{attributes}{resource} || $self->{'attributes'}{'ResDB'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attributes'}{'resource'} or $self->{'attributes'}{'ResDB'}) { } 0 1 1
Condition33%1/3
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
$self->{"attributes"}{"resource"} $self->{"attributes"}{"ResDB"}
Truth table33%1/3
A or B
A= $self->{"attributes"}{"resource"}B= $self->{"attributes"}{"ResDB"}
ABresult
1X1
011
000
920 {
921 # Resource database
922 # Record database
923 0 my $rsrc_data;
924  
925 0 foreach $rsrc_data (@record_data)
926 {
927 0 my $type;
928 my $id;
929 0 my $data;
930 0 my $index_data;
931  
932 0 ($type, $id, $data) = @{$rsrc_data};
933 0 $index_data = pack "a4 n N",
934 $type,
935 $id,
936 $rec_offset;
937 0 print $handle "$index_data";
938  
939 0 $rec_offset += length($data);
940 }
941 } else {
942 # Record database
943 1 my $rec_data;
944  
945 1 foreach $rec_data (@record_data)
946 {
947 3 my $attributes;
948 my $data;
949 3 my $id;
950 3 my $index_data;
951  
952 # XXX - Probably shouldn't write this record if
953 # length($data) == 0
954 3 ($attributes, $id, $data) = @{$rec_data};
955  
956 3 ▶ if (length($data) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (length $data == 0) 0 3 3
957 {
958 0 warn printf("Write: Warning: record 0x%08x has length 0\n", $id)
959 }
960  
961 3 $index_data = pack "N C C3",
962 $rec_offset,
963 $attributes,
964 ($id >> 16) & 0xff,
965 ($id >> 8) & 0xff,
966 $id & 0xff;
967 3 print $handle "$index_data";
968  
969 3 $rec_offset += length($data);
970 }
971 }
972  
973 # Write the two NULs
974 1 ▶ if (length($self->{"2NULs"}) == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'2NULs'} == 2) { } 1 0 1
975 {
976 1 print $handle $self->{"2NULs"};
977 } else {
978 0 print $handle "\0\0";
979 }
980  
981 # Write AppInfo block
982 1 ▶ print $handle $appinfo_block unless $appinfo_offset == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $appinfo_offset == 0 0 1 1
983  
984 # Write sort block
985 1 ▶ print $handle $sort_block unless $sort_offset == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $sort_offset == 0 0 1 1
986  
987 # Write record/resource list
988 1 my $record;
989 1 foreach $record (@record_data)
990 {
991 3 my $data;
992  
993 3 ▶ if ($self->{attributes}{resource} || $self->{'attributes'}{'ResDB'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'attributes'}{'resource'} or $self->{'attributes'}{'ResDB'}) { } 0 3 3
Condition33%1/3
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
$self->{"attributes"}{"resource"} $self->{"attributes"}{"ResDB"}
Truth table33%1/3
A or B
A= $self->{"attributes"}{"resource"}B= $self->{"attributes"}{"ResDB"}
ABresult
1X1
011
000
994 {
995 # Resource database
996 0 my $type;
997 my $id;
998  
999 0 ($type, $id, $data) = @{$record};
1000 } else {
1001 3 my $attributes;
1002 my $id;
1003  
1004 3 ($attributes, $id, $data) = @{$record};
1005 }
1006 3 print $handle $data;
1007 }
1008  
1009 1 return $self;
1010 }
1011  
1012  
1013 # PDB::new_Record()
1014 # Create a new, initialized record, and return a reference to it.
1015 # The record is initially marked as being dirty, since that's usually
1016 # the Right Thing.
1017 sub new_Record
1018 {
1019 3 ▶ my $classname = shift;
Subroutine new_Record: called CC=1 SCAR=0.0
Pod documented
1020 3 my $retval = {};
1021  
1022 # Initialize the record
1023 3 $retval->{'category'} = 0; # Unfiled, by convention
1024 3 $retval->{'attributes'} = {
1025 # expunged => 0,
1026 dirty => 1, # Note: originally dirty
1027 'Dirty' => 1,
1028 # deleted => 0,
1029 # private => 0,
1030 # archive => 0,
1031 };
1032 3 $retval->{'id'} = 0; # Initially, no record ID
1033  
1034 3 return $retval;
1035 }
1036  
1037 #'
1038  
1039 sub is_Dirty
1040 {
1041 0 ▶ my $self = shift;
Subroutine is_Dirty: not called CC=9 SCAR=45.0
Pod documented
1042  
1043 # try the quick and easy tests first
1044 0 ▶ return 1 if $self->{'dirty'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dirty"} 0 0 0
1045 0 ▶ return 1 if $self->{'attributes'}{'AppInfoDirty'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"AppInfoDirty"} 0 0 0
1046 0 ▶ return 1 if $self->{'attributes'}{'AppInfo dirty'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}{"AppInfo dirty"} 0 0 0
1047  
1048 # okay, check the records. Note that resource entries appear to
1049 # have no dirty flags for us to use.
1050 0 ▶ if (!$self->{attributes}{resource} and !$self->{'attributes'}{'ResDB'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}) 0 0 0
Condition0%0/3
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"attributes"}{"resource"} or $self->{"attributes"}{"ResDB"}
$self->{"attributes"}{"resource"} $self->{"attributes"}{"ResDB"}
Truth table0%0/3
A or B
A= $self->{"attributes"}{"resource"}B= $self->{"attributes"}{"ResDB"}
ABresult
1X1
011
000
1051 {
1052 0 my $record;
1053  
1054 0 foreach $record (@{$self->{records}})
1055 {
1056 0 ▶ return 1 if $record->{'attributes'}{'Dirty'};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"Dirty"} 0 0 0
1057 0 ▶ return 1 if $record->{'attributes'}{'dirty'};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"attributes"}{"dirty"} 0 0 0
1058 }
1059 }
1060  
1061 0 return 0;
1062 }
1063  
1064 #'
1065  
1066 # append_Record
1067 # Append the given records to the database's list of records. If no
1068 # records are given, create one, append it, and return a reference to
1069 # it.
1070 sub append_Record
1071 {
1072 3 ▶ my $self = shift;
Subroutine append_Record: called CC=5 SCAR=20.9
Pod documented
1073  
1074 3 ▶ unless (@_)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 3 0 3
1075 {
1076 # No arguments given. Create a new record.
1077 3 my $record = $self->new_Record;
1078  
1079 # Validate the unique ID.
1080 $self->_setUniqueID($record)
1081 3 ▶ if $record->{'id'} eq 0;
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"id"} eq 0 3 0 3
1082  
1083 3 push @{$self->{records}}, $record;
1084  
1085 # Update the "last modification time".
1086 3 $self->{mtime} = time;
1087 3 $self->{dirty} = 1;
1088  
1089 3 return $record;
1090 }
1091  
1092 # Validate the unique IDs.
1093 0 foreach my $record (@_)
1094 {
1095 $self->_setUniqueID($record)
1096 0 ▶ if $record->{'id'} eq 0;
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"id"} eq 0 0 0 0
1097 }
1098  
1099 # At least one argument was given. Append all of the arguments
1100 # to the list of records, and return the first one.
1101 0 push @{$self->{records}}, @_;
1102  
1103 # Update the "last modification time".
1104 0 $self->{mtime} = time;
1105 0 $self->{'dirty'} = 1;
1106  
1107 0 return $_[0];
1108 }
1109  
1110 sub _setUniqueID
1111 {
1112 3 ▶ my($self, $record) = @_;
Subroutine _setUniqueID: called CC=2 SCAR=7.0
1113  
1114 # Bump the seed to prevent a uniqueIDseed of 0 which represents
1115 # an unassigned uniqueID.
1116 # XXX IMHO this just couldn't happen given the way the seed it's
1117 # generated. But if Palm OS goes this way maybe it's better to do
1118 # the same.
1119  
1120 3 $self->{'uniqueIDseed'}++;
1121  
1122 # Check for wrap around. Remember that an uniqueID is made of only 24 bits.
1123 $self->{'uniqueIDseed'} = (dmRecordIDReservedRange + 1) << 12
1124 3 ▶ if ($self->{'uniqueIDseed'} & 0xFF000000);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"uniqueIDseed"} & 4278190080 0 3 3
1125  
1126 # Copy the seed into the new record.
1127 3 $record->{'id'} = $self->{'uniqueIDseed'};
1128 }
1129  
1130  
1131 # new_Resource
1132 # Create a new, initialized resource, and return a reference to it.
1133 sub new_Resource
1134 {
1135 0 ▶ my $classname = shift;
Subroutine new_Resource: not called CC=1 SCAR=6.9
Pod documented
1136 0 my $retval = {};
1137  
1138 # Initialize the resource
1139 0 $retval->{type} = "\0\0\0\0";
1140 0 $retval->{id} = 0;
1141  
1142 0 return $retval;
1143 }
1144  
1145 #'
1146  
1147 # append_Resource
1148 # Append the given resources to the database's list of resources. If no
1149 # resources are given, create one, append it, and return a reference to
1150 # it.
1151 sub append_Resource
1152 {
1153 0 ▶ my $self = shift;
Subroutine append_Resource: not called CC=2 SCAR=17.9
Pod documented
1154  
1155 0 ▶ unless (@_)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
1156 {
1157 # No arguments given. Create a new resource
1158 0 my $resource = $self->new_Resource;
1159  
1160 0 push @{$self->{resources}}, $resource;
1161  
1162 # Update the "last modification time".
1163 0 $self->{mtime} = time;
1164 0 $self->{'dirty'} = 1;
1165  
1166 0 return $resource;
1167 }
1168  
1169 # At least one argument was given. Append all of the arguments
1170 # to the list of resources, and return the first one.
1171 0 push @{$self->{resources}}, @_;
1172  
1173 # Update the "last modification time".
1174 0 $self->{mtime} = time;
1175 0 $self->{'dirty'} = 1;
1176  
1177 0 return $_[0];
1178 }
1179  
1180  
1181 # findRecordByID
1182 # Returns a reference to the record with the given ID, or 'undef' if
1183 # it doesn't exist.
1184 sub findRecordByID
1185 {
1186 0 ▶ my $self = shift;
Subroutine findRecordByID: not called CC=4 SCAR=30.0
Pod documented
1187 0 my $id = shift;
1188  
1189 0 ▶ return undef if $id eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $id eq "" 0 0 0
1190  
1191 0 for (@{$self->{records}})
1192 {
1193 0 ▶ next unless $_->{id} == $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"id"} == $id 0 0 0
1194 0 return $_; # Found it
1195 }
1196  
1197 0 return undef; # Not found
1198 }
1199  
1200 #'
1201  
1202 # delete_Record
1203 # $pdb->delete_Record($record ?, $expunge?)
1204 #
1205 # Mark the given record for deletion. If $expunge is true, mark the
1206 # record for deletion without an archive.
1207  
1208 sub delete_Record
1209 {
1210 0 ▶ my $self = shift;
Subroutine delete_Record: not called CC=2 SCAR=17.9
Pod documented
1211 0 my $record = shift;
1212 0 my $expunge = shift;
1213  
1214 0 $record->{attributes}{deleted} = 1;
1215 0 ▶ if ($expunge)
Branch: 1 branch
BranchTrueFalseTotal
if ($expunge) { } 0 0 0
1216 {
1217 0 $record->{attributes}{expunged} = 1;
1218 0 $record->{attributes}{archive} = 0;
1219 } else {
1220 0 $record->{attributes}{expunged} = 0;
1221 0 $record->{attributes}{archive} = 1;
1222 }
1223  
1224 # Update the "last modification time".
1225 0 $self->{mtime} = time;
1226 0 $self->{'dirty'} = 1;
1227 }
1228  
1229 #'
1230  
1231 sub remove_Record($$)
1232 {
1233 0 ▶ my $self = shift;
Subroutine remove_Record: not called CC=3 SCAR=24.8
Pod documented
1234 0 my $record = shift;
1235  
1236 0 ▶ for (my $i = 0; $i <= $#{$self->{records}}; $i ++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"records"};}) 0 0 0
1237 {
1238 0 ▶ if ($self->{records}->[$i] == $record)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"records"}[$i] == $record) 0 0 0
1239 {
1240 # make a copy of the records array. This is really necessary
1241 # because there's frequently something using the records reference
1242 # for iteration purposes (like the doc example) and we can't
1243 # just start splicing that apart (tried, failed).
1244 # So we have to make a new copy. This does, unfortunately,
1245 # make remove_Record() more expensive that you'd expect.
1246 0 $self->{records} = [ @{$self->{records}} ];
1247  
1248 # remove the record index.
1249 0 splice @{$self->{records}}, $i, 1;
1250  
1251 0 $self->{mtime} = time;
1252 0 $self->{'dirty'} = 1;
1253  
1254 0 last;
1255 }
1256 }
1257 }
1258  
1259 3 1;
1260  
1261 __END__
↑ Summary Raw.pm »