summaryrefslogtreecommitdiff
path: root/examples/disassemble_gif.pl
blob: 2a6cbb1e82085b5b04f178f5c0bc293155313ea6 (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
#!/usr/bin/perl
#
# Disassemble a GIF file and display the sections and chunks within.
#
# Warning: only part of the specification is implemented.
#

use strict;
use warnings;

die "Usage: $0 IMAGE\n" unless @ARGV == 1;
my ($image) = @ARGV;

open IMAGE, '<', $image or die "$0: open $image: $!\n";
undef $/;
my $gif = <IMAGE>;
close IMAGE;

print "$image: ", length $gif, " bytes\n\n";

my @gif = unpack 'C*', $gif;
my $z = 0;

output_chunk('Header', 6);
output_chunk('Logical Screen Descriptor', 7);

my $global_colors = $gif[10] & 0x80;
my $color_table_size = 2 << ($gif[10] & 0x07);

if ($global_colors) {
	output_chunk('Global Color Table', $color_table_size * 3);
}

while (1) {
	while ($gif[$z] == 0x21) {
		if ($gif[$z + 1] == 0xf9) {
			output_chunk('Graphic Control Extension', 5 + 2);
		} elsif ($gif[$z + 1] == 0xfe) {
			output_chunk('Comment Extension', 2);
		} elsif ($gif[$z + 1] == 0x01) {
			output_chunk('Plain Text Extension', 13 + 2);
		} elsif ($gif[$z + 1] == 0xff) {
			output_chunk('Application Extension', 12 + 2);
		} else {
			output_chunk((sprintf 'Unknown Extension 0x%.2x',
					$gif[$z + 1]), $gif[$z + 2] + 3);
		}
		
		while ($gif[$z] != 0) {
			output_chunk('Data Sub-block', $gif[$z] + 1);
		}
		output_chunk('Block Terminator', 1);
	}
	
	if ($gif[$z] == 0x3b) {
		output_chunk('Trailer', 1);
		last;
	}
	
	if ($gif[$z] != 0x2c) {
		last;
	}
	
	output_chunk('Image Descriptor', 10);
	
	output_chunk('Table Based Image Data', 1);

	while ($gif[$z] != 0) {
		output_chunk('Data Sub-block', $gif[$z] + 1);
	}
	output_chunk('Block Terminator', 1);
}

if ($z != @gif) {
	output_chunk('*** Junk on End ***', @gif - $z);
}


#
# Output a chunk of data as hex and characters.
#
sub output_chunk
{
	my ($description, $length) = @_;

	print "$description";
	for (my $i = 0; $i != $length; $i++) {
		if ($i % 8 == 0) {
			print "\n";
			printf "%8i:  ", $z + $i;
		}
		if ($z + $i == @gif) {
			print "EOF\n\n";
			print "Unexpected end of file\n";
			exit;
		}
		my $c = $gif[$z + $i];
		printf "%.2x ", $c;
		if (32 <= $c and $c <= 126) {
			printf "'%c'", $c;
		} else {
			print "   ";
		}
		print "   ";
	}
	print "\n\n";
	
	$z += $length;
}