Move the print modules into epan.
[metze/wireshark/wip.git] / wiretap / dbs-etherwatch.c
1 /* dbs-etherwatch.c
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 2001 by Marc Milgram <ethereal@mmilgram.NOSPAMmail.net>
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 #include "config.h"
24 #include "wtap-int.h"
25 #include "buffer.h"
26 #include "dbs-etherwatch.h"
27 #include "file_wrappers.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 /* This module reads the text output of the 'DBS-ETHERTRACE' command in VMS
35  * It was initially based on vms.c.
36  */
37
38 /*
39    Example 'ETHERWATCH' output data (with "printable" characters in the
40    "printable characters" section of the output replaced by "." if they have
41    the 8th bit set, so as not to upset compilers that are expecting text
42    in comments to be in a particular character encoding that can't handle
43    those values):
44 ETHERWATCH  X5-008
45 42 names and addresses were loaded
46 Reading recorded data from PERSISTENCE
47 ------------------------------------------------------------------------------
48 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
49 Protocol 08-00 00 00-00-00-00-00,   60 byte buffer at 10-OCT-2001 10:20:45.16
50   [E..<8...........]-    0-[45 00 00 3C 38 93 00 00 1D 06 D2 12 80 93 11 1A]
51   [.........(......]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 90 00 00 00 00]
52   [................]-   32-[A0 02 FF FF 95 BD 00 00 02 04 05 B4 03 03 04 01]
53   [............    ]-   48-[01 01 08 0A 90 90 E5 14 00 00 00 00]
54 ------------------------------------------------------------------------------
55 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
56 Protocol 08-00 00 00-00-00-00-00,   50 byte buffer at 10-OCT-2001 10:20:45.17
57   [E..(8......%....]-    0-[45 00 00 28 38 94 00 00 1D 06 D2 25 80 93 11 1A]
58   [.........(..Z.4w]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 91 5A 1C 34 77]
59   [P.#(.s..........]-   32-[50 10 23 28 C1 73 00 00 02 04 05 B4 03 03 00 00]
60   [..              ]-   48-[02 04]
61
62
63 Alternative HEX only output, slightly more efficient and all wireshark needs:
64 ------------------------------------------------------------------------------
65 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
66 Protocol 08-00 00 00-00-00-00-00,   50 byte buffer at 10-OCT-2001 10:20:45.17
67      0-[45 00 00 28 38 9B 00 00 1D 06 D2 1E 80 93 11 1A 80 93 80 D6]
68     20-[02 D2 02 03 00 28 A4 BF 5A 1C 34 79 50 10 23 28 C1 43 00 00]
69     40-[03 30 30 30 30 30 00 00 03 30]
70  */
71
72 /* Magic text to check for DBS-ETHERWATCH-ness of file */
73 static const char dbs_etherwatch_hdr_magic[]  =
74 { 'E', 'T', 'H', 'E', 'R', 'W', 'A', 'T', 'C', 'H', ' ', ' '};
75 #define DBS_ETHERWATCH_HDR_MAGIC_SIZE  \
76         (sizeof dbs_etherwatch_hdr_magic  / sizeof dbs_etherwatch_hdr_magic[0])
77
78 /* Magic text for start of packet */
79 static const char dbs_etherwatch_rec_magic[]  =
80 {'F', 'r', 'o', 'm', ' '};
81 #define DBS_ETHERWATCH_REC_MAGIC_SIZE \
82         (sizeof dbs_etherwatch_rec_magic  / sizeof dbs_etherwatch_rec_magic[0])
83
84 /*
85  * XXX - is this the biggest packet we can get?
86  */
87 #define DBS_ETHERWATCH_MAX_PACKET_LEN   16384
88
89 static gboolean dbs_etherwatch_read(wtap *wth, int *err, gchar **err_info,
90         gint64 *data_offset);
91 static gboolean dbs_etherwatch_seek_read(wtap *wth, gint64 seek_off,
92         struct wtap_pkthdr *phdr, Buffer *buf, int len,
93         int *err, gchar **err_info);
94 static gboolean parse_dbs_etherwatch_packet(struct wtap_pkthdr *phdr, FILE_T fh,
95         Buffer* buf, int *err, gchar **err_info);
96 static guint parse_single_hex_dump_line(char* rec, guint8 *buf,
97         int byte_offset);
98 static guint parse_hex_dump(char* dump, guint8 *buf, char seperator, char end);
99
100 /* Seeks to the beginning of the next packet, and returns the
101    byte offset.  Returns -1 on failure, and sets "*err" to the error
102    and "*err_info" to null or an additional error string. */
103 static gint64 dbs_etherwatch_seek_next_packet(wtap *wth, int *err,
104     gchar **err_info)
105 {
106   int byte;
107   unsigned int level = 0;
108   gint64 cur_off;
109
110   while ((byte = file_getc(wth->fh)) != EOF) {
111     if (byte == dbs_etherwatch_rec_magic[level]) {
112       level++;
113       if (level >= DBS_ETHERWATCH_REC_MAGIC_SIZE) {
114         /* note: we're leaving file pointer right after the magic characters */
115         cur_off = file_tell(wth->fh);
116         if (cur_off == -1) {
117           /* Error. */
118           *err = file_error(wth->fh, err_info);
119           return -1;
120         }
121         return cur_off + 1;
122       }
123     } else {
124       level = 0;
125     }
126   }
127   /* EOF or error. */
128   *err = file_error(wth->fh, err_info);
129   return -1;
130 }
131
132 #define DBS_ETHERWATCH_HEADER_LINES_TO_CHECK    200
133 #define DBS_ETHERWATCH_LINE_LENGTH              240
134
135 /* Look through the first part of a file to see if this is
136  * a DBS Ethertrace text trace file.
137  *
138  * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
139  * if we get an I/O error, "*err" will be set to a non-zero value and
140  * "*err_info" will be set to null or an error string.
141  */
142 static gboolean dbs_etherwatch_check_file_type(wtap *wth, int *err,
143     gchar **err_info)
144 {
145         char    buf[DBS_ETHERWATCH_LINE_LENGTH];
146         int     line, byte;
147         gsize   reclen;
148         unsigned int i, level;
149
150         buf[DBS_ETHERWATCH_LINE_LENGTH-1] = 0;
151
152         for (line = 0; line < DBS_ETHERWATCH_HEADER_LINES_TO_CHECK; line++) {
153                 if (file_gets(buf, DBS_ETHERWATCH_LINE_LENGTH, wth->fh) == NULL) {
154                         /* EOF or error. */
155                         *err = file_error(wth->fh, err_info);
156                         return FALSE;
157                 }
158
159                 reclen = strlen(buf);
160                 if (reclen < DBS_ETHERWATCH_HDR_MAGIC_SIZE)
161                         continue;
162
163                 level = 0;
164                 for (i = 0; i < reclen; i++) {
165                         byte = buf[i];
166                         if (byte == dbs_etherwatch_hdr_magic[level]) {
167                                 level++;
168                                 if (level >=
169                                       DBS_ETHERWATCH_HDR_MAGIC_SIZE) {
170                                         return TRUE;
171                                 }
172                         }
173                         else
174                                 level = 0;
175                 }
176         }
177         *err = 0;
178         return FALSE;
179 }
180
181
182 int dbs_etherwatch_open(wtap *wth, int *err, gchar **err_info)
183 {
184         /* Look for DBS ETHERWATCH header */
185         if (!dbs_etherwatch_check_file_type(wth, err, err_info)) {
186                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
187                         return -1;
188                 return 0;
189         }
190
191         wth->file_encap = WTAP_ENCAP_ETHERNET;
192         wth->file_type = WTAP_FILE_DBS_ETHERWATCH;
193         wth->snapshot_length = 0;       /* not known */
194         wth->subtype_read = dbs_etherwatch_read;
195         wth->subtype_seek_read = dbs_etherwatch_seek_read;
196         wth->tsprecision = WTAP_FILE_TSPREC_CSEC;
197
198         return 1;
199 }
200
201 /* Find the next packet and parse it; called from wtap_read(). */
202 static gboolean dbs_etherwatch_read(wtap *wth, int *err, gchar **err_info,
203     gint64 *data_offset)
204 {
205         gint64  offset;
206
207         /* Find the next packet */
208         offset = dbs_etherwatch_seek_next_packet(wth, err, err_info);
209         if (offset < 1)
210                 return FALSE;
211         *data_offset = offset;
212
213         /* Parse the packet */
214         return parse_dbs_etherwatch_packet(&wth->phdr, wth->fh,
215              wth->frame_buffer, err, err_info);
216 }
217
218 /* Used to read packets in random-access fashion */
219 static gboolean
220 dbs_etherwatch_seek_read(wtap *wth, gint64 seek_off,
221         struct wtap_pkthdr *phdr, Buffer *buf, int len _U_,
222         int *err, gchar **err_info)
223 {
224         if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
225                 return FALSE;
226
227         return parse_dbs_etherwatch_packet(phdr, wth->random_fh, buf, err,
228             err_info);
229 }
230
231 /* Parse a packet */
232 /*
233 Packet header:
234           1         2         3         4
235 0123456789012345678901234567890123456789012345
236 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
237 Protocol 08-00 00 00-00-00-00-00,   50 byte buffer at 10-OCT-2001 10:20:45.17
238 */
239 #define MAC_ADDR_LENGTH         6                       /* Length MAC address */
240 #define DEST_MAC_PREFIX         "] to "         /* Prefix to the dest. MAC address */
241 #define PROTOCOL_LENGTH         2                       /* Length protocol */
242 #define PROTOCOL_POS            9                       /* Position protocol */
243 #define SAP_LENGTH                      2                       /* Length DSAP+SSAP */
244 #define SAP_POS                         9                       /* Position DSAP+SSAP */
245 #define CTL_UNNUMB_LENGTH       1                       /* Length unnumbered control field */
246 #define CTL_NUMB_LENGTH         2                       /* Length numbered control field */
247 #define CTL_POS                         15                      /* Position control field */
248 #define PID_LENGTH                      5                       /* Length PID */
249 #define PID_POS                         18                      /* Position PID */
250 #define LENGTH_POS                      33                      /* Position length */
251 #define HEX_HDR_SPR                     '-'                     /* Seperator char header hex values */
252 #define HEX_HDR_END                     ' '                     /* End char hdr. hex val. except PID */
253 #define HEX_PID_END                     ','                     /* End char PID hex value */
254 #define IEEE802_LEN_LEN         2                       /* Length of the IEEE 802 len. field */
255 /*
256 To check whether it is Ethernet II or IEEE 802 we check the values of the
257 control field and PID, when they are all 0's we assume it is Ethernet II
258 else IEEE 802. In IEEE 802 the DSAP and SSAP are behind protocol, the
259 length in the IEEE data we have to construct.
260 */
261 #define ETH_II_CHECK_POS    15
262 #define ETH_II_CHECK_STR    "00 00-00-00-00-00,"
263 /*
264 To check whether it IEEE 802.3 with SNAP we check that both the DSAP & SSAP
265 values are 0xAA and the control field 0x03.
266 */
267 #define SNAP_CHECK_POS          9
268 #define SNAP_CHECK_STR          "AA-AA 03"
269 /*
270 To check whether the control field is 1 or two octets we check if it is
271 unnumbered. Unnumbered has length 1, numbered 2.
272 */
273 #define CTL_UNNUMB_MASK         0x03
274 #define CTL_UNNUMB_VALUE        0x03
275 static gboolean
276 parse_dbs_etherwatch_packet(struct wtap_pkthdr *phdr, FILE_T fh, Buffer* buf,
277     int *err, gchar **err_info)
278 {
279         guint8 *pd;
280         char    line[DBS_ETHERWATCH_LINE_LENGTH];
281         int     num_items_scanned;
282         int     eth_hdr_len, pkt_len, csec;
283         int length_pos, length_from, length;
284         struct tm tm;
285         char mon[4] = "xxx";
286         gchar *p;
287         static const gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
288         int     count, line_count;
289
290         /* Make sure we have enough room for the packet */
291         buffer_assure_space(buf, DBS_ETHERWATCH_MAX_PACKET_LEN);
292         pd = buffer_start_ptr(buf);
293
294         eth_hdr_len = 0;
295         memset(&tm, 0, sizeof(tm));
296         /* Our file pointer should be on the first line containing the
297          * summary information for a packet. Read in that line and
298          * extract the useful information
299          */
300         if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
301                 *err = file_error(fh, err_info);
302                 if (*err == 0) {
303                         *err = WTAP_ERR_SHORT_READ;
304                 }
305                 return FALSE;
306         }
307
308         /* Get the destination address */
309         p = strstr(line, DEST_MAC_PREFIX);
310         if(!p) {
311                 *err = WTAP_ERR_BAD_FILE;
312                 *err_info = g_strdup("dbs_etherwatch: destination address not found");
313                 return FALSE;
314         }
315         p += strlen(DEST_MAC_PREFIX);
316         if(parse_hex_dump(p, &pd[eth_hdr_len], HEX_HDR_SPR, HEX_HDR_END)
317                                 != MAC_ADDR_LENGTH) {
318                 *err = WTAP_ERR_BAD_FILE;
319                 *err_info = g_strdup("dbs_etherwatch: destination address not valid");
320                 return FALSE;
321         }
322         eth_hdr_len += MAC_ADDR_LENGTH;
323
324         /* Get the source address */
325         /*
326          * Since the first part of the line is already skipped in order to find
327          * the start of the record we cannot index, just look for the first
328          * 'HEX' character
329          */
330         p = line;
331         while(!isxdigit((guchar)*p)) {
332                 p++;
333         }
334         if(parse_hex_dump(p, &pd[eth_hdr_len], HEX_HDR_SPR,
335                 HEX_HDR_END) != MAC_ADDR_LENGTH) {
336                 *err = WTAP_ERR_BAD_FILE;
337                 *err_info = g_strdup("dbs_etherwatch: source address not valid");
338                 return FALSE;
339         }
340         eth_hdr_len += MAC_ADDR_LENGTH;
341
342         /* Read the next line of the record header */
343         if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
344                 *err = file_error(fh, err_info);
345                 if (*err == 0) {
346                         *err = WTAP_ERR_SHORT_READ;
347                 }
348                 return FALSE;
349         }
350
351         /* Check the lines is as least as long as the length position */
352         if(strlen(line) < LENGTH_POS) {
353                 *err = WTAP_ERR_BAD_FILE;
354                 *err_info = g_strdup("dbs_etherwatch: line too short");
355                 return FALSE;
356         }
357
358         num_items_scanned = sscanf(line + LENGTH_POS,
359                                 "%9d byte buffer at %2d-%3s-%4d %2d:%2d:%2d.%9d",
360                                 &pkt_len,
361                                 &tm.tm_mday, mon,
362                                 &tm.tm_year, &tm.tm_hour, &tm.tm_min,
363                                 &tm.tm_sec, &csec);
364
365         if (num_items_scanned != 8) {
366                 *err = WTAP_ERR_BAD_FILE;
367                 *err_info = g_strdup("dbs_etherwatch: header line not valid");
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->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
439
440         p = strstr(months, mon);
441         if (p)
442                 tm.tm_mon = (int)(p - months) / 3;
443         tm.tm_year -= 1900;
444
445         tm.tm_isdst = -1;
446         phdr->ts.secs = mktime(&tm);
447         phdr->ts.nsecs = csec * 10000000;
448         phdr->caplen = eth_hdr_len + pkt_len;
449         phdr->len = eth_hdr_len + pkt_len;
450
451         /*
452          * We don't have an FCS in this frame.
453          */
454         phdr->pseudo_header.eth.fcs_len = 0;
455
456         /* Parse the hex dump */
457         count = 0;
458         while (count < pkt_len) {
459                 if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
460                         *err = file_error(fh, err_info);
461                         if (*err == 0) {
462                                 *err = WTAP_ERR_SHORT_READ;
463                         }
464                         return FALSE;
465                 }
466                 if (!(line_count = parse_single_hex_dump_line(line,
467                                 &pd[eth_hdr_len + count], count))) {
468                         *err = WTAP_ERR_BAD_FILE;
469                         *err_info = g_strdup("dbs_etherwatch: packet data value not valid");
470                         return FALSE;
471                 }
472                 count += line_count;
473                 if (count > pkt_len) {
474                         *err = WTAP_ERR_BAD_FILE;
475                         *err_info = g_strdup("dbs_etherwatch: packet data value has too many bytes");
476                         return FALSE;
477                 }
478         }
479         return TRUE;
480 }
481
482 /* Parse a hex dump line */
483 /*
484 /DISPLAY=BOTH output:
485
486           1         2         3         4
487 0123456789012345678901234567890123456789012345
488   [E..(8...........]-    0-[45 00 00 28 38 9B 00 00 1D 06 D2 1E 80 93 11 1A]
489   [.........(..Z.4y]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 BF 5A 1C 34 79]
490   [P.#(.C...00000..]-   32-[50 10 23 28 C1 43 00 00 03 30 30 30 30 30 00 00]
491   [.0              ]-   48-[03 30]
492
493 /DISPLAY=HEXADECIMAL output:
494
495           1         2         3         4
496 0123456789012345678901234567890123456789012345
497      0-[45 00 00 28 38 9B 00 00 1D 06 D2 1E 80 93 11 1A 80 93 80 D6]
498     20-[02 D2 02 03 00 28 A4 BF 5A 1C 34 79 50 10 23 28 C1 43 00 00]
499     40-[03 30 30 30 30 30 00 00 03 30]
500
501 */
502
503 #define TYPE_CHECK_POS          2       /* Position to check the type of hex dump */
504 #define TYPE_CHECK_BOTH         '['     /* Value at pos. that indicates BOTH type */
505 #define COUNT_POS_BOTH          21      /* Count position BOTH type */
506 #define COUNT_POS_HEX           1       /* Count position HEX type */
507 #define COUNT_SIZE              5       /* Length counter */
508 #define HEX_DUMP_START          '['     /* Start char */
509 #define HEX_DUMP_SPR            ' ' /* Seperator char */
510 #define HEX_DUMP_END            ']' /* End char */
511
512 /* Take a string representing one line from a hex dump and converts the
513  * text to binary data. We check the printed offset with the offset
514  * we are passed to validate the record. We place the bytes in the buffer
515  * at the specified offset.
516  *
517  * Returns length parsed if a good hex dump, 0 if bad.
518  */
519 static guint
520 parse_single_hex_dump_line(char* rec, guint8 *buf, int byte_offset) {
521
522         int             pos, i;
523         int             value;
524
525
526         /* Check that the record is as least as long as the check offset */
527         for(i = 0; i < TYPE_CHECK_POS; i++)
528         {
529                 if(rec[i] == '\0') {
530                         return 0;
531                 }
532         }
533         /* determine the format and thus the counter offset and hex dump length */
534         if(rec[TYPE_CHECK_POS] == TYPE_CHECK_BOTH)
535         {
536                 pos = COUNT_POS_BOTH;
537         }
538         else
539         {
540                 pos = COUNT_POS_HEX;
541         }
542
543         /* Check that the record is as least as long as the start position */
544         while(i < pos)
545         {
546                 if(rec[i] == '\0') {
547                         return 0;
548                 }
549                 i++;
550         }
551
552         /* Get the byte_offset directly from the record */
553         value = 0;
554         for(i = 0; i < COUNT_SIZE; i++) {
555                 if(!isspace((guchar)rec[pos])) {
556                         if(isdigit((guchar)rec[pos])) {
557                                 value *= 10;
558                                 value += rec[pos] - '0';
559                         } else {
560                                 return 0;
561                         }
562                 }
563                 pos++;
564         }
565
566         if (value != byte_offset) {
567                 return 0;
568         }
569
570         /* find the start of the hex dump */
571         while(rec[pos] != HEX_DUMP_START) {
572                 if(rec[pos] == '\0') {
573                         return 0;
574                 }
575                 pos++;
576         }
577         pos++;
578         return parse_hex_dump(&rec[pos], buf, HEX_DUMP_SPR, HEX_DUMP_END);
579 }
580
581 /* Parse a hex dump */
582 static guint
583 parse_hex_dump(char* dump, guint8 *buf, char seperator, char end) {
584         int             pos, count;
585
586         /* Parse the hex dump */
587         pos = 0;
588         count = 0;
589         while(dump[pos] != end) {
590                 /* Check the hex value */
591                 if(!(isxdigit((guchar)dump[pos]) &&
592                     isxdigit((guchar)dump[pos + 1]))) {
593                         return 0;
594                 }
595                 /* Get the hex value value */
596                 if(isdigit((guchar)dump[pos])) {
597                         buf[count] = (dump[pos] - '0') << 4;
598                 } else {
599                         buf[count] = (toupper(dump[pos]) - 'A' + 10) << 4;
600                 }
601                 pos++;
602                 if(isdigit((guchar)dump[pos])) {
603                         buf[count] += dump[pos] - '0';
604                 } else {
605                         buf[count] += toupper(dump[pos]) - 'A' + 10;
606                 }
607                 pos++;
608                 count++;
609                 /* Skip the seperator characters */
610                 while(dump[pos] == seperator) {
611                         pos++;
612                 }
613         }
614         return count;
615 }