OCTET_STRING_oer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
  3. * All rights reserved.
  4. * Redistribution and modifications are permitted subject to BSD license.
  5. */
  6. #ifndef ASN_DISABLE_OER_SUPPORT
  7. #include <asn_internal.h>
  8. #include <OCTET_STRING.h>
  9. #include <errno.h>
  10. asn_dec_rval_t
  11. OCTET_STRING_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
  12. const asn_TYPE_descriptor_t *td,
  13. const asn_oer_constraints_t *constraints, void **sptr,
  14. const void *ptr, size_t size) {
  15. const asn_OCTET_STRING_specifics_t *specs =
  16. td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  17. : &asn_SPC_OCTET_STRING_specs;
  18. OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
  19. const asn_oer_constraints_t *cts =
  20. constraints ? constraints : td->encoding_constraints.oer_constraints;
  21. ssize_t ct_size = cts ? cts->size : -1;
  22. asn_dec_rval_t rval = {RC_OK, 0};
  23. size_t expected_length = 0;
  24. size_t unit_bytes;
  25. switch(specs->subvariant) {
  26. default:
  27. case ASN_OSUBV_BIT:
  28. ASN_DEBUG("Invalid use of OCTET STRING to decode BIT STRING");
  29. ASN__DECODE_FAILED;
  30. case ASN_OSUBV_ANY:
  31. /* Fall through */
  32. case ASN_OSUBV_STR:
  33. unit_bytes = 1;
  34. break;
  35. case ASN_OSUBV_U16:
  36. unit_bytes = 2;
  37. break;
  38. case ASN_OSUBV_U32:
  39. unit_bytes = 4;
  40. break;
  41. }
  42. (void)opt_codec_ctx;
  43. if(!st) {
  44. st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
  45. if(!st) ASN__DECODE_FAILED;
  46. }
  47. if(ct_size >= 0) {
  48. expected_length = unit_bytes * ct_size;
  49. } else {
  50. /*
  51. * X.696 (08/2015) #27.2
  52. * Encode length determinant as _number of octets_, but only
  53. * if upper bound is not equal to lower bound.
  54. */
  55. ssize_t len_len = oer_fetch_length(ptr, size, &expected_length);
  56. if(len_len > 0) {
  57. rval.consumed = len_len;
  58. ptr = (const char *)ptr + len_len;
  59. size -= len_len;
  60. } else if(len_len == 0) {
  61. ASN__DECODE_STARVED;
  62. } else if(len_len < 0) {
  63. ASN__DECODE_FAILED;
  64. }
  65. if(expected_length % unit_bytes != 0) {
  66. ASN_DEBUG(
  67. "Data size %" ASN_PRI_SIZE " bytes is not consistent with multiplier %" ASN_PRI_SIZE "",
  68. expected_length, unit_bytes);
  69. ASN__DECODE_FAILED;
  70. }
  71. }
  72. if(size < expected_length) {
  73. ASN__DECODE_STARVED;
  74. } else {
  75. uint8_t *buf = MALLOC(expected_length + 1);
  76. if(buf == NULL) {
  77. ASN__DECODE_FAILED;
  78. } else {
  79. memcpy(buf, ptr, expected_length);
  80. buf[expected_length] = '\0';
  81. }
  82. FREEMEM(st->buf);
  83. st->buf = buf;
  84. st->size = expected_length;
  85. rval.consumed += expected_length;
  86. return rval;
  87. }
  88. }
  89. /*
  90. * Encode as Canonical OER.
  91. */
  92. asn_enc_rval_t
  93. OCTET_STRING_encode_oer(const asn_TYPE_descriptor_t *td,
  94. const asn_oer_constraints_t *constraints,
  95. const void *sptr, asn_app_consume_bytes_f *cb,
  96. void *app_key) {
  97. const asn_OCTET_STRING_specifics_t *specs =
  98. td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  99. : &asn_SPC_OCTET_STRING_specs;
  100. const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
  101. const asn_oer_constraints_t *cts =
  102. constraints ? constraints : td->encoding_constraints.oer_constraints;
  103. ssize_t ct_size = cts ? cts->size : -1;
  104. asn_enc_rval_t er = {0, 0, 0};
  105. if(!st) ASN__ENCODE_FAILED;
  106. ASN_DEBUG("Encoding %s %" ASN_PRI_SIZE " as OCTET STRING", td ? td->name : "", st->size);
  107. if(ct_size >= 0) {
  108. /*
  109. * Check that available data matches the constraint
  110. */
  111. size_t unit_bytes;
  112. switch(specs->subvariant) {
  113. default:
  114. case ASN_OSUBV_BIT:
  115. ASN_DEBUG("Invalid use of OCTET STRING to encode BIT STRING");
  116. ASN__ENCODE_FAILED;
  117. case ASN_OSUBV_ANY:
  118. /* Fall through */
  119. case ASN_OSUBV_STR:
  120. unit_bytes = 1;
  121. break;
  122. case ASN_OSUBV_U16:
  123. unit_bytes = 2;
  124. break;
  125. case ASN_OSUBV_U32:
  126. unit_bytes = 4;
  127. break;
  128. }
  129. if(st->size != unit_bytes * (size_t)ct_size) {
  130. ASN_DEBUG(
  131. "Trying to encode %s (%" ASN_PRI_SIZE " bytes) which doesn't fit SIZE "
  132. "constraint (%" ASN_PRI_SIZE ")",
  133. td->name, st->size, ct_size);
  134. ASN__ENCODE_FAILED;
  135. }
  136. } else {
  137. /*
  138. * X.696 (08/2015) #27.2
  139. * Encode length determinant as _number of octets_, but only
  140. * if upper bound is not equal to lower bound.
  141. */
  142. ssize_t ret = oer_serialize_length(st->size, cb, app_key);
  143. if(ret < 0) {
  144. ASN__ENCODE_FAILED;
  145. }
  146. er.encoded += ret;
  147. }
  148. er.encoded += st->size;
  149. if(cb(st->buf, st->size, app_key) < 0) {
  150. ASN__ENCODE_FAILED;
  151. } else {
  152. ASN__ENCODED_OK(er);
  153. }
  154. }
  155. #endif /* ASN_DISABLE_OER_SUPPORT */