1 /*
2  * Copyright (c) 2003-2012 Tony Bybell.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef DEFS_LXTR_H
24 #define DEFS_LXTR_H
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <errno.h>
35 
36 #ifndef _MSC_VER
37 #include <unistd.h>
38 #if HAVE_INTTYPES_H
39 #include <inttypes.h>
40 #endif
41 #else
42 typedef long off_t;
43 #include <windows.h>
44 #include <io.h>
45 #endif
46 
47 #ifndef HAVE_FSEEKO
48 #define fseeko fseek
49 #define ftello ftell
50 #endif
51 
52 #include <zlib.h>
53 
54 #ifdef __GNUC__
55 #if __STDC_VERSION__ >= 199901L
56 #define _LXT2_RD_INLINE inline __attribute__((__gnu_inline__))
57 #else
58 #define _LXT2_RD_INLINE inline
59 #endif
60 #else
61 #define _LXT2_RD_INLINE
62 #endif
63 
64 #define LXT2_RDLOAD "LXTLOAD | "
65 
66 #define LXT2_RD_HDRID (0x1380)
67 #define LXT2_RD_VERSION (0x0001)
68 
69 #define LXT2_RD_GRANULE_SIZE (64)
70 #define LXT2_RD_PARTIAL_SIZE (2048)
71 
72 #define LXT2_RD_GRAN_SECT_TIME 0
73 #define LXT2_RD_GRAN_SECT_DICT 1
74 #define LXT2_RD_GRAN_SECT_TIME_PARTIAL 2
75 
76 #define LXT2_RD_MAX_BLOCK_MEM_USAGE (64*1024*1024)	/* 64MB */
77 
78 #ifndef _MSC_VER
79 typedef uint8_t 		lxtint8_t;
80 typedef uint16_t 		lxtint16_t;
81 typedef uint32_t		lxtint32_t;
82 typedef uint64_t	 	lxtint64_t;
83 typedef int32_t			lxtsint32_t;
84 typedef int64_t			lxtsint64_t;
85 #ifndef __MINGW32__
86 #define LXT2_RD_LLD "%"PRId64
87 #define LXT2_RD_LD "%"PRId32
88 #else
89 #define LXT2_RD_LLD "%I64d"
90 #define LXT2_RD_LD "%d"
91 #endif
92 #define LXT2_RD_LLDESC(x) x##LL
93 #define LXT2_RD_ULLDESC(x) x##ULL
94 #else
95 typedef unsigned __int8		lxtint8_t;
96 typedef unsigned __int16	lxtint16_t;
97 typedef unsigned __int32	lxtint32_t;
98 typedef unsigned __int64	lxtint64_t;
99 typedef          __int32	lxtsint32_t;
100 typedef          __int64	lxtsint64_t;
101 #define LXT2_RD_LLD "%I64d"
102 #define LXT2_RD_LD "%d"
103 #define LXT2_RD_LLDESC(x) x##i64
104 #define LXT2_RD_ULLDESC(x) x##i64
105 #endif
106 
107 #if LXT2_RD_GRANULE_SIZE > 32
108 typedef lxtint64_t		granmsk_t;
109 typedef lxtint32_t		granmsk_smaller_t;
110 #define LXT2_RD_GRAN_0VAL 	(LXT2_RD_ULLDESC(0))
111 #define LXT2_RD_GRAN_1VAL 	(LXT2_RD_ULLDESC(1))
112 #define get_fac_msk 		lxt2_rd_get_64
113 #define get_fac_msk_smaller 	lxt2_rd_get_32
114 #else
115 typedef lxtint32_t		granmsk_t;
116 #define LXT2_RD_GRAN_0VAL 	(0)
117 #define LXT2_RD_GRAN_1VAL 	(1)
118 #define get_fac_msk 		lxt2_rd_get_32
119 #endif
120 
121 
122 #define LXT2_RD_SYM_F_BITS      (0)
123 #define LXT2_RD_SYM_F_INTEGER   (1<<0)
124 #define LXT2_RD_SYM_F_DOUBLE    (1<<1)
125 #define LXT2_RD_SYM_F_STRING    (1<<2)
126 #define LXT2_RD_SYM_F_TIME	(LXT2_RD_SYM_F_STRING)	/* user must correctly format this as a string */
127 #define LXT2_RD_SYM_F_ALIAS     (1<<3)
128 
129 #define LXT2_RD_SYM_F_SIGNED	(1<<4)
130 #define LXT2_RD_SYM_F_BOOLEAN	(1<<5)
131 #define LXT2_RD_SYM_F_NATURAL	((1<<6)|(LXT2_RD_SYM_F_INTEGER))
132 #define LXT2_RD_SYM_F_POSITIVE	((1<<7)|(LXT2_RD_SYM_F_INTEGER))
133 #define LXT2_RD_SYM_F_CHARACTER	(1<<8)
134 #define LXT2_RD_SYM_F_CONSTANT	(1<<9)
135 #define LXT2_RD_SYM_F_VARIABLE	(1<<10)
136 #define LXT2_RD_SYM_F_SIGNAL	(1<<11)
137 
138 #define LXT2_RD_SYM_F_IN	(1<<12)
139 #define LXT2_RD_SYM_F_OUT	(1<<13)
140 #define LXT2_RD_SYM_F_INOUT	(1<<14)
141 
142 #define LXT2_RD_SYM_F_WIRE	(1<<15)
143 #define LXT2_RD_SYM_F_REG	(1<<16)
144 
145 
146 enum LXT2_RD_Encodings {
147         LXT2_RD_ENC_0,
148         LXT2_RD_ENC_1,
149         LXT2_RD_ENC_INV,
150         LXT2_RD_ENC_LSH0,
151         LXT2_RD_ENC_LSH1,
152         LXT2_RD_ENC_RSH0,
153         LXT2_RD_ENC_RSH1,
154 
155         LXT2_RD_ENC_ADD1,
156         LXT2_RD_ENC_ADD2,
157         LXT2_RD_ENC_ADD3,
158         LXT2_RD_ENC_ADD4,
159 
160         LXT2_RD_ENC_SUB1,
161         LXT2_RD_ENC_SUB2,
162         LXT2_RD_ENC_SUB3,
163         LXT2_RD_ENC_SUB4,
164 
165         LXT2_RD_ENC_X,
166         LXT2_RD_ENC_Z,
167 
168 	LXT2_RD_ENC_BLACKOUT,
169 
170         LXT2_RD_DICT_START
171         };
172 
173 
174 struct lxt2_rd_block
175 {
176 char *mem;
177 struct lxt2_rd_block *next;
178 
179 lxtint32_t uncompressed_siz, compressed_siz;
180 lxtint64_t start, end;
181 
182 lxtint32_t num_map_entries, num_dict_entries;
183 char *map_start;
184 char *dict_start;
185 
186 char **string_pointers; /* based inside dict_start */
187 unsigned int *string_lens;
188 
189 off_t filepos; /* where block starts in file if we have to reload */
190 
191 unsigned short_read_ignore : 1;	 /* tried to read once and it was corrupt so ignore next time */
192 unsigned exclude_block : 1;	 /* user marked this block off to be ignored */
193 };
194 
195 
196 struct lxt2_rd_geometry
197 {
198 lxtint32_t rows;
199 lxtsint32_t msb, lsb;
200 lxtint32_t flags, len;
201 };
202 
203 
204 struct lxt2_rd_facname_cache
205 {
206 char *n;
207 char *bufprev, *bufcurr;
208 
209 lxtint32_t old_facidx;
210 };
211 
212 
213 struct lxt2_rd_trace
214 {
215 lxtint32_t *rows;
216 lxtsint32_t *msb, *lsb;
217 lxtint32_t *flags, *len;
218 char **value;
219 
220 granmsk_t *fac_map;
221 char **fac_curpos;
222 char *process_mask;
223 char *process_mask_compressed;
224 
225 void **radix_sort[LXT2_RD_GRANULE_SIZE+1];
226 void **next_radix;
227 
228 void (*value_change_callback)(struct lxt2_rd_trace **lt, lxtint64_t *time, lxtint32_t *facidx, char **value);
229 void *user_callback_data_pointer;
230 
231 unsigned char fac_map_index_width;
232 unsigned char fac_curpos_width;
233 lxtint8_t granule_size;
234 
235 lxtint32_t numfacs, numrealfacs, numfacbytes, longestname, zfacnamesize, zfacname_predec_size, zfacgeometrysize;
236 lxtint8_t timescale;
237 
238 lxtsint64_t timezero;
239 lxtint64_t prev_time;
240 unsigned char num_time_table_entries;
241 lxtint64_t time_table[LXT2_RD_GRANULE_SIZE];
242 
243 char *zfacnames;
244 
245 unsigned int numblocks;
246 struct lxt2_rd_block *block_head, *block_curr;
247 
248 lxtint64_t start, end;
249 struct lxt2_rd_geometry geometry;
250 
251 struct lxt2_rd_facname_cache *faccache;
252 
253 FILE *handle;
254 gzFile zhandle;
255 
256 lxtint64_t block_mem_consumed, block_mem_max;
257 
258 unsigned process_mask_dirty : 1; /* only used on partial block reads */
259 };
260 
261 
262 /*
263  * LXT2 Reader API functions...
264  */
265 struct lxt2_rd_trace *       	lxt2_rd_init(const char *name);
266 void                    	lxt2_rd_close(struct lxt2_rd_trace *lt);
267 
268 lxtint64_t			lxt2_rd_set_max_block_mem_usage(struct lxt2_rd_trace *lt, lxtint64_t block_mem_max);
269 lxtint64_t			lxt2_rd_get_block_mem_usage(struct lxt2_rd_trace *lt);
270 unsigned int			lxt2_rd_get_num_blocks(struct lxt2_rd_trace *lt);
271 unsigned int			lxt2_rd_get_num_active_blocks(struct lxt2_rd_trace *lt);
272 
273 lxtint32_t			lxt2_rd_get_num_facs(struct lxt2_rd_trace *lt);
274 char *				lxt2_rd_get_facname(struct lxt2_rd_trace *lt, lxtint32_t facidx);
275 struct lxt2_rd_geometry *	lxt2_rd_get_fac_geometry(struct lxt2_rd_trace *lt, lxtint32_t facidx);
276 lxtint32_t			lxt2_rd_get_fac_rows(struct lxt2_rd_trace *lt, lxtint32_t facidx);
277 lxtsint32_t			lxt2_rd_get_fac_msb(struct lxt2_rd_trace *lt, lxtint32_t facidx);
278 lxtsint32_t			lxt2_rd_get_fac_lsb(struct lxt2_rd_trace *lt, lxtint32_t facidx);
279 lxtint32_t			lxt2_rd_get_fac_flags(struct lxt2_rd_trace *lt, lxtint32_t facidx);
280 lxtint32_t			lxt2_rd_get_fac_len(struct lxt2_rd_trace *lt, lxtint32_t facidx);
281 lxtint32_t			lxt2_rd_get_alias_root(struct lxt2_rd_trace *lt, lxtint32_t facidx);
282 
283 char				lxt2_rd_get_timescale(struct lxt2_rd_trace *lt);
284 lxtsint64_t 			lxt2_rd_get_timezero(struct lxt2_rd_trace *lt);
285 lxtint64_t			lxt2_rd_get_start_time(struct lxt2_rd_trace *lt);
286 lxtint64_t			lxt2_rd_get_end_time(struct lxt2_rd_trace *lt);
287 
288 int				lxt2_rd_get_fac_process_mask(struct lxt2_rd_trace *lt, lxtint32_t facidx);
289 int				lxt2_rd_set_fac_process_mask(struct lxt2_rd_trace *lt, lxtint32_t facidx);
290 int				lxt2_rd_clr_fac_process_mask(struct lxt2_rd_trace *lt, lxtint32_t facidx);
291 int				lxt2_rd_set_fac_process_mask_all(struct lxt2_rd_trace *lt);
292 int				lxt2_rd_clr_fac_process_mask_all(struct lxt2_rd_trace *lt);
293 
294 				/* null value_change_callback calls an empty dummy function */
295 int 				lxt2_rd_iter_blocks(struct lxt2_rd_trace *lt,
296 				void (*value_change_callback)(struct lxt2_rd_trace **lt, lxtint64_t *time, lxtint32_t *facidx, char **value),
297 				void *user_callback_data_pointer);
298 void *				lxt2_rd_get_user_callback_data_pointer(struct lxt2_rd_trace *lt);
299 
300 				/* time (un)/restricted read ops */
301 unsigned int			lxt2_rd_limit_time_range(struct lxt2_rd_trace *lt, lxtint64_t strt_time, lxtint64_t end_time);
302 unsigned int			lxt2_rd_unlimit_time_range(struct lxt2_rd_trace *lt);
303 
304 #ifdef __cplusplus
305 }
306 #endif
307 
308 #endif
309 
310