Move 3 ASN1 dissectors to 'clean' group; move 1 PIDL dissector to 'dirty' group.
[metze/wireshark/wip.git] / epan / dissectors / packet-image-png.c
1 /* packet-image-png.c
2  *
3  * Routines for PNG (Portable Network Graphics) image file dissection
4  *
5  * $Id$
6  *
7  * Copyright 2006 Ronnie Sahlberg
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
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.
17  *
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.
22  *
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.
26  */
27 /* See http://www.w3.org/TR/PNG for specification
28  */
29
30 #include "config.h"
31
32 #include <glib.h>
33 #include <epan/packet.h>
34
35
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;
68
69 static gint ett_png = -1;
70 static gint ett_png_chunk = -1;
71 static gint ett_png_chunk_item = -1;
72
73
74 static const value_string colour_type_vals[] = {
75         { 0,    "Greyscale"},
76         { 2,    "Truecolour"},
77         { 3,    "Indexed-colour"},
78         { 4,    "Greyscale with alpha"},
79         { 6,    "Truecolour with alpha"},
80         { 0, NULL }
81 };
82 static const value_string compression_method_vals[] = {
83         { 0,    "Deflate"},
84         { 0, NULL }
85 };
86 static const value_string filter_method_vals[] = {
87         { 0,    "Adaptive"},
88         { 0, NULL }
89 };
90 static const value_string interlace_method_vals[] = {
91         { 0,    "No interlace"},
92         { 1,    "Adam7"},
93         { 0, NULL }
94 };
95
96 static void
97 dissect_png_ihdr(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
98 {
99         int offset=0;
100
101         proto_tree_add_item(tree, hf_png_ihdr_width, tvb, offset, 4, ENC_BIG_ENDIAN);
102         offset+=4;
103
104         proto_tree_add_item(tree, hf_png_ihdr_height, tvb, offset, 4, ENC_BIG_ENDIAN);
105         offset+=4;
106
107         proto_tree_add_item(tree, hf_png_ihdr_bitdepth, tvb, offset, 1, ENC_BIG_ENDIAN);
108         offset+=1;
109
110         proto_tree_add_item(tree, hf_png_ihdr_colour_type, tvb, offset, 1, ENC_BIG_ENDIAN);
111         offset+=1;
112
113         proto_tree_add_item(tree, hf_png_ihdr_compression_method, tvb, offset, 1, ENC_BIG_ENDIAN);
114         offset+=1;
115
116         proto_tree_add_item(tree, hf_png_ihdr_filter_method, tvb, offset, 1, ENC_BIG_ENDIAN);
117         offset+=1;
118
119         proto_tree_add_item(tree, hf_png_ihdr_interlace_method, tvb, offset, 1, ENC_BIG_ENDIAN);
120         offset+=1;
121
122         return;
123 }
124
125 static void
126 dissect_png_text(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
127 {
128         int offset=1;
129
130         /* find the null that separates keyword and text string */
131         while(1){
132                 if(!tvb_get_guint8(tvb, offset)){
133                         break;
134                 }
135                 offset++;
136         }
137
138         proto_tree_add_item(tree, hf_png_text_keyword, tvb, 0, offset, ENC_ASCII|ENC_NA);
139         offset++;
140
141         proto_tree_add_item(tree, hf_png_text_string, tvb, offset, tvb_length_remaining(tvb, offset), ENC_ASCII|ENC_NA);
142
143 }
144
145 static void
146 dissect_png_time(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
147 {
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);
154 }
155
156 static const value_string phys_unit_vals[] = {
157         { 0,    "Unit is unknown"},
158         { 1,    "Unit is METRE"},
159         { 0, NULL }
160 };
161 static void
162 dissect_png_phys(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
163 {
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);
167 }
168
169 static void
170 dissect_png_bkgd(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
171 {
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);
175                 break;
176         case 2: /* colour type 0, 4 */
177                 proto_tree_add_item(tree, hf_png_bkgd_greyscale, tvb, 0, 2, ENC_BIG_ENDIAN);
178                 break;
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);
183                 break;
184         }
185 }
186
187 typedef struct _chunk_dissector_t {
188         guint32 type;
189         const char *name;
190         void (*dissector)(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
191 } chunk_dissector_t;
192
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 */
202         { 0, NULL, NULL }
203 };
204
205 static const true_false_string png_chunk_anc = {
206         "This is an ANCILLARY chunk",
207         "This is a CRITICAL chunk"
208 };
209 static const true_false_string png_chunk_priv = {
210         "This is a PRIVATE chunk",
211         "This is a PUBLIC chunk"
212 };
213 static const true_false_string png_chunk_stc = {
214         "This chunk is SAFE TO COPY",
215         "This chunk is NOT safe to copy"
216 };
217
218
219 static gint
220 dissect_png(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void *data _U_)
221 {
222         proto_tree *tree = NULL;
223         proto_item *ti;
224         int offset=0;
225
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)
229                 return 0;
230
231         if (tvb_memeql(tvb, 0, magic, sizeof(magic)) != 0)
232                 return 0;
233
234         col_append_str(pinfo->cinfo, COL_INFO, " (PNG)");
235
236         if(parent_tree){
237                 ti=proto_tree_add_item(parent_tree, proto_png, tvb, offset, -1, ENC_NA);
238                 tree=proto_item_add_subtree(ti, ett_png);
239         }
240
241         proto_tree_add_item(tree, hf_png_signature, tvb, offset, 8, ENC_NA);
242         offset+=8;
243
244         while(tvb_reported_length_remaining(tvb, offset)){
245                 proto_tree *chunk_tree=NULL;
246                 proto_item *it=NULL;
247                 guint32 len, type;
248                 chunk_dissector_t *cd;
249                 char str[5];
250
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);
257                 str[4]=0;
258
259                 if(tree){
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);
262                 }
263
264                 proto_tree_add_item(chunk_tree, hf_png_chunk_len, tvb, offset, 4, ENC_BIG_ENDIAN);
265                 offset+=4;
266
267
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);
272                 offset+=4;
273
274                 if (len >= 1000000000)
275                         THROW(ReportedBoundsError);
276                 cd=&chunk_table[0];
277                 while(1){
278                         if(cd->type==0){
279                                 cd=NULL;
280                                 break;
281                         }
282                         if(cd->type==type){
283                                 break;
284                         }
285                         cd++;
286                 }
287                 if(chunk_tree){
288                         proto_item_append_text(chunk_tree, " %s", cd?cd->name:"(don't know how to dissect this)");
289                 }
290
291                 if(!cd){
292                         proto_tree_add_item(chunk_tree, hf_png_chunk_data, tvb, offset, len, ENC_NA);
293                 } else {
294                         if(cd->dissector){
295                                 tvbuff_t *next_tvb;
296                                 proto_tree *cti=NULL;
297
298                                 next_tvb=tvb_new_subset(tvb, offset, MIN(tvb_length_remaining(tvb, offset), (int)len), len);
299                                 if(it){
300                                         cti=proto_item_add_subtree(it, ett_png_chunk_item);
301                                 }
302                                 cd->dissector(next_tvb, pinfo, cti);
303                         }
304                 }
305                 offset+=len;
306
307                 proto_tree_add_item(chunk_tree, hf_png_chunk_crc, tvb, offset, 4, ENC_BIG_ENDIAN);
308                 offset+=4;
309         }
310         return offset;
311 }
312
313 void
314 proto_register_png(void)
315 {
316         static hf_register_info hf[] =
317         {
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 }},
411         };
412
413         static gint *ett[] =
414         {
415                 &ett_png,
416                 &ett_png_chunk,
417                 &ett_png_chunk_item,
418         };
419
420
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);
425 }
426
427 static gboolean dissect_png_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
428 {
429         return dissect_png(tvb, pinfo, tree, NULL) > 0;
430 }
431
432 void
433 proto_reg_handoff_png(void)
434 {
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);
438 }