packet-kerberos: remember in what frame a key was learned
[metze/wireshark/wip.git] / capinfos.c
1 /* capinfos.c
2  * Reports capture file information including # of packets, duration, others
3  *
4  * Copyright 2004 Ian Schorr
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 /*
28  * 2009-09-19: jyoung
29  *
30  * New capinfos features
31  *
32  * Continue processing additional files after
33  * a wiretap open failure.  The new -C option
34  * reverts to capinfos' original behavior which
35  * is to cancel any further file processing at
36  * first file open failure.
37  *
38  * Change the behavior of how the default display
39  * of all infos is initiated.  This gets rid of a
40  * special post getopt() argument count test.
41  *
42  * Add new table output format (with related options)
43  * This feature allows outputting the various infos
44  * into a tab delimited text file, or to a comma
45  * separated variables file (*.csv) instead of the
46  * original "long" format.
47  *
48  * 2011-04-05: wmeier
49  * behaviour changed: Upon exit capinfos will return
50  *  an error status if an error occurred at any
51  *  point during "continuous" file processing.
52  *  (Previously a success status was always
53  *   returned if the -C option was not used).
54  *
55
56  */
57
58
59 #include "config.h"
60
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <stdarg.h>
65 #include <locale.h>
66 #include <errno.h>
67
68 #ifdef HAVE_UNISTD_H
69 #include <unistd.h>
70 #endif
71
72 #ifdef HAVE_SYS_TIME_H
73 #include <sys/time.h>
74 #endif
75
76 #include <glib.h>
77
78 #include <wsutil/privileges.h>
79 #include <wsutil/filesystem.h>
80
81 #ifdef HAVE_PLUGINS
82 #include <wsutil/plugins.h>
83 #endif
84
85 #include "wtap.h"
86 #include <wsutil/report_err.h>
87 #include <wsutil/privileges.h>
88 #include <wsutil/str_util.h>
89
90 #ifdef HAVE_LIBGCRYPT
91 #include <wsutil/wsgcrypt.h>
92 #include <wsutil/file_util.h>
93 #endif
94
95 #ifndef HAVE_GETOPT
96 #include "wsutil/wsgetopt.h"
97 #endif
98
99 #ifdef _WIN32
100 #include <wsutil/unicode-utils.h>
101 #endif /* _WIN32 */
102
103 #include "version.h"
104
105 /*
106  * By default capinfos now continues processing
107  * the next filename if and when wiretap detects
108  * a problem opening a file.
109  * Use the '-C' option to revert back to original
110  * capinfos behavior which is to abort any
111  * additional file processing at first open file
112  * failure.
113  */
114
115 static gboolean continue_after_wtap_open_offline_failure = TRUE;
116
117 /*
118  * table report variables
119  */
120
121 static gboolean long_report        = TRUE;  /* By default generate long report       */
122 static gchar table_report_header   = TRUE;  /* Generate column header by default     */
123 static gchar field_separator       = '\t';  /* Use TAB as field separator by default */
124 static gchar quote_char            = '\0';  /* Do NOT quote fields by default        */
125 static gboolean machine_readable   = FALSE; /* Display machine-readable numbers      */
126
127 /*
128  * capinfos has the ability to report on a number of
129  * various characteristics ("infos") for each input file.
130  *
131  * By default reporting of all info fields is enabled.
132  *
133  * Optionally the reporting of any specific info field
134  * or combination of info fields can be enabled with
135  * individual options.
136  */
137
138 static gboolean report_all_infos   = TRUE;  /* Report all infos           */
139
140 static gboolean cap_file_type      = TRUE;  /* Report capture type        */
141 static gboolean cap_file_encap     = TRUE;  /* Report encapsulation       */
142 static gboolean cap_snaplen        = TRUE;  /* Packet size limit (snaplen)*/
143 static gboolean cap_packet_count   = TRUE;  /* Report packet count        */
144 static gboolean cap_file_size      = TRUE;  /* Report file size           */
145 static gboolean cap_comment        = TRUE;  /* Display the capture comment */
146
147 static gboolean cap_data_size      = TRUE;  /* Report packet byte size    */
148 static gboolean cap_duration       = TRUE;  /* Report capture duration    */
149 static gboolean cap_start_time     = TRUE;  /* Report capture start time  */
150 static gboolean cap_end_time       = TRUE;  /* Report capture end time    */
151 static gboolean time_as_secs       = FALSE; /* Report time values as raw seconds */
152
153 static gboolean cap_data_rate_byte = TRUE;  /* Report data rate bytes/sec */
154 static gboolean cap_data_rate_bit  = TRUE;  /* Report data rate bites/sec */
155 static gboolean cap_packet_size    = TRUE;  /* Report average packet size */
156 static gboolean cap_packet_rate    = TRUE;  /* Report average packet rate */
157 static gboolean cap_order          = TRUE;  /* Report if packets are in chronological order (True/False) */
158
159 #ifdef HAVE_LIBGCRYPT
160 static gboolean cap_file_hashes    = TRUE;  /* Calculate file hashes */
161 #endif
162
163 #ifdef USE_GOPTION
164 static gboolean cap_help     = FALSE;
165 static gboolean table_report = FALSE;
166
167 static GOptionEntry general_entries[] =
168 {
169   /* General */
170   { "type", 't', 0, G_OPTION_ARG_NONE, &cap_file_type,
171     "display the capture file type", NULL },
172   { "Encapsulation", 'E', 0, G_OPTION_ARG_NONE, &cap_file_encap,
173     "display the capture file encapsulation", NULL },
174 #ifdef HAVE_LIBGCRYPT
175   { "Hash", 'H', 0, G_OPTION_ARG_NONE, &cap_file_hashes,
176     "display the SHA1, RMD160, and MD5 hashes of the file", NULL },
177 #endif /* HAVE_LIBGCRYPT */
178   { "capture-comment", 'k', 0, G_OPTION_ARG_NONE, &cap_comment,
179     "display the capture comment ", NULL },
180   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
181 };
182 static GOptionEntry size_entries[] =
183 {
184   /* Size */
185   { "packets", 'c', 0, G_OPTION_ARG_NONE, &cap_packet_count,
186     "display the number of packets", NULL },
187   { "size", 's', 0, G_OPTION_ARG_NONE, &cap_file_size,
188     "display the size of the file (in bytes)", NULL },
189   { "tot-len-of-pkts", 'd', 0, G_OPTION_ARG_NONE, &cap_data_size,
190     "display the total length of all packets (in bytes)", NULL },
191   { "snap", 'l', 0, G_OPTION_ARG_NONE, &cap_snaplen,
192     "display the packet size limit (snapshot length)", NULL },
193   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
194 };
195 static GOptionEntry time_entries[] =
196 {
197   /* Time */
198   { "duration", 'u', 0, G_OPTION_ARG_NONE, &cap_duration,
199     "display the capture duration (in seconds)", NULL },
200   { "start", 'a', 0, G_OPTION_ARG_NONE, &cap_start_time,
201     "display the capture start time", NULL },
202   { "end", 'e', 0, G_OPTION_ARG_NONE, &cap_end_time,
203     "display the capture end time", NULL },
204   { "cron", 'o', 0, G_OPTION_ARG_NONE, &cap_order,
205     "display the capture file chronological status (True/False)", NULL },
206   { "start-end-time-sec", 'S', 0, G_OPTION_ARG_NONE, &time_as_secs,
207     "display start and end times as seconds", NULL },
208   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
209 };
210
211 static GOptionEntry stats_entries[] =
212 {
213   /* Statistics */
214   { "bytes", 'y', 0, G_OPTION_ARG_NONE, &cap_data_rate_byte,
215     "display average data rate (in bytes/s)", NULL },
216   { "bits", 'i', 0, G_OPTION_ARG_NONE, &cap_data_rate_bit,
217     "display average data rate (in bits/s)", NULL },
218   { "packet-bytes", 'z', 0, G_OPTION_ARG_NONE, &cap_packet_size,
219     "display average packet size (in bytes)", NULL },
220   { "packets", 'x', 0, G_OPTION_ARG_NONE, &cap_packet_rate,
221     "display average packet rate (in packets/s)", NULL },
222   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
223 };
224
225 static GOptionEntry output_format_entries[] =
226 {
227   /* Output format */
228   { "long", 'L', 0, G_OPTION_ARG_NONE, &long_report,
229     "generate long report (default)", NULL },
230   { "Table", 'T', 0, G_OPTION_ARG_NONE, &table_report,
231     "generate table report", NULL },
232   { "machine-readable", 'M', 0, G_OPTION_ARG_NONE, &machine_readable,
233     "display machine-readable (unabbreviated) values in long reports", NULL },
234   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
235 };
236
237 static GOptionEntry table_report_entries[] =
238 {
239   /* Table report */
240   { "header-rec", 'R', 0, G_OPTION_ARG_NONE, &table_report_header,
241     "generate header record (default)", NULL },
242   { "no-table", 'r', 0, G_OPTION_ARG_NONE, &table_report_header,
243     "do not generate header record", NULL },
244   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
245 };
246
247 static GOptionEntry misc_entries[] =
248 {
249   { "helpcompat", 'h', 0, G_OPTION_ARG_NONE, &cap_help,
250     "display help", NULL },
251   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
252 };
253
254 GOptionContext *ctx;
255 GOptionGroup *general_grp, *size_grp, *time_grp, *stats_grp, *output_grp, *table_report_grp;
256 GError *parse_err = NULL;
257
258 #endif /* USE_GOPTION */
259
260 #ifdef HAVE_LIBGCRYPT
261 #define HASH_SIZE_SHA1   20
262 #define HASH_SIZE_RMD160 20
263 #define HASH_SIZE_MD5    16
264
265 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
266 #define HASH_BUF_SIZE (1024 * 1024)
267
268
269 static gchar file_sha1[HASH_STR_SIZE];
270 static gchar file_rmd160[HASH_STR_SIZE];
271 static gchar file_md5[HASH_STR_SIZE];
272
273 #define FILE_HASH_OPT "H"
274 #else
275 #define FILE_HASH_OPT ""
276 #endif /* HAVE_LIBGCRYPT */
277
278 /*
279  * If we have at least two packets with time stamps, and they're not in
280  * order - i.e., the later packet has a time stamp older than the earlier
281  * packet - the time stamps are known not to be in order.
282  *
283  * If every packet has a time stamp, and they're all in order, the time
284  * stamp is known to be in order.
285  *
286  * Otherwise, we have no idea.
287  */
288 typedef enum {
289   IN_ORDER,
290   NOT_IN_ORDER,
291   ORDER_UNKNOWN
292 } order_t;
293
294 typedef struct _capture_info {
295   const char    *filename;
296   guint16        file_type;
297   gboolean       iscompressed;
298   int            file_encap;
299   gint64         filesize;
300   gchar         *comment;
301
302   guint64        packet_bytes;
303   gboolean       times_known;
304   double         start_time;
305   double         stop_time;
306   guint32        packet_count;
307   gboolean       snap_set;                /* If set in capture file header      */
308   guint32        snaplen;                 /* value from the capture file header */
309   guint32        snaplen_min_inferred;    /* If caplen < len for 1 or more rcds */
310   guint32        snaplen_max_inferred;    /*  ...                               */
311   gboolean       drops_known;
312   guint32        drop_count;
313
314   double         duration;
315   double         packet_rate;
316   double         packet_size;
317   double         data_rate;              /* in bytes */
318   gboolean       know_order;
319   order_t        order;
320
321   int           *encap_counts;           /* array of per_packet encap counts; array has one entry per wtap_encap type */
322 } capture_info;
323
324
325 static void
326 enable_all_infos(void)
327 {
328   report_all_infos   = TRUE;
329
330   cap_file_type      = TRUE;
331   cap_file_encap     = TRUE;
332   cap_snaplen        = TRUE;
333   cap_packet_count   = TRUE;
334   cap_file_size      = TRUE;
335   cap_comment        = TRUE;
336
337   cap_data_size      = TRUE;
338   cap_duration       = TRUE;
339   cap_start_time     = TRUE;
340   cap_end_time       = TRUE;
341   cap_order          = TRUE;
342
343   cap_data_rate_byte = TRUE;
344   cap_data_rate_bit  = TRUE;
345   cap_packet_size    = TRUE;
346   cap_packet_rate    = TRUE;
347
348 #ifdef HAVE_LIBGCRYPT
349   cap_file_hashes    = TRUE;
350 #endif /* HAVE_LIBGCRYPT */
351 }
352
353 static void
354 disable_all_infos(void)
355 {
356   report_all_infos   = FALSE;
357
358   cap_file_type      = FALSE;
359   cap_file_encap     = FALSE;
360   cap_snaplen        = FALSE;
361   cap_packet_count   = FALSE;
362   cap_file_size      = FALSE;
363   cap_comment        = FALSE;
364
365   cap_data_size      = FALSE;
366   cap_duration       = FALSE;
367   cap_start_time     = FALSE;
368   cap_end_time       = FALSE;
369   cap_order          = FALSE;
370
371   cap_data_rate_byte = FALSE;
372   cap_data_rate_bit  = FALSE;
373   cap_packet_size    = FALSE;
374   cap_packet_rate    = FALSE;
375
376 #ifdef HAVE_LIBGCRYPT
377   cap_file_hashes    = FALSE;
378 #endif /* HAVE_LIBGCRYPT */
379 }
380
381 static const gchar *
382 order_string(order_t order)
383 {
384   switch (order) {
385
386     case IN_ORDER:
387       return "True";
388
389     case NOT_IN_ORDER:
390       return "False";
391
392     case ORDER_UNKNOWN:
393       return "Unknown";
394
395     default:
396       return "???";  /* "cannot happen" (the next step is "Profit!") */
397   }
398 }
399
400 static gchar *
401 time_string(time_t timer, capture_info *cf_info, gboolean want_lf)
402 {
403   const gchar  *lf = want_lf ? "\n" : "";
404   static gchar  time_string_buf[20];
405   char         *time_string_ctime;
406
407   if (cf_info->times_known && cf_info->packet_count > 0) {
408     if (time_as_secs) {
409       /* XXX - Would it be useful to show sub-second precision? */
410       g_snprintf(time_string_buf, 20, "%lu%s", (unsigned long)timer, lf);
411       return time_string_buf;
412     } else {
413       time_string_ctime = ctime(&timer);
414       if (time_string_ctime == NULL) {
415         g_snprintf(time_string_buf, 20, "Not representable%s", lf);
416         return time_string_buf;
417       }
418       if (!want_lf) {
419         /*
420          * The ctime() function returns a string formatted as:
421          *   "Www Mmm dd hh:mm:ss yyyy\n"
422          * The unwanted '\n' is the 24th character.
423          */
424         time_string_ctime[24] = '\0';
425       }
426       return time_string_ctime;
427     }
428   }
429
430   g_snprintf(time_string_buf, 15, "n/a%s", lf);
431   return time_string_buf;
432 }
433
434 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
435   if (value > 0.0)
436     printf("%s%.*f%s\n", text_p1, width, value, text_p2);
437   else
438     printf("%sn/a\n", text_p1);
439 }
440
441 static void
442 print_stats(const gchar *filename, capture_info *cf_info)
443 {
444   const gchar           *file_type_string, *file_encap_string;
445   time_t                 start_time_t;
446   time_t                 stop_time_t;
447   gchar                 *size_string;
448
449   /* Build printable strings for various stats */
450   file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
451   file_encap_string = wtap_encap_string(cf_info->file_encap);
452   start_time_t = (time_t)cf_info->start_time;
453   stop_time_t = (time_t)cf_info->stop_time;
454
455   if (filename)           printf     ("File name:           %s\n", filename);
456   if (cap_file_type)      printf     ("File type:           %s%s\n",
457       file_type_string,
458       cf_info->iscompressed ? " (gzip compressed)" : "");
459   if (cap_file_encap)     printf     ("File encapsulation:  %s\n", file_encap_string);
460   if (cap_file_encap && (cf_info->file_encap == WTAP_ENCAP_PER_PACKET)) {
461     int i;
462     for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
463       if (cf_info->encap_counts[i] > 0)
464         printf("                       %s\n", wtap_encap_string(i));
465     }
466   }
467   if (cap_snaplen && cf_info->snap_set)
468     printf     ("Packet size limit:   file hdr: %u bytes\n", cf_info->snaplen);
469   else if (cap_snaplen && !cf_info->snap_set)
470     printf     ("Packet size limit:   file hdr: (not set)\n");
471   if (cf_info->snaplen_max_inferred > 0) {
472     if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
473       printf     ("Packet size limit:   inferred: %u bytes\n", cf_info->snaplen_min_inferred);
474     else
475       printf     ("Packet size limit:   inferred: %u bytes - %u bytes (range)\n",
476           cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
477   }
478   if (cap_packet_count) {
479     printf     ("Number of packets:   ");
480     if (machine_readable) {
481       printf ("%u\n", cf_info->packet_count);
482     } else {
483       size_string = format_size(cf_info->packet_count, format_size_unit_none);
484       printf ("%s\n", size_string);
485       g_free(size_string);
486     }
487   }
488   if (cap_file_size) {
489     printf     ("File size:           ");
490     if (machine_readable) {
491       printf     ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
492     } else {
493       size_string = format_size(cf_info->filesize, format_size_unit_bytes);
494       printf ("%s\n", size_string);
495       g_free(size_string);
496     }
497   }
498   if (cap_data_size) {
499     printf     ("Data size:           ");
500     if (machine_readable) {
501       printf     ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
502     } else {
503       size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
504       printf ("%s\n", size_string);
505       g_free(size_string);
506     }
507   }
508   if (cf_info->times_known) {
509     if (cap_duration) /* XXX - shorten to hh:mm:ss */
510                           print_value("Capture duration:    ", 0, " seconds",   cf_info->duration);
511     if (cap_start_time)
512                           printf     ("Start time:          %s", time_string(start_time_t, cf_info, TRUE));
513     if (cap_end_time)
514                           printf     ("End time:            %s", time_string(stop_time_t, cf_info, TRUE));
515     if (cap_data_rate_byte) {
516                           printf     ("Data byte rate:      ");
517       if (machine_readable) {
518         print_value("", 2, " bytes/sec",   cf_info->data_rate);
519       } else {
520         size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
521         printf ("%s\n", size_string);
522         g_free(size_string);
523       }
524     }
525     if (cap_data_rate_bit) {
526                           printf     ("Data bit rate:       ");
527       if (machine_readable) {
528         print_value("", 2, " bits/sec",    cf_info->data_rate*8);
529       } else {
530         size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
531         printf ("%s\n", size_string);
532         g_free(size_string);
533       }
534     }
535   }
536   if (cap_packet_size)    printf     ("Average packet size: %.2f bytes\n",        cf_info->packet_size);
537   if (cf_info->times_known) {
538     if (cap_packet_rate) {
539                           printf     ("Average packet rate: ");
540       if (machine_readable) {
541         print_value("", 2, " packets/sec", cf_info->packet_rate);
542       } else {
543         size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_none);
544         printf ("%spackets/sec\n", size_string);
545         g_free(size_string);
546       }
547     }
548   }
549 #ifdef HAVE_LIBGCRYPT
550   if (cap_file_hashes) {
551     printf     ("SHA1:                %s\n", file_sha1);
552     printf     ("RIPEMD160:           %s\n", file_rmd160);
553     printf     ("MD5:                 %s\n", file_md5);
554   }
555 #endif /* HAVE_LIBGCRYPT */
556   if (cap_order)          printf     ("Strict time order:   %s\n", order_string(cf_info->order));
557   if (cap_comment && cf_info->comment)
558     printf     ("Capture comment:     %s\n", cf_info->comment);
559 }
560
561 static void
562 putsep(void)
563 {
564   if (field_separator) putchar(field_separator);
565 }
566
567 static void
568 putquote(void)
569 {
570   if (quote_char) putchar(quote_char);
571 }
572
573 static void
574 print_stats_table_header_label(const gchar *label)
575 {
576   putsep();
577   putquote();
578   printf("%s", label);
579   putquote();
580 }
581
582 static void
583 print_stats_table_header(void)
584 {
585   putquote();
586   printf("File name");
587   putquote();
588
589   if (cap_file_type)      print_stats_table_header_label("File type");
590   if (cap_file_encap)     print_stats_table_header_label("File encapsulation");
591   if (cap_snaplen)        print_stats_table_header_label("Packet size limit");
592   if (cap_snaplen)        print_stats_table_header_label("Packet size limit min (inferred)");
593   if (cap_snaplen)        print_stats_table_header_label("Packet size limit max (inferred)");
594   if (cap_packet_count)   print_stats_table_header_label("Number of packets");
595   if (cap_file_size)      print_stats_table_header_label("File size (bytes)");
596   if (cap_data_size)      print_stats_table_header_label("Data size (bytes)");
597   if (cap_duration)       print_stats_table_header_label("Capture duration (seconds)");
598   if (cap_start_time)     print_stats_table_header_label("Start time");
599   if (cap_end_time)       print_stats_table_header_label("End time");
600   if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
601   if (cap_data_rate_bit)  print_stats_table_header_label("Data bit rate (bits/sec)");
602   if (cap_packet_size)    print_stats_table_header_label("Average packet size (bytes)");
603   if (cap_packet_rate)    print_stats_table_header_label("Average packet rate (packets/sec)");
604 #ifdef HAVE_LIBGCRYPT
605   if (cap_file_hashes) {
606     print_stats_table_header_label("SHA1");
607     print_stats_table_header_label("RIPEMD160");
608     print_stats_table_header_label("MD5");
609   }
610 #endif /* HAVE_LIBGCRYPT */
611   if (cap_order)          print_stats_table_header_label("Strict time order");
612   if (cap_comment)        print_stats_table_header_label("Capture comment");
613
614   printf("\n");
615 }
616
617 static void
618 print_stats_table(const gchar *filename, capture_info *cf_info)
619 {
620   const gchar           *file_type_string, *file_encap_string;
621   time_t                 start_time_t;
622   time_t                 stop_time_t;
623
624   /* Build printable strings for various stats */
625   file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
626   file_encap_string = wtap_encap_string(cf_info->file_encap);
627   start_time_t = (time_t)cf_info->start_time;
628   stop_time_t = (time_t)cf_info->stop_time;
629
630   if (filename) {
631     putquote();
632     printf("%s", filename);
633     putquote();
634   }
635
636   if (cap_file_type) {
637     putsep();
638     putquote();
639     printf("%s", file_type_string);
640     putquote();
641   }
642
643   /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
644    *       Output a line for each different encap with all fields repeated except
645    *        the encapsulation field which has "Per Packet: ..." for each
646    *        encapsulation type seen ?
647    */
648   if (cap_file_encap) {
649     putsep();
650     putquote();
651     printf("%s", file_encap_string);
652     putquote();
653   }
654
655   if (cap_snaplen) {
656     putsep();
657     putquote();
658     if (cf_info->snap_set)
659       printf("%u", cf_info->snaplen);
660     else
661       printf("(not set)");
662     putquote();
663     if (cf_info->snaplen_max_inferred > 0) {
664       putsep();
665       putquote();
666       printf("%u", cf_info->snaplen_min_inferred);
667       putquote();
668       putsep();
669       putquote();
670       printf("%u", cf_info->snaplen_max_inferred);
671       putquote();
672     }
673     else {
674       putsep();
675       putquote();
676       printf("n/a");
677       putquote();
678       putsep();
679       putquote();
680       printf("n/a");
681       putquote();
682     }
683   }
684
685   if (cap_packet_count) {
686     putsep();
687     putquote();
688     printf("%u", cf_info->packet_count);
689     putquote();
690   }
691
692   if (cap_file_size) {
693     putsep();
694     putquote();
695     printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
696     putquote();
697   }
698
699   if (cap_data_size) {
700     putsep();
701     putquote();
702     printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
703     putquote();
704   }
705
706   if (cap_duration) {
707     putsep();
708     putquote();
709     if (cf_info->times_known)
710       printf("%f", cf_info->duration);
711     else
712       printf("n/a");
713     putquote();
714   }
715
716   if (cap_start_time) {
717     putsep();
718     putquote();
719     printf("%s", time_string(start_time_t, cf_info, FALSE));
720     putquote();
721   }
722
723   if (cap_end_time) {
724     putsep();
725     putquote();
726     printf("%s", time_string(stop_time_t, cf_info, FALSE));
727     putquote();
728   }
729
730   if (cap_data_rate_byte) {
731     putsep();
732     putquote();
733     if (cf_info->times_known)
734       printf("%.2f", cf_info->data_rate);
735     else
736       printf("n/a");
737     putquote();
738   }
739
740   if (cap_data_rate_bit) {
741     putsep();
742     putquote();
743     if (cf_info->times_known)
744       printf("%.2f", cf_info->data_rate*8);
745     else
746       printf("n/a");
747     putquote();
748   }
749
750   if (cap_packet_size) {
751     putsep();
752     putquote();
753     printf("%.2f", cf_info->packet_size);
754     putquote();
755   }
756
757   if (cap_packet_rate) {
758     putsep();
759     putquote();
760     if (cf_info->times_known)
761       printf("%.2f", cf_info->packet_rate);
762     else
763       printf("n/a");
764     putquote();
765   }
766
767 #ifdef HAVE_LIBGCRYPT
768   if (cap_file_hashes) {
769     putsep();
770     putquote();
771     printf("%s", file_sha1);
772     putquote();
773
774     putsep();
775     putquote();
776     printf("%s", file_rmd160);
777     putquote();
778
779     putsep();
780     putquote();
781     printf("%s", file_md5);
782     putquote();
783   }
784 #endif /* HAVE_LIBGCRYPT */
785
786   if (cap_order) {
787     putsep();
788     putquote();
789     printf("%s", order_string(cf_info->order));
790     putquote();
791   }
792
793   if (cap_comment) {
794     putsep();
795     putquote();
796     printf("%s", cf_info->comment);
797     putquote();
798   }
799
800
801   printf("\n");
802 }
803
804 static int
805 process_cap_file(wtap *wth, const char *filename)
806 {
807   int                   status = 0;
808   int                   err;
809   gchar                *err_info;
810   gint64                size;
811   gint64                data_offset;
812
813   guint32               packet = 0;
814   gint64                bytes  = 0;
815   guint32               snaplen_min_inferred = 0xffffffff;
816   guint32               snaplen_max_inferred =          0;
817   const struct wtap_pkthdr *phdr;
818   capture_info          cf_info;
819   gboolean              have_times = TRUE;
820   double                start_time = 0;
821   double                stop_time  = 0;
822   double                cur_time   = 0;
823   double                prev_time = 0;
824   gboolean              know_order = FALSE;
825   order_t               order = IN_ORDER;
826   wtapng_section_t     *shb_inf;
827   gchar                *p;
828
829
830   cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
831
832   /* Tally up data that we need to parse through the file to find */
833   while (wtap_read(wth, &err, &err_info, &data_offset))  {
834     phdr = wtap_phdr(wth);
835     if (phdr->presence_flags & WTAP_HAS_TS) {
836       prev_time = cur_time;
837       cur_time = nstime_to_sec(&phdr->ts);
838       if (packet == 0) {
839         start_time = cur_time;
840         stop_time  = cur_time;
841         prev_time  = cur_time;
842       }
843       if (cur_time < prev_time) {
844         order = NOT_IN_ORDER;
845       }
846       if (cur_time < start_time) {
847         start_time = cur_time;
848       }
849       if (cur_time > stop_time) {
850         stop_time = cur_time;
851       }
852     } else {
853       have_times = FALSE; /* at least one packet has no time stamp */
854       if (order != NOT_IN_ORDER)
855         order = ORDER_UNKNOWN;
856     }
857
858     bytes+=phdr->len;
859     packet++;
860
861     /* If caplen < len for a rcd, then presumably           */
862     /* 'Limit packet capture length' was done for this rcd. */
863     /* Keep track as to the min/max actual snapshot lengths */
864     /*  seen for this file.                                 */
865     if (phdr->caplen < phdr->len) {
866       if (phdr->caplen < snaplen_min_inferred)
867         snaplen_min_inferred = phdr->caplen;
868       if (phdr->caplen > snaplen_max_inferred)
869         snaplen_max_inferred = phdr->caplen;
870     }
871
872     /* Per-packet encapsulation */
873     if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
874       if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
875         cf_info.encap_counts[phdr->pkt_encap] += 1;
876       } else {
877         fprintf(stderr, "capinfos: Unknown per-packet encapsulation: %d [frame number: %d]\n", phdr->pkt_encap, packet);
878       }
879     }
880
881   } /* while */
882
883   if (err != 0) {
884     fprintf(stderr,
885         "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
886         packet, filename, wtap_strerror(err));
887     switch (err) {
888
889       case WTAP_ERR_SHORT_READ:
890         status = 1;
891         fprintf(stderr,
892           "  (will continue anyway, checksums might be incorrect)\n");
893         break;
894
895       case WTAP_ERR_UNSUPPORTED:
896       case WTAP_ERR_UNSUPPORTED_ENCAP:
897       case WTAP_ERR_BAD_FILE:
898       case WTAP_ERR_DECOMPRESS:
899         fprintf(stderr, "(%s)\n", err_info);
900         g_free(err_info);
901         /* fallthrough */
902
903       default:
904         g_free(cf_info.encap_counts);
905         return 1;
906     }
907   }
908
909   /* File size */
910   size = wtap_file_size(wth, &err);
911   if (size == -1) {
912     fprintf(stderr,
913         "capinfos: Can't get size of \"%s\": %s.\n",
914         filename, g_strerror(err));
915     g_free(cf_info.encap_counts);
916     return 1;
917   }
918
919   cf_info.filesize = size;
920
921   /* File Type */
922   cf_info.file_type = wtap_file_type_subtype(wth);
923   cf_info.iscompressed = wtap_iscompressed(wth);
924
925   /* File Encapsulation */
926   cf_info.file_encap = wtap_file_encap(wth);
927
928   /* Packet size limit (snaplen) */
929   cf_info.snaplen = wtap_snapshot_length(wth);
930   if (cf_info.snaplen > 0)
931     cf_info.snap_set = TRUE;
932   else
933     cf_info.snap_set = FALSE;
934
935   cf_info.snaplen_min_inferred = snaplen_min_inferred;
936   cf_info.snaplen_max_inferred = snaplen_max_inferred;
937
938   /* # of packets */
939   cf_info.packet_count = packet;
940
941   /* File Times */
942   cf_info.times_known = have_times;
943   cf_info.start_time = start_time;
944   cf_info.stop_time = stop_time;
945   cf_info.duration = stop_time-start_time;
946   cf_info.know_order = know_order;
947   cf_info.order = order;
948
949   /* Number of packet bytes */
950   cf_info.packet_bytes = bytes;
951
952   cf_info.data_rate   = 0.0;
953   cf_info.packet_rate = 0.0;
954   cf_info.packet_size = 0.0;
955
956   if (packet > 0) {
957     if (cf_info.duration > 0.0) {
958       cf_info.data_rate   = (double)bytes  / (stop_time-start_time); /* Data rate per second */
959       cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
960     }
961     cf_info.packet_size = (double)bytes / packet;                  /* Avg packet size      */
962   }
963
964   cf_info.comment = NULL;
965   shb_inf = wtap_file_get_shb_info(wth);
966   if (shb_inf) {
967     /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
968     cf_info.comment = g_strdup(shb_inf->opt_comment);
969   }
970   g_free(shb_inf);
971   if (cf_info.comment) {
972     /* multi-line comments would conflict with the formatting that capinfos uses
973        we replace linefeeds with spaces */
974     p = cf_info.comment;
975     while (*p != '\0') {
976       if (*p == '\n')
977         *p = ' ';
978       p++;
979     }
980   }
981
982   if (long_report) {
983     print_stats(filename, &cf_info);
984   } else {
985     print_stats_table(filename, &cf_info);
986   }
987
988   g_free(cf_info.encap_counts);
989   g_free(cf_info.comment);
990
991   return status;
992 }
993
994 static void
995 usage(gboolean is_error)
996 {
997   FILE *output;
998
999   if (!is_error) {
1000     output = stdout;
1001     /* XXX - add capinfos header info here */
1002   }
1003   else {
1004     output = stderr;
1005   }
1006
1007   fprintf(output, "Capinfos %s"
1008 #ifdef GITVERSION
1009       " (" GITVERSION " from " GITBRANCH ")"
1010 #endif
1011       "\n", VERSION);
1012   fprintf(output, "Prints various information (infos) about capture files.\n");
1013   fprintf(output, "See http://www.wireshark.org for more information.\n");
1014   fprintf(output, "\n");
1015   fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1016   fprintf(output, "\n");
1017   fprintf(output, "General infos:\n");
1018   fprintf(output, "  -t display the capture file type\n");
1019   fprintf(output, "  -E display the capture file encapsulation\n");
1020 #ifdef HAVE_LIBGCRYPT
1021   fprintf(output, "  -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1022 #endif
1023   fprintf(output, "  -k display the capture comment\n");
1024   fprintf(output, "\n");
1025   fprintf(output, "Size infos:\n");
1026   fprintf(output, "  -c display the number of packets\n");
1027   fprintf(output, "  -s display the size of the file (in bytes)\n");
1028   fprintf(output, "  -d display the total length of all packets (in bytes)\n");
1029   fprintf(output, "  -l display the packet size limit (snapshot length)\n");
1030   fprintf(output, "\n");
1031   fprintf(output, "Time infos:\n");
1032   fprintf(output, "  -u display the capture duration (in seconds)\n");
1033   fprintf(output, "  -a display the capture start time\n");
1034   fprintf(output, "  -e display the capture end time\n");
1035   fprintf(output, "  -o display the capture file chronological status (True/False)\n");
1036   fprintf(output, "  -S display start and end times as seconds\n");
1037   fprintf(output, "\n");
1038   fprintf(output, "Statistic infos:\n");
1039   fprintf(output, "  -y display average data rate (in bytes/sec)\n");
1040   fprintf(output, "  -i display average data rate (in bits/sec)\n");
1041   fprintf(output, "  -z display average packet size (in bytes)\n");
1042   fprintf(output, "  -x display average packet rate (in packets/sec)\n");
1043   fprintf(output, "\n");
1044   fprintf(output, "Output format:\n");
1045   fprintf(output, "  -L generate long report (default)\n");
1046   fprintf(output, "  -T generate table report\n");
1047   fprintf(output, "  -M display machine-readable values in long reports\n");
1048   fprintf(output, "\n");
1049   fprintf(output, "Table report options:\n");
1050   fprintf(output, "  -R generate header record (default)\n");
1051   fprintf(output, "  -r do not generate header record\n");
1052   fprintf(output, "\n");
1053   fprintf(output, "  -B separate infos with TAB character (default)\n");
1054   fprintf(output, "  -m separate infos with comma (,) character\n");
1055   fprintf(output, "  -b separate infos with SPACE character\n");
1056   fprintf(output, "\n");
1057   fprintf(output, "  -N do not quote infos (default)\n");
1058   fprintf(output, "  -q quote infos with single quotes (')\n");
1059   fprintf(output, "  -Q quote infos with double quotes (\")\n");
1060   fprintf(output, "\n");
1061   fprintf(output, "Miscellaneous:\n");
1062   fprintf(output, "  -h display this help and exit\n");
1063   fprintf(output, "  -C cancel processing if file open fails (default is to continue)\n");
1064   fprintf(output, "  -A generate all infos (default)\n");
1065   fprintf(output, "\n");
1066   fprintf(output, "Options are processed from left to right order with later options superceding\n");
1067   fprintf(output, "or adding to earlier options.\n");
1068   fprintf(output, "\n");
1069   fprintf(output, "If no options are given the default is to display all infos in long report\n");
1070   fprintf(output, "output format.\n");
1071 #ifndef HAVE_LIBGCRYPT
1072   fprintf(output, "\nFile hashing support (-H) is not present.\n");
1073 #endif
1074 }
1075
1076 #ifdef HAVE_PLUGINS
1077 /*
1078  *  Don't report failures to load plugins because most (non-wiretap) plugins
1079  *  *should* fail to load (because we're not linked against libwireshark and
1080  *  dissector plugins need libwireshark).
1081  */
1082 static void
1083 failure_message(const char *msg_format _U_, va_list ap _U_)
1084 {
1085   return;
1086 }
1087 #endif
1088
1089 #ifdef HAVE_LIBGCRYPT
1090 static void
1091 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1092   int i;
1093
1094   for (i = 0; i < (int) length; i++) {
1095     g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1096   }
1097 }
1098 #endif /* HAVE_LIBGCRYPT */
1099
1100 int
1101 main(int argc, char *argv[])
1102 {
1103   wtap  *wth;
1104   int    err;
1105   gchar *err_info;
1106   int    opt;
1107   int    overall_error_status;
1108
1109   int status = 0;
1110 #ifdef HAVE_PLUGINS
1111   char  *init_progfile_dir_error;
1112 #endif
1113 #ifdef HAVE_LIBGCRYPT
1114   FILE  *fh;
1115   char  *hash_buf = NULL;
1116   gcry_md_hd_t hd = NULL;
1117   size_t hash_bytes;
1118 #endif
1119
1120 #ifdef _WIN32
1121   arg_list_utf_16to8(argc, argv);
1122   create_app_running_mutex();
1123 #endif /* _WIN32 */
1124
1125   /*
1126    * Get credential information for later use.
1127    */
1128   init_process_policies();
1129   init_open_routines();
1130
1131 #ifdef HAVE_PLUGINS
1132   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1133     g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1134     g_free(init_progfile_dir_error);
1135   } else {
1136     /* Register all the plugin types we have. */
1137     wtap_register_plugin_types(); /* Types known to libwiretap */
1138
1139     init_report_err(failure_message, NULL, NULL, NULL);
1140
1141     /* Scan for plugins.  This does *not* call their registration routines;
1142        that's done later. */
1143     scan_plugins();
1144
1145     /* Register all libwiretap plugin modules. */
1146     register_all_wiretap_modules();
1147   }
1148 #endif
1149
1150   /* Process the options */
1151 #ifdef USE_GOPTION
1152   ctx = g_option_context_new(" <infile> ... - print information about capture file(s)");
1153   general_grp = g_option_group_new("gen", "General infos:",
1154       "Show general options", NULL, NULL);
1155   size_grp = g_option_group_new("size", "Size infos:",
1156       "Show size options", NULL, NULL);
1157   time_grp = g_option_group_new("time", "Time infos:",
1158       "Show time options", NULL, NULL);
1159   stats_grp = g_option_group_new("stats", "Statistics infos:",
1160       "Show statistics options", NULL, NULL);
1161   output_grp = g_option_group_new("output", "Output format:",
1162       "Show output format options", NULL, NULL);
1163   table_report_grp = g_option_group_new("table", "Table report options:",
1164       "Show table report options", NULL, NULL);
1165   g_option_group_add_entries(general_grp, general_entries);
1166   g_option_group_add_entries(size_grp, size_entries);
1167   g_option_group_add_entries(time_grp, time_entries);
1168   g_option_group_add_entries(stats_grp, stats_entries);
1169   g_option_group_add_entries(output_grp, output_format_entries);
1170   g_option_group_add_entries(table_report_grp, table_report_entries);
1171   g_option_context_add_main_entries(ctx, misc_entries, NULL);
1172   g_option_context_add_group(ctx, general_grp);
1173   g_option_context_add_group(ctx, size_grp);
1174   g_option_context_add_group(ctx, time_grp);
1175   g_option_context_add_group(ctx, stats_grp);
1176   g_option_context_add_group(ctx, output_grp);
1177   g_option_context_add_group(ctx, table_report_grp);
1178   /* There's probably a better way to do this, but this works for now.
1179      GOptions displays the name in argv[0] as the name of the
1180      application.  This is reasonable, but because we actually have a
1181      script wrapper that calls the executable.  The name that gets
1182      displayed is not exactly the same as the command the user used
1183      ran.
1184    */
1185   argv[0] = (char *)"capinfos";
1186
1187   /* if we have at least one cmdline option, we disable printing all infos */
1188   if (argc > 2 && report_all_infos)
1189     disable_all_infos();
1190
1191   if ( !g_option_context_parse(ctx, &argc, &argv, &parse_err) ) {
1192     if (parse_err)
1193       g_printerr ("option parsing failed: %s\n", parse_err->message);
1194     g_printerr("%s", g_option_context_get_help (ctx, TRUE, NULL));
1195     exit(1);
1196   }
1197   if ( cap_help ) {
1198     g_print("%s", g_option_context_get_help (ctx, FALSE, NULL));
1199     exit(0);
1200   }
1201   if ( argc < 2 ) {
1202     g_printerr("%s", g_option_context_get_help (ctx, FALSE, NULL));
1203     exit(1);
1204   }
1205   g_option_context_free(ctx);
1206
1207 #endif /* USE_GOPTION */
1208   while ((opt = getopt(argc, argv, "tEcs" FILE_HASH_OPT "dluaeyizvhxokCALTMRrSNqQBmb")) !=-1) {
1209
1210     switch (opt) {
1211
1212       case 't':
1213         if (report_all_infos) disable_all_infos();
1214         cap_file_type = TRUE;
1215         break;
1216
1217       case 'E':
1218         if (report_all_infos) disable_all_infos();
1219         cap_file_encap = TRUE;
1220         break;
1221
1222       case 'l':
1223         if (report_all_infos) disable_all_infos();
1224         cap_snaplen = TRUE;
1225         break;
1226
1227       case 'c':
1228         if (report_all_infos) disable_all_infos();
1229         cap_packet_count = TRUE;
1230         break;
1231
1232       case 's':
1233         if (report_all_infos) disable_all_infos();
1234         cap_file_size = TRUE;
1235         break;
1236
1237       case 'd':
1238         if (report_all_infos) disable_all_infos();
1239         cap_data_size = TRUE;
1240         break;
1241
1242       case 'u':
1243         if (report_all_infos) disable_all_infos();
1244         cap_duration = TRUE;
1245         break;
1246
1247       case 'a':
1248         if (report_all_infos) disable_all_infos();
1249         cap_start_time = TRUE;
1250         break;
1251
1252       case 'e':
1253         if (report_all_infos) disable_all_infos();
1254         cap_end_time = TRUE;
1255         break;
1256
1257       case 'S':
1258         time_as_secs = TRUE;
1259         break;
1260
1261       case 'y':
1262         if (report_all_infos) disable_all_infos();
1263         cap_data_rate_byte = TRUE;
1264         break;
1265
1266       case 'i':
1267         if (report_all_infos) disable_all_infos();
1268         cap_data_rate_bit = TRUE;
1269         break;
1270
1271       case 'z':
1272         if (report_all_infos) disable_all_infos();
1273         cap_packet_size = TRUE;
1274         break;
1275
1276       case 'x':
1277         if (report_all_infos) disable_all_infos();
1278         cap_packet_rate = TRUE;
1279         break;
1280
1281 #ifdef HAVE_LIBGCRYPT
1282       case 'H':
1283         if (report_all_infos) disable_all_infos();
1284         cap_file_hashes = TRUE;
1285         break;
1286 #endif
1287
1288       case 'o':
1289         if (report_all_infos) disable_all_infos();
1290         cap_order = TRUE;
1291         break;
1292
1293       case 'k':
1294         if (report_all_infos) disable_all_infos();
1295         cap_comment = TRUE;
1296         break;
1297
1298       case 'C':
1299         continue_after_wtap_open_offline_failure = FALSE;
1300         break;
1301
1302       case 'A':
1303         enable_all_infos();
1304         break;
1305
1306       case 'L':
1307         long_report = TRUE;
1308         break;
1309
1310       case 'T':
1311         long_report = FALSE;
1312         break;
1313
1314       case 'M':
1315         machine_readable = TRUE;
1316         break;
1317
1318       case 'R':
1319         table_report_header = TRUE;
1320         break;
1321
1322       case 'r':
1323         table_report_header = FALSE;
1324         break;
1325
1326       case 'N':
1327         quote_char = '\0';
1328         break;
1329
1330       case 'q':
1331         quote_char = '\'';
1332         break;
1333
1334       case 'Q':
1335         quote_char = '"';
1336         break;
1337
1338       case 'B':
1339         field_separator = '\t';
1340         break;
1341
1342       case 'm':
1343         field_separator = ',';
1344         break;
1345
1346       case 'b':
1347         field_separator = ' ';
1348         break;
1349
1350       case 'h':
1351         usage(FALSE);
1352         exit(0);
1353         break;
1354
1355       case '?':              /* Bad flag - print usage message */
1356         usage(TRUE);
1357         exit(1);
1358         break;
1359     }
1360   }
1361
1362   /* Set the C-language locale to the native environment. */
1363   setlocale(LC_ALL, "");
1364
1365   if ((argc - optind) < 1) {
1366     usage(TRUE);
1367     exit(1);
1368   }
1369
1370   if (!long_report && table_report_header) {
1371     print_stats_table_header();
1372   }
1373
1374 #ifdef HAVE_LIBGCRYPT
1375   if (cap_file_hashes) {
1376     gcry_check_version(NULL);
1377     gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1378     if (hd) {
1379       gcry_md_enable(hd, GCRY_MD_RMD160);
1380       gcry_md_enable(hd, GCRY_MD_MD5);
1381     }
1382     hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1383   }
1384 #endif
1385
1386   overall_error_status = 0;
1387
1388   for (opt = optind; opt < argc; opt++) {
1389
1390 #ifdef HAVE_LIBGCRYPT
1391     g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1392     g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1393     g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1394
1395     if (cap_file_hashes) {
1396       fh = ws_fopen(argv[opt], "rb");
1397       if (fh && hd) {
1398         while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1399           gcry_md_write(hd, hash_buf, hash_bytes);
1400         }
1401         gcry_md_final(hd);
1402         hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1403         hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1404         hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1405       }
1406       if (fh) fclose(fh);
1407       if (hd) gcry_md_reset(hd);
1408     }
1409 #endif /* HAVE_LIBGCRYPT */
1410
1411     wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1412
1413     if (!wth) {
1414       fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1415           wtap_strerror(err));
1416       switch (err) {
1417
1418         case WTAP_ERR_UNSUPPORTED:
1419         case WTAP_ERR_UNSUPPORTED_ENCAP:
1420         case WTAP_ERR_BAD_FILE:
1421           fprintf(stderr, "(%s)\n", err_info);
1422           g_free(err_info);
1423           break;
1424       }
1425       overall_error_status = 1; /* remember that an error has occurred */
1426       if (!continue_after_wtap_open_offline_failure)
1427         exit(1); /* error status */
1428     }
1429
1430     if (wth) {
1431       if ((opt > optind) && (long_report))
1432         printf("\n");
1433       status = process_cap_file(wth, argv[opt]);
1434
1435       wtap_close(wth);
1436       if (status)
1437         exit(status);
1438     }
1439   }
1440
1441   return overall_error_status;
1442 }
1443
1444 /*
1445  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
1446  *
1447  * Local variables:
1448  * c-basic-offset: 2
1449  * tab-width: 8
1450  * indent-tabs-mode: nil
1451  * End:
1452  *
1453  * vi: set shiftwidth=2 tabstop=8 expandtab:
1454  * :indentSize=2:tabSize=8:noTabs=true:
1455  */