2 * We don't use input, so don't generate code for it.
7 * We don't use unput, so don't generate code for it.
12 * We don't read from the terminal.
14 %option never-interactive
17 * Prefix scanner routines with "K12Text_" rather than "yy", so this scanner
18 * can coexist with other scanners.
20 %option prefix="K12Text_"
22 %option outfile="k12text.c"
24 /* Options useful for debugging */
25 /* noline: Prevent generation of #line directives */
26 /* Seems to be required when using the */
27 /* Windows VS debugger so as to be able */
28 /* to properly step through the code and */
29 /* set breakpoints & etc using the */
30 /* k12text.c file rather than the */
32 /* XXX: %option noline gives an error message: */
33 /* "unrecognized %option: line" */
34 /* with flex 2.5.35; the --noline */
35 /* command-line option works OK. */
37 /* debug: Do output of "rule acceptance" info */
49 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
51 * This program is free software; you can redistribute it and/or
52 * modify it under the terms of the GNU General Public License
53 * as published by the Free Software Foundation; either version 2
54 * of the License, or (at your option) any later version.
56 * This program is distributed in the hope that it will be useful,
57 * but WITHOUT ANY WARRANTY; without even the implied warranty of
58 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59 * GNU General Public License for more details.
61 * You should have received a copy of the GNU General Public License
62 * along with this program; if not, write to the Free Software
63 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
68 * - fix timestamps after midnight
69 * - verify encapsulations
79 #include "file_wrappers.h"
82 #include "k12text_lex.h"
85 #define YY_NO_UNISTD_H
94 static guint8 bb[WTAP_MAX_PACKET_SIZE];
96 static gboolean is_k12text;
97 static gboolean at_eof;
98 static guint junk_chars;
99 static void finalize_frame(void);
100 static gchar* error_str;
101 static guint file_bytes_read;
102 static gboolean ok_frame;
105 #define KERROR(text) do { error_str = g_strdup(text); yyterminate(); } while(0)
106 #define SET_HOURS(text) g_h = (guint) strtoul(text,NULL,10)
107 #define SET_MINUTES(text) g_m = (guint) strtoul(text,NULL,10)
108 #define SET_SECONDS(text) g_s = (guint) strtoul(text,NULL,10)
109 #define SET_MS(text) g_ms = (guint) strtoul(text,NULL,10)
110 #define SET_NS(text) g_ns = (guint) strtoul(text,NULL,10)
111 #define ADD_BYTE(text) do {if (ii >= WTAP_MAX_PACKET_SIZE) {KERROR("frame too large");} bb[ii++] = (guint8)strtoul(text,NULL,16); } while(0)
112 #define FINALIZE_FRAME() finalize_frame()
114 #define YY_USER_ACTION file_bytes_read += yyleng;
115 #define YY_INPUT(buf,result,max_size) { int c = file_getc(yy_fh); result = (c==EOF) ? YY_NULL : (buf[0] = c, 1); }
117 #define MAX_JUNK 400000
121 * Private per-file data.
125 * The file position after the end of the previous frame processed by
128 * We need to keep this around, and seek to it at the beginning of
129 * each call to k12text_read(), since the lexer undoubtedly did some
130 * amount of look-ahead when processing the previous frame.
132 gint64 next_frame_offset;
135 start_timestamp \053[\055]{9}\053[\055]{15,100}\053[\055]{10,100}\053
136 oneormoredigits [0-9]+:
140 threedigits [0-9][0-9][0-9]
141 start_bytes \174\060\040\040\040\174
142 bytes_junk \174[A-F0-9][A-F0-9\040][A-F0-9\040][A-F0-9\040]\174
143 byte [a-f0-9][a-f0-9]\174
144 end_bytes \015?\012\015?\012
146 /* mtp2 MTP-L2 ;; XXX: Not supported until it's determined how to fill in the pseudo_header req'd by packet-mtp2 */
147 /* sscop SSCOP ;; XXX: Not supported until it's determined how to fill in the pseudo_header req'd by packet-atm */
151 %START MAGIC NEXT_FRAME HOURS MINUTES M2S SECONDS S2M MS M2N NS ENCAP STARTBYTES BYTE
153 <MAGIC>{start_timestamp} { is_k12text = TRUE; yyterminate(); }
155 <MAGIC>. { if (++ junk_chars > MAX_JUNK) { is_k12text = FALSE; yyterminate(); } }
157 <NEXT_FRAME>{start_timestamp} {BEGIN(HOURS); }
158 <HOURS>{oneormoredigits} { SET_HOURS(yytext); BEGIN(MINUTES); }
159 <MINUTES>{twodigits} { SET_MINUTES(yytext); BEGIN(M2S);}
160 <M2S>{colon} { BEGIN(SECONDS);}
161 <SECONDS>{twodigits} { SET_SECONDS(yytext); BEGIN(S2M); }
162 <S2M>{comma} { BEGIN(MS); }
163 <MS>{threedigits} { SET_MS(yytext); BEGIN(M2N); }
164 <M2N>{comma} { BEGIN(NS); }
165 <NS>{threedigits} { SET_NS(yytext); BEGIN(ENCAP);}
166 <ENCAP>{eth} {g_encap = WTAP_ENCAP_ETHERNET; BEGIN(STARTBYTES); }
167 /* <ENCAP>{mtp2} {g_encap = WTAP_ENCAP_MTP2; BEGIN(STARTBYTES); } Not supported as yet */
168 /* <ENCAP>{sscop} {g_encap = WTAP_ENCAP_ATM_PDUS; BEGIN(STARTBYTES); } Not supported as yet */
169 <ENCAP>{sscfnni} {g_encap = WTAP_ENCAP_MTP3; BEGIN(STARTBYTES); }
170 <ENCAP>{hdlc} {g_encap = WTAP_ENCAP_CHDLC; BEGIN(STARTBYTES); }
171 <ENCAP,STARTBYTES>{start_bytes} { BEGIN(BYTE); }
172 <BYTE>{byte} { ADD_BYTE(yytext); }
174 <BYTE>{end_bytes} { FINALIZE_FRAME(); yyterminate(); }
176 . { if (++junk_chars > MAX_JUNK) { KERROR("too much junk"); } }
177 <<EOF>> { at_eof = TRUE; yyterminate(); }
181 static void finalize_frame(void) {
188 k12text_set_headers(struct wtap_pkthdr *phdr)
190 phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
192 phdr->ts.secs = 946681200 + (3600*g_h) + (60*g_m) + g_s;
193 phdr->ts.nsecs = 1000000*g_ms + 1000*g_ns;
195 phdr->caplen = phdr->len = ii;
197 phdr->pkt_encap = g_encap;
199 /* The file-encap is WTAP_ENCAP_PER_PACKET */
201 case WTAP_ENCAP_ETHERNET:
202 phdr->pseudo_header.eth.fcs_len = 0;
204 case WTAP_ENCAP_MTP3:
205 case WTAP_ENCAP_CHDLC:
206 /* no pseudo_header to fill in for these types */
209 case WTAP_ENCAP_MTP2: /* not (yet) supported */
210 case WTAP_ENCAP_ATM_PDUS: /* not (yet) supported */
211 /* XXX: I don't know how to fill in the */
212 /* pseudo_header for these types. So: The Lexer */
213 /* has recognition for these types commented */
222 /* Note: k12text_reset is called each time data is to be processed from */
223 /* a file. This ensures that no "state" from a previous read is */
224 /* used (such as the lexer look-ahead buffer, file_handle, file */
225 /* position and so on. This allows a single lexer buffer to be */
226 /* used even when multiple files are open simultaneously (as for */
230 k12text_reset(FILE_T fh)
234 g_encap = WTAP_ENCAP_UNKNOWN;
250 k12text_read(wtap *wth, int *err, char ** err_info, gint64 *data_offset)
252 k12text_t *k12text = (k12text_t *)wth->priv;
255 * We seek to the file position after the end of the previous frame
256 * processed by k12text_read(), since the lexer undoubtedly did some
257 * amount of look-ahead when processing the previous frame.
259 * We also clear out any lexer state (eg: look-ahead buffer) and
260 * init vars set by lexer.
263 if ( file_seek(wth->fh, k12text->next_frame_offset, SEEK_SET, err) == -1) {
266 k12text_reset(wth->fh); /* init lexer buffer and vars set by lexer */
271 if (ok_frame == FALSE) {
276 *err = WTAP_ERR_BAD_FILE;
277 *err_info = error_str;
282 *data_offset = k12text->next_frame_offset; /* file position for beginning of this frame */
283 k12text->next_frame_offset += file_bytes_read; /* file position after end of this frame */
285 k12text_set_headers(&wth->phdr);
287 buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
288 memcpy(buffer_start_ptr(wth->frame_buffer), bb, wth->phdr.caplen);
294 k12text_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr, Buffer *buf, int *err, char **err_info)
296 if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
299 k12text_reset(wth->random_fh); /* init lexer buffer and vars set by lexer */
304 if (ok_frame == FALSE) {
305 *err = WTAP_ERR_BAD_FILE;
307 /* What happened ? The desired frame was previously read without a problem */
308 *err_info = g_strdup("Unexpected EOF (program error ?)");
310 *err_info = error_str;
315 k12text_set_headers(phdr);
317 buffer_assure_space(buf, phdr->caplen);
318 memcpy(buffer_start_ptr(buf), bb, phdr->caplen);
324 k12text_open(wtap *wth, int *err, gchar **err_info _U_)
328 k12text_reset(wth->fh); /* init lexer buffer and vars set by lexer */
333 if (! is_k12text) return 0;
335 if ( file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
339 k12text = (k12text_t *)g_malloc(sizeof(k12text_t));
340 wth->priv = (void *)k12text;
341 k12text->next_frame_offset = 0;
342 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_K12TEXT;
343 wth->file_encap = WTAP_ENCAP_PER_PACKET;
344 wth->snapshot_length = 0;
345 wth->subtype_read = k12text_read;
346 wth->subtype_seek_read = k12text_seek_read;
347 wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
353 static const struct { int e; const char* s; } encaps[] = {
354 { WTAP_ENCAP_ETHERNET, "ETHER" },
355 { WTAP_ENCAP_MTP2, "MTP-L2" },
356 { WTAP_ENCAP_ATM_PDUS, "SSCOP" },
357 { WTAP_ENCAP_MTP3, "SSCF" },
358 { WTAP_ENCAP_CHDLC, "HDLC" },
364 k12text_dump(wtap_dumper *wdh _U_, const struct wtap_pkthdr *phdr,
365 const guint8 *pd, int *err) {
366 #define K12BUF_SIZE 196808
368 size_t left = K12BUF_SIZE;
378 /* Don't write anything bigger than we're willing to read. */
379 if (phdr->caplen > WTAP_MAX_PACKET_SIZE) {
380 *err = WTAP_ERR_PACKET_TOO_LARGE;
385 for(i=0; encaps[i].s; i++) {
386 if (phdr->pkt_encap == encaps[i].e) {
387 str_enc = encaps[i].s;
391 if (str_enc == NULL) {
393 * That encapsulation type is not supported. Fail.
395 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
399 buf = (char *)g_malloc(K12BUF_SIZE);
402 ms = phdr->ts.nsecs / 1000000;
403 ns = (phdr->ts.nsecs - (1000000*ms))/1000;
405 tmp = gmtime(&phdr->ts.secs);
407 g_snprintf(p, 90, "+---------+---------------+----------+\r\nXX:XX:XX,");
409 strftime(p, 90, "+---------+---------------+----------+\r\n%H:%M:%S,", tmp);
414 wl = g_snprintf(p, left, "%.3d,%.3d %s\r\n|0 |", ms, ns, str_enc);
418 for(i = 0; i < phdr->caplen && left > 2; i++) {
419 wl = g_snprintf(p, left, "%.2x|", pd[i]);
424 wl = g_snprintf(p, left, "\r\n\r\n");
427 ret = wtap_dump_file_write(wdh, buf, K12BUF_SIZE - left, err);
435 k12text_dump_open(wtap_dumper *wdh, int *err _U_)
437 wdh->subtype_write = k12text_dump;
443 k12text_dump_can_write_encap(int encap)
446 case WTAP_ENCAP_PER_PACKET:
447 case WTAP_ENCAP_ETHERNET:
448 case WTAP_ENCAP_MTP3:
449 case WTAP_ENCAP_CHDLC:
451 case WTAP_ENCAP_MTP2:
452 case WTAP_ENCAP_ATM_PDUS:
454 return WTAP_ERR_UNSUPPORTED_ENCAP;
459 * We want to stop processing when we get to the end of the input.
460 * (%option noyywrap is not used because if used then
461 * some flex versions (eg: 2.5.35) generate code which causes
462 * warnings by the Windows VC compiler).