Rename wtap_dump_file_write_all() to wtap_dump_file_write(), and have
[metze/wireshark/wip.git] / wiretap / wtap-int.h
1 /* wtap-int.h
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifndef __WTAP_INT_H__
24 #define __WTAP_INT_H__
25
26 #ifdef HAVE_SYS_TIME_H
27 #include <sys/time.h>
28 #endif
29
30 #include <glib.h>
31 #include <stdio.h>
32 #include <time.h>
33
34 #ifdef HAVE_WINSOCK2_H
35 #include <winsock2.h>
36 #endif
37
38 #ifdef HAVE_LIBZ
39 #include <zlib.h>
40 #define FILE_T  gzFile
41 #else /* No zLib */
42 #define FILE_T  FILE *
43 #endif /* HAVE_LIBZ */
44
45 #include "wtap.h"
46
47 typedef gboolean (*subtype_read_func)(struct wtap*, int*, char**, gint64*);
48 typedef gboolean (*subtype_seek_read_func)(struct wtap*, gint64, union wtap_pseudo_header*,
49                                         guint8*, int, int *, char **);
50 struct wtap {
51         FILE_T                  fh;
52         int                     fd;           /* File descriptor for cap file */
53         FILE_T                  random_fh;    /* Secondary FILE_T for random access */
54         int                     file_type;
55         int                     snapshot_length;
56         struct Buffer           *frame_buffer;
57         struct wtap_pkthdr      phdr;
58         union wtap_pseudo_header pseudo_header;
59
60         gint64                  data_offset;
61
62         void                    *priv;
63
64         subtype_read_func       subtype_read;
65         subtype_seek_read_func  subtype_seek_read;
66         void                    (*subtype_sequential_close)(struct wtap*);
67         void                    (*subtype_close)(struct wtap*);
68         int                     file_encap;     /* per-file, for those
69                                                    file formats that have
70                                                    per-file encapsulation
71                                                    types */
72         int                     tsprecision;    /* timestamp precision of the lower 32bits
73                                                  * e.g. WTAP_FILE_TSPREC_USEC */
74 };
75
76 struct wtap_dumper;
77
78 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
79                 const struct wtap_pkthdr*, const union wtap_pseudo_header*,
80                 const guchar*, int*);
81 typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
82
83 struct wtap_dumper {
84         FILE*                   fh;
85         int                     file_type;
86         int                     snaplen;
87         int                     encap;
88         gboolean        compressed;
89         gint64          bytes_dumped;
90
91         void                    *priv;
92
93         subtype_write_func      subtype_write;
94         subtype_close_func      subtype_close;
95
96         int                     tsprecision;    /* timestamp precision of the lower 32bits
97                                                          * e.g. WTAP_FILE_TSPREC_USEC */
98 };
99
100 extern gboolean wtap_dump_file_write(wtap_dumper *wdh, const void *buf,
101     size_t bufsize, int *err);
102
103 extern gint wtap_num_file_types;
104
105 /* Macros to byte-swap 64-bit, 32-bit and 16-bit quantities. */
106 #define BSWAP64(x) \
107         ((((x)&G_GINT64_CONSTANT(0xFF00000000000000U))>>56) |   \
108          (((x)&G_GINT64_CONSTANT(0x00FF000000000000U))>>40) |   \
109          (((x)&G_GINT64_CONSTANT(0x0000FF0000000000U))>>24) |   \
110          (((x)&G_GINT64_CONSTANT(0x000000FF00000000U))>>8) |    \
111          (((x)&G_GINT64_CONSTANT(0x00000000FF000000U))<<8) |    \
112          (((x)&G_GINT64_CONSTANT(0x0000000000FF0000U))<<24) |   \
113          (((x)&G_GINT64_CONSTANT(0x000000000000FF00U))<<40) |   \
114          (((x)&G_GINT64_CONSTANT(0x00000000000000FFU))<<56))
115 #define BSWAP32(x) \
116         ((((x)&0xFF000000)>>24) | \
117          (((x)&0x00FF0000)>>8) | \
118          (((x)&0x0000FF00)<<8) | \
119          (((x)&0x000000FF)<<24))
120 #define BSWAP16(x) \
121          ((((x)&0xFF00)>>8) | \
122           (((x)&0x00FF)<<8))
123
124 /* Macros to byte-swap possibly-unaligned 64-bit, 32-bit and 16-bit quantities;
125  * they take a pointer to the quantity, and byte-swap it in place.
126  */
127 #define PBSWAP64(p) \
128         {                       \
129         guint8 tmp;             \
130         tmp = (p)[7];           \
131         (p)[7] = (p)[0];        \
132         (p)[0] = tmp;           \
133         tmp = (p)[6];           \
134         (p)[6] = (p)[1];        \
135         (p)[1] = tmp;           \
136         tmp = (p)[5];           \
137         (p)[5] = (p)[2];        \
138         (p)[2] = tmp;           \
139         tmp = (p)[4];           \
140         (p)[4] = (p)[3];        \
141         (p)[3] = tmp;           \
142         }
143 #define PBSWAP32(p) \
144         {                       \
145         guint8 tmp;             \
146         tmp = (p)[3];           \
147         (p)[3] = (p)[0];        \
148         (p)[0] = tmp;           \
149         tmp = (p)[2];           \
150         (p)[2] = (p)[1];        \
151         (p)[1] = tmp;           \
152         }
153 #define PBSWAP16(p) \
154         {                       \
155         guint8 tmp;             \
156         tmp = (p)[1];           \
157         (p)[1] = (p)[0];        \
158         (p)[0] = tmp;           \
159         }
160
161 /* Turn host-byte-order values into little-endian values. */
162 #define htoles(s) GUINT16_TO_LE(s)
163 #define htolel(l) GUINT32_TO_LE(l)
164 #define htolell(ll) GUINT64_TO_LE(ll)
165
166 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
167  * byte array, returns the value of the two or four bytes at the pointer.
168  * The pletoh[sl] versions return the little-endian representation.
169  * We also provide pntohll and pletohll, which extract 64-bit integral
170  * quantities.
171  *
172  * These will work regardless of the byte alignment of the pointer.
173  */
174
175 #ifndef pntohs
176 #define pntohs(p)  ((guint16)                       \
177                     ((guint16)*((const guint8 *)(p)+0)<<8|  \
178                      (guint16)*((const guint8 *)(p)+1)<<0))
179 #endif
180
181 #ifndef pntoh24
182 #define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16| \
183                      (guint32)*((const guint8 *)(p)+1)<<8|  \
184                      (guint32)*((const guint8 *)(p)+2)<<0)
185 #endif
186
187 #ifndef pntohl
188 #define pntohl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
189                     (guint32)*((const guint8 *)(p)+1)<<16|  \
190                     (guint32)*((const guint8 *)(p)+2)<<8|   \
191                     (guint32)*((const guint8 *)(p)+3)<<0)
192 #endif
193
194 #ifndef pntohll
195 #define pntohll(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
196                      (guint64)*((const guint8 *)(p)+1)<<48|  \
197                      (guint64)*((const guint8 *)(p)+2)<<40|  \
198                      (guint64)*((const guint8 *)(p)+3)<<32|  \
199                      (guint64)*((const guint8 *)(p)+4)<<24|  \
200                      (guint64)*((const guint8 *)(p)+5)<<16|  \
201                      (guint64)*((const guint8 *)(p)+6)<<8|   \
202                      (guint64)*((const guint8 *)(p)+7)<<0)
203 #endif
204
205
206 #ifndef pletohs
207 #define pletohs(p) ((guint16)                       \
208                     ((guint16)*((const guint8 *)(p)+1)<<8|  \
209                      (guint16)*((const guint8 *)(p)+0)<<0))
210 #endif
211
212 #ifndef pletoh24
213 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
214                      (guint32)*((const guint8 *)(p)+1)<<8|  \
215                      (guint32)*((const guint8 *)(p)+0)<<0)
216 #endif
217
218
219 #ifndef pletohl
220 #define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24|  \
221                     (guint32)*((const guint8 *)(p)+2)<<16|  \
222                     (guint32)*((const guint8 *)(p)+1)<<8|   \
223                     (guint32)*((const guint8 *)(p)+0)<<0)
224 #endif
225
226
227 #ifndef pletohll
228 #define pletohll(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
229                      (guint64)*((const guint8 *)(p)+6)<<48|  \
230                      (guint64)*((const guint8 *)(p)+5)<<40|  \
231                      (guint64)*((const guint8 *)(p)+4)<<32|  \
232                      (guint64)*((const guint8 *)(p)+3)<<24|  \
233                      (guint64)*((const guint8 *)(p)+2)<<16|  \
234                      (guint64)*((const guint8 *)(p)+1)<<8|   \
235                      (guint64)*((const guint8 *)(p)+0)<<0)
236 #endif
237
238 /* Pointer routines to put items out in a particular byte order.
239  * These will work regardless of the byte alignment of the pointer.
240  */
241
242 #ifndef phtons
243 #define phtons(p, v) \
244         {                               \
245         (p)[0] = (guint8)((v) >> 8);    \
246         (p)[1] = (guint8)((v) >> 0);    \
247         }
248 #endif
249
250 #ifndef phtonl
251 #define phtonl(p, v) \
252         {                               \
253         (p)[0] = (guint8)((v) >> 24);   \
254         (p)[1] = (guint8)((v) >> 16);   \
255         (p)[2] = (guint8)((v) >> 8);    \
256         (p)[3] = (guint8)((v) >> 0);    \
257         }
258 #endif
259
260 #ifndef phtonll
261 #define phtonll(p, v) \
262         {                               \
263         (p)[0] = (guint8)((v) >> 56);   \
264         (p)[1] = (guint8)((v) >> 48);   \
265         (p)[2] = (guint8)((v) >> 40);   \
266         (p)[3] = (guint8)((v) >> 32);   \
267         (p)[4] = (guint8)((v) >> 24);   \
268         (p)[5] = (guint8)((v) >> 16);   \
269         (p)[6] = (guint8)((v) >> 8);    \
270         (p)[7] = (guint8)((v) >> 0);    \
271         }
272 #endif
273
274 #ifndef pletonll
275 #define pletonll(p, v) \
276         {                               \
277         (p)[0] = (guint8)((v) >> 0);    \
278         (p)[1] = (guint8)((v) >> 8);    \
279         (p)[2] = (guint8)((v) >> 16);   \
280         (p)[3] = (guint8)((v) >> 24);   \
281         (p)[4] = (guint8)((v) >> 32);   \
282         (p)[5] = (guint8)((v) >> 40);   \
283         (p)[6] = (guint8)((v) >> 48);   \
284         (p)[7] = (guint8)((v) >> 56);   \
285         }
286 #endif
287
288 #define wtap_file_read_unknown_bytes(target, num_bytes, fh, err) \
289         G_STMT_START \
290         { \
291                 int _bytes_read; \
292                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
293                 if (_bytes_read != (int) (num_bytes)) { \
294                         *(err) = file_error((fh)); \
295                         return FALSE; \
296                 } \
297         } \
298         G_STMT_END
299
300 #define wtap_file_read_expected_bytes(target, num_bytes, fh, err) \
301         G_STMT_START \
302         { \
303                 int _bytes_read; \
304                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
305                 if (_bytes_read != (int) (num_bytes)) { \
306                         *(err) = file_error((fh)); \
307                         if (*(err) == 0 && _bytes_read > 0) { \
308                                 *(err) = WTAP_ERR_SHORT_READ; \
309                         } \
310                         return FALSE; \
311                 } \
312         } \
313         G_STMT_END
314
315 /* glib doesn't have g_ptr_array_len of all things!*/
316 #ifndef g_ptr_array_len
317 #define g_ptr_array_len(a)      ((a)->len)
318 #endif
319
320 #endif /* __WTAP_INT_H__ */