blib/lib/AppleII/Disk.pm

filter: Statementstmt 96.6% 145 / 150 Branchbran 67.6% 46 / 68 Conditioncond 82.3% 14 / 17 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 96.7% 30 / 31 Podpod 100% 6 / 6 totaltotal 87.9% 248 / 282 SCAR 38.7
CC
44
Cov
87%
fully_allocate
20.0
new
9.1
pad_block
7.0
CRAP 48.0
« blib/lib/AppleII/ProDOS.pm ↑ Summary Build »
1 #---------------------------------------------------------------------
2 package AppleII::Disk;
3 #
4 # Copyright 1996-2006 Christopher J. Madsen
5 #
6 # Author: Christopher J. Madsen <perl@cjmweb.net>
7 # Created: 25 Jul 1996
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the same terms as Perl itself.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
15 # GNU General Public License or the Artistic License for more details.
16 #
17 # ABSTRACT: Block-level access to Apple II disk image files
18 #---------------------------------------------------------------------
19  
20 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 3 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 #=====================================================================
29 # Package Global Variables:
30  
31 3 our $VERSION = '0.09';
32  
33 #=====================================================================
34 # Class AppleII::Disk:
35 #
36 # Member Variables:
37 # filename: The pathname of the disk image file
38 # writable: True if the image is opened in read/write mode
39 # file: The IO::File attached to the image file
40 # actlen: The size of the image file in bytes
41 # maxlen: The maximum allowable size of the image file in bytes
42 #---------------------------------------------------------------------
43 # Constructor:
44 #
45 # Input:
46 # filename:
47 # The pathname of the image file you want to open
48 # mode:
49 # A string indicating how the image should be opened
50 # May contain any of the following characters (case sensitive):
51 # r Allow reads (this is actually ignored; you can always read)
52 # w Allow writes
53 # d Disk image is in DOS 3.3 order (default)
54 # p Disk image is in ProDOS order
55  
56 sub new
57 {
58 4 ▶ my ($type, $filename, $mode) = @_;
Subroutine new: called CC=9 SCAR=22.1
Pod documented
59 4 my $self = {};
60 4 $self->{filename} = $filename;
61  
62 4 my $file = IO::File->new;
63  
64 4 ▶ $mode = 'r' unless $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode 0 4 4
65 4 my $openMode = '<';
66 4 ▶ if ($mode =~ /w/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ /w/) 4 0 4
67 4 $self->{writable} = 1;
68 4 $openMode = '+<';
69 4 ▶ $openMode = '+>' if not -e $filename; # Create empty file
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 2 2 4
70 } # end if writable
71  
72 4 ▶ $file->open($filename, $openMode) or croak("Couldn't open `$filename': $!");
Branch: 1 branch
BranchTrueFalseTotal
$file->open($filename, $openMode) or croak("Couldn't open `${filename}': $!") 0 4 4
73 4 binmode $file; # binmode didn't become a method until IO::File 1.11
74  
75 4 $self->{file} = $file;
76 4 $self->{actlen} = ($file->stat)[7]; # Get real size of file
77 4 $self->{maxlen} = $self->{actlen};
78  
79 4 ▶ $type = 'AppleII::Disk::ProDOS' if $mode =~ /p/;
Branch: 1 branch
BranchTrueFalseTotal
if $mode =~ /p/ 2 2 4
80 4 ▶ $type = 'AppleII::Disk::DOS33' if $mode =~ /d/;
Branch: 1 branch
BranchTrueFalseTotal
if $mode =~ /d/ 1 3 4
81 4 ▶ $type = (($filename =~ /\.(?:hdv|po)$/i)
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "AppleII::Disk" 1 3 4
$filename =~ /\.(?:hdv|po)$/i ? : 1 0 1
82 ? 'AppleII::Disk::ProDOS'
83 : 'AppleII::Disk::DOS33')
84 if ($type eq 'AppleII::Disk');
85 4 bless $self, $type;
86 } # end AppleII::Disk::new
87  
88 #---------------------------------------------------------------------
89 # Pad a block of data:
90 #
91 # This is a normal subroutine, NOT a method!
92 #
93 # Input:
94 # data: The block to be padded
95 # pad: The character to pad with (default "\0") or '' for no padding
96 # length: The length to pad to (default 0x200)
97 #
98 # Returns:
99 # The BLOCK padded to LENGTH with PAD
100 # Dies if the block is too long.
101 # If PAD is the null string, dies if BLOCK is not already LENGTH.
102  
103 sub pad_block
104 {
105 32 ▶ my ($data, $pad, $length) = @_;
Subroutine pad_block: called CC=7 SCAR=19.5
Pod documented
106  
107 32 ▶ $pad = "\0" unless defined $pad;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pad 0 32 32
108 32 ▶ $length = $length || 0x200;
Condition100%2/2
$length || 512
l!l
1022
MC/DC100%1/1
$length || 512
$length
Truth table100%2/2
A
A= $length
Aresult
11
00
109  
110 32 ▶ $data .= $pad x ($length - length($data))
Branch: 1 branch
BranchTrueFalseTotal
if length $pad and length $data < $length 11 21 32
Condition100%3/3
length $pad and length $data < $length
!ll&&!rl&&r
111011
MC/DC100%2/2
length $pad and length $data < $length
length $pad length $data < $length
Truth table100%3/3
A and B
A= length $padB= length $data < $length
ABresult
0X0
100
111
111 if (length($pad) and length($data) < $length);
112  
113 32 ▶ unless (length($data) == $length) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $data == $length) 1 31 32
114 1 local $Carp::CarpLevel = $Carp::CarpLevel;
115 1 ▶ ++$Carp::CarpLevel if (caller)[0] =~ /^AppleII::Disk::/;
Branch: 1 branch
BranchTrueFalseTotal
if (caller)[0] =~ /^AppleII::Disk::/ 1 0 1
116 1 croak(sprintf("Data block is %d bytes",length($data)));
117 }
118  
119 31 $data;
120 } # end AppleII::Disk::pad_block
121  
122 #---------------------------------------------------------------------
123 # Get or set the disk size:
124 #
125 # Input:
126 # size: The number of blocks in the disk image
127 # If SIZE is omitted, the disk size is not changed
128 #
129 # Returns:
130 # The number of blocks in the disk image
131  
132 sub blocks
133 {
134 6 ▶ my $self = shift;
Subroutine blocks: called CC=3 SCAR=11.0
Pod documented
135  
136 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 3 6
137 3 $self->{maxlen} = $_[0] * 0x200;
138 carp "Disk image contains more than $_[0] blocks"
139 3 ▶ if $self->{maxlen} < $self->{actlen};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"maxlen"} < $self->{"actlen"} 0 3 3
140 }
141  
142 6 int($self->{maxlen} / 0x200);
143 } # end AppleII::Disk::blocks
144  
145 #---------------------------------------------------------------------
146 # Extend the image file to its full size:
147  
148 sub fully_allocate
149 {
150 0 ▶ my $self = shift;
Subroutine fully_allocate: not called CC=4 SCAR=30.0
Pod documented
151  
152 0 ▶ if ($self->{maxlen} > $self->{actlen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"maxlen"} > $self->{"actlen"}) 0 0 0
153 0 ▶ croak("Disk image is read/only") unless $self->{writable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writable"} 0 0 0
154  
155 0 ▶ $self->{file}->truncate($self->{maxlen}) or croak "Can't extend file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"file"}->truncate($self->{"maxlen"}) or croak("Can't extend file: $!") 0 0 0
156  
157 0 $self->{actlen} = $self->{maxlen};
158 } # end if file is not already at maximum size
159  
160 } # end AppleII::Disk::fully_allocate
161  
162 #---------------------------------------------------------------------
163 # Read a ProDOS block:
164 #
165 # Input:
166 # block: The block number to read
167 #
168 # Returns:
169 # A 512 byte block
170 #
171 # Implemented in AppleII::Disk::ProDOS & AppleII::Disk::DOS33
172 #
173 # sub read_block
174  
175 #---------------------------------------------------------------------
176 # Read a series of ProDOS blocks:
177 #
178 # As a special case, block 0 cannot be read by this method. Instead,
179 # it returns a block full of 0 bytes. This is how sparse files are
180 # implemented. If you want to read the actual contents of block 0,
181 # you must call $disk->read_block(0) directly.
182 #
183 # Input:
184 # blocks: An array of block numbers to read
185 #
186 # Returns:
187 # The data from the disk (512 bytes times the number of blocks)
188  
189 sub read_blocks
190 {
191 10 ▶ my ($self, $blocks) = @_;
Subroutine read_blocks: called CC=3 SCAR=11.0
Pod documented
192 10 my $data = '';
193 10 foreach (@$blocks) {
194 1044 ▶ if ($_) { $data .= $self->read_block($_) }
Branch: 1 branch
BranchTrueFalseTotal
if ($_) { } 24 1020 1044
195 1020 else { $data .= "\0" x 0x200 } # Sparse block
196 }
197 10 $data;
198 } # end AppleII::Disk::read_blocks
199  
200 #---------------------------------------------------------------------
201 # Read a DOS 3.3 sector:
202 #
203 # Input:
204 # track: The track number to read
205 # sector: The sector number to read
206 #
207 # Returns:
208 # A 256 byte sector
209 #
210 # Implemented in AppleII::Disk::ProDOS & AppleII::Disk::DOS33
211 #
212 # sub read_sector
213  
214 #---------------------------------------------------------------------
215 # Write a ProDOS block:
216 #
217 # Input:
218 # block: The block number to read
219 # data: The contents of the block
220 # pad: A character to pad the block with (optional)
221 # If PAD is omitted, an error is generated if data is not 512 bytes
222 #
223 # Implemented in AppleII::Disk::ProDOS & AppleII::Disk::DOS33
224 #
225 # sub write_block
226  
227 #---------------------------------------------------------------------
228 # Write a series of ProDOS blocks:
229 #
230 # As a special case, block 0 cannot be written by this method.
231 # Instead, that block is just skipped. This is how sparse files are
232 # implemented. If you want to write the contents of block 0, you must
233 # call $disk->write_block directly.
234 #
235 # Input:
236 # blocks: An array of the block numbers to write to
237 # data: The data to write (must be exactly the right size)
238 # pad: A character to pad the last block with (optional)
239  
240 sub write_blocks
241 {
242 5 ▶ my ($self, $blocks, $data, $pad) = @_;
Subroutine write_blocks: called CC=3 SCAR=11.0
Pod documented
243 5 my $index = 0;
244 5 foreach (@$blocks) {
245 271 ▶ $self->write_block($_, substr($data, $index, 0x200), $pad) if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 13 258 271
246 271 $index += 0x200;
247 }
248 } # end AppleII::Disk::write_blocks
249  
250 #---------------------------------------------------------------------
251 # Write a DOS 3.3 sector:
252 #
253 # Input:
254 # track: The track number to read
255 # sector: The sector number to read
256 # data: The contents of the sector
257 # pad: The value to pad the sector with (optional)
258 # If PAD is omitted, an error is generated if data is not 256 bytes
259 #
260 # Implemented in AppleII::Disk::ProDOS & AppleII::Disk::DOS33
261 #
262 # sub write_sector
263  
264 #=====================================================================
265 package AppleII::Disk::ProDOS;
266 #
267 # Handle ProDOS-order disk images
268 #---------------------------------------------------------------------
269  
270 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
271 3 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
272 3 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
273 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
274 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
275  
276 3 our @ISA = qw(AppleII::Disk);
277  
278 #---------------------------------------------------------------------
279 # Read a block from a ProDOS order disk:
280 #
281 # See AppleII::Disk::read_block
282  
283 sub read_block
284 {
285 56 ▶ my $self = shift;
Subroutine read_block: called CC=3 SCAR=11.0
286  
287 return "\0" x 0x200
288 56 ▶ if $self->seek_block($_[0]) >= $self->{actlen}; # Past EOF
Branch: 1 branch
BranchTrueFalseTotal
if $self->seek_block($_[0]) >= $self->{"actlen"} 1 54 55
289 54 my $buffer = '';
290 54 ▶ read($self->{file},$buffer,0x200) or die;
Branch: 1 branch
BranchTrueFalseTotal
read $self->{"file"}, $buffer, 512 or die 0 54 54
291  
292 54 $buffer;
293 } # end AppleII::Disk::ProDOS::read_block
294  
295 #---------------------------------------------------------------------
296 # FIXME AppleII::Disk::ProDOS::read_sector not implemented yet
297  
298 #---------------------------------------------------------------------
299 # Seek to the beginning of a block:
300 #
301 # Input:
302 # block: The block number to seek to
303 #
304 # Returns:
305 # The new position of the file pointer
306  
307 sub seek_block
308 {
309 76 ▶ my ($self, $block) = @_;
Subroutine seek_block: called CC=4 SCAR=14.0
310  
311 76 my $pos = $block * 0x200;
312 croak("Invalid block number $block")
313 76 ▶ if $pos < 0 or $pos >= $self->{maxlen};
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 or $pos >= $self->{"maxlen"} 1 75 76
Condition66%2/3
$pos < 0 or $pos >= $self->{"maxlen"}
l!l&&r!l&&!r
0175
MC/DC50%1/2
$pos < 0 or $pos >= $self->{"maxlen"}
$pos < 0 $pos >= $self->{"maxlen"}
Truth table66%2/3
A or B
A= $pos < 0B= $pos >= $self->{"maxlen"}
ABresult
1X1
011
000
314  
315 75 ▶ $self->{file}->seek($pos,0) or die;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"file"}->seek($pos, 0) or die 0 75 75
316  
317 75 $pos;
318 } # end AppleII::Disk::ProDOS::seek_block
319  
320 #---------------------------------------------------------------------
321 # Write a block from a ProDOS order disk:
322 #
323 # See AppleII::Disk::write_block
324  
325 sub write_block
326 {
327 21 ▶ my ($self, $block, $data, $pad) = @_;
Subroutine write_block: called CC=5 SCAR=16.2
328 21 ▶ croak("Disk image is read/only") unless $self->{writable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writable"} 0 21 21
329  
330 21 ▶ $data = AppleII::Disk::pad_block($data, $pad || '');
Condition100%2/2
$pad || ''
l!l
147
MC/DC100%1/1
$pad || ''
$pad
Truth table100%2/2
A
A= $pad
Aresult
11
00
331  
332 20 my $pos = $self->seek_block($block);
333 20 ▶ print {$self->{file}} $data or die;
Branch: 1 branch
BranchTrueFalseTotal
print {$self->{"file"};} $data or die 0 20 20
334  
335 20 ▶ $self->{actlen} = $pos + 0x200 unless $self->{actlen} > $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"actlen"} > $pos 1 19 20
336 } # end AppleII::Disk::ProDOS::write_block
337  
338 #=====================================================================
339 package AppleII::Disk::DOS33;
340 #
341 # Handle DOS 3.3-order disk images
342 #---------------------------------------------------------------------
343  
344 #$debug = 1;
345  
346 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
347 3 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
348 3 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
349 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
350 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
351  
352 3 our @ISA = qw(AppleII::Disk);
353  
354 #---------------------------------------------------------------------
355 # Convert ProDOS block number to track & sectors:
356  
357 3 { my @sector1 = ( 0, 13, 11, 9, 7, 5, 3, 1);
358 3 my @sector2 = (14, 12, 10, 8, 6, 4, 2, 15);
359  
360 sub block2sector
361 {
362 4 ▶ my $block = shift;
Subroutine block2sector: called CC=1 SCAR=0.0
363 4 my $offset = $block % 8;
364  
365 4 ($block/8, $sector1[$offset], $sector2[$offset]); # INTEGER division
366 } # end block2sector
367 }
368  
369 #---------------------------------------------------------------------
370 # Read a block from a DOS 3.3 order disk:
371 #
372 # See AppleII::Disk::read_block
373  
374 sub read_block
375 3 {
376 3 ▶ my ($self, $block) = @_;
Subroutine read_block: called CC=1 SCAR=0.0
377 3 my ($track, $sector1, $sector2) = block2sector($block);
378  
379 3 $self->read_sector($track,$sector1) . $self->read_sector($track,$sector2);
380 } # end AppleII::Disk::DOS33::read_block
381  
382 #---------------------------------------------------------------------
383 # Read a DOS 3.3 sector:
384 #
385 # See AppleII::Disk::read_sector
386  
387 sub read_sector
388 {
389 7 ▶ my $self = shift;
Subroutine read_sector: called CC=3 SCAR=11.0
390 return "\0" x 0x100
391 7 ▶ if $self->seek_sector(@_[0..1]) >= $self->{actlen}; # Past EOF
Branch: 1 branch
BranchTrueFalseTotal
if $self->seek_sector(@_[0, 1]) >= $self->{"actlen"} 2 4 6
392 4 my $buffer = '';
393 4 ▶ read($self->{file},$buffer,0x100) or die;
Branch: 1 branch
BranchTrueFalseTotal
read $self->{"file"}, $buffer, 256 or die 0 4 4
394  
395 4 $buffer;
396 } # end AppleII::Disk::DOS33::read_sector
397  
398 #---------------------------------------------------------------------
399 # Seek to the beginning of a sector:
400 #
401 # Input:
402 # track: The track number to seek to
403 # sector: The sector number to seek to
404 #
405 # Returns:
406 # The new position of the file pointer
407  
408 sub seek_sector
409 {
410 13 ▶ my ($self, $track, $sector) = @_;
Subroutine seek_sector: called CC=4 SCAR=14.0
411  
412 13 my $pos = $track * 0x1000 + $sector * 0x100;
413 croak("Invalid position track $track sector $sector")
414 13 ▶ if $pos < 0 or $pos >= $self->{maxlen};
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 or $pos >= $self->{"maxlen"} 1 12 13
Condition66%2/3
$pos < 0 or $pos >= $self->{"maxlen"}
l!l&&r!l&&!r
0112
MC/DC50%1/2
$pos < 0 or $pos >= $self->{"maxlen"}
$pos < 0 $pos >= $self->{"maxlen"}
Truth table66%2/3
A or B
A= $pos < 0B= $pos >= $self->{"maxlen"}
ABresult
1X1
011
000
415  
416 12 ▶ $self->{file}->seek($pos,0) or die;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"file"}->seek($pos, 0) or die 0 12 12
417 12 $pos;
418 } # end AppleII::Disk::DOS33::seek_sector
419  
420 #---------------------------------------------------------------------
421 # Write a sector to a DOS 3.3 order image:
422 #
423 # See AppleII::Disk::write_sector
424  
425 sub write_sector
426 {
427 6 ▶ my ($self, $track, $sector, $data, $pad) = @_;
Subroutine write_sector: called CC=5 SCAR=16.2
428 6 ▶ croak("Disk image is read/only") unless $self->{writable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writable"} 0 6 6
429  
430 6 ▶ $data = AppleII::Disk::pad_block($data, $pad || '', 0x100);
Condition100%2/2
$pad || ''
l!l
24
MC/DC100%1/1
$pad || ''
$pad
Truth table100%2/2
A
A= $pad
Aresult
11
00
431  
432 6 my $pos = $self->seek_sector($track, $sector);
433 6 ▶ print {$self->{file}} $data or die;
Branch: 1 branch
BranchTrueFalseTotal
print {$self->{"file"};} $data or die 0 6 6
434  
435 6 ▶ $self->{actlen} = $pos + 0x100 unless $self->{actlen} > $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"actlen"} > $pos 2 4 6
436 } # end AppleII::Disk::DOS33::write_sector
437  
438 #---------------------------------------------------------------------
439 # Write a block to a DOS33 order disk:
440 #
441 # See AppleII::Disk::write_block
442  
443 sub write_block
444 {
445 1 ▶ my ($self, $block, $data, $pad) = @_;
Subroutine write_block: called CC=3 SCAR=11.2
446 1 ▶ croak("Disk image is read/only") unless $self->{writable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writable"} 0 1 1
447 1 my ($track, $sector1, $sector2) = block2sector($block);
448  
449 1 ▶ $data = AppleII::Disk::pad_block($data, $pad || '');
Condition50%1/2
$pad || ''
l!l
10
MC/DC0%0/1
$pad || ''
$pad
Truth table50%1/2
A
A= $pad
Aresult
11
00
450  
451 1 $self->write_sector($track, $sector1, substr($data,0,0x100));
452 1 $self->write_sector($track, $sector2, substr($data,0x100,0x100));
453 } # end AppleII::Disk::DOS33::write_block
454  
455 #=====================================================================
456 # Package Return Value:
457  
458 3 1;
459  
460 __END__
« blib/lib/AppleII/ProDOS.pm ↑ Summary Build »