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