SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / asn_internal.h
1 /*
2  * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3  * Redistribution and modifications are permitted subject to BSD license.
4  */
5 /*
6  * Declarations internally useful for the ASN.1 support code.
7  */
8 #ifndef ASN_INTERNAL_H
9 #define ASN_INTERNAL_H
10 #ifndef __EXTENSIONS__
11 #define __EXTENSIONS__          /* for Sun */
12 #endif
13
14 #include "asn_application.h"    /* Application-visible API */
15
16 #ifndef __NO_ASSERT_H__         /* Include assert.h only for internal use. */
17 #include <assert.h>             /* for assert() macro */
18 #endif
19
20 #ifdef  __cplusplus
21 extern "C" {
22 #endif
23
24 #if !defined(ASN_DISABLE_UPER_SUPPORT)
25 #include <uper_decoder.h>
26 #include <uper_encoder.h>
27 #endif  /* !defined(ASN_DISABLE_UPER_SUPPORT) */
28 #if !defined(ASN_DISABLE_APER_SUPPORT)
29 #include <aper_decoder.h>
30 #include <aper_encoder.h>
31 #endif  /* !defined(ASN_DISABLE_APER_SUPPORT) */
32
33 /* Environment version might be used to avoid running with the old library */
34 #define ASN1C_ENVIRONMENT_VERSION       923     /* Compile-time version */
35 int get_asn1c_environment_version(void);        /* Run-time version */
36
37 #define CALLOC(nmemb, size)     calloc(nmemb, size)
38 #define MALLOC(size)            malloc(size)
39 #define REALLOC(oldptr, size)   realloc(oldptr, size)
40 #define FREEMEM(ptr)            free(ptr)
41
42 #define asn_debug_indent        0
43 #define ASN_DEBUG_INDENT_ADD(i) do{}while(0)
44
45 #ifdef  EMIT_ASN_DEBUG
46 #warning "Use ASN_EMIT_DEBUG instead of EMIT_ASN_DEBUG"
47 #define ASN_EMIT_DEBUG  EMIT_ASN_DEBUG
48 #endif
49
50 /*
51  * A macro for debugging the ASN.1 internals.
52  * You may enable or override it.
53  */
54 #ifndef ASN_DEBUG       /* If debugging code is not defined elsewhere... */
55 #if     ASN_EMIT_DEBUG == 1     /* And it was asked to emit this code... */
56 #if __STDC_VERSION__ >= 199901L
57 #ifdef  ASN_THREAD_SAFE
58 /* Thread safety requires sacrifice in output indentation:
59  * Retain empty definition of ASN_DEBUG_INDENT_ADD. */
60 #else   /* !ASN_THREAD_SAFE */
61 #undef  ASN_DEBUG_INDENT_ADD
62 #undef  asn_debug_indent
63 int asn_debug_indent;
64 #define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)
65 #endif  /* ASN_THREAD_SAFE */
66 #define ASN_DEBUG(fmt, args...) do {                    \
67                 int adi = asn_debug_indent;             \
68                 while(adi--) fprintf(stderr, " ");      \
69                 fprintf(stderr, fmt, ##args);           \
70                 fprintf(stderr, " (%s:%d)\n",           \
71                         __FILE__, __LINE__);            \
72         } while(0)
73 #else   /* !C99 */
74 void CC_PRINTFLIKE(1, 2) ASN_DEBUG_f(const char *fmt, ...);
75 #define ASN_DEBUG       ASN_DEBUG_f
76 #endif  /* C99 */
77 #else   /* ASN_EMIT_DEBUG != 1 */
78 #if __STDC_VERSION__ >= 199901L
79 #define ASN_DEBUG(...) do{}while(0)
80 #else   /* not C99 */
81 static void CC_PRINTFLIKE(1, 2) ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
82 #endif  /* C99 or better */
83 #endif  /* ASN_EMIT_DEBUG */
84 #endif  /* ASN_DEBUG */
85
86 /*
87  * Print to a callback.
88  * The callback is expected to return negative values on error.
89  * 0 and positive values are treated as success.
90  * RETURN VALUES:
91  *  -1: Failed to format or invoke the callback.
92  *  >0: Size of the data that got delivered to the callback.
93  */
94 ssize_t CC_PRINTFLIKE(3, 4)
95 asn__format_to_callback(
96     int (*callback)(const void *, size_t, void *key), void *key,
97     const char *fmt, ...);
98
99 /*
100  * Invoke the application-supplied callback and fail, if something is wrong.
101  */
102 #define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
103 #define ASN__E_CALLBACK(size, foo) \
104     do {                           \
105         if(foo) goto cb_failed;    \
106         er.encoded += (size);      \
107     } while(0)
108 #define ASN__CALLBACK(buf, size) ASN__E_CALLBACK(size, ASN__E_cbc(buf, size))
109 #define ASN__CALLBACK2(buf1, size1, buf2, size2) \
110     ASN__E_CALLBACK((size1) + (size2),           \
111                     ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2))
112 #define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3)          \
113     ASN__E_CALLBACK((size1) + (size2) + (size3),                       \
114                     ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2) \
115                         || ASN__E_cbc(buf3, size3))
116
117 #define ASN__TEXT_INDENT(nl, level)                                          \
118     do {                                                                     \
119         int tmp_level = (level);                                             \
120         int tmp_nl = ((nl) != 0);                                            \
121         int tmp_i;                                                           \
122         if(tmp_nl) ASN__CALLBACK("\n", 1);                                   \
123         if(tmp_level < 0) tmp_level = 0;                                     \
124         for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) ASN__CALLBACK("    ", 4); \
125     } while(0)
126
127 #define _i_INDENT(nl)   do {                        \
128         int tmp_i;                                  \
129         if((nl) && cb("\n", 1, app_key) < 0)        \
130             return -1;                              \
131         for(tmp_i = 0; tmp_i < ilevel; tmp_i++)     \
132             if(cb("    ", 4, app_key) < 0)          \
133                 return -1;                          \
134     } while(0)
135
136 /*
137  * Check stack against overflow, if limit is set.
138  */
139
140 /* Since GCC 13, AddressSanitizer started defaulting to
141 * ASAN_OPTIONS="detect_stack_use_after_return=1", which makes this check
142 * fail due to apparently jumping stack pointers.
143 * Hence, disable this check if building with ASan, as documented in:
144 * GCC: https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
145 * Clang: https://clang.llvm.org/docs/AddressSanitizer.html#conditional-compilation-with-has-feature-address-sanitizer
146 */
147 #if defined(__SANITIZE_ADDRESS__)
148         #define ASN__SANITIZE_ENABLED 1
149 #elif defined(__has_feature)
150 #if __has_feature(address_sanitizer)
151         #define ASN__SANITIZE_ENABLED 1
152 #endif
153 #endif
154
155 #define ASN__DEFAULT_STACK_MAX  (30000)
156
157 #if defined(ASN__SANITIZE_ENABLED) || defined(ASN_DISABLE_STACK_OVERFLOW_CHECK)
158 static int CC_NOTUSED
159 ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
160    (void)ctx;
161    return 0;
162 }
163 #else
164 static int CC_NOTUSED
165 ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
166         if(ctx && ctx->max_stack_size) {
167
168                 /* ctx MUST be allocated on the stack */
169                 ptrdiff_t usedstack = ((const char *)ctx - (const char *)&ctx);
170                 if(usedstack > 0) usedstack = -usedstack; /* grows up! */
171
172                 /* double negative required to avoid int wrap-around */
173                 if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
174                         ASN_DEBUG("Stack limit %ld reached",
175                                 (long)ctx->max_stack_size);
176                         return -1;
177                 }
178         }
179         return 0;
180 }
181 #endif
182
183 #ifdef  __cplusplus
184 }
185 #endif
186
187 #endif  /* ASN_INTERNAL_H */