blob: 561dcf67791e40baf460e618855b8fe325f61f92 [file] [log] [blame]
Lev Walkina9cc46e2004-09-22 16:06:28 +00001/*-
Lev Walkin3e86a4e2007-06-29 11:38:57 +00002 * Copyright (c) 2003, 2004, 2005, 2007 Lev Walkin <vlm@lionet.info>.
Lev Walkin03e6d6e2005-08-03 07:01:32 +00003 * All rights reserved.
Lev Walkina9cc46e2004-09-22 16:06:28 +00004 * 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
Lev Walkin1eded352006-07-13 11:19:01 +000012#include "asn_application.h" /* Application-visible API */
Lev Walkina9cc46e2004-09-22 16:06:28 +000013
Lev Walkin2ffa1c62005-04-19 09:19:17 +000014#ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */
15#include <assert.h> /* for assert() macro */
16#endif
17
Lev Walkin6a0370b2005-07-28 08:39:46 +000018#ifdef __cplusplus
19extern "C" {
20#endif
21
Lev Walkinda9a3b82005-08-16 17:00:21 +000022/* Environment version might be used to avoid running with the old library */
Harald Welte498c9712015-08-30 16:33:07 +020023#define ASN1C_ENVIRONMENT_VERSION 924 /* Compile-time version */
Lev Walkin6a0370b2005-07-28 08:39:46 +000024int get_asn1c_environment_version(void); /* Run-time version */
25
Lev Walkina9cc46e2004-09-22 16:06:28 +000026#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)
30
Lev Walkinb85507a2012-03-02 03:54:57 -080031#define asn_debug_indent 0
32#define ASN_DEBUG_INDENT_ADD(i) do{}while(0)
33
Lev Walkina9cc46e2004-09-22 16:06:28 +000034/*
35 * A macro for debugging the ASN.1 internals.
36 * You may enable or override it.
37 */
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... */
40#ifdef __GNUC__
Lev Walkin3e86a4e2007-06-29 11:38:57 +000041#ifdef ASN_THREAD_SAFE
Lev Walkinb85507a2012-03-02 03:54:57 -080042/* Thread safety requires sacrifice in output indentation:
43 * Retain empty definition of ASN_DEBUG_INDENT_ADD. */
Lev Walkin27f53bc2007-06-29 12:10:50 +000044#else /* !ASN_THREAD_SAFE */
Lev Walkinb85507a2012-03-02 03:54:57 -080045#undef ASN_DEBUG_INDENT_ADD
46#undef asn_debug_indent
Lev Walkin3e86a4e2007-06-29 11:38:57 +000047int asn_debug_indent;
Lev Walkine2c05bf2012-02-27 10:24:35 -080048#define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)
Lev Walkin27f53bc2007-06-29 12:10:50 +000049#endif /* ASN_THREAD_SAFE */
Harald Welte498c9712015-08-30 16:33:07 +020050extern int asn_debug; /* Allow option on execution */
51#define ASN_DEBUG(fmt, args...) \
52if (asn_debug) { \
53 do { \
Lev Walkin3e86a4e2007-06-29 11:38:57 +000054 int adi = asn_debug_indent; \
55 while(adi--) fprintf(stderr, " "); \
56 fprintf(stderr, fmt, ##args); \
57 fprintf(stderr, " (%s:%d)\n", \
58 __FILE__, __LINE__); \
Harald Welte498c9712015-08-30 16:33:07 +020059 } while(0); \
60}
Lev Walkina9cc46e2004-09-22 16:06:28 +000061#else /* !__GNUC__ */
Lev Walkin8e8078a2004-09-26 13:10:40 +000062void ASN_DEBUG_f(const char *fmt, ...);
Lev Walkina9cc46e2004-09-22 16:06:28 +000063#define ASN_DEBUG ASN_DEBUG_f
64#endif /* __GNUC__ */
65#else /* EMIT_ASN_DEBUG != 1 */
Lev Walkinfdb25922005-07-21 09:32:49 +000066static inline void ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
Lev Walkina9cc46e2004-09-22 16:06:28 +000067#endif /* EMIT_ASN_DEBUG */
68#endif /* ASN_DEBUG */
69
70/*
71 * Invoke the application-supplied callback and fail, if something is wrong.
72 */
Lev Walkin942fd082004-10-03 09:13:02 +000073#define __ASN_E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +000074#define _ASN_E_CALLBACK(foo) do { \
Lev Walkin942fd082004-10-03 09:13:02 +000075 if(foo) goto cb_failed; \
Lev Walkina9cc46e2004-09-22 16:06:28 +000076 } while(0)
77#define _ASN_CALLBACK(buf, size) \
78 _ASN_E_CALLBACK(__ASN_E_cbc(buf, size))
79#define _ASN_CALLBACK2(buf1, size1, buf2, size2) \
80 _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) || __ASN_E_cbc(buf2, size2))
81#define _ASN_CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \
82 _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) \
83 || __ASN_E_cbc(buf2, size2) \
84 || __ASN_E_cbc(buf3, size3))
85
86#define _i_ASN_TEXT_INDENT(nl, level) do { \
Lev Walkin8e8078a2004-09-26 13:10:40 +000087 int __level = (level); \
88 int __nl = ((nl) != 0); \
89 int __i; \
90 if(__nl) _ASN_CALLBACK("\n", 1); \
Lev Walkin2c23dca2006-12-06 08:39:47 +000091 if(__level < 0) __level = 0; \
Lev Walkin8e8078a2004-09-26 13:10:40 +000092 for(__i = 0; __i < __level; __i++) \
93 _ASN_CALLBACK(" ", 4); \
94 er.encoded += __nl + 4 * __level; \
95} while(0)
96
97#define _i_INDENT(nl) do { \
98 int __i; \
99 if((nl) && cb("\n", 1, app_key) < 0) return -1; \
100 for(__i = 0; __i < ilevel; __i++) \
101 if(cb(" ", 4, app_key) < 0) return -1; \
Lev Walkina9cc46e2004-09-22 16:06:28 +0000102} while(0)
103
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000104/*
105 * Check stack against overflow, if limit is set.
106 */
107#define _ASN_DEFAULT_STACK_MAX (30000)
108static inline int
109_ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) {
110 if(ctx && ctx->max_stack_size) {
111
112 /* ctx MUST be allocated on the stack */
113 ptrdiff_t usedstack = ((char *)ctx - (char *)&ctx);
114 if(usedstack > 0) usedstack = -usedstack; /* grows up! */
115
116 /* double negative required to avoid int wrap-around */
117 if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
118 ASN_DEBUG("Stack limit %ld reached",
119 (long)ctx->max_stack_size);
120 return -1;
121 }
122 }
123 return 0;
124}
125
Lev Walkin6a0370b2005-07-28 08:39:46 +0000126#ifdef __cplusplus
127}
128#endif
129
Lev Walkina9cc46e2004-09-22 16:06:28 +0000130#endif /* _ASN_INTERNAL_H_ */