Whitespace fixes: Indented lines should not start with
[metze/wireshark/wip.git] / wiretap / catapult_dct2000.c
index 33ab05c5d609fefbee9a92eceef1686978cf21ac..5037c12b081a48296277a19325b508b190c3585c 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
@@ -73,9 +71,21 @@ typedef struct
 
 
 /*******************************************************************/
-/* Information stored external to a file (wtap) needed for dumping */
+/* Information stored external to a file (wtap) needed for reading and dumping */
 typedef struct dct2000_file_externals
 {
+    /* Remember the time at the start of capture */
+    time_t  start_secs;
+    guint32 start_usecs;
+
+    /*
+     * The following information is needed only for dumping.
+     *
+     * XXX - Wiretap is not *supposed* to require that a packet being
+     * dumped come from a file of the same type that you currently have
+     * open; this should be fixed.
+     */
+
     /* Buffer to hold first line, including magic and format number */
     gchar firstline[MAX_FIRST_LINE_LENGTH];
     gint  firstline_length;
@@ -86,7 +96,6 @@ typedef struct dct2000_file_externals
 
     /* Hash table to store text prefix data part of displayed packets.
        Records (file offset -> line_prefix_info_t)
-       N.B. This is only needed for dumping
     */
     GHashTable *packet_prefix_table;
 } dct2000_file_externals_t;
@@ -99,39 +108,40 @@ static const gchar catapult_dct2000_magic[] = "Session Transcript";
 static gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info,
                                       gint64 *data_offset);
 static gboolean catapult_dct2000_seek_read(wtap *wth, gint64 seek_off,
-                                           union wtap_pseudo_header *pseudo_header,
-                                           guchar *pd, int length,
+                                           struct wtap_pkthdr *phdr,
+                                           guint8 *pd, int length,
                                            int *err, gchar **err_info);
 static void catapult_dct2000_close(wtap *wth);
 
 static gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-                                      const union wtap_pseudo_header *pseudo_header,
-                                      const guchar *pd, int *err);
-static gboolean catapult_dct2000_dump_close(wtap_dumper *wdh, int *err);
+                                      const guint8 *pd, int *err);
 
 
 /************************************************************/
 /* Private helper functions                                 */
 static gboolean read_new_line(FILE_T fh, gint64 *offset, gint *length,
-                              gchar *buf, size_t bufsize);
+                              gchar *buf, size_t bufsize, int *err,
+                              gchar **err_info);
 static gboolean parse_line(char *linebuff, gint line_length,
                            gint *seconds, gint *useconds,
                            long *before_time_offset, long *after_time_offset,
                            long *data_offset,
                            gint *data_chars,
                            packet_direction_t *direction,
-                           int *encap, int *is_comment,
+                           int *encap, int *is_comment, int *is_sprint,
                            gchar *aal_header_chars,
                            gchar *context_name, guint8 *context_portp,
                            gchar *protocol_name, gchar *variant_name,
                            gchar *outhdr_name);
-static int write_stub_header(guchar *frame_buffer, char *timestamp_string,
+static int write_stub_header(guint8 *frame_buffer, char *timestamp_string,
                              packet_direction_t direction, int encap,
                              gchar *context_name, guint8 context_port,
                              gchar *protocol_name, gchar *variant_name,
                              gchar *outhdr_name);
-static guchar hex_from_char(gchar c);
-static gchar char_from_hex(guchar hex);
+static guint8 hex_from_char(gchar c);
+static void   prepare_hex_byte_from_chars_table(void);
+static guint8 hex_byte_from_chars(gchar *c);
+static gchar char_from_hex(guint8 hex);
 
 static void set_pseudo_header_info(wtap *wth,
                                    int pkt_encap,
@@ -158,14 +168,16 @@ static gboolean free_line_prefix_info(gpointer key, gpointer value, gpointer use
 /********************************************/
 /* Open file (for reading)                 */
 /********************************************/
-int catapult_dct2000_open(wtap *wth, int *err, gchar **err_info _U_)
+int
+catapult_dct2000_open(wtap *wth, int *err, gchar **err_info)
 {
     gint64  offset = 0;
     time_t  timestamp;
     guint32 usecs;
     gint firstline_length = 0;
     dct2000_file_externals_t *file_externals;
-    gchar linebuff[MAX_LINE_LENGTH];
+    static gchar linebuff[MAX_LINE_LENGTH];
+    static gboolean hex_byte_table_values_set = FALSE;
 
     /* Clear errno before reading from the file */
     errno = 0;
@@ -174,8 +186,12 @@ int catapult_dct2000_open(wtap *wth, int *err, gchar **err_info _U_)
     /********************************************************************/
     /* First line needs to contain at least as many characters as magic */
 
-    read_new_line(wth->fh, &offset, &firstline_length, linebuff,
-                  sizeof linebuff);
+    if (!read_new_line(wth->fh, &offset, &firstline_length, linebuff,
+                       sizeof linebuff, err, err_info)) {
+        if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+            return -1;
+        return 0;
+    }
     if (((size_t)firstline_length < strlen(catapult_dct2000_magic)) ||
         firstline_length >= MAX_FIRST_LINE_LENGTH) {
 
@@ -187,12 +203,17 @@ int catapult_dct2000_open(wtap *wth, int *err, gchar **err_info _U_)
         return 0;
     }
 
+    /* Make sure table is ready for use */
+    if (!hex_byte_table_values_set) {
+        prepare_hex_byte_from_chars_table();
+        hex_byte_table_values_set = TRUE;
+    }
 
     /*********************************************************************/
     /* Need entry in file_externals table                                */
 
     /* Allocate a new file_externals structure for this file */
-    file_externals = g_malloc(sizeof(dct2000_file_externals_t));
+    file_externals = g_new(dct2000_file_externals_t,1);
     memset((void*)file_externals, '\0', sizeof(dct2000_file_externals_t));
 
     /* Copy this first line into buffer so could write out later */
@@ -202,10 +223,15 @@ int catapult_dct2000_open(wtap *wth, int *err, gchar **err_info _U_)
 
     /***********************************************************/
     /* Second line contains file timestamp                     */
-    /* Store this offset in in wth->capture->catapult_dct2000  */
+    /* Store this offset in in file_externals                  */
 
-    read_new_line(wth->fh, &offset, &(file_externals->secondline_length),
-                  linebuff, sizeof linebuff);
+    if (!read_new_line(wth->fh, &offset, &(file_externals->secondline_length),
+                       linebuff, sizeof linebuff, err, err_info)) {
+        g_free(file_externals);
+        if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+            return -1;
+        return 0;
+    }
     if ((file_externals->secondline_length >= MAX_TIMESTAMP_LINE_LENGTH) ||
         (!get_file_time_stamp(linebuff, &timestamp, &usecs))) {
 
@@ -214,10 +240,9 @@ int catapult_dct2000_open(wtap *wth, int *err, gchar **err_info _U_)
         return 0;
     }
 
-    /* Allocate struct and fill in timestamp */
-    wth->capture.catapult_dct2000 = g_malloc(sizeof(catapult_dct2000_t));
-    wth->capture.catapult_dct2000->start_secs = timestamp;
-    wth->capture.catapult_dct2000->start_usecs = usecs;
+    /* Fill in timestamp */
+    file_externals->start_secs = timestamp;
+    file_externals->start_usecs = usecs;
 
     /* Copy this second line into buffer so could write out later */
     g_strlcpy(file_externals->secondline, linebuff, file_externals->secondline_length+1);
@@ -247,29 +272,81 @@ int catapult_dct2000_open(wtap *wth, int *err, gchar **err_info _U_)
         g_hash_table_new(packet_offset_hash_func, packet_offset_equal);
 
     /* Set this wtap to point to the file_externals */
-    wth->capture.generic = (void*)file_externals;
+    wth->priv = (void*)file_externals;
 
     *err = errno;
     return 1;
 }
 
+/* Ugly, but much faster than using g_snprintf! */
+static void write_timestamp_string(char *timestamp_string, int secs, int tenthousandths)
+{
+    int idx = 0;
+
+    /* Secs */
+    if (secs < 10) {
+        timestamp_string[idx++] = ((secs % 10))         + '0';
+    }
+    else if (secs < 100) {
+        timestamp_string[idx++] = ( secs  /       10)   + '0';
+        timestamp_string[idx++] = ((secs % 10))          + '0';
+    }
+    else if (secs < 1000) {
+        timestamp_string[idx++] = ((secs)         / 100)   + '0';
+        timestamp_string[idx++] = ((secs % 100))  / 10     + '0';
+        timestamp_string[idx++] = ((secs % 10))            + '0';
+    }
+    else if (secs < 10000) {
+        timestamp_string[idx++] = ((secs)          / 1000)   + '0';
+        timestamp_string[idx++] = ((secs % 1000))  / 100     + '0';
+        timestamp_string[idx++] = ((secs % 100))   / 10      + '0';
+        timestamp_string[idx++] = ((secs % 10))              + '0';
+    }
+    else if (secs < 100000) {
+        timestamp_string[idx++] = ((secs)          / 10000)   + '0';
+        timestamp_string[idx++] = ((secs % 10000)) / 1000     + '0';
+        timestamp_string[idx++] = ((secs % 1000))  / 100      + '0';
+        timestamp_string[idx++] = ((secs % 100))   / 10       + '0';
+        timestamp_string[idx++] = ((secs % 10))               + '0';
+    }
+    else if (secs < 1000000) {
+        timestamp_string[idx++] = ((secs)           / 100000) + '0';
+        timestamp_string[idx++] = ((secs % 100000)) / 10000   + '0';
+        timestamp_string[idx++] = ((secs % 10000))  / 1000    + '0';
+        timestamp_string[idx++] = ((secs % 1000))   / 100     + '0';
+        timestamp_string[idx++] = ((secs % 100))    / 10      + '0';
+        timestamp_string[idx++] = ((secs % 10))               + '0';
+    }
+    else {
+        g_snprintf(timestamp_string, MAX_TIMESTAMP_LEN, "%d.%04d", secs, tenthousandths);
+        return;
+    }
+
+    timestamp_string[idx++] = '.';
+    timestamp_string[idx++] = ( tenthousandths          / 1000) + '0';
+    timestamp_string[idx++] = ((tenthousandths % 1000)  / 100)  + '0';
+    timestamp_string[idx++] = ((tenthousandths % 100)   / 10)   + '0';
+    timestamp_string[idx++] = ((tenthousandths % 10))           + '0';
+    timestamp_string[idx++] = '\0';
+}
 
 /**************************************************/
 /* Read packet function.                          */
 /* Look for and read the next usable packet       */
 /* - return TRUE and details if found             */
 /**************************************************/
-gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
-                               gint64 *data_offset)
+static gboolean
+catapult_dct2000_read(wtap *wth, int *err, gchar **err_info,
+                      gint64 *data_offset)
 {
-    gint64 offset = wth->data_offset;
+    gint64 offset = file_tell(wth->fh);
     long dollar_offset, before_time_offset, after_time_offset;
     packet_direction_t direction;
     int encap;
 
     /* Get wtap external structure for this wtap */
     dct2000_file_externals_t *file_externals =
-        (dct2000_file_externals_t*)wth->capture.generic;
+        (dct2000_file_externals_t*)wth->priv;
 
     /* There *has* to be an entry for this wth */
     if (!file_externals) {
@@ -280,28 +357,28 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
     while (1) {
         int line_length, seconds, useconds, data_chars;
         int is_comment = FALSE;
+        int is_sprint = FALSE;
         gint64 this_offset = offset;
-        gchar linebuff[MAX_LINE_LENGTH+1];
+        static gchar linebuff[MAX_LINE_LENGTH+1];
         gchar aal_header_chars[AAL_HEADER_CHARS];
         gchar context_name[MAX_CONTEXT_NAME];
-        guint8 context_port;
+        guint8 context_port = 0;
         gchar protocol_name[MAX_PROTOCOL_NAME+1];
         gchar variant_name[MAX_VARIANT_DIGITS+1];
         gchar outhdr_name[MAX_OUTHDR_NAME+1];
 
         /* Are looking for first packet after 2nd line */
-        if (wth->data_offset == 0) {
+        if (file_tell(wth->fh) == 0) {
             this_offset += (file_externals->firstline_length+1+
                             file_externals->secondline_length+1);
         }
 
-        /* Clear errno before reading from the file */
-        errno = 0;
-
         /* Read a new line from file into linebuff */
-        if (read_new_line(wth->fh, &offset, &line_length, linebuff,
-                          sizeof linebuff) == FALSE) {
-            /* Get out if no more lines can be read */
+        if (!read_new_line(wth->fh, &offset, &line_length, linebuff,
+                           sizeof linebuff, err, err_info)) {
+            if (*err != 0)
+                return FALSE;  /* error */
+            /* No more lines can be read, so quit. */
             break;
         }
 
@@ -309,18 +386,20 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
         if (parse_line(linebuff, line_length, &seconds, &useconds,
                        &before_time_offset, &after_time_offset,
                        &dollar_offset,
-                       &data_chars, &direction, &encap, &is_comment,
+                       &data_chars, &direction, &encap, &is_comment, &is_sprint,
                        aal_header_chars,
                        context_name, &context_port,
                        protocol_name, variant_name, outhdr_name)) {
-            guchar *frame_buffer;
+            guint8 *frame_buffer;
             int n;
             int stub_offset = 0;
             line_prefix_info_t *line_prefix_info;
             char timestamp_string[MAX_TIMESTAMP_LEN+1];
             gint64 *pkey = NULL;
 
-            g_snprintf(timestamp_string, 32, "%d.%04d", seconds, useconds/100);
+            write_timestamp_string(timestamp_string, seconds, useconds/100);
+
+            wth->phdr.presence_flags = WTAP_HAS_TS;
 
             /* All packets go to Catapult DCT2000 stub dissector */
             wth->phdr.pkt_encap = WTAP_ENCAP_CATAPULT_DCT2000;
@@ -330,16 +409,13 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
             */
             *data_offset = this_offset;
 
-            /* This is the position in the file where the next _read() will be called from */
-            wth->data_offset = this_offset + line_length + 1;
-
             /* Fill in timestamp (capture base + packet offset) */
-            wth->phdr.ts.secs = wth->capture.catapult_dct2000->start_secs + seconds;
-            if ((wth->capture.catapult_dct2000->start_usecs + useconds) >= 1000000) {
+            wth->phdr.ts.secs = file_externals->start_secs + seconds;
+            if ((file_externals->start_usecs + useconds) >= 1000000) {
                 wth->phdr.ts.secs++;
             }
             wth->phdr.ts.nsecs =
-                ((wth->capture.catapult_dct2000->start_usecs + useconds) % 1000000) *1000;
+                ((file_externals->start_usecs + useconds) % 1000000) *1000;
 
             /* Get buffer pointer ready */
             buffer_assure_space(wth->frame_buffer,
@@ -351,7 +427,7 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
                                 strlen(protocol_name)+1 +    /* Protocol name */
                                 1 +                          /* direction */
                                 1 +                          /* encap */
-                                is_comment ? data_chars : (data_chars/2));
+                                (is_comment ? data_chars : (data_chars/2)));
             frame_buffer = buffer_start_ptr(wth->frame_buffer);
 
 
@@ -371,27 +447,26 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
             if (!is_comment) {
                 /****************************************************/
                 /* Copy data into buffer, converting from ascii hex */
-                for (n=0; n <= data_chars; n+=2) {
+                for (n=0; n < data_chars; n+=2) {
                     frame_buffer[stub_offset + n/2] =
-                        (hex_from_char(linebuff[dollar_offset+n]) << 4) |
-                         hex_from_char(linebuff[dollar_offset+n+1]);
+                        hex_byte_from_chars(linebuff+dollar_offset+n);
                 }
             }
             else {
                 /***********************************************************/
                 /* Copy packet data into buffer, just copying ascii chars  */
-                for (n=0; n <= data_chars; n++) {
+                for (n=0; n < data_chars; n++) {
                     frame_buffer[stub_offset + n] = linebuff[dollar_offset+n];
                 }
             }
 
             /* Store the packet prefix in the hash table */
-            line_prefix_info = g_malloc(sizeof(line_prefix_info_t));
+            line_prefix_info = g_new(line_prefix_info_t,1);
 
             /* Create and use buffer for contents before time */
-            line_prefix_info->before_time = g_malloc(before_time_offset+2);
-            g_strlcpy(line_prefix_info->before_time, linebuff, before_time_offset+1);
-            line_prefix_info->before_time[before_time_offset+1] = '\0';
+            line_prefix_info->before_time = (gchar *)g_malloc(before_time_offset+1);
+            memcpy(line_prefix_info->before_time, linebuff, before_time_offset);
+            line_prefix_info->before_time[before_time_offset] = '\0';
 
             /* Create and use buffer for contents before time.
                Do this only if it doesn't correspond to " l ", which is by far the most
@@ -403,29 +478,26 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
             }
             else {
                 /* Allocate & write buffer for line between timestamp and data */
-                line_prefix_info->after_time = g_malloc(dollar_offset - after_time_offset);
-                g_strlcpy(line_prefix_info->after_time, linebuff+after_time_offset,
-                          dollar_offset - after_time_offset);
+                line_prefix_info->after_time = (gchar *)g_malloc(dollar_offset - after_time_offset);
+                memcpy(line_prefix_info->after_time, linebuff+after_time_offset, dollar_offset - after_time_offset);
                 line_prefix_info->after_time[dollar_offset - after_time_offset-1] = '\0';
             }
 
             /* Add packet entry into table */
-            pkey = g_malloc(sizeof(*pkey));
+            pkey = (gint64 *)g_malloc(sizeof(*pkey));
             *pkey = this_offset;
             g_hash_table_insert(file_externals->packet_prefix_table, pkey, line_prefix_info);
 
             /* Set pseudo-header if necessary */
-            set_pseudo_header_info(wth, encap, this_offset, &wth->pseudo_header,
+            set_pseudo_header_info(wth, encap, this_offset, &wth->phdr.pseudo_header,
                                    direction, aal_header_chars);
 
             /* OK, we have packet details to return */
-            *err = errno;
             return TRUE;
         }
     }
 
     /* No packet details to return... */
-    *err = errno;
     return FALSE;
 }
 
@@ -435,23 +507,34 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
 /**************************************************/
 static gboolean
 catapult_dct2000_seek_read(wtap *wth, gint64 seek_off,
-                           union wtap_pseudo_header *pseudo_header, guchar *pd,
+                           struct wtap_pkthdr *phdr, guint8 *pd,
                            int length, int *err, gchar **err_info)
 {
-    gint64 offset = wth->data_offset;
+    union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
+    gint64 offset = 0;
     long dollar_offset, before_time_offset, after_time_offset;
-    gchar linebuff[MAX_LINE_LENGTH+1];
+    static gchar linebuff[MAX_LINE_LENGTH+1];
     gchar aal_header_chars[AAL_HEADER_CHARS];
     gchar context_name[MAX_CONTEXT_NAME];
-    guint8 context_port;
+    guint8 context_port = 0;
     gchar protocol_name[MAX_PROTOCOL_NAME+1];
     gchar variant_name[MAX_VARIANT_DIGITS+1];
     gchar outhdr_name[MAX_OUTHDR_NAME+1];
     int  is_comment = FALSE;
+    int  is_sprint = FALSE;
     packet_direction_t direction;
     int encap;
     int seconds, useconds, data_chars;
 
+    /* Get wtap external structure for this wtap */
+    dct2000_file_externals_t *file_externals =
+        (dct2000_file_externals_t*)wth->priv;
+
+    /* There *has* to be an entry for this wth */
+    if (!file_externals) {
+        return FALSE;
+    }
+
     /* Reset errno */
     *err = errno = 0;
 
@@ -461,8 +544,8 @@ catapult_dct2000_seek_read(wtap *wth, gint64 seek_off,
     }
 
     /* Re-read whole line (this really should succeed) */
-    if (read_new_line(wth->random_fh, &offset, &length, linebuff,
-                      sizeof linebuff) == FALSE) {
+    if (!read_new_line(wth->random_fh, &offset, &length, linebuff,
+                      sizeof linebuff, err, err_info)) {
         return FALSE;
     }
 
@@ -470,14 +553,15 @@ catapult_dct2000_seek_read(wtap *wth, gint64 seek_off,
     if (parse_line(linebuff, length, &seconds, &useconds,
                    &before_time_offset, &after_time_offset,
                    &dollar_offset,
-                   &data_chars, &direction, &encap, &is_comment,
+                   &data_chars, &direction, &encap, &is_comment, &is_sprint,
                    aal_header_chars,
                    context_name, &context_port,
                    protocol_name, variant_name, outhdr_name)) {
         int n;
         int stub_offset = 0;
-        char timestamp_string[32];
-        g_snprintf(timestamp_string, 32, "%d.%04d", seconds, useconds/100);
+        char timestamp_string[MAX_TIMESTAMP_LEN+1];
+
+        write_timestamp_string(timestamp_string, seconds, useconds/100);
 
         /* Make sure all packets go to catapult dct2000 dissector */
         wth->phdr.pkt_encap = WTAP_ENCAP_CATAPULT_DCT2000;
@@ -496,8 +580,7 @@ catapult_dct2000_seek_read(wtap *wth, gint64 seek_off,
             /***********************************************************/
             /* Copy packet data into buffer, converting from ascii hex */
             for (n=0; n <= data_chars; n+=2) {
-                pd[stub_offset + n/2] = (hex_from_char(linebuff[dollar_offset+n]) << 4) |
-                                         hex_from_char(linebuff[dollar_offset+n+1]);
+                pd[stub_offset + n/2] = hex_byte_from_chars(linebuff+dollar_offset+n);
             }
         }
         else {
@@ -528,11 +611,12 @@ catapult_dct2000_seek_read(wtap *wth, gint64 seek_off,
 /***************************************************************************/
 /* Free dct2000-specific capture info from file that was open for reading  */
 /***************************************************************************/
-void catapult_dct2000_close(wtap *wth)
+static void
+catapult_dct2000_close(wtap *wth)
 {
     /* Get externals for this file */
     dct2000_file_externals_t *file_externals =
-        (dct2000_file_externals_t*)wth->capture.generic;
+        (dct2000_file_externals_t*)wth->priv;
 
     /* The entry *has* to be found */
     if (!file_externals) {
@@ -544,12 +628,6 @@ void catapult_dct2000_close(wtap *wth)
                                 free_line_prefix_info, NULL);
     /* Free up its line prefix table */
     g_hash_table_destroy(file_externals->packet_prefix_table);
-
-    /* And free up file_externals itself */
-    g_free(file_externals);
-
-    /* Also free this capture info */
-    g_free(wth->capture.catapult_dct2000);
 }
 
 
@@ -559,15 +637,20 @@ void catapult_dct2000_close(wtap *wth)
 /* Dump functions          */
 /***************************/
 
+typedef struct {
+    gboolean           first_packet_written;
+    struct wtap_nstime start_time;
+} dct2000_dump_t;
+
 /*****************************************************/
 /* The file that we are writing to has been opened.  */
 /* Set other dump callbacks.                         */
 /*****************************************************/
-gboolean catapult_dct2000_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err _U_)
+gboolean
+catapult_dct2000_dump_open(wtap_dumper *wdh, int *err _U_)
 {
     /* Fill in other dump callbacks */
     wdh->subtype_write = catapult_dct2000_dump;
-    wdh->subtype_close = catapult_dct2000_dump_close;
 
     return TRUE;
 }
@@ -576,7 +659,8 @@ gboolean catapult_dct2000_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, in
 /* Respond to queries about which encap types we support */
 /* writing to.                                           */
 /*********************************************************/
-int catapult_dct2000_dump_can_write_encap(int encap)
+int
+catapult_dct2000_dump_can_write_encap(int encap)
 {
     switch (encap) {
         case WTAP_ENCAP_CATAPULT_DCT2000:
@@ -594,72 +678,60 @@ int catapult_dct2000_dump_can_write_encap(int encap)
 /* Write a single packet out to the file */
 /*****************************************/
 
-static gboolean do_fwrite(const void *data, size_t size, size_t count, FILE *stream, int *err_p)
-{
-    size_t nwritten;
-
-    nwritten = fwrite(data, size, count, stream);
-    if (nwritten != count) {
-        if (nwritten == 0 && ferror(stream)) {
-            *err_p = errno;
-        }
-        else {
-            *err_p = WTAP_ERR_SHORT_WRITE;
-        }
-
-        return FALSE;
-    }
-    return TRUE;
-}
-
-gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-                               const union wtap_pseudo_header *pseudo_header,
-                               const guchar *pd, int *err)
+static gboolean
+catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
+                      const guint8 *pd, int *err)
 {
+    const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
     guint32 n;
     line_prefix_info_t *prefix = NULL;
     gchar time_string[16];
     gboolean is_comment;
+    gboolean is_sprint = FALSE;
+    dct2000_dump_t *dct2000;
+    int consecutive_slashes=0;
+    char *p_c;
 
     /******************************************************/
     /* Get the file_externals structure for this file */
     /* Find wtap external structure for this wtap */
     dct2000_file_externals_t *file_externals =
-        (dct2000_file_externals_t*)pseudo_header->dct2000.wth->capture.generic;
+        (dct2000_file_externals_t*)pseudo_header->dct2000.wth->priv;
 
-    if (wdh->dump.dct2000 == NULL) {
-        /* Allocate the dct2000-specific dump structure */
-        wdh->dump.dct2000 = g_malloc(sizeof(catapult_dct2000_t));
+    dct2000 = (dct2000_dump_t *)wdh->priv;
+    if (dct2000 == NULL) {
 
         /* Write out saved first line */
-        if (! do_fwrite(file_externals->firstline, 1, file_externals->firstline_length, wdh->fh, err)) {
+        if (!wtap_dump_file_write(wdh, file_externals->firstline,
+                                  file_externals->firstline_length, err)) {
             return FALSE;
         }
-        if (! do_fwrite("\n", 1, 1, wdh->fh, err)) {
+        if (!wtap_dump_file_write(wdh, "\n", 1, err)) {
             return FALSE;
         }
 
         /* Also write out saved second line with timestamp corresponding to the
            opening time of the log.
         */
-        if (! do_fwrite(file_externals->secondline, 1, file_externals->secondline_length, wdh->fh, err)) {
+        if (!wtap_dump_file_write(wdh, file_externals->secondline,
+                                  file_externals->secondline_length, err)) {
             return FALSE;
         }
-        if (! do_fwrite("\n", 1, 1, wdh->fh, err)) {
+        if (!wtap_dump_file_write(wdh, "\n", 1, err)) {
             return FALSE;
         }
 
         /* Allocate the dct2000-specific dump structure */
-        wdh->dump.dct2000 = g_malloc(sizeof(catapult_dct2000_t));
+        dct2000 = (dct2000_dump_t *)g_malloc(sizeof(dct2000_dump_t));
+        wdh->priv = (void *)dct2000;
 
         /* Copy time of beginning of file */
-        wdh->dump.dct2000->start_time.secs =
-            pseudo_header->dct2000.wth->capture.catapult_dct2000->start_secs;
-        wdh->dump.dct2000->start_time.nsecs =
-            (pseudo_header->dct2000.wth->capture.catapult_dct2000->start_usecs * 1000);
+        dct2000->start_time.secs = file_externals->start_secs;
+        dct2000->start_time.nsecs =
+            (file_externals->start_usecs * 1000);
 
-        /* Set flag do don't write header out again */
-        wdh->dump.dct2000->first_packet_written = TRUE;
+        /* Set flag so don't write header out again */
+        dct2000->first_packet_written = TRUE;
     }
 
 
@@ -671,38 +743,49 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
                                                       (const void*)&(pseudo_header->dct2000.seek_off));
 
     /* Write out text before timestamp */
-    if (! do_fwrite(prefix->before_time, 1, strlen(prefix->before_time), wdh->fh, err)) {
+    if (!wtap_dump_file_write(wdh, prefix->before_time,
+                              strlen(prefix->before_time), err)) {
         return FALSE;
     }
 
     /* Can infer from prefix if this is a comment (whose payload is displayed differently) */
-    is_comment = (strstr(prefix->before_time, "/////") != NULL);
+    /* This is much faster than strstr() for "/////" */
+    p_c = prefix->before_time;
+    while (p_c && (*p_c != '/')) {
+        p_c++;
+    }
+    while (p_c && (*p_c == '/')) {
+        consecutive_slashes++;
+        p_c++;
+    }
+    is_comment = (consecutive_slashes == 5);
 
     /* Calculate time of this packet to write, relative to start of dump */
-    if (phdr->ts.nsecs >= wdh->dump.dct2000->start_time.nsecs) {
-        g_snprintf(time_string, 16, "%ld.%04d",
-                 (long)(phdr->ts.secs - wdh->dump.dct2000->start_time.secs),
-                 (phdr->ts.nsecs - wdh->dump.dct2000->start_time.nsecs) / 100000);
+    if (phdr->ts.nsecs >= dct2000->start_time.nsecs) {
+        write_timestamp_string(time_string,
+                               (int)(phdr->ts.secs - dct2000->start_time.secs),
+                               (phdr->ts.nsecs - dct2000->start_time.nsecs) / 100000);
     }
     else {
-        g_snprintf(time_string, 16, "%ld.%04u",
-                 (long)(phdr->ts.secs - wdh->dump.dct2000->start_time.secs-1),
-                 ((1000000000 + (phdr->ts.nsecs / 100000)) - (wdh->dump.dct2000->start_time.nsecs / 100000)) % 10000);
+        write_timestamp_string(time_string,
+                               (int)(phdr->ts.secs - dct2000->start_time.secs-1),
+                               ((1000000000 + (phdr->ts.nsecs / 100000)) - (dct2000->start_time.nsecs / 100000)) % 10000);
     }
 
     /* Write out the calculated timestamp */
-    if (! do_fwrite(time_string, 1, strlen(time_string), wdh->fh, err)) {
+    if (!wtap_dump_file_write(wdh, time_string, strlen(time_string), err)) {
         return FALSE;
     }
 
     /* Write out text between timestamp and start of hex data */
     if (prefix->after_time == NULL) {
-        if (!do_fwrite(" l ", 1, strlen(" l "), wdh->fh, err)) {
+        if (!wtap_dump_file_write(wdh, " l ", strlen(" l "), err)) {
             return FALSE;
         }
     }
     else {
-        if (!do_fwrite(prefix->after_time, 1, strlen(prefix->after_time), wdh->fh, err)) {
+        if (!wtap_dump_file_write(wdh, prefix->after_time,
+                                  strlen(prefix->after_time), err)) {
             return FALSE;
         }
     }
@@ -723,6 +806,9 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
     n++;
 
     /* Protocol name */
+    if (is_comment) {
+        is_sprint = strcmp(pd+n, "sprint") == 0;
+    }
     for (; pd[n] != '\0'; n++);
     n++;
 
@@ -740,19 +826,19 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
 
     /**************************************/
     /* Remainder is encapsulated protocol */
-    if (! do_fwrite("$", 1, 1, wdh->fh, err)) {
+    if (!wtap_dump_file_write(wdh, is_sprint ? " " : "$", 1, err)) {
         return FALSE;
     }
 
     if (!is_comment) {
-        /* Each binary byte is written out as 2 hex string chars */ 
+        /* Each binary byte is written out as 2 hex string chars */
         for (; n < phdr->len; n++) {
             gchar c[2];
-            c[0] = char_from_hex((guchar)(pd[n] >> 4));
-            c[1] = char_from_hex((guchar)(pd[n] & 0x0f));
+            c[0] = char_from_hex((guint8)(pd[n] >> 4));
+            c[1] = char_from_hex((guint8)(pd[n] & 0x0f));
 
             /* Write both hex chars of byte together */
-            if (! do_fwrite(c, 1, 2, wdh->fh, err)) {
+            if (!wtap_dump_file_write(wdh, c, 2, err)) {
                 return FALSE;
             }
         }
@@ -763,14 +849,14 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
             c[0] = pd[n];
 
             /* Write both hex chars of byte together */
-            if (!do_fwrite(c, 1, 1, wdh->fh, err)) {
+            if (!wtap_dump_file_write(wdh, c, 1, err)) {
                 return FALSE;
             }
         }
     }
 
     /* End the line */
-    if (! do_fwrite("\n", 1, 1, wdh->fh, err)) {
+    if (!wtap_dump_file_write(wdh, "\n", 1, err)) {
         return FALSE;
     }
 
@@ -778,17 +864,6 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
 }
 
 
-/******************************************************/
-/* Close a file we've been writing to.                */
-/******************************************************/
-static gboolean catapult_dct2000_dump_close(wtap_dumper *wdh _U_, int *err _U_)
-{
-    return TRUE;
-}
-
-
-
-
 /****************************/
 /* Private helper functions */
 /****************************/
@@ -799,20 +874,21 @@ static gboolean catapult_dct2000_dump_close(wtap_dumper *wdh _U_, int *err _U_)
 /* - on return 'offset' will point to the next position to read from  */
 /* - return TRUE if this read is successful                           */
 /**********************************************************************/
-gboolean read_new_line(FILE_T fh, gint64 *offset, gint *length,
-                       gchar *linebuff, size_t linebuffsize)
+static gboolean
+read_new_line(FILE_T fh, gint64 *offset, gint *length,
+              gchar *linebuff, size_t linebuffsize, int *err, gchar **err_info)
 {
-    char *result;
-
     /* Read in a line */
-    result = file_gets(linebuff, linebuffsize - 1, fh);
-    if (result == NULL) {
+    gint64 pos_before = file_tell(fh);
+
+    if (file_gets(linebuff, (int)linebuffsize - 1, fh) == NULL) {
         /* No characters found, or error */
+        *err = file_error(fh, err_info);
         return FALSE;
     }
 
-    /* Set length and offset.. */
-    *length = (gint)strlen(linebuff);
+    /* Set length (avoiding strlen()) and offset.. */
+    *length = (gint)(file_tell(fh) - pos_before);
     *offset = *offset + *length;
 
     /* ...but don't want to include newline in line length */
@@ -820,6 +896,11 @@ gboolean read_new_line(FILE_T fh, gint64 *offset, gint *length,
         linebuff[*length-1] = '\0';
         *length = *length - 1;
     }
+    /* Nor do we want '\r' (as will be written when log is created on windows) */
+    if (linebuff[*length-1] == '\r') {
+        linebuff[*length-1] = '\0';
+        *length = *length - 1;
+    }
 
     return TRUE;
 }
@@ -832,19 +913,20 @@ gboolean read_new_line(FILE_T fh, gint64 *offset, gint *length,
 /* - data position and length                                         */
 /* Return TRUE if this packet looks valid and can be displayed        */
 /**********************************************************************/
-static gboolean parse_line(gchar *linebuff, gint line_length,
-                           gint *seconds, gint *useconds,
-                           long *before_time_offset, long *after_time_offset,
-                           long *data_offset, gint *data_chars,
-                           packet_direction_t *direction,
-                           int *encap, int *is_comment,
-                           gchar *aal_header_chars,
-                           gchar *context_name, guint8 *context_portp,
-                           gchar *protocol_name, gchar *variant_name,
-                           gchar *outhdr_name)
+static gboolean
+parse_line(gchar *linebuff, gint line_length,
+           gint *seconds, gint *useconds,
+           long *before_time_offset, long *after_time_offset,
+           long *data_offset, gint *data_chars,
+           packet_direction_t *direction,
+           int *encap, int *is_comment, int *is_sprint,
+           gchar *aal_header_chars,
+           gchar *context_name, guint8 *context_portp,
+           gchar *protocol_name, gchar *variant_name,
+           gchar *outhdr_name)
 {
     int  n = 0;
-    int  port_digits = 0;
+    int  port_digits;
     char port_number_string[MAX_PORT_DIGITS+1];
     int  variant_digits = 0;
     int  variant = 1;
@@ -859,6 +941,7 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
     gboolean atm_header_present = FALSE;
 
     *is_comment = FALSE;
+    *is_sprint = FALSE;
 
     /* Read context name until find '.' */
     for (n=0; (linebuff[n] != '.') && (n < MAX_CONTEXT_NAME) && (n+1 < line_length); n++) {
@@ -871,7 +954,7 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
             }
 
             /* There is no variant, outhdr, etc.  Set protocol to be a comment */
-            g_snprintf(protocol_name, MAX_PROTOCOL_NAME, "comment");
+            g_strlcpy(protocol_name, "comment", MAX_PROTOCOL_NAME);
             *is_comment = TRUE;
             break;
         }
@@ -884,11 +967,10 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
         return FALSE;
     }
 
-    /* Reset strings (that won't be set be comments) */
-    g_strlcpy(variant_name, "0", MAX_VARIANT_DIGITS);
-    g_strlcpy(outhdr_name, "", MAX_OUTHDR_NAME);
-    port_digits = 1;
-    g_strlcpy(port_number_string, "0", MAX_PORT_DIGITS);
+    /* Reset strings (that won't be set by comments) */
+    variant_name[0] = '\0';
+    outhdr_name[0] = '\0';
+    port_number_string[0] = '\0';
 
     if (!(*is_comment)) {
         /* '.' must follow context name */
@@ -919,7 +1001,12 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
             return FALSE;
         }
         port_number_string[port_digits] = '\0';
-        *context_portp = atoi(port_number_string);
+        if (port_digits == 1) {
+            *context_portp = port_number_string[0] - '0';
+        }
+        else {
+            *context_portp = atoi(port_number_string);
+        }
         /* Skip it */
         n++;
 
@@ -960,12 +1047,19 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
         if (variant_digits > MAX_VARIANT_DIGITS || (n+1 >= line_length)) {
             return FALSE;
         }
+
         if (variant_digits > 0) {
             variant_name[variant_digits] = '\0';
-            variant = atoi(variant_name);
+            if (variant_digits == 1) {
+                variant = variant_name[0] - '0';
+            }
+            else {
+                variant = atoi(variant_name);
+            }
         }
         else {
-            g_strlcpy(variant_name, "1", MAX_VARIANT_DIGITS+1);
+            variant_name[0] = '1';
+            variant_name[1] = '\0';
         }
 
 
@@ -1009,10 +1103,7 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
 
     /* FP may be carried over ATM, which has separate atm header to parse */
     if ((strcmp(protocol_name, "fp") == 0) ||
-        (strcmp(protocol_name, "fp_r4") == 0) ||
-        (strcmp(protocol_name, "fp_r5") == 0) ||
-        (strcmp(protocol_name, "fp_r6") == 0) ||
-        (strcmp(protocol_name, "fp_r7") == 0)) {
+        (strncmp(protocol_name, "fp_r", 4) == 0)) {
 
         if ((variant > 256) && (variant % 256 == 3)) {
             /* FP over udp is contained in IPPrim... */
@@ -1030,7 +1121,6 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
         atm_header_present = TRUE;
     }
 
-
     else
     if (strcmp(protocol_name, "ppp") == 0) {
         *encap = WTAP_ENCAP_PPP;
@@ -1109,14 +1199,28 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
         }
     }
 
-    /* Scan ahead to the next space */
-    for (; (linebuff[n] != ' ') && (n+1 < line_length); n++);
-    if (n+1 >= line_length) {
-        return FALSE;
-    }
-    /* Skip it */
+    /* Skip next '/' */
     n++;
 
+    /* If there is a number, skip all info to next '/'.
+       TODO: for IP encapsulation, should store PDCP ueid, drb in pseudo info
+       and display dct2000 dissector... */
+    if (isdigit(linebuff[n])) {
+        while ((n+1 < line_length) && linebuff[n] != '/') {
+            n++;
+        }
+    }
+
+    /* Skip '/' */
+    while ((n+1 < line_length) && linebuff[n] == '/') {
+        n++;
+    }
+
+    /* Skip a space that may happen here */
+    if ((n+1 < line_length) && linebuff[n] == ' ') {
+        n++;
+    }
+
     /* Next character gives direction of message (must be 's' or 'r') */
     if (!(*is_comment)) {
         if (linebuff[n] == 's') {
@@ -1133,7 +1237,7 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
         n++;
     }
     else {
-        *direction = received;
+        *direction = sent;
     }
 
 
@@ -1209,16 +1313,26 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
         return FALSE;
     }
 
-    *after_time_offset = n;
+    *after_time_offset = n++;
 
-    /* Now skip ahead to find start of data (marked by '$') */
-    /* Want to avoid matching with normal sprint command output at the moment... */
-    for (; (linebuff[n] != '$') && (linebuff[n] != '\'') && (n+1 < line_length); n++);
-    if ((linebuff[n] == '\'') || (n+1 >= line_length)) {
-        return FALSE;
+    /* If we have a string message, it could either be a comment (with '$') or
+       a sprint line (no '$') */
+    if (*is_comment) {
+        if (strncmp(linebuff+n, "l $", 3) != 0) {
+            *is_sprint = TRUE;
+            g_strlcpy(protocol_name, "sprint", MAX_PROTOCOL_NAME);
+        }
+    }
+    
+    if (!(*is_sprint)) {
+        /* Now skip ahead to find start of data (marked by '$') */
+        for (; (linebuff[n] != '$') && (linebuff[n] != '\'') && (n+1 < line_length); n++);
+        if ((linebuff[n] == '\'') || (n+1 >= line_length)) {
+            return FALSE;
+        }
+        /* Skip it */
+        n++;
     }
-    /* Skip it */
-    n++;
 
     /* Set offset to data start within line */
     *data_offset = n;
@@ -1238,36 +1352,38 @@ static gboolean parse_line(gchar *linebuff, gint line_length,
 /*****************************************************************/
 /* Write the stub info to the data buffer while reading a packet */
 /*****************************************************************/
-static int write_stub_header(guchar *frame_buffer, char *timestamp_string,
-                             packet_direction_t direction, int encap,
-                             gchar *context_name, guint8 context_port,
-                             gchar *protocol_name, gchar *variant_name,
-                             gchar *outhdr_name)
+static int
+write_stub_header(guint8 *frame_buffer, char *timestamp_string,
+                  packet_direction_t direction, int encap,
+                  gchar *context_name, guint8 context_port,
+                  gchar *protocol_name, gchar *variant_name,
+                  gchar *outhdr_name)
 {
     int stub_offset = 0;
+    gsize length;
 
-    g_strlcpy((char*)frame_buffer, context_name, MAX_CONTEXT_NAME+1);
-    stub_offset += (int)(strlen(context_name) + 1);
+    length = g_strlcpy((char*)frame_buffer, context_name, MAX_CONTEXT_NAME+1);
+    stub_offset += (int)(length + 1);
 
     /* Context port number */
     frame_buffer[stub_offset] = context_port;
     stub_offset++;
 
     /* Timestamp within file */
-    g_strlcpy((char*)&frame_buffer[stub_offset], timestamp_string, MAX_TIMESTAMP_LEN+1);
-    stub_offset += (int)(strlen(timestamp_string) + 1);
+    length = g_strlcpy((char*)&frame_buffer[stub_offset], timestamp_string, MAX_TIMESTAMP_LEN+1);
+    stub_offset += (int)(length + 1);
 
     /* Protocol name */
-    g_strlcpy((char*)&frame_buffer[stub_offset], protocol_name, MAX_PROTOCOL_NAME+1);
-    stub_offset += (int)(strlen(protocol_name) + 1);
+    length = g_strlcpy((char*)&frame_buffer[stub_offset], protocol_name, MAX_PROTOCOL_NAME+1);
+    stub_offset += (int)(length + 1);
 
     /* Protocol variant number (as string) */
-    g_strlcpy((void*)&frame_buffer[stub_offset], variant_name, MAX_VARIANT_DIGITS+1);
-    stub_offset += (int)(strlen(variant_name) + 1);
+    length = g_strlcpy((gchar*)&frame_buffer[stub_offset], variant_name, MAX_VARIANT_DIGITS+1);
+    stub_offset += (int)(length + 1);
 
     /* Outhdr */
-    g_strlcpy((char*)&frame_buffer[stub_offset], outhdr_name, MAX_OUTHDR_NAME+1);
-    stub_offset += (int)(strlen(outhdr_name) + 1);
+    length = g_strlcpy((char*)&frame_buffer[stub_offset], outhdr_name, MAX_OUTHDR_NAME+1);
+    stub_offset += (int)(length + 1);
 
     /* Direction */
     frame_buffer[stub_offset] = direction;
@@ -1284,12 +1400,13 @@ static int write_stub_header(guchar *frame_buffer, char *timestamp_string,
 /**************************************************************/
 /* Set pseudo-header info depending upon packet encapsulation */
 /**************************************************************/
-static void set_pseudo_header_info(wtap *wth,
-                                   int pkt_encap,
-                                   gint64 file_offset,
-                                   union wtap_pseudo_header *pseudo_header,
-                                   packet_direction_t direction,
-                                   gchar *aal_header_chars)
+static void
+set_pseudo_header_info(wtap *wth,
+                       int pkt_encap,
+                       gint64 file_offset,
+                       union wtap_pseudo_header *pseudo_header,
+                       packet_direction_t direction,
+                       gchar *aal_header_chars)
 {
     pseudo_header->dct2000.seek_off = file_offset;
     pseudo_header->dct2000.wth = wth;
@@ -1315,9 +1432,10 @@ static void set_pseudo_header_info(wtap *wth,
 /*********************************************/
 /* Fill in atm pseudo-header with known info */
 /*********************************************/
-static void set_aal_info(union wtap_pseudo_header *pseudo_header,
-                         packet_direction_t direction,
-                         gchar *aal_header_chars)
+static void
+set_aal_info(union wtap_pseudo_header *pseudo_header,
+             packet_direction_t direction,
+             gchar *aal_header_chars)
 {
     /* 'aal_head_chars' has this format (for AAL2 at least):
        Global Flow Control (4 bits) | VPI (8 bits) | VCI (16 bits) |
@@ -1342,8 +1460,7 @@ static void set_aal_info(union wtap_pseudo_header *pseudo_header,
 
     /* vpi is 8 bits (2nd & 3rd nibble) */
     pseudo_header->dct2000.inner_pseudo_header.atm.vpi =
-        ((hex_from_char(aal_header_chars[1]) << 4) |
-          hex_from_char(aal_header_chars[2]));
+        hex_byte_from_chars(aal_header_chars+1);
 
     /* vci is next 16 bits */
     pseudo_header->dct2000.inner_pseudo_header.atm.vci =
@@ -1359,12 +1476,11 @@ static void set_aal_info(union wtap_pseudo_header *pseudo_header,
        case cid is derived from last char in ascii */
     if (isalnum((guchar)aal_header_chars[11])) {
         pseudo_header->dct2000.inner_pseudo_header.atm.aal2_cid =
-            ((hex_from_char(aal_header_chars[10]) << 4) |
-              hex_from_char(aal_header_chars[11]));
+            hex_byte_from_chars(aal_header_chars+10);
     }
     else {
         pseudo_header->dct2000.inner_pseudo_header.atm.aal2_cid =
-            (int)aal_header_chars[11] - 48;
+            (int)aal_header_chars[11] - '0';
     }
 }
 
@@ -1372,8 +1488,9 @@ static void set_aal_info(union wtap_pseudo_header *pseudo_header,
 /**********************************************/
 /* Fill in isdn pseudo-header with known info */
 /**********************************************/
-void set_isdn_info(union wtap_pseudo_header *pseudo_header,
-                   packet_direction_t direction)
+static void
+set_isdn_info(union wtap_pseudo_header *pseudo_header,
+              packet_direction_t direction)
 {
     /* This field is used to set the 'Source' and 'Destination' columns to
        'User' or 'Network'. If we assume that we're simulating the network,
@@ -1391,8 +1508,9 @@ void set_isdn_info(union wtap_pseudo_header *pseudo_header,
 /*********************************************/
 /* Fill in ppp pseudo-header with known info */
 /*********************************************/
-static void set_ppp_info(union wtap_pseudo_header *pseudo_header,
-                         packet_direction_t direction)
+static void
+set_ppp_info(union wtap_pseudo_header *pseudo_header,
+             packet_direction_t direction)
 {
     /* Set direction. */
     pseudo_header->dct2000.inner_pseudo_header.p2p.sent = (direction == sent);
@@ -1402,7 +1520,8 @@ static void set_ppp_info(union wtap_pseudo_header *pseudo_header,
 /********************************************************/
 /* Return hex nibble equivalent of hex string character */
 /********************************************************/
-guchar hex_from_char(gchar c)
+static guint8
+hex_from_char(gchar c)
 {
     if ((c >= '0') && (c <= '9')) {
         return c - '0';
@@ -1417,10 +1536,38 @@ guchar hex_from_char(gchar c)
 }
 
 
+
+/* Table allowing fast lookup from a pair of ascii hex characters to a guint8 */
+static guint8 s_tableValues[255][255];
+
+/* Prepare table values so ready so don't need to check inside hex_byte_from_chars() */
+static void  prepare_hex_byte_from_chars_table(void)
+{
+    guchar hex_char_array[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+                                  'a', 'b', 'c', 'd', 'e', 'f' };
+
+    gint i, j;
+    for (i=0; i < 16; i++) {
+        for (j=0; j < 16; j++) {
+            s_tableValues[hex_char_array[i]][hex_char_array[j]] = i*16 + j;
+        }
+    }
+}
+
+/* Extract and return a byte value from 2 ascii hex chars, starting from the given pointer */
+static guint8 hex_byte_from_chars(gchar *c)
+{
+    /* Return value from quick table lookup */
+    return s_tableValues[(unsigned char)c[0]][(unsigned char)c[1]];
+}
+
+
+
 /********************************************************/
 /* Return character corresponding to hex nibble value   */
 /********************************************************/
-gchar char_from_hex(guchar hex)
+static gchar
+char_from_hex(guint8 hex)
 {
     static char hex_lookup[16] =
     { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
@@ -1435,7 +1582,8 @@ gchar char_from_hex(guchar hex)
 /***********************************************/
 /* Equality test for packet prefix hash tables */
 /***********************************************/
-gint packet_offset_equal(gconstpointer v, gconstpointer v2)
+static gint
+packet_offset_equal(gconstpointer v, gconstpointer v2)
 {
     /* Dereferenced pointers must have same gint64 offset value */
     return (*(const gint64*)v == *(const gint64*)v2);
@@ -1445,9 +1593,10 @@ gint packet_offset_equal(gconstpointer v, gconstpointer v2)
 /********************************************/
 /* Hash function for packet-prefix hash table */
 /********************************************/
-guint packet_offset_hash_func(gconstpointer v)
+static guint
+packet_offset_hash_func(gconstpointer v)
 {
-    /* Use low-order bits of git64 offset value */
+    /* Use low-order bits of gint64 offset value */
     return (guint)(*(const gint64*)v);
 }
 
@@ -1457,7 +1606,8 @@ guint packet_offset_hash_func(gconstpointer v)
 /* Set secs and usecs as output                                         */
 /* Return FALSE if no valid time can be read                            */
 /************************************************************************/
-gboolean get_file_time_stamp(gchar *linebuff, time_t *secs, guint32 *usecs)
+static gboolean
+get_file_time_stamp(gchar *linebuff, time_t *secs, guint32 *usecs)
 {
     int n;
     struct tm tm;
@@ -1500,7 +1650,7 @@ gboolean get_file_time_stamp(gchar *linebuff, time_t *secs, guint32 *usecs)
 
     /********************************************************/
     /* Scan for remaining numerical fields                  */
-    scan_found = sscanf(linebuff+n, "%d, %d     %d:%d:%d.%u",
+    scan_found = sscanf(linebuff+n, "%2d, %4d     %2d:%2d:%2d.%4u",
                         &day, &year, &hour, &minute, &second, usecs);
     if (scan_found != 6) {
         /* Give up if not all found */
@@ -1526,8 +1676,9 @@ gboolean get_file_time_stamp(gchar *linebuff, time_t *secs, guint32 *usecs)
 }
 
 /* Free the data allocated inside a line_prefix_info_t */
-gboolean free_line_prefix_info(gpointer key, gpointer value,
-                               gpointer user_data _U_)
+static gboolean
+free_line_prefix_info(gpointer key, gpointer value,
+                      gpointer user_data _U_)
 {
     line_prefix_info_t *info = (line_prefix_info_t*)value;
 
@@ -1546,4 +1697,3 @@ gboolean free_line_prefix_info(gpointer key, gpointer value,
     /* Item will always be removed from table */
     return TRUE;
 }
-