Move some code (including the optional objects) into libwsutil
[obnox/wireshark/wip.git] / wiretap / k12.c
index 5eb21d57211ebd0581435faaa95eda6a6e26c139..99d8aae596351e3034a0152219ee33d748aac7ba 100644 (file)
@@ -3,7 +3,7 @@
  *
  *  routines for importing tektronix k12xx *.rf5 files
  *
- *  Copyright (c) 2005, Luis E. Garia Ontanon <luis.ontanon@gmail.com>
+ *  Copyright (c) 2005, Luis E. Garia Ontanon <luis@ontanon.org>
  *
  * $Id$
  *
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
-/* #define DEBUG_K12 */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+
 #include "wtap-int.h"
 #include "wtap.h"
 #include "file_wrappers.h"
 #include "buffer.h"
 #include "k12.h"
 
-/*#define DEBUG_K12*/
+#include <wsutil/str_util.h>
+
+/* #define DEBUG_K12 */
 #ifdef DEBUG_K12
 #include <stdio.h>
 #include <ctype.h>
 #include <stdarg.h>
-
+#include <wsutil/file_util.h>
 
 FILE* dbg_out = NULL;
 char* env_file = NULL;
@@ -88,7 +90,7 @@ void k12_hexdump(guint level, gint64 offset, char* label, unsigned char* b, unsi
 
     if (debug_level < level) return;
 
-    fprintf(dbg_out,"%s(%.8llx,%.4x): ",label,offset,len);
+    fprintf(dbg_out,"%s(%.8" G_GINT64_MODIFIER "x,%.4x): ",label,offset,len);
 
     for (i=0 ; i<len ; i++) {
 
@@ -97,7 +99,7 @@ void k12_hexdump(guint level, gint64 offset, char* label, unsigned char* b, unsi
         else if (!(i%4))
             fprintf(dbg_out," ");
 
-        fprintf(dbg_out,c2t[b[i]]);
+        fprintf(dbg_out, "%s", c2t[b[i]]);
     }
 
        fprintf(dbg_out,"\n");
@@ -126,7 +128,7 @@ void k12_hexdump(guint level, gint64 offset, char* label, unsigned char* b, unsi
 
 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
 
-struct _k12_t {
+typedef struct {
     guint32 file_len;
     guint32 num_of_records; /* XXX: not sure about this */
 
@@ -134,7 +136,7 @@ struct _k12_t {
     GHashTable* src_by_name; /* k12_srcdsc_recs by stack_name */
 
     Buffer extra_info; /* Buffer to hold per packet extra information */
-};
+} k12_t;
 
 typedef struct _k12_src_desc_t {
     guint32 input;
@@ -208,112 +210,118 @@ typedef struct _k12_src_desc_t {
  * XXX: works at most with 0x1FFF bytes per record
  */
 static gint get_record(guint8** bufferp, FILE* fh, gint64 file_offset) {
-       static guint8* buffer = NULL;
-       static guint buffer_len = 0x2000 ;
-    guint read;
-       guint last_read;
+    static guint8* buffer = NULL;
+    static guint buffer_len = 0x2000 ;
+    guint bytes_read;
+    guint last_read;
     guint actual_len, left;
-       guint8 junk[0x14];
-       guint8* writep;
+    guint8 junk[0x14];
+    guint8* writep;
 
-       /* where the next unknown 0x10 bytes are stuffed to the file */
-       guint junky_offset = 0x2000 - (gint) ( (file_offset - 0x200) % 0x2000 );
+    /* where the next unknown 0x10 bytes are stuffed to the file */
+    guint junky_offset = 0x2000 - (gint) ( (file_offset - 0x200) % 0x2000 );
 
-       K12_DBG(6,("get_record: ENTER: junky_offset=%" G_GINT64_MODIFIER "d, file_offset=%" G_GINT64_MODIFIER "d",junky_offset,file_offset));
+    K12_DBG(6,("get_record: ENTER: junky_offset=%" G_GINT64_MODIFIER "d, file_offset=%" G_GINT64_MODIFIER "d",junky_offset,file_offset));
 
-       /* no buffer is given, lets create it */
-       if (buffer == NULL) {
-               buffer = g_malloc(0x2000);
-               buffer_len = 0x2000;
-       }
+    /* no buffer is given, lets create it */
+    if (buffer == NULL) {
+        buffer = g_malloc(0x2000);
+        buffer_len = 0x2000;
+    }
 
-       *bufferp = buffer;
+    *bufferp = buffer;
 
-       if  ( junky_offset == 0x2000 ) {
-               /* the length of the record is 0x10 bytes ahead from we are reading */
-               read = file_read(junk,1,0x14,fh);
+    if  ( junky_offset == 0x2000 ) {
+        /* the length of the record is 0x10 bytes ahead from we are reading */
+        bytes_read = file_read(junk,1,0x14,fh);
 
-        if (read == 2 && junk[0] == 0xff && junk[1] == 0xff) {
+        if (bytes_read == 2 && junk[0] == 0xff && junk[1] == 0xff) {
             K12_DBG(1,("get_record: EOF"));
             return 0;
-        } else if ( read < 0x14 ){
+        } else if ( bytes_read < 0x14 ){
             K12_DBG(1,("get_record: SHORT READ"));
             return -1;
         }
 
-               memcpy(buffer,&(junk[0x10]),4);
-       } else {
-               /* the length of the record is right where we are reading */
-               read = file_read(buffer,1, 0x4, fh);
+        memcpy(buffer,&(junk[0x10]),4);
+    } else {
+        /* the length of the record is right where we are reading */
+        bytes_read = file_read(buffer,1, 0x4, fh);
 
-               if (read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
+        if (bytes_read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
             K12_DBG(1,("get_record: EOF"));
             return 0;
-        } else if ( read != 0x4 ) {
+        } else if ( bytes_read != 0x4 ) {
             K12_DBG(1,("get_record: SHORT READ"));
             return -1;
         }
-       }
+    }
 
-       actual_len = left = pntohl(buffer);
-       junky_offset -= 0x4;
+    actual_len = left = pntohl(buffer);
+    junky_offset -= 0x4;
 
-       K12_DBG(5,("get_record: GET length=%d",left));
+    K12_DBG(5,("get_record: GET length=%u",left));
 
-       g_assert(left >= 4);
+    /* XXX - Is WTAP_MAX_PACKET_SIZE */
+    if (left < 4 || left > WTAP_MAX_PACKET_SIZE) {
+        K12_DBG(1,("get_record: Invalid GET length=%u",left));
+        errno = WTAP_ERR_BAD_RECORD;
+        return -1;
+    }
 
-       while (left > buffer_len) *bufferp = buffer = g_realloc(buffer,buffer_len*=2);
+    while (left > buffer_len) *bufferp = buffer = g_realloc(buffer,buffer_len*=2);
 
-       writep = buffer + 4;
-       left -= 4;
+    writep = buffer + 4;
+    left -= 4;
 
-       do {
-               K12_DBG(6,("get_record: looping left=%d junky_offset=%" G_GINT64_MODIFIER "d",left,junky_offset));
+    do {
+        K12_DBG(6,("get_record: looping left=%d junky_offset=%" G_GINT64_MODIFIER "d",left,junky_offset));
 
-               if (junky_offset > left) {
-                       read += last_read = file_read(writep,1, left, fh);
+        if (junky_offset > left) {
+            bytes_read += last_read = file_read(writep,1, left, fh);
 
-                       if ( last_read != left ) {
-                               K12_DBG(1,("get_record: SHORT READ"));
-                               return -1;
-                       } else {
-                               K12_HEXDMP(5,file_offset, "GOT record", buffer, actual_len);
-                               return read;
-                       }
-               } else {
-                       read += last_read = file_read(writep,1, junky_offset, fh);
+            if ( last_read != left ) {
+                K12_DBG(1,("get_record: SHORT READ"));
+                return -1;
+            } else {
+                K12_HEXDMP(5,file_offset, "GOT record", buffer, actual_len);
+                return bytes_read;
+            }
+        } else {
+            bytes_read += last_read = file_read(writep,1, junky_offset, fh);
 
-                       if ( last_read != junky_offset ) {
-                               K12_DBG(1,("get_record: SHORT READ, read=%d expected=%d",last_read, junky_offset));
-                               return -1;
-                       }
+            if ( last_read != junky_offset ) {
+                K12_DBG(1,("get_record: SHORT READ, read=%d expected=%d",last_read, junky_offset));
+                return -1;
+            }
 
-                       writep += last_read;
+            writep += last_read;
 
-                       read += last_read = file_read(junk,1, 0x10, fh);
+            bytes_read += last_read = file_read(junk,1, 0x10, fh);
 
-                       if ( last_read != 0x10 ) {
-                               K12_DBG(1,("get_record: SHORT READ"));
-                               return -1;
-                       }
+            if ( last_read != 0x10 ) {
+                K12_DBG(1,("get_record: SHORT READ"));
+                return -1;
+            }
 
-                       left -= junky_offset;
-                       junky_offset = 0x2000;
-               }
+            left -= junky_offset;
+            junky_offset = 0x2000;
+        }
 
-       } while(left);
+    } while(left);
 
-       K12_HEXDMP(5,file_offset, "GOT record", buffer, actual_len);
-       return read;
+    K12_HEXDMP(5,file_offset, "GOT record", buffer, actual_len);
+    return bytes_read;
 }
 
 static gboolean k12_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data_offset) {
+    k12_t *k12 = (k12_t *)wth->priv;
     k12_src_desc_t* src_desc;
     guint8* buffer = NULL;
     gint64 offset;
-    long len;
+    gint len;
     guint32 type;
-       guint32 src_id;
+    guint32 src_id;
     guint64 ts;
     guint32 extra_len;
 
@@ -321,7 +329,7 @@ static gboolean k12_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data
 
     /* ignore the record if it isn't a packet */
     do {
-               K12_DBG(5,("k12_read: offset=%i",offset));
+        K12_DBG(5,("k12_read: offset=%i",offset));
 
         *data_offset = offset;
 
@@ -339,16 +347,16 @@ static gboolean k12_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data
         src_id = pntohl(buffer + K12_RECORD_SRC_ID);
 
 
-        if ( ! (src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(src_id))) ) {
-                       /*
-                        * Some records from K15 files have a port ID of an undeclared
-                        * interface which happens to be the only one with the first byte changed.
-                        * It is still unknown how to recognize when this happens.
-                        * If the lookup of the interface record fails we'll mask it
-                        * and retry.
-                        */
-                       src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(src_id&K12_RECORD_SRC_ID_MASK));
-               }
+        if ( ! (src_desc = g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id))) ) {
+            /*
+             * Some records from K15 files have a port ID of an undeclared
+             * interface which happens to be the only one with the first byte changed.
+             * It is still unknown how to recognize when this happens.
+             * If the lookup of the interface record fails we'll mask it
+             * and retry.
+             */
+            src_desc = g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id&K12_RECORD_SRC_ID_MASK));
+        }
 
         K12_DBG(5,("k12_read: record type=%x src_id=%x",type,src_id));
 
@@ -373,46 +381,47 @@ static gboolean k12_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data
     memcpy(buffer_start_ptr(wth->frame_buffer), buffer + K12_PACKET_FRAME, wth->phdr.caplen);
 
     /* extra information need by some protocols */
-    buffer_assure_space(&(wth->capture.k12->extra_info), extra_len);
-    memcpy(buffer_start_ptr(&(wth->capture.k12->extra_info)),
+    buffer_assure_space(&(k12->extra_info), extra_len);
+    memcpy(buffer_start_ptr(&(k12->extra_info)),
            buffer + K12_PACKET_FRAME + wth->phdr.caplen, extra_len);
-    wth->pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(wth->capture.k12->extra_info));
+    wth->pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(k12->extra_info));
     wth->pseudo_header.k12.extra_length = extra_len;
 
     wth->pseudo_header.k12.input = src_id;
 
     K12_DBG(5,("k12_read: wth->pseudo_header.k12.input=%x wth->phdr.len=%i input_name='%s' stack_file='%s' type=%x",
-                          wth->pseudo_header.k12.input,wth->phdr.len,src_desc->input_name,src_desc->stack_file,src_desc->input_type));\
-
-       wth->pseudo_header.k12.input_name = src_desc->input_name;
-       wth->pseudo_header.k12.stack_file = src_desc->stack_file;
-       wth->pseudo_header.k12.input_type = src_desc->input_type;
-
-       switch(src_desc->input_type) {
-               case K12_PORT_ATMPVC:
-               if ((long)(K12_PACKET_FRAME + wth->phdr.len + K12_PACKET_OFFSET_CID) < len) {
-                       wth->pseudo_header.k12.input_info.atm.vp =  pntohs(buffer + (K12_PACKET_FRAME + wth->phdr.caplen + K12_PACKET_OFFSET_VP));
-                       wth->pseudo_header.k12.input_info.atm.vc =  pntohs(buffer + (K12_PACKET_FRAME + wth->phdr.caplen + K12_PACKET_OFFSET_VC));
-                       wth->pseudo_header.k12.input_info.atm.cid =  *((unsigned char*)(buffer + K12_PACKET_FRAME + wth->phdr.len + K12_PACKET_OFFSET_CID));
-                       break;
-               }
-               /* Fall through */
-               default:
-               memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
-               break;
+               wth->pseudo_header.k12.input,wth->phdr.len,src_desc->input_name,src_desc->stack_file,src_desc->input_type));\
 
-       }
+    wth->pseudo_header.k12.input_name = src_desc->input_name;
+    wth->pseudo_header.k12.stack_file = src_desc->stack_file;
+    wth->pseudo_header.k12.input_type = src_desc->input_type;
+
+    switch(src_desc->input_type) {
+        case K12_PORT_ATMPVC:
+        if ((long)(K12_PACKET_FRAME + wth->phdr.len + K12_PACKET_OFFSET_CID) < len) {
+            wth->pseudo_header.k12.input_info.atm.vp =  pntohs(buffer + (K12_PACKET_FRAME + wth->phdr.caplen + K12_PACKET_OFFSET_VP));
+            wth->pseudo_header.k12.input_info.atm.vc =  pntohs(buffer + (K12_PACKET_FRAME + wth->phdr.caplen + K12_PACKET_OFFSET_VC));
+            wth->pseudo_header.k12.input_info.atm.cid =  *((unsigned char*)(buffer + K12_PACKET_FRAME + wth->phdr.len + K12_PACKET_OFFSET_CID));
+            break;
+        }
+        /* Fall through */
+        default:
+        memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
+        break;
 
-    wth->pseudo_header.k12.stuff = wth->capture.k12;
+    }
+
+    wth->pseudo_header.k12.stuff = k12;
 
     return TRUE;
 }
 
 
 static gboolean k12_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err _U_, gchar **err_info _U_) {
+    k12_t *k12 = (k12_t *)wth->priv;
     k12_src_desc_t* src_desc;
     guint8* buffer;
-    long len;
+    gint len;
     guint32 extra_len;
     guint32 input;
 
@@ -431,20 +440,20 @@ static gboolean k12_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_head
     memcpy(pd, buffer + K12_PACKET_FRAME, length);
 
     extra_len = len - K12_PACKET_FRAME - length;
-    buffer_assure_space(&(wth->capture.k12->extra_info), extra_len);
-    memcpy(buffer_start_ptr(&(wth->capture.k12->extra_info)),
+    buffer_assure_space(&(k12->extra_info), extra_len);
+    memcpy(buffer_start_ptr(&(k12->extra_info)),
            buffer + K12_PACKET_FRAME + length, extra_len);
-    wth->pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(wth->capture.k12->extra_info));
+    wth->pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(k12->extra_info));
     wth->pseudo_header.k12.extra_length = extra_len;
     if (pseudo_header) {
-        pseudo_header->k12.extra_info = (void*)buffer_start_ptr(&(wth->capture.k12->extra_info));
+        pseudo_header->k12.extra_info = (void*)buffer_start_ptr(&(k12->extra_info));
         pseudo_header->k12.extra_length = extra_len;
     }
 
     input = pntohl(buffer + K12_RECORD_SRC_ID);
     K12_DBG(5,("k12_seek_read: input=%.8x",input));
 
-       if ( ! (src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(input))) ) {
+       if ( ! (src_desc = g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(input))) ) {
                /*
                 * Some records from K15 files have a port ID of an undeclared
                 * interface which happens to be the only one with the first byte changed.
@@ -452,7 +461,7 @@ static gboolean k12_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_head
                 * If the lookup of the interface record fails we'll mask it
                 * and retry.
                 */
-               src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(input&K12_RECORD_SRC_ID_MASK));
+               src_desc = g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(input&K12_RECORD_SRC_ID_MASK));
        }
 
     if (src_desc) {
@@ -512,11 +521,11 @@ static gboolean k12_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_head
 
     if (pseudo_header) {
         pseudo_header->k12.input = input;
-        pseudo_header->k12.stuff = wth->capture.k12;
+        pseudo_header->k12.stuff = k12;
     }
 
     wth->pseudo_header.k12.input = input;
-    wth->pseudo_header.k12.stuff = wth->capture.k12;
+    wth->pseudo_header.k12.stuff = k12;
 
     K12_DBG(5,("k12_seek_read: DONE OK"));
 
@@ -540,12 +549,8 @@ static k12_t* new_k12_file_data(void) {
 static gboolean destroy_srcdsc(gpointer k _U_, gpointer v, gpointer p _U_) {
     k12_src_desc_t* rec = v;
 
-    if(rec->input_name)
-        g_free(rec->input_name);
-
-    if(rec->stack_file)
-        g_free(rec->stack_file);
-
+    g_free(rec->input_name);
+    g_free(rec->stack_file);
     g_free(rec);
 
     return TRUE;
@@ -560,7 +565,10 @@ static void destroy_k12_file_data(k12_t* fd) {
 }
 
 static void k12_close(wtap *wth) {
-    destroy_k12_file_data(wth->capture.k12);
+    k12_t *k12 = (k12_t *)wth->priv;
+
+    destroy_k12_file_data(k12);
+    wth->priv = NULL;  /* destroy_k12_file_data freed it */
 #ifdef DEBUG_K12
     K12_DBG(5,("k12_close: CLOSED"));
     if (env_file) fclose(dbg_out);
@@ -585,7 +593,7 @@ int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
 #ifdef DEBUG_K12
     gchar* env_level = getenv("K12_DEBUG_LEVEL");
     env_file = getenv("K12_DEBUG_FILENAME");
-    if ( env_file ) dbg_out = eth_fopen(env_file,"w");
+    if ( env_file ) dbg_out = ws_fopen(env_file,"w");
     else dbg_out = stderr;
     if ( env_level ) debug_level = strtoul(env_level,NULL,10);
     K12_DBG(1,("k12_open: ENTER debug_level=%u",debug_level));
@@ -619,6 +627,8 @@ int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
 
         if ( len <= 0 ) {
             K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
+            destroy_k12_file_data(file_data);
+            g_free(file_data);
             return -1;
         }
 
@@ -631,6 +641,7 @@ int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
              */
             if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
                 destroy_k12_file_data(file_data);
+                g_free(file_data);
                 return -1;
             }
             K12_DBG(5,("k12_open: FIRST PACKET offset=%x",offset));
@@ -652,6 +663,8 @@ int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
                 g_free(rec);
                 K12_DBG(5,("k12_open: failed (name_len == 0 || stack_len == 0 "
                         "|| 0x20 + extra_len + name_len + stack_len > rec_len)  extra_len=%i name_len=%i stack_len=%i"));
+                destroy_k12_file_data(file_data);
+                g_free(file_data);
                 return 0;
             }
 
@@ -681,10 +694,13 @@ int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
                     rec->input_type = K12_PORT_ATMPVC;
             }
 
+            /* XXX - this is assumed, in a number of places (not just in the
+               ascii_strdown_inplace() call below) to be null-terminated;
+               is that guaranteed (even with a corrupt file)? */
             rec->input_name = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len, name_len);
             rec->stack_file = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len + name_len, stack_len);
 
-            g_strdown(rec->stack_file);
+            ascii_strdown_inplace (rec->stack_file);
 
             g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec);
             g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec);
@@ -704,12 +720,18 @@ int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
     wth->subtype_read = k12_read;
     wth->subtype_seek_read = k12_seek_read;
     wth->subtype_close = k12_close;
-    wth->capture.k12 = file_data;
+    wth->priv = (void *)file_data;
     wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
 
     return 1;
 }
 
+typedef struct {
+       guint32 file_len;
+       guint32 num_of_records;
+       guint32 file_offset;
+} k12_dump_t;
+
 int k12_dump_can_write_encap(int encap) {
 
     if (encap == WTAP_ENCAP_PER_PACKET)
@@ -737,8 +759,9 @@ static gboolean do_fwrite(const void *data, size_t size, size_t count, FILE *str
     return TRUE;
 }
 
-static gboolean k12_dump_record(wtap_dumper *wdh, long len,  guint8* buffer, int *err_p) {
-    long junky_offset = (0x2000 - ( (wdh->dump.k12->file_offset - 0x200) % 0x2000 )) % 0x2000;
+static gboolean k12_dump_record(wtap_dumper *wdh, guint32 len,  guint8* buffer, int *err_p) {
+    k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
+    guint32 junky_offset = (0x2000 - ( (k12->file_offset - 0x200) % 0x2000 )) % 0x2000;
 
     if (len > junky_offset) {
         if (junky_offset) {
@@ -751,14 +774,14 @@ static gboolean k12_dump_record(wtap_dumper *wdh, long len,  guint8* buffer, int
         if (! do_fwrite(buffer+junky_offset, 1, len - junky_offset, wdh->fh, err_p))
             return FALSE;
 
-        wdh->dump.k12->file_offset += len + 0x10;
+        k12->file_offset += len + 0x10;
     } else {
         if (! do_fwrite(buffer, 1, len, wdh->fh, err_p))
             return FALSE;
-        wdh->dump.k12->file_offset += len;
+        k12->file_offset += len;
     }
 
-    wdh->dump.k12->num_of_records++;
+    k12->num_of_records++;
     return TRUE;
 }
 
@@ -826,8 +849,8 @@ static void k12_dump_src_setting(gpointer k _U_, gpointer v, gpointer p) {
     }
 
     obj.record.unk16_1 = g_htons(0x0000);
-    obj.record.name_len = strlen(src_desc->input_name) + 1;
-    obj.record.stack_len = strlen(src_desc->stack_file) + 1;
+    obj.record.name_len = (guint16) strlen(src_desc->input_name) + 1;
+    obj.record.stack_len = (guint16) strlen(src_desc->stack_file) + 1;
 
     obj.record.extra.type = g_htonl(src_desc->input_type);
 
@@ -873,7 +896,8 @@ static void k12_dump_src_setting(gpointer k _U_, gpointer v, gpointer p) {
 static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
                          const union wtap_pseudo_header *pseudo_header,
                          const guchar *pd, int *err) {
-    long len;
+    k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
+    guint32 len;
     union {
         guint8 buffer[0x2000];
         struct {
@@ -890,7 +914,7 @@ static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
         } record;
     } obj;
 
-    if (wdh->dump.k12->num_of_records == 0) {
+    if (k12->num_of_records == 0) {
         k12_t* file_data = pseudo_header->k12.stuff;
         /* XXX: We'll assume that any fwrite errors in k12_dump_src_setting will    */
         /*      repeat during the final k12_dump_record at the end of k12_dump      */
@@ -920,6 +944,7 @@ static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
 static const guint8 k12_eof[] = {0xff,0xff};
 
 static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
+    k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
     union {
         guint8 b[sizeof(guint32)];
         guint32 u;
@@ -933,12 +958,12 @@ static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
         return FALSE;
     }
 
-    d.u = g_htonl(wdh->dump.k12->file_len);
+    d.u = g_htonl(k12->file_len);
 
     if (! do_fwrite(d.b, 1, 4, wdh->fh, err))
         return FALSE;
 
-    d.u = g_htonl(wdh->dump.k12->num_of_records);
+    d.u = g_htonl(k12->num_of_records);
 
     if (! do_fwrite(d.b, 1, 4, wdh->fh, err))
         return FALSE;
@@ -948,6 +973,7 @@ static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
 
 
 gboolean k12_dump_open(wtap_dumper *wdh, gboolean cant_seek, int *err) {
+    k12_dump_t *k12;
 
     if (cant_seek) {
         *err = WTAP_ERR_CANT_WRITE_TO_PIPE;
@@ -966,10 +992,11 @@ gboolean k12_dump_open(wtap_dumper *wdh, gboolean cant_seek, int *err) {
     wdh->subtype_write = k12_dump;
     wdh->subtype_close = k12_dump_close;
 
-    wdh->dump.k12 = g_malloc(sizeof(k12_dump_t));
-    wdh->dump.k12->file_len = 0x200;
-    wdh->dump.k12->num_of_records = 0;
-    wdh->dump.k12->file_offset  = 0x200;
+    k12 = (k12_dump_t *)g_malloc(sizeof(k12_dump_t));
+    wdh->priv = (void *)k12;
+    k12->file_len = 0x200;
+    k12->num_of_records = 0;
+    k12->file_offset  = 0x200;
 
     return TRUE;
 }