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