blob: 4e72d62f30addb09bc9fd92c354ff1233d321e69 [file] [log] [blame]
Lev Walkin20696a42017-10-17 21:27:33 -07001/*
2 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkina9cc46e2004-09-22 16:06:28 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
6 * Declarations internally useful for the ASN.1 support code.
7 */
Lev Walkinc6cac8e2016-03-14 02:57:07 -07008#ifndef ASN_INTERNAL_H
9#define ASN_INTERNAL_H
Damir Franusic6db79fd2019-03-31 19:24:11 +020010#ifndef __EXTENSIONS__
Lev Walkin6cbed3d2017-10-07 16:42:41 -070011#define __EXTENSIONS__ /* for Sun */
Damir Franusic6db79fd2019-03-31 19:24:11 +020012#endif
Lev Walkin1eded352006-07-13 11:19:01 +000013#include "asn_application.h" /* Application-visible API */
Lev Walkina9cc46e2004-09-22 16:06:28 +000014
Lev Walkin2ffa1c62005-04-19 09:19:17 +000015#ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */
16#include <assert.h> /* for assert() macro */
17#endif
18
Lev Walkin6a0370b2005-07-28 08:39:46 +000019#ifdef __cplusplus
20extern "C" {
21#endif
22
Lev Walkinda9a3b82005-08-16 17:00:21 +000023/* Environment version might be used to avoid running with the old library */
Lev Walkincf6c1e22010-12-12 02:40:27 -080024#define ASN1C_ENVIRONMENT_VERSION 923 /* Compile-time version */
Lev Walkin6a0370b2005-07-28 08:39:46 +000025int get_asn1c_environment_version(void); /* Run-time version */
26
Lev Walkina9cc46e2004-09-22 16:06:28 +000027#define CALLOC(nmemb, size) calloc(nmemb, size)
28#define MALLOC(size) malloc(size)
29#define REALLOC(oldptr, size) realloc(oldptr, size)
30#define FREEMEM(ptr) free(ptr)
31
Lev Walkinb85507a2012-03-02 03:54:57 -080032#define asn_debug_indent 0
33#define ASN_DEBUG_INDENT_ADD(i) do{}while(0)
34
Lev Walkin9175a282017-10-24 00:58:42 -070035#ifdef EMIT_ASN_DEBUG
36#warning "Use ASN_EMIT_DEBUG instead of EMIT_ASN_DEBUG"
37#define ASN_EMIT_DEBUG EMIT_ASN_DEBUG
38#endif
39
Lev Walkina9cc46e2004-09-22 16:06:28 +000040/*
41 * A macro for debugging the ASN.1 internals.
42 * You may enable or override it.
43 */
44#ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */
Lev Walkin9175a282017-10-24 00:58:42 -070045#if ASN_EMIT_DEBUG == 1 /* And it was asked to emit this code... */
Lev Walkin588bf0f2017-10-13 23:51:16 -070046#if __STDC_VERSION__ >= 199901L
Lev Walkin3e86a4e2007-06-29 11:38:57 +000047#ifdef ASN_THREAD_SAFE
Lev Walkinb85507a2012-03-02 03:54:57 -080048/* Thread safety requires sacrifice in output indentation:
49 * Retain empty definition of ASN_DEBUG_INDENT_ADD. */
Lev Walkin27f53bc2007-06-29 12:10:50 +000050#else /* !ASN_THREAD_SAFE */
Lev Walkinb85507a2012-03-02 03:54:57 -080051#undef ASN_DEBUG_INDENT_ADD
52#undef asn_debug_indent
Lev Walkin3e86a4e2007-06-29 11:38:57 +000053int asn_debug_indent;
Lev Walkine2c05bf2012-02-27 10:24:35 -080054#define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)
Lev Walkin27f53bc2007-06-29 12:10:50 +000055#endif /* ASN_THREAD_SAFE */
Lev Walkin3e86a4e2007-06-29 11:38:57 +000056#define ASN_DEBUG(fmt, args...) do { \
57 int adi = asn_debug_indent; \
58 while(adi--) fprintf(stderr, " "); \
59 fprintf(stderr, fmt, ##args); \
60 fprintf(stderr, " (%s:%d)\n", \
61 __FILE__, __LINE__); \
Lev Walkina9cc46e2004-09-22 16:06:28 +000062 } while(0)
Lev Walkin588bf0f2017-10-13 23:51:16 -070063#else /* !C99 */
Lev Walkind84f6032017-10-03 16:33:59 -070064void CC_PRINTFLIKE(1, 2) ASN_DEBUG_f(const char *fmt, ...);
Lev Walkina9cc46e2004-09-22 16:06:28 +000065#define ASN_DEBUG ASN_DEBUG_f
Lev Walkin588bf0f2017-10-13 23:51:16 -070066#endif /* C99 */
Lev Walkin9175a282017-10-24 00:58:42 -070067#else /* ASN_EMIT_DEBUG != 1 */
Lev Walkin2b290be2017-08-03 23:19:23 -070068#if __STDC_VERSION__ >= 199901L
69#define ASN_DEBUG(...) do{}while(0)
70#else /* not C99 */
Lev Walkind84f6032017-10-03 16:33:59 -070071static void CC_PRINTFLIKE(1, 2) ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
Lev Walkin2b290be2017-08-03 23:19:23 -070072#endif /* C99 or better */
Lev Walkin9175a282017-10-24 00:58:42 -070073#endif /* ASN_EMIT_DEBUG */
Lev Walkina9cc46e2004-09-22 16:06:28 +000074#endif /* ASN_DEBUG */
75
76/*
Lev Walkine2bbbf82017-10-08 18:52:37 -070077 * Print to a callback.
78 * The callback is expected to return negative values on error.
79 * 0 and positive values are treated as success.
80 * RETURN VALUES:
81 * -1: Failed to format or invoke the callback.
82 * >0: Size of the data that got delivered to the callback.
83 */
84ssize_t CC_PRINTFLIKE(3, 4)
85asn__format_to_callback(
86 int (*callback)(const void *, size_t, void *key), void *key,
87 const char *fmt, ...);
88
89/*
Lev Walkina9cc46e2004-09-22 16:06:28 +000090 * Invoke the application-supplied callback and fail, if something is wrong.
91 */
Lev Walkin4fe28822017-09-18 02:57:34 -070092#define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
93#define ASN__E_CALLBACK(size, foo) \
94 do { \
95 if(foo) goto cb_failed; \
96 er.encoded += (size); \
97 } while(0)
98#define ASN__CALLBACK(buf, size) ASN__E_CALLBACK(size, ASN__E_cbc(buf, size))
99#define ASN__CALLBACK2(buf1, size1, buf2, size2) \
100 ASN__E_CALLBACK((size1) + (size2), \
101 ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2))
102#define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \
103 ASN__E_CALLBACK((size1) + (size2) + (size3), \
104 ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2) \
105 || ASN__E_cbc(buf3, size3))
Lev Walkina9cc46e2004-09-22 16:06:28 +0000106
Lev Walkin4fe28822017-09-18 02:57:34 -0700107#define ASN__TEXT_INDENT(nl, level) \
108 do { \
109 int tmp_level = (level); \
110 int tmp_nl = ((nl) != 0); \
111 int tmp_i; \
112 if(tmp_nl) ASN__CALLBACK("\n", 1); \
113 if(tmp_level < 0) tmp_level = 0; \
114 for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) ASN__CALLBACK(" ", 4); \
Lev Walkin1ec76052016-03-13 17:13:20 -0700115 } while(0)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000116
Lev Walkin1ec76052016-03-13 17:13:20 -0700117#define _i_INDENT(nl) do { \
118 int tmp_i; \
119 if((nl) && cb("\n", 1, app_key) < 0) \
120 return -1; \
121 for(tmp_i = 0; tmp_i < ilevel; tmp_i++) \
122 if(cb(" ", 4, app_key) < 0) \
123 return -1; \
124 } while(0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000125
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000126/*
127 * Check stack against overflow, if limit is set.
128 */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700129#define ASN__DEFAULT_STACK_MAX (30000)
Lev Walkin3f995632017-09-26 18:27:32 -0700130static int CC_NOTUSED
Lev Walkinafbf2a92017-09-12 23:30:27 -0700131ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000132 if(ctx && ctx->max_stack_size) {
133
134 /* ctx MUST be allocated on the stack */
Lev Walkinafbf2a92017-09-12 23:30:27 -0700135 ptrdiff_t usedstack = ((const char *)ctx - (const char *)&ctx);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000136 if(usedstack > 0) usedstack = -usedstack; /* grows up! */
137
138 /* double negative required to avoid int wrap-around */
139 if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
140 ASN_DEBUG("Stack limit %ld reached",
141 (long)ctx->max_stack_size);
142 return -1;
143 }
144 }
145 return 0;
146}
147
Lev Walkin6a0370b2005-07-28 08:39:46 +0000148#ifdef __cplusplus
149}
150#endif
151
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700152#endif /* ASN_INTERNAL_H */