18108dccecffd409ded8591865592331bfc388f9
[ric-plt/resource-status-manager.git] / RSM / 3rdparty / asn1codec / e2ap_engine / asn_internal.h
1
2 /*
3  * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 /*
7  * Declarations internally useful for the ASN.1 support code.
8  */
9 #ifndef ASN_INTERNAL_H
10 #define ASN_INTERNAL_H
11 #define __EXTENSIONS__          /* for Sun */
12
13 #include "asn_application.h"    /* Application-visible API */
14
15 #ifndef __NO_ASSERT_H__         /* Include assert.h only for internal use. */
16 #include <assert.h>             /* for assert() macro */
17 #endif
18
19 #ifdef  __cplusplus
20 extern "C" {
21 #endif
22
23 /* Environment version might be used to avoid running with the old library */
24 #define ASN1C_ENVIRONMENT_VERSION       923     /* Compile-time version */
25 int get_asn1c_environment_version(void);        /* Run-time version */
26
27 #define CALLOC(nmemb, size)     calloc(nmemb, size)
28 #define MALLOC(size)            malloc(size)
29 #define REALLOC(oldptr, size)   realloc(oldptr, size)
30 #define FREEMEM(ptr)            free(ptr)
31
32 #define asn_debug_indent        0
33 #define ASN_DEBUG_INDENT_ADD(i) do{}while(0)
34
35 #ifdef  EMIT_ASN_DEBUG
36 #warning "Use ASN_EMIT_DEBUG instead of EMIT_ASN_DEBUG"
37 #define ASN_EMIT_DEBUG  EMIT_ASN_DEBUG
38 #endif
39
40 /*
41  * A macro for debugging the ASN.1 internals.
42  * You may enable or override it.
43  */
44 #ifndef ASN_DEBUG       /* If debugging code is not defined elsewhere... */
45 #if     ASN_EMIT_DEBUG == 1     /* And it was asked to emit this code... */
46 #if !defined(BELL_LABS) /* Bell Labs */
47   //#if __STDC_VERSION__ >= 199901L
48 #ifdef  ASN_THREAD_SAFE
49 /* Thread safety requires sacrifice in output indentation:
50  * Retain empty definition of ASN_DEBUG_INDENT_ADD. */
51 #else   /* !ASN_THREAD_SAFE */
52 #undef  ASN_DEBUG_INDENT_ADD
53 #undef  asn_debug_indent
54 int asn_debug_indent;
55 #define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)
56 #endif  /* ASN_THREAD_SAFE */
57 #if defined(BELL_LABS) /* Bell Labs version */
58 extern int logAsn1c(const char *filename, int linenumber, const char *format, ...);  
59 #define ASN_DEBUG(fmt, args...) do {                    \
60     (void) logAsn1c(__FILE__, __LINE__, fmt, ##args);   \
61   } while(0)
62 #else  
63 #define ASN_DEBUG(fmt, args...) do {                    \
64                 int adi = asn_debug_indent;             \
65                 while(adi--) fprintf(stderr, " ");      \
66                 fprintf(stderr, fmt, ##args);           \
67                 fprintf(stderr, " (%s:%d)\n",           \
68                         __FILE__, __LINE__);            \
69         } while(0)
70 #endif /* BELL_LABS */  
71 #else   /* !C99 */
72 void CC_PRINTFLIKE(1, 2) ASN_DEBUG_f(const char *fmt, ...);
73 #define ASN_DEBUG       ASN_DEBUG_f
74 #endif  /* C99 */
75 #else   /* ASN_EMIT_DEBUG != 1 */
76 #if __STDC_VERSION__ >= 199901L
77 #define ASN_DEBUG(...) do{}while(0)
78 #else   /* not C99 */
79 static void CC_PRINTFLIKE(1, 2) ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
80 #endif  /* C99 or better */
81 #endif  /* ASN_EMIT_DEBUG */
82 #endif  /* ASN_DEBUG */
83
84 /*
85  * Print to a callback.
86  * The callback is expected to return negative values on error.
87  * 0 and positive values are treated as success.
88  * RETURN VALUES:
89  *  -1: Failed to format or invoke the callback.
90  *  >0: Size of the data that got delivered to the callback.
91  */
92 ssize_t CC_PRINTFLIKE(3, 4)
93 asn__format_to_callback(
94     int (*callback)(const void *, size_t, void *key), void *key,
95     const char *fmt, ...);
96
97 /*
98  * Invoke the application-supplied callback and fail, if something is wrong.
99  */
100 #define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
101 #define ASN__E_CALLBACK(size, foo) \
102     do {                           \
103         if(foo) goto cb_failed;    \
104         er.encoded += (size);      \
105     } while(0)
106 #define ASN__CALLBACK(buf, size) ASN__E_CALLBACK(size, ASN__E_cbc(buf, size))
107 #define ASN__CALLBACK2(buf1, size1, buf2, size2) \
108     ASN__E_CALLBACK((size1) + (size2),           \
109                     ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2))
110 #define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3)          \
111     ASN__E_CALLBACK((size1) + (size2) + (size3),                       \
112                     ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2) \
113                         || ASN__E_cbc(buf3, size3))
114
115 #define ASN__TEXT_INDENT(nl, level)                                          \
116     do {                                                                     \
117         int tmp_level = (level);                                             \
118         int tmp_nl = ((nl) != 0);                                            \
119         int tmp_i;                                                           \
120         if(tmp_nl) ASN__CALLBACK("\n", 1);                                   \
121         if(tmp_level < 0) tmp_level = 0;                                     \
122         for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) ASN__CALLBACK("    ", 4); \
123     } while(0)
124
125 #define _i_INDENT(nl)   do {                        \
126         int tmp_i;                                  \
127         if((nl) && cb("\n", 1, app_key) < 0)        \
128             return -1;                              \
129         for(tmp_i = 0; tmp_i < ilevel; tmp_i++)     \
130             if(cb("    ", 4, app_key) < 0)          \
131                 return -1;                          \
132     } while(0)
133
134 /*
135  * Check stack against overflow, if limit is set.
136  */
137 #define ASN__DEFAULT_STACK_MAX  (30000)
138 static int CC_NOTUSED
139 ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
140         if(ctx && ctx->max_stack_size) {
141
142                 /* ctx MUST be allocated on the stack */
143                 ptrdiff_t usedstack = ((const char *)ctx - (const char *)&ctx);
144                 if(usedstack > 0) usedstack = -usedstack; /* grows up! */
145
146                 /* double negative required to avoid int wrap-around */
147                 if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
148                         ASN_DEBUG("Stack limit %ld reached",
149                                 (long)ctx->max_stack_size);
150                         return -1;
151                 }
152         }
153         return 0;
154 }
155
156 #ifdef  __cplusplus
157 }
158 #endif
159
160 #endif  /* ASN_INTERNAL_H */