asn_internal.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2003-2017 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. #ifndef __EXTENSIONS__
  11. #define __EXTENSIONS__ /* for Sun */
  12. #endif
  13. #include "asn_application.h" /* Application-visible API */
  14. #ifndef __NO_ASSERT_H__ /* Include assert.h only for internal use. */
  15. #include <assert.h> /* for assert() macro */
  16. #endif
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* Environment version might be used to avoid running with the old library */
  21. #define ASN1C_ENVIRONMENT_VERSION 923 /* Compile-time version */
  22. int get_asn1c_environment_version(void); /* Run-time version */
  23. #define CALLOC(nmemb, size) calloc(nmemb, size)
  24. #define MALLOC(size) malloc(size)
  25. #define REALLOC(oldptr, size) realloc(oldptr, size)
  26. #define FREEMEM(ptr) free(ptr)
  27. #define asn_debug_indent 0
  28. #define ASN_DEBUG_INDENT_ADD(i) do{}while(0)
  29. #ifdef EMIT_ASN_DEBUG
  30. #warning "Use ASN_EMIT_DEBUG instead of EMIT_ASN_DEBUG"
  31. #define ASN_EMIT_DEBUG EMIT_ASN_DEBUG
  32. #endif
  33. /*
  34. * A macro for debugging the ASN.1 internals.
  35. * You may enable or override it.
  36. */
  37. #ifndef ASN_DEBUG /* If debugging code is not defined elsewhere... */
  38. #if ASN_EMIT_DEBUG == 1 /* And it was asked to emit this code... */
  39. #if __STDC_VERSION__ >= 199901L
  40. #ifdef ASN_THREAD_SAFE
  41. /* Thread safety requires sacrifice in output indentation:
  42. * Retain empty definition of ASN_DEBUG_INDENT_ADD. */
  43. #else /* !ASN_THREAD_SAFE */
  44. #undef ASN_DEBUG_INDENT_ADD
  45. #undef asn_debug_indent
  46. int asn_debug_indent;
  47. #define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)
  48. #endif /* ASN_THREAD_SAFE */
  49. #define ASN_DEBUG(fmt, args...) do { \
  50. int adi = asn_debug_indent; \
  51. while(adi--) fprintf(stderr, " "); \
  52. fprintf(stderr, fmt, ##args); \
  53. fprintf(stderr, " (%s:%d)\n", \
  54. __FILE__, __LINE__); \
  55. } while(0)
  56. #else /* !C99 */
  57. void CC_PRINTFLIKE(1, 2) ASN_DEBUG_f(const char *fmt, ...);
  58. #define ASN_DEBUG ASN_DEBUG_f
  59. #endif /* C99 */
  60. #else /* ASN_EMIT_DEBUG != 1 */
  61. #if __STDC_VERSION__ >= 199901L
  62. #define ASN_DEBUG(...) do{}while(0)
  63. #else /* not C99 */
  64. static void CC_PRINTFLIKE(1, 2) ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
  65. #endif /* C99 or better */
  66. #endif /* ASN_EMIT_DEBUG */
  67. #endif /* ASN_DEBUG */
  68. /*
  69. * Print to a callback.
  70. * The callback is expected to return negative values on error.
  71. * 0 and positive values are treated as success.
  72. * RETURN VALUES:
  73. * -1: Failed to format or invoke the callback.
  74. * >0: Size of the data that got delivered to the callback.
  75. */
  76. ssize_t CC_PRINTFLIKE(3, 4)
  77. asn__format_to_callback(
  78. int (*callback)(const void *, size_t, void *key), void *key,
  79. const char *fmt, ...);
  80. /*
  81. * Invoke the application-supplied callback and fail, if something is wrong.
  82. */
  83. #define ASN__E_cbc(buf, size) (cb((buf), (size), app_key) < 0)
  84. #define ASN__E_CALLBACK(size, foo) \
  85. do { \
  86. if(foo) goto cb_failed; \
  87. er.encoded += (size); \
  88. } while(0)
  89. #define ASN__CALLBACK(buf, size) ASN__E_CALLBACK(size, ASN__E_cbc(buf, size))
  90. #define ASN__CALLBACK2(buf1, size1, buf2, size2) \
  91. ASN__E_CALLBACK((size1) + (size2), \
  92. ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2))
  93. #define ASN__CALLBACK3(buf1, size1, buf2, size2, buf3, size3) \
  94. ASN__E_CALLBACK((size1) + (size2) + (size3), \
  95. ASN__E_cbc(buf1, size1) || ASN__E_cbc(buf2, size2) \
  96. || ASN__E_cbc(buf3, size3))
  97. #define ASN__TEXT_INDENT(nl, level) \
  98. do { \
  99. int tmp_level = (level); \
  100. int tmp_nl = ((nl) != 0); \
  101. int tmp_i; \
  102. if(tmp_nl) ASN__CALLBACK("\n", 1); \
  103. if(tmp_level < 0) tmp_level = 0; \
  104. for(tmp_i = 0; tmp_i < tmp_level; tmp_i++) ASN__CALLBACK(" ", 4); \
  105. } while(0)
  106. #define _i_INDENT(nl) do { \
  107. int tmp_i; \
  108. if((nl) && cb("\n", 1, app_key) < 0) \
  109. return -1; \
  110. for(tmp_i = 0; tmp_i < ilevel; tmp_i++) \
  111. if(cb(" ", 4, app_key) < 0) \
  112. return -1; \
  113. } while(0)
  114. /*
  115. * Check stack against overflow, if limit is set.
  116. */
  117. #define ASN__DEFAULT_STACK_MAX (30000)
  118. static int CC_NOTUSED
  119. ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx) {
  120. if(ctx && ctx->max_stack_size) {
  121. /* ctx MUST be allocated on the stack */
  122. ptrdiff_t usedstack = ((const char *)ctx - (const char *)&ctx);
  123. if(usedstack > 0) usedstack = -usedstack; /* grows up! */
  124. /* double negative required to avoid int wrap-around */
  125. if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
  126. ASN_DEBUG("Stack limit %ld reached",
  127. (long)ctx->max_stack_size);
  128. return -1;
  129. }
  130. }
  131. return 0;
  132. }
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif /* ASN_INTERNAL_H */