"Fossies" - the Fresh Open Source Software Archive

Member "ffe-0.3.9/src/ffe.h" (17 Sep 2017, 8991 Bytes) of package /linux/privat/ffe-0.3.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ffe.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.3.7-1_vs_0.3.8.

    1 /*  
    2  *    ffe - Flat File Extractor
    3  *
    4  *    Copyright (C) 2006 Timo Savinen
    5  *    This file is part of ffe.
    6  * 
    7  *    ffe is free software; you can redistribute it and/or modify
    8  *    it under the terms of the GNU General Public License as published by
    9  *    the Free Software Foundation; either version 2 of the License, or
   10  *    (at your option) any later version.
   11  *
   12  *    ffe is distributed in the hope that it will be useful,
   13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  *    GNU General Public License for more details.
   16  *
   17  *    You should have received a copy of the GNU General Public License
   18  *    along with ffe; if not, write to the Free Software
   19  *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   20  *
   21  */
   22 
   23 /* $Id: ffe.h,v 1.71 2011-04-10 10:12:10 timo Exp $ */
   24 
   25 #ifdef HAVE_CONFIG_H
   26 #include "config.h"
   27 #endif
   28 
   29 #ifdef HAVE_FEATURES_H
   30 #include <features.h>
   31 #endif
   32 
   33 #ifdef HAVE_ERRNO_H
   34 #include <errno.h>
   35 #endif
   36 
   37 #ifdef HAVE_ERROR_H
   38 #include <error.h>
   39 #endif
   40 
   41 #ifdef HAVE_SYS_TYPES_H
   42 #include <sys/types.h>
   43 #endif
   44 
   45 #ifdef HAVE_REGEX_H
   46 #include <regex.h>
   47 #endif
   48 
   49 
   50 #ifdef HAVE_UNISTD_H
   51 #include <unistd.h>
   52 #endif 
   53 
   54 #ifdef HAVE_FCNTL_H
   55 #include <fcntl.h>
   56 #endif
   57 
   58 #ifdef HAVE_INTTYPES_H
   59 #include <inttypes.h>
   60 #endif
   61 
   62 #ifdef HAVE_STDINT_H
   63 #include <stdint.h>
   64 #endif
   65 
   66 #include <stdio.h>
   67 
   68 #if defined(__MINGW32__)
   69 #ifndef WIN32
   70 #define WIN32 1
   71 #endif
   72 #endif
   73 
   74 #if defined(HAVE_REGEX) && !defined(HAVE_REGCOMP)
   75 #undef HAVE_REGEX
   76 #endif
   77 
   78 
   79 #ifdef WIN32 
   80 #define PATH_SEPARATOR_STRING "\\"
   81 #else
   82 #define PATH_SEPARATOR_STRING "/"
   83 #endif
   84 
   85 #ifndef HAVE_STRCASECMP
   86 #define strcasecmp strcmp
   87 #endif
   88 
   89 #ifndef HAVE_STRNCASECMP
   90 #define strncasecmp strncmp
   91 #endif
   92 
   93 #ifndef HAVE_STRCASESTR
   94 #define strcasestr strstr
   95 #endif
   96 
   97 /* Types */
   98 
   99 #define DEFAULT_OUTPUT "default"
  100 #define LEFT_JUSTIFY 1
  101 #define RIGHT_JUSTIFY 2
  102 
  103 #define EXACT 1
  104 #define LONGEST 2
  105 
  106 /* contains field names from include-option or from -f paramter */
  107 struct include_field {
  108     char *name;
  109     int found;  /* does this belong to any record */
  110     int reported; /* is this field reported to be unmatched */
  111     struct include_field *next;
  112 };
  113 
  114 struct output {
  115     char *name;
  116     uint8_t *file_header;
  117     uint8_t *file_trailer;
  118     uint8_t *header;
  119     uint8_t *data;
  120     uint8_t *lookup;
  121     uint8_t *separator;
  122     uint8_t *record_header;
  123     uint8_t *record_trailer;
  124     uint8_t *group_header;
  125     uint8_t *group_trailer;
  126     uint8_t *element_header;
  127     uint8_t *element_trailer;
  128     uint8_t justify;
  129     uint8_t *indent;
  130     int no_data;
  131     char *empty_chars;
  132     int print_empty;
  133     int hex_cap;
  134     struct include_field *fl;
  135     char *output_file;
  136     FILE *ofp;
  137     struct output *next;
  138 };
  139 
  140 #define MAX_EXPR_HASH 32771
  141 
  142 struct pipe {
  143     char *name;
  144     char *command;
  145     struct pipe *next;
  146 };
  147 
  148 struct lookup_data {
  149     uint8_t *key;
  150     uint8_t *value;
  151     struct lookup_data *next;
  152 };
  153 
  154 struct lookup {
  155     char *name;
  156     char type; 
  157     uint8_t *default_value;
  158     struct lookup_data *data[MAX_EXPR_HASH];
  159     struct lookup *next;
  160 };
  161 
  162 /* replace, field and value */
  163 struct replace {
  164     char *field;
  165     uint8_t *value;
  166     int found;
  167     struct replace *next;
  168 };
  169 
  170 /* expression values */
  171 struct expr_list {
  172     char *value;
  173     int value_len;
  174 #if HAVE_REGEX
  175     regex_t reg;
  176 #endif
  177     struct expr_list *next;
  178 };
  179 
  180 /* anonymization methods */
  181 #define A_MASK 0
  182 #define A_RANDOM 1
  183 #define A_NRANDOM 2
  184 #define A_HASH 3
  185 #define A_NHASH 4
  186 #define A_UNKNOWN 999
  187 
  188 struct anon_field {
  189    char *anon_name;  /* anonymize block name */
  190    char *field_name;
  191    int method;
  192    int start;
  193    int length;
  194    int key_length;
  195    uint8_t *key;
  196    struct anon_field *next;
  197 };
  198 
  199 
  200 #define MAX_EXPR_FAST_LIST 61
  201 
  202 /* search expression */
  203 struct expression {
  204     char *field;
  205     char op;
  206     int found;
  207     struct field *f;  /* pointer to field used in expression */
  208     size_t exp_min_len;
  209     size_t exp_max_len;
  210     size_t fast_entries;
  211     size_t fast_expr_hash[MAX_EXPR_FAST_LIST + 1];  /* fast access list for cases there is low number of values in hash list */
  212     struct expr_list *expr_hash[MAX_EXPR_HASH]; /* value list*/
  213     struct expression *next;
  214 };
  215 
  216 
  217 struct format
  218 {
  219     char *conversion;      /* printf conversion spec */
  220     int type;              /* data type for conversion, argtypes from parse_printf_format */
  221 };
  222 
  223 
  224 /* Information for one field */
  225 struct field {
  226     char *name;
  227     char *const_data;
  228     int type;     /* field type ASC,CHAR,SHORT,.... */
  229     int endianess; /* binary data endianess */
  230     int position; /* first position = 0 byte position for fixed, first position = 1 for field number for separated */
  231     int bposition; /* position in current input buffer */
  232     int length;
  233     int print;
  234     int var_length; /* is this field variable length */
  235     char *lookup_table_name;
  236     struct lookup *lookup;
  237     struct replace *rep;  /* non NULL if value should be replaced */
  238     char *output_name;
  239     struct output *o;
  240     char *pipe_name;
  241     struct pipe *p;
  242     struct anon_field *a; 
  243     struct format *f;
  244     struct field *next;
  245 };
  246 
  247 
  248 /* contains pointer to fields which will be printed */
  249 struct print_field {
  250     struct field *f;
  251     uint8_t *data;                 // data start position in output buffer;
  252     int justify_length;
  253     int empty;                  // does the field contain only "empty" chars
  254     struct print_field *next;
  255 };
  256 
  257 struct id {
  258     int position;
  259     uint8_t *key;
  260     int regexp; /* 1 or 0 for regexp */
  261 #if HAVE_REGEX
  262     regex_t reg;
  263 #endif
  264     int length;
  265     struct id *next;
  266 };
  267 
  268 struct level {
  269     int level;
  270     char *group_name;
  271     char *element_name;
  272     int indent_count;
  273 };
  274 
  275 
  276 struct record {
  277     char *name;
  278     struct id *i;
  279     struct field *f;
  280     char *fields_from;
  281     struct print_field *pf;
  282     struct output *o;
  283     char *output_name;
  284     int vote;
  285     int length;
  286     int var_length_adjust;
  287     int arb_length; /* arbitrary length */
  288     char *length_field_name;
  289     struct field *length_field;
  290     char *var_field_name;
  291     //struct field *var_field;
  292     struct level *level;
  293     struct record *next;
  294 };
  295 
  296 #define NO_READ '0'
  297 #define FIXED_LENGTH 'f'
  298 #define SEPARATED 's'
  299 #define BINARY 'b'
  300 
  301 /* header types of input files */
  302 #define HEADER_NO 0
  303 #define HEADER_FIRST 1
  304 #define HEADER_ALL 2
  305 
  306 struct structure {
  307     char *name;
  308     char type[3]; /* [0] = f,b or s,[1] = separator, [2] = asterisk or 0 */
  309     int max_record_len;
  310     char quote;
  311     int header;
  312     char *output_name;
  313     int vote;
  314     struct output *o;
  315     struct record *r;
  316     struct structure *next;
  317 };
  318 
  319 struct input_file {
  320     char *name;
  321     long int lineno;
  322     struct input_file *next;
  323 };
  324 
  325 
  326 /* Constants */
  327 #define MAXLEVEL 1024
  328 /* exit values */
  329 #define EXIT_FAILURE 1
  330 #define EXIT_SUCCESS 0
  331 
  332 #define OP_START '^'
  333 #define OP_EQUAL '='
  334 #define OP_CONTAINS '~'
  335 #define OP_NOT_EQUAL '!'
  336 #define OP_REQEXP '?'
  337 
  338 /* field types */
  339 #define F_ASC 1
  340 #define F_INT 3
  341 #define F_CHAR 4
  342 #define F_FLOAT 7
  343 #define F_DOUBLE 8
  344 #define F_BCD 9
  345 #define F_UINT 10
  346 #define F_HEX 11
  347 
  348 /* endianess */
  349 #define F_UNKNOWN_ENDIAN 0
  350 #define F_BIG_ENDIAN 1
  351 #define F_LITTLE_ENDIAN 3
  352 #define F_SYSTEM_ENDIAN 4
  353 
  354 /* record length values */
  355 #define RL_STRICT 0
  356 #define RL_MIN 1
  357 #define TL_MAX 2
  358 
  359 /* function prototypes */
  360 extern void 
  361 panic(char *msg,char *info,char *syserror);
  362 
  363 extern void 
  364 problem(char *msg,char *info,char *syserror);
  365 
  366 extern void *
  367 xmalloc (size_t size);
  368 
  369 extern char *
  370 xstrdup(char *str);
  371 
  372 extern void
  373 parserc(char *,char *);
  374 
  375 extern void
  376 set_input_file(char *);
  377 
  378 extern void
  379 open_input_file();
  380 
  381 extern void *
  382 xrealloc (void *, size_t);
  383 
  384 extern char *
  385 guess_structure();
  386 
  387 extern void
  388 set_output_file(char *);
  389 
  390 extern void 
  391 close_output_file();
  392 
  393 extern void 
  394 execute(struct structure *,int,int,int,int,int,char *);
  395 
  396 extern char *
  397 expand_home(char *);
  398 
  399 extern FILE *
  400 xfopen(char *, char *);
  401 
  402 extern FILE *
  403 xfopenb(char *, char *);
  404 
  405 extern uint8_t *
  406 endian_and_align(uint8_t *,int,int,int);
  407 
  408 extern int
  409 check_system_endianess();
  410 
  411 extern char *
  412 guess_binary_structure();
  413 
  414 extern void
  415 file_to_text(FILE *);
  416 
  417 extern void
  418 writec(uint8_t);
  419 
  420 extern void
  421 writes(uint8_t *);
  422 
  423 extern void
  424 start_write();
  425 
  426 extern void
  427 flush_write();
  428 
  429 extern void
  430 reset_levels(int,int);
  431 
  432 extern void
  433 print_level_before(struct record *, struct record *);
  434 
  435 extern void
  436 print_level_end(struct record *);
  437 
  438 extern int
  439 get_indent_depth(int);
  440 
  441 extern void
  442 print_indent(uint8_t *,int);
  443 
  444 extern size_t
  445 hash(char *,size_t);
  446 
  447 extern void
  448 anonymize_fields(char *,uint8_t,struct record *,int,uint8_t *);
  449 
  450 
  451 
  452 
  453 /* global variables */
  454 extern struct structure *structure;
  455 extern struct output *output;
  456 extern struct expression *expression;
  457 extern struct lookup *lookup;
  458 extern struct output *no_output;
  459 extern struct output *raw;
  460 extern struct field *const_field;
  461 extern int system_endianess;
  462 extern int max_binary_record_length;
  463 extern char *ffe_open;
  464 extern struct pipe *pipes;
  465 extern struct anon_field *anonymize;
  466