Call mac_lte_drx_control_element_received and make it static again
[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
80 /*
81  * The symbols declared in the below are exported from libwireshark,
82  * but we don't want to link whole libwireshark to editcap.
83  * We link the object directly instead and this needs a little trick
84  * with the WS_BUILD_DLL #define.
85  */
86 #define WS_BUILD_DLL
87 #define RESET_SYMBOL_EXPORT
88 #include <epan/filesystem.h>
89 #include <epan/plugins.h>
90 #undef WS_BUILD_DLL
91 #define RESET_SYMBOL_EXPORT
92
93 #include "wtap.h"
94 #include <wsutil/report_err.h>
95 #include <wsutil/privileges.h>
96 #include <wsutil/str_util.h>
97
98 #ifdef HAVE_LIBGCRYPT
99 #include <wsutil/wsgcrypt.h>
100 #include <wsutil/file_util.h>
101 #endif
102
103 #ifndef HAVE_GETOPT
104 #include "wsutil/wsgetopt.h"
105 #endif
106
107 #ifdef _WIN32
108 #include <wsutil/unicode-utils.h>
109 #endif /* _WIN32 */
110
111 #include "svnversion.h"
112
113 /*
114  * By default capinfos now continues processing
115  * the next filename if and when wiretap detects
116  * a problem opening a file.
117  * Use the '-C' option to revert back to original
118  * capinfos behavior which is to abort any
119  * additional file processing at first open file
120  * failure.
121  */
122
123 static gboolean continue_after_wtap_open_offline_failure = TRUE;
124
125 /*
126  * table report variables
127  */
128
129 static gboolean long_report = TRUE;         /* By default generate long report       */
130 static gchar table_report_header = TRUE;    /* Generate column header by default     */
131 static gchar field_separator = '\t';        /* Use TAB as field separator by default */
132 static gchar quote_char = '\0';             /* Do NOT quote fields by default        */
133 static gboolean machine_readable = FALSE;   /* Display machine-readable numbers      */
134
135 /*
136  * capinfos has the ability to report on a number of
137  * various characteristics ("infos") for each input file.
138  *
139  * By default reporting of all info fields is enabled.
140  *
141  * Optionally the reporting of any specific info field
142  * or combination of info fields can be enabled with
143  * individual options.
144  */
145
146 static gboolean report_all_infos = TRUE;    /* Report all infos           */
147
148 static gboolean cap_file_type = TRUE;       /* Report capture type        */
149 static gboolean cap_file_encap = TRUE;      /* Report encapsulation       */
150 static gboolean cap_snaplen = TRUE;         /* Packet size limit (snaplen)*/
151 static gboolean cap_packet_count = TRUE;    /* Report packet count        */
152 static gboolean cap_file_size = TRUE;       /* Report file size           */
153 static gboolean cap_comment = TRUE;         /* Display the capture comment */
154
155 static gboolean cap_data_size = TRUE;       /* Report packet byte size    */
156 static gboolean cap_duration = TRUE;        /* Report capture duration    */
157 static gboolean cap_start_time = TRUE;      /* Report capture start time  */
158 static gboolean cap_end_time = TRUE;        /* Report capture end time    */
159 static gboolean time_as_secs = FALSE;       /* Report time values as raw seconds */
160
161 static gboolean cap_data_rate_byte = TRUE;  /* Report data rate bytes/sec */
162 static gboolean cap_data_rate_bit = TRUE;   /* Report data rate bites/sec */
163 static gboolean cap_packet_size = TRUE;     /* Report average packet size */
164 static gboolean cap_packet_rate = TRUE;     /* Report average packet rate */
165 static gboolean cap_order = TRUE;           /* Report if packets are in chronological order (True/False) */
166
167 #ifdef HAVE_LIBGCRYPT
168 static gboolean cap_file_hashes = TRUE;     /* Calculate file hashes */
169 #endif
170
171 #ifdef USE_GOPTION
172 static gboolean cap_help = FALSE;
173 static gboolean table_report = FALSE;
174
175 static GOptionEntry general_entries[] =
176 {
177   /* General */
178   { "type", 't', 0, G_OPTION_ARG_NONE, &cap_file_type,
179     "display the capture file type", NULL },
180   { "Encapsulation", 'E', 0, G_OPTION_ARG_NONE, &cap_file_encap,
181     "display the capture file encapsulation", NULL },
182 #ifdef HAVE_LIBGCRYPT
183   { "Hash", 'H', 0, G_OPTION_ARG_NONE, &cap_file_hashes,
184     "display the SHA1, RMD160, and MD5 hashes of the file", NULL },
185 #endif /* HAVE_LIBGCRYPT */
186   { "capture-comment", 'k', 0, G_OPTION_ARG_NONE, &cap_comment,
187     "display the capture comment ", NULL },
188   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
189 };
190 static GOptionEntry size_entries[] =
191 {
192   /* Size */
193   { "packets", 'c', 0, G_OPTION_ARG_NONE, &cap_packet_count,
194     "display the number of packets", NULL },
195   { "size", 's', 0, G_OPTION_ARG_NONE, &cap_file_size,
196     "display the size of the file (in bytes)", NULL },
197   { "tot-len-of-pkts", 'd', 0, G_OPTION_ARG_NONE, &cap_data_size,
198     "display the total length of all packets (in bytes)", NULL },
199   { "snap", 'l', 0, G_OPTION_ARG_NONE, &cap_snaplen,
200     "display the packet size limit (snapshot length)", NULL },
201   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
202 };
203 static GOptionEntry time_entries[] =
204 {
205   /* Time */
206   { "duration", 'u', 0, G_OPTION_ARG_NONE, &cap_duration,
207     "display the capture duration (in seconds)", NULL },
208   { "start", 'a', 0, G_OPTION_ARG_NONE, &cap_start_time,
209     "display the capture start time", NULL },
210   { "end", 'e', 0, G_OPTION_ARG_NONE, &cap_end_time,
211     "display the capture end time", NULL },
212   { "cron", 'o', 0, G_OPTION_ARG_NONE, &cap_order,
213     "display the capture file chronological status (True/False)", NULL },
214   { "start-end-time-sec", 'S', 0, G_OPTION_ARG_NONE, &time_as_secs,
215     "display start and end times as seconds", NULL },
216   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
217 };
218
219 static GOptionEntry stats_entries[] =
220 {
221   /* Statistics */
222   { "bytes", 'y', 0, G_OPTION_ARG_NONE, &cap_data_rate_byte,
223     "display average data rate (in bytes/s)", NULL },
224   { "bits", 'i', 0, G_OPTION_ARG_NONE, &cap_data_rate_bit,
225     "display average data rate (in bits/s)", NULL },
226   { "packet-bytes", 'z', 0, G_OPTION_ARG_NONE, &cap_packet_size,
227     "display average packet size (in bytes)", NULL },
228   { "packets", 'x', 0, G_OPTION_ARG_NONE, &cap_packet_rate,
229     "display average packet rate (in packets/s)", NULL },
230   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
231 };
232
233 static GOptionEntry output_format_entries[] =
234 {
235   /* Output format */
236   { "long", 'L', 0, G_OPTION_ARG_NONE, &long_report,
237     "generate long report (default)", NULL },
238   { "Table", 'T', 0, G_OPTION_ARG_NONE, &table_report,
239     "generate table report", NULL },
240   { "machine-readable", 'M', 0, G_OPTION_ARG_NONE, &machine_readable,
241     "display machine-readable (unabbreviated) values in long reports", NULL },
242   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
243 };
244
245 static GOptionEntry table_report_entries[] =
246 {
247   /* Table report */
248   { "header-rec", 'R', 0, G_OPTION_ARG_NONE, &table_report_header,
249     "generate header record (default)", NULL },
250   { "no-table", 'r', 0, G_OPTION_ARG_NONE, &table_report_header,
251     "do not generate header record", NULL },
252   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
253 };
254
255 static GOptionEntry misc_entries[] =
256 {
257   { "helpcompat", 'h', 0, G_OPTION_ARG_NONE, &cap_help,
258     "display help", NULL },
259   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
260 };
261
262 GOptionContext *ctx;
263 GOptionGroup *general_grp, *size_grp, *time_grp, *stats_grp, *output_grp, *table_report_grp;
264 GError *parse_err = NULL;
265
266 #endif /* USE_GOPTION */
267
268 #ifdef HAVE_LIBGCRYPT
269 #define HASH_SIZE_SHA1 20
270 #define HASH_SIZE_RMD160 20
271 #define HASH_SIZE_MD5 16
272
273 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
274 #define HASH_BUF_SIZE (1024 * 1024)
275
276
277 static gchar file_sha1[HASH_STR_SIZE];
278 static gchar file_rmd160[HASH_STR_SIZE];
279 static gchar file_md5[HASH_STR_SIZE];
280
281 #define FILE_HASH_OPT "H"
282 #else
283 #define FILE_HASH_OPT ""
284 #endif /* HAVE_LIBGCRYPT */
285
286 /*
287  * If we have at least two packets with time stamps, and they're not in
288  * order - i.e., the later packet has a time stamp older than the earlier
289  * packet - the time stamps are known not to be in order.
290  *
291  * If every packet has a time stamp, and they're all in order, the time
292  * stamp is known to be in order.
293  *
294  * Otherwise, we have no idea.
295  */
296 typedef enum {
297   IN_ORDER,
298   NOT_IN_ORDER,
299   ORDER_UNKNOWN
300 } order_t;
301
302 typedef struct _capture_info {
303   const char    *filename;
304   guint16       file_type;
305   gboolean      iscompressed;
306   int           file_encap;
307   gint64        filesize;
308   gchar         *comment;
309
310   guint64       packet_bytes;
311   gboolean      times_known;
312   double        start_time;
313   double        stop_time;
314   guint32       packet_count;
315   gboolean      snap_set;                /* If set in capture file header      */
316   guint32       snaplen;                 /* value from the capture file header */
317   guint32       snaplen_min_inferred;    /* If caplen < len for 1 or more rcds */
318   guint32       snaplen_max_inferred;    /*  ...                               */
319   gboolean      drops_known;
320   guint32       drop_count;
321
322   double        duration;
323   double        packet_rate;
324   double        packet_size;
325   double        data_rate;              /* in bytes */
326   gboolean      know_order;
327   order_t       order;
328
329   int          *encap_counts;           /* array of per_packet encap counts; array has one entry per wtap_encap type */
330 } capture_info;
331
332
333 static void
334 enable_all_infos(void)
335 {
336   report_all_infos = TRUE;
337
338   cap_file_type = TRUE;
339   cap_file_encap = TRUE;
340   cap_snaplen = TRUE;
341   cap_packet_count = TRUE;
342   cap_file_size = TRUE;
343   cap_comment = TRUE;
344
345   cap_data_size = TRUE;
346   cap_duration = TRUE;
347   cap_start_time = TRUE;
348   cap_end_time = TRUE;
349   cap_order = TRUE;
350
351   cap_data_rate_byte = TRUE;
352   cap_data_rate_bit = TRUE;
353   cap_packet_size = TRUE;
354   cap_packet_rate = TRUE;
355
356 #ifdef HAVE_LIBGCRYPT
357   cap_file_hashes = TRUE;
358 #endif /* HAVE_LIBGCRYPT */
359 }
360
361 static void
362 disable_all_infos(void)
363 {
364   report_all_infos   = FALSE;
365
366   cap_file_type      = FALSE;
367   cap_file_encap     = FALSE;
368   cap_snaplen        = FALSE;
369   cap_packet_count   = FALSE;
370   cap_file_size      = FALSE;
371   cap_comment        = FALSE;
372
373   cap_data_size      = FALSE;
374   cap_duration       = FALSE;
375   cap_start_time     = FALSE;
376   cap_end_time       = FALSE;
377   cap_order          = FALSE;
378
379   cap_data_rate_byte = FALSE;
380   cap_data_rate_bit  = FALSE;
381   cap_packet_size    = FALSE;
382   cap_packet_rate    = FALSE;
383
384 #ifdef HAVE_LIBGCRYPT
385   cap_file_hashes = FALSE;
386 #endif /* HAVE_LIBGCRYPT */
387 }
388
389 static const gchar *
390 order_string(order_t order)
391 {
392   switch (order) {
393
394     case IN_ORDER:
395       return "True";
396
397     case NOT_IN_ORDER:
398       return "False";
399
400     case ORDER_UNKNOWN:
401       return "Unknown";
402
403     default:
404       return "???";  /* "cannot happen" (the next step is "Profit!") */
405   }
406 }
407
408 static gchar *
409 time_string(time_t timer, capture_info *cf_info, gboolean want_lf)
410 {
411   const gchar *lf = want_lf ? "\n" : "";
412   static gchar time_string_buf[20];
413   char *time_string_ctime;
414
415   if (cf_info->times_known && cf_info->packet_count > 0) {
416     if (time_as_secs) {
417       /* XXX - Would it be useful to show sub-second precision? */
418       g_snprintf(time_string_buf, 20, "%lu%s", (unsigned long)timer, lf);
419       return time_string_buf;
420     } else {
421 #if (defined _WIN32) && (_MSC_VER < 1500)
422       /* calling localtime(), and thus ctime(), on MSVC 2005 with huge values causes it to crash */
423       /* XXX - find the exact value that still does work */
424       /* XXX - using _USE_32BIT_TIME_T might be another way to circumvent this problem */
425       if (timer > 2000000000) {
426         time_string_ctime = NULL;
427       } else
428 #endif
429         time_string_ctime = ctime(&timer);
430       if (time_string_ctime == NULL) {
431         g_snprintf(time_string_buf, 20, "Not representable%s", lf);
432         return time_string_buf;
433       }
434       if (!want_lf) {
435         /*
436          * The ctime() function returns a string formatted as:
437          *   "Www Mmm dd hh:mm:ss yyyy\n"
438          * The unwanted '\n' is the 24th character.
439          */
440         time_string_ctime[24] = '\0';
441       }
442       return time_string_ctime;
443     }
444   }
445
446   g_snprintf(time_string_buf, 15, "n/a%s", lf);
447   return time_string_buf;
448 }
449
450 static double
451 secs_nsecs(const struct wtap_nstime * nstime)
452 {
453   return (nstime->nsecs / 1000000000.0) + (double)nstime->secs;
454 }
455
456 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
457   if (value > 0.0)
458     printf("%s%.*f%s\n", text_p1, width, value, text_p2);
459   else
460     printf("%sn/a\n", text_p1);
461 }
462
463 static void
464 print_stats(const gchar *filename, capture_info *cf_info)
465 {
466   const gchar           *file_type_string, *file_encap_string;
467   time_t                start_time_t;
468   time_t                stop_time_t;
469   gchar                 *size_string;
470
471   /* Build printable strings for various stats */
472   file_type_string = wtap_file_type_string(cf_info->file_type);
473   file_encap_string = wtap_encap_string(cf_info->file_encap);
474   start_time_t = (time_t)cf_info->start_time;
475   stop_time_t = (time_t)cf_info->stop_time;
476
477   if (filename)           printf     ("File name:           %s\n", filename);
478   if (cap_file_type)      printf     ("File type:           %s%s\n",
479       file_type_string,
480       cf_info->iscompressed ? " (gzip compressed)" : "");
481   if (cap_file_encap)     printf     ("File encapsulation:  %s\n", file_encap_string);
482   if (cap_file_encap && (cf_info->file_encap == WTAP_ENCAP_PER_PACKET)) {
483     int i;
484     for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
485       if (cf_info->encap_counts[i] > 0)
486         printf("                       %s\n", wtap_encap_string(i));
487     }
488   }
489   if (cap_snaplen && cf_info->snap_set)
490     printf     ("Packet size limit:   file hdr: %u bytes\n", cf_info->snaplen);
491   else if(cap_snaplen && !cf_info->snap_set)
492     printf     ("Packet size limit:   file hdr: (not set)\n");
493   if (cf_info->snaplen_max_inferred > 0) {
494     if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
495       printf     ("Packet size limit:   inferred: %u bytes\n", cf_info->snaplen_min_inferred);
496     else
497       printf     ("Packet size limit:   inferred: %u bytes - %u bytes (range)\n",
498           cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
499   }
500   if (cap_packet_count) {
501     printf     ("Number of packets:   ");
502     if (machine_readable) {
503       printf ("%u\n", cf_info->packet_count);
504     } else {
505       size_string = format_size(cf_info->packet_count, format_size_unit_none);
506       printf ("%s\n", size_string);
507       g_free(size_string);
508     }
509   }
510   if (cap_file_size) {
511     printf     ("File size:           ");
512     if (machine_readable) {
513       printf     ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
514     } else {
515       size_string = format_size(cf_info->filesize, format_size_unit_bytes);
516       printf ("%s\n", size_string);
517       g_free(size_string);
518     }
519   }
520   if (cap_data_size) {
521     printf     ("Data size:           ");
522     if (machine_readable) {
523       printf     ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
524     } else {
525       size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
526       printf ("%s\n", size_string);
527       g_free(size_string);
528     }
529   }
530   if (cf_info->times_known) {
531     if (cap_duration) /* XXX - shorten to hh:mm:ss */
532                           print_value("Capture duration:    ", 0, " seconds",   cf_info->duration);
533     if (cap_start_time)
534                           printf     ("Start time:          %s", time_string(start_time_t, cf_info, TRUE));
535     if (cap_end_time)
536                           printf     ("End time:            %s", time_string(stop_time_t, cf_info, TRUE));
537     if (cap_data_rate_byte) {
538                           printf     ("Data byte rate:      ");
539       if (machine_readable) {
540         print_value("", 2, " bytes/sec",   cf_info->data_rate);
541       } else {
542         size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
543         printf ("%s\n", size_string);
544         g_free(size_string);
545       }
546     }
547     if (cap_data_rate_bit) {
548                           printf     ("Data bit rate:       ");
549       if (machine_readable) {
550         print_value("", 2, " bits/sec",    cf_info->data_rate*8);
551       } else {
552         size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
553         printf ("%s\n", size_string);
554         g_free(size_string);
555       }
556     }
557   }
558   if (cap_packet_size)    printf     ("Average packet size: %.2f bytes\n",        cf_info->packet_size);
559   if (cf_info->times_known) {
560     if (cap_packet_rate) {
561                           printf     ("Average packet rate: ");
562       if (machine_readable) {
563         print_value("", 2, " packets/sec", cf_info->packet_rate);
564       } else {
565         size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_none);
566         printf ("%spackets/sec\n", size_string);
567         g_free(size_string);
568       }
569     }
570   }
571 #ifdef HAVE_LIBGCRYPT
572   if (cap_file_hashes) {
573     printf     ("SHA1:                %s\n", file_sha1);
574     printf     ("RIPEMD160:           %s\n", file_rmd160);
575     printf     ("MD5:                 %s\n", file_md5);
576   }
577 #endif /* HAVE_LIBGCRYPT */
578   if (cap_order)          printf     ("Strict time order:   %s\n", order_string(cf_info->order));
579   if (cap_comment && cf_info->comment)
580     printf     ("Capture comment:     %s\n", cf_info->comment);
581 }
582
583 static void
584 putsep(void)
585 {
586   if (field_separator) putchar(field_separator);
587 }
588
589 static void
590 putquote(void)
591 {
592   if (quote_char) putchar(quote_char);
593 }
594
595 static void
596 print_stats_table_header_label(const gchar *label)
597 {
598   putsep();
599   putquote();
600   printf("%s", label);
601   putquote();
602 }
603
604 static void
605 print_stats_table_header(void)
606 {
607   putquote();
608   printf("File name");
609   putquote();
610
611   if (cap_file_type)      print_stats_table_header_label("File type");
612   if (cap_file_encap)     print_stats_table_header_label("File encapsulation");
613   if (cap_snaplen)        print_stats_table_header_label("Packet size limit");
614   if (cap_snaplen)        print_stats_table_header_label("Packet size limit min (inferred)");
615   if (cap_snaplen)        print_stats_table_header_label("Packet size limit max (inferred)");
616   if (cap_packet_count)   print_stats_table_header_label("Number of packets");
617   if (cap_file_size)      print_stats_table_header_label("File size (bytes)");
618   if (cap_data_size)      print_stats_table_header_label("Data size (bytes)");
619   if (cap_duration)       print_stats_table_header_label("Capture duration (seconds)");
620   if (cap_start_time)     print_stats_table_header_label("Start time");
621   if (cap_end_time)       print_stats_table_header_label("End time");
622   if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
623   if (cap_data_rate_bit)  print_stats_table_header_label("Data bit rate (bits/sec)");
624   if (cap_packet_size)    print_stats_table_header_label("Average packet size (bytes)");
625   if (cap_packet_rate)    print_stats_table_header_label("Average packet rate (packets/sec)");
626 #ifdef HAVE_LIBGCRYPT
627   if (cap_file_hashes) {
628     print_stats_table_header_label("SHA1");
629     print_stats_table_header_label("RIPEMD160");
630     print_stats_table_header_label("MD5");
631   }
632 #endif /* HAVE_LIBGCRYPT */
633   if (cap_order)          print_stats_table_header_label("Strict time order");
634   if (cap_comment)        print_stats_table_header_label("Capture comment");
635
636   printf("\n");
637 }
638
639 static void
640 print_stats_table(const gchar *filename, capture_info *cf_info)
641 {
642   const gchar           *file_type_string, *file_encap_string;
643   time_t                start_time_t;
644   time_t                stop_time_t;
645
646   /* Build printable strings for various stats */
647   file_type_string = wtap_file_type_string(cf_info->file_type);
648   file_encap_string = wtap_encap_string(cf_info->file_encap);
649   start_time_t = (time_t)cf_info->start_time;
650   stop_time_t = (time_t)cf_info->stop_time;
651
652   if (filename) {
653     putquote();
654     printf("%s", filename);
655     putquote();
656   }
657
658   if (cap_file_type) {
659     putsep();
660     putquote();
661     printf("%s", file_type_string);
662     putquote();
663   }
664
665   /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
666    *       Output a line for each different encap with all fields repeated except
667    *        the encapsulation field which has "Per Packet: ..." for each
668    *        encapsulation type seen ?
669    */
670   if (cap_file_encap) {
671     putsep();
672     putquote();
673     printf("%s", file_encap_string);
674     putquote();
675   }
676
677   if (cap_snaplen) {
678     putsep();
679     putquote();
680     if(cf_info->snap_set)
681       printf("%u", cf_info->snaplen);
682     else
683       printf("(not set)");
684     putquote();
685     if (cf_info->snaplen_max_inferred > 0) {
686       putsep();
687       putquote();
688       printf("%u", cf_info->snaplen_min_inferred);
689       putquote();
690       putsep();
691       putquote();
692       printf("%u", cf_info->snaplen_max_inferred);
693       putquote();
694     }
695     else {
696       putsep();
697       putquote();
698       printf("n/a");
699       putquote();
700       putsep();
701       putquote();
702       printf("n/a");
703       putquote();
704     }
705   }
706
707   if (cap_packet_count) {
708     putsep();
709     putquote();
710     printf("%u", cf_info->packet_count);
711     putquote();
712   }
713
714   if (cap_file_size) {
715     putsep();
716     putquote();
717     printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
718     putquote();
719   }
720
721   if (cap_data_size) {
722     putsep();
723     putquote();
724     printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
725     putquote();
726   }
727
728   if (cap_duration) {
729     putsep();
730     putquote();
731     if (cf_info->times_known)
732       printf("%f", cf_info->duration);
733     else
734       printf("n/a");
735     putquote();
736   }
737
738   if (cap_start_time) {
739     putsep();
740     putquote();
741     printf("%s", time_string(start_time_t, cf_info, FALSE));
742     putquote();
743   }
744
745   if (cap_end_time) {
746     putsep();
747     putquote();
748     printf("%s", time_string(stop_time_t, cf_info, FALSE));
749     putquote();
750   }
751
752   if (cap_data_rate_byte) {
753     putsep();
754     putquote();
755     if (cf_info->times_known)
756       printf("%.2f", cf_info->data_rate);
757     else
758       printf("n/a");
759     putquote();
760   }
761
762   if (cap_data_rate_bit) {
763     putsep();
764     putquote();
765     if (cf_info->times_known)
766       printf("%.2f", cf_info->data_rate*8);
767     else
768       printf("n/a");
769     putquote();
770   }
771
772   if (cap_packet_size) {
773     putsep();
774     putquote();
775     printf("%.2f", cf_info->packet_size);
776     putquote();
777   }
778
779   if (cap_packet_rate) {
780     putsep();
781     putquote();
782     if (cf_info->times_known)
783       printf("%.2f", cf_info->packet_rate);
784     else
785       printf("n/a");
786     putquote();
787   }
788
789 #ifdef HAVE_LIBGCRYPT
790   if (cap_file_hashes) {
791     putsep();
792     putquote();
793     printf("%s", file_sha1);
794     putquote();
795
796     putsep();
797     putquote();
798     printf("%s", file_rmd160);
799     putquote();
800
801     putsep();
802     putquote();
803     printf("%s", file_md5);
804     putquote();
805   }
806 #endif /* HAVE_LIBGCRYPT */
807
808   if (cap_order) {
809     putsep();
810     putquote();
811     printf("%s", order_string(cf_info->order));
812     putquote();
813   }
814
815   if (cap_comment) {
816     putsep();
817     putquote();
818     printf("%s", cf_info->comment);
819     putquote();
820   }
821
822
823   printf("\n");
824 }
825
826 static int
827 process_cap_file(wtap *wth, const char *filename)
828 {
829   int                   err;
830   gchar                *err_info;
831   gint64                size;
832   gint64                data_offset;
833
834   guint32               packet = 0;
835   gint64                bytes  = 0;
836   guint32               snaplen_min_inferred = 0xffffffff;
837   guint32               snaplen_max_inferred =          0;
838   const struct wtap_pkthdr *phdr;
839   capture_info          cf_info;
840   gboolean              have_times = TRUE;
841   double                start_time = 0;
842   double                stop_time  = 0;
843   double                cur_time   = 0;
844   double                prev_time = 0;
845   gboolean              know_order = FALSE;
846   order_t               order = IN_ORDER;
847   wtapng_section_t     *shb_inf;
848   gchar                *p;
849
850
851   cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
852
853   /* Tally up data that we need to parse through the file to find */
854   while (wtap_read(wth, &err, &err_info, &data_offset))  {
855     phdr = wtap_phdr(wth);
856     if (phdr->presence_flags & WTAP_HAS_TS) {
857       prev_time = cur_time;
858       cur_time = secs_nsecs(&phdr->ts);
859       if(packet==0) {
860         start_time = cur_time;
861         stop_time = cur_time;
862         prev_time = cur_time;
863       }
864       if (cur_time < prev_time) {
865         order = NOT_IN_ORDER;
866       }
867       if (cur_time < start_time) {
868         start_time = cur_time;
869       }
870       if (cur_time > stop_time) {
871         stop_time = cur_time;
872       }
873     } else {
874       have_times = FALSE; /* at least one packet has no time stamp */
875       if (order != NOT_IN_ORDER)
876         order = ORDER_UNKNOWN;
877     }
878
879     bytes+=phdr->len;
880     packet++;
881
882     /* If caplen < len for a rcd, then presumably           */
883     /* 'Limit packet capture length' was done for this rcd. */
884     /* Keep track as to the min/max actual snapshot lengths */
885     /*  seen for this file.                                 */
886     if (phdr->caplen < phdr->len) {
887       if (phdr->caplen < snaplen_min_inferred)
888         snaplen_min_inferred = phdr->caplen;
889       if (phdr->caplen > snaplen_max_inferred)
890         snaplen_max_inferred = phdr->caplen;
891     }
892
893     /* Per-packet encapsulation */
894     if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
895       if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
896         cf_info.encap_counts[phdr->pkt_encap] += 1;
897       } else {
898         fprintf(stderr, "capinfos: Unknown per-packet encapsulation: %d [frame number: %d]\n", phdr->pkt_encap, packet);
899       }
900     }
901
902   } /* while */
903
904   if (err != 0) {
905     fprintf(stderr,
906         "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
907         packet, filename, wtap_strerror(err));
908     switch (err) {
909
910       case WTAP_ERR_UNSUPPORTED:
911       case WTAP_ERR_UNSUPPORTED_ENCAP:
912       case WTAP_ERR_BAD_FILE:
913       case WTAP_ERR_DECOMPRESS:
914         fprintf(stderr, "(%s)\n", err_info);
915         g_free(err_info);
916         break;
917     }
918     g_free(cf_info.encap_counts);
919     return 1;
920   }
921
922   /* File size */
923   size = wtap_file_size(wth, &err);
924   if (size == -1) {
925     fprintf(stderr,
926         "capinfos: Can't get size of \"%s\": %s.\n",
927         filename, g_strerror(err));
928     g_free(cf_info.encap_counts);
929     return 1;
930   }
931
932   cf_info.filesize = size;
933
934   /* File Type */
935   cf_info.file_type = wtap_file_type(wth);
936   cf_info.iscompressed = wtap_iscompressed(wth);
937
938   /* File Encapsulation */
939   cf_info.file_encap = wtap_file_encap(wth);
940
941   /* Packet size limit (snaplen) */
942   cf_info.snaplen = wtap_snapshot_length(wth);
943   if(cf_info.snaplen > 0)
944     cf_info.snap_set = TRUE;
945   else
946     cf_info.snap_set = FALSE;
947
948   cf_info.snaplen_min_inferred = snaplen_min_inferred;
949   cf_info.snaplen_max_inferred = snaplen_max_inferred;
950
951   /* # of packets */
952   cf_info.packet_count = packet;
953
954   /* File Times */
955   cf_info.times_known = have_times;
956   cf_info.start_time = start_time;
957   cf_info.stop_time = stop_time;
958   cf_info.duration = stop_time-start_time;
959   cf_info.know_order = know_order;
960   cf_info.order = order;
961
962   /* Number of packet bytes */
963   cf_info.packet_bytes = bytes;
964
965   cf_info.data_rate   = 0.0;
966   cf_info.packet_rate = 0.0;
967   cf_info.packet_size = 0.0;
968
969   if (packet > 0) {
970     if (cf_info.duration > 0.0) {
971       cf_info.data_rate   = (double)bytes  / (stop_time-start_time); /* Data rate per second */
972       cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
973     }
974     cf_info.packet_size = (double)bytes / packet;                  /* Avg packet size      */
975   }
976
977   cf_info.comment = NULL;
978   shb_inf = wtap_file_get_shb_info(wth);
979   if (shb_inf) {
980     /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
981     cf_info.comment = g_strdup(shb_inf->opt_comment);
982   }
983   g_free(shb_inf);
984   if (cf_info.comment) {
985     /* multi-line comments would conflict with the formatting that capinfos uses
986        we replace linefeeds with spaces */
987     p = cf_info.comment;
988     while (*p != '\0') {
989       if (*p=='\n')
990         *p=' ';
991       p++;
992     }
993   }
994
995   if(long_report) {
996     print_stats(filename, &cf_info);
997   } else {
998     print_stats_table(filename, &cf_info);
999   }
1000
1001   g_free(cf_info.encap_counts);
1002   g_free(cf_info.comment);
1003
1004   return 0;
1005 }
1006
1007 static void
1008 usage(gboolean is_error)
1009 {
1010   FILE *output;
1011
1012   if (!is_error) {
1013     output = stdout;
1014     /* XXX - add capinfos header info here */
1015   }
1016   else {
1017     output = stderr;
1018   }
1019
1020   fprintf(output, "Capinfos %s"
1021 #ifdef SVNVERSION
1022       " (" SVNVERSION " from " SVNPATH ")"
1023 #endif
1024       "\n", VERSION);
1025   fprintf(output, "Prints various information (infos) about capture files.\n");
1026   fprintf(output, "See http://www.wireshark.org for more information.\n");
1027   fprintf(output, "\n");
1028   fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1029   fprintf(output, "\n");
1030   fprintf(output, "General infos:\n");
1031   fprintf(output, "  -t display the capture file type\n");
1032   fprintf(output, "  -E display the capture file encapsulation\n");
1033 #ifdef HAVE_LIBGCRYPT
1034   fprintf(output, "  -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1035 #endif
1036   fprintf(output, "  -k display the capture comment\n");
1037   fprintf(output, "\n");
1038   fprintf(output, "Size infos:\n");
1039   fprintf(output, "  -c display the number of packets\n");
1040   fprintf(output, "  -s display the size of the file (in bytes)\n");
1041   fprintf(output, "  -d display the total length of all packets (in bytes)\n");
1042   fprintf(output, "  -l display the packet size limit (snapshot length)\n");
1043   fprintf(output, "\n");
1044   fprintf(output, "Time infos:\n");
1045   fprintf(output, "  -u display the capture duration (in seconds)\n");
1046   fprintf(output, "  -a display the capture start time\n");
1047   fprintf(output, "  -e display the capture end time\n");
1048   fprintf(output, "  -o display the capture file chronological status (True/False)\n");
1049   fprintf(output, "  -S display start and end times as seconds\n");
1050   fprintf(output, "\n");
1051   fprintf(output, "Statistic infos:\n");
1052   fprintf(output, "  -y display average data rate (in bytes/sec)\n");
1053   fprintf(output, "  -i display average data rate (in bits/sec)\n");
1054   fprintf(output, "  -z display average packet size (in bytes)\n");
1055   fprintf(output, "  -x display average packet rate (in packets/sec)\n");
1056   fprintf(output, "\n");
1057   fprintf(output, "Output format:\n");
1058   fprintf(output, "  -L generate long report (default)\n");
1059   fprintf(output, "  -T generate table report\n");
1060   fprintf(output, "  -M display machine-readable values in long reports\n");
1061   fprintf(output, "\n");
1062   fprintf(output, "Table report options:\n");
1063   fprintf(output, "  -R generate header record (default)\n");
1064   fprintf(output, "  -r do not generate header record\n");
1065   fprintf(output, "\n");
1066   fprintf(output, "  -B separate infos with TAB character (default)\n");
1067   fprintf(output, "  -m separate infos with comma (,) character\n");
1068   fprintf(output, "  -b separate infos with SPACE character\n");
1069   fprintf(output, "\n");
1070   fprintf(output, "  -N do not quote infos (default)\n");
1071   fprintf(output, "  -q quote infos with single quotes (')\n");
1072   fprintf(output, "  -Q quote infos with double quotes (\")\n");
1073   fprintf(output, "\n");
1074   fprintf(output, "Miscellaneous:\n");
1075   fprintf(output, "  -h display this help and exit\n");
1076   fprintf(output, "  -C cancel processing if file open fails (default is to continue)\n");
1077   fprintf(output, "  -A generate all infos (default)\n");
1078   fprintf(output, "\n");
1079   fprintf(output, "Options are processed from left to right order with later options superceding\n");
1080   fprintf(output, "or adding to earlier options.\n");
1081   fprintf(output, "\n");
1082   fprintf(output, "If no options are given the default is to display all infos in long report\n");
1083   fprintf(output, "output format.\n");
1084 #ifndef HAVE_LIBGCRYPT
1085   fprintf(output, "\nFile hashing support (-H) is not present.\n");
1086 #endif
1087 }
1088
1089 #ifdef HAVE_PLUGINS
1090 /*
1091  *  Don't report failures to load plugins because most (non-wiretap) plugins
1092  *  *should* fail to load (because we're not linked against libwireshark and
1093  *  dissector plugins need libwireshark).
1094  */
1095 static void
1096 failure_message(const char *msg_format _U_, va_list ap _U_)
1097 {
1098   return;
1099 }
1100 #endif
1101
1102 #ifdef HAVE_LIBGCRYPT
1103 static void
1104 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1105   int i;
1106
1107   for (i = 0; i < (int) length; i++) {
1108     g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1109   }
1110 }
1111 #endif /* HAVE_LIBGCRYPT */
1112
1113 int
1114 main(int argc, char *argv[])
1115 {
1116   wtap  *wth;
1117   int    err;
1118   gchar *err_info;
1119   int    opt;
1120   int    overall_error_status;
1121
1122   int status = 0;
1123 #ifdef HAVE_PLUGINS
1124   char  *init_progfile_dir_error;
1125 #endif
1126 #ifdef HAVE_LIBGCRYPT
1127   FILE  *fh;
1128   char  *hash_buf = NULL;
1129   gcry_md_hd_t hd = NULL;
1130   size_t hash_bytes;
1131 #endif
1132
1133 #ifdef _WIN32
1134   arg_list_utf_16to8(argc, argv);
1135   create_app_running_mutex();
1136 #endif /* _WIN32 */
1137
1138   /*
1139    * Get credential information for later use.
1140    */
1141   init_process_policies();
1142
1143 #ifdef HAVE_PLUGINS
1144   /* Register wiretap plugins */
1145
1146   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1147     g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1148     g_free(init_progfile_dir_error);
1149   } else {
1150     init_report_err(failure_message,NULL,NULL,NULL);
1151     init_plugins();
1152   }
1153 #endif
1154
1155   /* Process the options */
1156 #ifdef USE_GOPTION
1157   ctx = g_option_context_new(" <infile> ... - print information about capture file(s)");
1158   general_grp = g_option_group_new("gen", "General infos:",
1159       "Show general options", NULL, NULL);
1160   size_grp = g_option_group_new("size", "Size infos:",
1161       "Show size options", NULL, NULL);
1162   time_grp = g_option_group_new("time", "Time infos:",
1163       "Show time options", NULL, NULL);
1164   stats_grp = g_option_group_new("stats", "Statistics infos:",
1165       "Show statistics options", NULL, NULL);
1166   output_grp = g_option_group_new("output", "Output format:",
1167       "Show output format options", NULL, NULL);
1168   table_report_grp = g_option_group_new("table", "Table report options:",
1169       "Show table report options", NULL, NULL);
1170   g_option_group_add_entries(general_grp, general_entries);
1171   g_option_group_add_entries(size_grp, size_entries);
1172   g_option_group_add_entries(time_grp, time_entries);
1173   g_option_group_add_entries(stats_grp, stats_entries);
1174   g_option_group_add_entries(output_grp, output_format_entries);
1175   g_option_group_add_entries(table_report_grp, table_report_entries);
1176   g_option_context_add_main_entries(ctx, misc_entries, NULL);
1177   g_option_context_add_group(ctx, general_grp);
1178   g_option_context_add_group(ctx, size_grp);
1179   g_option_context_add_group(ctx, time_grp);
1180   g_option_context_add_group(ctx, stats_grp);
1181   g_option_context_add_group(ctx, output_grp);
1182   g_option_context_add_group(ctx, table_report_grp);
1183   /* There's probably a better way to do this, but this works for now.
1184      GOptions displays the name in argv[0] as the name of the
1185      application.  This is reasonable, but because we actually have a
1186      script wrapper that calls the executable.  The name that gets
1187      displayed is not exactly the same as the command the user used
1188      ran.
1189    */
1190   argv[0] = (char *)"capinfos";
1191
1192   /* if we have at least one cmdline option, we disable printing all infos */
1193   if (argc>=2 && report_all_infos)
1194     disable_all_infos();
1195
1196   if( !g_option_context_parse(ctx, &argc, &argv, &parse_err) ) {
1197     if(parse_err) g_print ("option parsing failed: %s\n", parse_err->message);
1198     g_print("%s", g_option_context_get_help (ctx, TRUE, NULL));
1199     exit(1);
1200   }
1201   if( cap_help || (argc < 2) ) {
1202     g_print("%s", g_option_context_get_help (ctx, FALSE, NULL));
1203     exit(0);
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(1);
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], &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: 2
1450  * indent-tabs-mode: nil
1451  * End:
1452  *
1453  * vi: set shiftwidth=2 tabstop=2 expandtab:
1454  * :indentSize=2:tabSize=2:noTabs=true:
1455  */