Move the new files to the same places as in automake.
[obnox/wireshark/wip.git] / wiretap / catapult_dct2000.c
index eaf5cb3862d6c6a91a4c08d4d97dfdaa8a85b7d7..041ee83275cfc6349c4ecdefb9be62d92d86f468 100644 (file)
@@ -111,13 +111,13 @@ 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,
+                                           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);
+                                      const guint8 *pd, int *err);
 
 
 /************************************************************/
@@ -135,13 +135,14 @@ static gboolean parse_line(char *linebuff, gint line_length,
                            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 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,
@@ -168,14 +169,15 @@ 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 _U_)
 {
     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];
 
     /* Clear errno before reading from the file */
     errno = 0;
@@ -268,7 +270,8 @@ int catapult_dct2000_open(wtap *wth, int *err, gchar **err_info _U_)
 /* 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_,
+static gboolean
+catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
                                gint64 *data_offset)
 {
     gint64 offset = wth->data_offset;
@@ -290,7 +293,7 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
         int line_length, seconds, useconds, data_chars;
         int is_comment = 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;
@@ -322,7 +325,7 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
                        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;
@@ -331,6 +334,8 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
 
             g_snprintf(timestamp_string, MAX_TIMESTAMP_LEN, "%d.%04d", 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;
 
@@ -382,8 +387,7 @@ gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
                 /* Copy data into buffer, converting from ascii hex */
                 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 {
@@ -444,12 +448,12 @@ 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,
+                           union wtap_pseudo_header *pseudo_header, guint8 *pd,
                            int length, int *err, gchar **err_info)
 {
     gint64 offset = wth->data_offset;
     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;
@@ -505,8 +509,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 {
@@ -537,7 +540,8 @@ 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 =
@@ -571,7 +575,8 @@ typedef struct {
 /* 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;
@@ -583,7 +588,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:
@@ -601,9 +607,10 @@ int catapult_dct2000_dump_can_write_encap(int encap)
 /* Write a single packet out to the file */
 /*****************************************/
 
-gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
+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)
+                               const guint8 *pd, int *err)
 {
     guint32 n;
     line_prefix_info_t *prefix = NULL;
@@ -741,8 +748,8 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
         /* 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 (!wtap_dump_file_write(wdh, c, 2, err)) {
@@ -781,18 +788,20 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
 /* - 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,
+static gboolean
+read_new_line(FILE_T fh, gint64 *offset, gint *length,
                        gchar *linebuff, size_t linebuffsize)
 {
     /* Read in a line */
+    gint64 pos_before = file_tell(fh);
     char *result = file_gets(linebuff, (int)linebuffsize - 1, fh);
     if (result == NULL) {
         /* No characters found, or error */
         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 */
@@ -812,7 +821,8 @@ 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,
+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,
@@ -1010,7 +1020,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;
@@ -1089,14 +1098,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') {
@@ -1218,7 +1241,8 @@ 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,
+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,
@@ -1264,7 +1288,8 @@ 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,
+static void
+set_pseudo_header_info(wtap *wth,
                                    int pkt_encap,
                                    gint64 file_offset,
                                    union wtap_pseudo_header *pseudo_header,
@@ -1295,7 +1320,8 @@ 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,
+static void
+set_aal_info(union wtap_pseudo_header *pseudo_header,
                          packet_direction_t direction,
                          gchar *aal_header_chars)
 {
@@ -1322,8 +1348,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 =
@@ -1339,8 +1364,7 @@ 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 =
@@ -1352,7 +1376,8 @@ 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,
+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
@@ -1371,7 +1396,8 @@ 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,
+static void
+set_ppp_info(union wtap_pseudo_header *pseudo_header,
                          packet_direction_t direction)
 {
     /* Set direction. */
@@ -1382,7 +1408,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';
@@ -1396,11 +1423,38 @@ guchar hex_from_char(gchar c)
     return 0xff;
 }
 
+/* Extract and return a byte value from 2 ascii hex chars, starting from the given pointer */
+static guint8
+hex_byte_from_chars(gchar *c)
+{
+    static guchar hex_char_array[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+                                         'a', 'b', 'c', 'd', 'e', 'f' };
+
+    /* Populate lookup table first time */
+    static guint8 tableValues[255][255];
+    static gint tableSet = FALSE;
+    if (!tableSet) {
+        gint i, j;
+        for (i=0; i < 16; i++) {
+            for (j=0; j < 16; j++) {
+                tableValues[hex_char_array[i]][hex_char_array[j]] = i*16 + j;
+            }
+        }
+
+        tableSet = TRUE;
+    }
+
+    /* Return value from quick table lookup */
+    return 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'};
@@ -1415,7 +1469,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);
@@ -1425,7 +1480,8 @@ 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 */
     return (guint)(*(const gint64*)v);
@@ -1437,7 +1493,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;
@@ -1480,7 +1537,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 */
@@ -1506,7 +1563,8 @@ 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,
+static gboolean
+free_line_prefix_info(gpointer key, gpointer value,
                                gpointer user_data _U_)
 {
     line_prefix_info_t *info = (line_prefix_info_t*)value;