Get rid of trailing blank lines.
[metze/wireshark/wip.git] / wiretap / k12.c
1 /*
2  * k12.c
3  *
4  *  routines for importing tektronix k12xx *.rf5 files
5  *
6  *  Copyright (c) 2005, Luis E. Garia Ontanon <luis@ontanon.org>
7  *
8  * Wiretap Library
9  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include "wtap-int.h"
32 #include "wtap.h"
33 #include "file_wrappers.h"
34 #include "buffer.h"
35 #include "k12.h"
36
37 #include <wsutil/str_util.h>
38
39 /*
40  * See
41  *
42  *  http://www.tek.com/manual/record-file-api-programmer-manual
43  *
44  * for some information about the file format.  You may have to fill in
45  * a form to download the document ("Record File API Programmer Manual").
46  *
47  * Unfortunately, it describes an API that delivers records from an rf5
48  * file, not the raw format of an rf5 file, so, while it gives the formats
49  * of the records with various types, it does not indicate how those records
50  * are stored in the file.
51  */
52
53 /* #define DEBUG_K12 */
54 #ifdef DEBUG_K12
55 #include <stdio.h>
56 #include <ctype.h>
57 #include <stdarg.h>
58 #include <wsutil/file_util.h>
59
60 FILE* dbg_out = NULL;
61 char* env_file = NULL;
62
63 static unsigned int debug_level = 0;
64
65 void k12_fprintf(const char* fmt, ...) {
66     va_list ap;
67
68     va_start(ap,fmt);
69     vfprintf(dbg_out, fmt, ap);
70     va_end(ap);
71 }
72
73 #define CAT(a,b) a##b
74 #define K12_DBG(level,args) do { if (level <= debug_level) { \
75         fprintf(dbg_out,"%s:%d: ",CAT(__FI,LE__),CAT(__LI,NE__)); \
76         k12_fprintf args ; \
77         fprintf(dbg_out,"\n"); \
78 } } while(0)
79
80 void k12_hex_ascii_dump(guint level, gint64 offset, const char* label, const unsigned char* b, unsigned int len) {
81     static const char* c2t[] = {
82         "00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f",
83         "10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f",
84         "20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f",
85         "30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f",
86         "40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f",
87         "50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f",
88         "60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f",
89         "70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f",
90         "80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f",
91         "90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f",
92         "a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af",
93         "b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf",
94         "c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf",
95         "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df",
96         "e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef",
97         "f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"
98     };
99     unsigned int i, j;
100
101     if (debug_level < level) return;
102
103     fprintf(dbg_out,"%s(%.8" G_GINT64_MODIFIER "x,%.4x):\n",label,offset,len);
104
105     for (i=0 ; i<len ; i += 16) {
106         for (j=0; j<16; j++) {
107             if ((j%4)==0)
108                 fprintf(dbg_out," ");
109             if ((i+j)<len)
110                 fprintf(dbg_out, "%s", c2t[b[i+j]]);
111             else
112                 fprintf(dbg_out, "  ");
113         }
114         fprintf(dbg_out, "    ");
115         for (j=0; j<16; j++) {
116             if ((i+j)<len)
117                 fprintf(dbg_out, "%c", g_ascii_isprint(b[i+j]) ? b[i+j] : '.');
118         }
119         fprintf(dbg_out,"\n");
120     }
121 }
122
123 #define K12_HEX_ASCII_DUMP(x,a,b,c,d) k12_hex_ascii_dump(x,a,b,c,d)
124
125 void k12_ascii_dump(guint level, guint8 *buf, guint32 len, guint32 buf_offset) {
126     guint32 i;
127
128     if (debug_level < level) return;
129
130     for (i = buf_offset; i < len; i++) {
131         if (g_ascii_isprint(buf[i]) || buf[i] == '\n' || buf[i] == '\t')
132             putc(buf[i], dbg_out);
133         else if (buf[i] == '\0')
134             fprintf(dbg_out, "(NUL)\n");
135     }
136 }
137
138 #define K12_ASCII_DUMP(x,a,b,c) k12_ascii_dump(x,a,b,c)
139
140 #else
141 #define K12_DBG(level,args) (void)0
142 #define K12_HEX_ASCII_DUMP(x,a,b,c,d)
143 #define K12_ASCII_DUMP(x,a,b,c)
144 #endif
145
146
147
148 /*
149  * the 32 bits .rf5 file contains:
150  *  an 8 byte magic number
151  *  32bit length
152  *  32bit number of records
153  *  other 0x200 bytes bytes of uncharted territory
154  *     1 or more copies of the num_of_records in there
155  *  the records whose first 32bits word is the length
156  *     they are stuffed by one to four words every 0x2000 bytes
157  *  and a 2 byte terminator FFFF
158  */
159
160 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
161
162 typedef struct {
163     guint32 file_len;
164     guint32 num_of_records;   /* XXX: not sure about this */
165
166     GHashTable* src_by_id;    /* k12_srcdsc_recs by input */
167     GHashTable* src_by_name;  /* k12_srcdsc_recs by stack_name */
168
169     guint8 *seq_read_buff;    /* read buffer for sequential reading */
170     guint seq_read_buff_len;  /* length of that buffer */
171     guint8 *rand_read_buff;   /* read buffer for random reading */
172     guint rand_read_buff_len; /* length of that buffer */
173
174     Buffer extra_info;        /* Buffer to hold per packet extra information */
175 } k12_t;
176
177 typedef struct _k12_src_desc_t {
178     guint32 input;
179     guint32 input_type;
180     gchar* input_name;
181     gchar* stack_file;
182     k12_input_info_t input_info;
183 } k12_src_desc_t;
184
185
186 /*
187  * According to the Tektronix documentation, this value is a combination of
188  * a "group" code and a "type" code, with both being 2-byte values and
189  * with the "group" code followe by the "type" code.  The "group" values
190  * are:
191  *
192  *      0x0001 - "data event"
193  *      0x0002 - "text or L1 event"
194  *      0x0007 - "configuration event"
195  *
196  * and the "type" values are:
197  *
198  *  data events:
199  *      0x0020 - "frame" (i.e., "an actual packet")
200  *      0x0021 - "transparent frame"
201  *      0x0022 - "bit data (TRAU frame)"
202  *      0x0024 - "used to mark the frame which is a fragment"
203  *      0x0026 - "used to mark the frame which is a fragment"
204  *      0x0028 - "used to mark the frame which is generated by the LSA"
205  *      0x002A - "used to mark the frame which is generated by the LSA"
206  *
207  *  text or L1 events:
208  *      0x0030 - "text event"
209  *      0x0031 - "L1 event"
210  *      0x0032 - "L1 event (BAI)"
211  *      0x0033 - "L1 event (VX)"
212  *
213  *  configuration events:
214  *      0x0040 - Logical Data Source configuration event
215  *      0x0041 - Logical Link configuration event
216  */
217 /* so far we've seen these types of records */
218 #define K12_REC_PACKET        0x00010020 /* an actual packet */
219 #define K12_REC_D0020         0x000d0020 /* an actual packet, seen in a k18 file */
220 #define K12_REC_SCENARIO      0x00070040 /* what appears as the window's title */
221 #define K12_REC_SRCDSC        0x00070041 /* port-stack mapping + more, the key of the whole thing */
222 #define K12_REC_STK_FILE      0x00070042 /* a dump of an stk file */
223 #define K12_REC_SRCDSC2       0x00070043 /* another port-stack mapping */
224 #define K12_REC_TEXT          0x00070044 /* a string containing something with a grammar (conditions/responses?) */
225 #define K12_REC_START         0x00020030 /* a string containing human readable start time  */
226 #define K12_REC_STOP          0x00020031 /* a string containing human readable stop time */
227
228 /*
229  * According to the Tektronix documentation, packets, i.e. "data events",
230  * have several different group/type values, which differ in the last
231  * nibble of the type code.  For now, we just mask that nibble off; the
232  * format of the items are different, so we might have to treat different
233  * data event types differently.
234  */
235 #define K12_MASK_PACKET       0xfffffff0
236
237 /* offsets of elements in the records */
238 #define K12_RECORD_LEN         0x0 /* uint32, in bytes */
239 #define K12_RECORD_TYPE        0x4 /* uint32, see above */
240 #define K12_RECORD_FRAME_LEN   0x8 /* uint32, in bytes */
241 #define K12_RECORD_SRC_ID      0xc /* uint32 */
242
243 /*
244  * Some records from K15 files have a port ID of an undeclared
245  * interface which happens to be the only one with the first byte changed.
246  * It is still unknown how to recognize when this happens.
247  * If the lookup of the interface record fails we'll mask it
248  * and retry.
249  */
250 #define K12_RECORD_SRC_ID_MASK 0x00ffffff
251
252 /* elements of packet records */
253 #define K12_PACKET_TIMESTAMP  0x18 /* int64 (8b) representing 1/2us since 01-01-1990 Z00:00:00 */
254
255 #define K12_PACKET_FRAME      0x20 /* start of the actual frame in the record */
256 #define K12_PACKET_FRAME_D0020 0x34 /* start of the actual frame in the record */
257
258 #define K12_PACKET_OFFSET_VP  0x08 /* 2 bytes, big endian */
259 #define K12_PACKET_OFFSET_VC  0x0a /* 2 bytes, big endian */
260 #define K12_PACKET_OFFSET_CID 0x0c /* 1 byte */
261
262 /* elements of the source description records */
263 #define K12_SRCDESC_COLOR_FOREGROUND 0x12 /* 1 byte */
264 #define K12_SRCDESC_COLOR_BACKGROUND 0x13 /* 1 byte */
265
266 #define K12_SRCDESC_PORT_TYPE  0x1a   /* 1 byte */
267 #define K12_SRCDESC_EXTRALEN   0x1e   /* uint16, big endian */
268 #define K12_SRCDESC_NAMELEN    0x20   /* uint16, big endian */
269 #define K12_SRCDESC_STACKLEN   0x22   /* uint16, big endian */
270
271 #define K12_SRCDESC_EXTRATYPE  0x24   /* uint32, big endian */
272
273 #define K12_SRCDESC_ATM_VPI    0x38   /* uint16, big endian */
274 #define K12_SRCDESC_ATM_VCI    0x3a   /* uint16, big endian */
275 #define K12_SRCDESC_ATM_AAL    0x3c   /* 1 byte */
276
277 #define K12_SRCDESC_DS0_MASK   0x3c   /* 32 bytes */
278
279 /*
280  * A "stack file", as appears in a K12_REC_STK_FILE record, is a text
281  * file (with CR-LF line endings) with a sequence of lines, each of
282  * which begins with a keyword, and has white-space-separated tokens
283  * after that.
284  *
285  * They appear to be:
286  *
287  *   STKVER, which is followed by a number (presumably a version number
288  *   for the stack file format)
289  *
290  *   STACK, which is followed by a quoted string ("ProtocolStack" in one
291  *   file) and two numbers
292  *
293  *   PATH, which is followed by a non-quoted string giving the pathname
294  *   of the directory containing the stack file
295  *
296  *   HLAYER, which is followed by a quoted string, a path for something
297  *   (protocol module?), a keyword ("LOADED", in one file), and a
298  *   quoted string giving a description - this is probably a protocol
299  *   layer of some sort
300  *
301  *   LAYER, which has a similar syntax to HLAYER - the first quoted
302  *   string is a protocol name
303  *
304  *   RELATION, which has a quoted string giving a protocol name,
305  *   another quoted string giving a protocol name, and a condition
306  *   specifier of some sort, which probably says the second protocol
307  *   is layered atop the first protocol if the condition is true.
308  *   The first protocol can also be "BASE", which means that the
309  *   second protocol is the lowest-level protocol.
310  *   The conditions are:
311  *
312  *     CPLX, which may mean "complex" - it has parenthesized expressions
313  *     including "&", presumably a boolean AND, with the individual
314  *     tests being L:expr, where L is a letter such as "L", "D", or "P",
315  *     and expr is:
316  *
317  *        0x........ for L, where each . is a hex digit or a ?, presumably
318  *        meaning "don't care"
319  *
320  *        0;0{=,!=}0b........ for D, where . is presumably a bit or a ?
321  *
322  *        param=value for P, where param is something such as "src_port"
323  *        and value is a value, presumably to test, for example, TCP or
324  *        UDP ports
325  *
326  *     UNCOND, presumably meaning "always"
327  *
328  *     PARAM, followed by a parameter name (as with P:) and a value,
329  *     possibly followed by LAYPARAM and a hex value
330  *
331  *   DECKRNL, followed by a quoted string protocol name, un-quoted
332  *   "LSBF" or "MSBF" (Least/Most Significant Byte First?), and
333  *   an un-quoted string ending with _DK
334  *
335  *   LAYPARAM, followed by a quoted protocol name and a number (-2147221504
336  *   in one file, which is 0x80040000)
337  *
338  *   SPC_CONF, folloed by a number, a quoted string with numbers separated
339  *   by hyphens, and another number
340  *
341  *   CIC_CONF, with a similar syntax to SPC_CONF
342  *
343  *   LAYPOS, followed by a protocol name or "BASE" and 3 numbers.
344  *
345  * Most of this is probably not useful, but the RELATION lines with
346  * "BASE" could be used to figure out how to start the dissection
347  * (if we knew what "L" and "D" did), and *some* of the others might
348  * be useful if they don't match what's already in various dissector
349  * tables (the ones for IP and a higher-level protocol, for example,
350  * aren't very useful, as those are standardized, but the ones for
351  * TCP, UDP, and SCTP ports, and SCTP PPIs, might be useful).
352  */
353
354 /*
355  * get_record: Get the next record into a buffer
356  *   Every about 0x2000 bytes 0x10 bytes are inserted in the file,
357  *   even in the middle of a record.
358  *   This reads the next record without the eventual 0x10 bytes.
359  *   returns the length of the record + the stuffing (if any)
360  *
361  *   Returns number of bytes read on success, 0 on EOF, -1 on error;
362  *   if -1 is returned, *err is set to the error indication and, for
363  *   errors where that's appropriate, *err_info is set to an additional
364  *   error string.
365  *
366  * XXX: works at most with 0x1FFF bytes per record
367  */
368 static gint get_record(k12_t *file_data, FILE_T fh, gint64 file_offset,
369                        gboolean is_random, int *err, gchar **err_info) {
370     guint8 *buffer = is_random ? file_data->rand_read_buff : file_data->seq_read_buff;
371     guint buffer_len = is_random ? file_data->rand_read_buff_len : file_data->seq_read_buff_len;
372     guint bytes_read;
373     guint last_read;
374     guint left;
375     guint8 junk[0x14];
376     guint8* writep;
377 #ifdef DEBUG_K12
378     guint actual_len;
379 #endif
380
381     /* where the next unknown 0x10 bytes are stuffed to the file */
382     guint junky_offset = 0x2000 - (gint) ( (file_offset - 0x200) % 0x2000 );
383
384     K12_DBG(6,("get_record: ENTER: junky_offset=%" G_GINT64_MODIFIER "d, file_offset=%" G_GINT64_MODIFIER "d",junky_offset,file_offset));
385
386     /* no buffer is given, lets create it */
387     if (buffer == NULL) {
388         buffer = (guint8*)g_malloc(0x2000);
389         buffer_len = 0x2000;
390         if (is_random) {
391             file_data->rand_read_buff = buffer;
392             file_data->rand_read_buff_len = buffer_len;
393         } else {
394             file_data->seq_read_buff = buffer;
395             file_data->seq_read_buff_len = buffer_len;
396         }
397     }
398
399     /* Get the record length. */
400     if ( junky_offset == 0x2000 ) {
401         /* the length of the record is 0x10 bytes ahead from we are reading */
402         bytes_read = file_read(junk,0x14,fh);
403
404         if (bytes_read == 2 && junk[0] == 0xff && junk[1] == 0xff) {
405             K12_DBG(1,("get_record: EOF"));
406             return 0;
407         } else if ( bytes_read < 0x14 ){
408             K12_DBG(1,("get_record: SHORT READ OR ERROR"));
409             *err = file_error(fh, err_info);
410             if (*err == 0) {
411                 *err = WTAP_ERR_SHORT_READ;
412             }
413             return -1;
414         }
415
416         memcpy(buffer,&(junk[0x10]),4);
417     } else {
418         /* the length of the record is right where we are reading */
419         bytes_read = file_read(buffer, 0x4, fh);
420
421         if (bytes_read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
422             K12_DBG(1,("get_record: EOF"));
423             return 0;
424         } else if (bytes_read == 4 && buffer[0] == 0xff && buffer[1] == 0xff
425                    && buffer[2] == 0x00 && buffer[3] == 0x00) {
426             /*
427              * In at least one k18 RF5 file, there appears to be a "record"
428              * with a length value of 0xffff0000, followed by a bunch of
429              * data that doesn't appear to be records, including a long
430              * list of numbers.
431              *
432              * We treat a length value of 0xffff0000 as an end-of-file
433              * indication.
434              *
435              * XXX - is this a length indication, or will it appear
436              * at the beginning of an 8KB block, so that we should
437              * check for it above?
438              */
439             K12_DBG(1,("get_record: EOF"));
440             return 0;
441         } else if ( bytes_read != 0x4 ) {
442             K12_DBG(1,("get_record: SHORT READ OR ERROR"));
443             *err = file_error(fh, err_info);
444             if (*err == 0) {
445                 *err = WTAP_ERR_SHORT_READ;
446             }
447             return -1;
448         }
449     }
450
451     left = pntoh32(buffer + K12_RECORD_LEN);
452 #ifdef DEBUG_K12
453     actual_len = left;
454 #endif
455     junky_offset -= 0x4;
456
457     K12_DBG(5,("get_record: GET length=%u",left));
458
459     /*
460      * Record length must be at least large enough for the length,
461      * hence 4 bytes.
462      *
463      * XXX - Is WTAP_MAX_PACKET_SIZE the right check for a maximum
464      * record size?  Should we report this error differently?
465      */
466     if (left < 4 || left > WTAP_MAX_PACKET_SIZE) {
467         K12_DBG(1,("get_record: Invalid GET length=%u",left));
468         *err = WTAP_ERR_BAD_FILE;
469         *err_info = g_strdup_printf("get_record: Invalid GET length=%u",left);
470         return -1;
471     }
472
473     /*
474      * XXX - calculate the lowest power of 2 >= left, rather than just
475      * looping.
476      */
477     while (left > buffer_len) {
478         buffer = (guint8*)g_realloc(buffer,buffer_len*=2);
479         if (is_random) {
480             file_data->rand_read_buff = buffer;
481             file_data->rand_read_buff_len = buffer_len;
482         } else {
483             file_data->seq_read_buff = buffer;
484             file_data->seq_read_buff_len = buffer_len;
485         }
486     }
487
488     writep = buffer + 4;
489     left -= 4;
490
491     /* Read the rest of the record. */
492     do {
493         K12_DBG(6,("get_record: looping left=%d junky_offset=%" G_GINT64_MODIFIER "d",left,junky_offset));
494
495         if (junky_offset > left) {
496             bytes_read += last_read = file_read(writep, left, fh);
497
498             if ( last_read != left ) {
499                 K12_DBG(1,("get_record: SHORT READ OR ERROR"));
500                 *err = file_error(fh, err_info);
501                 if (*err == 0) {
502                     *err = WTAP_ERR_SHORT_READ;
503                 }
504                 return -1;
505             } else {
506                 K12_HEX_ASCII_DUMP(5,file_offset, "GOT record", buffer, actual_len);
507                 return bytes_read;
508             }
509         } else {
510             bytes_read += last_read = file_read(writep, junky_offset, fh);
511
512             if ( last_read != junky_offset ) {
513                 K12_DBG(1,("get_record: SHORT READ OR ERROR, read=%d expected=%d",last_read, junky_offset));
514                 *err = file_error(fh, err_info);
515                 if (*err == 0) {
516                     *err = WTAP_ERR_SHORT_READ;
517                 }
518                 return -1;
519             }
520
521             writep += last_read;
522
523             bytes_read += last_read = file_read(junk, 0x10, fh);
524
525             if ( last_read != 0x10 ) {
526                 K12_DBG(1,("get_record: SHORT READ OR ERROR"));
527                 *err = file_error(fh, err_info);
528                 if (*err == 0) {
529                     *err = WTAP_ERR_SHORT_READ;
530                 }
531                 return -1;
532             }
533
534             left -= junky_offset;
535             junky_offset = 0x2000;
536         }
537
538     } while(left);
539
540     K12_HEX_ASCII_DUMP(5,file_offset, "GOT record", buffer, actual_len);
541     return bytes_read;
542 }
543
544 static void
545 process_packet_data(struct wtap_pkthdr *phdr, Buffer *target, guint8 *buffer,
546                     gint len, k12_t *k12)
547 {
548     guint32 type;
549     guint   buffer_offset;
550     guint64 ts;
551     guint32 length;
552     guint32 extra_len;
553     guint32 src_id;
554     k12_src_desc_t* src_desc;
555
556     phdr->presence_flags = WTAP_HAS_TS;
557
558     ts = pntoh64(buffer + K12_PACKET_TIMESTAMP);
559
560     phdr->ts.secs = (guint32) ((ts / 2000000) + 631152000);
561     phdr->ts.nsecs = (guint32) ( (ts % 2000000) * 500 );
562
563     length = pntoh32(buffer + K12_RECORD_FRAME_LEN) & 0x00001FFF;
564     phdr->len = phdr->caplen = length;
565
566     type = pntoh32(buffer + K12_RECORD_TYPE);
567     buffer_offset = (type == K12_REC_D0020) ? K12_PACKET_FRAME_D0020 : K12_PACKET_FRAME;
568
569     buffer_assure_space(target, length);
570     memcpy(buffer_start_ptr(target), buffer + buffer_offset, length);
571
572     /* extra information need by some protocols */
573     extra_len = len - buffer_offset - length;
574     buffer_assure_space(&(k12->extra_info), extra_len);
575     memcpy(buffer_start_ptr(&(k12->extra_info)),
576            buffer + buffer_offset + length, extra_len);
577     phdr->pseudo_header.k12.extra_info = (guint8*)buffer_start_ptr(&(k12->extra_info));
578     phdr->pseudo_header.k12.extra_length = extra_len;
579
580     src_id = pntoh32(buffer + K12_RECORD_SRC_ID);
581     K12_DBG(5,("process_packet_data: src_id=%.8x",src_id));
582     phdr->pseudo_header.k12.input = src_id;
583
584     if ( ! (src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id))) ) {
585         /*
586          * Some records from K15 files have a port ID of an undeclared
587          * interface which happens to be the only one with the first byte changed.
588          * It is still unknown how to recognize when this happens.
589          * If the lookup of the interface record fails we'll mask it
590          * and retry.
591          */
592         src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id&K12_RECORD_SRC_ID_MASK));
593     }
594
595     if (src_desc) {
596         K12_DBG(5,("process_packet_data: input_name='%s' stack_file='%s' type=%x",src_desc->input_name,src_desc->stack_file,src_desc->input_type));
597         phdr->pseudo_header.k12.input_name = src_desc->input_name;
598         phdr->pseudo_header.k12.stack_file = src_desc->stack_file;
599         phdr->pseudo_header.k12.input_type = src_desc->input_type;
600
601         switch(src_desc->input_type) {
602             case K12_PORT_ATMPVC:
603                 if ((long)(buffer_offset + length + K12_PACKET_OFFSET_CID) < len) {
604                     phdr->pseudo_header.k12.input_info.atm.vp =  pntoh16(buffer + buffer_offset + length + K12_PACKET_OFFSET_VP);
605                     phdr->pseudo_header.k12.input_info.atm.vc =  pntoh16(buffer + buffer_offset + length + K12_PACKET_OFFSET_VC);
606                     phdr->pseudo_header.k12.input_info.atm.cid =  *((unsigned char*)(buffer + buffer_offset + length + K12_PACKET_OFFSET_CID));
607                     break;
608                 }
609                 /* Fall through */
610             default:
611                 memcpy(&(phdr->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
612                 break;
613         }
614     } else {
615         K12_DBG(5,("process_packet_data: NO SRC_RECORD FOUND"));
616
617         memset(&(phdr->pseudo_header.k12),0,sizeof(phdr->pseudo_header.k12));
618         phdr->pseudo_header.k12.input_name = "unknown port";
619         phdr->pseudo_header.k12.stack_file = "unknown stack file";
620     }
621
622     phdr->pseudo_header.k12.input = src_id;
623     phdr->pseudo_header.k12.stuff = k12;
624 }
625
626 static gboolean k12_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset) {
627     k12_t *k12 = (k12_t *)wth->priv;
628     k12_src_desc_t* src_desc;
629     guint8* buffer;
630     gint64 offset;
631     gint len;
632     guint32 type;
633     guint32 src_id;
634
635     offset = file_tell(wth->fh);
636
637     /* ignore the record if it isn't a packet */
638     do {
639         K12_DBG(5,("k12_read: offset=%i",offset));
640
641         *data_offset = offset;
642
643         len = get_record(k12, wth->fh, offset, FALSE, err, err_info);
644
645         if (len < 0) {
646             /* read error */
647             return FALSE;
648         } else if (len == 0) {
649             /* EOF */
650             *err = 0;
651             return FALSE;
652         } else if (len < K12_RECORD_SRC_ID + 4) {
653             /* Record not large enough to contain a src ID */
654             *err = WTAP_ERR_BAD_FILE;
655             *err_info = g_strdup_printf("data record length %d too short", len);
656             return FALSE;
657         }
658
659         buffer = k12->seq_read_buff;
660
661         type = pntoh32(buffer + K12_RECORD_TYPE);
662         src_id = pntoh32(buffer + K12_RECORD_SRC_ID);
663
664
665         if ( ! (src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id))) ) {
666             /*
667              * Some records from K15 files have a port ID of an undeclared
668              * interface which happens to be the only one with the first byte changed.
669              * It is still unknown how to recognize when this happens.
670              * If the lookup of the interface record fails we'll mask it
671              * and retry.
672              */
673             src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id&K12_RECORD_SRC_ID_MASK));
674         }
675
676         K12_DBG(5,("k12_read: record type=%x src_id=%x",type,src_id));
677
678         offset += len;
679
680     } while ( ((type & K12_MASK_PACKET) != K12_REC_PACKET && (type & K12_MASK_PACKET) != K12_REC_D0020) || !src_id || !src_desc );
681
682     process_packet_data(&wth->phdr, wth->frame_buffer, buffer, len, k12);
683
684     return TRUE;
685 }
686
687
688 static gboolean k12_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info) {
689     k12_t *k12 = (k12_t *)wth->priv;
690     guint8* buffer;
691     gint len;
692
693     K12_DBG(5,("k12_seek_read: ENTER"));
694
695     if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
696         K12_DBG(5,("k12_seek_read: SEEK ERROR"));
697         return FALSE;
698     }
699
700     len = get_record(k12, wth->random_fh, seek_off, TRUE, err, err_info);
701     if (len < 0) {
702         K12_DBG(5,("k12_seek_read: READ ERROR"));
703         return FALSE;
704     } else if (len < K12_RECORD_SRC_ID + 4) {
705         /* Record not large enough to contain a src ID */
706         K12_DBG(5,("k12_seek_read: SHORT READ"));
707         *err = WTAP_ERR_SHORT_READ;
708         return FALSE;
709     }
710
711     buffer = k12->rand_read_buff;
712
713     process_packet_data(phdr, buf, buffer, len, k12);
714
715     K12_DBG(5,("k12_seek_read: DONE OK"));
716
717     return TRUE;
718 }
719
720
721 static k12_t* new_k12_file_data(void) {
722     k12_t* fd = g_new(k12_t,1);
723
724     fd->file_len = 0;
725     fd->num_of_records = 0;
726     fd->src_by_name = g_hash_table_new(g_str_hash,g_str_equal);
727     fd->src_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
728     fd->seq_read_buff = NULL;
729     fd->seq_read_buff_len = 0;
730     fd->rand_read_buff = NULL;
731     fd->rand_read_buff_len = 0;
732
733     buffer_init(&(fd->extra_info), 100);
734
735     return fd;
736 }
737
738 static gboolean destroy_srcdsc(gpointer k _U_, gpointer v, gpointer p _U_) {
739     k12_src_desc_t* rec = (k12_src_desc_t*)v;
740
741     g_free(rec->input_name);
742     g_free(rec->stack_file);
743     g_free(rec);
744
745     return TRUE;
746 }
747
748 static void destroy_k12_file_data(k12_t* fd) {
749     g_hash_table_destroy(fd->src_by_id);
750     g_hash_table_foreach_remove(fd->src_by_name,destroy_srcdsc,NULL);
751     g_hash_table_destroy(fd->src_by_name);
752     buffer_free(&(fd->extra_info));
753     g_free(fd->seq_read_buff);
754     g_free(fd->rand_read_buff);
755     g_free(fd);
756 }
757
758 static void k12_close(wtap *wth) {
759     k12_t *k12 = (k12_t *)wth->priv;
760
761     destroy_k12_file_data(k12);
762     wth->priv = NULL;   /* destroy_k12_file_data freed it */
763 #ifdef DEBUG_K12
764     K12_DBG(5,("k12_close: CLOSED"));
765     if (env_file) fclose(dbg_out);
766 #endif
767 }
768
769
770 int k12_open(wtap *wth, int *err, gchar **err_info) {
771     k12_src_desc_t* rec;
772     guint8 header_buffer[0x200];
773     guint8* read_buffer;
774     guint32 type;
775     long offset;
776     long len;
777     guint32 rec_len;
778     guint32 extra_len;
779     guint32 name_len;
780     guint32 stack_len;
781     guint i;
782     k12_t* file_data;
783
784 #ifdef DEBUG_K12
785     gchar* env_level = getenv("K12_DEBUG_LEVEL");
786     env_file = getenv("K12_DEBUG_FILENAME");
787     if ( env_file ) {
788         dbg_out = ws_fopen(env_file,"w");
789         if (dbg_out == NULL) {
790                 dbg_out = stderr;
791                 K12_DBG(1,("unable to open K12 DEBUG FILENAME for writing!  Logging to standard error"));
792         }
793     }
794     else
795         dbg_out = stderr;
796     if ( env_level ) debug_level = (unsigned int)strtoul(env_level,NULL,10);
797     K12_DBG(1,("k12_open: ENTER debug_level=%u",debug_level));
798 #endif
799
800     if ( file_read(header_buffer,0x200,wth->fh) != 0x200 ) {
801         K12_DBG(1,("k12_open: FILE HEADER TOO SHORT OR READ ERROR"));
802         *err = file_error(wth->fh, err_info);
803         if (*err != 0 && *err != WTAP_ERR_SHORT_READ) {
804             return -1;
805         }
806         return 0;
807     } else {
808         if ( memcmp(header_buffer,k12_file_magic,8) != 0 ) {
809             K12_DBG(1,("k12_open: BAD MAGIC"));
810             return 0;
811         }
812     }
813
814     offset = 0x200;
815
816     file_data = new_k12_file_data();
817
818     file_data->file_len = pntoh32( header_buffer + 0x8);
819     file_data->num_of_records = pntoh32( header_buffer + 0xC );
820
821     K12_DBG(5,("k12_open: FILE_HEADER OK: offset=%x file_len=%i records=%i",
822             offset,
823             file_data->file_len,
824             file_data->num_of_records ));
825
826     do {
827
828         len = get_record(file_data, wth->fh, offset, FALSE, err, err_info);
829
830         if ( len < 0 ) {
831             K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
832             destroy_k12_file_data(file_data);
833             return -1;
834         }
835         if (len == 0) {
836             K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
837             *err = WTAP_ERR_SHORT_READ;
838             destroy_k12_file_data(file_data);
839             return -1;
840         }
841
842         if (len == 0) {
843             K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
844             *err = WTAP_ERR_SHORT_READ;
845             destroy_k12_file_data(file_data);
846             return -1;
847         }
848
849         read_buffer = file_data->seq_read_buff;
850
851         rec_len = pntoh32( read_buffer + K12_RECORD_LEN );
852         if (rec_len < K12_RECORD_TYPE + 4) {
853             /* Record isn't long enough to have a type field */
854             *err = WTAP_ERR_BAD_FILE;
855             *err_info = g_strdup_printf("k12_open: record length %u < %u",
856                                         rec_len, K12_RECORD_TYPE + 4);
857             return -1;
858         }
859         type = pntoh32( read_buffer + K12_RECORD_TYPE );
860
861         if ( (type & K12_MASK_PACKET) == K12_REC_PACKET ||
862              (type & K12_MASK_PACKET) == K12_REC_D0020) {
863             /*
864              * we are at the first packet record, rewind and leave.
865              */
866             if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
867                 destroy_k12_file_data(file_data);
868                 return -1;
869             }
870             K12_DBG(5,("k12_open: FIRST PACKET offset=%x",offset));
871             break;
872         } else if (type == K12_REC_SRCDSC || type == K12_REC_SRCDSC2 ) {
873             rec = g_new0(k12_src_desc_t,1);
874
875             if (rec_len < K12_SRCDESC_STACKLEN + 2) {
876                 /* Record isn't long enough to have a stack length field */
877                 *err = WTAP_ERR_BAD_FILE;
878                 *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
879                                             rec_len, K12_SRCDESC_STACKLEN + 2);
880                 return -1;
881             }
882             extra_len = pntoh16( read_buffer + K12_SRCDESC_EXTRALEN );
883             name_len = pntoh16( read_buffer + K12_SRCDESC_NAMELEN );
884             stack_len = pntoh16( read_buffer + K12_SRCDESC_STACKLEN );
885
886             rec->input = pntoh32( read_buffer + K12_RECORD_SRC_ID );
887
888             K12_DBG(5,("k12_open: INTERFACE RECORD offset=%x interface=%x",offset,rec->input));
889
890             if (name_len == 0 || stack_len == 0
891                 || 0x20 + extra_len + name_len + stack_len > rec_len ) {
892                 g_free(rec);
893                 K12_DBG(5,("k12_open: failed (name_len == 0 || stack_len == 0 "
894                         "|| 0x20 + extra_len + name_len + stack_len > rec_len)  extra_len=%i name_len=%i stack_len=%i"));
895                 destroy_k12_file_data(file_data);
896                 return 0;
897             }
898
899             if (extra_len) {
900                 if (rec_len < K12_SRCDESC_EXTRATYPE + 4) {
901                     /* Record isn't long enough to have a source descriptor extra type field */
902                     *err = WTAP_ERR_BAD_FILE;
903                     *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
904                                                 rec_len, K12_SRCDESC_EXTRATYPE + 4);
905                     return -1;
906                 }
907                 switch(( rec->input_type = pntoh32( read_buffer + K12_SRCDESC_EXTRATYPE ) )) {
908                     case K12_PORT_DS0S:
909                         if (rec_len < K12_SRCDESC_DS0_MASK + 32) {
910                             /* Record isn't long enough to have a source descriptor extra type field */
911                             *err = WTAP_ERR_BAD_FILE;
912                             *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
913                                                         rec_len, K12_SRCDESC_DS0_MASK + 12);
914                             return -1;
915                         }
916
917                         rec->input_info.ds0mask = 0x00000000;
918
919                         for (i = 0; i < 32; i++) {
920                             rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 0x1<<(31-i) : 0x0;
921                         }
922
923                         break;
924                     case K12_PORT_ATMPVC:
925                         if (rec_len < K12_SRCDESC_ATM_VCI + 2) {
926                             /* Record isn't long enough to have a source descriptor extra type field */
927                             *err = WTAP_ERR_BAD_FILE;
928                             *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
929                                                         rec_len, K12_SRCDESC_DS0_MASK + 12);
930                             return -1;
931                         }
932
933                         rec->input_info.atm.vp = pntoh16( read_buffer + K12_SRCDESC_ATM_VPI );
934                         rec->input_info.atm.vc = pntoh16( read_buffer + K12_SRCDESC_ATM_VCI );
935                         break;
936                     default:
937                         break;
938                 }
939             } else {
940                 /* Record viewer generated files don't have this information */
941                 if (rec_len < K12_SRCDESC_PORT_TYPE + 1) {
942                     /* Record isn't long enough to have a source descriptor extra type field */
943                     *err = WTAP_ERR_BAD_FILE;
944                     *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
945                                                 rec_len, K12_SRCDESC_DS0_MASK + 12);
946                     return -1;
947                 }
948                 if (read_buffer[K12_SRCDESC_PORT_TYPE] >= 0x14
949                     && read_buffer[K12_SRCDESC_PORT_TYPE] <= 0x17) {
950                     /* For ATM2_E1DS1, ATM2_E3DS3,
951                        ATM2_STM1EL and ATM2_STM1OP */
952                     rec->input_type = K12_PORT_ATMPVC;
953                     rec->input_info.atm.vp = 0;
954                     rec->input_info.atm.vc = 0;
955                 }
956             }
957
958             /* XXX - this is assumed, in a number of places (not just in the
959                ascii_strdown_inplace() call below) to be null-terminated;
960                is that guaranteed (even with a corrupt file)?
961                Obviously not, as a corrupt file could contain anything
962                here; the Tektronix document says the strings "must end
963                with \0", but a bad file could fail to add the \0. */
964             if (rec_len < K12_SRCDESC_EXTRATYPE + extra_len + name_len + stack_len) {
965                 /* Record isn't long enough to have a source descriptor extra type field */
966                 *err = WTAP_ERR_BAD_FILE;
967                 *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
968                                             rec_len, K12_SRCDESC_EXTRATYPE + extra_len + name_len + stack_len);
969                 return -1;
970             }
971             rec->input_name = (gchar *)g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len, name_len);
972             rec->stack_file = (gchar *)g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len + name_len, stack_len);
973
974             ascii_strdown_inplace (rec->stack_file);
975
976             g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec);
977             g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec);
978
979             offset += len;
980             continue;
981         } else if (type == K12_REC_STK_FILE) {
982             K12_DBG(1,("k12_open: K12_REC_STK_FILE"));
983             K12_DBG(1,("Field 1: 0x%08x",pntoh32( read_buffer + 0x08 )));
984             K12_DBG(1,("Field 2: 0x%08x",pntoh32( read_buffer + 0x0c )));
985             K12_ASCII_DUMP(1, read_buffer, rec_len, 0x10);
986
987             offset += len;
988             continue;
989         } else {
990             K12_DBG(1,("k12_open: RECORD TYPE 0x%08x",type));
991             offset += len;
992             continue;
993         }
994     } while(1);
995
996     wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_K12;
997     wth->file_encap = WTAP_ENCAP_K12;
998     wth->snapshot_length = 0;
999     wth->subtype_read = k12_read;
1000     wth->subtype_seek_read = k12_seek_read;
1001     wth->subtype_close = k12_close;
1002     wth->priv = (void *)file_data;
1003     wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
1004
1005     return 1;
1006 }
1007
1008 typedef struct {
1009         guint32 file_len;
1010         guint32 num_of_records;
1011         guint32 file_offset;
1012 } k12_dump_t;
1013
1014 int k12_dump_can_write_encap(int encap) {
1015
1016     if (encap == WTAP_ENCAP_PER_PACKET)
1017         return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
1018
1019     if (encap != WTAP_ENCAP_K12)
1020         return WTAP_ERR_UNSUPPORTED_ENCAP;
1021
1022     return 0;
1023 }
1024
1025 static const gchar dumpy_junk[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
1026
1027 static gboolean k12_dump_record(wtap_dumper *wdh, guint32 len,  guint8* buffer, int *err_p) {
1028     k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
1029     guint32 junky_offset = (0x2000 - ( (k12->file_offset - 0x200) % 0x2000 )) % 0x2000;
1030
1031     if (len > junky_offset) {
1032         if (junky_offset) {
1033             if (! wtap_dump_file_write(wdh, buffer, junky_offset, err_p))
1034                 return FALSE;
1035         }
1036         if (! wtap_dump_file_write(wdh, dumpy_junk, 0x10, err_p))
1037             return FALSE;
1038
1039         if (! wtap_dump_file_write(wdh, buffer+junky_offset, len - junky_offset, err_p))
1040             return FALSE;
1041
1042         k12->file_offset += len + 0x10;
1043     } else {
1044         if (! wtap_dump_file_write(wdh, buffer, len, err_p))
1045             return FALSE;
1046         k12->file_offset += len;
1047     }
1048
1049     k12->num_of_records++;
1050     return TRUE;
1051 }
1052
1053 static void k12_dump_src_setting(gpointer k _U_, gpointer v, gpointer p) {
1054     k12_src_desc_t* src_desc = (k12_src_desc_t*)v;
1055     wtap_dumper *wdh = (wtap_dumper *)p;
1056     guint32 len;
1057     guint offset;
1058     guint i;
1059     int   errxxx; /* dummy */
1060
1061     union {
1062         guint8 buffer[0x2000];
1063
1064         struct {
1065             guint32 len;
1066             guint32 type;
1067             guint32 unk32_1;
1068             guint32 input;
1069
1070             guint16 unk32_2;
1071             guint16 color;
1072             guint32 unk32_3;
1073             guint32 unk32_4;
1074             guint16 unk16_1;
1075             guint16 extra_len;
1076
1077             guint16 name_len;
1078             guint16 stack_len;
1079
1080             struct {
1081                 guint32 type;
1082
1083                 union {
1084                     struct {
1085                         guint32 unk32;
1086                         guint8 mask[32];
1087                     } ds0mask;
1088
1089                     struct {
1090                         guint8 unk_data[0x10];
1091                         guint16 vp;
1092                         guint16 vc;
1093                     } atm;
1094
1095                     guint32 unk;
1096                 } desc;
1097             } extra;
1098         } record;
1099     } obj;
1100
1101     obj.record.type = g_htonl(K12_REC_SRCDSC);
1102     obj.record.unk32_1 = g_htonl(0x00000001);
1103     obj.record.input = g_htonl(src_desc->input);
1104
1105     obj.record.unk32_2 = g_htons(0x0000);
1106     obj.record.color = g_htons(0x060f);
1107     obj.record.unk32_3 = g_htonl(0x00000003);
1108     switch (src_desc->input_type) {
1109         case K12_PORT_ATMPVC:
1110             obj.record.unk32_4 = g_htonl(0x01001400);
1111             break;
1112         default:
1113             obj.record.unk32_4 = g_htonl(0x01000100);
1114     }
1115
1116     obj.record.unk16_1 = g_htons(0x0000);
1117     obj.record.name_len = (guint16) strlen(src_desc->input_name) + 1;
1118     obj.record.stack_len = (guint16) strlen(src_desc->stack_file) + 1;
1119
1120     obj.record.extra.type = g_htonl(src_desc->input_type);
1121
1122     switch (src_desc->input_type) {
1123         case K12_PORT_ATMPVC:
1124             obj.record.extra_len = g_htons(0x18);
1125             obj.record.extra.desc.atm.vp = g_htons(src_desc->input_info.atm.vp);
1126             obj.record.extra.desc.atm.vc = g_htons(src_desc->input_info.atm.vc);
1127             offset = 0x3c;
1128             break;
1129         case K12_PORT_DS0S:
1130             obj.record.extra_len = g_htons(0x18);
1131             for( i=0; i<32; i++ ) {
1132                 obj.record.extra.desc.ds0mask.mask[i] =
1133                 (src_desc->input_info.ds0mask & (1 << i)) ? 0xff : 0x00;
1134             }
1135             offset = 0x3c;
1136             break;
1137         default:
1138             obj.record.extra_len = g_htons(0x08);
1139             offset = 0x2c;
1140             break;
1141     }
1142
1143     memcpy(obj.buffer + offset,
1144            src_desc->input_name,
1145            obj.record.name_len);
1146
1147     memcpy(obj.buffer + offset + obj.record.name_len,
1148            src_desc->stack_file,
1149            obj.record.stack_len);
1150
1151     len = offset + obj.record.name_len + obj.record.stack_len;
1152     len += (len % 4) ? 4 - (len % 4) : 0;
1153
1154     obj.record.len = g_htonl(len);
1155     obj.record.name_len =  g_htons(obj.record.name_len);
1156     obj.record.stack_len = g_htons(obj.record.stack_len);
1157
1158     k12_dump_record(wdh,len,obj.buffer, &errxxx); /* fwrite errs ignored: see k12_dump below */
1159 }
1160
1161 static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
1162                          const guint8 *pd, int *err) {
1163     const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
1164     k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
1165     guint32 len;
1166     union {
1167         guint8 buffer[0x2000];
1168         struct {
1169             guint32 len;
1170             guint32 type;
1171             guint32 frame_len;
1172             guint32 input;
1173
1174             guint32 datum_1;
1175             guint32 datum_2;
1176             guint64 ts;
1177
1178             guint8 frame[0x1fc0];
1179         } record;
1180     } obj;
1181
1182     if (k12->num_of_records == 0) {
1183         k12_t* file_data = (k12_t*)pseudo_header->k12.stuff;
1184         /* XXX: We'll assume that any fwrite errors in k12_dump_src_setting will    */
1185         /*      repeat during the final k12_dump_record at the end of k12_dump      */
1186         /*      (and thus cause an error return from k12_dump).                     */
1187         /*      (I don't see a reasonably clean way to handle any fwrite errors     */
1188         /*       encountered in k12_dump_src_setting).                              */
1189         g_hash_table_foreach(file_data->src_by_id,k12_dump_src_setting,wdh);
1190     }
1191     obj.record.len = 0x20 + phdr->caplen;
1192     obj.record.len += (obj.record.len % 4) ? 4 - obj.record.len % 4 : 0;
1193
1194     len = obj.record.len;
1195
1196     obj.record.len = g_htonl(obj.record.len);
1197
1198     obj.record.type = g_htonl(K12_REC_PACKET);
1199     obj.record.frame_len = g_htonl(phdr->caplen);
1200     obj.record.input = g_htonl(pseudo_header->k12.input);
1201
1202     obj.record.ts = GUINT64_TO_BE((((guint64)phdr->ts.secs - 631152000) * 2000000) + (phdr->ts.nsecs / 1000 * 2));
1203
1204     memcpy(obj.record.frame,pd,phdr->caplen);
1205
1206     return k12_dump_record(wdh,len,obj.buffer, err);
1207 }
1208
1209 static const guint8 k12_eof[] = {0xff,0xff};
1210
1211 static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
1212     k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
1213     union {
1214         guint8 b[sizeof(guint32)];
1215         guint32 u;
1216     } d;
1217
1218     if (! wtap_dump_file_write(wdh, k12_eof, 2, err))
1219         return FALSE;
1220
1221     if (wtap_dump_file_seek(wdh, 8, SEEK_SET, err) == -1)
1222         return FALSE;
1223
1224     d.u = g_htonl(k12->file_len);
1225
1226     if (! wtap_dump_file_write(wdh, d.b, 4, err))
1227         return FALSE;
1228
1229     d.u = g_htonl(k12->num_of_records);
1230
1231     if (! wtap_dump_file_write(wdh, d.b, 4, err))
1232         return FALSE;
1233
1234     return TRUE;
1235 }
1236
1237
1238 gboolean k12_dump_open(wtap_dumper *wdh, int *err) {
1239     k12_dump_t *k12;
1240
1241     if ( ! wtap_dump_file_write(wdh, k12_file_magic, 8, err)) {
1242         return FALSE;
1243     }
1244
1245     if (wtap_dump_file_seek(wdh, 0x200, SEEK_SET, err) == -1)
1246         return FALSE;
1247
1248     wdh->subtype_write = k12_dump;
1249     wdh->subtype_close = k12_dump_close;
1250
1251     k12 = (k12_dump_t *)g_malloc(sizeof(k12_dump_t));
1252     wdh->priv = (void *)k12;
1253     k12->file_len = 0x200;
1254     k12->num_of_records = 0;
1255     k12->file_offset  = 0x200;
1256
1257     return TRUE;
1258 }