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