blob: 84d692acc0fda96fbfa16008b8408e6407a074fa [file] [log] [blame]
Lev Walkina9cc46e2004-09-22 16:06:28 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
6 * Declarations internally useful for the ASN.1 support code.
7 */
8#ifndef _ASN_INTERNAL_H_
9#define _ASN_INTERNAL_H_
10
Lev Walkindc06f6b2004-10-20 15:50:55 +000011#define ASN1C_ENVIRONMENT_VERSION 98 /* Compile-time version */
Lev Walkina9cc46e2004-09-22 16:06:28 +000012int get_asn1c_environment_version(void); /* Run-time version */
13
Lev Walkin8e559942004-09-24 20:57:56 +000014#include <asn_application.h> /* Application-visible API */
Lev Walkina9cc46e2004-09-22 16:06:28 +000015
Lev Walkin2ffa1c62005-04-19 09:19:17 +000016#ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */
17#include <assert.h> /* for assert() macro */
18#endif
19
Lev Walkina9cc46e2004-09-22 16:06:28 +000020#define CALLOC(nmemb, size) calloc(nmemb, size)
21#define MALLOC(size) malloc(size)
22#define REALLOC(oldptr, size) realloc(oldptr, size)
23#define FREEMEM(ptr) free(ptr)
24
25/*
26 * A macro for debugging the ASN.1 internals.
27 * You may enable or override it.
28 */
29#ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */
30#if EMIT_ASN_DEBUG == 1 /* And it was asked to emit this code... */
31#ifdef __GNUC__
32#define ASN_DEBUG(fmt, args...) do { \
33 fprintf(stderr, fmt, ##args); \
Lev Walkin8e559942004-09-24 20:57:56 +000034 fprintf(stderr, " (%s:%d)\n", \
35 __FILE__, __LINE__); \
Lev Walkina9cc46e2004-09-22 16:06:28 +000036 } while(0)
37#else /* !__GNUC__ */
Lev Walkin8e8078a2004-09-26 13:10:40 +000038void ASN_DEBUG_f(const char *fmt, ...);
Lev Walkina9cc46e2004-09-22 16:06:28 +000039#define ASN_DEBUG ASN_DEBUG_f
40#endif /* __GNUC__ */
41#else /* EMIT_ASN_DEBUG != 1 */
Lev Walkinfdb25922005-07-21 09:32:49 +000042static inline void ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
Lev Walkina9cc46e2004-09-22 16:06:28 +000043#endif /* EMIT_ASN_DEBUG */
44#endif /* ASN_DEBUG */
45
46/*
47 * Invoke the application-supplied callback and fail, if something is wrong.
48 */
Lev Walkin942fd082004-10-03 09:13:02 +000049#define __ASN_E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +000050#define _ASN_E_CALLBACK(foo) do { \
Lev Walkin942fd082004-10-03 09:13:02 +000051 if(foo) goto cb_failed; \
Lev Walkina9cc46e2004-09-22 16:06:28 +000052 } while(0)
53#define _ASN_CALLBACK(buf, size) \
54 _ASN_E_CALLBACK(__ASN_E_cbc(buf, size))
55#define _ASN_CALLBACK2(buf1, size1, buf2, size2) \
56 _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) || __ASN_E_cbc(buf2, size2))
57#define _ASN_CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \
58 _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) \
59 || __ASN_E_cbc(buf2, size2) \
60 || __ASN_E_cbc(buf3, size3))
61
62#define _i_ASN_TEXT_INDENT(nl, level) do { \
Lev Walkin8e8078a2004-09-26 13:10:40 +000063 int __level = (level); \
64 int __nl = ((nl) != 0); \
65 int __i; \
66 if(__nl) _ASN_CALLBACK("\n", 1); \
67 for(__i = 0; __i < __level; __i++) \
68 _ASN_CALLBACK(" ", 4); \
69 er.encoded += __nl + 4 * __level; \
70} while(0)
71
72#define _i_INDENT(nl) do { \
73 int __i; \
74 if((nl) && cb("\n", 1, app_key) < 0) return -1; \
75 for(__i = 0; __i < ilevel; __i++) \
76 if(cb(" ", 4, app_key) < 0) return -1; \
Lev Walkina9cc46e2004-09-22 16:06:28 +000077} while(0)
78
Lev Walkina9cc46e2004-09-22 16:06:28 +000079#endif /* _ASN_INTERNAL_H_ */