oer_encoder.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
  3. * Redistribution and modifications are permitted subject to BSD license.
  4. */
  5. #include <asn_internal.h>
  6. #include <asn_codecs_prim.h>
  7. /*
  8. * The OER encoder of any type.
  9. */
  10. asn_enc_rval_t
  11. oer_encode(const asn_TYPE_descriptor_t *type_descriptor, const void *struct_ptr,
  12. asn_app_consume_bytes_f *consume_bytes, void *app_key) {
  13. ASN_DEBUG("OER encoder invoked for %s", type_descriptor->name);
  14. /*
  15. * Invoke type-specific encoder.
  16. */
  17. return type_descriptor->op->oer_encoder(
  18. type_descriptor, 0,
  19. struct_ptr, /* Pointer to the destination structure */
  20. consume_bytes, app_key);
  21. }
  22. /*
  23. * Argument type and callback necessary for oer_encode_to_buffer().
  24. */
  25. typedef struct enc_to_buf_arg {
  26. void *buffer;
  27. size_t left;
  28. } enc_to_buf_arg;
  29. static int
  30. encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
  31. enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
  32. if(arg->left < size) return -1; /* Data exceeds the available buffer size */
  33. memcpy(arg->buffer, buffer, size);
  34. arg->buffer = ((char *)arg->buffer) + size;
  35. arg->left -= size;
  36. return 0;
  37. }
  38. /*
  39. * A variant of the oer_encode() which encodes the data into the provided buffer
  40. */
  41. asn_enc_rval_t
  42. oer_encode_to_buffer(const asn_TYPE_descriptor_t *type_descriptor,
  43. const asn_oer_constraints_t *constraints,
  44. const void *struct_ptr, /* Structure to be encoded */
  45. void *buffer, /* Pre-allocated buffer */
  46. size_t buffer_size /* Initial buffer size (maximum) */
  47. ) {
  48. enc_to_buf_arg arg;
  49. asn_enc_rval_t ec;
  50. arg.buffer = buffer;
  51. arg.left = buffer_size;
  52. if(type_descriptor->op->oer_encoder == NULL) {
  53. ec.encoded = -1;
  54. ec.failed_type = type_descriptor;
  55. ec.structure_ptr = struct_ptr;
  56. ASN_DEBUG("OER encoder is not defined for %s",
  57. type_descriptor->name);
  58. } else {
  59. ec = type_descriptor->op->oer_encoder(
  60. type_descriptor, constraints,
  61. struct_ptr, /* Pointer to the destination structure */
  62. encode_to_buffer_cb, &arg);
  63. if(ec.encoded != -1) {
  64. assert(ec.encoded == (ssize_t)(buffer_size - arg.left));
  65. /* Return the encoded contents size */
  66. }
  67. }
  68. return ec;
  69. }
  70. asn_enc_rval_t
  71. oer_encode_primitive(const asn_TYPE_descriptor_t *td,
  72. const asn_oer_constraints_t *constraints, const void *sptr,
  73. asn_app_consume_bytes_f *cb, void *app_key) {
  74. const ASN__PRIMITIVE_TYPE_t *st = (const ASN__PRIMITIVE_TYPE_t *)sptr;
  75. asn_enc_rval_t er = {0, 0, 0};
  76. ssize_t ret;
  77. (void)constraints;
  78. if(!st) ASN__ENCODE_FAILED;
  79. ASN_DEBUG("Encoding %s (%" ASN_PRI_SIZE " bytes)", td ? td->name : "", st->size);
  80. /*
  81. * X.696 (08/2015) #27.2
  82. */
  83. ret = oer_serialize_length(st->size, cb, app_key);
  84. if(ret < 0) {
  85. ASN__ENCODE_FAILED;
  86. }
  87. er.encoded += ret;
  88. er.encoded += st->size;
  89. if(cb(st->buf, st->size, app_key) < 0) {
  90. ASN__ENCODE_FAILED;
  91. } else {
  92. ASN__ENCODED_OK(er);
  93. }
  94. }
  95. static int
  96. oer__count_bytes(const void *buffer, size_t size, void *bytes_ptr) {
  97. size_t *bytes = bytes_ptr;
  98. (void)buffer;
  99. *bytes += size;
  100. return 0;
  101. }
  102. ssize_t
  103. oer_open_type_put(const asn_TYPE_descriptor_t *td,
  104. const asn_oer_constraints_t *constraints, const void *sptr,
  105. asn_app_consume_bytes_f *cb, void *app_key) {
  106. size_t serialized_byte_count = 0;
  107. asn_enc_rval_t er;
  108. ssize_t len_len;
  109. er = td->op->oer_encoder(td, constraints, sptr, oer__count_bytes,
  110. &serialized_byte_count);
  111. if(er.encoded < 0) return -1;
  112. assert(serialized_byte_count == (size_t)er.encoded);
  113. len_len = oer_serialize_length(serialized_byte_count, cb, app_key);
  114. if(len_len == -1) return -1;
  115. er = td->op->oer_encoder(td, constraints, sptr, cb, app_key);
  116. if(er.encoded < 0) return -1;
  117. assert(serialized_byte_count == (size_t)er.encoded);
  118. return len_len + er.encoded;
  119. }