2 * Copyright (c) 2003, 2004, 2005, 2007 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
7 * Declarations internally useful for the ASN.1 support code.
10 #define ASN_INTERNAL_H
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 * A macro for debugging the ASN.1 internals.
36 * You may enable or override it.
38 #ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */
39 #if EMIT_ASN_DEBUG == 1 /* And it was asked to emit this code... */
41 #ifdef ASN_THREAD_SAFE
42 /* Thread safety requires sacrifice in output indentation:
43 * Retain empty definition of ASN_DEBUG_INDENT_ADD. */
44 #else /* !ASN_THREAD_SAFE */
45 #undef ASN_DEBUG_INDENT_ADD
46 #undef asn_debug_indent
48 #define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)
49 #endif /* ASN_THREAD_SAFE */
50 #define ASN_DEBUG(fmt, args...) do { \
51 int adi = asn_debug_indent; \
52 while(adi--) fprintf(stderr, " "); \
53 fprintf(stderr, fmt, ##args); \
54 fprintf(stderr, " (%s:%d)\n", \
55 __FILE__, __LINE__); \
58 void ASN_DEBUG_f(const char *fmt, ...);
59 #define ASN_DEBUG ASN_DEBUG_f
61 #else /* EMIT_ASN_DEBUG != 1 */
62 static void ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
63 #endif /* EMIT_ASN_DEBUG */
64 #endif /* ASN_DEBUG */
67 * Invoke the application-supplied callback and fail, if something is wrong.
69 #define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
70 #define ASN__E_CALLBACK(foo) do { \
71 if(foo) goto cb_failed; \
73 #define ASN__CALLBACK(buf, size) \
74 ASN__E_CALLBACK(ASN__E_cbc(buf, size))
75 #define ASN__CALLBACK2(buf1, size1, buf2, size2) \
76 ASN__E_CALLBACK(ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2))
77 #define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \
78 ASN__E_CALLBACK(ASN__E_cbc(buf1, size1) \
79 || ASN__E_cbc(buf2, size2) \
80 || ASN__E_cbc(buf3, size3))
82 #define ASN__TEXT_INDENT(nl, level) do { \
83 int tmp_level = (level); \
84 int tmp_nl = ((nl) != 0); \
86 if(tmp_nl) ASN__CALLBACK("\n", 1); \
87 if(tmp_level < 0) tmp_level = 0; \
88 for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) \
89 ASN__CALLBACK(" ", 4); \
90 er.encoded += tmp_nl + 4 * tmp_level; \
93 #define _i_INDENT(nl) do { \
95 if((nl) && cb("\n", 1, app_key) < 0) \
97 for(tmp_i = 0; tmp_i < ilevel; tmp_i++) \
98 if(cb(" ", 4, app_key) < 0) \
103 * Check stack against overflow, if limit is set.
105 #define ASN__DEFAULT_STACK_MAX (30000)
106 static int __attribute__((unused))
107 ASN__STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) {
108 if(ctx && ctx->max_stack_size) {
110 /* ctx MUST be allocated on the stack */
111 ptrdiff_t usedstack = ((char *)ctx - (char *)&ctx);
112 if(usedstack > 0) usedstack = -usedstack; /* grows up! */
114 /* double negative required to avoid int wrap-around */
115 if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
116 ASN_DEBUG("Stack limit %ld reached",
117 (long)ctx->max_stack_size);
128 #endif /* ASN_INTERNAL_H */