asn_application.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*-
  2. * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
  3. * Redistribution and modifications are permitted subject to BSD license.
  4. */
  5. /*
  6. * Application-level ASN.1 callbacks.
  7. */
  8. #ifndef ASN_APPLICATION_H
  9. #define ASN_APPLICATION_H
  10. #include "asn_system.h" /* for platform-dependent types */
  11. #include "asn_codecs.h" /* for ASN.1 codecs specifics */
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /*
  16. * A selection of ASN.1 Transfer Syntaxes to use with generalized
  17. * encoders and decoders declared further in this .h file.
  18. */
  19. enum asn_transfer_syntax {
  20. /* Avoid appearance of a default transfer syntax. */
  21. ATS_INVALID = 0,
  22. /* Plaintext output (not conforming to any standard), for debugging. */
  23. ATS_NONSTANDARD_PLAINTEXT,
  24. /* Returns a randomly generatede structure. */
  25. ATS_RANDOM,
  26. /*
  27. * X.690:
  28. * BER: Basic Encoding Rules.
  29. * DER: Distinguished Encoding Rules.
  30. * CER: Canonical Encoding Rules.
  31. * DER and CER are more strict variants of BER.
  32. */
  33. ATS_BER,
  34. ATS_DER,
  35. ATS_CER, /* Only decoding is supported */
  36. /*
  37. * X.696:
  38. * OER: Octet Encoding Rules.
  39. * CANONICAL-OER is a more strict variant of BASIC-OER.
  40. */
  41. ATS_BASIC_OER,
  42. ATS_CANONICAL_OER,
  43. /*
  44. * X.691:
  45. * PER: Packed Encoding Rules.
  46. * CANONICAL-PER is a more strict variant of BASIC-PER.
  47. * NOTE: Produces or consumes a complete encoding (X.691 (08/2015) #11.1).
  48. */
  49. ATS_UNALIGNED_BASIC_PER,
  50. ATS_UNALIGNED_CANONICAL_PER,
  51. /*
  52. * X.693:
  53. * XER: XML Encoding Rules.
  54. * CANONICAL-XER is a more strict variant of BASIC-XER.
  55. */
  56. ATS_BASIC_XER,
  57. ATS_CANONICAL_XER
  58. };
  59. /*
  60. * A generic encoder for any supported transfer syntax.
  61. * RETURN VALUES:
  62. * The (.encoded) field of the return value is REDEFINED to mean the following:
  63. * >=0: The computed size of the encoded data. Can exceed the (buffer_size).
  64. * -1: Error encoding the structure. See the error code in (errno):
  65. * EINVAL: Incorrect parameters to the function, such as NULLs.
  66. * ENOENT: Encoding transfer syntax is not defined (for this type).
  67. * EBADF: The structure has invalid form or content constraint failed.
  68. * The (.failed_type) and (.structure_ptr) MIGHT be set to the appropriate
  69. * values at the place of failure, if at all possible.
  70. * WARNING: The (.encoded) field of the return value can exceed the buffer_size.
  71. * This is similar to snprintf(3) contract which might return values
  72. * greater than the buffer size.
  73. */
  74. asn_enc_rval_t asn_encode_to_buffer(
  75. const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */
  76. enum asn_transfer_syntax,
  77. const struct asn_TYPE_descriptor_s *type_to_encode,
  78. const void *structure_to_encode, void *buffer, size_t buffer_size);
  79. /*
  80. * A variant of asn_encode_to_buffer() with automatically allocated buffer.
  81. * RETURN VALUES:
  82. * On success, returns a newly allocated (.buffer) containing the whole message.
  83. * The message size is returned in (.result.encoded).
  84. * On failure:
  85. * (.buffer) is NULL,
  86. * (.result.encoded) as in asn_encode_to_buffer(),
  87. * The errno codes as in asn_encode_to_buffer(), plus the following:
  88. * ENOMEM: Memory allocation failed due to system or internal limits.
  89. * The user is responsible for freeing the (.buffer).
  90. */
  91. typedef struct asn_encode_to_new_buffer_result_s {
  92. void *buffer; /* NULL if failed to encode. */
  93. asn_enc_rval_t result;
  94. } asn_encode_to_new_buffer_result_t;
  95. asn_encode_to_new_buffer_result_t asn_encode_to_new_buffer(
  96. const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */
  97. enum asn_transfer_syntax,
  98. const struct asn_TYPE_descriptor_s *type_to_encode,
  99. const void *structure_to_encode);
  100. /*
  101. * Generic type of an application-defined callback to return various
  102. * types of data to the application.
  103. * EXPECTED RETURN VALUES:
  104. * -1: Failed to consume bytes. Abort the mission.
  105. * Non-negative return values indicate success, and ignored.
  106. */
  107. typedef int(asn_app_consume_bytes_f)(const void *buffer, size_t size,
  108. void *application_specific_key);
  109. /*
  110. * A generic encoder for any supported transfer syntax.
  111. * Returns the comprehensive encoding result descriptor (see asn_codecs.h).
  112. * RETURN VALUES:
  113. * The negative (.encoded) field of the return values is accompanied with the
  114. * following error codes (errno):
  115. * EINVAL: Incorrect parameters to the function, such as NULLs.
  116. * ENOENT: Encoding transfer syntax is not defined (for this type).
  117. * EBADF: The structure has invalid form or content constraint failed.
  118. * EIO: The (callback) has returned negative value during encoding.
  119. */
  120. asn_enc_rval_t asn_encode(
  121. const asn_codec_ctx_t *opt_codec_parameters, /* See asn_codecs.h */
  122. enum asn_transfer_syntax,
  123. const struct asn_TYPE_descriptor_s *type_to_encode,
  124. const void *structure_to_encode,
  125. asn_app_consume_bytes_f *callback, void *callback_key);
  126. /*
  127. * A generic decoder for any supported transfer syntax.
  128. */
  129. asn_dec_rval_t asn_decode(
  130. const asn_codec_ctx_t *opt_codec_parameters, enum asn_transfer_syntax,
  131. const struct asn_TYPE_descriptor_s *type_to_decode,
  132. void **structure_ptr, /* Pointer to a target structure's pointer */
  133. const void *buffer, /* Data to be decoded */
  134. size_t size /* Size of that buffer */
  135. );
  136. /*
  137. * A callback of this type is called whenever constraint validation fails
  138. * on some ASN.1 type. See "constraints.h" for more details on constraint
  139. * validation.
  140. * This callback specifies a descriptor of the ASN.1 type which failed
  141. * the constraint check, as well as human readable message on what
  142. * particular constraint has failed.
  143. */
  144. typedef void (asn_app_constraint_failed_f)(void *application_specific_key,
  145. const struct asn_TYPE_descriptor_s *type_descriptor_which_failed,
  146. const void *structure_which_failed_ptr,
  147. const char *error_message_format, ...) CC_PRINTFLIKE(4, 5);
  148. #ifdef __cplusplus
  149. }
  150. #endif
  151. #include "constr_TYPE.h" /* for asn_TYPE_descriptor_t */
  152. #endif /* ASN_APPLICATION_H */