blob: abd487f735a2a99caacf5d4467c6a2f5d4fefd20 [file] [log] [blame]
Harald Welte43ab79f2018-10-03 23:34:21 +02001/*-
2 * Copyright (c) 2003, 2004, 2005, 2007 Lev Walkin <vlm@lionet.info>.
3 * 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
12#include "asn_application.h" /* Application-visible API */
13
14#ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */
15#include <assert.h> /* for assert() macro */
16#endif
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/* Environment version might be used to avoid running with the old library */
23#define ASN1C_ENVIRONMENT_VERSION 924 /* Compile-time version */
24int get_asn1c_environment_version(void); /* Run-time version */
25
Harald Welteb54a51e2019-03-31 15:57:59 +020026#include <osmocom/core/talloc.h>
27extern __thread void *talloc_asn1_ctx;
28#define CALLOC(nmemb, size) talloc_zero_size(talloc_asn1_ctx, (nmemb) * (size))
29#define MALLOC(size) talloc_size(talloc_asn1_ctx, size)
30#define REALLOC(oldptr, size) talloc_realloc_size(talloc_asn1_ctx, oldptr, size)
31#define FREEMEM(ptr) talloc_free(ptr)
Harald Welte43ab79f2018-10-03 23:34:21 +020032
33#define asn_debug_indent 0
34#define ASN_DEBUG_INDENT_ADD(i) do{}while(0)
35
36/*
37 * A macro for debugging the ASN.1 internals.
38 * You may enable or override it.
39 */
40#ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */
41#if EMIT_ASN_DEBUG == 1 /* And it was asked to emit this code... */
42#ifdef __GNUC__
43#ifdef ASN_THREAD_SAFE
44/* Thread safety requires sacrifice in output indentation:
45 * Retain empty definition of ASN_DEBUG_INDENT_ADD. */
46#else /* !ASN_THREAD_SAFE */
47#undef ASN_DEBUG_INDENT_ADD
48#undef asn_debug_indent
49int asn_debug_indent;
50#define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)
51#endif /* ASN_THREAD_SAFE */
52extern int asn_debug; /* Allow option on execution */
53#define ASN_DEBUG(fmt, args...) \
54if (asn_debug) { \
55 do { \
56 int adi = asn_debug_indent; \
57 while(adi--) fprintf(stderr, " "); \
58 fprintf(stderr, fmt, ##args); \
59 fprintf(stderr, " (%s:%d)\n", \
60 __FILE__, __LINE__); \
61 } while(0); \
62}
63#else /* !__GNUC__ */
64void ASN_DEBUG_f(const char *fmt, ...);
65#define ASN_DEBUG ASN_DEBUG_f
66#endif /* __GNUC__ */
67#else /* EMIT_ASN_DEBUG != 1 */
68static inline void ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
69#endif /* EMIT_ASN_DEBUG */
70#endif /* ASN_DEBUG */
71
72/*
73 * Invoke the application-supplied callback and fail, if something is wrong.
74 */
75#define __ASN_E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
76#define _ASN_E_CALLBACK(foo) do { \
77 if(foo) goto cb_failed; \
78 } while(0)
79#define _ASN_CALLBACK(buf, size) \
80 _ASN_E_CALLBACK(__ASN_E_cbc(buf, size))
81#define _ASN_CALLBACK2(buf1, size1, buf2, size2) \
82 _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) || __ASN_E_cbc(buf2, size2))
83#define _ASN_CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \
84 _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) \
85 || __ASN_E_cbc(buf2, size2) \
86 || __ASN_E_cbc(buf3, size3))
87
88#define _i_ASN_TEXT_INDENT(nl, level) do { \
89 int __level = (level); \
90 int __nl = ((nl) != 0); \
91 int __i; \
92 if(__nl) _ASN_CALLBACK("\n", 1); \
93 if(__level < 0) __level = 0; \
94 for(__i = 0; __i < __level; __i++) \
95 _ASN_CALLBACK(" ", 4); \
96 er.encoded += __nl + 4 * __level; \
97} while(0)
98
99#define _i_INDENT(nl) do { \
100 int __i; \
101 if((nl) && cb("\n", 1, app_key) < 0) return -1; \
102 for(__i = 0; __i < ilevel; __i++) \
103 if(cb(" ", 4, app_key) < 0) return -1; \
104} while(0)
105
106/*
107 * Check stack against overflow, if limit is set.
108 */
109#define _ASN_DEFAULT_STACK_MAX (30000)
110static inline int
111_ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) {
112 if(ctx && ctx->max_stack_size) {
113
114 /* ctx MUST be allocated on the stack */
115 ptrdiff_t usedstack = ((char *)ctx - (char *)&ctx);
116 if(usedstack > 0) usedstack = -usedstack; /* grows up! */
117
118 /* double negative required to avoid int wrap-around */
119 if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
120 ASN_DEBUG("Stack limit %ld reached",
121 (long)ctx->max_stack_size);
122 return -1;
123 }
124 }
125 return 0;
126}
127
128#ifdef __cplusplus
129}
130#endif
131
132#endif /* _ASN_INTERNAL_H_ */