change all file offsets from long to gint64 so we can - theoretically - handle files...
[obnox/wireshark/wip.git] / wiretap / cosine.c
1 /* cosine.c
2  *
3  * $Id$
4  *
5  * CoSine IPNOS L2 debug output parsing
6  * Copyright (c) 2002 by Motonori Shindo <mshindo@mshindo.net>
7  *
8  * Wiretap Library
9  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include "wtap-int.h"
30 #include "buffer.h"
31 #include "cosine.h"
32 #include "file_wrappers.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38
39 /*
40
41   IPNOS: CONFIG VPN(100) VR(1.1.1.1)# diags
42   ipnos diags: Control (1/0) :: layer-2 ?
43   Registered commands for area "layer-2"
44       apply-pkt-log-profile  Configure packet logging on an interface
45       create-pkt-log-profile  Set packet-log-profile to be used for packet logging (see layer-2 pkt-log)
46       detail                Get Layer 2 low-level details
47
48   ipnos diags: Control (1/0) :: layer-2 create ?
49       create-pkt-log-profile  <pkt-log-profile-id ctl-tx-trace-length ctl-rx-trace-length data-tx-trace-length data-rx-trace-length pe-logging-or-control-blade>
50
51   ipnos diags: Control (1/0) :: layer-2 create 1 32 32 0 0 0
52   ipnos diags: Control (1/0) :: layer-2 create 2 32 32 100 100 0
53   ipnos diags: Control (1/0) :: layer-2 apply ?
54       apply-pkt-log-profile  <slot port channel subif pkt-log-profile-id>
55
56   ipnos diags: Control (1/0) :: layer-2 apply 3 0x0701 100 0 1
57   Successfully applied packet-log-profile on LI
58
59   -- Note that only the control packets are logged because the data packet size parameters are 0 in profile 1
60   IPNOS: CONFIG VPN(200) VR(3.3.3.3)# ping 20.20.20.43
61   vpn 200 : [max tries 4, timeout 5 seconds, data length 64 bytes, ttl 255]
62   ping #1 ok, RTT 0.000 seconds
63   ping #2 ok, RTT 0.000 seconds
64   ping #3 ok, RTT 0.000 seconds
65   ping #4 ok, RTT 0.000 seconds
66   [finished]
67
68   IPNOS: CONFIG VPN(200) VR(3.3.3.3)# 2000-2-1,18:19:46.8:  l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
69
70
71   2000-2-1,18:19:46.8:  l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x30000]
72
73   2000-2-1,18:19:46.8:  l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
74
75   2000-2-1,18:19:46.8:  l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x8030000]
76
77   ipnos diags: Control (1/0) :: layer-2 apply 3 0x0701 100 0 0
78   Successfully applied packet-log-profile on LI
79   ipnos diags: Control (1/0) :: layer-2 apply 3 0x0701 100 0 2
80   Successfully applied packet-log-profile on LI
81
82   -- Note that both control and data packets are logged because the data packet size parameter is 100 in profile 2
83      Please ignore the event-log messages getting mixed up with the ping command
84   ping 20.20.20.43 cou2000-2-1,18:20:17.0:  l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
85
86           00 D0 D8 D2 FF 03 C0 21  09 29 00 08 6B 60 84 AA
87
88   2000-2-1,18:20:17.0:  l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x30000]
89           00 D0 D8 D2 FF 03 C0 21  09 29 00 08 6D FE FA AA
90
91   2000-2-1,18:20:17.0:  l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
92           00 D0 D8 D2 FF 03 C0 21  0A 29 00 08 6B 60 84 AA
93
94   2000-2-1,18:20:17.0:  l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x8030000]
95           00 D0 D8 D2 FF 03 C0 21  0A 29 00 08 6D FE FA AA
96
97   nt 1 length 500
98   vpn 200 : [max tries 1, timeout 5 seconds, data length 500 bytes, ttl 255]
99   2000-2-1,18:20:24.1:  l2-tx (PPP:3/7/1:100), Length:536, Pro:1, Off:8, Pri:7, RM:0, Err:0 [0x4070, 0x801]
100           00 D0 D8 D2 FF 03 00 21  45 00 02 10 00 27 00 00
101           FF 01 69 51 14 14 14 22  14 14 14 2B 08 00 AD B8
102           00 03 00 01 10 11 12 13  14 15 16 17 18 19 1A 1B
103           1C 1D 1E 1F 20 21 22 23  24 25 26 27 28 29 2A 2B
104           2C 2D 2E 2F 30 31 32 33  34 35 36 37 38 39 3A 3B
105           3C 3D 3E 3F 40 41 42 43  44 45 46 47 48 49 4A 4B
106           4C 4D 4E 4F
107
108   ping #1 ok, RTT 0.010 seconds
109   2000-2-1,18:20:24.1:  l2-rx (PPP:3/7/1:100), Length:536, Pro:1, Off:8, Pri:7, RM:0, Err:0 [0x4071, 0x30801]
110           00 D0 D8 D2 FF 03 00 21  45 00 02 10 00 23 00 00
111           FF 01 69 55 14 14 14 2B  14 14 14 22 00 00 B5 B8
112           00 03 00 01 10 11 12 13  14 15 16 17 18 19 1A 1B
113           1C 1D 1E 1F 20 21 22 23  24 25 26 27 28 29 2A 2B
114           2C 2D 2E 2F 30 31 32 33  34 35 36 37 38 39 3A 3B
115           3C 3D 3E 3F 40 41 42 43  44 45 46 47 48 49 4A 4B
116           4C 4D 4E 4F
117
118   [finished]
119
120   IPNOS: CONFIG VPN(200) VR(3.3.3.3)# 2000-2-1,18:20:27.0:  l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
121
122           00 D0 D8 D2 FF 03 C0 21  09 2A 00 08 6B 60 84 AA
123
124   2000-2-1,18:20:27.0:  l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x30000]
125           00 D0 D8 D2 FF 03 C0 21  09 2A 00 08 6D FE FA AA
126
127   2000-2-1,18:20:27.0:  l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
128           00 D0 D8 D2 FF 03 C0 21  0A 2A 00 08 6B 60 84 AA
129
130   2000-2-1,18:20:27.0:  l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x30000]
131           00 D0 D8 D2 FF 03 C0 21  0A 2A 00 08 6D FE FA AA
132
133
134   ipnos diags: Control (1/0) :: layer-2 apply 3 0x0701 100 0 0
135   Successfully applied packet-log-profile on LI
136   ipnos diags: Control (1/0) ::
137
138  */
139
140 /* XXX TODO:
141
142   o Handle a case where an empty line doesn't exists as a delimiter of
143     each packet. If the output is sent to a control blade and
144     displayed as an event log, there's always an empty line between
145     each packet output, but it may not be true when it is an PE
146     output.
147
148   o Some telnet client on Windows may put in a line break at 80
149     columns when it save the session to a text file ("CRT" is such an
150     example). I don't think it's a good idea for the telnet client to
151     do so, but CRT is widely used in Windows community, I should
152     take care of that in the future.
153
154 */
155
156 /* Magic text to check for CoSine L2 debug output */
157 #define COSINE_HDR_MAGIC_STR1   "l2-tx"
158 #define COSINE_HDR_MAGIC_STR2   "l2-rx"
159
160 /* Magic text for start of packet */
161 #define COSINE_REC_MAGIC_STR1   COSINE_HDR_MAGIC_STR1
162 #define COSINE_REC_MAGIC_STR2   COSINE_HDR_MAGIC_STR2
163
164 #define COSINE_HEADER_LINES_TO_CHECK    200
165 #define COSINE_LINE_LENGTH              240
166
167 #define COSINE_MAX_PACKET_LEN   65536
168
169 static gboolean empty_line(const gchar *line);
170 static gint64 cosine_seek_next_packet(wtap *wth, int *err, char *hdr);
171 static gboolean cosine_check_file_type(wtap *wth, int *err);
172 static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
173         gint64 *data_offset);
174 static gboolean cosine_seek_read(wtap *wth, gint64 seek_off,
175         union wtap_pseudo_header *pseudo_header, guint8 *pd,
176         int len, int *err, gchar **err_info);
177 static int parse_cosine_rec_hdr(wtap *wth, const char *line,
178         union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
179 static int parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf,
180         int *err, gchar **err_info);
181 static int parse_single_hex_dump_line(char* rec, guint8 *buf,
182         guint byte_offset);
183
184 /* Returns TRUE if the line appears to be an empty line. Otherwise it
185    returns FALSE. */
186 static gboolean empty_line(const gchar *line)
187 {
188         while (*line) {
189                 if (isspace((guchar)*line)) {
190                         line++;
191                         continue;
192                 } else {
193                         break;
194                 }
195         }
196         if (*line == '\0')
197                 return TRUE;
198         else
199                 return FALSE;
200 }
201
202 /* Seeks to the beginning of the next packet, and returns the
203    byte offset. Copy the header line to hdr. Returns -1 on failure,
204    and sets "*err" to the error and set hdr as NULL. */
205 static gint64 cosine_seek_next_packet(wtap *wth, int *err, char *hdr)
206 {
207         gint64 cur_off;
208         char buf[COSINE_LINE_LENGTH];
209
210         while (1) {
211                 cur_off = file_tell(wth->fh);
212                 if (cur_off == -1) {
213                         /* Error */
214                         *err = file_error(wth->fh);
215                         hdr = NULL;
216                         return -1;
217                 }
218                 if (file_gets(buf, sizeof(buf), wth->fh) != NULL) {
219                         if (strstr(buf, COSINE_REC_MAGIC_STR1) ||
220                             strstr(buf, COSINE_REC_MAGIC_STR2)) {
221                                 strncpy(hdr, buf, COSINE_LINE_LENGTH-1);
222                                 hdr[COSINE_LINE_LENGTH-1] = '\0';
223                                 return cur_off;
224                         }
225                 } else {
226                         if (file_eof(wth->fh)) {
227                                 /* We got an EOF. */
228                                 *err = 0;
229                         } else {
230                                 /* We (presumably) got an error (there's no
231                                    equivalent to "ferror()" in zlib, alas,
232                                    so we don't have a wrapper to check for
233                                    an error). */
234                                 *err = file_error(wth->fh);
235                         }
236                         break;
237                 }
238         }
239         hdr = NULL;
240         return -1;
241 }
242
243 /* Look through the first part of a file to see if this is
244  * a CoSine L2 debug output.
245  *
246  * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
247  * if we get an I/O error, "*err" will be set to a non-zero value.
248  */
249 static gboolean cosine_check_file_type(wtap *wth, int *err)
250 {
251         char    buf[COSINE_LINE_LENGTH];
252         guint   reclen, line;
253
254         buf[COSINE_LINE_LENGTH-1] = '\0';
255
256         for (line = 0; line < COSINE_HEADER_LINES_TO_CHECK; line++) {
257                 if (file_gets(buf, COSINE_LINE_LENGTH, wth->fh) != NULL) {
258
259                         reclen = strlen(buf);
260                         if (reclen < strlen(COSINE_HDR_MAGIC_STR1) ||
261                                 reclen < strlen(COSINE_HDR_MAGIC_STR2)) {
262                                 continue;
263                         }
264
265                         if (strstr(buf, COSINE_HDR_MAGIC_STR1) ||
266                             strstr(buf, COSINE_HDR_MAGIC_STR2)) {
267                                 return TRUE;
268                         }
269                 } else {
270                         /* EOF or error. */
271                         if (file_eof(wth->fh))
272                                 *err = 0;
273                         else
274                                 *err = file_error(wth->fh);
275                         return FALSE;
276                 }
277         }
278         *err = 0;
279         return FALSE;
280 }
281
282
283 int cosine_open(wtap *wth, int *err, gchar **err_info _U_)
284 {
285         /* Look for CoSine header */
286         if (!cosine_check_file_type(wth, err)) {
287                 if (*err == 0)
288                         return 0;
289                 else
290                         return -1;
291         }
292
293         if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1)        /* rewind */
294                 return -1;
295
296         wth->data_offset = 0;
297         wth->file_encap = WTAP_ENCAP_COSINE;
298         wth->file_type = WTAP_FILE_COSINE;
299         wth->snapshot_length = 0; /* not known */
300         wth->subtype_read = cosine_read;
301         wth->subtype_seek_read = cosine_seek_read;
302     wth->tsprecision = WTAP_FILE_TSPREC_CSEC;
303
304         return 1;
305 }
306
307 /* Find the next packet and parse it; called from wtap_read(). */
308 static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
309     gint64 *data_offset)
310 {
311         gint64  offset;
312         guint8  *buf;
313         int     pkt_len, caplen;
314         char    line[COSINE_LINE_LENGTH];
315
316         /* Find the next packet */
317         offset = cosine_seek_next_packet(wth, err, line);
318         if (offset < 0)
319                 return FALSE;
320
321         /* Parse the header */
322         pkt_len = parse_cosine_rec_hdr(wth, line, &wth->pseudo_header, err,
323             err_info);
324         if (pkt_len == -1)
325                 return FALSE;
326
327         /* Make sure we have enough room for the packet */
328         buffer_assure_space(wth->frame_buffer, COSINE_MAX_PACKET_LEN);
329         buf = buffer_start_ptr(wth->frame_buffer);
330
331         /* Convert the ASCII hex dump to binary data */
332         if ((caplen = parse_cosine_hex_dump(wth->fh, pkt_len, buf, err,
333             err_info)) == -1)
334                 return FALSE;
335
336         wth->data_offset = offset;
337         wth->phdr.caplen = caplen;
338         *data_offset = offset;
339         return TRUE;
340 }
341
342 /* Used to read packets in random-access fashion */
343 static gboolean
344 cosine_seek_read (wtap *wth, gint64 seek_off,
345         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
346         int *err, gchar **err_info)
347 {
348         char    line[COSINE_LINE_LENGTH];
349
350         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
351                 return FALSE;
352
353         if (file_gets(line, COSINE_LINE_LENGTH, wth->random_fh) == NULL) {
354                 *err = file_error(wth->random_fh);
355                 if (*err == 0) {
356                         *err = WTAP_ERR_SHORT_READ;
357                 }
358                 return FALSE;
359         }
360
361         if (parse_cosine_rec_hdr(NULL, line, pseudo_header, err, err_info) == -1)
362                 return FALSE;
363
364         return parse_cosine_hex_dump(wth->random_fh, len, pd, err, err_info);
365 }
366
367 /* Parses a packet record header. There are two possible formats:
368     1) output to a control blade with date and time
369         2002-5-10,20:1:31.4:  l2-tx (FR:3/7/1:1), Length:18, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
370     2) output to PE without date and time
371         l2-tx (FR:3/7/1:1), Length:18, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0] */
372 static int
373 parse_cosine_rec_hdr(wtap *wth, const char *line,
374     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
375 {
376         int     num_items_scanned;
377         int     yy, mm, dd, hr, min, sec, csec, pkt_len;
378         int     pro, off, pri, rm, error;
379         guint   code1, code2;
380         char    if_name[COSINE_MAX_IF_NAME_LEN], direction[6];
381         struct  tm tm;
382
383         if (sscanf(line, "%d-%d-%d,%d:%d:%d.%d:",
384                    &yy, &mm, &dd, &hr, &min, &sec, &csec) == 7) {
385                 /* appears to be output to a control blade */
386                 num_items_scanned = sscanf(line,
387                    "%d-%d-%d,%d:%d:%d.%d: %5s (%127[A-Za-z0-9/:]), Length:%d, Pro:%d, Off:%d, Pri:%d, RM:%d, Err:%d [%x, %x]",
388                         &yy, &mm, &dd, &hr, &min, &sec, &csec,
389                                    direction, if_name, &pkt_len,
390                                    &pro, &off, &pri, &rm, &error,
391                                    &code1, &code2);
392
393                 if (num_items_scanned != 17) {
394                         *err = WTAP_ERR_BAD_RECORD;
395                         *err_info = g_strdup("cosine: purported control blade line doesn't have code values");
396                         return -1;
397                 }
398         } else {
399                 /* appears to be output to PE */
400                 num_items_scanned = sscanf(line,
401                    "%5s (%127[A-Za-z0-9/:]), Length:%d, Pro:%d, Off:%d, Pri:%d, RM:%d, Err:%d [%x, %x]",
402                                    direction, if_name, &pkt_len,
403                                    &pro, &off, &pri, &rm, &error,
404                                    &code1, &code2);
405
406                 if (num_items_scanned != 10) {
407                         *err = WTAP_ERR_BAD_RECORD;
408                         *err_info = g_strdup("cosine: header line is neither control blade nor PE output");
409                         return -1;
410                 }
411                 yy = mm = dd = hr = min = sec = csec = 0;
412         }
413
414         if (wth) {
415                 tm.tm_year = yy - 1900;
416                 tm.tm_mon = mm - 1;
417                 tm.tm_mday = dd;
418                 tm.tm_hour = hr;
419                 tm.tm_min = min;
420                 tm.tm_sec = sec;
421                 tm.tm_isdst = -1;
422                 wth->phdr.ts.secs = mktime(&tm);
423                 wth->phdr.ts.nsecs = csec * 10000000;
424                 wth->phdr.len = pkt_len;
425         }
426         /* XXX need to handle other encapsulations like Cisco HDLC,
427            Frame Relay and ATM */
428         if (strncmp(if_name, "TEST:", 5) == 0) {
429                 pseudo_header->cosine.encap = COSINE_ENCAP_TEST;
430         } else if (strncmp(if_name, "PPoATM:", 7) == 0) {
431                 pseudo_header->cosine.encap = COSINE_ENCAP_PPoATM;
432         } else if (strncmp(if_name, "PPoFR:", 6) == 0) {
433                 pseudo_header->cosine.encap = COSINE_ENCAP_PPoFR;
434         } else if (strncmp(if_name, "ATM:", 4) == 0) {
435                 pseudo_header->cosine.encap = COSINE_ENCAP_ATM;
436         } else if (strncmp(if_name, "FR:", 3) == 0) {
437                 pseudo_header->cosine.encap = COSINE_ENCAP_FR;
438         } else if (strncmp(if_name, "HDLC:", 5) == 0) {
439                 pseudo_header->cosine.encap = COSINE_ENCAP_HDLC;
440         } else if (strncmp(if_name, "PPP:", 4) == 0) {
441                 pseudo_header->cosine.encap = COSINE_ENCAP_PPP;
442         } else if (strncmp(if_name, "ETH:", 4) == 0) {
443                 pseudo_header->cosine.encap = COSINE_ENCAP_ETH;
444         } else {
445                 pseudo_header->cosine.encap = COSINE_ENCAP_UNKNOWN;
446         }
447         if (strncmp(direction, "l2-tx", 5) == 0) {
448                 pseudo_header->cosine.direction = COSINE_DIR_TX;
449         } else if (strncmp(direction, "l2-rx", 5) == 0) {
450                 pseudo_header->cosine.direction = COSINE_DIR_RX;
451         }
452         strncpy(pseudo_header->cosine.if_name, if_name,
453                 COSINE_MAX_IF_NAME_LEN - 1);
454         pseudo_header->cosine.pro = pro;
455         pseudo_header->cosine.off = off;
456         pseudo_header->cosine.pri = pri;
457         pseudo_header->cosine.rm = rm;
458         pseudo_header->cosine.err = error;
459
460         return pkt_len;
461 }
462
463 /* Converts ASCII hex dump to binary data. Returns the capture length.
464    If any error is encountered, -1 is returned. */
465 static int
466 parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err,
467     gchar **err_info)
468 {
469         gchar   line[COSINE_LINE_LENGTH];
470         int     i, hex_lines, n, caplen = 0;
471
472         /* Calculate the number of hex dump lines, each
473          * containing 16 bytes of data */
474         hex_lines = pkt_len / 16 + ((pkt_len % 16) ? 1 : 0);
475
476         for (i = 0; i < hex_lines; i++) {
477                 if (file_gets(line, COSINE_LINE_LENGTH, fh) == NULL) {
478                         *err = file_error(fh);
479                         if (*err == 0) {
480                                 *err = WTAP_ERR_SHORT_READ;
481                         }
482                         return -1;
483                 }
484                 if (empty_line(line)) {
485                         break;
486                 }
487                 if ((n = parse_single_hex_dump_line(line, buf, i*16)) == -1) {
488                         *err = WTAP_ERR_BAD_RECORD;
489                         *err_info = g_strdup("cosine: hex dump line doesn't have 16 numbers");
490                         return -1;
491                 }
492                 caplen += n;
493         }
494         return caplen;
495 }
496
497
498 /* Take a string representing one line from a hex dump and converts
499  * the text to binary data. We place the bytes in the buffer at the
500  * specified offset.
501  *
502  * Returns number of bytes successfully read, -1 if bad.  */
503 static int
504 parse_single_hex_dump_line(char* rec, guint8 *buf, guint byte_offset)
505 {
506         int num_items_scanned, i;
507         unsigned int bytes[16];
508
509         num_items_scanned = sscanf(rec, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
510                                &bytes[0], &bytes[1], &bytes[2], &bytes[3],
511                                &bytes[4], &bytes[5], &bytes[6], &bytes[7],
512                                &bytes[8], &bytes[9], &bytes[10], &bytes[11],
513                                &bytes[12], &bytes[13], &bytes[14], &bytes[15]);
514         if (num_items_scanned == 0)
515                 return -1;
516
517         if (num_items_scanned > 16)
518                 num_items_scanned = 16;
519
520         for (i=0; i<num_items_scanned; i++) {
521                 buf[byte_offset + i] = (guint8)bytes[i];
522         }
523
524         return num_items_scanned;
525 }