blob: 53f12a6bbff1c6456cd3415e0e30c0905b1f48a8 [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 */
Lev Walkinc6cac8e2016-03-14 02:57:07 -07009#ifndef ASN_INTERNAL_H
10#define ASN_INTERNAL_H
Lev Walkin6cbed3d2017-10-07 16:42:41 -070011#define __EXTENSIONS__ /* for Sun */
Lev Walkina9cc46e2004-09-22 16:06:28 +000012
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 Walkina9cc46e2004-09-22 16:06:28 +000035/*
36 * A macro for debugging the ASN.1 internals.
37 * You may enable or override it.
38 */
39#ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */
40#if EMIT_ASN_DEBUG == 1 /* And it was asked to emit this code... */
41#ifdef __GNUC__
Lev Walkin3e86a4e2007-06-29 11:38:57 +000042#ifdef ASN_THREAD_SAFE
Lev Walkinb85507a2012-03-02 03:54:57 -080043/* Thread safety requires sacrifice in output indentation:
44 * Retain empty definition of ASN_DEBUG_INDENT_ADD. */
Lev Walkin27f53bc2007-06-29 12:10:50 +000045#else /* !ASN_THREAD_SAFE */
Lev Walkinb85507a2012-03-02 03:54:57 -080046#undef ASN_DEBUG_INDENT_ADD
47#undef asn_debug_indent
Lev Walkin3e86a4e2007-06-29 11:38:57 +000048int asn_debug_indent;
Lev Walkine2c05bf2012-02-27 10:24:35 -080049#define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)
Lev Walkin27f53bc2007-06-29 12:10:50 +000050#endif /* ASN_THREAD_SAFE */
Lev Walkin3e86a4e2007-06-29 11:38:57 +000051#define ASN_DEBUG(fmt, args...) do { \
52 int adi = asn_debug_indent; \
53 while(adi--) fprintf(stderr, " "); \
54 fprintf(stderr, fmt, ##args); \
55 fprintf(stderr, " (%s:%d)\n", \
56 __FILE__, __LINE__); \
Lev Walkina9cc46e2004-09-22 16:06:28 +000057 } while(0)
58#else /* !__GNUC__ */
Lev Walkind84f6032017-10-03 16:33:59 -070059void CC_PRINTFLIKE(1, 2) ASN_DEBUG_f(const char *fmt, ...);
Lev Walkina9cc46e2004-09-22 16:06:28 +000060#define ASN_DEBUG ASN_DEBUG_f
61#endif /* __GNUC__ */
62#else /* EMIT_ASN_DEBUG != 1 */
Lev Walkin2b290be2017-08-03 23:19:23 -070063#if __STDC_VERSION__ >= 199901L
64#define ASN_DEBUG(...) do{}while(0)
65#else /* not C99 */
Lev Walkind84f6032017-10-03 16:33:59 -070066static void CC_PRINTFLIKE(1, 2) ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
Lev Walkin2b290be2017-08-03 23:19:23 -070067#endif /* C99 or better */
Lev Walkina9cc46e2004-09-22 16:06:28 +000068#endif /* EMIT_ASN_DEBUG */
69#endif /* ASN_DEBUG */
70
71/*
72 * Invoke the application-supplied callback and fail, if something is wrong.
73 */
Lev Walkin4fe28822017-09-18 02:57:34 -070074#define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
75#define ASN__E_CALLBACK(size, foo) \
76 do { \
77 if(foo) goto cb_failed; \
78 er.encoded += (size); \
79 } while(0)
80#define ASN__CALLBACK(buf, size) ASN__E_CALLBACK(size, ASN__E_cbc(buf, size))
81#define ASN__CALLBACK2(buf1, size1, buf2, size2) \
82 ASN__E_CALLBACK((size1) + (size2), \
83 ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2))
84#define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \
85 ASN__E_CALLBACK((size1) + (size2) + (size3), \
86 ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2) \
87 || ASN__E_cbc(buf3, size3))
Lev Walkina9cc46e2004-09-22 16:06:28 +000088
Lev Walkin4fe28822017-09-18 02:57:34 -070089#define ASN__TEXT_INDENT(nl, level) \
90 do { \
91 int tmp_level = (level); \
92 int tmp_nl = ((nl) != 0); \
93 int tmp_i; \
94 if(tmp_nl) ASN__CALLBACK("\n", 1); \
95 if(tmp_level < 0) tmp_level = 0; \
96 for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) ASN__CALLBACK(" ", 4); \
Lev Walkin1ec76052016-03-13 17:13:20 -070097 } while(0)
Lev Walkin8e8078a2004-09-26 13:10:40 +000098
Lev Walkin1ec76052016-03-13 17:13:20 -070099#define _i_INDENT(nl) do { \
100 int tmp_i; \
101 if((nl) && cb("\n", 1, app_key) < 0) \
102 return -1; \
103 for(tmp_i = 0; tmp_i < ilevel; tmp_i++) \
104 if(cb(" ", 4, app_key) < 0) \
105 return -1; \
106 } while(0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000107
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000108/*
109 * Check stack against overflow, if limit is set.
110 */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700111#define ASN__DEFAULT_STACK_MAX (30000)
Lev Walkin3f995632017-09-26 18:27:32 -0700112static int CC_NOTUSED
Lev Walkinafbf2a92017-09-12 23:30:27 -0700113ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000114 if(ctx && ctx->max_stack_size) {
115
116 /* ctx MUST be allocated on the stack */
Lev Walkinafbf2a92017-09-12 23:30:27 -0700117 ptrdiff_t usedstack = ((const char *)ctx - (const char *)&ctx);
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000118 if(usedstack > 0) usedstack = -usedstack; /* grows up! */
119
120 /* double negative required to avoid int wrap-around */
121 if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
122 ASN_DEBUG("Stack limit %ld reached",
123 (long)ctx->max_stack_size);
124 return -1;
125 }
126 }
127 return 0;
128}
129
Lev Walkin6a0370b2005-07-28 08:39:46 +0000130#ifdef __cplusplus
131}
132#endif
133
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700134#endif /* ASN_INTERNAL_H */