From Martin Kaiser: dissector for DVB-CI (Common Interface)
[obnox/wireshark/wip.git] / wiretap / wtap.c
1 /* wtap.c
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <errno.h>
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #ifdef HAVE_LIBZ
39 #include <zlib.h>
40 #endif
41
42 #include "wtap-int.h"
43 #include "wtap.h"
44
45 #include "file_wrappers.h"
46 #include <wsutil/file_util.h>
47 #include "buffer.h"
48
49 /*
50  * Return the size of the file, as reported by the OS.
51  * (gint64, in case that's 64 bits.)
52  */
53 gint64
54 wtap_file_size(wtap *wth, int *err)
55 {
56         struct stat statb;
57
58         if (fstat(wth->fd, &statb) == -1) {
59                 if (err != NULL)
60                         *err = errno;
61                 return -1;
62         }
63         return statb.st_size;
64 }
65
66 int
67 wtap_file_type(wtap *wth)
68 {
69         return wth->file_type;
70 }
71
72 int
73 wtap_snapshot_length(wtap *wth)
74 {
75         return wth->snapshot_length;
76 }
77
78 int
79 wtap_file_encap(wtap *wth)
80 {
81         return wth->file_encap;
82 }
83
84 int
85 wtap_file_tsprecision(wtap *wth)
86 {
87         return wth->tsprecision;
88 }
89
90 /* Table of the encapsulation types we know about. */
91 struct encap_type_info {
92         const char *name;
93         const char *short_name;
94 };
95
96 static struct encap_type_info encap_table_base[] = {
97         /* WTAP_ENCAP_UNKNOWN */
98         { "Unknown", "unknown" },
99
100         /* WTAP_ENCAP_ETHERNET */
101         { "Ethernet", "ether" },
102
103         /* WTAP_ENCAP_TOKEN_RING */
104         { "Token Ring", "tr" },
105
106         /* WTAP_ENCAP_SLIP */
107         { "SLIP", "slip" },
108
109         /* WTAP_ENCAP_PPP */
110         { "PPP", "ppp" },
111
112         /* WTAP_ENCAP_FDDI */
113         { "FDDI", "fddi" },
114
115         /* WTAP_ENCAP_FDDI_BITSWAPPED */
116         { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
117
118         /* WTAP_ENCAP_RAW_IP */
119         { "Raw IP", "rawip" },
120
121         /* WTAP_ENCAP_ARCNET */
122         { "ARCNET", "arcnet" },
123
124         /* WTAP_ENCAP_ARCNET_LINUX */
125         { "Linux ARCNET", "arcnet_linux" },
126
127         /* WTAP_ENCAP_ATM_RFC1483 */
128         { "RFC 1483 ATM", "atm-rfc1483" },
129
130         /* WTAP_ENCAP_LINUX_ATM_CLIP */
131         { "Linux ATM CLIP", "linux-atm-clip" },
132
133         /* WTAP_ENCAP_LAPB */
134         { "LAPB", "lapb" },
135
136         /* WTAP_ENCAP_ATM_PDUS */
137         { "ATM PDUs", "atm-pdus" },
138
139         /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
140         { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
141
142         /* WTAP_ENCAP_NULL */
143         { "NULL", "null" },
144
145         /* WTAP_ENCAP_ASCEND */
146         { "Lucent/Ascend access equipment", "ascend" },
147
148         /* WTAP_ENCAP_ISDN */
149         { "ISDN", "isdn" },
150
151         /* WTAP_ENCAP_IP_OVER_FC */
152         { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
153
154         /* WTAP_ENCAP_PPP_WITH_PHDR */
155         { "PPP with Directional Info", "ppp-with-direction" },
156
157         /* WTAP_ENCAP_IEEE_802_11 */
158         { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
159
160         /* WTAP_ENCAP_PRISM_HEADER */
161         { "IEEE 802.11 plus Prism II monitor mode header", "prism" },
162
163         /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
164         { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
165
166         /* WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP */
167         { "IEEE 802.11 plus radiotap WLAN header", "ieee-802-11-radiotap" },
168
169         /* WTAP_ENCAP_IEEE_802_11_WLAN_AVS */
170         { "IEEE 802.11 plus AVS WLAN header", "ieee-802-11-avs" },
171
172         /* WTAP_ENCAP_SLL */
173         { "Linux cooked-mode capture", "linux-sll" },
174
175         /* WTAP_ENCAP_FRELAY */
176         { "Frame Relay", "frelay" },
177
178         /* WTAP_ENCAP_FRELAY_WITH_PHDR */
179         { "Frame Relay with Directional Info", "frelay-with-direction" },
180
181         /* WTAP_ENCAP_CHDLC */
182         { "Cisco HDLC", "chdlc" },
183
184         /* WTAP_ENCAP_CISCO_IOS */
185         { "Cisco IOS internal", "ios" },
186
187         /* WTAP_ENCAP_LOCALTALK */
188         { "Localtalk", "ltalk" },
189
190         /* WTAP_ENCAP_OLD_PFLOG  */
191         { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
192
193         /* WTAP_ENCAP_HHDLC */
194         { "HiPath HDLC", "hhdlc" },
195
196         /* WTAP_ENCAP_DOCSIS */
197         { "Data Over Cable Service Interface Specification", "docsis" },
198
199         /* WTAP_ENCAP_COSINE */
200         { "CoSine L2 debug log", "cosine" },
201
202         /* WTAP_ENCAP_WFLEET_HDLC */
203         { "Wellfleet HDLC", "whdlc" },
204
205         /* WTAP_ENCAP_SDLC */
206         { "SDLC", "sdlc" },
207
208         /* WTAP_ENCAP_TZSP */
209         { "Tazmen sniffer protocol", "tzsp" },
210
211         /* WTAP_ENCAP_ENC */
212         { "OpenBSD enc(4) encapsulating interface", "enc" },
213
214         /* WTAP_ENCAP_PFLOG  */
215         { "OpenBSD PF Firewall logs", "pflog" },
216
217         /* WTAP_ENCAP_CHDLC_WITH_PHDR */
218         { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
219
220         /* WTAP_ENCAP_BLUETOOTH_H4 */
221         { "Bluetooth H4", "bluetooth-h4" },
222
223         /* WTAP_ENCAP_MTP2 */
224         { "SS7 MTP2", "mtp2" },
225
226         /* WTAP_ENCAP_MTP3 */
227         { "SS7 MTP3", "mtp3" },
228
229         /* WTAP_ENCAP_IRDA */
230         { "IrDA", "irda" },
231
232         /* WTAP_ENCAP_USER0 */
233         { "USER 0", "user0" },
234
235         /* WTAP_ENCAP_USER1 */
236         { "USER 1", "user1" },
237
238         /* WTAP_ENCAP_USER2 */
239         { "USER 2", "user2" },
240
241         /* WTAP_ENCAP_USER3 */
242         { "USER 3", "user3" },
243
244         /* WTAP_ENCAP_USER4 */
245         { "USER 4", "user4" },
246
247         /* WTAP_ENCAP_USER5 */
248         { "USER 5", "user5" },
249
250         /* WTAP_ENCAP_USER6 */
251         { "USER 6", "user6" },
252
253         /* WTAP_ENCAP_USER7 */
254         { "USER 7", "user7" },
255
256         /* WTAP_ENCAP_USER8 */
257         { "USER 8", "user8" },
258
259         /* WTAP_ENCAP_USER9 */
260         { "USER 9", "user9" },
261
262         /* WTAP_ENCAP_USER10 */
263         { "USER 10", "user10" },
264
265         /* WTAP_ENCAP_USER11 */
266         { "USER 11", "user11" },
267
268         /* WTAP_ENCAP_USER12 */
269         { "USER 12", "user12" },
270
271         /* WTAP_ENCAP_USER13 */
272         { "USER 13", "user13" },
273
274         /* WTAP_ENCAP_USER14 */
275         { "USER 14", "user14" },
276
277         /* WTAP_ENCAP_USER15 */
278         { "USER 15", "user15" },
279
280         /* WTAP_ENCAP_SYMANTEC */
281         { "Symantec Enterprise Firewall", "symantec" },
282
283         /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
284         { "Apple IP-over-IEEE 1394", "ap1394" },
285
286         /* WTAP_ENCAP_BACNET_MS_TP */
287         { "BACnet MS/TP", "bacnet-ms-tp" },
288
289         /* WTAP_ENCAP_NETTL_RAW_ICMP */
290         { "Raw ICMP with nettl headers", "raw-icmp-nettl" },
291
292         /* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
293         { "Raw ICMPv6 with nettl headers", "raw-icmpv6-nettl" },
294
295         /* WTAP_ENCAP_GPRS_LLC */
296         { "GPRS LLC", "gprs-llc" },
297
298         /* WTAP_ENCAP_JUNIPER_ATM1 */
299         { "Juniper ATM1", "juniper-atm1" },
300
301         /* WTAP_ENCAP_JUNIPER_ATM2 */
302         { "Juniper ATM2", "juniper-atm2" },
303
304         /* WTAP_ENCAP_REDBACK */
305         { "Redback SmartEdge", "redback" },
306
307         /* WTAP_ENCAP_NETTL_RAW_IP */
308         { "Raw IP with nettl headers", "rawip-nettl" },
309
310         /* WTAP_ENCAP_NETTL_ETHERNET */
311         { "Ethernet with nettl headers", "ether-nettl" },
312
313         /* WTAP_ENCAP_NETTL_TOKEN_RING */
314         { "Token Ring with nettl headers", "tr-nettl" },
315
316         /* WTAP_ENCAP_NETTL_FDDI */
317         { "FDDI with nettl headers", "fddi-nettl" },
318
319         /* WTAP_ENCAP_NETTL_UNKNOWN */
320         { "Unknown link-layer type with nettl headers", "unknown-nettl" },
321
322         /* WTAP_ENCAP_MTP2_WITH_PHDR */
323         { "MTP2 with pseudoheader", "mtp2-with-phdr" },
324
325         /* WTAP_ENCAP_JUNIPER_PPPOE */
326         { "Juniper PPPoE", "juniper-pppoe" },
327
328         /* WTAP_ENCAP_GCOM_TIE1 */
329         { "GCOM TIE1", "gcom-tie1" },
330
331         /* WTAP_ENCAP_GCOM_SERIAL */
332         { "GCOM Serial", "gcom-serial" },
333
334         /* WTAP_ENCAP_NETTL_X25 */
335         { "X25 with nettl headers", "x25-nettl" },
336
337         /* WTAP_ENCAP_K12 */
338         { "K12 protocol analyzer", "k12" },
339
340         /* WTAP_ENCAP_JUNIPER_MLPPP */
341         { "Juniper MLPPP", "juniper-mlppp" },
342
343         /* WTAP_ENCAP_JUNIPER_MLFR */
344         { "Juniper MLFR", "juniper-mlfr" },
345
346         /* WTAP_ENCAP_JUNIPER_ETHER */
347         { "Juniper Ethernet", "juniper-ether" },
348
349         /* WTAP_ENCAP_JUNIPER_PPP */
350         { "Juniper PPP", "juniper-ppp" },
351
352         /* WTAP_ENCAP_JUNIPER_FRELAY */
353         { "Juniper Frame-Relay", "juniper-frelay" },
354
355         /* WTAP_ENCAP_JUNIPER_CHDLC */
356         { "Juniper C-HDLC", "juniper-chdlc" },
357
358         /* WTAP_ENCAP_JUNIPER_GGSN */
359         { "Juniper GGSN", "juniper-ggsn" },
360
361         /* WTAP_ENCAP_LINUX_LAPD */
362         { "LAPD", "lapd" },
363
364         /* WTAP_ENCAP_CATAPULT_DCT2000 */
365         { "Catapult DCT2000", "dct2000" },
366
367         /* WTAP_ENCAP_BER */
368         { "ASN.1 Basic Encoding Rules", "ber" },
369
370         /* WTAP_ENCAP_JUNIPER_VP */
371         { "Juniper Voice PIC", "juniper-vp" },
372
373         /* WTAP_ENCAP_USB */
374         { "Raw USB packets", "usb" },
375
376         /* WTAP_ENCAP_IEEE802_16_MAC_CPS */
377         { "IEEE 802.16 MAC Common Part Sublayer", "ieee-802-16-mac-cps" },
378
379         /* WTAP_ENCAP_NETTL_RAW_TELNET */
380         { "Raw telnet with nettl headers", "raw-telnet-nettl" },
381
382         /* WTAP_ENCAP_USB_LINUX */
383         { "USB packets with Linux header", "usb-linux" },
384
385         /* WTAP_ENCAP_MPEG */
386         { "MPEG", "mpeg" },
387
388         /* WTAP_ENCAP_PPI */
389         { "Per-Packet Information header", "ppi" },
390
391         /* WTAP_ENCAP_ERF */
392         { "Endace Record File", "erf" },
393
394         /* WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR */
395         { "Bluetooth H4 with linux header", "bluetooth-h4-linux" },
396
397         /* WTAP_ENCAP_SITA */
398         { "SITA WAN packets", "sita-wan" },
399
400         /* WTAP_ENCAP_SCCP */
401         { "SS7 SCCP", "sccp" },
402
403         /* WTAP_ENCAP_BLUETOOTH_HCI */
404         { "Bluetooth without transport layer", "bluetooth-hci" },
405
406         /* WTAP_ENCAP_IPMB */
407         { "Intelligent Platform Management Bus", "ipmb" },
408
409         /* WTAP_ENCAP_IEEE802_15_4 */
410         { "IEEE 802.15.4 Wireless PAN", "wpan" },
411
412         /* WTAP_ENCAP_X2E_XORAYA */
413         { "X2E Xoraya", "x2e-xoraya" },
414
415         /* WTAP_ENCAP_FLEXRAY */
416         { "FlexRay", "flexray" },
417
418         /* WTAP_ENCAP_LIN */
419         { "Local Interconnect Network", "lin" },
420
421         /* WTAP_ENCAP_MOST */
422         { "Media Oriented Systems Transport", "most" },
423
424         /* WTAP_ENCAP_CAN20B */
425         { "Controller Area Network 2.0B", "can20b" },
426
427         /* WTAP_ENCAP_LAYER1_EVENT */
428         { "EyeSDN Layer 1 event", "layer1-event" },
429
430         /* WTAP_ENCAP_X2E_SERIAL */
431         { "X2E serial line capture", "x2e-serial" },
432
433         /* WTAP_ENCAP_I2C */
434         { "I2C", "i2c" },
435
436         /* WTAP_ENCAP_IEEE802_15_4_NONASK_PHY */
437         { "IEEE 802.15.4 Wireless PAN non-ASK PHY", "wpan-nonask-phy" },
438
439         /* WTAP_ENCAP_TNEF */
440         { "Transport-Neutral Encapsulation Format", "tnef" },
441
442         /* WTAP_ENCAP_USB_LINUX_MMAPPED */
443         { "USB packets with Linux header and padding", "usb-linux-mmap" },
444
445         /* WTAP_ENCAP_GSM_UM */
446         { "GSM Um Interface", "gsm_um" },
447
448         /* WTAP_ENCAP_DPNSS */
449         { "Digital Private Signalling System No 1 Link Layer", "dpnss_link" },
450
451         /* WTAP_ENCAP_PACKETLOGGER */
452         { "PacketLogger", "packetlogger" },
453
454         /* WTAP_ENCAP_NSTRACE_1_0 */
455         { "NetScaler Encapsulation 1.0 of Ethernet", "nstrace10" },
456
457         /* WTAP_ENCAP_NSTRACE_2_0 */
458         { "NetScaler Encapsulation 2.0 of Ethernet", "nstrace20" },
459
460         /* WTAP_ENCAP_FIBRE_CHANNEL_FC2 */
461         { "Fibre Channel FC-2", "fc2" },
462
463         /* WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS */
464         { "Fibre Channel FC-2 With Frame Delimiter", "fc2sof"},
465
466         /* WTAP_ENCAP_JPEG_JFIF */
467         { "JPEG/JFIF", "jfif" },
468
469         /* WTAP_ENCAP_IPNET */
470         { "Solaris IPNET", "ipnet" },
471
472         /* WTAP_ENCAP_SOCKETCAN */
473         { "SocketCAN", "socketcan" },
474
475         /* WTAP_ENCAP_IEEE802_11_NETMON_RADIO */
476         { "IEEE 802.11 plus Network Monitor radio header", "ieee-802-11-netmon" },
477
478         /* WTAP_ENCAP_IEEE802_15_4_NOFCS */
479         { "IEEE 802.15.4 Wireless PAN with FCS not present", "wpan-nofcs" },
480
481         /* WTAP_ENCAP_RAW_IPFIX */
482         { "IPFIX", "ipfix" },
483
484         /* WTAP_ENCAP_RAW_IP4 */
485         { "Raw IPv4", "rawip4" },
486
487         /* WTAP_ENCAP_RAW_IP6 */
488         { "Raw IPv6", "rawip6" },
489
490         /* WTAP_ENCAP_LAPD */
491         { "Lapd header", "lapd" },
492
493         /* WTAP_ENCAP_DVBCI */
494         { "DVB-CI (Common Interface)", "dvbci"}
495 };
496
497 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
498 static GArray* encap_table_arr = NULL;
499 static const struct encap_type_info* encap_table = NULL;
500
501 static void wtap_init_encap_types(void) {
502
503         if (encap_table_arr) return;
504
505         encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
506
507         g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
508
509         encap_table = (void*)encap_table_arr->data;
510 }
511
512 int wtap_get_num_encap_types(void) {
513         wtap_init_encap_types();
514         return wtap_num_encap_types;
515 }
516
517
518 int wtap_register_encap_type(char* name, char* short_name) {
519         struct encap_type_info e;
520         wtap_init_encap_types();
521
522         e.name = g_strdup(name);
523         e.short_name = g_strdup(short_name);
524
525         g_array_append_val(encap_table_arr,e);
526
527         encap_table = (void*)encap_table_arr->data;
528
529         return wtap_num_encap_types++;
530 }
531
532
533 /* Name that should be somewhat descriptive. */
534 const char
535 *wtap_encap_string(int encap)
536 {
537         if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
538                 return "Illegal";
539         else if (encap == WTAP_ENCAP_PER_PACKET)
540                 return "Per packet";
541         else
542                 return encap_table[encap].name;
543 }
544
545 /* Name to use in, say, a command-line flag specifying the type. */
546 const char
547 *wtap_encap_short_string(int encap)
548 {
549         if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
550                 return "illegal";
551         else if (encap == WTAP_ENCAP_PER_PACKET)
552                 return "per-packet";
553         else
554                 return encap_table[encap].short_name;
555 }
556
557 /* Translate a short name to a capture file type. */
558 int
559 wtap_short_string_to_encap(const char *short_name)
560 {
561         int encap;
562
563         for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
564                 if (encap_table[encap].short_name != NULL &&
565                     strcmp(short_name, encap_table[encap].short_name) == 0)
566                         return encap;
567         }
568         return -1;      /* no such encapsulation type */
569 }
570
571 static const char *wtap_errlist[] = {
572         "The file isn't a plain file or pipe",
573         "The file is being opened for random access but is a pipe",
574         "The file isn't a capture file in a known format",
575         "File contains record data we don't support",
576         "That file format cannot be written to a pipe",
577         NULL,
578         "Files can't be saved in that format",
579         "Files from that network type can't be saved in that format",
580         "That file format doesn't support per-packet encapsulations",
581         NULL,
582         NULL,
583         "Less data was read than was expected",
584         "File contains a record that's not valid",
585         "Less data was written than was requested",
586         "Uncompression error: data oddly truncated",
587         "Uncompression error: data would overflow buffer",
588         "Uncompression error: bad LZ77 offset",
589         "The standard input cannot be opened for random access"
590 };
591 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
592
593 const char
594 *wtap_strerror(int err)
595 {
596         static char errbuf[128];
597         unsigned int wtap_errlist_index;
598
599         if (err < 0) {
600 #ifdef HAVE_LIBZ
601                 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
602                         /* Assume it's a zlib error. */
603                         g_snprintf(errbuf, 128, "Uncompression error: %s",
604                             zError(err - WTAP_ERR_ZLIB));
605                         return errbuf;
606                 }
607 #endif
608                 wtap_errlist_index = -1 - err;
609                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
610                         g_snprintf(errbuf, 128, "Error %d", err);
611                         return errbuf;
612                 }
613                 if (wtap_errlist[wtap_errlist_index] == NULL)
614                         return "Unknown reason";
615                 return wtap_errlist[wtap_errlist_index];
616         } else
617                 return strerror(err);
618 }
619
620 /* Close only the sequential side, freeing up memory it uses.
621
622    Note that we do *not* want to call the subtype's close function,
623    as it would free any per-subtype data, and that data may be
624    needed by the random-access side.
625
626    Instead, if the subtype has a "sequential close" function, we call it,
627    to free up stuff used only by the sequential side. */
628 void
629 wtap_sequential_close(wtap *wth)
630 {
631         if (wth->subtype_sequential_close != NULL)
632                 (*wth->subtype_sequential_close)(wth);
633
634         if (wth->fh != NULL) {
635                 file_close(wth->fh);
636                 wth->fh = NULL;
637         }
638
639         if (wth->frame_buffer) {
640                 buffer_free(wth->frame_buffer);
641                 g_free(wth->frame_buffer);
642                 wth->frame_buffer = NULL;
643         }
644 }
645
646 void
647 wtap_close(wtap *wth)
648 {
649         wtap_sequential_close(wth);
650
651         if (wth->subtype_close != NULL)
652                 (*wth->subtype_close)(wth);
653
654         if (wth->random_fh != NULL)
655                 file_close(wth->random_fh);
656
657         if (wth->priv != NULL)
658                 g_free(wth->priv);
659
660         g_free(wth);
661 }
662
663 void
664 wtap_cleareof(wtap *wth _U_) {
665 #ifdef HAVE_LIBZ
666 #ifdef HAVE_GZCLEARERR
667         /* Reset EOF */
668         if (gzeof(wth->fh))
669                 gzclearerr(wth->fh);
670 #endif
671 #endif
672 }
673
674 gboolean
675 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
676 {
677         /*
678          * Set the packet encapsulation to the file's encapsulation
679          * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
680          * right answer (and means that the read routine for this
681          * capture file type doesn't have to set it), and if it
682          * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
683          * anyway.
684          */
685         wth->phdr.pkt_encap = wth->file_encap;
686
687         if (!wth->subtype_read(wth, err, err_info, data_offset))
688                 return FALSE;   /* failure */
689
690         /*
691          * It makes no sense for the captured data length to be bigger
692          * than the actual data length.
693          */
694         if (wth->phdr.caplen > wth->phdr.len)
695                 wth->phdr.caplen = wth->phdr.len;
696
697         /*
698          * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
699          * probably means the file has that encapsulation type
700          * but the read routine didn't set this packet's
701          * encapsulation type.
702          */
703         g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
704
705         return TRUE;    /* success */
706 }
707
708 /*
709  * Return an approximation of the amount of data we've read sequentially
710  * from the file so far.  (gint64, in case that's 64 bits.)
711  */
712 gint64
713 wtap_read_so_far(wtap *wth, int *err)
714 {
715         off_t file_pos;
716
717         file_pos = ws_lseek(wth->fd, 0, SEEK_CUR);
718         if (file_pos == -1) {
719                 if (err != NULL)
720                         *err = errno;
721                 return -1;
722         }
723         return file_pos;
724 }
725
726 struct wtap_pkthdr*
727 wtap_phdr(wtap *wth)
728 {
729         return &wth->phdr;
730 }
731
732 union wtap_pseudo_header*
733 wtap_pseudoheader(wtap *wth)
734 {
735         return &wth->pseudo_header;
736 }
737
738 guint8*
739 wtap_buf_ptr(wtap *wth)
740 {
741         return buffer_start_ptr(wth->frame_buffer);
742 }
743
744 gboolean
745 wtap_seek_read(wtap *wth, gint64 seek_off,
746         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
747         int *err, gchar **err_info)
748 {
749         return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,
750                 err, err_info);
751 }