* Prefer col_append_str instead of col_append_fstr for constant strings
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27 /* See http://www.w3.org/TR/PNG for specification
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <glib.h>
35 #include <epan/packet.h>
36
37
38 static int proto_png = -1;
39 static int hf_png_signature = -1;
40 static int hf_png_chunk_data = -1;
41 static int hf_png_chunk_type = -1;
42 static int hf_png_chunk_len = -1;
43 static int hf_png_chunk_crc = -1;
44 static int hf_png_chunk_flag_anc = -1;
45 static int hf_png_chunk_flag_priv = -1;
46 static int hf_png_chunk_flag_stc = -1;
47 static int hf_png_ihdr_width = -1;
48 static int hf_png_ihdr_height = -1;
49 static int hf_png_ihdr_bitdepth = -1;
50 static int hf_png_ihdr_colour_type = -1;
51 static int hf_png_ihdr_compression_method = -1;
52 static int hf_png_ihdr_filter_method = -1;
53 static int hf_png_ihdr_interlace_method = -1;
54 static int hf_png_text_keyword = -1;
55 static int hf_png_text_string = -1;
56 static int hf_png_time_year = -1;
57 static int hf_png_time_month = -1;
58 static int hf_png_time_day = -1;
59 static int hf_png_time_hour = -1;
60 static int hf_png_time_minute = -1;
61 static int hf_png_time_second = -1;
62 static int hf_png_phys_horiz = -1;
63 static int hf_png_phys_vert = -1;
64 static int hf_png_phys_unit = -1;
65 static int hf_png_bkgd_palette_index = -1;
66 static int hf_png_bkgd_greyscale = -1;
67 static int hf_png_bkgd_red = -1;
68 static int hf_png_bkgd_green = -1;
69 static int hf_png_bkgd_blue = -1;
70
71 static gint ett_png = -1;
72 static gint ett_png_chunk = -1;
73 static gint ett_png_chunk_item = -1;
74
75
76 static const value_string colour_type_vals[] = {
77         { 0,    "Greyscale"},
78         { 2,    "Truecolour"},
79         { 3,    "Indexed-colour"},
80         { 4,    "Greyscale with alpha"},
81         { 6,    "Truecolour with alpha"},
82         { 0, NULL }
83 };
84 static const value_string compression_method_vals[] = {
85         { 0,    "Deflate"},
86         { 0, NULL }
87 };
88 static const value_string filter_method_vals[] = {
89         { 0,    "Adaptive"},
90         { 0, NULL }
91 };
92 static const value_string interlace_method_vals[] = {
93         { 0,    "No interlace"},
94         { 1,    "Adam7"},
95         { 0, NULL }
96 };
97
98 static void
99 dissect_png_ihdr(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
100 {
101         int offset=0;
102
103         proto_tree_add_item(tree, hf_png_ihdr_width, tvb, offset, 4, FALSE);
104         offset+=4;
105
106         proto_tree_add_item(tree, hf_png_ihdr_height, tvb, offset, 4, FALSE);
107         offset+=4;
108
109         proto_tree_add_item(tree, hf_png_ihdr_bitdepth, tvb, offset, 1, FALSE);
110         offset+=1;
111
112         proto_tree_add_item(tree, hf_png_ihdr_colour_type, tvb, offset, 1, FALSE);
113         offset+=1;
114
115         proto_tree_add_item(tree, hf_png_ihdr_compression_method, tvb, offset, 1, FALSE);
116         offset+=1;
117
118         proto_tree_add_item(tree, hf_png_ihdr_filter_method, tvb, offset, 1, FALSE);
119         offset+=1;
120
121         proto_tree_add_item(tree, hf_png_ihdr_interlace_method, tvb, offset, 1, FALSE);
122         offset+=1;
123
124         return;
125 }
126
127 static void
128 dissect_png_text(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
129 {
130         int offset=1;
131
132         /* find the null that separates keyword and text string */
133         while(1){
134                 if(!tvb_get_guint8(tvb, offset)){
135                         break;
136                 }
137                 offset++;
138         }
139
140         proto_tree_add_item(tree, hf_png_text_keyword, tvb, 0, offset, FALSE);
141         offset++;
142
143         proto_tree_add_item(tree, hf_png_text_string, tvb, offset, tvb_length_remaining(tvb, offset), FALSE);
144
145 }
146
147 static void
148 dissect_png_time(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
149 {
150         proto_tree_add_item(tree, hf_png_time_year, tvb, 0, 2, FALSE);
151         proto_tree_add_item(tree, hf_png_time_month, tvb, 2, 1, FALSE);
152         proto_tree_add_item(tree, hf_png_time_day, tvb, 3, 1, FALSE);
153         proto_tree_add_item(tree, hf_png_time_hour, tvb, 4, 1, FALSE);
154         proto_tree_add_item(tree, hf_png_time_minute, tvb, 5, 1, FALSE);
155         proto_tree_add_item(tree, hf_png_time_second, tvb, 6, 1, FALSE);
156 }
157
158 static const value_string phys_unit_vals[] = {
159         { 0,    "Unit is unknown"},
160         { 1,    "Unit is METRE"},
161         { 0, NULL }
162 };
163 static void
164 dissect_png_phys(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
165 {
166         proto_tree_add_item(tree, hf_png_phys_horiz, tvb, 0, 4, FALSE);
167         proto_tree_add_item(tree, hf_png_phys_vert, tvb, 4, 4, FALSE);
168         proto_tree_add_item(tree, hf_png_phys_unit, tvb, 8, 1, FALSE);
169 }
170
171 static void
172 dissect_png_bkgd(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
173 {
174         switch(tvb_reported_length(tvb)){
175         case 1: /* colour type 3 */
176                 proto_tree_add_item(tree, hf_png_bkgd_palette_index, tvb, 0, 1, FALSE);
177                 break;
178         case 2: /* colour type 0, 4 */
179                 proto_tree_add_item(tree, hf_png_bkgd_greyscale, tvb, 0, 2, FALSE);
180                 break;
181         case 6: /* colour type 2, 6 */
182                 proto_tree_add_item(tree, hf_png_bkgd_red, tvb, 0, 2, FALSE);
183                 proto_tree_add_item(tree, hf_png_bkgd_green, tvb, 2, 2, FALSE);
184                 proto_tree_add_item(tree, hf_png_bkgd_blue, tvb, 4, 2, FALSE);
185                 break;
186         }
187 }
188
189 typedef struct _chunk_dissector_t {
190         guint32 type;
191         const char *name;
192         void (*dissector)(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
193 } chunk_dissector_t;
194
195 static chunk_dissector_t chunk_table[] = {
196         { 0x49484452, "Image Header", dissect_png_ihdr },       /* IHDR */
197         { 0x624b4744, "Background colour", dissect_png_bkgd },  /* bKGD */
198         { 0x70485973, "Physical pixel dimensions",
199                                         dissect_png_phys },     /* pHYs */
200         { 0x74455874, "Textual data", dissect_png_text },       /* tEXt */
201         { 0x74494d45, "Image last-modification time",
202                                         dissect_png_time },     /* tIME */
203         { 0x49454e44, "Image Trailer", NULL },                  /* IEND */
204         { 0, NULL, NULL }
205 };
206
207 static const true_false_string png_chunk_anc = {
208         "This is an ANCILLARY chunk",
209         "This is a CRITICAL chunk"
210 };
211 static const true_false_string png_chunk_priv = {
212         "This is a PRIVATE chunk",
213         "This is a PUBLIC chunk"
214 };
215 static const true_false_string png_chunk_stc = {
216         "This chunk is SAFE TO COPY",
217         "This chunk is NOT safe to copy"
218 };
219
220
221 static void
222 dissect_png(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
223 {
224         proto_tree *tree = NULL;
225         proto_item *ti;
226         int offset=0;
227
228         col_append_str(pinfo->cinfo, COL_INFO, " (PNG)");
229
230         if(parent_tree){
231                 ti=proto_tree_add_item(parent_tree, proto_png, tvb, offset, -1, FALSE);
232                 tree=proto_item_add_subtree(ti, ett_png);
233         }
234
235         proto_tree_add_item(tree, hf_png_signature, tvb, offset, 8, FALSE);
236         offset+=8;
237
238         while(tvb_reported_length_remaining(tvb, offset)){
239                 proto_tree *chunk_tree=NULL;
240                 proto_item *it=NULL;
241                 guint32 len, type;
242                 chunk_dissector_t *cd;
243                 char str[5];
244
245                 len=tvb_get_ntohl(tvb, offset);
246                 type=tvb_get_ntohl(tvb, offset+4);
247                 str[0]=tvb_get_guint8(tvb, offset+4);
248                 str[1]=tvb_get_guint8(tvb, offset+5);
249                 str[2]=tvb_get_guint8(tvb, offset+6);
250                 str[3]=tvb_get_guint8(tvb, offset+7);
251                 str[4]=0;
252
253                 if(tree){
254                         it=proto_tree_add_text(tree, tvb, offset, offset+8+len+4, "%s", str);
255                         chunk_tree=proto_item_add_subtree(it, ett_png_chunk);
256                 }
257
258                 proto_tree_add_item(chunk_tree, hf_png_chunk_len, tvb, offset, 4, FALSE);
259                 offset+=4;
260
261
262                 it=proto_tree_add_item(chunk_tree, hf_png_chunk_type, tvb, offset, 4, FALSE);
263                 proto_tree_add_item(chunk_tree, hf_png_chunk_flag_anc, tvb, offset, 4, FALSE);
264                 proto_tree_add_item(chunk_tree, hf_png_chunk_flag_priv, tvb, offset, 4, FALSE);
265                 proto_tree_add_item(chunk_tree, hf_png_chunk_flag_stc, tvb, offset, 4, FALSE);
266                 offset+=4;
267
268                 if (len >= 1000000000)
269                         THROW(ReportedBoundsError);
270                 cd=&chunk_table[0];
271                 while(1){
272                         if(cd->type==0){
273                                 cd=NULL;
274                                 break;
275                         }
276                         if(cd->type==type){
277                                 break;
278                         }
279                         cd++;
280                 }
281                 if(chunk_tree){
282                         proto_item_append_text(chunk_tree, " %s", cd?cd->name:"(don't know how to dissect this)");
283                 }
284
285                 if(!cd){
286                         proto_tree_add_item(chunk_tree, hf_png_chunk_data, tvb, offset, len, FALSE);
287                 } else {
288                         if(cd->dissector){
289                                 tvbuff_t *next_tvb;
290                                 proto_tree *cti=NULL;
291
292                                 next_tvb=tvb_new_subset(tvb, offset, MIN(tvb_length_remaining(tvb, offset), (int)len), len);
293                                 if(it){
294                                         cti=proto_item_add_subtree(it, ett_png_chunk_item);
295                                 }
296                                 cd->dissector(next_tvb, pinfo, cti);
297                         }
298                 }
299                 offset+=len;
300
301                 proto_tree_add_item(chunk_tree, hf_png_chunk_crc, tvb, offset, 4, FALSE);
302                 offset+=4;
303         }
304 }
305
306 void
307 proto_register_png(void)
308 {
309         static hf_register_info hf[] =
310         {
311         { &hf_png_signature, {
312           "PNG Signature", "png.signature", FT_BYTES, BASE_NONE,
313           NULL, 0, NULL, HFILL }},
314         { &hf_png_chunk_type, {
315           "Chunk", "png.chunk.type", FT_STRING, BASE_NONE,
316           NULL, 0, NULL, HFILL }},
317         { &hf_png_chunk_data, {
318           "Data", "png.chunk.data", FT_NONE, BASE_NONE,
319           NULL, 0, NULL, HFILL }},
320         { &hf_png_chunk_len, {
321           "Len", "png.chunk.len", FT_UINT32, BASE_DEC,
322           NULL, 0, NULL, HFILL }},
323         { &hf_png_chunk_crc, {
324           "CRC", "png.chunk.crc", FT_UINT32, BASE_HEX,
325           NULL, 0, NULL, HFILL }},
326         { &hf_png_chunk_flag_anc, {
327           "Ancillary", "png.chunk.flag.ancillary", FT_BOOLEAN, 32,
328           TFS(&png_chunk_anc), 0x20000000, NULL, HFILL }},
329         { &hf_png_chunk_flag_priv, {
330           "Private", "png.chunk.flag.private", FT_BOOLEAN, 32,
331           TFS(&png_chunk_priv), 0x00200000, NULL, HFILL }},
332         { &hf_png_chunk_flag_stc, {
333           "Safe To Copy", "png.chunk.flag.stc", FT_BOOLEAN, 32,
334           TFS(&png_chunk_stc), 0x00000020, NULL, HFILL }},
335         { &hf_png_ihdr_width, {
336           "Width", "png.ihdr.width", FT_UINT32, BASE_DEC,
337           NULL, 0, NULL, HFILL }},
338         { &hf_png_ihdr_height, {
339           "Height", "png.ihdr.height", FT_UINT32, BASE_DEC,
340           NULL, 0, NULL, HFILL }},
341         { &hf_png_ihdr_bitdepth, {
342           "Bit Depth", "png.ihdr.bitdepth", FT_UINT8, BASE_DEC,
343           NULL, 0, NULL, HFILL }},
344         { &hf_png_ihdr_colour_type, {
345           "Colour Type", "png.ihdr.colour_type", FT_UINT8, BASE_DEC,
346           VALS(colour_type_vals), 0, NULL, HFILL }},
347         { &hf_png_ihdr_compression_method, {
348           "Compression Method", "png.ihdr.compression_method", FT_UINT8, BASE_DEC,
349           VALS(compression_method_vals), 0, NULL, HFILL }},
350         { &hf_png_ihdr_filter_method, {
351           "Filter Method", "png.ihdr.filter_method", FT_UINT8, BASE_DEC,
352           VALS(filter_method_vals), 0, NULL, HFILL }},
353         { &hf_png_ihdr_interlace_method, {
354           "Interlace Method", "png.ihdr.interlace_method", FT_UINT8, BASE_DEC,
355           VALS(interlace_method_vals), 0, NULL, HFILL }},
356         { &hf_png_text_keyword, {
357           "Keyword", "png.text.keyword", FT_STRING, BASE_NONE,
358           NULL, 0, NULL, HFILL }},
359         { &hf_png_text_string, {
360           "String", "png.text.string", FT_STRING, BASE_NONE,
361           NULL, 0, NULL, HFILL }},
362         { &hf_png_time_year, {
363           "Year", "png.time.year", FT_UINT16, BASE_DEC,
364           NULL, 0, NULL, HFILL }},
365         { &hf_png_time_month, {
366           "Month", "png.time.month", FT_UINT8, BASE_DEC,
367           NULL, 0, NULL, HFILL }},
368         { &hf_png_time_day, {
369           "Day", "png.time.day", FT_UINT8, BASE_DEC,
370           NULL, 0, NULL, HFILL }},
371         { &hf_png_time_hour, {
372           "Hour", "png.time.hour", FT_UINT8, BASE_DEC,
373           NULL, 0, NULL, HFILL }},
374         { &hf_png_time_minute, {
375           "Minute", "png.time.minute", FT_UINT8, BASE_DEC,
376           NULL, 0, NULL, HFILL }},
377         { &hf_png_time_second, {
378           "Second", "png.time.second", FT_UINT8, BASE_DEC,
379           NULL, 0, NULL, HFILL }},
380         { &hf_png_phys_horiz, {
381           "Horizontal pixels per unit", "png.phys.horiz", FT_UINT32, BASE_DEC,
382           NULL, 0, NULL, HFILL }},
383         { &hf_png_phys_vert, {
384           "Vertical pixels per unit", "png.phys.vert", FT_UINT32, BASE_DEC,
385           NULL, 0, NULL, HFILL }},
386         { &hf_png_phys_unit, {
387           "Unit", "png.phys.unit", FT_UINT8, BASE_DEC,
388           VALS(phys_unit_vals), 0, NULL, HFILL }},
389         { &hf_png_bkgd_palette_index, {
390           "Palette Index", "png.bkgd.palette_index", FT_UINT8, BASE_DEC,
391           NULL, 0, NULL, HFILL }},
392         { &hf_png_bkgd_greyscale, {
393           "Greyscale", "png.bkgd.greyscale", FT_UINT16, BASE_HEX,
394           NULL, 0, NULL, HFILL }},
395         { &hf_png_bkgd_red, {
396           "Red", "png.bkgd.red", FT_UINT16, BASE_HEX,
397           NULL, 0, NULL, HFILL }},
398         { &hf_png_bkgd_green, {
399           "Green", "png.bkgd.green", FT_UINT16, BASE_HEX,
400           NULL, 0, NULL, HFILL }},
401         { &hf_png_bkgd_blue, {
402           "Blue", "png.bkgd.blue", FT_UINT16, BASE_HEX,
403           NULL, 0, NULL, HFILL }},
404         };
405
406         static gint *ett[] =
407         {
408                 &ett_png,
409                 &ett_png_chunk,
410                 &ett_png_chunk_item,
411         };
412
413
414         proto_png = proto_register_protocol("Portable Network Graphics","PNG","png");
415         proto_register_field_array(proto_png, hf, array_length(hf));
416         proto_register_subtree_array(ett, array_length(ett));
417 }
418
419 void
420 proto_reg_handoff_png(void)
421 {
422         dissector_handle_t png_handle;
423
424         png_handle = create_dissector_handle(dissect_png, proto_png);
425         dissector_add_string("media_type", "image/png", png_handle);
426 }