asn_codecs.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifndef ASN_CODECS_H
  6. #define ASN_CODECS_H
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. struct asn_TYPE_descriptor_s; /* Forward declaration */
  11. /*
  12. * This structure defines a set of parameters that may be passed
  13. * to every ASN.1 encoder or decoder function.
  14. * WARNING: if max_stack_size member is set, and you are calling the
  15. * function pointers of the asn_TYPE_descriptor_t directly,
  16. * this structure must be ALLOCATED ON THE STACK!
  17. * If you can't always satisfy this requirement, use ber_decode(),
  18. * xer_decode() and uper_decode() functions instead.
  19. */
  20. typedef struct asn_codec_ctx_s {
  21. /*
  22. * Limit the decoder routines to use no (much) more stack than a given
  23. * number of bytes. Most of decoders are stack-based, and this
  24. * would protect against stack overflows if the number of nested
  25. * encodings is high.
  26. * The OCTET STRING, BIT STRING and ANY BER decoders are heap-based,
  27. * and are safe from this kind of overflow.
  28. * A value from getrlimit(RLIMIT_STACK) may be used to initialize
  29. * this variable. Be careful in multithreaded environments, as the
  30. * stack size is rather limited.
  31. */
  32. size_t max_stack_size; /* 0 disables stack bounds checking */
  33. } asn_codec_ctx_t;
  34. /*
  35. * Type of the return value of the encoding functions (der_encode, xer_encode).
  36. */
  37. typedef struct asn_enc_rval_s {
  38. /*
  39. * Number of bytes encoded.
  40. * -1 indicates failure to encode the structure.
  41. * In this case, the members below this one are meaningful.
  42. */
  43. ssize_t encoded;
  44. /*
  45. * Members meaningful when (encoded == -1), for post mortem analysis.
  46. */
  47. /* Type which cannot be encoded */
  48. const struct asn_TYPE_descriptor_s *failed_type;
  49. /* Pointer to the structure of that type */
  50. const void *structure_ptr;
  51. } asn_enc_rval_t;
  52. #define ASN__ENCODE_FAILED do { \
  53. asn_enc_rval_t tmp_error; \
  54. tmp_error.encoded = -1; \
  55. tmp_error.failed_type = td; \
  56. tmp_error.structure_ptr = sptr; \
  57. ASN_DEBUG("Failed to encode element %s", td ? td->name : ""); \
  58. return tmp_error; \
  59. } while(0)
  60. #define ASN__ENCODED_OK(rval) do { \
  61. rval.structure_ptr = 0; \
  62. rval.failed_type = 0; \
  63. return rval; \
  64. } while(0)
  65. /*
  66. * Type of the return value of the decoding functions (ber_decode, xer_decode)
  67. *
  68. * Please note that the number of consumed bytes is ALWAYS meaningful,
  69. * even if code==RC_FAIL. This is to indicate the number of successfully
  70. * decoded bytes, hence providing a possibility to fail with more diagnostics
  71. * (i.e., print the offending remainder of the buffer).
  72. */
  73. enum asn_dec_rval_code_e {
  74. RC_OK, /* Decoded successfully */
  75. RC_WMORE, /* More data expected, call again */
  76. RC_FAIL /* Failure to decode data */
  77. };
  78. typedef struct asn_dec_rval_s {
  79. enum asn_dec_rval_code_e code; /* Result code */
  80. size_t consumed; /* Number of bytes consumed */
  81. } asn_dec_rval_t;
  82. #define ASN__DECODE_FAILED do { \
  83. asn_dec_rval_t tmp_error; \
  84. tmp_error.code = RC_FAIL; \
  85. tmp_error.consumed = 0; \
  86. ASN_DEBUG("Failed to decode element %s", td ? td->name : ""); \
  87. return tmp_error; \
  88. } while(0)
  89. #define ASN__DECODE_STARVED do { \
  90. asn_dec_rval_t tmp_error; \
  91. tmp_error.code = RC_WMORE; \
  92. tmp_error.consumed = 0; \
  93. return tmp_error; \
  94. } while(0)
  95. #ifdef __cplusplus
  96. }
  97. #endif
  98. #endif /* ASN_CODECS_H */