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