NativeEnumerated.c vars NULL init and check
[com/asn1c.git] / libasn1fix / asn1fix_internal.h
1 #ifndef ASN1FIX_INTERNAL_H
2 #define ASN1FIX_INTERNAL_H
3
4 #ifdef  HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 /*
9  * System headers required in various modules.
10  */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdarg.h>
14 #include <string.h>
15 #include <ctype.h>              /* isupper() */
16 #include <errno.h>
17 #include <assert.h>
18
19 #ifdef  HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22
23 #include <asn1parser.h>         /* Our lovely ASN.1 parser module */
24 #include <asn1_namespace.h>
25 #include <genhash.h>
26 #include "asn1fix.h"
27
28 #ifdef  _WIN32
29 #define EX_NOINPUT      66
30 #define EX_DATAERR      65
31 #define snprintf        _snprintf
32 #if defined HAVE_DECL_STRCASECMP && !HAVE_DECL_STRCASECMP
33 #define strcasecmp      stricmp
34 #endif
35 #endif
36
37 #ifndef ETOOMANYREFS
38 #define ETOOMANYREFS    144
39 #endif
40
41 /*
42  * A definition of a function that will log error messages.
43  */
44 typedef void (*error_logger_f)(int _is_fatal, const char *fmt, ...);
45
46 /*
47  * Universal argument.
48  */
49 typedef struct arg_s {
50     asn1p_t *asn;
51     asn1_namespace_t *ns;
52     asn1p_module_t *mod;
53     asn1p_expr_t *expr;
54     error_logger_f eh;
55     error_logger_f debug;
56     void *key; /* The next level key */
57     enum asn1f_flags flags;
58 } arg_t;
59
60 /*
61  * Functions performing normalization of various types.
62  */
63 #include "asn1fix_misc.h"               /* Support functions */
64 #include "asn1fix_value.h"              /* Value processing */
65 #include "asn1fix_cstring.h"            /* Fix cstring values */
66 #include "asn1fix_compat.h"             /* Data compatibility */
67 #include "asn1fix_constr.h"             /* Constructed types */
68 #include "asn1fix_class.h"              /* CLASS support */
69 #include "asn1fix_cws.h"                /* CLASS WITH SYNTAX support */
70 #include "asn1fix_param.h"              /* Parameterization */
71 #include "asn1fix_retrieve.h"           /* Data retrieval */
72 #include "asn1fix_enum.h"               /* Process ENUMERATED */
73 #include "asn1fix_integer.h"            /* Process INTEGER */
74 #include "asn1fix_bitstring.h"          /* Process BIT STRING */
75 #include "asn1fix_dereft.h"             /* Dereference types */
76 #include "asn1fix_derefv.h"             /* Dereference values */
77 #include "asn1fix_tags.h"               /* Tags-related stuff */
78 #include "asn1fix_constraint.h"         /* Constraint manipulation */
79 #include "asn1fix_crange.h"             /* Constraint groking, exportable */
80 #include "asn1fix_export.h"             /* Exported functions */
81
82
83 /*
84  * Merge the return value of the called function with the already
85  * partially computed return value of the current function.
86  */
87 #define RET2RVAL(ret, rv)                      \
88     do {                                       \
89         int __ret = ret;                       \
90         switch(__ret) {                        \
91         case 0:                                \
92             break;                             \
93         case 1:                                \
94             if(rv) break;                      \
95             /* Fall through */                 \
96         case -1:                               \
97             rv = __ret;                        \
98             break;                             \
99         default:                               \
100             assert(__ret >= -1 && __ret <= 1); \
101             rv = -1;                           \
102         }                                      \
103     } while(0)
104
105 /*
106  * Temporary substitute module for the purposes of evaluating expression.
107  */
108 #define WITH_MODULE(tmp_mod, code)                            \
109     ({                                                        \
110         void *_saved_mod = arg->mod;                          \
111         asn1_namespace_t *_saved_ns = arg->ns;                \
112         arg->mod = tmp_mod;                                   \
113         arg->ns = asn1_namespace_new_from_module(tmp_mod, 1); \
114         typeof(code) ret = code;                              \
115         asn1_namespace_free(arg->ns);                         \
116         arg->ns = _saved_ns;                                  \
117         arg->mod = _saved_mod;                                \
118         ret;                                                  \
119     })
120
121 #define LOG(code, fmt, args...)                                          \
122     do {                                                                 \
123         int _save_errno = errno;                                         \
124         if(code < 0) {                                                   \
125             if(arg->debug) {                                             \
126                 arg->debug(code, "%s: " fmt " in %s (%s:%d)", __func__,  \
127                            ##args, arg->mod->source_file_name, __FILE__, \
128                            __LINE__);                                    \
129             }                                                            \
130         } else if(arg->eh) {                                             \
131             if(arg->debug) {                                             \
132                 arg->eh(code, fmt " in %s (%s:%d)", ##args,              \
133                         arg->mod->source_file_name, __FILE__, __LINE__); \
134             } else {                                                     \
135                 arg->eh(code, fmt " in %s", ##args,                      \
136                         arg->mod->source_file_name);                     \
137             }                                                            \
138             errno = _save_errno;                                         \
139         }                                                                \
140     } while(0)
141
142 #define DEBUG(fmt, args...)     LOG(-1, fmt, ##args)
143 #define FATAL(fmt, args...)     LOG( 1, fmt, ##args)
144 #define WARNING(fmt, args...)   LOG( 0, fmt, ##args)
145
146
147 /*
148  * Define the symbol corresponding to the name of the current function.
149  */
150 #if __STDC_VERSION__ < 199901
151 #if !(__GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 3)
152 #define __func__        (char *)0       /* Name of the current function */
153 #endif  /* GNUC */
154 /* __func__ is supposed to be defined */
155 #endif
156
157
158 #endif  /* ASN1FIX_INTERNAL_H */