asn_internal.h 4.2 KB
Newer Older
Lev Walkin's avatar
Lev Walkin committed
1
/*-
Lev Walkin's avatar
Lev Walkin committed
2
 * Copyright (c) 2003, 2004, 2005, 2007 Lev Walkin <vlm@lionet.info>.
Lev Walkin's avatar
Lev Walkin committed
3
 * All rights reserved.
Lev Walkin's avatar
Lev Walkin committed
4 5 6 7 8
 * Redistribution and modifications are permitted subject to BSD license.
 */
/*
 * Declarations internally useful for the ASN.1 support code.
 */
9 10
#ifndef	ASN_INTERNAL_H
#define	ASN_INTERNAL_H
Lev Walkin's avatar
Lev Walkin committed
11

Lev Walkin's avatar
Lev Walkin committed
12
#include "asn_application.h"	/* Application-visible API */
Lev Walkin's avatar
Lev Walkin committed
13

14 15 16 17
#ifndef	__NO_ASSERT_H__		/* Include assert.h only for internal use. */
#include <assert.h>		/* for assert() macro */
#endif

Lev Walkin's avatar
Lev Walkin committed
18 19 20 21
#ifdef	__cplusplus
extern "C" {
#endif

22
/* Environment version might be used to avoid running with the old library */
Lev Walkin's avatar
Lev Walkin committed
23
#define	ASN1C_ENVIRONMENT_VERSION	923	/* Compile-time version */
Lev Walkin's avatar
Lev Walkin committed
24 25
int get_asn1c_environment_version(void);	/* Run-time version */

Lev Walkin's avatar
Lev Walkin committed
26 27 28 29 30
#define	CALLOC(nmemb, size)	calloc(nmemb, size)
#define	MALLOC(size)		malloc(size)
#define	REALLOC(oldptr, size)	realloc(oldptr, size)
#define	FREEMEM(ptr)		free(ptr)

Lev Walkin's avatar
Lev Walkin committed
31 32 33
#define	asn_debug_indent	0
#define ASN_DEBUG_INDENT_ADD(i) do{}while(0)

Lev Walkin's avatar
Lev Walkin committed
34 35 36 37 38 39 40
/*
 * A macro for debugging the ASN.1 internals.
 * You may enable or override it.
 */
#ifndef	ASN_DEBUG	/* If debugging code is not defined elsewhere... */
#if	EMIT_ASN_DEBUG == 1	/* And it was asked to emit this code... */
#ifdef	__GNUC__
Lev Walkin's avatar
Lev Walkin committed
41
#ifdef	ASN_THREAD_SAFE
Lev Walkin's avatar
Lev Walkin committed
42 43
/* Thread safety requires sacrifice in output indentation:
 * Retain empty definition of ASN_DEBUG_INDENT_ADD. */
Lev Walkin's avatar
Lev Walkin committed
44
#else	/* !ASN_THREAD_SAFE */
Lev Walkin's avatar
Lev Walkin committed
45 46
#undef  ASN_DEBUG_INDENT_ADD
#undef  asn_debug_indent
Lev Walkin's avatar
Lev Walkin committed
47
int asn_debug_indent;
Lev Walkin's avatar
Lev Walkin committed
48
#define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)
Lev Walkin's avatar
Lev Walkin committed
49
#endif	/* ASN_THREAD_SAFE */
Lev Walkin's avatar
Lev Walkin committed
50 51 52 53 54 55
#define	ASN_DEBUG(fmt, args...)	do {			\
		int adi = asn_debug_indent;		\
		while(adi--) fprintf(stderr, " ");	\
		fprintf(stderr, fmt, ##args);		\
		fprintf(stderr, " (%s:%d)\n",		\
			__FILE__, __LINE__);		\
Lev Walkin's avatar
Lev Walkin committed
56 57
	} while(0)
#else	/* !__GNUC__ */
58
void ASN_DEBUG_f(const char *fmt, ...);
Lev Walkin's avatar
Lev Walkin committed
59 60 61
#define	ASN_DEBUG	ASN_DEBUG_f
#endif	/* __GNUC__ */
#else	/* EMIT_ASN_DEBUG != 1 */
62 63 64
#if __STDC_VERSION__ >= 199901L
#define ASN_DEBUG(...) do{}while(0)
#else   /* not C99 */
Lev Walkin's avatar
Lev Walkin committed
65
static void ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
66
#endif  /* C99 or better */
Lev Walkin's avatar
Lev Walkin committed
67 68 69 70 71 72
#endif	/* EMIT_ASN_DEBUG */
#endif	/* ASN_DEBUG */

/*
 * Invoke the application-supplied callback and fail, if something is wrong.
 */
73 74
#define	ASN__E_cbc(buf, size)	(cb((buf), (size), app_key) < 0)
#define	ASN__E_CALLBACK(foo)	do {					\
Lev Walkin's avatar
Lev Walkin committed
75
		if(foo)	goto cb_failed;					\
Lev Walkin's avatar
Lev Walkin committed
76
	} 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 83 84
	ASN__E_CALLBACK(ASN__E_cbc(buf1, size1)			\
		|| ASN__E_cbc(buf2, size2)				\
		|| ASN__E_cbc(buf3, size3))
Lev Walkin's avatar
Lev Walkin committed
85

86
#define	ASN__TEXT_INDENT(nl, level) do {            \
87 88 89
        int tmp_level = (level);                    \
        int tmp_nl = ((nl) != 0);                   \
        int tmp_i;                                  \
90
        if(tmp_nl) ASN__CALLBACK("\n", 1);          \
91 92
        if(tmp_level < 0) tmp_level = 0;            \
        for(tmp_i = 0; tmp_i < tmp_level; tmp_i++)  \
93
            ASN__CALLBACK("    ", 4);               \
94 95
        er.encoded += tmp_nl + 4 * tmp_level;       \
    } while(0)
96

97 98 99 100 101 102 103 104
#define	_i_INDENT(nl)	do {                        \
        int tmp_i;                                  \
        if((nl) && cb("\n", 1, app_key) < 0)        \
            return -1;                              \
        for(tmp_i = 0; tmp_i < ilevel; tmp_i++)     \
            if(cb("    ", 4, app_key) < 0)          \
                return -1;                          \
    } while(0)
Lev Walkin's avatar
Lev Walkin committed
105

Lev Walkin's avatar
Lev Walkin committed
106 107 108
/*
 * Check stack against overflow, if limit is set.
 */
109
#define	ASN__DEFAULT_STACK_MAX	(30000)
Lev Walkin's avatar
Lev Walkin committed
110
static int GCC_NOTUSED
Lev Walkin's avatar
Lev Walkin committed
111
ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
Lev Walkin's avatar
Lev Walkin committed
112 113 114
	if(ctx && ctx->max_stack_size) {

		/* ctx MUST be allocated on the stack */
Lev Walkin's avatar
Lev Walkin committed
115
		ptrdiff_t usedstack = ((const char *)ctx - (const char *)&ctx);
Lev Walkin's avatar
Lev Walkin committed
116 117 118 119 120 121 122 123 124 125 126 127
		if(usedstack > 0) usedstack = -usedstack; /* grows up! */

		/* double negative required to avoid int wrap-around */
		if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
			ASN_DEBUG("Stack limit %ld reached",
				(long)ctx->max_stack_size);
			return -1;
		}
	}
	return 0;
}

Lev Walkin's avatar
Lev Walkin committed
128 129 130 131
#ifdef	__cplusplus
}
#endif

132
#endif	/* ASN_INTERNAL_H */