4 * routines for importing tektronix k12xx *.rf5 files
6 * Copyright (c) 2005, Luis E. Garia Ontanon <luis@ontanon.org>
11 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 /* #define DEBUG_K12 */
38 #include "file_wrappers.h"
42 #include <wsutil/str_util.h>
52 char* env_file = NULL;
54 static unsigned debug_level = 0;
56 void k12_fprintf(char* fmt, ...) {
60 vfprintf(dbg_out, fmt, ap);
65 #define K12_DBG(level,args) do { if (level <= debug_level) { \
66 fprintf(dbg_out,"%s:%d: ",CAT(__FI,LE__),CAT(__LI,NE__)); \
68 fprintf(dbg_out,"\n"); \
71 void k12_hexdump(guint level, gint64 offset, char* label, unsigned char* b, unsigned len) {
72 static const char* c2t[] = {
73 "00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f",
74 "10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f",
75 "20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f",
76 "30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f",
77 "40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f",
78 "50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f",
79 "60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f",
80 "70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f",
81 "80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f",
82 "90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f",
83 "a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af",
84 "b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf",
85 "c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf",
86 "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df",
87 "e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef",
88 "f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"
92 if (debug_level < level) return;
94 fprintf(dbg_out,"%s(%.8llx,%.4x): ",label,offset,len);
96 for (i=0 ; i<len ; i++) {
99 fprintf(dbg_out,"\n");
101 fprintf(dbg_out," ");
103 fprintf(dbg_out,c2t[b[i]]);
106 fprintf(dbg_out,"\n");
109 #define K12_HEXDMP(x,a,b,c,d) k12_hexdump(x,a,b,c,d)
112 #define K12_DBG(level,args) (void)0
113 #define K12_HEXDMP(x,a,b,c,d)
119 * the 32 bits .rf5 file contains:
120 * an 8 byte magic number
122 * 32bit number of records
123 * other 0x200 bytes bytes of uncharted territory
124 * 1 or more copies of the num_of_records in there
125 * the records whose first 32bits word is the length
126 * they are stuffed by one to four words every 0x2000 bytes
127 * and a 2 byte terminator FFFF
130 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
134 guint32 num_of_records; /* XXX: not sure about this */
136 GHashTable* src_by_id; /* k12_srcdsc_recs by input */
137 GHashTable* src_by_name; /* k12_srcdsc_recs by stack_name */
139 Buffer extra_info; /* Buffer to hold per packet extra information */
142 typedef struct _k12_src_desc_t {
147 k12_input_info_t input_info;
151 /* so far we've seen these types of records */
152 #define K12_REC_PACKET 0x00010020 /* an actual packet */
153 #define K12_REC_SRCDSC 0x00070041 /* port-stack mapping + more, the key of the whole thing */
154 #define K12_REC_SCENARIO 0x00070040 /* what appears as the window's title */
155 #define K12_REC_STK_FILE 0x00070042 /* a dump of an stk file */
156 #define K12_REC_SRCDSC2 0x00070043 /* another port-stack mapping */
157 #define K12_REC_TEXT 0x00070044 /* a string containing something with a grammar (conditions/responses?) */
158 #define K12_REC_START 0x00020030 /* a string containing human readable start time */
159 #define K12_REC_STOP 0x00020031 /* a string containing human readable stop time */
161 #define K12_MASK_PACKET 0xfffffff0 /* the last nibble in packet records somentimes change (not yet understood why) */
163 /* offsets of elements in the records */
164 #define K12_RECORD_LEN 0x0 /* uint32, in bytes */
165 #define K12_RECORD_TYPE 0x4 /* uint32, see above */
166 #define K12_RECORD_FRAME_LEN 0x8 /* uint32, in bytes */
167 #define K12_RECORD_SRC_ID 0xc /* uint32 */
170 * Some records from K15 files have a port ID of an undeclared
171 * interface which happens to be the only one with the first byte changed.
172 * It is still unknown how to recognize when this happens.
173 * If the lookup of the interface record fails we'll mask it
176 #define K12_RECORD_SRC_ID_MASK 0x00ffffff
178 /* elements of packet records */
179 #define K12_PACKET_TIMESTAMP 0x18 /* int64 (8b) representing 1/2us since 01-01-1990 Z00:00:00 */
181 #define K12_PACKET_FRAME 0x20 /* start of the actual frame in the record */
183 #define K12_PACKET_OFFSET_VP 0x08 /* 2 bytes, big endian */
184 #define K12_PACKET_OFFSET_VC 0x0a /* 2 bytes, big endian */
185 #define K12_PACKET_OFFSET_CID 0x0c /* 1 byte */
187 /* elements of the source description records */
188 #define K12_SRCDESC_COLOR_FOREGROUND 0x12 /* 1 byte */
189 #define K12_SRCDESC_COLOR_BACKGROUND 0x13 /* 1 byte */
191 #define K12_SRCDESC_PORT_TYPE 0x1a /* 1 byte */
192 #define K12_SRCDESC_EXTRALEN 0x1e /* uint16, big endian */
193 #define K12_SRCDESC_NAMELEN 0x20 /* uint16, big endian */
194 #define K12_SRCDESC_STACKLEN 0x22 /* uint16, big endian */
196 #define K12_SRCDESC_EXTRATYPE 0x24 /* uint32, big endian */
197 #define K12_SRCDESC_ATM_VPI 0x38 /* uint16, big endian */
198 #define K12_SRCDESC_ATM_VCI 0x3a /* uint16, big endian */
200 #define K12_SRCDESC_ATM_AAL 0x3c /* 1 byte */
201 #define K12_SRCDESC_DS0_MASK 0x3c /* 1 byte */
205 * get_record: Get the next record into a buffer
206 * Every about 0x2000 bytes 0x10 bytes are inserted in the file,
207 * even in the middle of a record.
208 * This reads the next record without the eventual 0x10 bytes.
209 * returns the length of the record + the stuffing (if any)
211 * XXX: works at most with 0x1FFF bytes per record
213 static gint get_record(guint8** bufferp, FILE* fh, gint64 file_offset) {
214 static guint8* buffer = NULL;
215 static guint buffer_len = 0x2000 ;
218 guint actual_len, left;
222 /* where the next unknown 0x10 bytes are stuffed to the file */
223 guint junky_offset = 0x2000 - (gint) ( (file_offset - 0x200) % 0x2000 );
225 K12_DBG(6,("get_record: ENTER: junky_offset=%" G_GINT64_MODIFIER "d, file_offset=%" G_GINT64_MODIFIER "d",junky_offset,file_offset));
227 /* no buffer is given, lets create it */
228 if (buffer == NULL) {
229 buffer = g_malloc(0x2000);
235 if ( junky_offset == 0x2000 ) {
236 /* the length of the record is 0x10 bytes ahead from we are reading */
237 bytes_read = file_read(junk,1,0x14,fh);
239 if (bytes_read == 2 && junk[0] == 0xff && junk[1] == 0xff) {
240 K12_DBG(1,("get_record: EOF"));
242 } else if ( bytes_read < 0x14 ){
243 K12_DBG(1,("get_record: SHORT READ"));
247 memcpy(buffer,&(junk[0x10]),4);
249 /* the length of the record is right where we are reading */
250 bytes_read = file_read(buffer,1, 0x4, fh);
252 if (bytes_read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
253 K12_DBG(1,("get_record: EOF"));
255 } else if ( bytes_read != 0x4 ) {
256 K12_DBG(1,("get_record: SHORT READ"));
261 actual_len = left = pntohl(buffer);
264 K12_DBG(5,("get_record: GET length=%d",left));
268 while (left > buffer_len) *bufferp = buffer = g_realloc(buffer,buffer_len*=2);
274 K12_DBG(6,("get_record: looping left=%d junky_offset=%" G_GINT64_MODIFIER "d",left,junky_offset));
276 if (junky_offset > left) {
277 bytes_read += last_read = file_read(writep,1, left, fh);
279 if ( last_read != left ) {
280 K12_DBG(1,("get_record: SHORT READ"));
283 K12_HEXDMP(5,file_offset, "GOT record", buffer, actual_len);
287 bytes_read += last_read = file_read(writep,1, junky_offset, fh);
289 if ( last_read != junky_offset ) {
290 K12_DBG(1,("get_record: SHORT READ, read=%d expected=%d",last_read, junky_offset));
296 bytes_read += last_read = file_read(junk,1, 0x10, fh);
298 if ( last_read != 0x10 ) {
299 K12_DBG(1,("get_record: SHORT READ"));
303 left -= junky_offset;
304 junky_offset = 0x2000;
309 K12_HEXDMP(5,file_offset, "GOT record", buffer, actual_len);
313 static gboolean k12_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data_offset) {
314 k12_src_desc_t* src_desc;
315 guint8* buffer = NULL;
323 offset = wth->data_offset;
325 /* ignore the record if it isn't a packet */
327 K12_DBG(5,("k12_read: offset=%i",offset));
329 *data_offset = offset;
331 len = get_record(&buffer, wth->fh, offset);
334 *err = WTAP_ERR_SHORT_READ;
336 } else if (len == 0) {
341 type = pntohl(buffer + K12_RECORD_TYPE);
342 src_id = pntohl(buffer + K12_RECORD_SRC_ID);
345 if ( ! (src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(src_id))) ) {
347 * Some records from K15 files have a port ID of an undeclared
348 * interface which happens to be the only one with the first byte changed.
349 * It is still unknown how to recognize when this happens.
350 * If the lookup of the interface record fails we'll mask it
353 src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(src_id&K12_RECORD_SRC_ID_MASK));
356 K12_DBG(5,("k12_read: record type=%x src_id=%x",type,src_id));
360 } while ( ((type & K12_MASK_PACKET) != K12_REC_PACKET) || !src_id || !src_desc );
362 wth->data_offset = offset;
364 ts = pntohll(buffer + K12_PACKET_TIMESTAMP);
366 wth->phdr.ts.secs = (guint32) ((ts / 2000000) + 631152000);
367 wth->phdr.ts.nsecs = (guint32) ( (ts % 2000000) * 500 );
369 K12_DBG(3,("k12_read: PACKET RECORD type=%x src_id=%x secs=%u nsecs=%u",type,src_id, wth->phdr.ts.secs,wth->phdr.ts.nsecs));
371 wth->phdr.len = wth->phdr.caplen = pntohl(buffer + K12_RECORD_FRAME_LEN) & 0x00001FFF;
372 extra_len = len - K12_PACKET_FRAME - wth->phdr.caplen;
375 buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
376 memcpy(buffer_start_ptr(wth->frame_buffer), buffer + K12_PACKET_FRAME, wth->phdr.caplen);
378 /* extra information need by some protocols */
379 buffer_assure_space(&(wth->capture.k12->extra_info), extra_len);
380 memcpy(buffer_start_ptr(&(wth->capture.k12->extra_info)),
381 buffer + K12_PACKET_FRAME + wth->phdr.caplen, extra_len);
382 wth->pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(wth->capture.k12->extra_info));
383 wth->pseudo_header.k12.extra_length = extra_len;
385 wth->pseudo_header.k12.input = src_id;
387 K12_DBG(5,("k12_read: wth->pseudo_header.k12.input=%x wth->phdr.len=%i input_name='%s' stack_file='%s' type=%x",
388 wth->pseudo_header.k12.input,wth->phdr.len,src_desc->input_name,src_desc->stack_file,src_desc->input_type));\
390 wth->pseudo_header.k12.input_name = src_desc->input_name;
391 wth->pseudo_header.k12.stack_file = src_desc->stack_file;
392 wth->pseudo_header.k12.input_type = src_desc->input_type;
394 switch(src_desc->input_type) {
395 case K12_PORT_ATMPVC:
396 if ((long)(K12_PACKET_FRAME + wth->phdr.len + K12_PACKET_OFFSET_CID) < len) {
397 wth->pseudo_header.k12.input_info.atm.vp = pntohs(buffer + (K12_PACKET_FRAME + wth->phdr.caplen + K12_PACKET_OFFSET_VP));
398 wth->pseudo_header.k12.input_info.atm.vc = pntohs(buffer + (K12_PACKET_FRAME + wth->phdr.caplen + K12_PACKET_OFFSET_VC));
399 wth->pseudo_header.k12.input_info.atm.cid = *((unsigned char*)(buffer + K12_PACKET_FRAME + wth->phdr.len + K12_PACKET_OFFSET_CID));
404 memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
409 wth->pseudo_header.k12.stuff = wth->capture.k12;
415 static gboolean k12_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err _U_, gchar **err_info _U_) {
416 k12_src_desc_t* src_desc;
422 K12_DBG(5,("k12_seek_read: ENTER"));
424 if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
425 K12_DBG(5,("k12_seek_read: SEEK ERROR"));
429 if ((len = get_record(&buffer, wth->random_fh, seek_off)) < 1) {
430 K12_DBG(5,("k12_seek_read: READ ERROR"));
434 memcpy(pd, buffer + K12_PACKET_FRAME, length);
436 extra_len = len - K12_PACKET_FRAME - length;
437 buffer_assure_space(&(wth->capture.k12->extra_info), extra_len);
438 memcpy(buffer_start_ptr(&(wth->capture.k12->extra_info)),
439 buffer + K12_PACKET_FRAME + length, extra_len);
440 wth->pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(wth->capture.k12->extra_info));
441 wth->pseudo_header.k12.extra_length = extra_len;
443 pseudo_header->k12.extra_info = (void*)buffer_start_ptr(&(wth->capture.k12->extra_info));
444 pseudo_header->k12.extra_length = extra_len;
447 input = pntohl(buffer + K12_RECORD_SRC_ID);
448 K12_DBG(5,("k12_seek_read: input=%.8x",input));
450 if ( ! (src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(input))) ) {
452 * Some records from K15 files have a port ID of an undeclared
453 * interface which happens to be the only one with the first byte changed.
454 * It is still unknown how to recognize when this happens.
455 * If the lookup of the interface record fails we'll mask it
458 src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(input&K12_RECORD_SRC_ID_MASK));
462 K12_DBG(5,("k12_seek_read: input_name='%s' stack_file='%s' type=%x",src_desc->input_name,src_desc->stack_file,src_desc->input_type));
464 pseudo_header->k12.input_name = src_desc->input_name;
465 pseudo_header->k12.stack_file = src_desc->stack_file;
466 pseudo_header->k12.input_type = src_desc->input_type;
468 switch(src_desc->input_type) {
469 case K12_PORT_ATMPVC:
470 if ((long)(K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID) < len) {
471 pseudo_header->k12.input_info.atm.vp = pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VP);
472 pseudo_header->k12.input_info.atm.vc = pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VC);
473 pseudo_header->k12.input_info.atm.cid = *((unsigned char*)(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID));
478 memcpy(&(pseudo_header->k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
483 wth->pseudo_header.k12.input_name = src_desc->input_name;
484 wth->pseudo_header.k12.stack_file = src_desc->stack_file;
485 wth->pseudo_header.k12.input_type = src_desc->input_type;
487 switch(src_desc->input_type) {
488 case K12_PORT_ATMPVC:
489 if ((long)(K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID) < len) {
490 wth->pseudo_header.k12.input_info.atm.vp = pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VP);
491 wth->pseudo_header.k12.input_info.atm.vc = pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VC);
492 wth->pseudo_header.k12.input_info.atm.cid = *((unsigned char*)(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID));
497 memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
502 K12_DBG(5,("k12_seek_read: NO SRC_RECORD FOUND"));
505 memset(&(pseudo_header->k12),0,sizeof(pseudo_header->k12));
506 pseudo_header->k12.input_name = "unknown port";
507 pseudo_header->k12.stack_file = "unknown stack file";
510 memset(&(wth->pseudo_header.k12),0,sizeof(wth->pseudo_header.k12));
511 wth->pseudo_header.k12.input_name = "unknown port";
512 wth->pseudo_header.k12.stack_file = "unknown stack file";
517 pseudo_header->k12.input = input;
518 pseudo_header->k12.stuff = wth->capture.k12;
521 wth->pseudo_header.k12.input = input;
522 wth->pseudo_header.k12.stuff = wth->capture.k12;
524 K12_DBG(5,("k12_seek_read: DONE OK"));
530 static k12_t* new_k12_file_data(void) {
531 k12_t* fd = g_malloc(sizeof(k12_t));
534 fd->num_of_records = 0;
535 fd->src_by_name = g_hash_table_new(g_str_hash,g_str_equal);
536 fd->src_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
538 buffer_init(&(fd->extra_info), 100);
543 static gboolean destroy_srcdsc(gpointer k _U_, gpointer v, gpointer p _U_) {
544 k12_src_desc_t* rec = v;
547 g_free(rec->input_name);
550 g_free(rec->stack_file);
557 static void destroy_k12_file_data(k12_t* fd) {
558 g_hash_table_destroy(fd->src_by_id);
559 g_hash_table_foreach_remove(fd->src_by_name,destroy_srcdsc,NULL);
560 g_hash_table_destroy(fd->src_by_name);
561 buffer_free(&(fd->extra_info));
565 static void k12_close(wtap *wth) {
566 destroy_k12_file_data(wth->capture.k12);
568 K12_DBG(5,("k12_close: CLOSED"));
569 if (env_file) fclose(dbg_out);
574 int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
576 guint8 header_buffer[0x200];
589 gchar* env_level = getenv("K12_DEBUG_LEVEL");
590 env_file = getenv("K12_DEBUG_FILENAME");
591 if ( env_file ) dbg_out = ws_fopen(env_file,"w");
592 else dbg_out = stderr;
593 if ( env_level ) debug_level = strtoul(env_level,NULL,10);
594 K12_DBG(1,("k12_open: ENTER debug_level=%u",debug_level));
597 if ( file_read(header_buffer,1,0x200,wth->fh) != 0x200 ) {
598 K12_DBG(1,("k12_open: FILE HEADER TOO SHORT"));
601 if ( memcmp(header_buffer,k12_file_magic,8) != 0 ) {
602 K12_DBG(1,("k12_open: BAD MAGIC"));
609 file_data = new_k12_file_data();
611 file_data->file_len = pntohl( header_buffer + 0x8);
612 file_data->num_of_records = pntohl( header_buffer + 0xC );
614 K12_DBG(5,("k12_open: FILE_HEADER OK: offset=%x file_len=%i records=%i",
617 file_data->num_of_records ));
621 len = get_record(&read_buffer, wth->fh, offset);
624 K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
629 type = pntohl( read_buffer + K12_RECORD_TYPE );
631 if ( (type & K12_MASK_PACKET) == K12_REC_PACKET) {
633 * we are at the first packet record, rewind and leave.
635 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
636 destroy_k12_file_data(file_data);
639 K12_DBG(5,("k12_open: FIRST PACKET offset=%x",offset));
641 } else if (type == K12_REC_SRCDSC || type == K12_REC_SRCDSC2 ) {
642 rec = g_malloc0(sizeof(k12_src_desc_t));
644 rec_len = pntohl( read_buffer + K12_RECORD_LEN );
645 extra_len = pntohs( read_buffer + K12_SRCDESC_EXTRALEN );
646 name_len = pntohs( read_buffer + K12_SRCDESC_NAMELEN );
647 stack_len = pntohs( read_buffer + K12_SRCDESC_STACKLEN );
649 rec->input = pntohl( read_buffer + K12_RECORD_SRC_ID );
651 K12_DBG(5,("k12_open: INTERFACE RECORD offset=%x interface=%x",offset,rec->input));
653 if (name_len == 0 || stack_len == 0
654 || 0x20 + extra_len + name_len + stack_len > rec_len ) {
656 K12_DBG(5,("k12_open: failed (name_len == 0 || stack_len == 0 "
657 "|| 0x20 + extra_len + name_len + stack_len > rec_len) extra_len=%i name_len=%i stack_len=%i"));
662 switch(( rec->input_type = pntohl( read_buffer + K12_SRCDESC_EXTRATYPE ) )) {
664 rec->input_info.ds0mask = 0x00000000;
666 for (i = 0; i < 32; i++) {
667 rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 0x1<<(31-i) : 0x0;
671 case K12_PORT_ATMPVC:
672 rec->input_info.atm.vp = pntohs( read_buffer + K12_SRCDESC_ATM_VPI );
673 rec->input_info.atm.vc = pntohs( read_buffer + K12_SRCDESC_ATM_VCI );
678 else { /* Record viewer generated files
679 don't have this information */
680 if (read_buffer[K12_SRCDESC_PORT_TYPE] >= 0x14
681 && read_buffer[K12_SRCDESC_PORT_TYPE] <= 0x17)
682 /* For ATM2_E1DS1, ATM2_E3DS3,
683 ATM2_STM1EL and ATM2_STM1OP */
684 rec->input_type = K12_PORT_ATMPVC;
687 /* XXX - this is assumed, in a number of places (not just in the
688 ascii_strdown_inplace() call below) to be null-terminated;
689 is that guaranteed (even with a corrupt file)? */
690 rec->input_name = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len, name_len);
691 rec->stack_file = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len + name_len, stack_len);
693 ascii_strdown_inplace (rec->stack_file);
695 g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec);
696 g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec);
706 wth->data_offset = offset;
707 wth->file_type = WTAP_FILE_K12;
708 wth->file_encap = WTAP_ENCAP_K12;
709 wth->snapshot_length = 0;
710 wth->subtype_read = k12_read;
711 wth->subtype_seek_read = k12_seek_read;
712 wth->subtype_close = k12_close;
713 wth->capture.k12 = file_data;
714 wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
719 int k12_dump_can_write_encap(int encap) {
721 if (encap == WTAP_ENCAP_PER_PACKET)
722 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
724 if (encap != WTAP_ENCAP_K12)
725 return WTAP_ERR_UNSUPPORTED_ENCAP;
730 static const gchar dumpy_junk[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
732 static gboolean do_fwrite(const void *data, size_t size, size_t count, FILE *stream, int *err_p) {
735 nwritten = fwrite(data, size, count, stream);
736 if (nwritten != count) {
737 if (nwritten == 0 && ferror(stream))
740 *err_p = WTAP_ERR_SHORT_WRITE;
746 static gboolean k12_dump_record(wtap_dumper *wdh, long len, guint8* buffer, int *err_p) {
747 long junky_offset = (0x2000 - ( (wdh->dump.k12->file_offset - 0x200) % 0x2000 )) % 0x2000;
749 if (len > junky_offset) {
751 if (! do_fwrite(buffer, 1, junky_offset, wdh->fh, err_p))
754 if (! do_fwrite(dumpy_junk, 1, 0x10, wdh->fh, err_p))
757 if (! do_fwrite(buffer+junky_offset, 1, len - junky_offset, wdh->fh, err_p))
760 wdh->dump.k12->file_offset += len + 0x10;
762 if (! do_fwrite(buffer, 1, len, wdh->fh, err_p))
764 wdh->dump.k12->file_offset += len;
767 wdh->dump.k12->num_of_records++;
771 static void k12_dump_src_setting(gpointer k _U_, gpointer v, gpointer p) {
772 k12_src_desc_t* src_desc = v;
773 wtap_dumper *wdh = p;
777 int errxxx; /* dummy */
780 guint8 buffer[0x2000];
808 guint8 unk_data[0x10];
819 obj.record.type = g_htonl(K12_REC_SRCDSC);
820 obj.record.unk32_1 = g_htonl(0x00000001);
821 obj.record.input = g_htonl(src_desc->input);
823 obj.record.unk32_2 = g_htons(0x0000);
824 obj.record.color = g_htons(0x060f);
825 obj.record.unk32_3 = g_htonl(0x00000003);
826 switch (src_desc->input_type) {
827 case K12_PORT_ATMPVC:
828 obj.record.unk32_4 = g_htonl(0x01001400);
831 obj.record.unk32_4 = g_htonl(0x01000100);
834 obj.record.unk16_1 = g_htons(0x0000);
835 obj.record.name_len = (guint16) strlen(src_desc->input_name) + 1;
836 obj.record.stack_len = (guint16) strlen(src_desc->stack_file) + 1;
838 obj.record.extra.type = g_htonl(src_desc->input_type);
840 switch (src_desc->input_type) {
841 case K12_PORT_ATMPVC:
842 obj.record.extra_len = g_htons(0x18);
843 obj.record.extra.desc.atm.vp = g_htons(src_desc->input_info.atm.vp);
844 obj.record.extra.desc.atm.vc = g_htons(src_desc->input_info.atm.vc);
848 obj.record.extra_len = g_htons(0x18);
849 for( i=0; i<32; i++ ) {
850 obj.record.extra.desc.ds0mask.mask[i] =
851 (src_desc->input_info.ds0mask & (1 << i)) ? 0xff : 0x00;
856 obj.record.extra_len = g_htons(0x08);
861 memcpy(obj.buffer + offset,
862 src_desc->input_name,
863 obj.record.name_len);
865 memcpy(obj.buffer + offset + obj.record.name_len,
866 src_desc->stack_file,
867 obj.record.stack_len);
869 len = offset + obj.record.name_len + obj.record.stack_len;
870 len += (len % 4) ? 4 - (len % 4) : 0;
872 obj.record.len = g_htonl(len);
873 obj.record.name_len = g_htons(obj.record.name_len);
874 obj.record.stack_len = g_htons(obj.record.stack_len);
876 k12_dump_record(wdh,len,obj.buffer, &errxxx); /* fwrite errs ignored: see k12_dump below */
879 static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
880 const union wtap_pseudo_header *pseudo_header,
881 const guchar *pd, int *err) {
884 guint8 buffer[0x2000];
895 guint8 frame[0x1fc0];
899 if (wdh->dump.k12->num_of_records == 0) {
900 k12_t* file_data = pseudo_header->k12.stuff;
901 /* XXX: We'll assume that any fwrite errors in k12_dump_src_setting will */
902 /* repeat during the final k12_dump_record at the end of k12_dump */
903 /* (and thus cause an error return from k12_dump). */
904 /* (I don't see a reasonably clean way to handle any fwrite errors */
905 /* encountered in k12_dump_src_setting). */
906 g_hash_table_foreach(file_data->src_by_id,k12_dump_src_setting,wdh);
908 obj.record.len = 0x20 + phdr->len;
909 obj.record.len += (obj.record.len % 4) ? 4 - obj.record.len % 4 : 0;
911 len = obj.record.len;
913 obj.record.len = g_htonl(obj.record.len);
915 obj.record.type = g_htonl(K12_REC_PACKET);
916 obj.record.frame_len = g_htonl(phdr->len);
917 obj.record.input = g_htonl(pseudo_header->k12.input);
919 obj.record.ts = GUINT64_TO_BE((((guint64)phdr->ts.secs - 631152000) * 2000000) + (phdr->ts.nsecs / 1000 * 2));
921 memcpy(obj.record.frame,pd,phdr->len);
923 return k12_dump_record(wdh,len,obj.buffer, err);
926 static const guint8 k12_eof[] = {0xff,0xff};
928 static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
930 guint8 b[sizeof(guint32)];
934 if (! do_fwrite(k12_eof, 1, 2, wdh->fh, err))
937 if (fseek(wdh->fh, 8, SEEK_SET) == -1) {
942 d.u = g_htonl(wdh->dump.k12->file_len);
944 if (! do_fwrite(d.b, 1, 4, wdh->fh, err))
947 d.u = g_htonl(wdh->dump.k12->num_of_records);
949 if (! do_fwrite(d.b, 1, 4, wdh->fh, err))
956 gboolean k12_dump_open(wtap_dumper *wdh, gboolean cant_seek, int *err) {
959 *err = WTAP_ERR_CANT_WRITE_TO_PIPE;
963 if ( ! do_fwrite(k12_file_magic, 1, 8, wdh->fh, err)) {
967 if (fseek(wdh->fh, 0x200, SEEK_SET) == -1) {
972 wdh->subtype_write = k12_dump;
973 wdh->subtype_close = k12_dump_close;
975 wdh->dump.k12 = g_malloc(sizeof(k12_dump_t));
976 wdh->dump.k12->file_len = 0x200;
977 wdh->dump.k12->num_of_records = 0;
978 wdh->dump.k12->file_offset = 0x200;