summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/Win32/Exe/Resource/Version.pm
blob: 98adfbb9b220817d6ddf575c48cd40738d5850c5 (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# $File: //local/member/autrijus/Win32-Exe/lib/Win32/Exe/Resource/Version.pm $ $Author: autrijus $
# $Revision: #6 $ $Change: 1130 $ $Date: 2004-02-17T15:40:29.640821Z $

package Win32::Exe::Resource::Version;

use strict;
use base 'Win32::Exe::Resource';
use constant FORMAT => (
    Data		=> 'a*',
);
use constant FIXED_INFO => [qw(
    Signature StrucVersion FileVersionMS FileVersionLS
    ProductVersionMS ProductVersionLS FileFlagsMask FileFlags
    FileOS FileType FileSubtype FileDateMS FileDateLS
)];
use constant STRING_INFO => [qw(
    Comments CompanyName FileDescription FileVersion InternalName
    LegalCopyright LegalTrademarks OriginalFilename PrivateBuild
    ProductName ProductVersion SpecialBuild FileNumber ProductNumber
)];
use constant FI_TO_ID => {
    map { (FIXED_INFO->[$_] => $_) } (0 .. $#{+FIXED_INFO})
};
use constant LC_TO_SI => {
    (map { (lc($_) => $_) } @{+STRING_INFO}, keys %{+FI_TO_ID}),
    (map { (lc($_) => $_) } map { /^(.+)MS$/ ? $1 : () } keys %{+FI_TO_ID}),
};

sub fi_to_id {
    my ($self, $name) = @_;
    return(+FI_TO_ID->{$name});
}

sub lc_to_si {
    my ($self, $name) = @_;
    return(+LC_TO_SI->{lc($name)} || $name);
}

sub info {
    my ($self) = @_;
    return $self->{info};
}

sub set_info {
    my ($self, $info) = @_;
    $self->{info} = $info;
}

sub initialize {
    my ($self) = @_;
    $self->set_info($self->decode_info($self->Data));
    die 'Invalid structure' unless $self->check;
}

sub refresh {
    my ($self) = @_;
    $self->SetData($self->encode_info($self->info));
    my $rsrc = $self->first_parent('Resources');
    $rsrc->remove("/#RT_VERSION");
    $rsrc->insert("/#RT_VERSION/#1/#0" => $self);
    $rsrc->refresh;
    $self->initialize;
}

sub encode_info {
    my ($self, $info) = @_;

    my $key = shift(@$info);
    $key = $self->encode_ucs2("$key\0");

    my $val = shift(@$info);
    my ($type, $vallen);

    if (ref $val) {
	$type   = 0;                   # binary
	$val    = pack('V*', @$val);
	$vallen = length($val);
    }
    elsif (length $val) {
	$type   = 1;                          # text;
	$val    = $self->encode_ucs2("$val\0");
	$vallen = length($val) / 2;
    }
    else {
	$type   = 1;
	$vallen = 0;
    }

    my @sub_objects;
    foreach my $sub_info (@$info) {
	my $obj = $self->encode_info($sub_info);
	push(@sub_objects, $obj);
    }

    my $buf = pack('v3', 0, $vallen, $type) . $key;
    $buf .= $self->pad($buf, 4);
    $buf .= $val;

    foreach my $sub_object (@sub_objects) {
	$buf .= $self->pad($buf, 4);
	$buf .= $sub_object;
    }

    substr($buf, 0, 2, pack('v', length($buf)));
    return $buf;
}

sub decode_info {
    my $self  = shift;
    my $level = $_[1] || 1;

    my ($len, $vallen, $type) = unpack('v3', $_[0]);
    die 'No record length' unless $len;
    die 'Long length' if $len > length($_[0]);

    my $buf = substr($_[0], 0, $len);
    substr($_[0], 0, $self->align($len, 4)) = '';

    my $endkey = index($buf, "\0\0", 6);
    while ($endkey > 0 and ($endkey % 2)) {
	$endkey = index($buf, "\0\0", $endkey + 1);
    }

    die 'Invalid endkey' if $endkey < 6 or $endkey > $len - $vallen;;

    my $key    = substr($buf, 6, $endkey - 6);
    my $u8_key = $self->decode_ucs2($key);

    my @res = ($u8_key);
    $endkey = $self->align($endkey + 2, 4);
    substr($buf, 0, $endkey, '');

    if ($vallen) {
	$vallen *= 2 if $level == 4;    # only for strings
	my $val = substr($buf, 0, $vallen);
	if ($type) {
	    $val = $self->decode_ucs2($val);
	    $val =~ s/\0\z//;
	}
	else {
	    $val = [ unpack('V*', $val) ];
	}
	push(@res, $val);
	$vallen = $self->align($vallen, 4);

	substr($buf, 0, $vallen) = '';
    }
    else {
	push(@res, '');
    }

    while (length $buf) {
	push(@res, $self->decode_info($buf, $level + 1));
    }

    return \@res;
}

sub empty_info {
    [ 'VS_VERSION_INFO', [ 0xFEEF04BD, 1 << 16, (0) x 11 ] ];
}

sub check_info {
    my ($self, $info) = @_;
    return 0 unless $info->[0] eq 'VS_VERSION_INFO';
    return 0 unless ref($info->[1]);
    return 0 unless $info->[1][0] == 0xFEEF04BD;
    return 0 unless $self->check_sub_info($info);
    return 1;
}

sub check_sub_info {
    my ($self, $info) = @_;
    return unless UNIVERSAL::isa($info, 'ARRAY');
    return if @$info < 2;
    return unless defined($info->[0]) and defined($info->[1]);
    return unless !ref($info->[0]) and length($info->[0]);
    return unless !ref($info->[1]) or UNIVERSAL::isa($info->[1], 'ARRAY');
    foreach my $idx (2 .. @$info - 1) {
	return 0 unless $self->check_sub_info($info->[$idx]);
    }
    return 1;
}

sub get {
    my ($self, $name) = @_;
    $name =~ s!\\!/!g;
    $name = $self->lc_to_si($name);
    my $info = $self->info;

    if ($name eq '/') {
	return undef unless ref $info->[1];
	return $info->[1];
    }

    my $fixed = $self->fi_to_id($name);
    if (defined $fixed) {
	my $struct = $info->[1];
	return undef unless $struct && ref($struct);
	return $struct->[$fixed];
    }

    $fixed = $self->fi_to_id($name.'MS');
    if (defined $fixed) {
	my $struct = $info->[1];
	return undef unless $struct && ref($struct);
	my $ms = $struct->[$fixed];
	my $ls = $struct->[ $self->fi_to_id($name.'LS') ];
	return join(',', $self->split_dword($ms), $self->split_dword($ls));
    }

    my $s;
    if ($name =~ s!^/!!) {
	$s = $info;
	while ($name =~ s!^([^/]+)/!!) {
	    $s = $self->find_info($s, $1) or return undef;
	}
    }
    else {
	$s = $self->find_info($info, 'StringFileInfo') or return undef;
	if (my $cur_trans = $self->{cur_trans}) {
	    $s = $self->find_info($s, $cur_trans, 1) or return undef;
	}
	else {
	    $s = $s->[2] or return undef;
	    $self->{cur_trans} = $s->[0];
	}
    }

    $s = $self->find_info($s, $name) or return undef;
    return $s->[1];
}

sub set {
    my ($self, $name, $value) = @_;
    $name =~ s!\\!/!g;
    $name = $self->lc_to_si($name);
    my $info = $self->info;

    if ($name eq '/') {
	if (!defined $value) {
	    $info->[1] = '';
	}
	elsif (UNIVERSAL::isa($value, 'ARRAY') and @$value == 13) {
	    $info->[1] = $value;
	}
	else {
	    die 'Invalid array assigned';
	}
    }

    my $fixed = $self->fi_to_id($name);
    if (defined $fixed) {
	$value = oct($value) if $value =~ /^0/;
	$info->[1][$fixed] = $value;
	return;
    }

    $fixed = $self->fi_to_id($name.'MS');
    if (defined $fixed) {
	my @value = split(/[,.]/, $value, -1);
	if (@value == 4) {
	    $value[0] = ($value[0] << 16) | $value[1];
	    $value[1] = ($value[2] << 16) | $value[3];
	    splice(@value, 2);
	}

	die 'Invalid MS/LS value' if @value != 2;
	$info->[1][$fixed] = $value[0] || 0;
	$info->[1][$self->fi_to_id($name.'LS')] = $value[1] || 0;
	return;
    }

    my $container = $info;

    if ($name =~ s!^/!!) {
	while ($name =~ s!^([^/]+)/!!) {
	    my $n = $1;
	    my $s = $self->find_info($container, $n);
	    unless ($s) {
		$s = [ $n => '' ];
		push(@$container, $s);
	    }
	    $container = $s;
	}
    }
    else {
	my $s = $self->find_info($container, 'StringFileInfo');
	unless ($s) {
	    $s = [ StringFileInfo => '' ];
	    push(@$container, $s);
	}
	$container = $s;

	my $cur_trans = $self->{cur_trans};
	unless ($cur_trans) {
	    if (@$container > 2) {
		$cur_trans = $container->[2][0];
	    }
	    else {
		$cur_trans = '000004B0';    # Language Neutral && CP 1200 = Unicode
	    }
	    $self->{cur_trans} = $cur_trans;
	}

	$s = $self->find_info($container, $cur_trans, 1);
	unless ($s) {
	    $s = [ $cur_trans => '' ];
	    push(@$container, $s);
	}
	$container = $s;
    }

    my ($kv, $kv_index) = $self->find_info($container, $name);
    unless ($kv) {
	push(@$container, [ $name => $value ]) if defined $value;
	return;
    }

    if (defined $value) {
	$kv->[1] = $value;
    }
    else {
	splice(@$container, $kv_index, 1);
    }
}

sub check {
    my $self = shift;
    return $self->check_info($self->info);
}

sub find_info {
    my ($self, $info, $name, $ignore) = @_;
    my $index;

    if ($name =~ /^#(\d+)$/) {
	$index = $1 - 1 + 2;
	$index = undef if $index < 2 || $index >= @$info;
    }
    else {
	for (2 .. @$info - 1) {
	    my $e = $info->[$_];
	    if ($e->[0] eq $name or $ignore && lc($e->[0]) eq lc($name)) {
		$index = $_;
		last;
	    }
	}
    }
    if ($index) {
	return $info->[$index] unless wantarray;
	return ($info->[$index], $index);
    }

    return undef unless wantarray;
    return (undef, undef);
}

sub split_dword {
    my ($self, $dword) = @_;
    return ($dword >> 16), ($dword & 0xFFFF);
}

1;