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