1 /* tables.c - tables serialization code
3 * Copyright (c) 1990 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
9 * The United States Government has rights in this work pursuant
10 * to contract no. DE-AC03-76SF00098 between the United States
11 * Department of Energy and the University of California.
13 * This file is part of flex.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
31 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 /** Convert size_t to t_flag.
43 #define BYTES2TFLAG(n)\
44 (((n) == sizeof(flex_int8_t))\
46 :(((n)== sizeof(flex_int16_t))\
50 /** Clear YYTD_DATA* bit flags
51 * @return the flag with the YYTD_DATA* bits cleared
53 #define TFLAGS_CLRDATA(flg) ((flg) & ~(YYTD_DATA8 | YYTD_DATA16 | YYTD_DATA32))
55 int yytbl_write32 (struct yytbl_writer *wr, flex_uint32_t v);
56 int yytbl_write16 (struct yytbl_writer *wr, flex_uint16_t v);
57 int yytbl_write8 (struct yytbl_writer *wr, flex_uint8_t v);
58 int yytbl_writen (struct yytbl_writer *wr, void *v, int len);
59 static flex_int32_t yytbl_data_geti (const struct yytbl_data *tbl, int i);
61 static flex_int32_t yytbl_data_getijk (const struct yytbl_data *tbl, int i,
66 /** Initialize the table writer.
67 * @param wr an uninitialized writer
68 * @param out the output file
69 * @return 0 on success
71 int yytbl_writer_init (struct yytbl_writer *wr, FILE * out)
74 wr->total_written = 0;
78 /** Initialize a table header.
79 * @param th The uninitialized structure
80 * @param version_str the version string
81 * @param name the name of this table set
83 int yytbl_hdr_init (struct yytbl_hdr *th, const char *version_str,
86 memset (th, 0, sizeof (struct yytbl_hdr));
88 th->th_magic = YYTBL_MAGIC;
89 th->th_hsize = (flex_uint32_t) (14 + strlen (version_str) + 1 + strlen (name) + 1);
90 th->th_hsize += yypad64 (th->th_hsize);
91 th->th_ssize = 0; // Not known at this point.
93 th->th_version = xstrdup(version_str);
94 th->th_name = xstrdup(name);
98 /** Allocate and initialize a table data structure.
99 * @param td a pointer to an uninitialized table
100 * @param id the table identifier
101 * @return 0 on success
103 int yytbl_data_init (struct yytbl_data *td, enum yytbl_id id)
106 memset (td, 0, sizeof (struct yytbl_data));
108 td->td_flags = YYTD_DATA32;
112 /** Clean up table and data array.
113 * @param td will be destroyed
114 * @return 0 on success
116 int yytbl_data_destroy (struct yytbl_data *td)
124 /** Write enough padding to bring the file pointer to a 64-bit boundary. */
125 static int yytbl_write_pad64 (struct yytbl_writer *wr)
127 int pad, bwritten = 0;
129 pad = yypad64 (wr->total_written);
131 if (yytbl_write8 (wr, 0) < 0)
138 /** write the header.
139 * @param wr the output stream
140 * @param th table header to be written
141 * @return -1 on error, or bytes written on success.
143 int yytbl_hdr_fwrite (struct yytbl_writer *wr, const struct yytbl_hdr *th)
148 if (yytbl_write32 (wr, th->th_magic) < 0
149 || yytbl_write32 (wr, th->th_hsize) < 0)
150 flex_die (_("th_magic|th_hsize write32 failed"));
153 if (fgetpos (wr->out, &(wr->th_ssize_pos)) != 0)
154 flex_die (_("fgetpos failed"));
156 if (yytbl_write32 (wr, th->th_ssize) < 0
157 || yytbl_write16 (wr, th->th_flags) < 0)
158 flex_die (_("th_ssize|th_flags write failed"));
161 sz = (int) strlen (th->th_version) + 1;
162 if ((rv = yytbl_writen (wr, th->th_version, sz)) != sz)
163 flex_die (_("th_version writen failed"));
166 sz = (int) strlen (th->th_name) + 1;
167 if ((rv = yytbl_writen (wr, th->th_name, sz)) != sz)
168 flex_die (_("th_name writen failed"));
172 if ((rv = yytbl_write_pad64 (wr)) < 0)
173 flex_die (_("pad64 failed"));
177 if (bwritten != (int) th->th_hsize)
178 flex_die (_("pad64 failed"));
184 /** Write this table.
185 * @param wr the file writer
186 * @param td table data to be written
187 * @return -1 on error, or bytes written on success.
189 int yytbl_data_fwrite (struct yytbl_writer *wr, struct yytbl_data *td)
192 flex_int32_t bwritten = 0;
193 flex_int32_t i, total_len;
196 if ((rv = yytbl_write16 (wr, td->td_id)) < 0)
200 if ((rv = yytbl_write16 (wr, td->td_flags)) < 0)
204 if ((rv = yytbl_write32 (wr, td->td_hilen)) < 0)
208 if ((rv = yytbl_write32 (wr, td->td_lolen)) < 0)
212 total_len = yytbl_calc_total_len (td);
213 for (i = 0; i < total_len; i++) {
214 switch (YYTDFLAGS2BYTES (td->td_flags)) {
215 case sizeof (flex_int8_t):
216 rv = yytbl_write8 (wr, (flex_uint8_t) yytbl_data_geti (td, i));
218 case sizeof (flex_int16_t):
219 rv = yytbl_write16 (wr, (flex_uint16_t) yytbl_data_geti (td, i));
221 case sizeof (flex_int32_t):
222 rv = yytbl_write32 (wr, (flex_uint32_t) yytbl_data_geti (td, i));
225 flex_die (_("invalid td_flags detected"));
228 flex_die (_("error while writing tables"));
235 if (bwritten != (12 + total_len * (int) YYTDFLAGS2BYTES (td->td_flags))) {
236 flex_die (_("insanity detected"));
241 if ((rv = yytbl_write_pad64 (wr)) < 0) {
242 flex_die (_("pad64 failed"));
247 /* Now go back and update the th_hsize member */
248 if (fgetpos (wr->out, &pos) != 0
249 || fsetpos (wr->out, &(wr->th_ssize_pos)) != 0
250 || yytbl_write32 (wr, (flex_uint32_t) wr->total_written) < 0
251 || fsetpos (wr->out, &pos)) {
252 flex_die (_("get|set|fwrite32 failed"));
256 /* Don't count the int we just wrote. */
257 wr->total_written -= (int) sizeof (flex_int32_t);
262 * @param wr the table writer
263 * @param v data to be written
264 * @param len number of bytes
265 * @return -1 on error. number of bytes written on success.
267 int yytbl_writen (struct yytbl_writer *wr, void *v, int len)
271 rv = (int) fwrite (v, 1, (size_t) len, wr->out);
274 wr->total_written += len;
278 /** Write four bytes in network byte order
279 * @param wr the table writer
280 * @param v a dword in host byte order
281 * @return -1 on error. number of bytes written on success.
283 int yytbl_write32 (struct yytbl_writer *wr, flex_uint32_t v)
289 bytes = (int) sizeof (flex_uint32_t);
290 rv = (int) fwrite (&vnet, (size_t) bytes, 1, wr->out);
293 wr->total_written += bytes;
297 /** Write two bytes in network byte order.
298 * @param wr the table writer
299 * @param v a word in host byte order
300 * @return -1 on error. number of bytes written on success.
302 int yytbl_write16 (struct yytbl_writer *wr, flex_uint16_t v)
308 bytes = (int) sizeof (flex_uint16_t);
309 rv = (int) fwrite (&vnet, (size_t) bytes, 1, wr->out);
312 wr->total_written += bytes;
317 * @param wr the table writer
318 * @param v the value to be written
319 * @return -1 on error. number of bytes written on success.
321 int yytbl_write8 (struct yytbl_writer *wr, flex_uint8_t v)
325 bytes = (int) sizeof (flex_uint8_t);
326 rv = (int) fwrite (&v, (size_t) bytes, 1, wr->out);
329 wr->total_written += bytes;
336 /** Extract data element [i][j] from array data tables.
337 * @param tbl data table
338 * @param i index into higher dimension array. i should be zero for one-dimensional arrays.
339 * @param j index into lower dimension array.
340 * @param k index into struct, must be 0 or 1. Only valid for YYTD_ID_TRANSITION table
341 * @return data[i][j + k]
343 static flex_int32_t yytbl_data_getijk (const struct yytbl_data *tbl, int i,
351 switch (YYTDFLAGS2BYTES (tbl->td_flags)) {
352 case sizeof (flex_int8_t):
353 return ((flex_int8_t *) (tbl->td_data))[(i * lo + j) * (k + 1) +
355 case sizeof (flex_int16_t):
356 return ((flex_int16_t *) (tbl->td_data))[(i * lo + j) * (k +
359 case sizeof (flex_int32_t):
360 return ((flex_int32_t *) (tbl->td_data))[(i * lo + j) * (k +
364 flex_die (_("invalid td_flags detected"));
370 #endif /* Not used */
372 /** Extract data element [i] from array data tables treated as a single flat array of integers.
373 * Be careful for 2-dimensional arrays or for YYTD_ID_TRANSITION, which is an array
375 * @param tbl data table
376 * @param i index into array.
379 static flex_int32_t yytbl_data_geti (const struct yytbl_data *tbl, int i)
382 switch (YYTDFLAGS2BYTES (tbl->td_flags)) {
383 case sizeof (flex_int8_t):
384 return ((flex_int8_t *) (tbl->td_data))[i];
385 case sizeof (flex_int16_t):
386 return ((flex_int16_t *) (tbl->td_data))[i];
387 case sizeof (flex_int32_t):
388 return ((flex_int32_t *) (tbl->td_data))[i];
390 flex_die (_("invalid td_flags detected"));
396 /** Set data element [i] in array data tables treated as a single flat array of integers.
397 * Be careful for 2-dimensional arrays or for YYTD_ID_TRANSITION, which is an array
399 * @param tbl data table
400 * @param i index into array.
401 * @param newval new value for data[i]
403 static void yytbl_data_seti (const struct yytbl_data *tbl, int i,
407 switch (YYTDFLAGS2BYTES (tbl->td_flags)) {
408 case sizeof (flex_int8_t):
409 ((flex_int8_t *) (tbl->td_data))[i] = (flex_int8_t) newval;
411 case sizeof (flex_int16_t):
412 ((flex_int16_t *) (tbl->td_data))[i] = (flex_int16_t) newval;
414 case sizeof (flex_int32_t):
415 ((flex_int32_t *) (tbl->td_data))[i] = (flex_int32_t) newval;
418 flex_die (_("invalid td_flags detected"));
423 /** Calculate the number of bytes needed to hold the largest
424 * absolute value in this data array.
425 * @param tbl the data table
426 * @return sizeof(n) where n in {flex_int8_t, flex_int16_t, flex_int32_t}
428 static size_t min_int_size (struct yytbl_data *tbl)
430 flex_int32_t i, total_len;
431 flex_int32_t max = 0;
433 total_len = yytbl_calc_total_len (tbl);
435 for (i = 0; i < total_len; i++) {
438 n = abs (yytbl_data_geti (tbl, i));
445 return sizeof (flex_int8_t);
446 else if (max <= INT16_MAX)
447 return sizeof (flex_int16_t);
449 return sizeof (flex_int32_t);
452 /** Transform data to smallest possible of (int32, int16, int8).
453 * For example, we may have generated an int32 array due to user options
454 * (e.g., %option align), but if the maximum value in that array
455 * is 80 (for example), then we can serialize it with only 1 byte per int.
456 * This is NOT the same as compressed DFA tables. We're just trying
457 * to save storage space here.
459 * @param tbl the table to be compressed
461 void yytbl_data_compress (struct yytbl_data *tbl)
463 flex_int32_t i, total_len;
465 struct yytbl_data newtbl;
467 yytbl_data_init (&newtbl, tbl->td_id);
468 newtbl.td_hilen = tbl->td_hilen;
469 newtbl.td_lolen = tbl->td_lolen;
470 newtbl.td_flags = tbl->td_flags;
472 newsz = min_int_size (tbl);
475 if (newsz == YYTDFLAGS2BYTES (tbl->td_flags))
476 /* No change in this table needed. */
479 if (newsz > YYTDFLAGS2BYTES (tbl->td_flags)) {
480 flex_die (_("detected negative compression"));
484 total_len = yytbl_calc_total_len (tbl);
485 newtbl.td_data = calloc ((size_t) total_len, newsz);
486 newtbl.td_flags = (flex_uint16_t)
487 (TFLAGS_CLRDATA (newtbl.td_flags) | BYTES2TFLAG (newsz));
489 for (i = 0; i < total_len; i++) {
492 g = yytbl_data_geti (tbl, i);
493 yytbl_data_seti (&newtbl, i, g);
497 /* Now copy over the old table */
502 /* vim:set noexpandtab cindent tabstop=8 softtabstop=0 shiftwidth=8 textwidth=0: */