Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved. |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | /* |
| 6 | * Declarations internally useful for the ASN.1 support code. |
| 7 | */ |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 8 | #ifndef ASN_INTERNAL_H |
| 9 | #define ASN_INTERNAL_H |
Lev Walkin | 6cbed3d | 2017-10-07 16:42:41 -0700 | [diff] [blame] | 10 | #define __EXTENSIONS__ /* for Sun */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 11 | |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 12 | #include "asn_application.h" /* Application-visible API */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 13 | |
Lev Walkin | 2ffa1c6 | 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 | |
Lev Walkin | 6a0370b | 2005-07-28 08:39:46 +0000 | [diff] [blame] | 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 22 | /* Environment version might be used to avoid running with the old library */ |
Lev Walkin | cf6c1e2 | 2010-12-12 02:40:27 -0800 | [diff] [blame] | 23 | #define ASN1C_ENVIRONMENT_VERSION 923 /* Compile-time version */ |
Lev Walkin | 6a0370b | 2005-07-28 08:39:46 +0000 | [diff] [blame] | 24 | int get_asn1c_environment_version(void); /* Run-time version */ |
| 25 | |
Lev Walkin | a9cc46e | 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 | |
Lev Walkin | b85507a | 2012-03-02 03:54:57 -0800 | [diff] [blame] | 31 | #define asn_debug_indent 0 |
| 32 | #define ASN_DEBUG_INDENT_ADD(i) do{}while(0) |
| 33 | |
Lev Walkin | 9175a28 | 2017-10-24 00:58:42 -0700 | [diff] [blame] | 34 | #ifdef EMIT_ASN_DEBUG |
| 35 | #warning "Use ASN_EMIT_DEBUG instead of EMIT_ASN_DEBUG" |
| 36 | #define ASN_EMIT_DEBUG EMIT_ASN_DEBUG |
| 37 | #endif |
| 38 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 39 | /* |
| 40 | * A macro for debugging the ASN.1 internals. |
| 41 | * You may enable or override it. |
| 42 | */ |
| 43 | #ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */ |
Lev Walkin | 9175a28 | 2017-10-24 00:58:42 -0700 | [diff] [blame] | 44 | #if ASN_EMIT_DEBUG == 1 /* And it was asked to emit this code... */ |
Lev Walkin | 588bf0f | 2017-10-13 23:51:16 -0700 | [diff] [blame] | 45 | #if __STDC_VERSION__ >= 199901L |
Lev Walkin | 3e86a4e | 2007-06-29 11:38:57 +0000 | [diff] [blame] | 46 | #ifdef ASN_THREAD_SAFE |
Lev Walkin | b85507a | 2012-03-02 03:54:57 -0800 | [diff] [blame] | 47 | /* Thread safety requires sacrifice in output indentation: |
| 48 | * Retain empty definition of ASN_DEBUG_INDENT_ADD. */ |
Lev Walkin | 27f53bc | 2007-06-29 12:10:50 +0000 | [diff] [blame] | 49 | #else /* !ASN_THREAD_SAFE */ |
Lev Walkin | b85507a | 2012-03-02 03:54:57 -0800 | [diff] [blame] | 50 | #undef ASN_DEBUG_INDENT_ADD |
| 51 | #undef asn_debug_indent |
Lev Walkin | 3e86a4e | 2007-06-29 11:38:57 +0000 | [diff] [blame] | 52 | int asn_debug_indent; |
Lev Walkin | e2c05bf | 2012-02-27 10:24:35 -0800 | [diff] [blame] | 53 | #define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0) |
Lev Walkin | 27f53bc | 2007-06-29 12:10:50 +0000 | [diff] [blame] | 54 | #endif /* ASN_THREAD_SAFE */ |
Lev Walkin | 3e86a4e | 2007-06-29 11:38:57 +0000 | [diff] [blame] | 55 | #define ASN_DEBUG(fmt, args...) do { \ |
| 56 | int adi = asn_debug_indent; \ |
| 57 | while(adi--) fprintf(stderr, " "); \ |
| 58 | fprintf(stderr, fmt, ##args); \ |
| 59 | fprintf(stderr, " (%s:%d)\n", \ |
| 60 | __FILE__, __LINE__); \ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 61 | } while(0) |
Lev Walkin | 588bf0f | 2017-10-13 23:51:16 -0700 | [diff] [blame] | 62 | #else /* !C99 */ |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 63 | void CC_PRINTFLIKE(1, 2) ASN_DEBUG_f(const char *fmt, ...); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 64 | #define ASN_DEBUG ASN_DEBUG_f |
Lev Walkin | 588bf0f | 2017-10-13 23:51:16 -0700 | [diff] [blame] | 65 | #endif /* C99 */ |
Lev Walkin | 9175a28 | 2017-10-24 00:58:42 -0700 | [diff] [blame] | 66 | #else /* ASN_EMIT_DEBUG != 1 */ |
Lev Walkin | 2b290be | 2017-08-03 23:19:23 -0700 | [diff] [blame] | 67 | #if __STDC_VERSION__ >= 199901L |
| 68 | #define ASN_DEBUG(...) do{}while(0) |
| 69 | #else /* not C99 */ |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 70 | static void CC_PRINTFLIKE(1, 2) ASN_DEBUG(const char *fmt, ...) { (void)fmt; } |
Lev Walkin | 2b290be | 2017-08-03 23:19:23 -0700 | [diff] [blame] | 71 | #endif /* C99 or better */ |
Lev Walkin | 9175a28 | 2017-10-24 00:58:42 -0700 | [diff] [blame] | 72 | #endif /* ASN_EMIT_DEBUG */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 73 | #endif /* ASN_DEBUG */ |
| 74 | |
| 75 | /* |
Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 76 | * Print to a callback. |
| 77 | * The callback is expected to return negative values on error. |
| 78 | * 0 and positive values are treated as success. |
| 79 | * RETURN VALUES: |
| 80 | * -1: Failed to format or invoke the callback. |
| 81 | * >0: Size of the data that got delivered to the callback. |
| 82 | */ |
| 83 | ssize_t CC_PRINTFLIKE(3, 4) |
| 84 | asn__format_to_callback( |
| 85 | int (*callback)(const void *, size_t, void *key), void *key, |
| 86 | const char *fmt, ...); |
| 87 | |
| 88 | /* |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 89 | * Invoke the application-supplied callback and fail, if something is wrong. |
| 90 | */ |
Lev Walkin | 4fe2882 | 2017-09-18 02:57:34 -0700 | [diff] [blame] | 91 | #define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0) |
| 92 | #define ASN__E_CALLBACK(size, foo) \ |
| 93 | do { \ |
| 94 | if(foo) goto cb_failed; \ |
| 95 | er.encoded += (size); \ |
| 96 | } while(0) |
| 97 | #define ASN__CALLBACK(buf, size) ASN__E_CALLBACK(size, ASN__E_cbc(buf, size)) |
| 98 | #define ASN__CALLBACK2(buf1, size1, buf2, size2) \ |
| 99 | ASN__E_CALLBACK((size1) + (size2), \ |
| 100 | ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2)) |
| 101 | #define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \ |
| 102 | ASN__E_CALLBACK((size1) + (size2) + (size3), \ |
| 103 | ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2) \ |
| 104 | || ASN__E_cbc(buf3, size3)) |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 105 | |
Lev Walkin | 4fe2882 | 2017-09-18 02:57:34 -0700 | [diff] [blame] | 106 | #define ASN__TEXT_INDENT(nl, level) \ |
| 107 | do { \ |
| 108 | int tmp_level = (level); \ |
| 109 | int tmp_nl = ((nl) != 0); \ |
| 110 | int tmp_i; \ |
| 111 | if(tmp_nl) ASN__CALLBACK("\n", 1); \ |
| 112 | if(tmp_level < 0) tmp_level = 0; \ |
| 113 | for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) ASN__CALLBACK(" ", 4); \ |
Lev Walkin | 1ec7605 | 2016-03-13 17:13:20 -0700 | [diff] [blame] | 114 | } while(0) |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 115 | |
Lev Walkin | 1ec7605 | 2016-03-13 17:13:20 -0700 | [diff] [blame] | 116 | #define _i_INDENT(nl) do { \ |
| 117 | int tmp_i; \ |
| 118 | if((nl) && cb("\n", 1, app_key) < 0) \ |
| 119 | return -1; \ |
| 120 | for(tmp_i = 0; tmp_i < ilevel; tmp_i++) \ |
| 121 | if(cb(" ", 4, app_key) < 0) \ |
| 122 | return -1; \ |
| 123 | } while(0) |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 124 | |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 125 | /* |
| 126 | * Check stack against overflow, if limit is set. |
| 127 | */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 128 | #define ASN__DEFAULT_STACK_MAX (30000) |
Lev Walkin | 3f99563 | 2017-09-26 18:27:32 -0700 | [diff] [blame] | 129 | static int CC_NOTUSED |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 130 | ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) { |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 131 | if(ctx && ctx->max_stack_size) { |
| 132 | |
| 133 | /* ctx MUST be allocated on the stack */ |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 134 | ptrdiff_t usedstack = ((const char *)ctx - (const char *)&ctx); |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 135 | if(usedstack > 0) usedstack = -usedstack; /* grows up! */ |
| 136 | |
| 137 | /* double negative required to avoid int wrap-around */ |
| 138 | if(usedstack < -(ptrdiff_t)ctx->max_stack_size) { |
| 139 | ASN_DEBUG("Stack limit %ld reached", |
| 140 | (long)ctx->max_stack_size); |
| 141 | return -1; |
| 142 | } |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
Lev Walkin | 6a0370b | 2005-07-28 08:39:46 +0000 | [diff] [blame] | 147 | #ifdef __cplusplus |
| 148 | } |
| 149 | #endif |
| 150 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 151 | #endif /* ASN_INTERNAL_H */ |