blob: 249d7ef64fd5aae74e740a07d0787baae82b265b [file] [log] [blame]
vlm39ba4c42004-09-22 16:06:28 +00001/*-
vlmd97308e2007-06-29 11:38:57 +00002 * Copyright (c) 2003, 2004, 2005, 2007 Lev Walkin <vlm@lionet.info>.
vlmd98cbfb2005-08-03 07:01:32 +00003 * All rights reserved.
vlm39ba4c42004-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
vlmaf68ef52006-07-13 11:19:01 +000012#include "asn_application.h" /* Application-visible API */
vlm39ba4c42004-09-22 16:06:28 +000013
vlmad96ab22005-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
vlmc1e72a92005-07-28 08:39:46 +000018#ifdef __cplusplus
19extern "C" {
20#endif
21
vlm5a6fc652005-08-16 17:00:21 +000022/* Environment version might be used to avoid running with the old library */
vlmec88ee42007-06-23 19:37:28 +000023#define ASN1C_ENVIRONMENT_VERSION 922 /* Compile-time version */
vlmc1e72a92005-07-28 08:39:46 +000024int get_asn1c_environment_version(void); /* Run-time version */
25
vlm39ba4c42004-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
31/*
32 * A macro for debugging the ASN.1 internals.
33 * You may enable or override it.
34 */
35#ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */
36#if EMIT_ASN_DEBUG == 1 /* And it was asked to emit this code... */
37#ifdef __GNUC__
vlmd97308e2007-06-29 11:38:57 +000038#ifdef ASN_THREAD_SAFE
39#define asn_debug_indent 0
vlmbed38dd2007-06-29 12:10:50 +000040#else /* !ASN_THREAD_SAFE */
vlmd97308e2007-06-29 11:38:57 +000041int asn_debug_indent;
vlmbed38dd2007-06-29 12:10:50 +000042#endif /* ASN_THREAD_SAFE */
vlmd97308e2007-06-29 11:38:57 +000043#define ASN_DEBUG(fmt, args...) do { \
44 int adi = asn_debug_indent; \
45 while(adi--) fprintf(stderr, " "); \
46 fprintf(stderr, fmt, ##args); \
47 fprintf(stderr, " (%s:%d)\n", \
48 __FILE__, __LINE__); \
vlm39ba4c42004-09-22 16:06:28 +000049 } while(0)
50#else /* !__GNUC__ */
vlm6678cb12004-09-26 13:10:40 +000051void ASN_DEBUG_f(const char *fmt, ...);
vlm39ba4c42004-09-22 16:06:28 +000052#define ASN_DEBUG ASN_DEBUG_f
53#endif /* __GNUC__ */
54#else /* EMIT_ASN_DEBUG != 1 */
vlme66fd8a2005-07-21 09:32:49 +000055static inline void ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
vlm39ba4c42004-09-22 16:06:28 +000056#endif /* EMIT_ASN_DEBUG */
57#endif /* ASN_DEBUG */
58
59/*
60 * Invoke the application-supplied callback and fail, if something is wrong.
61 */
vlm84d551b2004-10-03 09:13:02 +000062#define __ASN_E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
vlm39ba4c42004-09-22 16:06:28 +000063#define _ASN_E_CALLBACK(foo) do { \
vlm84d551b2004-10-03 09:13:02 +000064 if(foo) goto cb_failed; \
vlm39ba4c42004-09-22 16:06:28 +000065 } while(0)
66#define _ASN_CALLBACK(buf, size) \
67 _ASN_E_CALLBACK(__ASN_E_cbc(buf, size))
68#define _ASN_CALLBACK2(buf1, size1, buf2, size2) \
69 _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) || __ASN_E_cbc(buf2, size2))
70#define _ASN_CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \
71 _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) \
72 || __ASN_E_cbc(buf2, size2) \
73 || __ASN_E_cbc(buf3, size3))
74
75#define _i_ASN_TEXT_INDENT(nl, level) do { \
vlm6678cb12004-09-26 13:10:40 +000076 int __level = (level); \
77 int __nl = ((nl) != 0); \
78 int __i; \
79 if(__nl) _ASN_CALLBACK("\n", 1); \
vlm1b91c612006-12-06 08:39:47 +000080 if(__level < 0) __level = 0; \
vlm6678cb12004-09-26 13:10:40 +000081 for(__i = 0; __i < __level; __i++) \
82 _ASN_CALLBACK(" ", 4); \
83 er.encoded += __nl + 4 * __level; \
84} while(0)
85
86#define _i_INDENT(nl) do { \
87 int __i; \
88 if((nl) && cb("\n", 1, app_key) < 0) return -1; \
89 for(__i = 0; __i < ilevel; __i++) \
90 if(cb(" ", 4, app_key) < 0) return -1; \
vlm39ba4c42004-09-22 16:06:28 +000091} while(0)
92
vlm4d2ca122005-12-07 05:46:03 +000093/*
94 * Check stack against overflow, if limit is set.
95 */
96#define _ASN_DEFAULT_STACK_MAX (30000)
97static inline int
98_ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) {
99 if(ctx && ctx->max_stack_size) {
100
101 /* ctx MUST be allocated on the stack */
102 ptrdiff_t usedstack = ((char *)ctx - (char *)&ctx);
103 if(usedstack > 0) usedstack = -usedstack; /* grows up! */
104
105 /* double negative required to avoid int wrap-around */
106 if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
107 ASN_DEBUG("Stack limit %ld reached",
108 (long)ctx->max_stack_size);
109 return -1;
110 }
111 }
112 return 0;
113}
114
vlmc1e72a92005-07-28 08:39:46 +0000115#ifdef __cplusplus
116}
117#endif
118
vlm39ba4c42004-09-22 16:06:28 +0000119#endif /* _ASN_INTERNAL_H_ */