-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMacbinary.pm
305 lines (224 loc) · 7.1 KB
/
Macbinary.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# Mac::Macbinary, updated by Jeremiah Morris because of horrible bugs
package Mac::Macbinary;
use strict;
use vars qw($VERSION $AUTOLOAD);
$VERSION = 0.07;
use Carp ();
sub new {
my($class, $thingy, $attr) = @_;
my $self = bless {
validate => $attr->{validate},
}, $class;
my $fh = _make_handle($thingy);
$self->_parse_handle($fh);
return $self;
}
sub _parse_handle {
my $self = shift;
my($fh) = @_;
read $fh, my ($header), 128;
$self->{header} = Mac::Macbinary::Header->new($header, {
validate => $self->{validate},
});
read $fh, $self->{data}, $self->header->dflen;
my $resourceoffset = 128 - (($self->header->dflen) % 128);
# don't eat a chunk if we came out equal (say, when there's no data fork)
$resourceoffset = 0 if $resourceoffset == 128;
read $fh, my($tmp), $resourceoffset;
read $fh, $self->{resource}, $self->header->rflen;
return $self;
}
sub _make_handle($) {
my $thingy = shift;
if (! ref($thingy) && -f $thingy) {
require FileHandle;
my $fh = FileHandle->new($thingy) or Carp::croak "$thingy: $!";
return $fh;
} else {
# tries to read it
eval {
read $thingy, my($tmp), 0;
};
if ($@) {
Carp::croak "Can't read $thingy!";
}
return $thingy;
}
}
sub AUTOLOAD {
my $self = shift;
$AUTOLOAD =~ s/.*://o;
return $self->{$AUTOLOAD};
}
package Mac::Macbinary::Header;
use vars qw($AUTOLOAD);
sub new {
my($class, $h, $attr) = @_;
my $self = bless { }, $class;
if ($attr->{validate}) {
$self->_validate_header($h)
or Carp::croak "Macbinary validation failed.";
}
$self->_parse_header($h);
return $self;
}
sub _validate_header {
my $self = shift;
my($h) = @_;
# stolen from Mac::Conversions
#
# Use a crude heuristic to decide whether or not a file is MacBinary. The
# first byte of any MacBinary file must be zero. The second has to be
# <= 63 according to the MacBinary II standard. The 122nd and 123rd
# each have to be >= 129. This has about a 1/8000 chance of failing on
# random bytes. This seems to be all that mcvert does. Unfortunately
# we can't also check the checksum because the standard software (Stuffit
# Deluxe, etc.) doesn't seem to checksum.
my($zero,
$namelength,
$filename,
$type,
$creator,
$highflag,
$dum1,
$dum2,
$dum3,
$datalength,
$reslength,
$dum4,
$dum5,
$dum6,
$lowflag,
$dum7,
$dum8,
$version_this,
$version_needed,
$crc) = unpack("CCA63a4a4CxNnCxNNNNnCx14NnCCN", $h);
return (!$zero && (($namelength - 1)< 63)
&& $version_this >= 129 && $version_needed >= 129);
}
sub _parse_header {
my $self = shift;
my($h) = @_;
my $namelen = unpack("C", substr($h, 1, 1));
$self->{name} = substr(unpack("a*", substr($h, 2, 63)), 0, $namelen);
$self->{type} = unpack("a*", substr($h, 65, 4));
$self->{creator} = unpack("a*", substr($h, 69, 4));
$self->{flags} = unpack("C", substr($h, 73, 1));
# $self->{location} = unpack("C", substr($h, 80, 6)); # oh, come on...
$self->{dflen} = unpack("N", substr($h, 83, 4));
$self->{rflen} = unpack("N", substr($h, 87, 4));
$self->{cdate} = unpack("N", substr($h, 91, 4));
$self->{mdate} = unpack("N", substr($h, 95, 4));
return $self;
}
sub AUTOLOAD {
my $self = shift;
$AUTOLOAD =~ s/.*://o;
return $self->{$AUTOLOAD};
}
1;
__END__
=head1 NAME
Mac::Macbinary - Decodes Macbinary files
=head1 SYNOPSIS
use Mac::Macbinary;
$mb = Mac::Macbinary->new(\*FH); # filehandle
$mb = Mac::Macbinary->new($fh); # IO::* instance
$mb = Mac::Macbinary->new("/path/to/file");
# do validation
eval {
$mb = Mac::Macbinary->new("/path/to/file", { validate => 1 });
};
$header = $mb->header; # Mac::Macbinary::Header instance
$name = $header->name;
=head1 DESCRIPTION
This module provides an object-oriented way to extract various kinds
of information from Macintosh Macbinary files.
=head1 METHODS
Following methods are available.
=head2 Class method
=over 4
=item new( THINGY, [ \%attr ] )
Constructor of Mac::Macbinary. Accepts filhandle GLOB reference,
FileHandle instance, IO::* instance, or whatever objects that can do
C<read> methods.
If the argument belongs none of those above, C<new()> treats it as a
path to file. Any of following examples are valid constructors.
open FH, "path/to/file";
$mb = Mac::Macbinary->new(\*FH);
$fh = FileHandle->new("path/to/file");
$mb = Mac::Macbinary->new($fh);
$io = IO::File->new("path/to/file");
$mb = Mac::Macbinary->new($io);
$mb = Mac::Macbinary->new("path/to/file");
C<new()> throws an exception "Can't read blahblah" if the given
argument to the constructor is neither a valid filehandle nor an
existing file.
The optional L<\%attr> parameter can be used for validation of file
format. You can check and see if a file is really a Macbinary or not
by setting "validate" attribute to 1.
$fh = FileHandle->new("path/to/file");
eval {
$mb = Mac::Macbinary->new(FileHandle->new($fh), {
validate => 1,
});
};
if ($@) {
warn "file is not a Macbinary.";
}
=back
=head2 Instance Method
=over 4
=item data
returns the data range of original file.
=item header
returns the header object (instance of Mac::Macbinary::Header).
=back
Following accessors are available via Mac::Macbinary::Header instance.
=over 4
=item name, type, creator, flags, location, dflen, rflen, cdate, mdate
returns the original entry in the header of Macbinary file.
Below is a structure of the info file, taken from MacBin.C
char zero1;
char nlen;
char name[63];
char type[4]; 65 0101
char creator[4]; 69
char flags; 73
char zero2; 74 0112
char location[6]; 80
char protected; 81 0121
char zero3; 82 0122
char dflen[4];
char rflen[4];
char cdate[4];
char mdate[4];
=back
=head1 EXAMPLE
Some versions of MSIE for Macintosh sends their local files as
Macbinary format via forms. You can decode them in a following way:
use CGI;
use Mac::Macbinary;
$q = new CGI;
$filename = $q->param('uploaded_file');
$type = $q->uploadInfo($filename)->{'Content-Type'};
if ($type eq 'application/x-macbinary') {
$mb = Mac::Macbinary->new($q->upload('uploaded_file'));
# now, you can get data via $mb->data;
}
=head1 COPYRIGHT
Copyright 2000 Tatsuhiko Miyagawa <miyagawa@bulknews.net>
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 ACKNOWLEDGEMENT
Macbinary.pm is originally written by Dan Kogai <dankogai@dan.co.jp>.
There are also C<Mac::Conversions> and C<Convert::BinHex>, working
kind similar to this module. (However, C<Mac::Conversions> works only
on MacPerl, and C<Convert::BinHex> is now deprecated.) Many thanks to
Paul J. Schinder and Eryq, authors of those ones.
Macbinary validation is almost a replication of B<is_macbinary> in
Mac::Conversions.
=head1 SEE ALSO
perl(1), L<Mac::Conversions>, L<Convert::BinHex>.
=cut