Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1 | /*- |
| 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 Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 11 | #define ASN1C_ENVIRONMENT_VERSION 98 /* Compile-time version */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 12 | int get_asn1c_environment_version(void); /* Run-time version */ |
| 13 | |
Lev Walkin | 8e55994 | 2004-09-24 20:57:56 +0000 | [diff] [blame] | 14 | #include <asn_application.h> /* Application-visible API */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 15 | |
Lev Walkin | 2ffa1c6 | 2005-04-19 09:19:17 +0000 | [diff] [blame] | 16 | #ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */ |
| 17 | #include <assert.h> /* for assert() macro */ |
| 18 | #endif |
| 19 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 20 | #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 Walkin | 8e55994 | 2004-09-24 20:57:56 +0000 | [diff] [blame] | 34 | fprintf(stderr, " (%s:%d)\n", \ |
| 35 | __FILE__, __LINE__); \ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 36 | } while(0) |
| 37 | #else /* !__GNUC__ */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 38 | void ASN_DEBUG_f(const char *fmt, ...); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 39 | #define ASN_DEBUG ASN_DEBUG_f |
| 40 | #endif /* __GNUC__ */ |
| 41 | #else /* EMIT_ASN_DEBUG != 1 */ |
| 42 | #ifdef __GNUC__ |
| 43 | #define ASN_DEBUG(fmt, args...) ((void)0) /* Emit a no-op operator */ |
| 44 | #else /* __GNUC__ */ |
| 45 | static void ASN_DEBUG(const char *fmt, ...) { (void)fmt; }; |
| 46 | #endif /* __GNUC__ */ |
| 47 | #endif /* EMIT_ASN_DEBUG */ |
| 48 | #endif /* ASN_DEBUG */ |
| 49 | |
| 50 | /* |
| 51 | * Invoke the application-supplied callback and fail, if something is wrong. |
| 52 | */ |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 53 | #define __ASN_E_cbc(buf, size) (cb((buf), (size), app_key) < 0) |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 54 | #define _ASN_E_CALLBACK(foo) do { \ |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 55 | if(foo) goto cb_failed; \ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 56 | } while(0) |
| 57 | #define _ASN_CALLBACK(buf, size) \ |
| 58 | _ASN_E_CALLBACK(__ASN_E_cbc(buf, size)) |
| 59 | #define _ASN_CALLBACK2(buf1, size1, buf2, size2) \ |
| 60 | _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) || __ASN_E_cbc(buf2, size2)) |
| 61 | #define _ASN_CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \ |
| 62 | _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) \ |
| 63 | || __ASN_E_cbc(buf2, size2) \ |
| 64 | || __ASN_E_cbc(buf3, size3)) |
| 65 | |
| 66 | #define _i_ASN_TEXT_INDENT(nl, level) do { \ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 67 | int __level = (level); \ |
| 68 | int __nl = ((nl) != 0); \ |
| 69 | int __i; \ |
| 70 | if(__nl) _ASN_CALLBACK("\n", 1); \ |
| 71 | for(__i = 0; __i < __level; __i++) \ |
| 72 | _ASN_CALLBACK(" ", 4); \ |
| 73 | er.encoded += __nl + 4 * __level; \ |
| 74 | } while(0) |
| 75 | |
| 76 | #define _i_INDENT(nl) do { \ |
| 77 | int __i; \ |
| 78 | if((nl) && cb("\n", 1, app_key) < 0) return -1; \ |
| 79 | for(__i = 0; __i < ilevel; __i++) \ |
| 80 | if(cb(" ", 4, app_key) < 0) return -1; \ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 81 | } while(0) |
| 82 | |
| 83 | #endif /* _ASN_INTERNAL_H_ */ |