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