blob: 9c94ca6c3b77b069c657d3c03ea0e3e650128747 [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 Walkina9cc46e2004-09-22 16:06:28 +000011
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 */
Lev Walkincf6c1e22010-12-12 02:40:27 -080023#define ASN1C_ENVIRONMENT_VERSION 923 /* 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 */
Lev Walkin3e86a4e2007-06-29 11:38:57 +000050#define ASN_DEBUG(fmt, args...) do { \
51 int adi = asn_debug_indent; \
52 while(adi--) fprintf(stderr, " "); \
53 fprintf(stderr, fmt, ##args); \
54 fprintf(stderr, " (%s:%d)\n", \
55 __FILE__, __LINE__); \
Lev Walkina9cc46e2004-09-22 16:06:28 +000056 } while(0)
57#else /* !__GNUC__ */
Lev Walkin8e8078a2004-09-26 13:10:40 +000058void ASN_DEBUG_f(const char *fmt, ...);
Lev Walkina9cc46e2004-09-22 16:06:28 +000059#define ASN_DEBUG ASN_DEBUG_f
60#endif /* __GNUC__ */
61#else /* EMIT_ASN_DEBUG != 1 */
Lev Walkin104af192016-01-24 22:13:27 -080062static void ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
Lev Walkina9cc46e2004-09-22 16:06:28 +000063#endif /* EMIT_ASN_DEBUG */
64#endif /* ASN_DEBUG */
65
66/*
67 * Invoke the application-supplied callback and fail, if something is wrong.
68 */
Lev Walkinc6cac8e2016-03-14 02:57:07 -070069#define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
70#define ASN__E_CALLBACK(foo) do { \
Lev Walkin942fd082004-10-03 09:13:02 +000071 if(foo) goto cb_failed; \
Lev Walkina9cc46e2004-09-22 16:06:28 +000072 } while(0)
Lev Walkin7c1dc052016-03-14 03:08:15 -070073#define ASN__CALLBACK(buf, size) \
Lev Walkinc6cac8e2016-03-14 02:57:07 -070074 ASN__E_CALLBACK(ASN__E_cbc(buf, size))
Lev Walkin7c1dc052016-03-14 03:08:15 -070075#define ASN__CALLBACK2(buf1, size1, buf2, size2) \
Lev Walkinc6cac8e2016-03-14 02:57:07 -070076 ASN__E_CALLBACK(ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2))
Lev Walkin7c1dc052016-03-14 03:08:15 -070077#define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \
Lev Walkinc6cac8e2016-03-14 02:57:07 -070078 ASN__E_CALLBACK(ASN__E_cbc(buf1, size1) \
79 || ASN__E_cbc(buf2, size2) \
80 || ASN__E_cbc(buf3, size3))
Lev Walkina9cc46e2004-09-22 16:06:28 +000081
Lev Walkinc6cac8e2016-03-14 02:57:07 -070082#define ASN__TEXT_INDENT(nl, level) do { \
Lev Walkin1ec76052016-03-13 17:13:20 -070083 int tmp_level = (level); \
84 int tmp_nl = ((nl) != 0); \
85 int tmp_i; \
Lev Walkin7c1dc052016-03-14 03:08:15 -070086 if(tmp_nl) ASN__CALLBACK("\n", 1); \
Lev Walkin1ec76052016-03-13 17:13:20 -070087 if(tmp_level < 0) tmp_level = 0; \
88 for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) \
Lev Walkin7c1dc052016-03-14 03:08:15 -070089 ASN__CALLBACK(" ", 4); \
Lev Walkin1ec76052016-03-13 17:13:20 -070090 er.encoded += tmp_nl + 4 * tmp_level; \
91 } while(0)
Lev Walkin8e8078a2004-09-26 13:10:40 +000092
Lev Walkin1ec76052016-03-13 17:13:20 -070093#define _i_INDENT(nl) do { \
94 int tmp_i; \
95 if((nl) && cb("\n", 1, app_key) < 0) \
96 return -1; \
97 for(tmp_i = 0; tmp_i < ilevel; tmp_i++) \
98 if(cb(" ", 4, app_key) < 0) \
99 return -1; \
100 } while(0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000101
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000102/*
103 * Check stack against overflow, if limit is set.
104 */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700105#define ASN__DEFAULT_STACK_MAX (30000)
Lev Walkin104af192016-01-24 22:13:27 -0800106static int __attribute__((unused))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700107ASN__STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) {
Lev Walkin1d9e8dd2005-12-07 05:46:03 +0000108 if(ctx && ctx->max_stack_size) {
109
110 /* ctx MUST be allocated on the stack */
111 ptrdiff_t usedstack = ((char *)ctx - (char *)&ctx);
112 if(usedstack > 0) usedstack = -usedstack; /* grows up! */
113
114 /* double negative required to avoid int wrap-around */
115 if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
116 ASN_DEBUG("Stack limit %ld reached",
117 (long)ctx->max_stack_size);
118 return -1;
119 }
120 }
121 return 0;
122}
123
Lev Walkin6a0370b2005-07-28 08:39:46 +0000124#ifdef __cplusplus
125}
126#endif
127
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700128#endif /* ASN_INTERNAL_H */