vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1 | /*- |
vlm | d98cbfb | 2005-08-03 07:01:32 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 4 | * 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 | |
vlm | af68ef5 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 12 | #include "asn_application.h" /* Application-visible API */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 13 | |
vlm | ad96ab2 | 2005-04-19 09:19:17 +0000 | [diff] [blame] | 14 | #ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */ |
| 15 | #include <assert.h> /* for assert() macro */ |
| 16 | #endif |
| 17 | |
vlm | c1e72a9 | 2005-07-28 08:39:46 +0000 | [diff] [blame] | 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 22 | /* Environment version might be used to avoid running with the old library */ |
vlm | 6d44a54 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 23 | #define ASN1C_ENVIRONMENT_VERSION 920 /* Compile-time version */ |
vlm | c1e72a9 | 2005-07-28 08:39:46 +0000 | [diff] [blame] | 24 | int get_asn1c_environment_version(void); /* Run-time version */ |
| 25 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 26 | #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__ |
| 38 | #define ASN_DEBUG(fmt, args...) do { \ |
| 39 | fprintf(stderr, fmt, ##args); \ |
vlm | 22dcbd1 | 2004-09-24 20:57:56 +0000 | [diff] [blame] | 40 | fprintf(stderr, " (%s:%d)\n", \ |
| 41 | __FILE__, __LINE__); \ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 42 | } while(0) |
| 43 | #else /* !__GNUC__ */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 44 | void ASN_DEBUG_f(const char *fmt, ...); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 45 | #define ASN_DEBUG ASN_DEBUG_f |
| 46 | #endif /* __GNUC__ */ |
| 47 | #else /* EMIT_ASN_DEBUG != 1 */ |
vlm | e66fd8a | 2005-07-21 09:32:49 +0000 | [diff] [blame] | 48 | static inline void ASN_DEBUG(const char *fmt, ...) { (void)fmt; } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 49 | #endif /* EMIT_ASN_DEBUG */ |
| 50 | #endif /* ASN_DEBUG */ |
| 51 | |
| 52 | /* |
| 53 | * Invoke the application-supplied callback and fail, if something is wrong. |
| 54 | */ |
vlm | 84d551b | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 55 | #define __ASN_E_cbc(buf, size) (cb((buf), (size), app_key) < 0) |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 56 | #define _ASN_E_CALLBACK(foo) do { \ |
vlm | 84d551b | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 57 | if(foo) goto cb_failed; \ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 58 | } while(0) |
| 59 | #define _ASN_CALLBACK(buf, size) \ |
| 60 | _ASN_E_CALLBACK(__ASN_E_cbc(buf, size)) |
| 61 | #define _ASN_CALLBACK2(buf1, size1, buf2, size2) \ |
| 62 | _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) || __ASN_E_cbc(buf2, size2)) |
| 63 | #define _ASN_CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \ |
| 64 | _ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) \ |
| 65 | || __ASN_E_cbc(buf2, size2) \ |
| 66 | || __ASN_E_cbc(buf3, size3)) |
| 67 | |
| 68 | #define _i_ASN_TEXT_INDENT(nl, level) do { \ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 69 | int __level = (level); \ |
| 70 | int __nl = ((nl) != 0); \ |
| 71 | int __i; \ |
| 72 | if(__nl) _ASN_CALLBACK("\n", 1); \ |
| 73 | for(__i = 0; __i < __level; __i++) \ |
| 74 | _ASN_CALLBACK(" ", 4); \ |
| 75 | er.encoded += __nl + 4 * __level; \ |
| 76 | } while(0) |
| 77 | |
| 78 | #define _i_INDENT(nl) do { \ |
| 79 | int __i; \ |
| 80 | if((nl) && cb("\n", 1, app_key) < 0) return -1; \ |
| 81 | for(__i = 0; __i < ilevel; __i++) \ |
| 82 | if(cb(" ", 4, app_key) < 0) return -1; \ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 83 | } while(0) |
| 84 | |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 85 | /* |
| 86 | * Check stack against overflow, if limit is set. |
| 87 | */ |
| 88 | #define _ASN_DEFAULT_STACK_MAX (30000) |
| 89 | static inline int |
| 90 | _ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) { |
| 91 | if(ctx && ctx->max_stack_size) { |
| 92 | |
| 93 | /* ctx MUST be allocated on the stack */ |
| 94 | ptrdiff_t usedstack = ((char *)ctx - (char *)&ctx); |
| 95 | if(usedstack > 0) usedstack = -usedstack; /* grows up! */ |
| 96 | |
| 97 | /* double negative required to avoid int wrap-around */ |
| 98 | if(usedstack < -(ptrdiff_t)ctx->max_stack_size) { |
| 99 | ASN_DEBUG("Stack limit %ld reached", |
| 100 | (long)ctx->max_stack_size); |
| 101 | return -1; |
| 102 | } |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
vlm | c1e72a9 | 2005-07-28 08:39:46 +0000 | [diff] [blame] | 107 | #ifdef __cplusplus |
| 108 | } |
| 109 | #endif |
| 110 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 111 | #endif /* _ASN_INTERNAL_H_ */ |