--- /dev/null
+/*\r
+ * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.\r
+ * Redistribution and modifications are permitted subject to BSD license.\r
+ */\r
+/*\r
+ * Declarations internally useful for the ASN.1 support code.\r
+ */\r
+#ifndef ASN_INTERNAL_H\r
+#define ASN_INTERNAL_H\r
+#define __EXTENSIONS__ /* for Sun */\r
+\r
+#include "asn_application.h" /* Application-visible API */\r
+\r
+#ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */\r
+#include <assert.h> /* for assert() macro */\r
+#endif\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+/* Environment version might be used to avoid running with the old library */\r
+#define ASN1C_ENVIRONMENT_VERSION 923 /* Compile-time version */\r
+int get_asn1c_environment_version(void); /* Run-time version */\r
+\r
+#define CALLOC(nmemb, size) calloc(nmemb, size)\r
+#define MALLOC(size) malloc(size)\r
+#define REALLOC(oldptr, size) realloc(oldptr, size)\r
+#define FREEMEM(ptr) free(ptr)\r
+\r
+#define asn_debug_indent 0\r
+#define ASN_DEBUG_INDENT_ADD(i) do{}while(0)\r
+\r
+#ifdef EMIT_ASN_DEBUG\r
+#warning "Use ASN_EMIT_DEBUG instead of EMIT_ASN_DEBUG"\r
+#define ASN_EMIT_DEBUG EMIT_ASN_DEBUG\r
+#endif\r
+\r
+/*\r
+ * A macro for debugging the ASN.1 internals.\r
+ * You may enable or override it.\r
+ */\r
+#ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */\r
+#if ASN_EMIT_DEBUG == 1 /* And it was asked to emit this code... */\r
+#if !defined(BELL_LABS) /* Bell Labs */\r
+ //#if __STDC_VERSION__ >= 199901L\r
+#ifdef ASN_THREAD_SAFE\r
+/* Thread safety requires sacrifice in output indentation:\r
+ * Retain empty definition of ASN_DEBUG_INDENT_ADD. */\r
+#else /* !ASN_THREAD_SAFE */\r
+#undef ASN_DEBUG_INDENT_ADD\r
+#undef asn_debug_indent\r
+int asn_debug_indent;\r
+#define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)\r
+#endif /* ASN_THREAD_SAFE */\r
+#if defined(BELL_LABS) /* Bell Labs version */\r
+extern int logAsn1c(const char *filename, int linenumber, const char *format, ...); \r
+#define ASN_DEBUG(fmt, args...) do { \\r
+ (void) logAsn1c(__FILE__, __LINE__, fmt, ##args); \\r
+ } while(0)\r
+#else \r
+#define ASN_DEBUG(fmt, args...) do { \\r
+ int adi = asn_debug_indent; \\r
+ while(adi--) fprintf(stderr, " "); \\r
+ fprintf(stderr, fmt, ##args); \\r
+ fprintf(stderr, " (%s:%d)\n", \\r
+ __FILE__, __LINE__); \\r
+ } while(0)\r
+#endif /* BELL_LABS */ \r
+#else /* !C99 */\r
+void CC_PRINTFLIKE(1, 2) ASN_DEBUG_f(const char *fmt, ...);\r
+#define ASN_DEBUG ASN_DEBUG_f\r
+#endif /* C99 */\r
+#else /* ASN_EMIT_DEBUG != 1 */\r
+#if __STDC_VERSION__ >= 199901L\r
+#define ASN_DEBUG(...) do{}while(0)\r
+#else /* not C99 */\r
+static void CC_PRINTFLIKE(1, 2) ASN_DEBUG(const char *fmt, ...) { (void)fmt; }\r
+#endif /* C99 or better */\r
+#endif /* ASN_EMIT_DEBUG */\r
+#endif /* ASN_DEBUG */\r
+\r
+/*\r
+ * Print to a callback.\r
+ * The callback is expected to return negative values on error.\r
+ * 0 and positive values are treated as success.\r
+ * RETURN VALUES:\r
+ * -1: Failed to format or invoke the callback.\r
+ * >0: Size of the data that got delivered to the callback.\r
+ */\r
+ssize_t CC_PRINTFLIKE(3, 4)\r
+asn__format_to_callback(\r
+ int (*callback)(const void *, size_t, void *key), void *key,\r
+ const char *fmt, ...);\r
+\r
+/*\r
+ * Invoke the application-supplied callback and fail, if something is wrong.\r
+ */\r
+#define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0)\r
+#define ASN__E_CALLBACK(size, foo) \\r
+ do { \\r
+ if(foo) goto cb_failed; \\r
+ er.encoded += (size); \\r
+ } while(0)\r
+#define ASN__CALLBACK(buf, size) ASN__E_CALLBACK(size, ASN__E_cbc(buf, size))\r
+#define ASN__CALLBACK2(buf1, size1, buf2, size2) \\r
+ ASN__E_CALLBACK((size1) + (size2), \\r
+ ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2))\r
+#define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \\r
+ ASN__E_CALLBACK((size1) + (size2) + (size3), \\r
+ ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2) \\r
+ || ASN__E_cbc(buf3, size3))\r
+\r
+#define ASN__TEXT_INDENT(nl, level) \\r
+ do { \\r
+ int tmp_level = (level); \\r
+ int tmp_nl = ((nl) != 0); \\r
+ int tmp_i; \\r
+ if(tmp_nl) ASN__CALLBACK("\n", 1); \\r
+ if(tmp_level < 0) tmp_level = 0; \\r
+ for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) ASN__CALLBACK(" ", 4); \\r
+ } while(0)\r
+\r
+#define _i_INDENT(nl) do { \\r
+ int tmp_i; \\r
+ if((nl) && cb("\n", 1, app_key) < 0) \\r
+ return -1; \\r
+ for(tmp_i = 0; tmp_i < ilevel; tmp_i++) \\r
+ if(cb(" ", 4, app_key) < 0) \\r
+ return -1; \\r
+ } while(0)\r
+\r
+/*\r
+ * Check stack against overflow, if limit is set.\r
+ */\r
+#define ASN__DEFAULT_STACK_MAX (30000)\r
+static int CC_NOTUSED\r
+ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {\r
+ if(ctx && ctx->max_stack_size) {\r
+\r
+ /* ctx MUST be allocated on the stack */\r
+ ptrdiff_t usedstack = ((const char *)ctx - (const char *)&ctx);\r
+ if(usedstack > 0) usedstack = -usedstack; /* grows up! */\r
+\r
+ /* double negative required to avoid int wrap-around */\r
+ if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {\r
+ ASN_DEBUG("Stack limit %ld reached",\r
+ (long)ctx->max_stack_size);\r
+ return -1;\r
+ }\r
+ }\r
+ return 0;\r
+}\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* ASN_INTERNAL_H */\r