aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-image-png.c
blob: d2669ba9c302ff4be446da46464afd7261d973ac (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* packet-image-png.c
 *
 * Routines for PNG (Portable Network Graphics) image file dissection
 *
 * $Id$
 *
 * Copyright 2006 Ronnie Sahlberg
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
/* See http://www.w3.org/TR/PNG for specification
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <glib.h>
#include <epan/packet.h>


static int proto_png = -1;
static int hf_png_signature = -1;
static int hf_png_chunk_data = -1;
static int hf_png_chunk_type = -1;
static int hf_png_chunk_len = -1;
static int hf_png_chunk_crc = -1;
static int hf_png_chunk_flag_anc = -1;
static int hf_png_chunk_flag_priv = -1;
static int hf_png_chunk_flag_stc = -1;
static int hf_png_ihdr_width = -1;
static int hf_png_ihdr_height = -1;
static int hf_png_ihdr_bitdepth = -1;
static int hf_png_ihdr_colour_type = -1;
static int hf_png_ihdr_compression_method = -1;
static int hf_png_ihdr_filter_method = -1;
static int hf_png_ihdr_interlace_method = -1;
static int hf_png_text_keyword = -1;
static int hf_png_text_string = -1;
static int hf_png_time_year = -1;
static int hf_png_time_month = -1;
static int hf_png_time_day = -1;
static int hf_png_time_hour = -1;
static int hf_png_time_minute = -1;
static int hf_png_time_second = -1;
static int hf_png_phys_horiz = -1;
static int hf_png_phys_vert = -1;
static int hf_png_phys_unit = -1;
static int hf_png_bkgd_palette_index = -1;
static int hf_png_bkgd_greyscale = -1;
static int hf_png_bkgd_red = -1;
static int hf_png_bkgd_green = -1;
static int hf_png_bkgd_blue = -1;

static gint ett_png = -1;
static gint ett_png_chunk = -1;
static gint ett_png_chunk_item = -1;


static const value_string colour_type_vals[] = {
	{ 0,	"Greyscale"},
	{ 2,	"Truecolour"},
	{ 3,	"Indexed-colour"},
	{ 4,	"Greyscale with alpha"},
	{ 6,	"Truecolour with alpha"},
	{ 0, NULL }
};
static const value_string compression_method_vals[] = {
	{ 0,	"Deflate"},
	{ 0, NULL }
};
static const value_string filter_method_vals[] = {
	{ 0,	"Adaptive"},
	{ 0, NULL }
};
static const value_string interlace_method_vals[] = {
	{ 0,	"No interlace"},
	{ 1,	"Adam7"},
	{ 0, NULL }
};

static void
dissect_png_ihdr(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
{
	int offset=0;

	proto_tree_add_item(tree, hf_png_ihdr_width, tvb, offset, 4, FALSE);
	offset+=4;

	proto_tree_add_item(tree, hf_png_ihdr_height, tvb, offset, 4, FALSE);
	offset+=4;

	proto_tree_add_item(tree, hf_png_ihdr_bitdepth, tvb, offset, 1, FALSE);
	offset+=1;

	proto_tree_add_item(tree, hf_png_ihdr_colour_type, tvb, offset, 1, FALSE);
	offset+=1;

	proto_tree_add_item(tree, hf_png_ihdr_compression_method, tvb, offset, 1, FALSE);
	offset+=1;

	proto_tree_add_item(tree, hf_png_ihdr_filter_method, tvb, offset, 1, FALSE);
	offset+=1;

	proto_tree_add_item(tree, hf_png_ihdr_interlace_method, tvb, offset, 1, FALSE);
	offset+=1;

	return;
}

static void
dissect_png_text(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
{
	int offset=1;

	/* find the null that separates keyword and text string */
	while(1){
		if(!tvb_get_guint8(tvb, offset)){
			break;
		}
		offset++;
	}

	proto_tree_add_item(tree, hf_png_text_keyword, tvb, 0, offset, FALSE);
	offset++;

	proto_tree_add_item(tree, hf_png_text_string, tvb, offset, tvb_length_remaining(tvb, offset), FALSE);

}

static void
dissect_png_time(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
{
	proto_tree_add_item(tree, hf_png_time_year, tvb, 0, 2, FALSE);
	proto_tree_add_item(tree, hf_png_time_month, tvb, 2, 1, FALSE);
	proto_tree_add_item(tree, hf_png_time_day, tvb, 3, 1, FALSE);
	proto_tree_add_item(tree, hf_png_time_hour, tvb, 4, 1, FALSE);
	proto_tree_add_item(tree, hf_png_time_minute, tvb, 5, 1, FALSE);
	proto_tree_add_item(tree, hf_png_time_second, tvb, 6, 1, FALSE);
}

static const value_string phys_unit_vals[] = {
	{ 0,	"Unit is unknown"},
	{ 1,	"Unit is METRE"},
	{ 0, NULL }
};
static void
dissect_png_phys(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
{
	proto_tree_add_item(tree, hf_png_phys_horiz, tvb, 0, 4, FALSE);
	proto_tree_add_item(tree, hf_png_phys_vert, tvb, 4, 4, FALSE);
	proto_tree_add_item(tree, hf_png_phys_unit, tvb, 8, 1, FALSE);
}

static void
dissect_png_bkgd(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
{
	switch(tvb_reported_length(tvb)){
	case 1: /* colour type 3 */
		proto_tree_add_item(tree, hf_png_bkgd_palette_index, tvb, 0, 1, FALSE);
		break;
	case 2: /* colour type 0, 4 */
		proto_tree_add_item(tree, hf_png_bkgd_greyscale, tvb, 0, 2, FALSE);
		break;
	case 6: /* colour type 2, 6 */
		proto_tree_add_item(tree, hf_png_bkgd_red, tvb, 0, 2, FALSE);
		proto_tree_add_item(tree, hf_png_bkgd_green, tvb, 2, 2, FALSE);
		proto_tree_add_item(tree, hf_png_bkgd_blue, tvb, 4, 2, FALSE);
		break;
	}
}

typedef struct _chunk_dissector_t {
	guint32 type;
	char *name;
	void (*dissector)(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
} chunk_dissector_t;

static chunk_dissector_t chunk_table[] = {
	{ 0x49484452, "Image Header", dissect_png_ihdr }, 	/* IHDR */
	{ 0x624b4744, "Background colour", dissect_png_bkgd }, 	/* bKGD */
	{ 0x70485973, "Physical pixel dimensions",
					dissect_png_phys }, 	/* pHYs */
	{ 0x74455874, "Textual data", dissect_png_text }, 	/* tEXt */
	{ 0x74494d45, "Image last-modification time",
					dissect_png_time }, 	/* tIME */
	{ 0x49454e44, "Image Trailer", NULL }, 			/* IEND */
	{ 0, NULL, NULL }
};

static const true_false_string png_chunk_anc = {
	"This is an ANCILLARY chunk",
	"This is a CRITICAL chunk"
};
static const true_false_string png_chunk_priv = {
	"This is a PRIVATE chunk",
	"This is a PUBLIC chunk"
};
static const true_false_string png_chunk_stc = {
	"This chunk is SAFE TO COPY",
	"This chunk is NOT safe to copy"
};


static void
dissect_png(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
{
	proto_tree *tree = NULL;
	proto_item *ti;
	int offset=0;

	if (check_col(pinfo->cinfo, COL_INFO))
		col_append_str(pinfo->cinfo, COL_INFO, " (PNG)");

	if(parent_tree){
		ti=proto_tree_add_item(parent_tree, proto_png, tvb, offset, -1, FALSE);
		tree=proto_item_add_subtree(ti, ett_png);
	}

	proto_tree_add_item(tree, hf_png_signature, tvb, offset, 8, FALSE);
	offset+=8;

	while(tvb_reported_length_remaining(tvb, offset)){
		proto_tree *chunk_tree=NULL;
		proto_item *it=NULL;
		guint32 len, type;
		chunk_dissector_t *cd;
		char str[5];

		len=tvb_get_ntohl(tvb, offset);
		type=tvb_get_ntohl(tvb, offset+4);
		str[0]=tvb_get_guint8(tvb, offset+4);
		str[1]=tvb_get_guint8(tvb, offset+5);
		str[2]=tvb_get_guint8(tvb, offset+6);
		str[3]=tvb_get_guint8(tvb, offset+7);
		str[4]=0;

		if(tree){
			it=proto_tree_add_text(tree, tvb, offset, offset+8+len+4, "%s", str);
			chunk_tree=proto_item_add_subtree(it, ett_png_chunk);
		}

		proto_tree_add_item(chunk_tree, hf_png_chunk_len, tvb, offset, 4, FALSE);
		offset+=4;


		it=proto_tree_add_item(chunk_tree, hf_png_chunk_type, tvb, offset, 4, FALSE);
		proto_tree_add_item(chunk_tree, hf_png_chunk_flag_anc, tvb, offset, 4, FALSE);
		proto_tree_add_item(chunk_tree, hf_png_chunk_flag_priv, tvb, offset, 4, FALSE);
		proto_tree_add_item(chunk_tree, hf_png_chunk_flag_stc, tvb, offset, 4, FALSE);
		offset+=4;

		if (len >= 1000000000)
			THROW(ReportedBoundsError);
		cd=&chunk_table[0];
		while(1){
			if(cd->type==0){
				cd=NULL;
				break;
			}
			if(cd->type==type){
				break;
			}
			cd++;
		}
		if(chunk_tree){
			proto_item_append_text(chunk_tree, " %s", cd?cd->name:"(dont know how to dissect this)");
		}

		if(!cd){
			proto_tree_add_item(chunk_tree, hf_png_chunk_data, tvb, offset, len, FALSE);
		} else {
			if(cd->dissector){
				tvbuff_t *next_tvb;
				proto_tree *cti=NULL;

				next_tvb=tvb_new_subset(tvb, offset, MIN(tvb_length_remaining(tvb, offset), (int)len), len);
				if(it){
					cti=proto_item_add_subtree(it, ett_png_chunk_item);
				}
				cd->dissector(next_tvb, pinfo, cti);
			}
		}
		offset+=len;

		proto_tree_add_item(chunk_tree, hf_png_chunk_crc, tvb, offset, 4, FALSE);
		offset+=4;
	}
}

void
proto_register_png(void)
{
	static hf_register_info hf[] =
	{
	{ &hf_png_signature, {
	  "PNG Signature", "png.signature", FT_BYTES, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_chunk_type, {
	  "Chunk", "png.chunk.type", FT_STRING, BASE_NONE,
	  NULL, 0, "", HFILL }},
	{ &hf_png_chunk_data, {
	  "Data", "png.chunk.data", FT_NONE, BASE_NONE,
	  NULL, 0, "", HFILL }},
	{ &hf_png_chunk_len, {
	  "Len", "png.chunk.len", FT_UINT32, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_chunk_crc, {
	  "CRC", "png.chunk.crc", FT_UINT32, BASE_HEX,
	  NULL, 0, "", HFILL }},
	{ &hf_png_chunk_flag_anc, {
	  "Ancillary", "png.chunk.flag.ancillary", FT_BOOLEAN, 32,
	  TFS(&png_chunk_anc), 0x20000000, "", HFILL }},
	{ &hf_png_chunk_flag_priv, {
	  "Private", "png.chunk.flag.private", FT_BOOLEAN, 32,
	  TFS(&png_chunk_priv), 0x00200000, "", HFILL }},
	{ &hf_png_chunk_flag_stc, {
	  "Safe To Copy", "png.chunk.flag.stc", FT_BOOLEAN, 32,
	  TFS(&png_chunk_stc), 0x00000020, "", HFILL }},
	{ &hf_png_ihdr_width, {
	  "Width", "png.ihdr.width", FT_UINT32, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_ihdr_height, {
	  "Height", "png.ihdr.height", FT_UINT32, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_ihdr_bitdepth, {
	  "Bit Depth", "png.ihdr.bitdepth", FT_UINT8, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_ihdr_colour_type, {
	  "Colour Type", "png.ihdr.colour_type", FT_UINT8, BASE_DEC,
	  VALS(colour_type_vals), 0, "", HFILL }},
	{ &hf_png_ihdr_compression_method, {
	  "Compression Method", "png.ihdr.compression_method", FT_UINT8, BASE_DEC,
	  VALS(compression_method_vals), 0, "", HFILL }},
	{ &hf_png_ihdr_filter_method, {
	  "Filter Method", "png.ihdr.filter_method", FT_UINT8, BASE_DEC,
	  VALS(filter_method_vals), 0, "", HFILL }},
	{ &hf_png_ihdr_interlace_method, {
	  "Interlace Method", "png.ihdr.interlace_method", FT_UINT8, BASE_DEC,
	  VALS(interlace_method_vals), 0, "", HFILL }},
	{ &hf_png_text_keyword, {
	  "Keyword", "png.text.keyword", FT_STRING, BASE_NONE,
	  NULL, 0, "", HFILL }},
	{ &hf_png_text_string, {
	  "String", "png.text.string", FT_STRING, BASE_NONE,
	  NULL, 0, "", HFILL }},
	{ &hf_png_time_year, {
	  "Year", "png.time.year", FT_UINT16, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_time_month, {
	  "Month", "png.time.month", FT_UINT8, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_time_day, {
	  "Day", "png.time.day", FT_UINT8, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_time_hour, {
	  "Hour", "png.time.hour", FT_UINT8, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_time_minute, {
	  "Minute", "png.time.minute", FT_UINT8, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_time_second, {
	  "Second", "png.time.second", FT_UINT8, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_phys_horiz, {
	  "Horizontal pixels per unit", "png.phys.horiz", FT_UINT32, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_phys_vert, {
	  "Vertical pixels per unit", "png.phys.vert", FT_UINT32, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_phys_unit, {
	  "Unit", "png.phys.unit", FT_UINT8, BASE_DEC,
	  VALS(phys_unit_vals), 0, "", HFILL }},
	{ &hf_png_bkgd_palette_index, {
	  "Palette Index", "png.bkgd.palette_index", FT_UINT8, BASE_DEC,
	  NULL, 0, "", HFILL }},
	{ &hf_png_bkgd_greyscale, {
	  "Greyscale", "png.bkgd.greyscale", FT_UINT16, BASE_HEX,
	  NULL, 0, "", HFILL }},
	{ &hf_png_bkgd_red, {
	  "Red", "png.bkgd.red", FT_UINT16, BASE_HEX,
	  NULL, 0, "", HFILL }},
	{ &hf_png_bkgd_green, {
	  "Green", "png.bkgd.green", FT_UINT16, BASE_HEX,
	  NULL, 0, "", HFILL }},
	{ &hf_png_bkgd_blue, {
	  "Blue", "png.bkgd.blue", FT_UINT16, BASE_HEX,
	  NULL, 0, "", HFILL }},
	};

	static gint *ett[] =
	{
		&ett_png,
		&ett_png_chunk,
		&ett_png_chunk_item,
	};


	proto_png = proto_register_protocol("Portable Network Graphics","PNG","png");
	proto_register_field_array(proto_png, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
}

void
proto_reg_handoff_png(void)
{
	dissector_handle_t png_handle;

	png_handle = create_dissector_handle(dissect_png, proto_png);
	dissector_add_string("media_type", "image/png", png_handle);
}