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