forked from spicyjack/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmg2iso.pl
122 lines (113 loc) · 3.4 KB
/
dmg2iso.pl
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
#!/usr/local/bin/perl
# Changes:
#
# Tue Apr 26 15:49:53 EDT 2005 Jeff Mahoney <[email protected]>
# * read/writes now do so in 4k blocks rather than allocate
# huge chunks of memory
use MIME::Base64;
use strict ;
local ($^W) = 1; #use warnings ;
use Compress::Zlib ;
my $x = inflateInit()
or die "ERROR: Cannot create inflation stream.\n"
. "Is Compress::Zlib installed?\n" ;
my $zfblock="\x00"; for (0..8) { $zfblock.=$zfblock; }
my $indxbeg=0;
my $indxend=0;
my $blocksz = 4096;
my @plist;
print "dmg2iso v0.2a by vu1tur (vu1tur\@gmx.de)\n\n";
if (@ARGV."" != 2) { die "Syntax: dmg2iso.pl filename.dmg filename.iso\n"; }
my $zeroblock = "\x00";
for (0..8) { $zeroblock.=$zeroblock; }
my $tmp;
my ($output,$status);
my $buffer;
open(FINPUT,$ARGV[0]) or die "ERROR: Can't open input file\n";
binmode FINPUT;
sysseek(FINPUT,-0x200000,2);
print "reading property list...";
my $fpos = sysseek(FINPUT,0,1);
while(my $ar = sysread(FINPUT,$buffer,0x10000))
{
my $fpos = sysseek(FINPUT,0,1)-$ar;
if ($buffer =~ /(.*)<plist version=\"1.0\">/s)
{
$indxbeg = $fpos+length($1);
}
if ($buffer =~ /(.*)<\/plist>/s)
{
$indxend = $fpos+length($1)+8;
}
}
open(FOUTPUT,">".$ARGV[1]) or die "ERROR: Can't open output file\n";
binmode FOUTPUT;
my $indxcur = $indxbeg + 0x28;
sysseek(FINPUT,$indxbeg,0);
sysread(FINPUT,$tmp,$indxend-$indxbeg);
if ($tmp =~ s/.*<key>blkx<\/key>.*?\s*<array>(.*?)<\/array>.*/$1/s)
{
while ($tmp =~ s/.*?<data>(.*?)<\/data>//s)
{
my $t = $1;
$t =~ s/\t//g;
$t =~ s/^\n//g;
push @plist,decode_base64($t);
}
} else {
die "PropertyList is corrupted\n";
}
print "found ".@plist." partitions\n";
print "decompressing:\n";
my $t=0;
my $zoffs = 0;
my $tempzoffs = 0;
foreach (@plist)
{
print "partition ".$t++."\n";
s/^.{204}//s;
while (s/^(.{8})(.{8})(.{8})(.{8})(.{8})//s)
{
$x = inflateInit();
my $block_type = unpack("H*",$1);
my $out_offs = 0x200*hex(unpack("H*",$2));
my $out_size = 0x200*hex(unpack("H*",$3));
my $in_offs = hex(unpack("H*",$4));
my $in_size = hex(unpack("H*",$5));
# $1 - block type, $2 - output offs
# $3 - output size $4 input offset $5 - input size
sysseek(FINPUT,$in_offs+$zoffs,0);
if ($block_type =~ /^80000005/ or $block_type =~ /^00000001/)
{
do {
my ($toread, $res);
$toread = $blocksz;
$toread = $in_size if ($in_size < $blocksz);
$res = sysread (FINPUT, $tmp, $toread);
die "read failure" if ($res != $toread);
$output = $tmp;
# If compressed, inflate it
if ($block_type =~ /^80000005/) {
($output,$status) = $x->inflate($tmp);
die "\nConversion failed.File may be corrupted.\n"
if (!($status == Z_OK or $status == Z_STREAM_END));
}
print FOUTPUT $output;
$in_size -= $toread;
} while ($in_size > 0);
}
if ($block_type =~ /^00000002/)
{
for(1..$out_size/0x200)
{
syswrite(FOUTPUT,$zeroblock,0x200);
}
}
if ($block_type =~ /^FFFFFFFF/i)
{
$zoffs += $tempzoffs;
}
$tempzoffs = $in_offs+$in_size;
}
}
print "\nconversion successful\n";