Update minimum version requirement for GTK to 2.12.
[obnox/wireshark/wip.git] / wiretap / catapult_dct2000.c
1 /* catapult_dct2000.c
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 #include "wtap-int.h"
32 #include "file_wrappers.h"
33 #include "buffer.h"
34
35 #include "catapult_dct2000.h"
36
37 #define MAX_FIRST_LINE_LENGTH      200
38 #define MAX_TIMESTAMP_LINE_LENGTH  100
39 #define MAX_LINE_LENGTH            65536
40 #define MAX_TIMESTAMP_LEN          32
41 #define MAX_SECONDS_CHARS          16
42 #define MAX_SUBSECOND_DECIMALS     4
43 #define MAX_CONTEXT_NAME           64
44 #define MAX_PROTOCOL_NAME          64
45 #define MAX_PORT_DIGITS            2
46 #define MAX_VARIANT_DIGITS         32
47 #define MAX_OUTHDR_NAME            256
48 #define AAL_HEADER_CHARS           12
49
50 /* TODO:
51    - support for FP over AAL0
52    - support for IuR interface FP
53    - support for x.25?
54 */
55
56 /* 's' or 'r' of a packet as read from .out file */
57 typedef enum packet_direction_t
58 {
59     sent,
60     received
61 } packet_direction_t;
62
63
64 /***********************************************************************/
65 /* For each line, store (in case we need to dump):                     */
66 /* - String before time field                                          */
67 /* - String beween time field and data (if NULL assume " l ")          */
68 typedef struct
69 {
70     gchar *before_time;
71     gchar *after_time;
72 } line_prefix_info_t;
73
74
75 /*******************************************************************/
76 /* Information stored external to a file (wtap) needed for reading and dumping */
77 typedef struct dct2000_file_externals
78 {
79     /* Remember the time at the start of capture */
80     time_t  start_secs;
81     guint32 start_usecs;
82
83     /*
84      * The following information is needed only for dumping.
85      *
86      * XXX - Wiretap is not *supposed* to require that a packet being
87      * dumped come from a file of the same type that you currently have
88      * open; this should be fixed.
89      */
90
91     /* Buffer to hold first line, including magic and format number */
92     gchar firstline[MAX_FIRST_LINE_LENGTH];
93     gint  firstline_length;
94
95     /* Buffer to hold second line with formatted file creation data/time */
96     gchar secondline[MAX_TIMESTAMP_LINE_LENGTH];
97     gint  secondline_length;
98
99     /* Hash table to store text prefix data part of displayed packets.
100        Records (file offset -> line_prefix_info_t)
101     */
102     GHashTable *packet_prefix_table;
103 } dct2000_file_externals_t;
104
105 /* 'Magic number' at start of Catapult DCT2000 .out files. */
106 static const gchar catapult_dct2000_magic[] = "Session Transcript";
107
108 /************************************************************/
109 /* Functions called from wiretap core                       */
110 static gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info,
111                                       gint64 *data_offset);
112 static gboolean catapult_dct2000_seek_read(wtap *wth, gint64 seek_off,
113                                            union wtap_pseudo_header *pseudo_header,
114                                            guint8 *pd, int length,
115                                            int *err, gchar **err_info);
116 static void catapult_dct2000_close(wtap *wth);
117
118 static gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
119                                       const union wtap_pseudo_header *pseudo_header,
120                                       const guint8 *pd, int *err);
121
122
123 /************************************************************/
124 /* Private helper functions                                 */
125 static gboolean read_new_line(FILE_T fh, gint64 *offset, gint *length,
126                               gchar *buf, size_t bufsize);
127 static gboolean parse_line(char *linebuff, gint line_length,
128                            gint *seconds, gint *useconds,
129                            long *before_time_offset, long *after_time_offset,
130                            long *data_offset,
131                            gint *data_chars,
132                            packet_direction_t *direction,
133                            int *encap, int *is_comment,
134                            gchar *aal_header_chars,
135                            gchar *context_name, guint8 *context_portp,
136                            gchar *protocol_name, gchar *variant_name,
137                            gchar *outhdr_name);
138 static int write_stub_header(guint8 *frame_buffer, char *timestamp_string,
139                              packet_direction_t direction, int encap,
140                              gchar *context_name, guint8 context_port,
141                              gchar *protocol_name, gchar *variant_name,
142                              gchar *outhdr_name);
143 static guint8 hex_from_char(gchar c);
144 static guint8 hex_byte_from_chars(gchar *c);
145 static gchar char_from_hex(guint8 hex);
146
147 static void set_pseudo_header_info(wtap *wth,
148                                    int pkt_encap,
149                                    gint64 file_offset,
150                                    union wtap_pseudo_header *pseudo_header,
151                                    packet_direction_t direction,
152                                    gchar *aal_header_chars);
153 static void set_aal_info(union wtap_pseudo_header *pseudo_header,
154                          packet_direction_t direction,
155                          gchar *aal_header_chars);
156 static void set_isdn_info(union wtap_pseudo_header *pseudo_header,
157                           packet_direction_t direction);
158 static void set_ppp_info(union wtap_pseudo_header *pseudo_header,
159                          packet_direction_t direction);
160
161 static gint packet_offset_equal(gconstpointer v, gconstpointer v2);
162 static guint packet_offset_hash_func(gconstpointer v);
163
164 static gboolean get_file_time_stamp(gchar *linebuff, time_t *secs, guint32 *usecs);
165 static gboolean free_line_prefix_info(gpointer key, gpointer value, gpointer user_data);
166
167
168
169 /********************************************/
170 /* Open file (for reading)                 */
171 /********************************************/
172 int catapult_dct2000_open(wtap *wth, int *err, gchar **err_info _U_)
173 {
174     gint64  offset = 0;
175     time_t  timestamp;
176     guint32 usecs;
177     gint firstline_length = 0;
178     dct2000_file_externals_t *file_externals;
179     static gchar linebuff[MAX_LINE_LENGTH];
180
181     /* Clear errno before reading from the file */
182     errno = 0;
183
184
185     /********************************************************************/
186     /* First line needs to contain at least as many characters as magic */
187
188     read_new_line(wth->fh, &offset, &firstline_length, linebuff,
189                   sizeof linebuff);
190     if (((size_t)firstline_length < strlen(catapult_dct2000_magic)) ||
191         firstline_length >= MAX_FIRST_LINE_LENGTH) {
192
193         return 0;
194     }
195
196     /* This file is not for us if it doesn't match our signature */
197     if (memcmp(catapult_dct2000_magic, linebuff, strlen(catapult_dct2000_magic)) != 0) {
198         return 0;
199     }
200
201
202     /*********************************************************************/
203     /* Need entry in file_externals table                                */
204
205     /* Allocate a new file_externals structure for this file */
206     file_externals = g_malloc(sizeof(dct2000_file_externals_t));
207     memset((void*)file_externals, '\0', sizeof(dct2000_file_externals_t));
208
209     /* Copy this first line into buffer so could write out later */
210     g_strlcpy(file_externals->firstline, linebuff, firstline_length+1);
211     file_externals->firstline_length = firstline_length;
212
213
214     /***********************************************************/
215     /* Second line contains file timestamp                     */
216     /* Store this offset in in file_externals                  */
217
218     read_new_line(wth->fh, &offset, &(file_externals->secondline_length),
219                   linebuff, sizeof linebuff);
220     if ((file_externals->secondline_length >= MAX_TIMESTAMP_LINE_LENGTH) ||
221         (!get_file_time_stamp(linebuff, &timestamp, &usecs))) {
222
223         /* Give up if file time line wasn't valid */
224         g_free(file_externals);
225         return 0;
226     }
227
228     /* Fill in timestamp */
229     file_externals->start_secs = timestamp;
230     file_externals->start_usecs = usecs;
231
232     /* Copy this second line into buffer so could write out later */
233     g_strlcpy(file_externals->secondline, linebuff, file_externals->secondline_length+1);
234
235
236     /************************************************************/
237     /* File is for us. Fill in details so packets can be read   */
238
239     /* Set our file type */
240     wth->file_type = WTAP_FILE_CATAPULT_DCT2000;
241
242     /* Use our own encapsulation to send all packets to our stub dissector */
243     wth->file_encap = WTAP_ENCAP_CATAPULT_DCT2000;
244
245     /* Callbacks for reading operations */
246     wth->subtype_read = catapult_dct2000_read;
247     wth->subtype_seek_read = catapult_dct2000_seek_read;
248     wth->subtype_close = catapult_dct2000_close;
249
250     /* Choose microseconds (have 4 decimal places...) */
251     wth->tsprecision = WTAP_FILE_TSPREC_USEC;
252
253
254     /***************************************************************/
255     /* Initialise packet_prefix_table (index is offset into file)  */
256     file_externals->packet_prefix_table =
257         g_hash_table_new(packet_offset_hash_func, packet_offset_equal);
258
259     /* Set this wtap to point to the file_externals */
260     wth->priv = (void*)file_externals;
261
262     *err = errno;
263     return 1;
264 }
265
266
267 /**************************************************/
268 /* Read packet function.                          */
269 /* Look for and read the next usable packet       */
270 /* - return TRUE and details if found             */
271 /**************************************************/
272 gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
273                                gint64 *data_offset)
274 {
275     gint64 offset = wth->data_offset;
276     long dollar_offset, before_time_offset, after_time_offset;
277     packet_direction_t direction;
278     int encap;
279
280     /* Get wtap external structure for this wtap */
281     dct2000_file_externals_t *file_externals =
282         (dct2000_file_externals_t*)wth->priv;
283
284     /* There *has* to be an entry for this wth */
285     if (!file_externals) {
286         return FALSE;
287     }
288
289     /* Search for a line containing a usable packet */
290     while (1) {
291         int line_length, seconds, useconds, data_chars;
292         int is_comment = FALSE;
293         gint64 this_offset = offset;
294         static gchar linebuff[MAX_LINE_LENGTH+1];
295         gchar aal_header_chars[AAL_HEADER_CHARS];
296         gchar context_name[MAX_CONTEXT_NAME];
297         guint8 context_port;
298         gchar protocol_name[MAX_PROTOCOL_NAME+1];
299         gchar variant_name[MAX_VARIANT_DIGITS+1];
300         gchar outhdr_name[MAX_OUTHDR_NAME+1];
301
302         /* Are looking for first packet after 2nd line */
303         if (wth->data_offset == 0) {
304             this_offset += (file_externals->firstline_length+1+
305                             file_externals->secondline_length+1);
306         }
307
308         /* Clear errno before reading from the file */
309         errno = 0;
310
311         /* Read a new line from file into linebuff */
312         if (read_new_line(wth->fh, &offset, &line_length, linebuff,
313                           sizeof linebuff) == FALSE) {
314             /* Get out if no more lines can be read */
315             break;
316         }
317
318         /* Try to parse the line as a frame record */
319         if (parse_line(linebuff, line_length, &seconds, &useconds,
320                        &before_time_offset, &after_time_offset,
321                        &dollar_offset,
322                        &data_chars, &direction, &encap, &is_comment,
323                        aal_header_chars,
324                        context_name, &context_port,
325                        protocol_name, variant_name, outhdr_name)) {
326             guint8 *frame_buffer;
327             int n;
328             int stub_offset = 0;
329             line_prefix_info_t *line_prefix_info;
330             char timestamp_string[MAX_TIMESTAMP_LEN+1];
331             gint64 *pkey = NULL;
332
333             g_snprintf(timestamp_string, MAX_TIMESTAMP_LEN, "%d.%04d", seconds, useconds/100);
334
335             /* All packets go to Catapult DCT2000 stub dissector */
336             wth->phdr.pkt_encap = WTAP_ENCAP_CATAPULT_DCT2000;
337
338             /* Set data_offset to the beginning of the line we're returning.
339                This will be the seek_off parameter when this frame is re-read.
340             */
341             *data_offset = this_offset;
342
343             /* This is the position in the file where the next _read() will be called from */
344             wth->data_offset = this_offset + line_length + 1;
345
346             /* Fill in timestamp (capture base + packet offset) */
347             wth->phdr.ts.secs = file_externals->start_secs + seconds;
348             if ((file_externals->start_usecs + useconds) >= 1000000) {
349                 wth->phdr.ts.secs++;
350             }
351             wth->phdr.ts.nsecs =
352                 ((file_externals->start_usecs + useconds) % 1000000) *1000;
353
354             /* Get buffer pointer ready */
355             buffer_assure_space(wth->frame_buffer,
356                                 strlen(context_name)+1 +     /* Context name */
357                                 1 +                          /* port */
358                                 strlen(timestamp_string)+1 + /* timestamp */
359                                 strlen(variant_name)+1 +     /* variant */
360                                 strlen(outhdr_name)+1 +      /* outhdr */
361                                 strlen(protocol_name)+1 +    /* Protocol name */
362                                 1 +                          /* direction */
363                                 1 +                          /* encap */
364                                 (is_comment ? data_chars : (data_chars/2)));
365             frame_buffer = buffer_start_ptr(wth->frame_buffer);
366
367
368             /*********************/
369             /* Write stub header */
370             stub_offset = write_stub_header(frame_buffer, timestamp_string,
371                                             direction, encap, context_name,
372                                             context_port,
373                                             protocol_name, variant_name,
374                                             outhdr_name);
375
376             /* Binary data length is half bytestring length + stub header */
377             wth->phdr.len = stub_offset + (is_comment ? data_chars : (data_chars/2));
378             wth->phdr.caplen = stub_offset + (is_comment ? data_chars : (data_chars/2));
379
380
381             if (!is_comment) {
382                 /****************************************************/
383                 /* Copy data into buffer, converting from ascii hex */
384                 for (n=0; n <= data_chars; n+=2) {
385                     frame_buffer[stub_offset + n/2] =
386                         hex_byte_from_chars(linebuff+dollar_offset+n);
387                 }
388             }
389             else {
390                 /***********************************************************/
391                 /* Copy packet data into buffer, just copying ascii chars  */
392                 for (n=0; n <= data_chars; n++) {
393                     frame_buffer[stub_offset + n] = linebuff[dollar_offset+n];
394                 }
395             }
396
397             /* Store the packet prefix in the hash table */
398             line_prefix_info = g_malloc(sizeof(line_prefix_info_t));
399
400             /* Create and use buffer for contents before time */
401             line_prefix_info->before_time = g_malloc(before_time_offset+2);
402             g_strlcpy(line_prefix_info->before_time, linebuff, before_time_offset+1);
403             line_prefix_info->before_time[before_time_offset+1] = '\0';
404
405             /* Create and use buffer for contents before time.
406                Do this only if it doesn't correspond to " l ", which is by far the most
407                common case. */
408             if (((size_t)(dollar_offset - after_time_offset -1) == strlen(" l ")) &&
409                 (strncmp(linebuff+after_time_offset, " l ", strlen(" l ")) == 0)) {
410
411                 line_prefix_info->after_time = NULL;
412             }
413             else {
414                 /* Allocate & write buffer for line between timestamp and data */
415                 line_prefix_info->after_time = g_malloc(dollar_offset - after_time_offset);
416                 g_strlcpy(line_prefix_info->after_time, linebuff+after_time_offset,
417                           dollar_offset - after_time_offset);
418                 line_prefix_info->after_time[dollar_offset - after_time_offset-1] = '\0';
419             }
420
421             /* Add packet entry into table */
422             pkey = g_malloc(sizeof(*pkey));
423             *pkey = this_offset;
424             g_hash_table_insert(file_externals->packet_prefix_table, pkey, line_prefix_info);
425
426             /* Set pseudo-header if necessary */
427             set_pseudo_header_info(wth, encap, this_offset, &wth->pseudo_header,
428                                    direction, aal_header_chars);
429
430             /* OK, we have packet details to return */
431             *err = errno;
432             return TRUE;
433         }
434     }
435
436     /* No packet details to return... */
437     *err = errno;
438     return FALSE;
439 }
440
441
442 /**************************************************/
443 /* Read & seek function.                          */
444 /**************************************************/
445 static gboolean
446 catapult_dct2000_seek_read(wtap *wth, gint64 seek_off,
447                            union wtap_pseudo_header *pseudo_header, guint8 *pd,
448                            int length, int *err, gchar **err_info)
449 {
450     gint64 offset = wth->data_offset;
451     long dollar_offset, before_time_offset, after_time_offset;
452     static gchar linebuff[MAX_LINE_LENGTH+1];
453     gchar aal_header_chars[AAL_HEADER_CHARS];
454     gchar context_name[MAX_CONTEXT_NAME];
455     guint8 context_port;
456     gchar protocol_name[MAX_PROTOCOL_NAME+1];
457     gchar variant_name[MAX_VARIANT_DIGITS+1];
458     gchar outhdr_name[MAX_OUTHDR_NAME+1];
459     int  is_comment = FALSE;
460     packet_direction_t direction;
461     int encap;
462     int seconds, useconds, data_chars;
463
464     /* Reset errno */
465     *err = errno = 0;
466
467     /* Seek to beginning of packet */
468     if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
469         return FALSE;
470     }
471
472     /* Re-read whole line (this really should succeed) */
473     if (read_new_line(wth->random_fh, &offset, &length, linebuff,
474                       sizeof linebuff) == FALSE) {
475         return FALSE;
476     }
477
478     /* Try to parse this line again (should succeed as re-reading...) */
479     if (parse_line(linebuff, length, &seconds, &useconds,
480                    &before_time_offset, &after_time_offset,
481                    &dollar_offset,
482                    &data_chars, &direction, &encap, &is_comment,
483                    aal_header_chars,
484                    context_name, &context_port,
485                    protocol_name, variant_name, outhdr_name)) {
486         int n;
487         int stub_offset = 0;
488         char timestamp_string[MAX_TIMESTAMP_LEN+1];
489         g_snprintf(timestamp_string, MAX_TIMESTAMP_LEN, "%d.%04d", seconds, useconds/100);
490
491         /* Make sure all packets go to catapult dct2000 dissector */
492         wth->phdr.pkt_encap = WTAP_ENCAP_CATAPULT_DCT2000;
493
494
495         /*********************/
496         /* Write stub header */
497         stub_offset = write_stub_header(pd, timestamp_string,
498                                         direction, encap, context_name,
499                                         context_port,
500                                         protocol_name, variant_name,
501                                         outhdr_name);
502
503
504         if (!is_comment) {
505             /***********************************************************/
506             /* Copy packet data into buffer, converting from ascii hex */
507             for (n=0; n <= data_chars; n+=2) {
508                 pd[stub_offset + n/2] = hex_byte_from_chars(linebuff+dollar_offset+n);
509             }
510         }
511         else {
512             /***********************************************************/
513             /* Copy packet data into buffer, just copying ascii chars  */
514             for (n=0; n <= data_chars; n++) {
515                 pd[stub_offset+n] = linebuff[dollar_offset+n];
516             }
517         }
518
519         /* Set packet pseudo-header if necessary */
520         set_pseudo_header_info(wth, encap, seek_off, pseudo_header, direction,
521                                aal_header_chars);
522
523         *err = errno = 0;
524         return TRUE;
525     }
526
527     /* If get here, must have failed */
528     *err = errno;
529     *err_info = g_strdup_printf("catapult dct2000: seek_read failed to read/parse "
530                                 "line at position %" G_GINT64_MODIFIER "d",
531                                 seek_off);
532     return FALSE;
533 }
534
535
536 /***************************************************************************/
537 /* Free dct2000-specific capture info from file that was open for reading  */
538 /***************************************************************************/
539 void catapult_dct2000_close(wtap *wth)
540 {
541     /* Get externals for this file */
542     dct2000_file_externals_t *file_externals =
543         (dct2000_file_externals_t*)wth->priv;
544
545     /* The entry *has* to be found */
546     if (!file_externals) {
547         return;
548     }
549
550     /* Free up its line prefix values */
551     g_hash_table_foreach_remove(file_externals->packet_prefix_table,
552                                 free_line_prefix_info, NULL);
553     /* Free up its line prefix table */
554     g_hash_table_destroy(file_externals->packet_prefix_table);
555 }
556
557
558
559
560 /***************************/
561 /* Dump functions          */
562 /***************************/
563
564 typedef struct {
565     gboolean           first_packet_written;
566     struct wtap_nstime start_time;
567 } dct2000_dump_t;
568
569 /*****************************************************/
570 /* The file that we are writing to has been opened.  */
571 /* Set other dump callbacks.                         */
572 /*****************************************************/
573 gboolean catapult_dct2000_dump_open(wtap_dumper *wdh, int *err _U_)
574 {
575     /* Fill in other dump callbacks */
576     wdh->subtype_write = catapult_dct2000_dump;
577
578     return TRUE;
579 }
580
581 /*********************************************************/
582 /* Respond to queries about which encap types we support */
583 /* writing to.                                           */
584 /*********************************************************/
585 int catapult_dct2000_dump_can_write_encap(int encap)
586 {
587     switch (encap) {
588         case WTAP_ENCAP_CATAPULT_DCT2000:
589             /* We support this */
590             return 0;
591
592         default:
593             /* But don't write to any other formats... */
594             return WTAP_ERR_UNSUPPORTED_ENCAP;
595     }
596 }
597
598
599 /*****************************************/
600 /* Write a single packet out to the file */
601 /*****************************************/
602
603 gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
604                                const union wtap_pseudo_header *pseudo_header,
605                                const guint8 *pd, int *err)
606 {
607     guint32 n;
608     line_prefix_info_t *prefix = NULL;
609     gchar time_string[16];
610     gboolean is_comment;
611     dct2000_dump_t *dct2000;
612
613     /******************************************************/
614     /* Get the file_externals structure for this file */
615     /* Find wtap external structure for this wtap */
616     dct2000_file_externals_t *file_externals =
617         (dct2000_file_externals_t*)pseudo_header->dct2000.wth->priv;
618
619     dct2000 = (dct2000_dump_t *)wdh->priv;
620     if (dct2000 == NULL) {
621
622         /* Write out saved first line */
623         if (!wtap_dump_file_write(wdh, file_externals->firstline,
624                                   file_externals->firstline_length, err)) {
625             return FALSE;
626         }
627         if (!wtap_dump_file_write(wdh, "\n", 1, err)) {
628             return FALSE;
629         }
630
631         /* Also write out saved second line with timestamp corresponding to the
632            opening time of the log.
633         */
634         if (!wtap_dump_file_write(wdh, file_externals->secondline,
635                                   file_externals->secondline_length, err)) {
636             return FALSE;
637         }
638         if (!wtap_dump_file_write(wdh, "\n", 1, err)) {
639             return FALSE;
640         }
641
642         /* Allocate the dct2000-specific dump structure */
643         dct2000 = (dct2000_dump_t *)g_malloc(sizeof(dct2000_dump_t));
644         wdh->priv = (void *)dct2000;
645
646         /* Copy time of beginning of file */
647         dct2000->start_time.secs = file_externals->start_secs;
648         dct2000->start_time.nsecs =
649             (file_externals->start_usecs * 1000);
650
651         /* Set flag do don't write header out again */
652         dct2000->first_packet_written = TRUE;
653     }
654
655
656     /******************************************************************/
657     /* Write out this packet's prefix, including calculated timestamp */
658
659     /* Look up line data prefix using stored offset */
660     prefix = (line_prefix_info_t*)g_hash_table_lookup(file_externals->packet_prefix_table,
661                                                       (const void*)&(pseudo_header->dct2000.seek_off));
662
663     /* Write out text before timestamp */
664     if (!wtap_dump_file_write(wdh, prefix->before_time,
665                               strlen(prefix->before_time), err)) {
666         return FALSE;
667     }
668
669     /* Can infer from prefix if this is a comment (whose payload is displayed differently) */
670     is_comment = (strstr(prefix->before_time, "/////") != NULL);
671
672     /* Calculate time of this packet to write, relative to start of dump */
673     if (phdr->ts.nsecs >= dct2000->start_time.nsecs) {
674         g_snprintf(time_string, 16, "%ld.%04d",
675                   (long)(phdr->ts.secs - dct2000->start_time.secs),
676                   (phdr->ts.nsecs - dct2000->start_time.nsecs) / 100000);
677     }
678     else {
679         g_snprintf(time_string, 16, "%ld.%04u",
680                   (long)(phdr->ts.secs - dct2000->start_time.secs-1),
681                   ((1000000000 + (phdr->ts.nsecs / 100000)) - (dct2000->start_time.nsecs / 100000)) % 10000);
682     }
683
684     /* Write out the calculated timestamp */
685     if (!wtap_dump_file_write(wdh, time_string, strlen(time_string), err)) {
686         return FALSE;
687     }
688
689     /* Write out text between timestamp and start of hex data */
690     if (prefix->after_time == NULL) {
691         if (!wtap_dump_file_write(wdh, " l ", strlen(" l "), err)) {
692             return FALSE;
693         }
694     }
695     else {
696         if (!wtap_dump_file_write(wdh, prefix->after_time,
697                                   strlen(prefix->after_time), err)) {
698             return FALSE;
699         }
700     }
701
702
703     /****************************************************************/
704     /* Need to skip stub header at start of pd before we reach data */
705
706     /* Context name */
707     for (n=0; pd[n] != '\0'; n++);
708     n++;
709
710     /* Context port number */
711     n++;
712
713     /* Timestamp */
714     for (; pd[n] != '\0'; n++);
715     n++;
716
717     /* Protocol name */
718     for (; pd[n] != '\0'; n++);
719     n++;
720
721     /* Variant number (as string) */
722     for (; pd[n] != '\0'; n++);
723     n++;
724
725     /* Outhdr (as string) */
726     for (; pd[n] != '\0'; n++);
727     n++;
728
729     /* Direction & encap */
730     n += 2;
731
732
733     /**************************************/
734     /* Remainder is encapsulated protocol */
735     if (!wtap_dump_file_write(wdh, "$", 1, err)) {
736         return FALSE;
737     }
738
739     if (!is_comment) {
740         /* Each binary byte is written out as 2 hex string chars */ 
741         for (; n < phdr->len; n++) {
742             gchar c[2];
743             c[0] = char_from_hex((guint8)(pd[n] >> 4));
744             c[1] = char_from_hex((guint8)(pd[n] & 0x0f));
745
746             /* Write both hex chars of byte together */
747             if (!wtap_dump_file_write(wdh, c, 2, err)) {
748                 return FALSE;
749             }
750         }
751     }
752     else {
753         for (; n < phdr->len; n++) {
754             char c[1];
755             c[0] = pd[n];
756
757             /* Write both hex chars of byte together */
758             if (!wtap_dump_file_write(wdh, c, 1, err)) {
759                 return FALSE;
760             }
761         }
762     }
763
764     /* End the line */
765     if (!wtap_dump_file_write(wdh, "\n", 1, err)) {
766         return FALSE;
767     }
768
769     return TRUE;
770 }
771
772
773 /****************************/
774 /* Private helper functions */
775 /****************************/
776
777 /**********************************************************************/
778 /* Read a new line from the file, starting at offset.                 */
779 /* - writes data to its argument linebuff                             */
780 /* - on return 'offset' will point to the next position to read from  */
781 /* - return TRUE if this read is successful                           */
782 /**********************************************************************/
783 gboolean read_new_line(FILE_T fh, gint64 *offset, gint *length,
784                        gchar *linebuff, size_t linebuffsize)
785 {
786     /* Read in a line */
787     gint64 pos_before = file_tell(fh);
788     char *result = file_gets(linebuff, (int)linebuffsize - 1, fh);
789     if (result == NULL) {
790         /* No characters found, or error */
791         return FALSE;
792     }
793
794     /* Set length (avoiding strlen()) and offset.. */
795     *length = (gint)(file_tell(fh) - pos_before);
796     *offset = *offset + *length;
797
798     /* ...but don't want to include newline in line length */
799     if (linebuff[*length-1] == '\n') {
800         linebuff[*length-1] = '\0';
801         *length = *length - 1;
802     }
803
804     return TRUE;
805 }
806
807
808 /**********************************************************************/
809 /* Parse a line from buffer, by identifying:                          */
810 /* - context, port and direction of packet                            */
811 /* - timestamp                                                        */
812 /* - data position and length                                         */
813 /* Return TRUE if this packet looks valid and can be displayed        */
814 /**********************************************************************/
815 static gboolean parse_line(gchar *linebuff, gint line_length,
816                            gint *seconds, gint *useconds,
817                            long *before_time_offset, long *after_time_offset,
818                            long *data_offset, gint *data_chars,
819                            packet_direction_t *direction,
820                            int *encap, int *is_comment,
821                            gchar *aal_header_chars,
822                            gchar *context_name, guint8 *context_portp,
823                            gchar *protocol_name, gchar *variant_name,
824                            gchar *outhdr_name)
825 {
826     int  n = 0;
827     int  port_digits;
828     char port_number_string[MAX_PORT_DIGITS+1];
829     int  variant_digits = 0;
830     int  variant = 1;
831     int  protocol_chars = 0;
832     int  outhdr_chars = 0;
833
834     char seconds_buff[MAX_SECONDS_CHARS+1];
835     int  seconds_chars;
836     char subsecond_decimals_buff[MAX_SUBSECOND_DECIMALS+1];
837     int  subsecond_decimals_chars;
838     int  skip_first_byte = FALSE;
839     gboolean atm_header_present = FALSE;
840
841     *is_comment = FALSE;
842
843     /* Read context name until find '.' */
844     for (n=0; (linebuff[n] != '.') && (n < MAX_CONTEXT_NAME) && (n+1 < line_length); n++) {
845         if (linebuff[n] == '/') {
846             context_name[n] = '\0';
847
848             /* If not a comment (/////), not a valid line */
849             if (strncmp(linebuff+n, "/////", 5) != 0) {
850                 return FALSE;
851             }
852
853             /* There is no variant, outhdr, etc.  Set protocol to be a comment */
854             g_snprintf(protocol_name, MAX_PROTOCOL_NAME, "comment");
855             *is_comment = TRUE;
856             break;
857         }
858         if (!isalnum((guchar)linebuff[n]) && (linebuff[n] != '_') && (linebuff[n] != '-')) {
859             return FALSE;
860         }
861         context_name[n] = linebuff[n];
862     }
863     if (n == MAX_CONTEXT_NAME || (n+1 >= line_length)) {
864         return FALSE;
865     }
866
867     /* Reset strings (that won't be set by comments) */
868     variant_name[0] = '\0';
869     outhdr_name[0] = '\0';
870     port_number_string[0] = '\0';
871
872     if (!(*is_comment)) {
873         /* '.' must follow context name */
874         if (linebuff[n] != '.') {
875             return FALSE;
876         }
877         context_name[n] = '\0';
878         /* Skip it */
879         n++;
880
881         /* Now read port number */
882         for (port_digits = 0;
883              (linebuff[n] != '/') && (port_digits <= MAX_PORT_DIGITS) && (n+1 < line_length);
884              n++, port_digits++) {
885
886             if (!isdigit((guchar)linebuff[n])) {
887                 return FALSE;
888             }
889             port_number_string[port_digits] = linebuff[n];
890         }
891         if (port_digits > MAX_PORT_DIGITS || (n+1 >= line_length)) {
892             return FALSE;
893         }
894
895         /* Slash char must follow port number */
896         if (linebuff[n] != '/')
897         {
898             return FALSE;
899         }
900         port_number_string[port_digits] = '\0';
901         *context_portp = atoi(port_number_string);
902         /* Skip it */
903         n++;
904
905         /* Now for the protocol name */
906         for (protocol_chars = 0;
907              (linebuff[n] != '/') && (protocol_chars < MAX_PROTOCOL_NAME) && (n < line_length);
908              n++, protocol_chars++) {
909
910             if (!isalnum((guchar)linebuff[n]) && linebuff[n] != '_') {
911                 return FALSE;
912             }
913             protocol_name[protocol_chars] = linebuff[n];
914         }
915         if (protocol_chars == MAX_PROTOCOL_NAME || n >= line_length) {
916             /* If doesn't fit, fail rather than truncate */
917             return FALSE;
918         }
919         protocol_name[protocol_chars] = '\0';
920
921         /* Slash char must follow protocol name */
922         if (linebuff[n] != '/') {
923             return FALSE;
924         }
925         /* Skip it */
926         n++;
927
928
929         /* Following the / is the variant number.  No digits indicate 1 */
930         for (variant_digits = 0;
931              (isdigit((guchar)linebuff[n])) && (variant_digits <= MAX_VARIANT_DIGITS) && (n+1 < line_length);
932              n++, variant_digits++) {
933
934             if (!isdigit((guchar)linebuff[n])) {
935                 return FALSE;
936             }
937             variant_name[variant_digits] = linebuff[n];
938         }
939         if (variant_digits > MAX_VARIANT_DIGITS || (n+1 >= line_length)) {
940             return FALSE;
941         }
942         if (variant_digits > 0) {
943             variant_name[variant_digits] = '\0';
944             variant = atoi(variant_name);
945         }
946         else {
947             g_strlcpy(variant_name, "1", MAX_VARIANT_DIGITS+1);
948         }
949
950
951         /* Outheader values may follow */
952         outhdr_name[0] = '\0';
953         if (linebuff[n] == ',') {
954             /* Skip , */
955             n++;
956
957             for (outhdr_chars = 0;
958                  (isdigit((guchar)linebuff[n]) || linebuff[n] == ',') &&
959                  (outhdr_chars <= MAX_OUTHDR_NAME) && (n+1 < line_length);
960                  n++, outhdr_chars++) {
961
962                 if (!isdigit((guchar)linebuff[n]) && (linebuff[n] != ',')) {
963                     return FALSE;
964                 }
965                 outhdr_name[outhdr_chars] = linebuff[n];
966             }
967             if (outhdr_chars > MAX_OUTHDR_NAME || (n+1 >= line_length)) {
968                 return FALSE;
969             }
970             /* Terminate (possibly empty) string */
971             outhdr_name[outhdr_chars] = '\0';
972         }
973     }
974
975
976     /******************************************************************/
977     /* Now check whether we know how to use a packet of this protocol */
978
979     if ((strcmp(protocol_name, "ip") == 0) ||
980         (strcmp(protocol_name, "sctp") == 0) ||
981         (strcmp(protocol_name, "gre") == 0) ||
982         (strcmp(protocol_name, "mipv6") == 0) ||
983         (strcmp(protocol_name, "igmp") == 0)) {
984
985         *encap = WTAP_ENCAP_RAW_IP;
986     }
987     else
988
989     /* FP may be carried over ATM, which has separate atm header to parse */
990     if ((strcmp(protocol_name, "fp") == 0) ||
991         (strcmp(protocol_name, "fp_r4") == 0) ||
992         (strcmp(protocol_name, "fp_r5") == 0) ||
993         (strcmp(protocol_name, "fp_r6") == 0) ||
994         (strcmp(protocol_name, "fp_r7") == 0) ||
995         (strcmp(protocol_name, "fp_r8") == 0)) {
996
997         if ((variant > 256) && (variant % 256 == 3)) {
998             /* FP over udp is contained in IPPrim... */
999             *encap = 0;
1000         }
1001         else {
1002             /* FP over AAL0 or AAL2 */
1003             *encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
1004             atm_header_present = TRUE;
1005         }
1006     }
1007     else if (strcmp(protocol_name, "fpiur_r5") == 0) {
1008         /* FP (IuR) over AAL2 */
1009         *encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
1010         atm_header_present = TRUE;
1011     }
1012
1013     else
1014     if (strcmp(protocol_name, "ppp") == 0) {
1015         *encap = WTAP_ENCAP_PPP;
1016     }
1017     else
1018     if (strcmp(protocol_name, "isdn_l3") == 0) {
1019        /* TODO: find out what this byte means... */
1020         skip_first_byte = TRUE;
1021         *encap = WTAP_ENCAP_ISDN;
1022     }
1023     else
1024     if (strcmp(protocol_name, "isdn_l2") == 0) {
1025         *encap = WTAP_ENCAP_ISDN;
1026     }
1027     else
1028     if (strcmp(protocol_name, "ethernet") == 0) {
1029         *encap = WTAP_ENCAP_ETHERNET;
1030     }
1031     else
1032     if ((strcmp(protocol_name, "saalnni_sscop") == 0) ||
1033         (strcmp(protocol_name, "saaluni_sscop") == 0)) {
1034
1035         *encap = DCT2000_ENCAP_SSCOP;
1036     }
1037     else
1038     if (strcmp(protocol_name, "frelay_l2") == 0) {
1039         *encap = WTAP_ENCAP_FRELAY;
1040     }
1041     else
1042     if (strcmp(protocol_name, "ss7_mtp2") == 0) {
1043         *encap = DCT2000_ENCAP_MTP2;
1044     }
1045     else
1046     if ((strcmp(protocol_name, "nbap") == 0) ||
1047         (strcmp(protocol_name, "nbap_r4") == 0) ||
1048         (strncmp(protocol_name, "nbap_sscfuni", strlen("nbap_sscfuni")) == 0)) {
1049
1050         /* The entire message in these cases is nbap, so use an encap value. */
1051         *encap = DCT2000_ENCAP_NBAP;
1052     }
1053     else {
1054         /* Not a supported board port protocol/encap, but can show as raw data or
1055            in some cases find protocol embedded inside primitive */
1056         *encap = DCT2000_ENCAP_UNHANDLED;
1057     }
1058
1059
1060     /* Find separate ATM header if necessary */
1061     if (atm_header_present) {
1062         int header_chars_seen = 0;
1063
1064         /* Scan ahead to the next $ */
1065         for (; (linebuff[n] != '$') && (n+1 < line_length); n++);
1066         /* Skip it */
1067         n++;
1068         if (n+1 >= line_length) {
1069             return FALSE;
1070         }
1071
1072         /* Read consecutive hex chars into atm header buffer */
1073         for (;
1074              ((linebuff[n] >= '0') && (linebuff[n] <= '?') &&
1075               (n < line_length) &&
1076               (header_chars_seen < AAL_HEADER_CHARS));
1077              n++, header_chars_seen++) {
1078
1079             aal_header_chars[header_chars_seen] = linebuff[n];
1080             /* Next 6 characters after '9' are mapped to a->f */
1081             if (!isdigit((guchar)linebuff[n])) {
1082                 aal_header_chars[header_chars_seen] = 'a' + (linebuff[n] - '9') -1;
1083             }
1084         }
1085
1086         if (header_chars_seen != AAL_HEADER_CHARS || n >= line_length) {
1087             return FALSE;
1088         }
1089     }
1090
1091     /* Skip next '/' */
1092     n++;
1093
1094     /* If there is a number, skip all info to next '/'.
1095        TODO: for IP encapsulation, should store PDCP ueid, drb in pseudo info
1096        and display dct2000 dissector... */
1097     if (isdigit(linebuff[n])) {
1098         while ((n+1 < line_length) && linebuff[n] != '/') {
1099             n++;
1100         }
1101     }
1102
1103     /* Skip '/' */
1104     while ((n+1 < line_length) && linebuff[n] == '/') {
1105         n++;
1106     }
1107
1108     /* Skip a space that may happen here */
1109     if ((n+1 < line_length) && linebuff[n] == ' ') {
1110         n++;
1111     }
1112
1113     /* Next character gives direction of message (must be 's' or 'r') */
1114     if (!(*is_comment)) {
1115         if (linebuff[n] == 's') {
1116             *direction = sent;
1117         }
1118         else
1119         if (linebuff[n] == 'r') {
1120             *direction = received;
1121         }
1122         else {
1123             return FALSE;
1124         }
1125         /* Skip it */
1126         n++;
1127     }
1128     else {
1129         *direction = sent;
1130     }
1131
1132
1133     /*********************************************************************/
1134     /* Find and read the timestamp                                       */
1135
1136     /* Now scan to the next digit, which should be the start of the timestamp */
1137     /* This will involve skipping " tm "                                      */
1138
1139     for (; ((linebuff[n] != 't') || (linebuff[n+1] != 'm')) && (n+1 < line_length); n++);
1140     if (n >= line_length) {
1141         return FALSE;
1142     }
1143
1144     for (; !isdigit((guchar)linebuff[n]) && (n < line_length); n++);
1145     if (n >= line_length) {
1146         return FALSE;
1147     }
1148
1149     *before_time_offset = n;
1150
1151     /* Seconds */
1152     for (seconds_chars = 0;
1153          (linebuff[n] != '.') &&
1154          (seconds_chars <= MAX_SECONDS_CHARS) &&
1155          (n < line_length);
1156          n++, seconds_chars++) {
1157
1158         if (!isdigit((guchar)linebuff[n])) {
1159             /* Found a non-digit before decimal point. Fail */
1160             return FALSE;
1161         }
1162         seconds_buff[seconds_chars] = linebuff[n];
1163     }
1164     if (seconds_chars > MAX_SECONDS_CHARS || n >= line_length) {
1165         /* Didn't fit in buffer.  Fail rather than use truncated */
1166         return FALSE;
1167     }
1168
1169     /* Convert found value into number */
1170     seconds_buff[seconds_chars] = '\0';
1171     *seconds = atoi(seconds_buff);
1172
1173     /* The decimal point must follow the last of the seconds digits */
1174     if (linebuff[n] != '.') {
1175         return FALSE;
1176     }
1177     /* Skip it */
1178     n++;
1179
1180     /* Subsecond decimal digits (expect 4-digit accuracy) */
1181     for (subsecond_decimals_chars = 0;
1182          (linebuff[n] != ' ') &&
1183          (subsecond_decimals_chars <= MAX_SUBSECOND_DECIMALS) &&
1184          (n < line_length);
1185          n++, subsecond_decimals_chars++) {
1186
1187         if (!isdigit((guchar)linebuff[n])) {
1188             return FALSE;
1189         }
1190         subsecond_decimals_buff[subsecond_decimals_chars] = linebuff[n];
1191     }
1192     if (subsecond_decimals_chars > MAX_SUBSECOND_DECIMALS || n >= line_length) {
1193         /* More numbers than expected - give up */
1194         return FALSE;
1195     }
1196     /* Convert found value into microseconds */
1197     subsecond_decimals_buff[subsecond_decimals_chars] = '\0';
1198     *useconds = atoi(subsecond_decimals_buff) * 100;
1199
1200     /* Space character must follow end of timestamp */
1201     if (linebuff[n] != ' ') {
1202         return FALSE;
1203     }
1204
1205     *after_time_offset = n;
1206
1207     /* Now skip ahead to find start of data (marked by '$') */
1208     /* Want to avoid matching with normal sprint command output at the moment... */
1209     for (; (linebuff[n] != '$') && (linebuff[n] != '\'') && (n+1 < line_length); n++);
1210     if ((linebuff[n] == '\'') || (n+1 >= line_length)) {
1211         return FALSE;
1212     }
1213     /* Skip it */
1214     n++;
1215
1216     /* Set offset to data start within line */
1217     *data_offset = n;
1218
1219     /* Set number of chars that comprise the hex string protocol data */
1220     *data_chars = line_length - n;
1221
1222     /* May need to skip first byte (2 hex string chars) */
1223     if (skip_first_byte) {
1224         *data_offset += 2;
1225         *data_chars -= 2;
1226     }
1227
1228     return TRUE;
1229 }
1230
1231 /*****************************************************************/
1232 /* Write the stub info to the data buffer while reading a packet */
1233 /*****************************************************************/
1234 static int write_stub_header(guint8 *frame_buffer, char *timestamp_string,
1235                              packet_direction_t direction, int encap,
1236                              gchar *context_name, guint8 context_port,
1237                              gchar *protocol_name, gchar *variant_name,
1238                              gchar *outhdr_name)
1239 {
1240     int stub_offset = 0;
1241
1242     g_strlcpy((char*)frame_buffer, context_name, MAX_CONTEXT_NAME+1);
1243     stub_offset += (int)(strlen(context_name) + 1);
1244
1245     /* Context port number */
1246     frame_buffer[stub_offset] = context_port;
1247     stub_offset++;
1248
1249     /* Timestamp within file */
1250     g_strlcpy((char*)&frame_buffer[stub_offset], timestamp_string, MAX_TIMESTAMP_LEN+1);
1251     stub_offset += (int)(strlen(timestamp_string) + 1);
1252
1253     /* Protocol name */
1254     g_strlcpy((char*)&frame_buffer[stub_offset], protocol_name, MAX_PROTOCOL_NAME+1);
1255     stub_offset += (int)(strlen(protocol_name) + 1);
1256
1257     /* Protocol variant number (as string) */
1258     g_strlcpy((void*)&frame_buffer[stub_offset], variant_name, MAX_VARIANT_DIGITS+1);
1259     stub_offset += (int)(strlen(variant_name) + 1);
1260
1261     /* Outhdr */
1262     g_strlcpy((char*)&frame_buffer[stub_offset], outhdr_name, MAX_OUTHDR_NAME+1);
1263     stub_offset += (int)(strlen(outhdr_name) + 1);
1264
1265     /* Direction */
1266     frame_buffer[stub_offset] = direction;
1267     stub_offset++;
1268
1269     /* Encap */
1270     frame_buffer[stub_offset] = (guint8)encap;
1271     stub_offset++;
1272
1273     return stub_offset;
1274 }
1275
1276
1277 /**************************************************************/
1278 /* Set pseudo-header info depending upon packet encapsulation */
1279 /**************************************************************/
1280 static void set_pseudo_header_info(wtap *wth,
1281                                    int pkt_encap,
1282                                    gint64 file_offset,
1283                                    union wtap_pseudo_header *pseudo_header,
1284                                    packet_direction_t direction,
1285                                    gchar *aal_header_chars)
1286 {
1287     pseudo_header->dct2000.seek_off = file_offset;
1288     pseudo_header->dct2000.wth = wth;
1289
1290     switch (pkt_encap) {
1291         case WTAP_ENCAP_ATM_PDUS_UNTRUNCATED:
1292             set_aal_info(pseudo_header, direction, aal_header_chars);
1293             break;
1294         case WTAP_ENCAP_ISDN:
1295             set_isdn_info(pseudo_header, direction);
1296             break;
1297         case WTAP_ENCAP_PPP:
1298             set_ppp_info(pseudo_header, direction);
1299             break;
1300
1301         default:
1302             /* Other supported types don't need to set anything here... */
1303             break;
1304     }
1305 }
1306
1307
1308 /*********************************************/
1309 /* Fill in atm pseudo-header with known info */
1310 /*********************************************/
1311 static void set_aal_info(union wtap_pseudo_header *pseudo_header,
1312                          packet_direction_t direction,
1313                          gchar *aal_header_chars)
1314 {
1315     /* 'aal_head_chars' has this format (for AAL2 at least):
1316        Global Flow Control (4 bits) | VPI (8 bits) | VCI (16 bits) |
1317        Payload Type (4 bits) | Padding (3 bits?) | Link? (1 bit) |
1318        Channel Identifier (8 bits) | ...
1319     */
1320
1321     /* Indicate that this is a reassembled PDU */
1322     pseudo_header->dct2000.inner_pseudo_header.atm.flags = 0x00;
1323
1324     /* Channel 0 is DTE->DCE, 1 is DCE->DTE. Always set 0 for now.
1325        TODO: Can we infer the correct value here?
1326        Meanwhile, just use the direction to make them distinguishable...
1327     */
1328     pseudo_header->dct2000.inner_pseudo_header.atm.channel = (direction == received);
1329
1330     /* Assume always AAL2 for FP */
1331     pseudo_header->dct2000.inner_pseudo_header.atm.aal = AAL_2;
1332
1333     pseudo_header->dct2000.inner_pseudo_header.atm.type = TRAF_UMTS_FP;
1334     pseudo_header->dct2000.inner_pseudo_header.atm.subtype = TRAF_ST_UNKNOWN;
1335
1336     /* vpi is 8 bits (2nd & 3rd nibble) */
1337     pseudo_header->dct2000.inner_pseudo_header.atm.vpi =
1338         hex_byte_from_chars(aal_header_chars+1);
1339
1340     /* vci is next 16 bits */
1341     pseudo_header->dct2000.inner_pseudo_header.atm.vci =
1342         ((hex_from_char(aal_header_chars[3]) << 12) |
1343          (hex_from_char(aal_header_chars[4]) << 8) |
1344          (hex_from_char(aal_header_chars[5]) << 4) |
1345          hex_from_char(aal_header_chars[6]));
1346
1347     /* 0 means we don't know how many cells the frame comprises. */
1348     pseudo_header->dct2000.inner_pseudo_header.atm.cells = 0;
1349
1350     /* cid is usually last byte.  Unless last char is not hex digit, in which
1351        case cid is derived from last char in ascii */
1352     if (isalnum((guchar)aal_header_chars[11])) {
1353         pseudo_header->dct2000.inner_pseudo_header.atm.aal2_cid =
1354             hex_byte_from_chars(aal_header_chars+10);
1355     }
1356     else {
1357         pseudo_header->dct2000.inner_pseudo_header.atm.aal2_cid =
1358             (int)aal_header_chars[11] - '0';
1359     }
1360 }
1361
1362
1363 /**********************************************/
1364 /* Fill in isdn pseudo-header with known info */
1365 /**********************************************/
1366 void set_isdn_info(union wtap_pseudo_header *pseudo_header,
1367                    packet_direction_t direction)
1368 {
1369     /* This field is used to set the 'Source' and 'Destination' columns to
1370        'User' or 'Network'. If we assume that we're simulating the network,
1371        treat Received messages as being destined for the network.
1372     */
1373     pseudo_header->dct2000.inner_pseudo_header.isdn.uton = (direction == received);
1374
1375     /* This corresponds to the circuit ID.  0 is treated as LAPD,
1376        everything else would be treated as a B-channel
1377     */
1378     pseudo_header->dct2000.inner_pseudo_header.isdn.channel = 0;
1379 }
1380
1381
1382 /*********************************************/
1383 /* Fill in ppp pseudo-header with known info */
1384 /*********************************************/
1385 static void set_ppp_info(union wtap_pseudo_header *pseudo_header,
1386                          packet_direction_t direction)
1387 {
1388     /* Set direction. */
1389     pseudo_header->dct2000.inner_pseudo_header.p2p.sent = (direction == sent);
1390 }
1391
1392
1393 /********************************************************/
1394 /* Return hex nibble equivalent of hex string character */
1395 /********************************************************/
1396 guint8 hex_from_char(gchar c)
1397 {
1398     if ((c >= '0') && (c <= '9')) {
1399         return c - '0';
1400     }
1401
1402     if ((c >= 'a') && (c <= 'f')) {
1403         return 0x0a + (c - 'a');
1404     }
1405
1406     /* Not a valid hex string character */
1407     return 0xff;
1408 }
1409
1410 /* Extract and return a byte value from 2 ascii hex chars, starting from the given pointer */
1411 static guint8 hex_byte_from_chars(gchar *c)
1412 {
1413     static guchar hex_char_array[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
1414                                          'a', 'b', 'c', 'd', 'e', 'f' };
1415
1416     /* Populate lookup table first time */
1417     static guint8 tableValues[255][255];
1418     static gint tableSet = FALSE;
1419     if (!tableSet) {
1420         gint i, j;
1421         for (i=0; i < 16; i++) {
1422             for (j=0; j < 16; j++) {
1423                 tableValues[hex_char_array[i]][hex_char_array[j]] = i*16 + j;
1424             }
1425         }
1426
1427         tableSet = TRUE;
1428     }
1429
1430     /* Return value from quick table lookup */
1431     return tableValues[(unsigned char)c[0]][(unsigned char)c[1]];
1432 }
1433
1434
1435
1436 /********************************************************/
1437 /* Return character corresponding to hex nibble value   */
1438 /********************************************************/
1439 gchar char_from_hex(guint8 hex)
1440 {
1441     static char hex_lookup[16] =
1442     { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
1443
1444     if (hex > 15) {
1445         return '?';
1446     }
1447
1448     return hex_lookup[hex];
1449 }
1450
1451 /***********************************************/
1452 /* Equality test for packet prefix hash tables */
1453 /***********************************************/
1454 gint packet_offset_equal(gconstpointer v, gconstpointer v2)
1455 {
1456     /* Dereferenced pointers must have same gint64 offset value */
1457     return (*(const gint64*)v == *(const gint64*)v2);
1458 }
1459
1460
1461 /********************************************/
1462 /* Hash function for packet-prefix hash table */
1463 /********************************************/
1464 guint packet_offset_hash_func(gconstpointer v)
1465 {
1466     /* Use low-order bits of git64 offset value */
1467     return (guint)(*(const gint64*)v);
1468 }
1469
1470
1471 /************************************************************************/
1472 /* Parse year, month, day, hour, minute, seconds out of formatted line. */
1473 /* Set secs and usecs as output                                         */
1474 /* Return FALSE if no valid time can be read                            */
1475 /************************************************************************/
1476 gboolean get_file_time_stamp(gchar *linebuff, time_t *secs, guint32 *usecs)
1477 {
1478     int n;
1479     struct tm tm;
1480     #define MAX_MONTH_LETTERS 9
1481     char month[MAX_MONTH_LETTERS+1];
1482
1483     int day, year, hour, minute, second;
1484     int scan_found;
1485
1486     /* If line longer than expected, file is probably not correctly formatted */
1487     if (strlen(linebuff) > MAX_TIMESTAMP_LINE_LENGTH) {
1488         return FALSE;
1489     }
1490
1491     /**************************************************************/
1492     /* First is month. Read until get a space following the month */
1493     for (n=0; (linebuff[n] != ' ') && (n < MAX_MONTH_LETTERS); n++) {
1494         month[n] = linebuff[n];
1495     }
1496     month[n] = '\0';
1497
1498     if      (strcmp(month, "January"  ) == 0)  tm.tm_mon = 0;
1499     else if (strcmp(month, "February" ) == 0)  tm.tm_mon = 1;
1500     else if (strcmp(month, "March"    ) == 0)  tm.tm_mon = 2;
1501     else if (strcmp(month, "April"    ) == 0)  tm.tm_mon = 3;
1502     else if (strcmp(month, "May"      ) == 0)  tm.tm_mon = 4;
1503     else if (strcmp(month, "June"     ) == 0)  tm.tm_mon = 5;
1504     else if (strcmp(month, "July"     ) == 0)  tm.tm_mon = 6;
1505     else if (strcmp(month, "August"   ) == 0)  tm.tm_mon = 7;
1506     else if (strcmp(month, "September") == 0)  tm.tm_mon = 8;
1507     else if (strcmp(month, "October"  ) == 0)  tm.tm_mon = 9;
1508     else if (strcmp(month, "November" ) == 0)  tm.tm_mon = 10;
1509     else if (strcmp(month, "December" ) == 0)  tm.tm_mon = 11;
1510     else {
1511         /* Give up if not found a properly-formatted date */
1512         return FALSE;
1513     }
1514     /* Skip space char */
1515     n++;
1516
1517     /********************************************************/
1518     /* Scan for remaining numerical fields                  */
1519     scan_found = sscanf(linebuff+n, "%d, %d     %d:%d:%d.%u",
1520                         &day, &year, &hour, &minute, &second, usecs);
1521     if (scan_found != 6) {
1522         /* Give up if not all found */
1523         return FALSE;
1524     }
1525
1526     /******************************************************/
1527     /* Fill in remaining fields and return it in a time_t */
1528     tm.tm_year = year - 1900;
1529     tm.tm_mday = day;
1530     tm.tm_hour = hour;
1531     tm.tm_min = minute;
1532     tm.tm_sec = second;
1533     tm.tm_isdst = -1;    /* daylight saving time info not known */
1534
1535     /* Get seconds from this time */
1536     *secs = mktime(&tm);
1537
1538     /* Multiply 4 digits given to get micro-seconds */
1539     *usecs = *usecs * 100;
1540
1541     return TRUE;
1542 }
1543
1544 /* Free the data allocated inside a line_prefix_info_t */
1545 gboolean free_line_prefix_info(gpointer key, gpointer value,
1546                                gpointer user_data _U_)
1547 {
1548     line_prefix_info_t *info = (line_prefix_info_t*)value;
1549
1550     /* Free the 64-bit key value */
1551     g_free(key);
1552
1553     /* Free the strings inside */
1554     g_free(info->before_time);
1555     if (info->after_time) {
1556         g_free(info->after_time);
1557     }
1558
1559     /* And the structure itself */
1560     g_free(info);
1561
1562     /* Item will always be removed from table */
1563     return TRUE;
1564 }
1565