blib/lib/Mac/AppleSingleDouble.pm

filter: Statementstmt 65.7% 140 / 213 Branchbran 55.2% 21 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 20 / 28 Podpod 100% 17 / 17 totaltotal 66.8% 198 / 296 CC 23 SCAR 14.6
CC
23
Cov
64%
dump_header
20.0
cache_entries
6.0
get_all_entries
6.0
CRAP 4.3
↑ Summary
1 # Mac::AppleSingleDouble.pm, (C) 2001 Jamie Flournoy (jamie@white-mountain.org).
2  
3 package Mac::AppleSingleDouble;
4 13 require 5;
5 13 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 $Mac::AppleSingleDouble::VERSION='1.0';
8  
9 # default Finder colors for label values.
10 13 %labelcolors = (0 => 'Black',
11 1 => 'Brown',
12 2 => 'Green',
13 3 => 'Blue',
14 4 => 'Cyan',
15 5 => 'Pink',
16 6 => 'Red',
17 7 => 'Orange');
18  
19 # default Finder names for label values.
20 13 %labelnames = (0 => 'None',
21 1 => 'Project 2',
22 2 => 'Project 1',
23 3 => 'Personal',
24 4 => 'Cool',
25 5 => 'In Progress',
26 6 => 'Hot',
27 7 => 'Essential');
28  
29 13 %entryids = (1 => 'Data Fork',
30 2 => 'Resource Fork',
31 3 => 'Real Name',
32 4 => 'Comment',
33 5 => 'Icon, B&W',
34 6 => 'Icon, Color',
35 8 => 'File Dates Info',
36 9 => 'Finder Info',
37 10 => 'Macintosh File Info',
38 11 => 'ProDOS File Info',
39 12 => 'MS-DOS File Info',
40 13 => 'Short Name',
41 14 => 'AFP File Info',
42 15 => 'Directory ID');
43  
44 # Magic number values mapped to file format (AppleSingle or
45 # AppleDouble). Any other value means it's not an AppleSingle or
46 # AppleDouble file.
47 13 %formats = ( pack('H8', "00051600") => 'AppleSingle',
48 pack('H8', "00051607") => 'AppleDouble');
49  
50 sub new
51 13 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
52 13 my $class = shift;
53 13 my $filename = shift;
54 13 ▶ if (!defined($filename))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 1 12 13
55 {
56 1 die "The constructor (new) requires a filename as an argument!";
57 }
58 12 my $this = {}; # instances are based on hashes
59 12 bless $this, $class; # now $this is an instance of $class
60 12 $this->_initialize($filename);
61 10 return $this;
62 }
63  
64 sub DESTROY
65 11 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
66 11 my $this = shift;
67 11 $this->close();
68 }
69  
70 sub close
71 20 ▶{
Subroutine close: called CC=2 SCAR=6.9
Pod documented
72 20 my $this = shift;
73 20 ▶ if ($this->{'_filehandle'})
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_filehandle"}) 8 12 20
74 {
75 8 CORE::close $this->{'_filehandle'};
76 8 undef($this->{'_filehandle'});
77 }
78 }
79  
80 sub get_finder_info
81 3 ▶{
Subroutine get_finder_info: called CC=2 SCAR=7.0
Pod documented
82 3 my $this = shift;
83 3 $this->_require_applesingledouble();
84 3 ▶ if (!defined($this->{'_finder_info'}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_finder_info"}) 3 0 3
85 {
86 3 $this->_parse_finder_info($this->get_entry(9));
87 }
88 3 return $this->{'_finder_info'}
89 }
90  
91 sub get_entry
92 5 ▶{
Subroutine get_entry: called CC=3 SCAR=11.3
Pod documented
93 5 my $this = shift;
94 5 my $entryid = shift;
95 5 $this->_require_applesingledouble();
96 5 my $entry = $this->{'_entries'}->{$entryid};
97 5 ▶ if (!defined($entry))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $entry) 5 0 5
98 {
99 5 $entry = $this->_get_entry_from_file($entryid);
100 }
101 5 ▶ if ($this->{'_cache_entries'})
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_cache_entries"}) 0 5 5
102 {
103 0 $this->{'_entries'}->{$entryid} = $entry;
104 }
105 5 return $entry;
106 }
107  
108 sub get_file_format
109 26 ▶{
Subroutine get_file_format: called CC=3 SCAR=11.0
Pod documented
110 26 my $this = shift;
111 26 ▶ if (!defined($this->{'_magicno'}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_magicno"}) 9 17 26
112 {
113 9 $this->_parse_header();
114 }
115 26 my $format = $formats{$this->{'_magicno'}};
116 26 ▶ if (!defined($format)) { $format = 'Plain'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $format) 2 24 26
117 26 return $format;
118 }
119  
120 sub is_applesingle
121 1 ▶{
Subroutine is_applesingle: called CC=1 SCAR=0.0
Pod documented
122 1 my $this = shift;
123 1 return ($this->get_file_format() eq 'AppleSingle');
124 }
125  
126 sub is_appledouble
127 1 ▶{
Subroutine is_appledouble: called CC=1 SCAR=0.0
Pod documented
128 1 my $this = shift;
129 1 return $this->get_file_format() eq 'AppleDouble';
130 }
131  
132 sub preload_entire_file
133 0 ▶{
Subroutine preload_entire_file: not called CC=1 SCAR=6.9
Pod documented
134 0 my $this = shift;
135 0 $this->_require_applesingledouble();
136 0 $this->cache_entries(1);
137 0 $this->get_all_entries();
138 0 $this->close();
139 }
140  
141 sub cache_entries
142 0 ▶{
Subroutine cache_entries: not called CC=2 SCAR=17.9
Pod documented
143 0 my $this = shift;
144 0 my $val = shift;
145 0 ▶ if (defined($val))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
146 {
147 0 $this->{'_cache_entries'} = $val;
148 }
149 0 return $this->{'_cache_entries'};
150 }
151  
152  
153 sub get_entry_descriptors
154 5 ▶{
Subroutine get_entry_descriptors: called CC=1 SCAR=0.0
Pod documented
155 5 my $this = shift;
156 5 $this->_require_applesingledouble();
157 5 return $this->{'_descriptors'};
158 }
159  
160 sub get_all_entries
161 0 ▶{
Subroutine get_all_entries: not called CC=2 SCAR=17.9
Pod documented
162 0 my $this = shift;
163 0 $this->_require_applesingledouble();
164 0 my %entries = ();
165 0 my $descriptors = $this->get_entry_descriptors();
166 0 foreach $entryid (keys( %{$descriptors} ))
167 {
168 0 $entries{$entryid} = $this->get_entry($entryid);
169 }
170 0 return \%entries;
171 }
172  
173 sub set_labelnames
174 1 ▶{
Subroutine set_labelnames: called CC=1 SCAR=0.0
Pod documented
175 1 my $this = shift;
176 1 my $new_labelnames = shift;
177 1 $this->{'_labelnames'} = $new_labelnames;
178 }
179  
180 sub set_labelcolors
181 1 ▶{
Subroutine set_labelcolors: called CC=1 SCAR=0.0
Pod documented
182 1 my $this = shift;
183 1 my $new_labelcolors = shift;
184 1 $this->{'_labelcolors'} = $new_labelcolors;
185 }
186  
187 sub dump
188 0 ▶{
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
189 0 my $this = shift;
190 0 $this->dump_header();
191 0 print "\n";
192 0 $this->dump_entries();
193 }
194  
195 sub dump_header
196 0 ▶{
Subroutine dump_header: not called CC=4 SCAR=30.0
Pod documented
197 0 my $this = shift;
198 0 $this->_require_applesingledouble();
199 0 print "Dumping " . $this->get_file_format() . " file '" . $this->{'_filename'} . "':\n";
200 0 ▶ if ($this->get_file_format() eq 'Plain')
Branch: 1 branch
BranchTrueFalseTotal
if ($this->get_file_format eq "Plain") 0 0 0
201 {
202 0 print "Can't dump a file unless it's in AppleSingle or AppleDouble format.\n";
203 0 return;
204 }
205 0 print "File is " . $this->{'_size'} . " bytes long.\n";
206 0 print "Magic Number is " . unpack('H8', $this->{'_magicno'}) . ".\n";
207 0 print "Version Number is " . unpack('H8', $this->{'_version'}) . ".\n";
208  
209 0 print "Entry descriptor table:\n";
210 0 my $descriptors = $this->{'_descriptors'};
211 0 my $d = $descriptors; # make next line look purty
212 0 foreach $entryid (sort {$d->{$a}->{'Offset'} <=> $d->{$b}->{'Offset'} } keys( %{$descriptors} ))
213 {
214 0 print "Offset: " . $descriptors->{$entryid}->{'Offset'} . "\t";
215 0 print "Length: " . $descriptors->{$entryid}->{'Length'} . "\t";
216 0 my $entryidname = $entryids{$entryid};
217 0 ▶ if (!defined($entryidname)) { $entryidname = '???'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $entryidname) 0 0 0
218 0 print "EntryID: $entryid ($entryidname)\n";
219 }
220 }
221  
222 sub dump_entries
223 0 ▶{
Subroutine dump_entries: not called CC=2 SCAR=17.9
Pod documented
224 0 my $this = shift;
225  
226 0 my $descriptors = $this->{'_descriptors'};
227 0 foreach $entryid (sort {$descriptors->{$a}->{'Offset'} <=> $descriptors->{$b}->{'Offset'} } keys( %{$descriptors} ))
228 {
229 0 $this->dump_entry($entryid);
230 }
231 }
232  
233 sub dump_entry
234 0 ▶{
Subroutine dump_entry: not called CC=2 SCAR=17.9
Pod documented
235 0 my $this = shift;
236 0 my $entryid = shift;
237  
238 0 my $entryidname = $entryids{$entryid};
239 0 ▶ if (!defined($entryidname)) { $entryidname = '???'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $entryidname) 0 0 0
240 0 print "EntryID: $entryid ($entryidname)\n";
241 0 print $this->_hex_dump($this->get_entry($entryid)) . "\n";
242 }
243  
244 sub _hex_dump
245 0 ▶{
Subroutine _hex_dump: not called CC=2 SCAR=17.9
246 0 my $this = shift;
247 0 my $bytes = shift;
248  
249 0 my $length = length($bytes);
250 0 my $hexdump = '';
251 # this code is based on a script by David Thorburn-Gundlach
252 0 ▶ for ($p = 0; $p < $length; $p += 16)
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $length) 0 0 0
253 {
254 0 $byteno = sprintf('%8lx', $p);
255 0 $byteno =~ s/ /0/g;
256 0 $byteno =~ s/^(....)/$1 /g;
257 0 $asc_string = substr($bytes, $p, 16);
258 0 $hex_string = unpack('H32', $asc_string);
259 0 $hex_string =~ s/(..)/$1 /g;
260 0 $pad = ' ' x (3*(16-length($asc_string)));
261 0 $asc_string =~ s/([\00-\37,\177])/./g;
262 0 $hexdump .= "$byteno: $hex_string$pad $asc_string\n";
263 }
264 0 return $hexdump;
265 }
266  
267 sub _initialize
268 12 ▶{
Subroutine _initialize: called CC=2 SCAR=6.9
269 12 my $this = shift;
270 12 $this->{'_filename'} = shift;
271 12 ▶ if (!-f $this->{'_filename'})
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $this->{"_filename"}) 2 10 12
272 {
273 2 die "'$this->{'_filename'}' is not a file!";
274 }
275 10 $this->{'_entries'} = {};
276 10 $this->{'_labelnames'} = \%labelnames;
277 10 $this->{'_labelcolors'} = \%labelcolors;
278 }
279  
280 sub _read_header
281 9 ▶{
Subroutine _read_header: called CC=1 SCAR=0.0
282 9 my $this = shift;
283 9 my $header_raw;
284  
285 9 my $fh = new FileHandle;
286 9 $fh->open($this->{'_filename'});
287 9 ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($fh);
288 9 $this->{'_size'} = $size;
289 9 read($fh, $header_raw, 26);
290 # not closed here - must use $this->close() later;
291 9 $this->{'_filehandle'} = $fh;
292  
293 9 return $header_raw;
294 }
295  
296 sub _parse_header
297 9 ▶{
Subroutine _parse_header: called CC=3 SCAR=11.0
298 9 my $this = shift;
299 9 ▶ return if defined($this->{'_magicno'}); # already did it
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_magicno"} 0 9 9
300
301 9 $header_raw = $this->_read_header();
302
303 9 $this->{'_magicno'} = substr($header_raw, 0,4);
304 9 ▶ if ($this->get_file_format() ne 'Plain') # will not infinitely recurse because we just set _magicno
Branch: 1 branch
BranchTrueFalseTotal
if ($this->get_file_format ne "Plain") 8 1 9
305 {
306 8 $this->{'_version'} = substr($header_raw, 4,4);
307 8 my $entrycount = unpack('n', substr($header_raw, 24,2));
308 8 my $descriptors_raw = $this->_read_descriptors($entrycount);
309 8 $this->_parse_descriptors($entrycount, $descriptors_raw);
310 }
311 }
312  
313 sub _read_descriptors
314 8 ▶{
Subroutine _read_descriptors: called CC=1 SCAR=0.0
315 8 my $this = shift;
316 8 my $entrycount = shift;
317 8 my $descriptors_raw;
318 # this must be called after _read_header!
319 8 seek($this->{'_filehandle'}, 26, 0);
320 8 read($this->{'_filehandle'}, $descriptors_raw, $entrycount * 12);
321 8 return $descriptors_raw;
322 }
323  
324 sub _parse_descriptors
325 8 ▶{
Subroutine _parse_descriptors: called CC=2 SCAR=6.9
326 8 my $this = shift;
327 8 my $entrycount = shift;
328 8 my $descriptors_raw = shift;
329 8 my(%descriptors);
330  
331 8 ▶ for($i = 0; $i < $entrycount; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $entrycount) 40 8 48
332 {
333 40 my(%descriptor);
334 40 $entrystart = (12 * $i);
335 40 $descriptor{'EntryID'} = unpack('N', substr($descriptors_raw, $entrystart, 4));
336 40 $descriptor{'Offset'} = unpack('N', substr($descriptors_raw, $entrystart + 4, 4));
337 40 $descriptor{'Length'} = unpack('N', substr($descriptors_raw, $entrystart + 8, 4));
338  
339 # store in the descriptors hash keyed by entry ID
340 40 $descriptors{$descriptor{'EntryID'}} = \%descriptor;
341 }
342  
343 8 $this->{'_descriptors'} = \%descriptors;
344 }
345  
346 sub _parse_finder_info
347 3 ▶{
Subroutine _parse_finder_info: called CC=3 SCAR=11.0
348 3 my $this = shift;
349 3 my $finderinfo_raw = shift;
350 3 my(%finderinfo);
351  
352 # based on page 7-76 of Inside Macintosh: Finder Interface
353 3 $finderinfo{'Type'} = substr($finderinfo_raw, 0, 4);
354 3 $finderinfo{'Creator'} = substr($finderinfo_raw, 4, 4);
355 3 $finderinfo{'Flags'} = unpack('n', substr($finderinfo_raw, 8, 2));
356 3 $finderinfo{'Location'} = unpack('nn', substr($finderinfo_raw, 10, 4));
357 3 $finderinfo{'Fldr'} = unpack('n', substr($finderinfo_raw, 14, 2));
358  
359 # Finder Flags
360 3 $flagbits = unpack('B8', substr($finderinfo_raw, 8, 1)) .unpack('B8', substr($finderinfo_raw, 9, 1)) ;
361 #print "flagbits is $flagbits\n";
362 3 $finderinfo{'Label'} = unpack('C', pack('B8', '0'x5 . substr($flagbits, 12, 3)));
363 3 $finderinfo{'Color'} = $finderinfo{'Label'};
364 3 $finderinfo{'IsOnDesk'} = substr($flagbits, 15, 1);
365 3 $finderinfo{'IsShared'} = substr($flagbits, 9, 1);
366 3 $finderinfo{'HasBeenInited'} = substr($flagbits, 7, 1);
367 3 $finderinfo{'HasCustomIcon'} = substr($flagbits, 5, 1);
368 3 $finderinfo{'IsStationery'} = substr($flagbits, 4, 1);
369 3 $finderinfo{'NameLocked'} = substr($flagbits, 3, 1);
370 3 $finderinfo{'HasBundle'} = substr($flagbits, 2, 1);
371 3 $finderinfo{'IsInvisible'} = substr($flagbits, 1, 1);
372 3 $finderinfo{'IsAlias'} = substr($flagbits, 0, 1);
373  
374 # Extended Finder Info
375 3 $finderinfo{'IconID'} = unpack('n', substr($finderinfo_raw, 16, 2));
376 3 $finderinfo{'Script'} = unpack('c', substr($finderinfo_raw, 24, 1));
377 3 $finderinfo{'XFlags'} = unpack('B8', substr($finderinfo_raw, 25, 1));
378 3 $finderinfo{'Comment'} = unpack('n', substr($finderinfo_raw, 26, 2));
379 3 $finderinfo{'PutAway'} = unpack('N', substr($finderinfo_raw, 28, 4));
380  
381 3 my $labelcolor = $this->{'_labelcolors'}->{$finderinfo{'Label'}};
382 3 my $labelname = $this->{'_labelnames'}->{$finderinfo{'Label'}};
383 3 ▶ $finderinfo{'LabelColor'} = defined($labelcolor)? $labelcolor : '(no labelcolor provided)';
Branch: 1 branch
BranchTrueFalseTotal
defined $labelcolor ? : 3 0 3
384 3 ▶ $finderinfo{'LabelName'} = defined($labelname)? $labelname : '(no labelname provided)';
Branch: 1 branch
BranchTrueFalseTotal
defined $labelname ? : 3 0 3
385  
386 3 $this->{'_finder_info'} = \%finderinfo;
387 }
388  
389 sub _get_entry_from_file
390 5 ▶{
Subroutine _get_entry_from_file: called CC=1 SCAR=0.0
391 5 my $this = shift;
392 5 my $entryid = shift;
393 5 my $descriptors = $this->get_entry_descriptors();
394
395 5 my $descriptor = $descriptors->{$entryid};
396 5 my $entry;
397 5 seek($this->{'_filehandle'}, $descriptor->{'Offset'}, 0);
398 5 read($this->{'_filehandle'}, $entry, $descriptor->{'Length'});
399 5 return $entry;
400 }
401  
402 sub _require_applesingledouble
403 13 ▶{
Subroutine _require_applesingledouble: called CC=2 SCAR=8.1
404 13 my $this = shift;
405 13 ▶ if ($this->get_file_format() eq 'Plain')
Branch: 1 branch
BranchTrueFalseTotal
if ($this->get_file_format eq "Plain") 0 13 13
406 {
407 0 die "File '" . $this->{'_filename'} . "' is not in AppleSingle or AppleDouble format!";
408 }
409 }
410  
411 13 1;
412 __END__
↑ Summary