Harry Tran | 1f1098a | 2020-03-10 10:40:10 -0400 | [diff] [blame] | 1 | /***************************************************************************** |
| 2 | # * |
| 3 | # Copyright 2019 AT&T Intellectual Property * |
| 4 | # * |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); * |
| 6 | # you may not use this file except in compliance with the License. * |
| 7 | # You may obtain a copy of the License at * |
| 8 | # * |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 * |
| 10 | # * |
| 11 | # Unless required by applicable law or agreed to in writing, software * |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, * |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 14 | # See the License for the specific language governing permissions and * |
| 15 | # limitations under the License. * |
| 16 | # * |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | /* |
| 20 | * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 21 | * Redistribution and modifications are permitted subject to BSD license. |
| 22 | */ |
| 23 | /* |
| 24 | * Declarations internally useful for the ASN.1 support code. |
| 25 | */ |
| 26 | #ifndef ASN_INTERNAL_H |
| 27 | #define ASN_INTERNAL_H |
| 28 | #define __EXTENSIONS__ /* for Sun */ |
| 29 | |
| 30 | #include "asn_application.h" /* Application-visible API */ |
| 31 | |
| 32 | #ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */ |
| 33 | #include <assert.h> /* for assert() macro */ |
| 34 | #endif |
| 35 | |
| 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
| 40 | /* Environment version might be used to avoid running with the old library */ |
| 41 | #define ASN1C_ENVIRONMENT_VERSION 923 /* Compile-time version */ |
| 42 | int get_asn1c_environment_version(void); /* Run-time version */ |
| 43 | |
| 44 | #define CALLOC(nmemb, size) calloc(nmemb, size) |
| 45 | #define MALLOC(size) malloc(size) |
| 46 | #define REALLOC(oldptr, size) realloc(oldptr, size) |
| 47 | #define FREEMEM(ptr) free(ptr) |
| 48 | |
| 49 | #define asn_debug_indent 0 |
| 50 | #define ASN_DEBUG_INDENT_ADD(i) do{}while(0) |
| 51 | |
| 52 | #ifdef EMIT_ASN_DEBUG |
| 53 | #warning "Use ASN_EMIT_DEBUG instead of EMIT_ASN_DEBUG" |
| 54 | #define ASN_EMIT_DEBUG EMIT_ASN_DEBUG |
| 55 | #endif |
| 56 | |
| 57 | /* |
| 58 | * A macro for debugging the ASN.1 internals. |
| 59 | * You may enable or override it. |
| 60 | */ |
| 61 | #ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */ |
| 62 | #if ASN_EMIT_DEBUG == 1 /* And it was asked to emit this code... */ |
| 63 | #if !defined(BELL_LABS) /* Bell Labs */ |
| 64 | //#if __STDC_VERSION__ >= 199901L |
| 65 | #ifdef ASN_THREAD_SAFE |
| 66 | /* Thread safety requires sacrifice in output indentation: |
| 67 | * Retain empty definition of ASN_DEBUG_INDENT_ADD. */ |
| 68 | #else /* !ASN_THREAD_SAFE */ |
| 69 | #undef ASN_DEBUG_INDENT_ADD |
| 70 | #undef asn_debug_indent |
| 71 | int asn_debug_indent; |
| 72 | #define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0) |
| 73 | #endif /* ASN_THREAD_SAFE */ |
| 74 | #if defined(BELL_LABS) /* Bell Labs version */ |
| 75 | extern int logAsn1c(const char *filename, int linenumber, const char *format, ...); |
| 76 | #define ASN_DEBUG(fmt, args...) do { \ |
| 77 | (void) logAsn1c(__FILE__, __LINE__, fmt, ##args); \ |
| 78 | } while(0) |
| 79 | #else |
| 80 | #define ASN_DEBUG(fmt, args...) do { \ |
| 81 | int adi = asn_debug_indent; \ |
| 82 | while(adi--) fprintf(stderr, " "); \ |
| 83 | fprintf(stderr, fmt, ##args); \ |
| 84 | fprintf(stderr, " (%s:%d)\n", \ |
| 85 | __FILE__, __LINE__); \ |
| 86 | } while(0) |
| 87 | #endif /* BELL_LABS */ |
| 88 | #else /* !C99 */ |
| 89 | void CC_PRINTFLIKE(1, 2) ASN_DEBUG_f(const char *fmt, ...); |
| 90 | #define ASN_DEBUG ASN_DEBUG_f |
| 91 | #endif /* C99 */ |
| 92 | #else /* ASN_EMIT_DEBUG != 1 */ |
| 93 | #if __STDC_VERSION__ >= 199901L |
| 94 | #define ASN_DEBUG(...) do{}while(0) |
| 95 | #else /* not C99 */ |
| 96 | static void CC_PRINTFLIKE(1, 2) ASN_DEBUG(const char *fmt, ...) { (void)fmt; } |
| 97 | #endif /* C99 or better */ |
| 98 | #endif /* ASN_EMIT_DEBUG */ |
| 99 | #endif /* ASN_DEBUG */ |
| 100 | |
| 101 | /* |
| 102 | * Print to a callback. |
| 103 | * The callback is expected to return negative values on error. |
| 104 | * 0 and positive values are treated as success. |
| 105 | * RETURN VALUES: |
| 106 | * -1: Failed to format or invoke the callback. |
| 107 | * >0: Size of the data that got delivered to the callback. |
| 108 | */ |
| 109 | ssize_t CC_PRINTFLIKE(3, 4) |
| 110 | asn__format_to_callback( |
| 111 | int (*callback)(const void *, size_t, void *key), void *key, |
| 112 | const char *fmt, ...); |
| 113 | |
| 114 | /* |
| 115 | * Invoke the application-supplied callback and fail, if something is wrong. |
| 116 | */ |
| 117 | #define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0) |
| 118 | #define ASN__E_CALLBACK(size, foo) \ |
| 119 | do { \ |
| 120 | if(foo) goto cb_failed; \ |
| 121 | er.encoded += (size); \ |
| 122 | } while(0) |
| 123 | #define ASN__CALLBACK(buf, size) ASN__E_CALLBACK(size, ASN__E_cbc(buf, size)) |
| 124 | #define ASN__CALLBACK2(buf1, size1, buf2, size2) \ |
| 125 | ASN__E_CALLBACK((size1) + (size2), \ |
| 126 | ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2)) |
| 127 | #define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \ |
| 128 | ASN__E_CALLBACK((size1) + (size2) + (size3), \ |
| 129 | ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2) \ |
| 130 | || ASN__E_cbc(buf3, size3)) |
| 131 | |
| 132 | #define ASN__TEXT_INDENT(nl, level) \ |
| 133 | do { \ |
| 134 | int tmp_level = (level); \ |
| 135 | int tmp_nl = ((nl) != 0); \ |
| 136 | int tmp_i; \ |
| 137 | if(tmp_nl) ASN__CALLBACK("\n", 1); \ |
| 138 | if(tmp_level < 0) tmp_level = 0; \ |
| 139 | for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) ASN__CALLBACK(" ", 4); \ |
| 140 | } while(0) |
| 141 | |
| 142 | #define _i_INDENT(nl) do { \ |
| 143 | int tmp_i; \ |
| 144 | if((nl) && cb("\n", 1, app_key) < 0) \ |
| 145 | return -1; \ |
| 146 | for(tmp_i = 0; tmp_i < ilevel; tmp_i++) \ |
| 147 | if(cb(" ", 4, app_key) < 0) \ |
| 148 | return -1; \ |
| 149 | } while(0) |
| 150 | |
| 151 | /* |
| 152 | * Check stack against overflow, if limit is set. |
| 153 | */ |
| 154 | #define ASN__DEFAULT_STACK_MAX (30000) |
| 155 | static int CC_NOTUSED |
| 156 | ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) { |
| 157 | if(ctx && ctx->max_stack_size) { |
| 158 | |
| 159 | /* ctx MUST be allocated on the stack */ |
| 160 | ptrdiff_t usedstack = ((const char *)ctx - (const char *)&ctx); |
| 161 | if(usedstack > 0) usedstack = -usedstack; /* grows up! */ |
| 162 | |
| 163 | /* double negative required to avoid int wrap-around */ |
| 164 | if(usedstack < -(ptrdiff_t)ctx->max_stack_size) { |
| 165 | ASN_DEBUG("Stack limit %ld reached", |
| 166 | (long)ctx->max_stack_size); |
| 167 | return -1; |
| 168 | } |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | #ifdef __cplusplus |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | #endif /* ASN_INTERNAL_H */ |