blob: 5fd7e7684220ffe574f25cda4da8d8ccdc18d669 [file] [log] [blame]
Lev Walkinc6cac8e2016-03-14 02:57:07 -07001#ifndef ASN1FIX_INTERNAL_H
2#define ASN1FIX_INTERNAL_H
Lev Walkinf15320b2004-06-03 03:38:44 +00003
Lev Walkin97bdee22004-06-28 21:21:24 +00004#ifdef HAVE_CONFIG_H
5#include <config.h>
6#endif
7
Lev Walkinf15320b2004-06-03 03:38:44 +00008/*
9 * System headers required in various modules.
10 */
11#include <stdio.h>
12#include <stdlib.h>
Lev Walkin4efbfb72005-02-25 14:20:30 +000013#include <stdarg.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000014#include <string.h>
15#include <ctype.h> /* isupper() */
16#include <errno.h>
17#include <assert.h>
18
Lev Walkin4efbfb72005-02-25 14:20:30 +000019#ifdef HAVE_UNISTD_H
20#include <unistd.h>
21#endif
22
Lev Walkinf15320b2004-06-03 03:38:44 +000023#include <asn1parser.h> /* Our lovely ASN.1 parser module */
Lev Walkinc0e03b92017-08-22 01:48:23 -070024#include <asn1_namespace.h>
Lev Walkin4efbfb72005-02-25 14:20:30 +000025#include "asn1fix.h"
26
Lev Walkin93659562010-11-20 09:47:13 -080027#ifdef _WIN32
Lev Walkin4efbfb72005-02-25 14:20:30 +000028#define EX_NOINPUT 66
29#define EX_DATAERR 65
30#define snprintf _snprintf
Frank Morgnercab30af2013-05-21 14:28:35 +020031#if defined HAVE_DECL_STRCASECMP && !HAVE_DECL_STRCASECMP
Lev Walkin4efbfb72005-02-25 14:20:30 +000032#define strcasecmp stricmp
33#endif
Frank Morgnercab30af2013-05-21 14:28:35 +020034#endif
Lev Walkin4efbfb72005-02-25 14:20:30 +000035
36#ifndef ETOOMANYREFS
37#define ETOOMANYREFS 144
38#endif
Lev Walkinf15320b2004-06-03 03:38:44 +000039
40/*
41 * A definition of a function that will log error messages.
42 */
43typedef void (*error_logger_f)(int _is_fatal, const char *fmt, ...);
44
45/*
46 * Universal argument.
47 */
48typedef struct arg_s {
Lev Walkinc0e03b92017-08-22 01:48:23 -070049 asn1p_t *asn;
50 asn1_namespace_t *ns;
51 asn1p_module_t *mod;
52 asn1p_expr_t *expr;
53 error_logger_f eh;
54 error_logger_f debug;
55 void *key; /* The next level key */
56 enum asn1f_flags flags;
Lev Walkinf15320b2004-06-03 03:38:44 +000057} arg_t;
58
59/*
60 * Functions performing normalization of various types.
61 */
62#include "asn1fix_misc.h" /* Support functions */
63#include "asn1fix_value.h" /* Value processing */
64#include "asn1fix_cstring.h" /* Fix cstring values */
65#include "asn1fix_compat.h" /* Data compatibility */
66#include "asn1fix_constr.h" /* Constructed types */
67#include "asn1fix_class.h" /* CLASS support */
Lev Walkin9d542d22006-03-14 16:31:37 +000068#include "asn1fix_cws.h" /* CLASS WITH SYNTAX support */
Lev Walkina00d6b32006-03-21 03:40:38 +000069#include "asn1fix_param.h" /* Parameterization */
Lev Walkinf15320b2004-06-03 03:38:44 +000070#include "asn1fix_retrieve.h" /* Data retrieval */
71#include "asn1fix_enum.h" /* Process ENUMERATED */
72#include "asn1fix_integer.h" /* Process INTEGER */
73#include "asn1fix_bitstring.h" /* Process BIT STRING */
74#include "asn1fix_dereft.h" /* Dereference types */
75#include "asn1fix_derefv.h" /* Dereference values */
76#include "asn1fix_tags.h" /* Tags-related stuff */
Lev Walkinb45e0672004-08-18 05:42:05 +000077#include "asn1fix_constraint.h" /* Constraint manipulation */
78#include "asn1fix_crange.h" /* Constraint groking, exportable */
79#include "asn1fix_export.h" /* Exported functions */
Lev Walkinf15320b2004-06-03 03:38:44 +000080
81
82/*
83 * Merge the return value of the called function with the already
84 * partially computed return value of the current function.
85 */
Lev Walkin48e82d12017-10-19 03:06:35 -070086#define RET2RVAL(ret, rv) \
87 do { \
88 int __ret = ret; \
89 switch(__ret) { \
90 case 0: \
91 break; \
92 case 1: \
93 if(rv) break; \
94 /* Fall through */ \
95 case -1: \
96 rv = __ret; \
97 break; \
98 default: \
99 assert(__ret >= -1 && __ret <= 1); \
100 rv = -1; \
101 } \
102 } while(0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000103
104/*
105 * Temporary substitute module for the purposes of evaluating expression.
106 */
Lev Walkinc0e03b92017-08-22 01:48:23 -0700107#define WITH_MODULE(tmp_mod, code) \
108 ({ \
109 void *_saved_mod = arg->mod; \
110 asn1_namespace_t *_saved_ns = arg->ns; \
111 arg->mod = tmp_mod; \
112 arg->ns = asn1_namespace_new_from_module(tmp_mod, 1); \
113 typeof(code) ret = code; \
Lev Walkin42457fe2017-09-26 18:07:50 -0700114 asn1_namespace_free(arg->ns); \
Lev Walkinc0e03b92017-08-22 01:48:23 -0700115 arg->ns = _saved_ns; \
116 arg->mod = _saved_mod; \
117 ret; \
118 })
Lev Walkinf15320b2004-06-03 03:38:44 +0000119
Lev Walkinc0e03b92017-08-22 01:48:23 -0700120#define LOG(code, fmt, args...) \
121 do { \
122 int _save_errno = errno; \
123 if(code < 0) { \
124 if(arg->debug) { \
125 arg->debug(code, "%s: " fmt " in %s (%s:%d)", __func__, \
126 ##args, arg->mod->source_file_name, __FILE__, \
127 __LINE__); \
128 } \
129 } else if(arg->eh) { \
130 if(arg->debug) { \
131 arg->eh(code, fmt " in %s (%s:%d)", ##args, \
132 arg->mod->source_file_name, __FILE__, __LINE__); \
133 } else { \
134 arg->eh(code, fmt " in %s", ##args, \
135 arg->mod->source_file_name); \
136 } \
137 errno = _save_errno; \
138 } \
139 } while(0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000140
141#define DEBUG(fmt, args...) LOG(-1, fmt, ##args)
142#define FATAL(fmt, args...) LOG( 1, fmt, ##args)
143#define WARNING(fmt, args...) LOG( 0, fmt, ##args)
144
145
146/*
147 * Define the symbol corresponding to the name of the current function.
148 */
149#if __STDC_VERSION__ < 199901
150#if !(__GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 3)
151#define __func__ (char *)0 /* Name of the current function */
152#endif /* GNUC */
153/* __func__ is supposed to be defined */
154#endif
155
156
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700157#endif /* ASN1FIX_INTERNAL_H */