2 /* Include this before everything else, for various large-file definitions */
8 * We want a reentrant scanner.
13 * We don't use input, so don't generate code for it.
18 * We don't use unput, so don't generate code for it.
23 * We don't read interactively from the terminal.
25 %option never-interactive
28 * We want to stop processing when we get to the end of the input.
33 * The type for the state we keep for a scanner.
35 %option extra-type="k12text_state_t *"
38 * Prefix scanner routines with "k12text_" rather than "yy", so this scanner
39 * can coexist with other scanners.
41 %option prefix="k12text_"
43 /* Options useful for debugging */
44 /* noline: Prevent generation of #line directives */
45 /* Seems to be required when using the */
46 /* Windows VS debugger so as to be able */
47 /* to properly step through the code and */
48 /* set breakpoints & etc using the */
49 /* k12text.c file rather than the */
51 /* XXX: %option noline gives an error message: */
52 /* "unrecognized %option: line" */
53 /* with flex 2.5.35; the --noline */
54 /* command-line option works OK. */
56 /* debug: Do output of "rule acceptance" info */
63 * We have to override the memory allocators so that we don't get
64 * "unused argument" warnings from the yyscanner argument (which
65 * we don't use, as we have a global memory allocator).
67 * We provide, as macros, our own versions of the routines generated by Flex,
68 * which just call malloc()/realloc()/free() (as the Flex versions do),
69 * discarding the extra argument.
79 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
81 * SPDX-License-Identifier: GPL-2.0-or-later
86 * - fix timestamps after midnight
87 * - verify encapsulations
96 #include "file_wrappers.h"
97 #include <wsutil/buffer.h>
100 #ifndef HAVE_UNISTD_H
101 #define YY_NO_UNISTD_H
105 * Disable diagnostics in the code generated by Flex.
110 * State kept by the scanner.
130 uint64_t file_bytes_read;
134 #define KERROR(text) do { yyextra->error_str = g_strdup(text); yyterminate(); } while(0)
135 #define SET_HOURS(text) yyextra->g_h = (unsigned) strtoul(text,NULL,10)
136 #define SET_MINUTES(text) yyextra->g_m = (unsigned) strtoul(text,NULL,10)
137 #define SET_SECONDS(text) yyextra->g_s = (unsigned) strtoul(text,NULL,10)
138 #define SET_MS(text) yyextra->g_ms = (unsigned) strtoul(text,NULL,10)
139 #define SET_NS(text) yyextra->g_ns = (unsigned) strtoul(text,NULL,10)
140 #define ADD_BYTE(text) do {if (yyextra->ii >= WTAP_MAX_PACKET_SIZE_STANDARD) {KERROR("frame too large");} yyextra->bb[yyextra->ii++] = (uint8_t)strtoul(text,NULL,16); } while(0)
141 #define FINALIZE_FRAME() do { yyextra->ok_frame = true; } while (0)
143 #define YY_USER_ACTION yyextra->file_bytes_read += yyleng;
144 #define YY_USER_INIT { \
145 k12text_state_t *scanner_state = k12text_get_extra(yyscanner); \
146 BEGIN(scanner_state->start_state); \
148 #define YY_INPUT(buf,result,max_size) { \
149 k12text_state_t *scanner_state = k12text_get_extra(yyscanner); \
150 int c = file_getc(scanner_state->fh); \
152 scanner_state->err = file_error(scanner_state->fh, \
153 &scanner_state->err_info); \
154 if (scanner_state->err == 0) \
155 scanner_state->err = WTAP_ERR_SHORT_READ; \
162 #define MAX_JUNK 400000
166 * Private per-file data.
170 * The file position after the end of the previous frame processed by
173 * We need to keep this around, and seek to it at the beginning of
174 * each call to k12text_read(), since the lexer undoubtedly did some
175 * amount of look-ahead when processing the previous frame.
177 int64_t next_frame_offset;
181 * Sleazy hack to suppress compiler warnings in yy_fatal_error().
183 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
186 * Macros for the allocators, to discard the extra argument.
188 #define k12text_alloc(size, yyscanner) (void *)malloc(size)
189 #define k12text_realloc(ptr, size, yyscanner) (void *)realloc((char *)(ptr), (size))
190 #define k12text_free(ptr, yyscanner) free((char *)ptr)
192 static int k12text_file_type_subtype = -1;
194 void register_k12text(void);
197 start_timestamp \053[\055]{9}\053[\055]{15,100}\053[\055]{10,100}\053
198 oneormoredigits [0-9]+:
202 threedigits [0-9][0-9][0-9]
203 start_bytes \174\060\040\040\040\174
204 bytes_junk \174[A-F0-9][A-F0-9\040][A-F0-9\040][A-F0-9\040]\174
205 byte [a-f0-9][a-f0-9]\174
206 end_bytes \015?\012\015?\012
213 %START MAGIC NEXT_FRAME HOURS MINUTES M2S SECONDS S2M MS M2N NS ENCAP STARTBYTES BYTE
215 <MAGIC>{start_timestamp} { yyextra->is_k12text = true; yyterminate(); }
217 <MAGIC>. { if (++ yyextra->junk_chars > MAX_JUNK) { yyextra->is_k12text = false; yyterminate(); } }
219 <NEXT_FRAME>{start_timestamp} {BEGIN(HOURS); }
220 <HOURS>{oneormoredigits} { SET_HOURS(yytext); BEGIN(MINUTES); }
221 <MINUTES>{twodigits} { SET_MINUTES(yytext); BEGIN(M2S);}
222 <M2S>{colon} { BEGIN(SECONDS);}
223 <SECONDS>{twodigits} { SET_SECONDS(yytext); BEGIN(S2M); }
224 <S2M>{comma} { BEGIN(MS); }
225 <MS>{threedigits} { SET_MS(yytext); BEGIN(M2N); }
226 <M2N>{comma} { BEGIN(NS); }
227 <NS>{threedigits} { SET_NS(yytext); BEGIN(ENCAP);}
228 <ENCAP>{eth} {yyextra->g_encap = WTAP_ENCAP_ETHERNET; BEGIN(STARTBYTES); }
229 <ENCAP>{mtp2} {yyextra->g_encap = WTAP_ENCAP_MTP2; BEGIN(STARTBYTES); }
230 <ENCAP>{sscop} {yyextra->g_encap = WTAP_ENCAP_ATM_PDUS; BEGIN(STARTBYTES); }
231 <ENCAP>{sscfnni} {yyextra->g_encap = WTAP_ENCAP_MTP3; BEGIN(STARTBYTES); }
232 <ENCAP>{hdlc} {yyextra->g_encap = WTAP_ENCAP_CHDLC; BEGIN(STARTBYTES); }
233 <ENCAP,STARTBYTES>{start_bytes} { BEGIN(BYTE); }
234 <BYTE>{byte} { ADD_BYTE(yytext); }
236 <BYTE>{end_bytes} { FINALIZE_FRAME(); yyterminate(); }
238 . { if (++yyextra->junk_chars > MAX_JUNK) { KERROR("too much junk"); } }
239 <<EOF>> { yyextra->at_eof = true; yyterminate(); }
244 * Turn diagnostics back on, so we check the code that we've written.
251 k12text_set_headers(wtap_rec *rec, k12text_state_t *state,
252 int *err, char **err_info)
254 rec->rec_type = REC_TYPE_PACKET;
255 rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
256 rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
258 rec->ts.secs = 946681200 + (3600*state->g_h) + (60*state->g_m) + state->g_s;
259 rec->ts.nsecs = 1000000*state->g_ms + 1000*state->g_ns;
261 rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = state->ii;
263 rec->rec_header.packet_header.pkt_encap = state->g_encap;
265 /* The file-encap is WTAP_ENCAP_PER_PACKET */
266 switch(state->g_encap) {
267 case WTAP_ENCAP_ETHERNET:
268 rec->rec_header.packet_header.pseudo_header.eth.fcs_len = 0;
270 case WTAP_ENCAP_MTP3:
271 case WTAP_ENCAP_CHDLC:
272 /* no pseudo_header to fill in for these types */
274 case WTAP_ENCAP_MTP2: /* not (yet) supported */
275 /* XXX: I don't know how to fill in the */
276 /* pseudo_header for these types. */
277 *err = WTAP_ERR_UNSUPPORTED;
278 *err_info = g_strdup("k12text: MTP2 packets not yet supported");
280 case WTAP_ENCAP_ATM_PDUS: /* not (yet) supported */
281 /* XXX: I don't know how to fill in the */
282 /* pseudo_header for these types. */
283 *err = WTAP_ERR_UNSUPPORTED;
284 *err_info = g_strdup("k12text: SSCOP packets not yet supported");
287 *err = WTAP_ERR_UNSUPPORTED;
288 *err_info = g_strdup("k12text: unknown encapsulation type");
294 /* Note: k12text_reset is called each time data is to be processed from */
295 /* a file. This ensures that no "state" from a previous read is */
296 /* used (such as the lexer look-ahead buffer, file_handle, file */
297 /* position and so on. This allows a single lexer buffer to be */
298 /* used even when multiple files are open simultaneously (as for */
302 k12text_run_scanner(k12text_state_t *state, FILE_T fh, int start_state,
303 int *err, char **err_info)
305 yyscan_t scanner = NULL;
307 if (yylex_init(&scanner) != 0) {
308 /* errno is set if this fails */
315 state->err_info = NULL;
316 state->start_state = start_state;
318 state->g_encap = WTAP_ENCAP_UNKNOWN;
319 state->ok_frame = false;
320 state->is_k12text = false;
321 state->at_eof = false;
322 state->junk_chars = 0;
323 state->error_str = NULL;
324 state->file_bytes_read=0;
332 /* Associate the state with the scanner */
333 k12text_set_extra(state, scanner);
336 yylex_destroy(scanner);
337 if (state->err != 0 && state->err != WTAP_ERR_SHORT_READ) {
340 *err_info = state->err_info;
347 k12text_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err, char ** err_info, int64_t *data_offset)
349 k12text_t *k12text = (k12text_t *)wth->priv;
350 k12text_state_t state;
353 * We seek to the file position after the end of the previous frame
354 * processed by k12text_read(), since the lexer undoubtedly did some
355 * amount of look-ahead when processing the previous frame.
357 * We also clear out any lexer state (eg: look-ahead buffer) and
358 * init vars set by lexer.
361 if ( file_seek(wth->fh, k12text->next_frame_offset, SEEK_SET, err) == -1) {
364 state.bb = (uint8_t*)g_malloc(WTAP_MAX_PACKET_SIZE_STANDARD);
366 if (!k12text_run_scanner(&state, wth->fh, NEXT_FRAME, err, err_info)) {
371 if (state.ok_frame == false) {
376 *err = WTAP_ERR_BAD_FILE;
377 *err_info = state.error_str;
383 *data_offset = k12text->next_frame_offset; /* file position for beginning of this frame */
384 k12text->next_frame_offset += state.file_bytes_read; /* file position after end of this frame */
386 if (!k12text_set_headers(rec, &state, err, err_info)) {
390 ws_buffer_assure_space(buf, rec->rec_header.packet_header.caplen);
391 memcpy(ws_buffer_start_ptr(buf), state.bb, rec->rec_header.packet_header.caplen);
398 k12text_seek_read(wtap *wth, int64_t seek_off, wtap_rec *rec, Buffer *buf, int *err, char **err_info)
400 k12text_state_t state;
402 if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
405 state.bb = (uint8_t*)g_malloc(WTAP_MAX_PACKET_SIZE_STANDARD);
407 if (!k12text_run_scanner(&state, wth->random_fh, NEXT_FRAME, err, err_info)) {
412 if (state.ok_frame == false) {
413 *err = WTAP_ERR_BAD_FILE;
415 /* What happened ? The desired frame was previously read without a problem */
416 *err_info = g_strdup("Unexpected EOF (program error ?)");
418 *err_info = state.error_str;
424 if (!k12text_set_headers(rec, &state, err, err_info)) {
428 ws_buffer_assure_space(buf, rec->rec_header.packet_header.caplen);
429 memcpy(ws_buffer_start_ptr(buf), state.bb, rec->rec_header.packet_header.caplen);
436 k12text_open(wtap *wth, int *err, char **err_info)
439 k12text_state_t state;
441 state.bb = (uint8_t*)g_malloc(WTAP_MAX_PACKET_SIZE_STANDARD);
442 if (!k12text_run_scanner(&state, wth->fh, MAGIC, err, err_info)) {
444 return WTAP_OPEN_ERROR;
447 if (!state.is_k12text) {
448 /* *err might have been set to WTAP_ERR_SHORT_READ */
451 return WTAP_OPEN_NOT_MINE;
454 if ( file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
456 return WTAP_OPEN_ERROR;
459 k12text = g_new(k12text_t, 1);
460 wth->priv = (void *)k12text;
461 k12text->next_frame_offset = 0;
462 wth->file_type_subtype = k12text_file_type_subtype;
463 wth->file_encap = WTAP_ENCAP_PER_PACKET;
464 wth->snapshot_length = 0;
465 wth->subtype_read = k12text_read;
466 wth->subtype_seek_read = k12text_seek_read;
467 wth->file_tsprec = WTAP_TSPREC_NSEC;
470 return WTAP_OPEN_MINE;
474 static const struct { int e; const char* s; } encaps[] = {
475 { WTAP_ENCAP_ETHERNET, "ETHER" },
476 { WTAP_ENCAP_MTP2, "MTP-L2" },
477 { WTAP_ENCAP_ATM_PDUS, "SSCOP" },
478 { WTAP_ENCAP_MTP3, "SSCF" },
479 { WTAP_ENCAP_CHDLC, "HDLC" },
485 k12text_dump(wtap_dumper *wdh, const wtap_rec *rec,
486 const uint8_t *pd, int *err, char **err_info _U_) {
487 #define K12BUF_SIZE 196808
489 size_t left = K12BUF_SIZE;
499 /* Don't write anything bigger than we're willing to read. */
500 if (rec->rec_header.packet_header.caplen > WTAP_MAX_PACKET_SIZE_STANDARD) {
501 *err = WTAP_ERR_PACKET_TOO_LARGE;
506 for(i=0; encaps[i].s; i++) {
507 if (rec->rec_header.packet_header.pkt_encap == encaps[i].e) {
508 str_enc = encaps[i].s;
512 if (str_enc == NULL) {
514 * That encapsulation type is not supported. Fail.
516 *err = WTAP_ERR_UNWRITABLE_ENCAP;
520 buf = (char *)g_malloc(K12BUF_SIZE);
523 ms = rec->ts.nsecs / 1000000;
524 ns = (rec->ts.nsecs - (1000000*ms))/1000;
526 tmp = gmtime(&rec->ts.secs);
528 snprintf(p, 90, "+---------+---------------+----------+\r\nXX:XX:XX,");
530 strftime(p, 90, "+---------+---------------+----------+\r\n%H:%M:%S,", tmp);
535 wl = snprintf(p, left, "%.3d,%.3d %s\r\n|0 |", ms, ns, str_enc);
539 for(i = 0; i < rec->rec_header.packet_header.caplen && left > 2; i++) {
540 wl = snprintf(p, left, "%.2x|", pd[i]);
545 wl = snprintf(p, left, "\r\n\r\n");
548 ret = wtap_dump_file_write(wdh, buf, K12BUF_SIZE - left, err);
556 k12text_dump_open(wtap_dumper *wdh, int *err _U_, char **err_info _U_)
558 wdh->subtype_write = k12text_dump;
564 k12text_dump_can_write_encap(int encap)
567 case WTAP_ENCAP_PER_PACKET:
568 case WTAP_ENCAP_ETHERNET:
569 case WTAP_ENCAP_MTP3:
570 case WTAP_ENCAP_CHDLC:
572 case WTAP_ENCAP_MTP2:
573 case WTAP_ENCAP_ATM_PDUS:
575 return WTAP_ERR_UNWRITABLE_ENCAP;
579 static const struct supported_block_type k12text_blocks_supported[] = {
581 * We support packet blocks, with no comments or other options.
583 { WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED }
586 static const struct file_type_subtype_info k12text_info = {
587 "K12 text file", "k12text", "txt", NULL,
588 false, BLOCKS_SUPPORTED(k12text_blocks_supported),
589 k12text_dump_can_write_encap, k12text_dump_open, NULL
592 void register_k12text(void)
594 k12text_file_type_subtype = wtap_register_file_type_subtype(&k12text_info);
597 * Register name for backwards compatibility with the
598 * wtap_filetypes table in Lua.
600 wtap_register_backwards_compatibility_lua_name("K12TEXT",
601 k12text_file_type_subtype);