Fix a typo.
[obnox/wireshark/wip.git] / packet-image-jfif.c
1 /* packet-image-jfif.c
2  *
3  * Routines for JFIF image/jpeg media dissection
4  * Copyright 2004, Olivier Biot <olivier.biot (ad) siemens.com>
5  *
6  * $Id: packet-image-jfif.c,v 1.2 2004/01/06 00:09:48 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * JFIF media decoding functionality provided by Olivier Biot.
13  * 
14  * The JFIF specifications are found at several locations, such as:
15  * http://www.jpeg.org/public/jfif.pdf
16  * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
17  * 
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version 2
21  * of the License, or (at your option) any later version.
22  * 
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  * 
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31  */
32
33 /* Edit this file with 4-space tabulation */
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include <glib.h>
44
45 #ifdef NEED_SNPRINTF_H
46 # include "snprintf.h"
47 #endif
48
49 #include <epan/packet.h>
50
51 /* General-purpose debug logger.
52  * Requires double parentheses because of variable arguments of printf().
53  *
54  * Enable debug logging for JFIF by defining AM_CFLAGS
55  * so that it contains "-DDEBUG_image_jfif" or "-DDEBUG_image"
56  */
57 #if (defined(DEBUG_image_jfif) || defined(DEBUG_image))
58 #define DebugLog(x) \
59         printf("%s:%u: ", __FILE__, __LINE__); \
60         printf x; \
61         fflush(stdout)
62 #else
63 #define DebugLog(x) ;
64 #endif
65
66 #define PLURALIZE(x) ((x) == 1 ? "" : "s")
67
68 #define IMG_JFIF "image-jfif"
69
70 /************************** Variable declarations **************************/
71
72 #define MARKER_TEM              0xFF01
73
74 /* 0xFF02 -- 0xFFBF are reserved */
75
76 #define MARKER_SOF0             0xFFC0
77 #define MARKER_SOF1             0xFFC1
78 #define MARKER_SOF2             0xFFC2
79 #define MARKER_SOF3             0xFFC3
80
81 #define MARKER_DHT              0xFFC4
82
83 #define MARKER_SOF5             0xFFC5
84 #define MARKER_SOF6             0xFFC6
85 #define MARKER_SOF7             0xFFC7
86 #define MARKER_SOF8             0xFFC8
87 #define MARKER_SOF9             0xFFC9
88 #define MARKER_SOF10    0xFFCA
89 #define MARKER_SOF11    0xFFCB
90
91 #define MARKER_DAC              0xFFCC
92
93 #define MARKER_SOF13    0xFFCD
94 #define MARKER_SOF14    0xFFCE
95 #define MARKER_SOF15    0xFFCF
96
97 #define MARKER_RST0             0xFFD0
98 #define MARKER_RST1             0xFFD1
99 #define MARKER_RST2             0xFFD2
100 #define MARKER_RST3             0xFFD3
101 #define MARKER_RST4             0xFFD4
102 #define MARKER_RST5             0xFFD5
103 #define MARKER_RST6             0xFFD6
104 #define MARKER_RST7             0xFFD7
105
106 #define MARKER_FFDB             0xFFDB
107 #define MARKER_SOI              0xFFD8
108 #define MARKER_EOI              0xFFD9
109 #define MARKER_SOS              0xFFDA
110 #define MARKER_DQT              0xFFDB
111 #define MARKER_DNL              0xFFDC
112 #define MARKER_DRI              0xFFDD
113 #define MARKER_DHP              0xFFDE
114 #define MARKER_EXP              0xFFDF
115
116 #define MARKER_APP0             0xFFE0
117 #define MARKER_APP1             0xFFE1
118 #define MARKER_APP2             0xFFE2
119 #define MARKER_APP3             0xFFE3
120 #define MARKER_APP4             0xFFE4
121 #define MARKER_APP5             0xFFE5
122 #define MARKER_APP6             0xFFE6
123 #define MARKER_APP7             0xFFE7
124 #define MARKER_APP8             0xFFE8
125 #define MARKER_APP9             0xFFE9
126 #define MARKER_APP10    0xFFEA
127 #define MARKER_APP11    0xFFEB
128 #define MARKER_APP12    0xFFEC
129 #define MARKER_APP13    0xFFED
130 #define MARKER_APP14    0xFFEE
131 #define MARKER_APP15    0xFFEF
132
133 #define MARKER_JPG0             0xFFF0
134 #define MARKER_JPG1             0xFFF1
135 #define MARKER_JPG2             0xFFF2
136 #define MARKER_JPG3             0xFFF3
137 #define MARKER_JPG4             0xFFF4
138 #define MARKER_JPG5             0xFFF5
139 #define MARKER_JPG6             0xFFF6
140 #define MARKER_JPG7             0xFFF7
141 #define MARKER_JPG8             0xFFF8
142 #define MARKER_JPG9             0xFFF9
143 #define MARKER_JPG10    0xFFFA
144 #define MARKER_JPG11    0xFFFB
145 #define MARKER_JPG12    0xFFFC
146 #define MARKER_JPG13    0xFFFD
147
148 #define MARKER_COM              0xFFFE
149
150 #define marker_has_length(marker) ( ! ( \
151            ((marker) == MARKER_TEM) \
152         || ((marker) == MARKER_SOI) \
153         || ((marker) == MARKER_EOI) \
154         || ( ((marker) >= MARKER_RST0) && ((marker) <= MARKER_RST7) ) \
155         ) )
156
157
158 static const value_string vals_marker[] = {
159         { MARKER_TEM,   "Reserved - For temporary private use in arithmetic coding" },
160         { MARKER_SOF0,  "Start of Frame (non-differential, Huffman coding) - Baseline DCT" },
161         { MARKER_SOF1,  "Start of Frame (non-differential, Huffman coding) - Extended sequential DCT" },
162         { MARKER_SOF2,  "Start of Frame (non-differential, Huffman coding) - Progressive DCT" },
163         { MARKER_SOF3,  "Start of Frame (non-differential, Huffman coding) - Lossless (sequential)" },
164         { MARKER_DHT,   "Define Huffman table(s)" },
165         { MARKER_SOF5,  "Start of Frame (differential, Huffman coding) - Differential sequential DCT" },
166         { MARKER_SOF6,  "Start of Frame (differential, Huffman coding) - Differential progressive DCT" },
167         { MARKER_SOF7,  "Start of Frame (differential, Huffman coding) - Differential lossless (sequential)" },
168         { MARKER_SOF8,  "Start of Frame (non-differential, arithmetic coding) - Reserved for JPEG extensions" },
169         { MARKER_SOF9,  "Start of Frame (non-differential, arithmetic coding) - Extended sequential DCT" },
170         { MARKER_SOF10, "Start of Frame (non-differential, arithmetic coding) - Progressive DCT" },
171         { MARKER_SOF11, "Start of Frame (non-differential, arithmetic coding) - Lossless (sequential)" },
172         { MARKER_DAC,   "Define arithmetic coding conditioning(s)" },
173         { MARKER_SOF13, "Start of Frame (differential, arithmetic coding) - Differential sequential DCT" },
174         { MARKER_SOF14, "Start of Frame (differential, arithmetic coding) - Differential progressive DCT" },
175         { MARKER_SOF15, "Start of Frame (differential, arithmetic coding) - Differential lossless (sequential)" },
176         { MARKER_RST0,  "Restart interval termination - Restart with modulo 8 count 0" },
177         { MARKER_RST1,  "Restart interval termination - Restart with modulo 8 count 1" },
178         { MARKER_RST2,  "Restart interval termination - Restart with modulo 8 count 2" },
179         { MARKER_RST3,  "Restart interval termination - Restart with modulo 8 count 3" },
180         { MARKER_RST4,  "Restart interval termination - Restart with modulo 8 count 4" },
181         { MARKER_RST5,  "Restart interval termination - Restart with modulo 8 count 5" },
182         { MARKER_RST6,  "Restart interval termination - Restart with modulo 8 count 6" },
183         { MARKER_RST7,  "Restart interval termination - Restart with modulo 8 count 7" },
184         { MARKER_SOI,   "Start of Image" },
185         { MARKER_EOI,   "End of Image" },
186         { MARKER_SOS,   "Start of Scan" },
187         { MARKER_DQT,   "Define quantization table(s)" },
188         { MARKER_DNL,   "Define number of lines" },
189         { MARKER_DRI,   "Define restart interval" },
190         { MARKER_DHP,   "Define hierarchical progression" },
191         { MARKER_EXP,   "Expand reference component(s)" },
192         { MARKER_APP0,  "Reserved for application segments - 0" },
193         { MARKER_APP1,  "Reserved for application segments - 1" },
194         { MARKER_APP2,  "Reserved for application segments - 2" },
195         { MARKER_APP3,  "Reserved for application segments - 3" },
196         { MARKER_APP4,  "Reserved for application segments - 4" },
197         { MARKER_APP5,  "Reserved for application segments - 5" },
198         { MARKER_APP6,  "Reserved for application segments - 6" },
199         { MARKER_APP7,  "Reserved for application segments - 7" },
200         { MARKER_APP8,  "Reserved for application segments - 8" },
201         { MARKER_APP9,  "Reserved for application segments - 9" },
202         { MARKER_APP10, "Reserved for application segments - 10" },
203         { MARKER_APP11, "Reserved for application segments - 11" },
204         { MARKER_APP12, "Reserved for application segments - 12" },
205         { MARKER_APP13, "Reserved for application segments - 13" },
206         { MARKER_APP14, "Reserved for application segments - 14" },
207         { MARKER_APP15, "Reserved for application segments - 15" },
208         { MARKER_JPG0,  "Reserved for JPEG extensions - 0" },
209         { MARKER_JPG1,  "Reserved for JPEG extensions - 1" },
210         { MARKER_JPG2,  "Reserved for JPEG extensions - 2" },
211         { MARKER_JPG3,  "Reserved for JPEG extensions - 3" },
212         { MARKER_JPG4,  "Reserved for JPEG extensions - 4" },
213         { MARKER_JPG5,  "Reserved for JPEG extensions - 5" },
214         { MARKER_JPG6,  "Reserved for JPEG extensions - 6" },
215         { MARKER_JPG7,  "Reserved for JPEG extensions - 7" },
216         { MARKER_JPG8,  "Reserved for JPEG extensions - 8" },
217         { MARKER_JPG9,  "Reserved for JPEG extensions - 9" },
218         { MARKER_JPG10, "Reserved for JPEG extensions - 10" },
219         { MARKER_JPG11, "Reserved for JPEG extensions - 11" },
220         { MARKER_JPG12, "Reserved for JPEG extensions - 12" },
221         { MARKER_JPG13, "Reserved for JPEG extensions - 13" },
222         { 0x00, NULL }
223 };
224
225 static const value_string vals_units[] = {
226         { 0, "No units; Xdensity and Ydensity specify the pixel aspect ratio" },
227         { 1, "Dots per inch" },
228         { 2, "Dots per centimeter" },
229         { 0x00, NULL }
230 };
231
232 static const value_string vals_extension_code[] = {
233         { 0x10, "Thumbnail encoded using JPEG" },
234         { 0x11, "Thumbnail encoded using 1 byte (8 bits) per pixel" },
235         { 0x13, "Thumbnail encoded using 3 bytes (24 bits) per pixel" },
236         { 0x00, NULL }
237 };
238
239 /* Initialize the protocol and registered fields */
240 static int proto_jfif = -1;
241
242 /* Marker */
243 static gint hf_marker = -1;
244 /* Marker segment */
245 static gint hf_marker_segment = -1;
246 static gint hf_len = -1;
247 /* MARKER_APP0 */
248 static gint hf_identifier = -1;
249 /* MARKER_APP0 - JFIF */
250 static gint hf_version = -1;
251 static gint hf_version_major = -1;
252 static gint hf_version_minor = -1;
253 static gint hf_units = -1;
254 static gint hf_xdensity = -1;
255 static gint hf_ydensity = -1;
256 static gint hf_xthumbnail = -1;
257 static gint hf_ythumbnail = -1;
258 static gint hf_rgb = -1;
259 /* MARKER_APP0 - JFXX */
260 static gint hf_extension_code = -1;
261 /* start of Frame */
262 static gint hf_sof_header = -1;
263 static gint hf_sof_precision = -1;
264 static gint hf_sof_lines = -1;
265 static gint hf_sof_samples_per_line = -1;
266 static gint hf_sof_nf = -1;
267 static gint hf_sof_c_i = -1;
268 static gint hf_sof_h_i = -1;
269 static gint hf_sof_v_i = -1;
270 static gint hf_sof_tq_i = -1;
271
272 /* Start of Scan */
273 static gint hf_sos_header = -1;
274 static gint hf_sos_ns = -1;
275 static gint hf_sos_cs_j = -1;
276 static gint hf_sos_td_j = -1;
277 static gint hf_sos_ta_j = -1;
278 static gint hf_sos_ss = -1;
279 static gint hf_sos_se = -1;
280 static gint hf_sos_ah = -1;
281 static gint hf_sos_al = -1;
282
283 /* Initialize the subtree pointers */
284 static gint ett_jfif = -1;
285 static gint ett_marker_segment = -1;
286 static gint ett_details = -1;
287
288
289 /**************** GIF related declarations and definitions ****************/
290
291
292 /************************** Function prototypes **************************/
293
294
295 static void
296 dissect_jfif(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
297
298 void
299 proto_register_jfif(void);
300
301
302
303 /****************** JFIF protocol dissection functions ******************/
304
305 #define ErrorInvalidJFIF "This is not a valid JFIF (JPEG) object"
306
307
308 /*
309  * Process a marker segment (with length).
310  */
311 static void
312 process_marker_segment(proto_tree *tree, tvbuff_t *tvb, guint32 len,
313                 guint16 marker, const char *marker_name)
314 {
315         proto_item *ti = NULL;
316         proto_tree *subtree = NULL;
317
318         if (! tree)
319                 return;
320
321         ti = proto_tree_add_item(tree, hf_marker_segment,
322                         tvb, 0, -1, FALSE);
323         subtree = proto_item_add_subtree(ti, ett_marker_segment);
324
325         proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
326         proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
327
328         proto_tree_add_item(subtree, hf_len, tvb, 2, 2, FALSE);
329
330         proto_tree_add_text(subtree, tvb, 4, -1,
331                         "Remaining segment data (%u bytes)", len - 2);
332 }
333
334 /*
335  * Process a Start of Frame header (with length).
336  */
337 static void
338 process_sof_header(proto_tree *tree, tvbuff_t *tvb, guint32 len _U_,
339                 guint16 marker, const char *marker_name)
340 {
341         proto_item *ti = NULL;
342         proto_tree *subtree = NULL;
343
344         if (! tree)
345                 return;
346
347         ti = proto_tree_add_item(tree, hf_sof_header,
348                         tvb, 0, -1, FALSE);
349         subtree = proto_item_add_subtree(ti, ett_marker_segment);
350
351         proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
352         proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
353
354         proto_tree_add_item(subtree, hf_len, tvb, 2, 2, FALSE);
355
356         proto_tree_add_item(subtree, hf_sof_precision, tvb, 4, 1, FALSE);
357
358         proto_tree_add_item(subtree, hf_sof_lines, tvb, 5, 2, FALSE);
359
360         proto_tree_add_item(subtree, hf_sof_samples_per_line, tvb, 7, 2, FALSE);
361
362         proto_tree_add_item(subtree, hf_sof_nf, tvb, 9, 1, FALSE);
363         {
364                 guint8 count = tvb_get_guint8(tvb, 9);
365                 guint32 offset = 10;
366                 while (count > 0) {
367                         proto_tree_add_item(subtree, hf_sof_c_i, tvb, offset++, 1, FALSE);
368                         proto_tree_add_item(subtree, hf_sof_h_i, tvb, offset, 1, FALSE);
369                         proto_tree_add_item(subtree, hf_sof_v_i, tvb, offset++, 1, FALSE);
370                         proto_tree_add_item(subtree, hf_sof_tq_i, tvb, offset++, 1, FALSE);
371                         count--;
372                 }
373         }
374 }
375
376 /*
377  * Process a Start of Segment header (with length).
378  */
379 static void
380 process_sos_header(proto_tree *tree, tvbuff_t *tvb, guint32 len _U_,
381                 guint16 marker, const char *marker_name)
382 {
383         proto_item *ti = NULL;
384         proto_tree *subtree = NULL;
385         guint32 offset;
386
387         if (! tree)
388                 return;
389
390         ti = proto_tree_add_item(tree, hf_sos_header,
391                         tvb, 0, -1, FALSE);
392         subtree = proto_item_add_subtree(ti, ett_marker_segment);
393
394         proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
395         proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
396
397         proto_tree_add_item(subtree, hf_len, tvb, 2, 2, FALSE);
398
399         proto_tree_add_item(subtree, hf_sos_ns, tvb, 4, 1, FALSE);
400         {
401                 guint8 count = tvb_get_guint8(tvb, 4);
402                 offset = 5;
403                 while (count > 0) {
404                         proto_tree_add_item(subtree, hf_sos_cs_j, tvb, offset++, 1, FALSE);
405                         proto_tree_add_item(subtree, hf_sos_td_j, tvb, offset, 1, FALSE);
406                         proto_tree_add_item(subtree, hf_sos_ta_j, tvb, offset++, 1, FALSE);
407                         count--;
408                 }
409         }
410         
411         proto_tree_add_item(subtree, hf_sos_ss, tvb, offset++, 1, FALSE);
412         proto_tree_add_item(subtree, hf_sos_se, tvb, offset++, 1, FALSE);
413
414         proto_tree_add_item(subtree, hf_sos_ah, tvb, offset, 1, FALSE);
415         proto_tree_add_item(subtree, hf_sos_al, tvb, offset++, 1, FALSE);
416 }
417
418 /* Process an APP0 block.
419  *
420  * XXX - This code only works on US-ASCII systems!!!
421  */
422 static void
423 process_app0_segment(proto_tree *tree, tvbuff_t *tvb, guint32 len,
424                 guint16 marker, const char *marker_name)
425 {
426         proto_item *ti = NULL;
427         proto_tree *subtree = NULL;
428         proto_tree *subtree_details = NULL;
429         guint32 offset;
430         char *str;
431         gint str_size;
432
433         if (!tree)
434                 return;
435
436         ti = proto_tree_add_item(tree, hf_marker_segment,
437                         tvb, 0, -1, FALSE);
438         subtree = proto_item_add_subtree(ti, ett_marker_segment);
439
440         proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
441         proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
442
443         proto_tree_add_item(subtree, hf_len, tvb, 2, 2, FALSE);
444
445         str = tvb_get_stringz(tvb, 4, &str_size);
446         ti = proto_tree_add_item(subtree, hf_identifier, tvb, 4, str_size, FALSE);
447         if (strcmp(str, "JFIF") == 0) {
448                 /* Version */
449                 ti = proto_tree_add_none_format(subtree, hf_version,
450                                 tvb, 9, 2, "Version: %u.%u",
451                                 tvb_get_guint8(tvb, 9),
452                                 tvb_get_guint8(tvb, 10));
453                 subtree_details = proto_item_add_subtree(ti, ett_details);
454                 proto_tree_add_item(subtree_details, hf_version_major,
455                                 tvb, 9, 1, FALSE);
456                 proto_tree_add_item(subtree_details, hf_version_minor,
457                                 tvb, 10, 1, FALSE);
458
459                 proto_tree_add_item(subtree, hf_units,
460                                 tvb, 11, 1, FALSE);
461
462                 /* Aspect ratio */
463                 proto_tree_add_item(subtree, hf_xdensity,
464                                 tvb, 12, 2, FALSE);
465                 proto_tree_add_item(subtree, hf_ydensity,
466                                 tvb, 14, 2, FALSE);
467
468                 /* Thumbnail */
469                 proto_tree_add_item(subtree, hf_xthumbnail,
470                                 tvb, 16, 1, FALSE);
471                 proto_tree_add_item(subtree, hf_ythumbnail,
472                                 tvb, 17, 1, FALSE);
473                 {
474                         guint16 x = tvb_get_guint8(tvb, 16);
475                         guint16 y = tvb_get_guint8(tvb, 17);
476                         if (x || y) {
477                                 proto_tree_add_item(subtree, hf_rgb,
478                                                 tvb, 18, 3 * (x * y), FALSE);
479                                 offset = 18 + (3 * (x * y));
480                         } else {
481                                 offset = 18;
482                         }
483                 }
484         } else if (strcmp(str, "JFXX") == 0) {
485                 proto_tree_add_item(subtree, hf_extension_code,
486                                 tvb, 9, 1, FALSE);
487                 {
488                         guint8 code = tvb_get_guint8(tvb, 9);
489                         switch (code) {
490                                 case 0x10: /* Thumbnail coded using JPEG */
491                                         break;
492                                 case 0x11: /* thumbnail stored using 1 byte per pixel */
493                                         break;
494                                 case 0x13: /* thumbnail stored using 3 bytes per pixel */
495                                         break;
496                                 default: /* Error */
497                                         break;
498                         }
499                 }
500         } else { /* Unknown */
501                 proto_item_append_text(ti, " (unknown identifier)");
502                 offset = 4 + str_size;
503
504                 proto_tree_add_text(subtree, tvb, offset, -1,
505                                 "Remaining segment data (%u bytes)", len - 2 - str_size);
506         }
507
508         return;
509 }
510
511 static void
512 dissect_jfif(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
513 {
514         proto_tree *subtree = NULL;
515         proto_item *ti;
516         guint tvb_len = tvb_reported_length(tvb);
517         guint32 offset = 0;
518         tvbuff_t *tmp_tvb;
519         guint32 len;
520         guint16 marker;
521
522         if (tree) {
523                 ti = proto_tree_add_item(tree, proto_jfif,
524                                 tvb, 0, -1, FALSE);
525                 subtree = proto_item_add_subtree(ti, ett_jfif);
526         }
527
528         marker = tvb_get_ntohs(tvb, 0);
529         if (marker != MARKER_SOI) {
530                 if (tree) {
531                         proto_tree_add_text(subtree, tvb, 0, 2, ErrorInvalidJFIF);
532                         return;
533                 }
534         }
535         if (tree)
536                 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
537
538         marker = tvb_get_ntohs(tvb, 2);
539         if (marker != MARKER_APP0) {
540                 if (tree) {
541                         proto_tree_add_text(subtree, tvb, 2, 2, ErrorInvalidJFIF);
542                         return;
543                 }
544         }
545
546         if (! tree)
547                 return;
548
549         offset = 4;
550         /* The APP0 segment has a length field */
551         len = tvb_get_ntohs(tvb, offset);
552         tmp_tvb = tvb_new_subset(tvb, offset - 2, 2 + len, 2 + len);
553         process_app0_segment(subtree, tmp_tvb, len, marker,
554                         match_strval(marker, vals_marker));
555         offset += len;
556
557         /*
558          * Process the remaining markers and marker segments
559          */
560         while (offset < tvb_len) {
561                 char *str;
562                 marker = tvb_get_ntohs(tvb, offset);
563                 str = match_strval(marker, vals_marker);
564                 if (str) { /* Known marker */
565                         if (marker_has_length(marker)) { /* Marker segment */
566                                 /* Length of marker segment = 2 + len */
567                                 len = tvb_get_ntohs(tvb, offset + 2);
568                                 tmp_tvb = tvb_new_subset(tvb, offset, 2 + len, 2 + len);
569                                 switch (marker) {
570                                         case MARKER_APP0:
571                                                 process_app0_segment(subtree, tmp_tvb, len, marker, str);
572                                                 break;
573                                         case MARKER_SOF0:
574                                         case MARKER_SOF1:
575                                         case MARKER_SOF2:
576                                         case MARKER_SOF3:
577                                         case MARKER_SOF5:
578                                         case MARKER_SOF6:
579                                         case MARKER_SOF7:
580                                         case MARKER_SOF8:
581                                         case MARKER_SOF9:
582                                         case MARKER_SOF10:
583                                         case MARKER_SOF11:
584                                         case MARKER_SOF13:
585                                         case MARKER_SOF14:
586                                         case MARKER_SOF15:
587                                                 process_sof_header(subtree, tmp_tvb, len, marker, str);
588                                                 break;
589                                         case MARKER_SOS:
590                                                 process_sos_header(subtree, tmp_tvb, len, marker, str);
591                                                 /*
592                                                  * TODO - dissect a scan; the scan data is encoded.
593                                                  */
594                                                 proto_tree_add_text(subtree, tvb, offset + 2 + len, -1,
595                                                                 "JFIF dissection stops here (dissection of a scan is not yet implemented)");
596                                                 return;
597                                                 break;
598                                         default:
599                                                 process_marker_segment(subtree, tmp_tvb, len, marker, str);
600                                                 break;
601                                 }
602                                 offset += 2 + len;
603                         } else { /* Marker but no segment */
604                                 /* Length = 2 */
605                                 proto_tree_add_item(subtree, hf_marker,
606                                                 tvb, offset, 2, FALSE);
607                                 offset += 2;
608                         }
609                 } else { /* Reserved! */
610                         ti = proto_tree_add_item(subtree, hf_marker,
611                                         tvb, offset, 2, FALSE);
612                         proto_item_append_text(ti, " (Reserved)");
613                         return;
614                 }
615         }
616
617         return;
618 }
619
620
621 /****************** Register the protocol with Ethereal ******************/
622
623
624 /* This format is required because a script is used to build the C function
625  * that calls the protocol registration. */
626
627 void
628 proto_register_jfif(void)
629 {
630         /*
631          * Setup list of header fields.
632          */
633         static hf_register_info hf[] = {
634                 /* Marker */
635                 { &hf_marker,
636                         {       "Marker",
637                                 IMG_JFIF ".marker",
638                                 FT_UINT8, BASE_HEX, VALS(vals_marker), 0x00,
639                                 "JFIF Marker",
640                                 HFILL
641                         }
642                 },
643                 /* Marker segment */
644                 { &hf_marker_segment,
645                         {       "Marker segment",
646                                 IMG_JFIF "marker_segment",
647                                 FT_NONE, BASE_NONE, NULL, 0x00,
648                                 "Marker segment",
649                                 HFILL
650                         }
651                 },
652                 { &hf_len,
653                         {       "Length",
654                                 IMG_JFIF ".length",
655                                 FT_UINT16, BASE_DEC, 0, 0x00,
656                                 "Length of segment (including length field)",
657                                 HFILL
658                         }
659                 },
660                 /* MARKER_APP0 */
661                 { &hf_identifier,
662                         {       "Identifier",
663                                 IMG_JFIF ".identifier",
664                                 FT_STRINGZ, BASE_NONE, NULL, 0x00,
665                                 "Identifier of the segment",
666                                 HFILL
667                         }
668                 },
669                 /* MARKER_APP0 - JFIF */
670                 { &hf_version,
671                         {       "Version",
672                                 IMG_JFIF ".version",
673                                 FT_NONE, BASE_NONE, NULL, 0x00,
674                                 "JFIF Version",
675                                 HFILL
676                         }
677                 },
678                 { &hf_version_major,
679                         {       "Major Version",
680                                 IMG_JFIF ".version.major",
681                                 FT_UINT8, BASE_DEC, NULL, 0x00,
682                                 "JFIF Major Version",
683                                 HFILL
684                         }
685                 },
686                 { &hf_version_minor,
687                         {       "Minor Version",
688                                 IMG_JFIF ".version.minor",
689                                 FT_UINT8, BASE_DEC, NULL, 0x00,
690                                 "JFIF Minor Version",
691                                 HFILL
692                         }
693                 },
694                 { &hf_units,
695                         {       "Units",
696                                 IMG_JFIF ".units",
697                                 FT_UINT8, BASE_DEC, VALS(vals_units), 0x00,
698                                 "Units used in this segment",
699                                 HFILL
700                         }
701                 },
702                 { &hf_xdensity,
703                         {       "Xdensity",
704                                 IMG_JFIF ".Xdensity",
705                                 FT_UINT16, BASE_DEC, NULL, 0x00,
706                                 "Horizontal pixel density",
707                                 HFILL
708                         }
709                 },
710                 { &hf_ydensity,
711                         {       "Ydensity",
712                                 IMG_JFIF ".Ydensity",
713                                 FT_UINT16, BASE_DEC, NULL, 0x00,
714                                 "Vertical pixel density",
715                                 HFILL
716                         }
717                 },
718                 { &hf_xthumbnail,
719                         {       "Xthumbnail",
720                                 IMG_JFIF ".Xthumbnail",
721                                 FT_UINT16, BASE_DEC, NULL, 0x00,
722                                 "Thumbnail horizontal pixel count",
723                                 HFILL
724                         }
725                 },
726                 { &hf_ythumbnail,
727                         {       "Ythumbnail",
728                                 IMG_JFIF ".Ythumbnail",
729                                 FT_UINT16, BASE_DEC, NULL, 0x00,
730                                 "Thumbnail vertical pixel count",
731                                 HFILL
732                         }
733                 },
734                 { &hf_rgb,
735                         {       "RGB values of thumbnail pixels",
736                                 IMG_JFIF ".RGB",
737                                 FT_BYTES, BASE_NONE, NULL, 0x00,
738                                 "RGB values of the thumbnail pixels (24 bit per pixel, Xthumbnail x Ythumbnail pixels)",
739                                 HFILL
740                         }
741                 },
742                 /* MARKER_APP0 - JFXX */
743                 { &hf_extension_code,
744                         {       "Extension code",
745                                 IMG_JFIF ".extension.code",
746                                 FT_UINT8, BASE_HEX, VALS(vals_extension_code), 0x00,
747                                 "JFXX extension code for thumbnail encoding",
748                                 HFILL
749                         }
750                 },
751                 /* Header: Start of Frame (MARKER_SOF) */
752                 { &hf_sof_header,
753                         {       "Start of Frame header",
754                                 IMG_JFIF ".sof",
755                                 FT_NONE, BASE_NONE, NULL, 0x00,
756                                 "Start of Frame header",
757                                 HFILL
758                         }
759                 },
760                 { &hf_sof_precision,
761                         {       "Sample Precision (bits)",
762                                 IMG_JFIF ".sof.precision",
763                                 FT_UINT8, BASE_DEC, NULL, 0x00,
764                                 "Specifies the precision in bits for the samples of the components in the frame.",
765                                 HFILL
766                         }
767                 },
768                 { &hf_sof_lines,
769                         {       "Lines",
770                                 IMG_JFIF ".sof.lines",
771                                 FT_UINT16, BASE_DEC, NULL, 0x00,
772                                 "Specifies the maximum number of lines in the source image.",
773                                 HFILL
774                         }
775                 },
776                 { &hf_sof_samples_per_line,
777                         {       "Samples per line",
778                                 IMG_JFIF ".sof.samples_per_line",
779                                 FT_UINT16, BASE_DEC, NULL, 0x00,
780                                 "Specifies the maximum number of samples per line in the source image.",
781                                 HFILL
782                         }
783                 },
784                 { &hf_sof_nf,
785                         {       "Number of image components in frame",
786                                 IMG_JFIF ".sof.nf",
787                                 FT_UINT8, BASE_DEC, NULL, 0x00,
788                                 "Specifies the number of source image components in the frame.",
789                                 HFILL
790                         }
791                 },
792                 { &hf_sof_c_i,
793                         {       "Component identifier",
794                                 IMG_JFIF ".sof.c_i",
795                                 FT_UINT8, BASE_DEC, NULL, 0x00,
796                                 "Assigns a unique label to the ith component in the sequence "
797                                 "of frame component specification parameters.",
798                                 HFILL
799                         }
800                 },
801                 { &hf_sof_h_i,
802                         {       "Horizontal sampling factor",
803                                 IMG_JFIF ".sof.h_i",
804                                 FT_UINT8, BASE_DEC, NULL, 0xF0,
805                                 "Specifies the relationship between the component horizontal "
806                                 "dimension and maximum image dimension X.",
807                                 HFILL
808                         }
809                 },
810                 { &hf_sof_v_i,
811                         {       "Vertical sampling factor",
812                                 IMG_JFIF ".sof.v_i",
813                                 FT_UINT8, BASE_DEC, NULL, 0x0F,
814                                 "Specifies the relationship between the component vertical "
815                                 "dimension and maximum image dimension Y.",                             
816                                 HFILL
817                         }
818                 },
819                 { &hf_sof_tq_i,
820                         {       "Quantization table destination selector",
821                                 IMG_JFIF ".sof.tq_i",
822                                 FT_UINT8, BASE_DEC, NULL, 0x00,
823                                 "Specifies one of four possible quantization table "
824                                 "destinations from which the quantization table to use "
825                                 "for dequantization of DCT coefficients of component Ci "
826                                 "is retrieved.",
827                                 HFILL
828                         }
829                 },
830
831                 /* Header: Start of Segment (MARKER_SOS) */
832                 { &hf_sos_header,
833                         {       "Start of Segment header",
834                                 IMG_JFIF ".header.sos",
835                                 FT_NONE, BASE_NONE, NULL, 0x00,
836                                 "Start of Segment header",
837                                 HFILL
838                         }
839                 },
840                 { &hf_sos_ns,
841                         {       "Number of image components in scan",
842                                 IMG_JFIF ".sos.ns",
843                                 FT_UINT8, BASE_DEC, NULL, 0x00,
844                                 "Specifies the number of source image components in the scan.",
845                                 HFILL
846                         }
847                 },
848                 { &hf_sos_cs_j,
849                         {       "Scan component selector",
850                                 IMG_JFIF ".sos.component_selector",
851                                 FT_UINT8, BASE_DEC, NULL, 0x00,
852                                 "Selects which of the Nf image components specified in the "
853                                 "frame parameters shall be the jth component in the scan.",
854                                 HFILL
855                         }
856                 },
857                 { &hf_sos_td_j,
858                         {       "DC entropy coding table destination selector",
859                                 IMG_JFIF ".sos.dc_entropy_selector",
860                                 FT_UINT8, BASE_DEC, NULL, 0xF0,
861                                 "Specifies one of four possible DC entropy coding table "
862                                 "destinations from which the entropy table needed "
863                                 "for decoding of the DC coefficients of component Csj "
864                                 "is retrieved.",
865                                 HFILL
866                         }
867                 },
868                 { &hf_sos_ta_j,
869                         {       "AC entropy coding table destination selector",
870                                 IMG_JFIF ".sos.ac_entropy_selector",
871                                 FT_UINT8, BASE_DEC, NULL, 0x0F,
872                                 "Specifies one of four possible AC entropy coding table "
873                                 "destinations from which the entropy table needed "
874                                 "for decoding of the AC coefficients of component Csj "
875                                 "is retrieved.",
876                                 HFILL
877                         }
878                 },
879                 { &hf_sos_ss,
880                         {       "Start of spectral or predictor selection",
881                                 IMG_JFIF ".sos.ss",
882                                 FT_UINT8, BASE_DEC, NULL, 0x00,
883                                 "In the DCT modes of operation, this parameter specifies the "
884                                 "first DCT coefficient in each block in zig-zag order which "
885                                 "shall be coded in the scan. This parameter shall be set to "
886                                 "zero for the sequential DCT processes. "
887                                 "In the lossless mode of operations this parameter is used "
888                                 "to select the predictor.",
889                                 HFILL
890                         }
891                 },
892                 { &hf_sos_se,
893                         {       "End of spectral selection",
894                                 IMG_JFIF ".sos.se",
895                                 FT_UINT8, BASE_DEC, NULL, 0x00,
896                                 "Specifies the last DCT coefficient in each block in zig-zag "
897                                 "order which shall be coded in the scan. This parameter shall "
898                                 "be set to 63 for the sequential DCT processes. "
899                                 "In the lossless mode of operations this parameter has no "
900                                 "meaning. It shall be set to zero.",
901                                 HFILL
902                         }
903                 },
904                 { &hf_sos_ah,
905                         {       "Successive approximation bit position high",
906                                 IMG_JFIF ".sos.ah",
907                                 FT_UINT8, BASE_DEC, NULL, 0xF0,
908                                 "This parameter specifies the point transform used in the "
909                                 "preceding scan (i.e. successive approximation bit position "
910                                 "low in the preceding scan) for the band of coefficients "
911                                 "specified by Ss and Se. This parameter shall be set to zero "
912                                 "for the first scan of each band of coefficients. "
913                                 "In the lossless mode of operations this parameter has no "
914                                 "meaning. It shall be set to zero.",
915                                 HFILL
916                         }
917                 },
918                 { &hf_sos_al, 
919                         {       "Successive approximation bit position low or point transform",
920                                 IMG_JFIF ".sos.al",
921                                 FT_UINT8, BASE_DEC, NULL, 0x0F,
922                                 "In the DCT modes of operation this parameter specifies the "
923                                 "point transform, i.e. bit position low, used before coding "
924                                 "the band of coefficients specified by Ss and Se. "
925                                 "This parameter shall be set to zero for the sequential DCT "
926                                 "processes. In the lossless mode of operations, this "
927                                 "parameter specifies the point transform, Pt.",
928                                 HFILL
929                         }
930                 },
931         };
932
933         /* Setup protocol subtree array */
934         static gint *ett[] = {
935                 &ett_jfif,
936                 &ett_marker_segment,
937                 &ett_details,
938         };
939
940         /* Register the protocol name and description */
941         proto_jfif = proto_register_protocol(
942                         "JPEG File Interchange Format",
943                         "JFIF (JPEG) image",
944                         IMG_JFIF
945         );
946
947         /* Required function calls to register the header fields
948          * and subtrees used */
949         proto_register_field_array(proto_jfif, hf, array_length(hf));
950         proto_register_subtree_array(ett, array_length(ett));
951
952         register_dissector("image-jfif", dissect_jfif, proto_jfif);
953 }
954
955
956 void
957 proto_reg_handoff_jfif(void)
958 {
959         dissector_handle_t jfif_handle;
960
961         jfif_handle = create_dissector_handle(dissect_jfif, proto_jfif);
962
963         /* Register the GIF media type */
964         dissector_add_string("media_type", "image/jfif", jfif_handle);
965         dissector_add_string("media_type", "image/jpg", jfif_handle);
966         dissector_add_string("media_type", "image/jpeg", jfif_handle);
967 }