File Coverage

blib/lib/Archive/ByteBoozer2.pm
Criterion Covered Total %
statement 45 47 95.7
branch 7 8 87.5
condition 5 6 83.3
subroutine 12 12 100.0
pod 3 3 100.0
total 72 76 94.7


line stmt bran cond sub pod time code
1             package Archive::ByteBoozer2;
2              
3             =head1 NAME
4              
5             Archive::ByteBoozer2 - Perl interface to David Malmborg's C, a data cruncher for Commodore files
6              
7             =head1 SYNOPSIS
8              
9             use Archive::ByteBoozer2 qw(:all);
10              
11             # Crunch file:
12             crunch($file_name);
13              
14             # Crunch file and make executable with start address $xxxx:
15             ecrunch($file_name, $address);
16              
17             # Crunch file and relocate data to hex address $xxxx:
18             rcrunch($file_name, $address);
19              
20             =head1 DESCRIPTION
21              
22             David Malmborg's C is a data cruncher for Commodore files written in C. C is very much the same as C, but it generates smaller files and decrunches at about 2x the speed. An additional effort was put into keeping the encoder at about the same speed as before. Obviously it is incompatible with the version C<1.0>.
23              
24             In Perl the following operations are implemented via C package:
25              
26             =over
27              
28             =item *
29             Compressing a file.
30              
31             =item *
32             Compressing a file and making an executable with start address C<$xxxx>.
33              
34             =item *
35             Compressing a file and relocating data to hex address C<$xxxx>.
36              
37             =back
38              
39             Compressed data is by default written into a file named with C<.b2> suffix. Target file must not exist. If you want an executable, use C. If you want to decrunch yourself, use C or C. The decruncher should be called with C and C registers loaded with a hi- and lo-byte address of the crunched file in a memory.
40              
41             =head1 METHODS
42              
43             =cut
44              
45 3     3   14641 use bytes;
  3         23  
  3         11  
46 3     3   65 use strict;
  3         3  
  3         43  
47 3     3   1331 use utf8;
  3         25  
  3         11  
48 3     3   66 use warnings;
  3         4  
  3         79  
49              
50 3     3   21 use base qw(Exporter);
  3         4  
  3         1997  
51             our %EXPORT_TAGS = ();
52             $EXPORT_TAGS{crunch} = [ qw(&crunch) ];
53             $EXPORT_TAGS{ecrunch} = [ qw(&ecrunch) ];
54             $EXPORT_TAGS{rcrunch} = [ qw(&rcrunch) ];
55             $EXPORT_TAGS{all} = [ @{$EXPORT_TAGS{crunch}}, @{$EXPORT_TAGS{ecrunch}}, @{$EXPORT_TAGS{rcrunch}} ];
56             our @EXPORT_OK = @{ $EXPORT_TAGS{all} };
57             our @EXPORT = qw();
58              
59             our $VERSION = '0.03';
60              
61             require XSLoader;
62             XSLoader::load(__PACKAGE__, $VERSION);
63              
64             =head2 crunch
65              
66             Crunch file:
67              
68             crunch($file_name);
69              
70             =cut
71              
72             sub crunch {
73 3     3 1 463 my ($file_name) = @_;
74              
75 3         7 _crunch($file_name, 0, 0, 0);
76             }
77              
78             =head2 ecrunch
79              
80             Crunch file and make executable with start address C<$xxxx>:
81              
82             ecrunch($file_name, $address);
83              
84             =cut
85              
86             sub ecrunch {
87 5     5 1 1100 my ($file_name, $address) = @_;
88              
89 5         7 _crunch($file_name, $address, 1, 0);
90             }
91              
92             =head2 rcrunch
93              
94             Crunch file and relocate data to hex address C<$xxxx>:
95              
96             rcrunch($file_name, $address);
97              
98             =cut
99              
100             sub rcrunch {
101 1     1 1 477 my ($file_name, $address) = @_;
102              
103 1         3 _crunch($file_name, $address, 0, 1);
104             }
105              
106             sub _crunch {
107 9     9   10 my ($file_name, $address, $is_executable, $is_relocated) = @_;
108              
109 9 100 66     70 unless ($address =~ m/^\d+$/ && $address >= 0x0000 && $address <= 0xffff) {
      100        
110 3         21 die qq{Don't understand, aborting...};
111             }
112              
113 6         10 my $file = _read_file($file_name);
114 5         11 my $bb_file = _crunch_file($file, $address, $is_executable, $is_relocated);
115 5         10 _write_file($bb_file, $file);
116              
117 4         117 printf qq{B2: "%s" -> "%s"\n}, file_name($file), file_name($bb_file);
118              
119 4         33 free_file($file, $bb_file);
120             }
121              
122             sub _read_file {
123 6     6   6 my ($file_name) = @_;
124              
125 6         24 my $file = alloc_file();
126 6 100       269 unless (read_file($file, $file_name)) {
127 1         4 free_file($file);
128 1         13 die qq{Error: Open file "$file_name" failed, aborting...};
129             }
130              
131 5         14 return $file;
132             }
133              
134             sub _crunch_file {
135 5     5   7 my ($file, $address, $is_executable, $is_relocated) = @_;
136              
137 5         13 my $bb_file = alloc_file();
138 5 50       1663 unless (crunch_file($file, $bb_file, $address, $is_executable, $is_relocated)) {
139 0         0 free_file($file, $bb_file);
140 0         0 die qq{Error: Crunch data failed, aborting...};
141             }
142              
143 5         8 return $bb_file;
144             }
145              
146             sub _write_file {
147 5     5   6 my ($bb_file, $file) = @_;
148              
149 5         12 my $file_name = file_name($file);
150 5 100       375 unless (write_file($bb_file, $file_name)) {
151 1         3 my $file_name = file_name($bb_file);
152 1         4 free_file($file, $bb_file);
153 1         13 die qq{Error: Write file "$file_name" failed, aborting...};
154             }
155             }
156              
157             =head1 BUGS
158              
159             There are no known bugs at the moment. Please report any bugs or feature requests.
160              
161             =head1 EXPORT
162              
163             C exports nothing by default.
164              
165             You are allowed to explicitly import the C, C, and C subroutines into the caller's namespace either by specifying their names in the import list (C, C, C) or by using the module with the C<:all> tag.
166              
167             =head1 SEE ALSO
168              
169             L
170              
171             =head1 AUTHOR
172              
173             Pawel Krol, Edjgruby@gmail.comE.
174              
175             =head1 VERSION
176              
177             Version 0.03 (2016-03-31)
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             C cruncher/decruncher:
182              
183             Copyright (C) 2016 David Malmborg.
184              
185             C Perl interface:
186              
187             Copyright (C) 2016 by Pawel Krol.
188              
189             This library is free open source software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
190              
191             PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND!
192              
193             =cut
194              
195             1;