ip-over-ib description entry was missing
[metze/wireshark/wip.git] / wiretap / dbs-etherwatch.c
1 /* dbs-etherwatch.c
2  *
3  * Wiretap Library
4  * Copyright (c) 2001 by Marc Milgram <ethereal@mmilgram.NOSPAMmail.net>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "config.h"
22 #include "wtap-int.h"
23 #include "dbs-etherwatch.h"
24 #include "file_wrappers.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 /* This module reads the text output of the 'DBS-ETHERTRACE' command in VMS
30  * It was initially based on vms.c.
31  */
32
33 /*
34    Example 'ETHERWATCH' output data (with "printable" characters in the
35    "printable characters" section of the output replaced by "." if they have
36    the 8th bit set, so as not to upset compilers that are expecting text
37    in comments to be in a particular character encoding that can't handle
38    those values):
39 ETHERWATCH  X5-008
40 42 names and addresses were loaded
41 Reading recorded data from PERSISTENCE
42 ------------------------------------------------------------------------------
43 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
44 Protocol 08-00 00 00-00-00-00-00,   60 byte buffer at 10-OCT-2001 10:20:45.16
45   [E..<8...........]-    0-[45 00 00 3C 38 93 00 00 1D 06 D2 12 80 93 11 1A]
46   [.........(......]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 90 00 00 00 00]
47   [................]-   32-[A0 02 FF FF 95 BD 00 00 02 04 05 B4 03 03 04 01]
48   [............    ]-   48-[01 01 08 0A 90 90 E5 14 00 00 00 00]
49 ------------------------------------------------------------------------------
50 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
51 Protocol 08-00 00 00-00-00-00-00,   50 byte buffer at 10-OCT-2001 10:20:45.17
52   [E..(8......%....]-    0-[45 00 00 28 38 94 00 00 1D 06 D2 25 80 93 11 1A]
53   [.........(..Z.4w]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 91 5A 1C 34 77]
54   [P.#(.s..........]-   32-[50 10 23 28 C1 73 00 00 02 04 05 B4 03 03 00 00]
55   [..              ]-   48-[02 04]
56
57
58 Alternative HEX only output, slightly more efficient and all wireshark needs:
59 ------------------------------------------------------------------------------
60 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
61 Protocol 08-00 00 00-00-00-00-00,   50 byte buffer at 10-OCT-2001 10:20:45.17
62      0-[45 00 00 28 38 9B 00 00 1D 06 D2 1E 80 93 11 1A 80 93 80 D6]
63     20-[02 D2 02 03 00 28 A4 BF 5A 1C 34 79 50 10 23 28 C1 43 00 00]
64     40-[03 30 30 30 30 30 00 00 03 30]
65  */
66
67 /* Magic text to check for DBS-ETHERWATCH-ness of file */
68 static const char dbs_etherwatch_hdr_magic[]  =
69 { 'E', 'T', 'H', 'E', 'R', 'W', 'A', 'T', 'C', 'H', ' ', ' '};
70 #define DBS_ETHERWATCH_HDR_MAGIC_SIZE  \
71         (sizeof dbs_etherwatch_hdr_magic  / sizeof dbs_etherwatch_hdr_magic[0])
72
73 /* Magic text for start of packet */
74 static const char dbs_etherwatch_rec_magic[]  =
75 {'F', 'r', 'o', 'm', ' '};
76 #define DBS_ETHERWATCH_REC_MAGIC_SIZE \
77     (sizeof dbs_etherwatch_rec_magic  / sizeof dbs_etherwatch_rec_magic[0])
78
79 /*
80  * Default packet size - maximum normal Ethernet packet size, without an
81  * FCS.
82  */
83 #define DBS_ETHERWATCH_MAX_ETHERNET_PACKET_LEN   1514
84
85 static gboolean dbs_etherwatch_read(wtap *wth, int *err, gchar **err_info,
86     gint64 *data_offset);
87 static gboolean dbs_etherwatch_seek_read(wtap *wth, gint64 seek_off,
88     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
89 static gboolean parse_dbs_etherwatch_packet(struct wtap_pkthdr *phdr, FILE_T fh,
90     Buffer* buf, int *err, gchar **err_info);
91 static guint parse_single_hex_dump_line(char* rec, guint8 *buf,
92     int byte_offset);
93 static guint parse_hex_dump(char* dump, guint8 *buf, char seperator, char end);
94
95 /* Seeks to the beginning of the next packet, and returns the
96    byte offset.  Returns -1 on failure, and sets "*err" to the error
97    and "*err_info" to null or an additional error string. */
98 static gint64 dbs_etherwatch_seek_next_packet(wtap *wth, int *err,
99                                               gchar **err_info)
100 {
101     int byte;
102     unsigned int level = 0;
103     gint64 cur_off;
104
105     while ((byte = file_getc(wth->fh)) != EOF) {
106         if (byte == dbs_etherwatch_rec_magic[level]) {
107             level++;
108             if (level >= DBS_ETHERWATCH_REC_MAGIC_SIZE) {
109                 /* note: we're leaving file pointer right after the magic characters */
110                 cur_off = file_tell(wth->fh);
111                 if (cur_off == -1) {
112                     /* Error. */
113                     *err = file_error(wth->fh, err_info);
114                     return -1;
115                 }
116                 return cur_off + 1;
117             }
118         } else {
119             level = 0;
120         }
121     }
122     /* EOF or error. */
123     *err = file_error(wth->fh, err_info);
124     return -1;
125 }
126
127 #define DBS_ETHERWATCH_HEADER_LINES_TO_CHECK    200
128 #define DBS_ETHERWATCH_LINE_LENGTH      240
129
130 /* Look through the first part of a file to see if this is
131  * a DBS Ethertrace text trace file.
132  *
133  * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
134  * if we get an I/O error, "*err" will be set to a non-zero value and
135  * "*err_info" will be set to null or an error string.
136  */
137 static gboolean dbs_etherwatch_check_file_type(wtap *wth, int *err,
138     gchar **err_info)
139 {
140     char    buf[DBS_ETHERWATCH_LINE_LENGTH];
141     int line, byte;
142     gsize   reclen;
143     unsigned int i, level;
144
145     buf[DBS_ETHERWATCH_LINE_LENGTH-1] = 0;
146
147     for (line = 0; line < DBS_ETHERWATCH_HEADER_LINES_TO_CHECK; line++) {
148         if (file_gets(buf, DBS_ETHERWATCH_LINE_LENGTH, wth->fh) == NULL) {
149             /* EOF or error. */
150             *err = file_error(wth->fh, err_info);
151             return FALSE;
152         }
153
154         reclen = strlen(buf);
155         if (reclen < DBS_ETHERWATCH_HDR_MAGIC_SIZE)
156             continue;
157
158         level = 0;
159         for (i = 0; i < reclen; i++) {
160             byte = buf[i];
161             if (byte == dbs_etherwatch_hdr_magic[level]) {
162                 level++;
163                 if (level >=
164                       DBS_ETHERWATCH_HDR_MAGIC_SIZE) {
165                     return TRUE;
166                 }
167             }
168             else
169                 level = 0;
170         }
171     }
172     *err = 0;
173     return FALSE;
174 }
175
176
177 wtap_open_return_val dbs_etherwatch_open(wtap *wth, int *err, gchar **err_info)
178 {
179     /* Look for DBS ETHERWATCH header */
180     if (!dbs_etherwatch_check_file_type(wth, err, err_info)) {
181         if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
182             return WTAP_OPEN_ERROR;
183         return WTAP_OPEN_NOT_MINE;
184     }
185
186     wth->file_encap = WTAP_ENCAP_ETHERNET;
187     wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_DBS_ETHERWATCH;
188     wth->snapshot_length = 0;   /* not known */
189     wth->subtype_read = dbs_etherwatch_read;
190     wth->subtype_seek_read = dbs_etherwatch_seek_read;
191     wth->file_tsprec = WTAP_TSPREC_CSEC;
192
193     return WTAP_OPEN_MINE;
194 }
195
196 /* Find the next packet and parse it; called from wtap_read(). */
197 static gboolean dbs_etherwatch_read(wtap *wth, int *err, gchar **err_info,
198     gint64 *data_offset)
199 {
200     gint64  offset;
201
202     /* Find the next packet */
203     offset = dbs_etherwatch_seek_next_packet(wth, err, err_info);
204     if (offset < 1)
205         return FALSE;
206     *data_offset = offset;
207
208     /* Parse the packet */
209     return parse_dbs_etherwatch_packet(&wth->phdr, wth->fh,
210          wth->frame_buffer, err, err_info);
211 }
212
213 /* Used to read packets in random-access fashion */
214 static gboolean
215 dbs_etherwatch_seek_read(wtap *wth, gint64 seek_off,
216     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
217 {
218     if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
219         return FALSE;
220
221     return parse_dbs_etherwatch_packet(phdr, wth->random_fh, buf, err,
222         err_info);
223 }
224
225 /* Parse a packet */
226 /*
227 Packet header:
228           1         2         3         4
229 0123456789012345678901234567890123456789012345
230 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
231 Protocol 08-00 00 00-00-00-00-00,   50 byte buffer at 10-OCT-2001 10:20:45.17
232 */
233 #define MAC_ADDR_LENGTH     6           /* Length MAC address */
234 #define DEST_MAC_PREFIX     "] to "     /* Prefix to the dest. MAC address */
235 #define PROTOCOL_LENGTH     2           /* Length protocol */
236 #define PROTOCOL_POS        9           /* Position protocol */
237 #define SAP_LENGTH          2           /* Length DSAP+SSAP */
238 #define SAP_POS             9           /* Position DSAP+SSAP */
239 #define CTL_UNNUMB_LENGTH   1           /* Length unnumbered control field */
240 #define CTL_NUMB_LENGTH     2           /* Length numbered control field */
241 #define CTL_POS             15          /* Position control field */
242 #define PID_LENGTH          5           /* Length PID */
243 #define PID_POS             18          /* Position PID */
244 #define LENGTH_POS          33          /* Position length */
245 #define HEX_HDR_SPR         '-'         /* Seperator char header hex values */
246 #define HEX_HDR_END         ' '         /* End char hdr. hex val. except PID */
247 #define HEX_PID_END         ','         /* End char PID hex value */
248 #define IEEE802_LEN_LEN     2           /* Length of the IEEE 802 len. field */
249 /*
250 To check whether it is Ethernet II or IEEE 802 we check the values of the
251 control field and PID, when they are all 0's we assume it is Ethernet II
252 else IEEE 802. In IEEE 802 the DSAP and SSAP are behind protocol, the
253 length in the IEEE data we have to construct.
254 */
255 #define ETH_II_CHECK_POS    15
256 #define ETH_II_CHECK_STR    "00 00-00-00-00-00,"
257 /*
258 To check whether it IEEE 802.3 with SNAP we check that both the DSAP & SSAP
259 values are 0xAA and the control field 0x03.
260 */
261 #define SNAP_CHECK_POS      9
262 #define SNAP_CHECK_STR      "AA-AA 03"
263 /*
264 To check whether the control field is 1 or two octets we check if it is
265 unnumbered. Unnumbered has length 1, numbered 2.
266 */
267 #define CTL_UNNUMB_MASK     0x03
268 #define CTL_UNNUMB_VALUE    0x03
269 static gboolean
270 parse_dbs_etherwatch_packet(struct wtap_pkthdr *phdr, FILE_T fh, Buffer* buf,
271     int *err, gchar **err_info)
272 {
273     guint8 *pd;
274     char    line[DBS_ETHERWATCH_LINE_LENGTH];
275     int num_items_scanned;
276     int eth_hdr_len, pkt_len, csec;
277     int length_pos, length_from, length;
278     struct tm tm;
279     char mon[4] = "xxx";
280     gchar *p;
281     static const gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
282     int count, line_count;
283
284     /* Make sure we have enough room for a regular Ethernet packet */
285     ws_buffer_assure_space(buf, DBS_ETHERWATCH_MAX_ETHERNET_PACKET_LEN);
286     pd = ws_buffer_start_ptr(buf);
287
288     eth_hdr_len = 0;
289     memset(&tm, 0, sizeof(tm));
290     /* Our file pointer should be on the first line containing the
291      * summary information for a packet. Read in that line and
292      * extract the useful information
293      */
294     if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
295         *err = file_error(fh, err_info);
296         if (*err == 0) {
297             *err = WTAP_ERR_SHORT_READ;
298         }
299         return FALSE;
300     }
301
302     /* Get the destination address */
303     p = strstr(line, DEST_MAC_PREFIX);
304     if(!p) {
305         *err = WTAP_ERR_BAD_FILE;
306         *err_info = g_strdup("dbs_etherwatch: destination address not found");
307         return FALSE;
308     }
309     p += strlen(DEST_MAC_PREFIX);
310     if(parse_hex_dump(p, &pd[eth_hdr_len], HEX_HDR_SPR, HEX_HDR_END)
311                 != MAC_ADDR_LENGTH) {
312         *err = WTAP_ERR_BAD_FILE;
313         *err_info = g_strdup("dbs_etherwatch: destination address not valid");
314         return FALSE;
315     }
316     eth_hdr_len += MAC_ADDR_LENGTH;
317
318     /* Get the source address */
319     /*
320      * Since the first part of the line is already skipped in order to find
321      * the start of the record we cannot index, just look for the first
322      * 'HEX' character
323      */
324     p = line;
325     while(!g_ascii_isxdigit(*p)) {
326         p++;
327     }
328     if(parse_hex_dump(p, &pd[eth_hdr_len], HEX_HDR_SPR,
329         HEX_HDR_END) != MAC_ADDR_LENGTH) {
330         *err = WTAP_ERR_BAD_FILE;
331         *err_info = g_strdup("dbs_etherwatch: source address not valid");
332         return FALSE;
333     }
334     eth_hdr_len += MAC_ADDR_LENGTH;
335
336     /* Read the next line of the record header */
337     if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
338         *err = file_error(fh, err_info);
339         if (*err == 0) {
340             *err = WTAP_ERR_SHORT_READ;
341         }
342         return FALSE;
343     }
344
345     /* Check the lines is as least as long as the length position */
346     if(strlen(line) < LENGTH_POS) {
347         *err = WTAP_ERR_BAD_FILE;
348         *err_info = g_strdup("dbs_etherwatch: line too short");
349         return FALSE;
350     }
351
352     num_items_scanned = sscanf(line + LENGTH_POS,
353                 "%9d byte buffer at %2d-%3s-%4d %2d:%2d:%2d.%9d",
354                 &pkt_len,
355                 &tm.tm_mday, mon,
356                 &tm.tm_year, &tm.tm_hour, &tm.tm_min,
357                 &tm.tm_sec, &csec);
358
359     if (num_items_scanned != 8) {
360         *err = WTAP_ERR_BAD_FILE;
361         *err_info = g_strdup("dbs_etherwatch: header line not valid");
362         return FALSE;
363     }
364
365     if (pkt_len < 0) {
366         *err = WTAP_ERR_BAD_FILE;
367         *err_info = g_strdup("dbs_etherwatch: packet header has a negative packet length");
368         return FALSE;
369     }
370
371     /* Determine whether it is Ethernet II or IEEE 802 */
372     if(strncmp(&line[ETH_II_CHECK_POS], ETH_II_CHECK_STR,
373         strlen(ETH_II_CHECK_STR)) == 0) {
374         /* Ethernet II */
375         /* Get the Protocol */
376         if(parse_hex_dump(&line[PROTOCOL_POS], &pd[eth_hdr_len], HEX_HDR_SPR,
377                     HEX_HDR_END) != PROTOCOL_LENGTH) {
378             *err = WTAP_ERR_BAD_FILE;
379             *err_info = g_strdup("dbs_etherwatch: Ethernet II protocol value not valid");
380             return FALSE;
381         }
382         eth_hdr_len += PROTOCOL_LENGTH;
383     } else {
384         /* IEEE 802 */
385         /* Remember where to put the length in the header */
386         length_pos = eth_hdr_len;
387         /* Leave room in the header for the length */
388         eth_hdr_len += IEEE802_LEN_LEN;
389         /* Remember how much of the header should not be added to the length */
390         length_from = eth_hdr_len;
391         /* Get the DSAP + SSAP */
392         if(parse_hex_dump(&line[SAP_POS], &pd[eth_hdr_len], HEX_HDR_SPR,
393                     HEX_HDR_END) != SAP_LENGTH) {
394             *err = WTAP_ERR_BAD_FILE;
395             *err_info = g_strdup("dbs_etherwatch: 802.2 DSAP+SSAP value not valid");
396             return FALSE;
397         }
398         eth_hdr_len += SAP_LENGTH;
399         /* Get the (first part of the) control field */
400         if(parse_hex_dump(&line[CTL_POS], &pd[eth_hdr_len], HEX_HDR_SPR,
401                     HEX_HDR_END) != CTL_UNNUMB_LENGTH) {
402             *err = WTAP_ERR_BAD_FILE;
403             *err_info = g_strdup("dbs_etherwatch: 802.2 control field first part not valid");
404             return FALSE;
405         }
406         /* Determine whether the control is numbered, and thus longer */
407         if((pd[eth_hdr_len] & CTL_UNNUMB_MASK) != CTL_UNNUMB_VALUE) {
408             /* Get the rest of the control field, the first octet in the PID */
409             if(parse_hex_dump(&line[PID_POS],
410                         &pd[eth_hdr_len + CTL_UNNUMB_LENGTH], HEX_HDR_END,
411                         HEX_HDR_SPR) != CTL_NUMB_LENGTH - CTL_UNNUMB_LENGTH) {
412                 *err = WTAP_ERR_BAD_FILE;
413                 *err_info = g_strdup("dbs_etherwatch: 802.2 control field second part value not valid");
414                 return FALSE;
415             }
416             eth_hdr_len += CTL_NUMB_LENGTH;
417         } else {
418             eth_hdr_len += CTL_UNNUMB_LENGTH;
419         }
420         /* Determine whether it is SNAP */
421         if(strncmp(&line[SNAP_CHECK_POS], SNAP_CHECK_STR,
422                 strlen(SNAP_CHECK_STR)) == 0) {
423             /* Get the PID */
424             if(parse_hex_dump(&line[PID_POS], &pd[eth_hdr_len], HEX_HDR_SPR,
425                         HEX_PID_END) != PID_LENGTH) {
426                 *err = WTAP_ERR_BAD_FILE;
427                 *err_info = g_strdup("dbs_etherwatch: 802.2 PID value not valid");
428                 return FALSE;
429             }
430             eth_hdr_len += PID_LENGTH;
431         }
432         /* Write the length in the header */
433         length = eth_hdr_len - length_from + pkt_len;
434         pd[length_pos] = (length) >> 8;
435         pd[length_pos+1] = (length) & 0xFF;
436     }
437
438     phdr->rec_type = REC_TYPE_PACKET;
439     phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
440
441     p = strstr(months, mon);
442     if (p)
443         tm.tm_mon = (int)(p - months) / 3;
444     tm.tm_year -= 1900;
445
446     tm.tm_isdst = -1;
447     phdr->ts.secs = mktime(&tm);
448     phdr->ts.nsecs = csec * 10000000;
449     phdr->caplen = eth_hdr_len + pkt_len;
450     phdr->len = eth_hdr_len + pkt_len;
451
452     if (phdr->caplen > WTAP_MAX_PACKET_SIZE) {
453         /*
454          * Probably a corrupt capture file; return an error,
455          * so that our caller doesn't blow up trying to allocate
456          * space for an immensely-large packet.
457          */
458         *err = WTAP_ERR_BAD_FILE;
459         *err_info = g_strdup_printf("dbs_etherwatch: File has %u-byte packet, bigger than maximum of %u",
460                                     phdr->caplen, WTAP_MAX_PACKET_SIZE);
461         return FALSE;
462     }
463
464     /* Make sure we have enough room, even for an oversized Ethernet packet */
465     ws_buffer_assure_space(buf, phdr->caplen);
466     pd = ws_buffer_start_ptr(buf);
467
468     /*
469      * We don't have an FCS in this frame.
470      */
471     phdr->pseudo_header.eth.fcs_len = 0;
472
473     /* Parse the hex dump */
474     count = 0;
475     while (count < pkt_len) {
476         if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
477             *err = file_error(fh, err_info);
478             if (*err == 0) {
479                 *err = WTAP_ERR_SHORT_READ;
480             }
481             return FALSE;
482         }
483         if (!(line_count = parse_single_hex_dump_line(line,
484                 &pd[eth_hdr_len + count], count))) {
485             *err = WTAP_ERR_BAD_FILE;
486             *err_info = g_strdup("dbs_etherwatch: packet data value not valid");
487             return FALSE;
488         }
489         count += line_count;
490         if (count > pkt_len) {
491             *err = WTAP_ERR_BAD_FILE;
492             *err_info = g_strdup("dbs_etherwatch: packet data value has too many bytes");
493             return FALSE;
494         }
495     }
496     return TRUE;
497 }
498
499 /* Parse a hex dump line */
500 /*
501 /DISPLAY=BOTH output:
502
503           1         2         3         4
504 0123456789012345678901234567890123456789012345
505   [E..(8...........]-    0-[45 00 00 28 38 9B 00 00 1D 06 D2 1E 80 93 11 1A]
506   [.........(..Z.4y]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 BF 5A 1C 34 79]
507   [P.#(.C...00000..]-   32-[50 10 23 28 C1 43 00 00 03 30 30 30 30 30 00 00]
508   [.0              ]-   48-[03 30]
509
510 /DISPLAY=HEXADECIMAL output:
511
512           1         2         3         4
513 0123456789012345678901234567890123456789012345
514      0-[45 00 00 28 38 9B 00 00 1D 06 D2 1E 80 93 11 1A 80 93 80 D6]
515     20-[02 D2 02 03 00 28 A4 BF 5A 1C 34 79 50 10 23 28 C1 43 00 00]
516     40-[03 30 30 30 30 30 00 00 03 30]
517
518 */
519
520 #define TYPE_CHECK_POS      2   /* Position to check the type of hex dump */
521 #define TYPE_CHECK_BOTH     '[' /* Value at pos. that indicates BOTH type */
522 #define COUNT_POS_BOTH      21  /* Count position BOTH type */
523 #define COUNT_POS_HEX       1   /* Count position HEX type */
524 #define COUNT_SIZE      5   /* Length counter */
525 #define HEX_DUMP_START      '[' /* Start char */
526 #define HEX_DUMP_SPR        ' ' /* Seperator char */
527 #define HEX_DUMP_END        ']' /* End char */
528
529 /* Take a string representing one line from a hex dump and converts the
530  * text to binary data. We check the printed offset with the offset
531  * we are passed to validate the record. We place the bytes in the buffer
532  * at the specified offset.
533  *
534  * Returns length parsed if a good hex dump, 0 if bad.
535  */
536 static guint
537 parse_single_hex_dump_line(char* rec, guint8 *buf, int byte_offset) {
538
539     int     pos, i;
540     int     value;
541
542
543     /* Check that the record is as least as long as the check offset */
544     for(i = 0; i < TYPE_CHECK_POS; i++)
545     {
546         if(rec[i] == '\0') {
547             return 0;
548         }
549     }
550     /* determine the format and thus the counter offset and hex dump length */
551     if(rec[TYPE_CHECK_POS] == TYPE_CHECK_BOTH)
552     {
553         pos = COUNT_POS_BOTH;
554     }
555     else
556     {
557         pos = COUNT_POS_HEX;
558     }
559
560     /* Check that the record is as least as long as the start position */
561     while(i < pos)
562     {
563         if(rec[i] == '\0') {
564             return 0;
565         }
566         i++;
567     }
568
569     /* Get the byte_offset directly from the record */
570     value = 0;
571     for(i = 0; i < COUNT_SIZE; i++) {
572         if(!g_ascii_isspace(rec[pos])) {
573             if(g_ascii_isdigit(rec[pos])) {
574                 value *= 10;
575                 value += rec[pos] - '0';
576             } else {
577                 return 0;
578             }
579         }
580         pos++;
581     }
582
583     if (value != byte_offset) {
584         return 0;
585     }
586
587     /* find the start of the hex dump */
588     while(rec[pos] != HEX_DUMP_START) {
589         if(rec[pos] == '\0') {
590             return 0;
591         }
592         pos++;
593     }
594     pos++;
595     return parse_hex_dump(&rec[pos], buf, HEX_DUMP_SPR, HEX_DUMP_END);
596 }
597
598 /* Parse a hex dump */
599 static guint
600 parse_hex_dump(char* dump, guint8 *buf, char seperator, char end) {
601     int     pos, count;
602
603     /* Parse the hex dump */
604     pos = 0;
605     count = 0;
606     while(dump[pos] != end) {
607         /* Check the hex value */
608         if(!(g_ascii_isxdigit(dump[pos]) &&
609             g_ascii_isxdigit(dump[pos + 1]))) {
610             return 0;
611         }
612         /* Get the hex value value */
613         if(g_ascii_isdigit(dump[pos])) {
614             buf[count] = (dump[pos] - '0') << 4;
615         } else {
616             buf[count] = (g_ascii_toupper(dump[pos]) - 'A' + 10) << 4;
617         }
618         pos++;
619         if(g_ascii_isdigit(dump[pos])) {
620             buf[count] += dump[pos] - '0';
621         } else {
622             buf[count] += g_ascii_toupper(dump[pos]) - 'A' + 10;
623         }
624         pos++;
625         count++;
626         /* Skip the seperator characters */
627         while(dump[pos] == seperator) {
628             pos++;
629         }
630     }
631     return count;
632 }
633
634 /*
635  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
636  *
637  * Local variables:
638  * c-basic-offset: 4
639  * tab-width: 8
640  * indent-tabs-mode: nil
641  * End:
642  *
643  * vi: set shiftwidth=4 tabstop=8 expandtab:
644  * :indentSize=4:tabSize=8:noTabs=true:
645  */