3 * Routines for PNG (Portable Network Graphics) image file dissection
7 * Copyright 2006 Ronnie Sahlberg
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 /* See http://www.w3.org/TR/PNG for specification
33 #include <epan/packet.h>
36 static int proto_png = -1;
37 static int hf_png_signature = -1;
38 static int hf_png_chunk_data = -1;
39 static int hf_png_chunk_type = -1;
40 static int hf_png_chunk_len = -1;
41 static int hf_png_chunk_crc = -1;
42 static int hf_png_chunk_flag_anc = -1;
43 static int hf_png_chunk_flag_priv = -1;
44 static int hf_png_chunk_flag_stc = -1;
45 static int hf_png_ihdr_width = -1;
46 static int hf_png_ihdr_height = -1;
47 static int hf_png_ihdr_bitdepth = -1;
48 static int hf_png_ihdr_colour_type = -1;
49 static int hf_png_ihdr_compression_method = -1;
50 static int hf_png_ihdr_filter_method = -1;
51 static int hf_png_ihdr_interlace_method = -1;
52 static int hf_png_text_keyword = -1;
53 static int hf_png_text_string = -1;
54 static int hf_png_time_year = -1;
55 static int hf_png_time_month = -1;
56 static int hf_png_time_day = -1;
57 static int hf_png_time_hour = -1;
58 static int hf_png_time_minute = -1;
59 static int hf_png_time_second = -1;
60 static int hf_png_phys_horiz = -1;
61 static int hf_png_phys_vert = -1;
62 static int hf_png_phys_unit = -1;
63 static int hf_png_bkgd_palette_index = -1;
64 static int hf_png_bkgd_greyscale = -1;
65 static int hf_png_bkgd_red = -1;
66 static int hf_png_bkgd_green = -1;
67 static int hf_png_bkgd_blue = -1;
69 static gint ett_png = -1;
70 static gint ett_png_chunk = -1;
71 static gint ett_png_chunk_item = -1;
74 static const value_string colour_type_vals[] = {
77 { 3, "Indexed-colour"},
78 { 4, "Greyscale with alpha"},
79 { 6, "Truecolour with alpha"},
82 static const value_string compression_method_vals[] = {
86 static const value_string filter_method_vals[] = {
90 static const value_string interlace_method_vals[] = {
97 dissect_png_ihdr(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
101 proto_tree_add_item(tree, hf_png_ihdr_width, tvb, offset, 4, ENC_BIG_ENDIAN);
104 proto_tree_add_item(tree, hf_png_ihdr_height, tvb, offset, 4, ENC_BIG_ENDIAN);
107 proto_tree_add_item(tree, hf_png_ihdr_bitdepth, tvb, offset, 1, ENC_BIG_ENDIAN);
110 proto_tree_add_item(tree, hf_png_ihdr_colour_type, tvb, offset, 1, ENC_BIG_ENDIAN);
113 proto_tree_add_item(tree, hf_png_ihdr_compression_method, tvb, offset, 1, ENC_BIG_ENDIAN);
116 proto_tree_add_item(tree, hf_png_ihdr_filter_method, tvb, offset, 1, ENC_BIG_ENDIAN);
119 proto_tree_add_item(tree, hf_png_ihdr_interlace_method, tvb, offset, 1, ENC_BIG_ENDIAN);
126 dissect_png_text(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
130 /* find the null that separates keyword and text string */
132 if(!tvb_get_guint8(tvb, offset)){
138 proto_tree_add_item(tree, hf_png_text_keyword, tvb, 0, offset, ENC_ASCII|ENC_NA);
141 proto_tree_add_item(tree, hf_png_text_string, tvb, offset, tvb_length_remaining(tvb, offset), ENC_ASCII|ENC_NA);
146 dissect_png_time(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
148 proto_tree_add_item(tree, hf_png_time_year, tvb, 0, 2, ENC_BIG_ENDIAN);
149 proto_tree_add_item(tree, hf_png_time_month, tvb, 2, 1, ENC_BIG_ENDIAN);
150 proto_tree_add_item(tree, hf_png_time_day, tvb, 3, 1, ENC_BIG_ENDIAN);
151 proto_tree_add_item(tree, hf_png_time_hour, tvb, 4, 1, ENC_BIG_ENDIAN);
152 proto_tree_add_item(tree, hf_png_time_minute, tvb, 5, 1, ENC_BIG_ENDIAN);
153 proto_tree_add_item(tree, hf_png_time_second, tvb, 6, 1, ENC_BIG_ENDIAN);
156 static const value_string phys_unit_vals[] = {
157 { 0, "Unit is unknown"},
158 { 1, "Unit is METRE"},
162 dissect_png_phys(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
164 proto_tree_add_item(tree, hf_png_phys_horiz, tvb, 0, 4, ENC_BIG_ENDIAN);
165 proto_tree_add_item(tree, hf_png_phys_vert, tvb, 4, 4, ENC_BIG_ENDIAN);
166 proto_tree_add_item(tree, hf_png_phys_unit, tvb, 8, 1, ENC_BIG_ENDIAN);
170 dissect_png_bkgd(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
172 switch(tvb_reported_length(tvb)){
173 case 1: /* colour type 3 */
174 proto_tree_add_item(tree, hf_png_bkgd_palette_index, tvb, 0, 1, ENC_BIG_ENDIAN);
176 case 2: /* colour type 0, 4 */
177 proto_tree_add_item(tree, hf_png_bkgd_greyscale, tvb, 0, 2, ENC_BIG_ENDIAN);
179 case 6: /* colour type 2, 6 */
180 proto_tree_add_item(tree, hf_png_bkgd_red, tvb, 0, 2, ENC_BIG_ENDIAN);
181 proto_tree_add_item(tree, hf_png_bkgd_green, tvb, 2, 2, ENC_BIG_ENDIAN);
182 proto_tree_add_item(tree, hf_png_bkgd_blue, tvb, 4, 2, ENC_BIG_ENDIAN);
187 typedef struct _chunk_dissector_t {
190 void (*dissector)(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
193 static chunk_dissector_t chunk_table[] = {
194 { 0x49484452, "Image Header", dissect_png_ihdr }, /* IHDR */
195 { 0x624b4744, "Background colour", dissect_png_bkgd }, /* bKGD */
196 { 0x70485973, "Physical pixel dimensions",
197 dissect_png_phys }, /* pHYs */
198 { 0x74455874, "Textual data", dissect_png_text }, /* tEXt */
199 { 0x74494d45, "Image last-modification time",
200 dissect_png_time }, /* tIME */
201 { 0x49454e44, "Image Trailer", NULL }, /* IEND */
205 static const true_false_string png_chunk_anc = {
206 "This is an ANCILLARY chunk",
207 "This is a CRITICAL chunk"
209 static const true_false_string png_chunk_priv = {
210 "This is a PRIVATE chunk",
211 "This is a PUBLIC chunk"
213 static const true_false_string png_chunk_stc = {
214 "This chunk is SAFE TO COPY",
215 "This chunk is NOT safe to copy"
220 dissect_png(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void *data _U_)
222 proto_tree *tree = NULL;
226 /* http://libpng.org/pub/png/spec/1.2/PNG-Structure.html#PNG-file-signature */
227 static const guint8 magic[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
228 if (tvb_length(tvb) < 20)
231 if (tvb_memeql(tvb, 0, magic, sizeof(magic)) != 0)
234 col_append_str(pinfo->cinfo, COL_INFO, " (PNG)");
237 ti=proto_tree_add_item(parent_tree, proto_png, tvb, offset, -1, ENC_NA);
238 tree=proto_item_add_subtree(ti, ett_png);
241 proto_tree_add_item(tree, hf_png_signature, tvb, offset, 8, ENC_NA);
244 while(tvb_reported_length_remaining(tvb, offset)){
245 proto_tree *chunk_tree=NULL;
248 chunk_dissector_t *cd;
251 len=tvb_get_ntohl(tvb, offset);
252 type=tvb_get_ntohl(tvb, offset+4);
253 str[0]=tvb_get_guint8(tvb, offset+4);
254 str[1]=tvb_get_guint8(tvb, offset+5);
255 str[2]=tvb_get_guint8(tvb, offset+6);
256 str[3]=tvb_get_guint8(tvb, offset+7);
260 it=proto_tree_add_text(tree, tvb, offset, offset+8+len+4, "%s", str);
261 chunk_tree=proto_item_add_subtree(it, ett_png_chunk);
264 proto_tree_add_item(chunk_tree, hf_png_chunk_len, tvb, offset, 4, ENC_BIG_ENDIAN);
268 it=proto_tree_add_item(chunk_tree, hf_png_chunk_type, tvb, offset, 4, ENC_ASCII|ENC_NA);
269 proto_tree_add_item(chunk_tree, hf_png_chunk_flag_anc, tvb, offset, 4, ENC_BIG_ENDIAN);
270 proto_tree_add_item(chunk_tree, hf_png_chunk_flag_priv, tvb, offset, 4, ENC_BIG_ENDIAN);
271 proto_tree_add_item(chunk_tree, hf_png_chunk_flag_stc, tvb, offset, 4, ENC_BIG_ENDIAN);
274 if (len >= 1000000000)
275 THROW(ReportedBoundsError);
288 proto_item_append_text(chunk_tree, " %s", cd?cd->name:"(don't know how to dissect this)");
292 proto_tree_add_item(chunk_tree, hf_png_chunk_data, tvb, offset, len, ENC_NA);
296 proto_tree *cti=NULL;
298 next_tvb=tvb_new_subset(tvb, offset, MIN(tvb_length_remaining(tvb, offset), (int)len), len);
300 cti=proto_item_add_subtree(it, ett_png_chunk_item);
302 cd->dissector(next_tvb, pinfo, cti);
307 proto_tree_add_item(chunk_tree, hf_png_chunk_crc, tvb, offset, 4, ENC_BIG_ENDIAN);
314 proto_register_png(void)
316 static hf_register_info hf[] =
318 { &hf_png_signature, {
319 "PNG Signature", "png.signature", FT_BYTES, BASE_NONE,
320 NULL, 0, NULL, HFILL }},
321 { &hf_png_chunk_type, {
322 "Chunk", "png.chunk.type", FT_STRING, BASE_NONE,
323 NULL, 0, NULL, HFILL }},
324 { &hf_png_chunk_data, {
325 "Data", "png.chunk.data", FT_NONE, BASE_NONE,
326 NULL, 0, NULL, HFILL }},
327 { &hf_png_chunk_len, {
328 "Len", "png.chunk.len", FT_UINT32, BASE_DEC,
329 NULL, 0, NULL, HFILL }},
330 { &hf_png_chunk_crc, {
331 "CRC", "png.chunk.crc", FT_UINT32, BASE_HEX,
332 NULL, 0, NULL, HFILL }},
333 { &hf_png_chunk_flag_anc, {
334 "Ancillary", "png.chunk.flag.ancillary", FT_BOOLEAN, 32,
335 TFS(&png_chunk_anc), 0x20000000, NULL, HFILL }},
336 { &hf_png_chunk_flag_priv, {
337 "Private", "png.chunk.flag.private", FT_BOOLEAN, 32,
338 TFS(&png_chunk_priv), 0x00200000, NULL, HFILL }},
339 { &hf_png_chunk_flag_stc, {
340 "Safe To Copy", "png.chunk.flag.stc", FT_BOOLEAN, 32,
341 TFS(&png_chunk_stc), 0x00000020, NULL, HFILL }},
342 { &hf_png_ihdr_width, {
343 "Width", "png.ihdr.width", FT_UINT32, BASE_DEC,
344 NULL, 0, NULL, HFILL }},
345 { &hf_png_ihdr_height, {
346 "Height", "png.ihdr.height", FT_UINT32, BASE_DEC,
347 NULL, 0, NULL, HFILL }},
348 { &hf_png_ihdr_bitdepth, {
349 "Bit Depth", "png.ihdr.bitdepth", FT_UINT8, BASE_DEC,
350 NULL, 0, NULL, HFILL }},
351 { &hf_png_ihdr_colour_type, {
352 "Colour Type", "png.ihdr.colour_type", FT_UINT8, BASE_DEC,
353 VALS(colour_type_vals), 0, NULL, HFILL }},
354 { &hf_png_ihdr_compression_method, {
355 "Compression Method", "png.ihdr.compression_method", FT_UINT8, BASE_DEC,
356 VALS(compression_method_vals), 0, NULL, HFILL }},
357 { &hf_png_ihdr_filter_method, {
358 "Filter Method", "png.ihdr.filter_method", FT_UINT8, BASE_DEC,
359 VALS(filter_method_vals), 0, NULL, HFILL }},
360 { &hf_png_ihdr_interlace_method, {
361 "Interlace Method", "png.ihdr.interlace_method", FT_UINT8, BASE_DEC,
362 VALS(interlace_method_vals), 0, NULL, HFILL }},
363 { &hf_png_text_keyword, {
364 "Keyword", "png.text.keyword", FT_STRING, BASE_NONE,
365 NULL, 0, NULL, HFILL }},
366 { &hf_png_text_string, {
367 "String", "png.text.string", FT_STRING, BASE_NONE,
368 NULL, 0, NULL, HFILL }},
369 { &hf_png_time_year, {
370 "Year", "png.time.year", FT_UINT16, BASE_DEC,
371 NULL, 0, NULL, HFILL }},
372 { &hf_png_time_month, {
373 "Month", "png.time.month", FT_UINT8, BASE_DEC,
374 NULL, 0, NULL, HFILL }},
375 { &hf_png_time_day, {
376 "Day", "png.time.day", FT_UINT8, BASE_DEC,
377 NULL, 0, NULL, HFILL }},
378 { &hf_png_time_hour, {
379 "Hour", "png.time.hour", FT_UINT8, BASE_DEC,
380 NULL, 0, NULL, HFILL }},
381 { &hf_png_time_minute, {
382 "Minute", "png.time.minute", FT_UINT8, BASE_DEC,
383 NULL, 0, NULL, HFILL }},
384 { &hf_png_time_second, {
385 "Second", "png.time.second", FT_UINT8, BASE_DEC,
386 NULL, 0, NULL, HFILL }},
387 { &hf_png_phys_horiz, {
388 "Horizontal pixels per unit", "png.phys.horiz", FT_UINT32, BASE_DEC,
389 NULL, 0, NULL, HFILL }},
390 { &hf_png_phys_vert, {
391 "Vertical pixels per unit", "png.phys.vert", FT_UINT32, BASE_DEC,
392 NULL, 0, NULL, HFILL }},
393 { &hf_png_phys_unit, {
394 "Unit", "png.phys.unit", FT_UINT8, BASE_DEC,
395 VALS(phys_unit_vals), 0, NULL, HFILL }},
396 { &hf_png_bkgd_palette_index, {
397 "Palette Index", "png.bkgd.palette_index", FT_UINT8, BASE_DEC,
398 NULL, 0, NULL, HFILL }},
399 { &hf_png_bkgd_greyscale, {
400 "Greyscale", "png.bkgd.greyscale", FT_UINT16, BASE_HEX,
401 NULL, 0, NULL, HFILL }},
402 { &hf_png_bkgd_red, {
403 "Red", "png.bkgd.red", FT_UINT16, BASE_HEX,
404 NULL, 0, NULL, HFILL }},
405 { &hf_png_bkgd_green, {
406 "Green", "png.bkgd.green", FT_UINT16, BASE_HEX,
407 NULL, 0, NULL, HFILL }},
408 { &hf_png_bkgd_blue, {
409 "Blue", "png.bkgd.blue", FT_UINT16, BASE_HEX,
410 NULL, 0, NULL, HFILL }},
421 proto_png = proto_register_protocol("Portable Network Graphics","PNG","png");
422 proto_register_field_array(proto_png, hf, array_length(hf));
423 proto_register_subtree_array(ett, array_length(ett));
424 new_register_dissector("png", dissect_png, proto_png);
427 static gboolean dissect_png_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
429 return dissect_png(tvb, pinfo, tree, NULL) > 0;
433 proto_reg_handoff_png(void)
435 dissector_handle_t png_handle = new_create_dissector_handle(dissect_png, proto_png);
436 dissector_add_string("media_type", "image/png", png_handle);
437 heur_dissector_add("http", dissect_png_heur, proto_png);