More updates to the wtap_open_offline() comment.
[obnox/wireshark/wip.git] / wiretap / k12text.l
1 /*
2  * We don't use unput, so don't generate code for it.
3  */
4 %option nounput
5
6 /*
7  * We don't read from the terminal.
8  */
9 %option never-interactive
10
11 /*
12  * Prefix scanner routines with "K12Text_" rather than "yy", so this scanner
13  * can coexist with other scanners.
14  */
15 %option prefix="K12Text_"
16
17 %option outfile="k12text.c"
18
19 /* Options useful for debugging                    */
20 /* noline:  Prevent generation of #line directives */
21 /*          Seems to be required when using the    */
22 /*           Windows VS debugger so as to be able  */
23 /*           to properly step through the code and */
24 /*           set breakpoints & etc using the       */
25 /*           k12text.c file rather than the        */
26 /*           k12text.l file                        */
27 /*     XXX: %option noline gives an error message: */
28 /*          "unrecognized %option: line"           */
29 /*          with flex 2.5.35; the --noline         */
30 /*          command-line option works OK.          */
31 /*                                                 */
32 /* debug:   Do output of "rule acceptance" info    */
33 /*           during parse                          */ 
34 /*                                                 */
35 /* %option noline  */
36 /* %option debug   */
37
38 %{
39 /* k12text.l
40  *
41  * $Id$
42  *
43  * Wiretap Library
44  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
45  *
46  * This program is free software; you can redistribute it and/or
47  * modify it under the terms of the GNU General Public License
48  * as published by the Free Software Foundation; either version 2
49  * of the License, or (at your option) any later version.
50  *
51  * This program is distributed in the hope that it will be useful,
52  * but WITHOUT ANY WARRANTY; without even the implied warranty of
53  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
54  * GNU General Public License for more details.
55  *
56  * You should have received a copy of the GNU General Public License
57  * along with this program; if not, write to the Free Software
58  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
59  */
60
61  /*
62   * TODO:
63   *   - fix timestamps after midnight
64   *   - verify encapsulations
65   */
66
67 #ifdef HAVE_CONFIG_H
68 #include "config.h"
69 #endif
70 #include <stdlib.h>
71 #include <string.h>
72 #include <errno.h>
73 #include <time.h>
74 #include "wtap-int.h"
75 #include "wtap.h"
76 #include "file_wrappers.h"
77 #include "buffer.h"
78 #include "k12.h"
79 #include "k12text_lex.h"
80
81 #ifndef HAVE_UNISTD_H
82 #define YY_NO_UNISTD_H
83 #endif
84
85 static guint g_h;
86 static guint g_m;
87 static guint g_s;
88 static guint g_ms;
89 static guint g_ns;
90 static gint g_encap;
91 static guint8 bb[WTAP_MAX_PACKET_SIZE];
92 static guint ii;
93 static gboolean is_k12text;
94 static gboolean at_eof;
95 static guint junk_chars;
96 static void finalize_frame(void);
97 static gchar* error_str;
98 static guint file_bytes_read;
99 static gboolean ok_frame;
100 static FILE_T yy_fh;
101
102 #define KERROR(text) do { error_str = g_strdup(text); yyterminate(); } while(0)
103 #define SET_HOURS(text) g_h = strtoul(text,NULL,10)
104 #define SET_MINUTES(text) g_m = strtoul(text,NULL,10)
105 #define SET_SECONDS(text) g_s = strtoul(text,NULL,10)
106 #define SET_MS(text) g_ms = strtoul(text,NULL,10)
107 #define SET_NS(text) g_ns = strtoul(text,NULL,10)
108 #define ADD_BYTE(text) do {if (ii >= WTAP_MAX_PACKET_SIZE) {KERROR("frame too large");} bb[ii++] = (guint8)strtoul(text,NULL,16); } while(0)
109 #define FINALIZE_FRAME() finalize_frame()
110 /*~ #define ECHO*/
111 #define YY_USER_ACTION file_bytes_read += yyleng;
112 #define YY_INPUT(buf,result,max_size) { int c = file_getc(yy_fh);  result = (c==EOF) ? YY_NULL : (buf[0] = c, 1); }
113
114 #define MAX_JUNK 400000
115 #define ECHO
116 %}
117 start_timestamp \053[\055]{9}\053[\055]{15,100}\053[\055]{10,100}\053
118 oneormoredigits [0-9]+:
119 twodigits [0-9][0-9]
120 colon :
121 comma ,
122 threedigits [0-9][0-9][0-9]
123 start_bytes \174\060\040\040\040\174
124 bytes_junk \174[A-F0-9][A-F0-9\040][A-F0-9\040][A-F0-9\040]\174
125 byte [a-f0-9][a-f0-9]\174
126 end_bytes \015?\012\015?\012
127 eth ETHER
128  /* mtp2 MTP-L2 ;; XXX: Not supported until it's determined how to fill in the pseudo_header req'd by packet-mtp2 */
129  /* sscop SSCOP ;; XXX: Not supported until it's determined how to fill in the pseudo_header req'd by packet-atm  */
130 sscfnni SSCF
131 hdlc HDLC
132
133 %START MAGIC NEXT_FRAME HOURS MINUTES M2S SECONDS S2M MS M2N NS ENCAP STARTBYTES BYTE
134 %%
135 <MAGIC>{start_timestamp}  { is_k12text = TRUE; yyterminate(); }
136
137 <MAGIC>. { if (++ junk_chars > MAX_JUNK) { is_k12text = FALSE;  yyterminate(); } }
138
139 <NEXT_FRAME>{start_timestamp} {BEGIN(HOURS); }
140 <HOURS>{oneormoredigits} { SET_HOURS(yytext); BEGIN(MINUTES); }
141 <MINUTES>{twodigits} { SET_MINUTES(yytext); BEGIN(M2S);}
142 <M2S>{colon} { BEGIN(SECONDS);}
143 <SECONDS>{twodigits} { SET_SECONDS(yytext); BEGIN(S2M); }
144 <S2M>{comma}  { BEGIN(MS); }
145 <MS>{threedigits} { SET_MS(yytext); BEGIN(M2N);  }
146 <M2N>{comma}  { BEGIN(NS); }
147 <NS>{threedigits} { SET_NS(yytext); BEGIN(ENCAP);}
148 <ENCAP>{eth} {g_encap = WTAP_ENCAP_ETHERNET; BEGIN(STARTBYTES); }
149  /* <ENCAP>{mtp2} {g_encap = WTAP_ENCAP_MTP2; BEGIN(STARTBYTES); }      Not supported as yet */
150  /* <ENCAP>{sscop} {g_encap = WTAP_ENCAP_ATM_PDUS; BEGIN(STARTBYTES); } Not supported as yet */
151 <ENCAP>{sscfnni} {g_encap = WTAP_ENCAP_MTP3; BEGIN(STARTBYTES); }
152 <ENCAP>{hdlc} {g_encap = WTAP_ENCAP_CHDLC; BEGIN(STARTBYTES); }
153 <ENCAP,STARTBYTES>{start_bytes} { BEGIN(BYTE); }
154 <BYTE>{byte} { ADD_BYTE(yytext); }
155 <BYTE>{bytes_junk} ;
156 <BYTE>{end_bytes} { FINALIZE_FRAME(); yyterminate(); }
157
158 . {  if (++junk_chars > MAX_JUNK) { KERROR("too much junk");  } }
159 <<EOF>> { at_eof = TRUE; yyterminate(); }
160
161 %%
162
163 static void finalize_frame(void) {
164         ok_frame = TRUE;
165 }
166
167 /* Fill in pseudo-header according to the encapsulation type             */
168
169 static void k12text_set_pseudo_header(wtap *wth, union wtap_pseudo_header *pseudo_header) {
170         /* The file-encap is WTAP_ENCAP_PER_PACKET */
171         switch(wth->phdr.pkt_encap) {
172             case WTAP_ENCAP_ETHERNET:
173                     pseudo_header->eth.fcs_len = 0;
174                     break;
175             case WTAP_ENCAP_MTP3:
176             case WTAP_ENCAP_CHDLC:
177                     /* no pseudo_header to fill in for these types */
178                     break;
179 #if 0
180             case WTAP_ENCAP_MTP2:      /* not (yet) supported       */
181             case WTAP_ENCAP_ATM_PDUS:  /* not (yet) supported       */
182                     /* XXX: I don't know how to fill in the         */
183                     /* pseudo_header for these types. So: The Lexer */
184                     /* has recognition for these types commented    */
185                     /* out ....                                     */
186                     break;
187 #endif
188             case WTAP_ENCAP_UNKNOWN:
189             default:
190                     break;
191         }
192 }
193
194 /* Note: k12text_reset is called each time data is to be processed from  */
195 /*       a file. This ensures that no "state" from a previous read is    */
196 /*       used (such as the lexer look-ahead buffer, file_handle, file    */
197 /*       position and so on. This allows a single lexer buffer to be     */
198 /*       used even when multiple files are open simultaneously (as for   */
199 /*       a file merge).                                                  */
200
201 static void k12text_reset(FILE_T fh) {
202         yy_fh = fh;
203         yyrestart(0);
204         g_encap = WTAP_ENCAP_UNKNOWN;
205         ok_frame = FALSE;
206         is_k12text = FALSE;
207         at_eof = FALSE;
208         junk_chars = 0;
209         error_str = NULL;
210         file_bytes_read=0;
211         g_h=0;
212         g_m=0;
213         g_s=0;
214         g_ns=0;
215         g_ms=0;
216         ii=0;
217 }
218
219 static gboolean k12text_read(wtap *wth, int *err, char ** err_info, gint64 *data_offset) {
220
221         /* We seek to the file position after the end of the previous frame processed by         */
222         /* k12text_read (kept in wth->data_offset). We do this each time since the lexer         */
223         /* undoubtedly did some amount of look-ahead when processing the previous frame.         */ 
224         /* We also clear out any lexer state (eg: look-ahead buffer) and init vars set by lexer. */
225
226         if ( file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
227                 return FALSE;
228         }
229         k12text_reset(wth->fh);          /* init lexer buffer and vars set by lexer */
230
231         BEGIN(NEXT_FRAME);
232         yylex();
233
234         if (ok_frame == FALSE) {
235                 if (at_eof) {
236                         *err = 0;
237                         *err_info = NULL;
238                 } else {
239                         *err = WTAP_ERR_BAD_RECORD;
240                         *err_info = error_str;
241                 }
242                 return FALSE;
243         }
244
245         *data_offset = wth->data_offset;       /* file position for beginning of this frame   */
246         wth->data_offset += file_bytes_read;   /* file position after end of this frame       */
247
248         wth->phdr.ts.secs = 946681200 + (3600*g_h) + (60*g_m) + g_s;
249         wth->phdr.ts.nsecs = 1000000*g_ms + 1000*g_ns;
250
251         wth->phdr.caplen = wth->phdr.len = ii;
252
253         wth->phdr.pkt_encap = g_encap;
254         k12text_set_pseudo_header(wth, &wth->pseudo_header);
255
256         buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
257         memcpy(buffer_start_ptr(wth->frame_buffer), bb, wth->phdr.caplen);
258
259         return TRUE;
260 }
261
262 static gboolean k12text_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header, guint8 *pd, int length, int *err, char **err_info) {
263
264         if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
265                 return FALSE;
266         }
267         k12text_reset(wth->random_fh);            /* init lexer buffer and vars set by lexer */
268
269         BEGIN(NEXT_FRAME);
270         yylex();
271
272         if (ok_frame == FALSE) {
273                 *err = WTAP_ERR_BAD_RECORD;
274                 if (at_eof) {
275                         /* What happened ? The desired frame was previously read without a problem */
276                         *err_info = g_strdup("Unexpected EOF (program error ?)");
277                 } else {
278                         *err_info = error_str;
279                 }
280                 return FALSE;
281         }
282
283         /* verify frame length parsed this time against original frame length */
284         if (ii != (guint)length) {
285                 /* What happened ? This now seems to have a different length than originally */
286                 *err = WTAP_ERR_BAD_RECORD;
287                 *err_info = g_strdup("Incorrect frame length (program error ?)");
288                 return FALSE;
289         }
290
291         k12text_set_pseudo_header(wth, pseudo_header);
292
293         memcpy(pd, bb, length);
294
295         return TRUE;
296 }
297
298 int k12text_open(wtap *wth, int *err, gchar **err_info _U_) {
299
300         k12text_reset(wth->fh);       /* init lexer buffer and vars set by lexer */
301
302         BEGIN(MAGIC);
303         yylex();
304
305         if (! is_k12text) return 0;
306
307         if ( file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
308                 return -1;
309         }
310
311         wth->data_offset = 0;
312         wth->file_type = WTAP_FILE_K12TEXT;
313         wth->file_encap = WTAP_ENCAP_PER_PACKET;
314         wth->snapshot_length = 0;
315         wth->subtype_read = k12text_read;
316         wth->subtype_seek_read = k12text_seek_read;
317         wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
318
319         return 1;
320 }
321
322
323 static const struct { int e; const char* s; } encaps[] = {
324         { WTAP_ENCAP_ETHERNET, "ETHER" },
325         { WTAP_ENCAP_MTP2, "MTP-L2" },
326         { WTAP_ENCAP_ATM_PDUS, "SSCOP" },
327         { WTAP_ENCAP_MTP3, "SSCF" },
328         { WTAP_ENCAP_CHDLC, "HDLC" },
329         /* ... */
330         { WTAP_ENCAP_UNKNOWN, "UNKNOWN" },
331         { 0, NULL }
332 };
333
334 static gboolean k12text_dump(wtap_dumper *wdh _U_, const struct wtap_pkthdr *phdr,
335                          const union wtap_pseudo_header *pseudo_header _U_,
336                          const guchar *pd, int *err) {
337         char buf[196808];
338         size_t left = 196808;
339         gint wl;
340         char* p=buf;
341         const char* str_enc = "";
342         guint i;
343         guint ns;
344         guint ms;
345
346         ms = phdr->ts.nsecs / 1000000;
347         ns = (phdr->ts.nsecs - (1000000*ms))/1000;
348
349         for(i=0; encaps[i].s; i++) {
350                 str_enc = encaps[i].s;
351                 if (phdr->pkt_encap == encaps[i].e) break;
352         }
353
354         strftime(p,90,"+---------+---------------+----------+\r\n%H:%M:%S,",gmtime(&phdr->ts.secs));
355         wl = strlen(p);
356         p += wl;
357         left -= wl;
358
359         wl = g_snprintf(p,left,"%.3d,%.3d   %s\r\n|0   |",ms,ns,str_enc);
360         p+= wl;
361         left -= wl;
362
363         for(i=0;i < phdr->caplen && left > 2; i++) {
364                 wl = g_snprintf(p,left,"%.2x|",pd[i]);
365                 p += wl;
366                 left -= wl;
367         }
368
369         wl = g_snprintf(p,left,"\r\n\r\n");
370         p+= wl;
371         left -= wl;
372
373         return wtap_dump_file_write(wdh, buf, strlen(buf), err);
374 }
375
376
377 gboolean k12text_dump_open(wtap_dumper *wdh, gboolean cant_seek, int *err) {
378
379     if (cant_seek) {
380         *err = WTAP_ERR_CANT_WRITE_TO_PIPE;
381         return FALSE;
382     }
383
384     wdh->subtype_write = k12text_dump;
385
386     return TRUE;
387 }
388
389 int k12text_dump_can_write_encap(int encap) {
390     switch (encap) {
391         case WTAP_ENCAP_PER_PACKET:
392         case WTAP_ENCAP_ETHERNET:
393         case WTAP_ENCAP_MTP3:
394         case WTAP_ENCAP_CHDLC:
395         case WTAP_ENCAP_UNKNOWN:
396                 return 0;
397         case WTAP_ENCAP_MTP2:
398         case WTAP_ENCAP_ATM_PDUS:
399         default:
400                 return WTAP_ERR_UNSUPPORTED_ENCAP;
401     }
402 }
403
404 /*
405  * We want to stop processing when we get to the end of the input.
406  * (%option noyywrap is not used because if used then 
407  * some flex versions (eg: 2.5.35) generate code which causes
408  * warnings by the Windows VC compiler).
409  */
410
411 int yywrap(void) {
412     return 1;
413 }