oer_decoder.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 decoder of any type.
  9. */
  10. asn_dec_rval_t
  11. oer_decode(const asn_codec_ctx_t *opt_codec_ctx,
  12. const asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr,
  13. const void *ptr, size_t size) {
  14. asn_codec_ctx_t s_codec_ctx;
  15. /*
  16. * Stack checker requires that the codec context
  17. * must be allocated on the stack.
  18. */
  19. if(opt_codec_ctx) {
  20. if(opt_codec_ctx->max_stack_size) {
  21. s_codec_ctx = *opt_codec_ctx;
  22. opt_codec_ctx = &s_codec_ctx;
  23. }
  24. } else {
  25. /* If context is not given, be security-conscious anyway */
  26. memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
  27. s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
  28. opt_codec_ctx = &s_codec_ctx;
  29. }
  30. /*
  31. * Invoke type-specific decoder.
  32. */
  33. return type_descriptor->op->oer_decoder(opt_codec_ctx, type_descriptor, 0,
  34. struct_ptr, /* Pointer to the destination structure */
  35. ptr, size /* Buffer and its size */
  36. );
  37. }
  38. /*
  39. * Open Type is encoded as a length (#8.6) followed by that number of bytes.
  40. * Since we're just skipping, reading the length would be enough.
  41. */
  42. ssize_t
  43. oer_open_type_skip(const void *bufptr, size_t size) {
  44. size_t len = 0;
  45. return oer_fetch_length(bufptr, size, &len);
  46. }
  47. /*
  48. * Read the Open Type (X.696 (08/2015), #30).
  49. * RETURN VALUES:
  50. * 0: More data expected than bufptr contains.
  51. * -1: Fatal error deciphering length.
  52. * >0: Number of bytes used from bufptr.
  53. */
  54. ssize_t
  55. oer_open_type_get(const asn_codec_ctx_t *opt_codec_ctx,
  56. const struct asn_TYPE_descriptor_s *td,
  57. const asn_oer_constraints_t *constraints, void **struct_ptr,
  58. const void *bufptr, size_t size) {
  59. asn_dec_rval_t dr;
  60. size_t container_len = 0;
  61. ssize_t len_len;
  62. enum asn_struct_free_method dispose_method =
  63. (*struct_ptr) ? ASFM_FREE_UNDERLYING_AND_RESET : ASFM_FREE_EVERYTHING;
  64. /* Get the size of a length determinant */
  65. len_len = oer_fetch_length(bufptr, size, &container_len);
  66. if(len_len <= 0) {
  67. return len_len; /* Error or more data expected */
  68. }
  69. /*
  70. * len_len can't be bigger than size, but size without len_len
  71. * should be bigger or equal to container length
  72. */
  73. if(size - len_len < container_len) {
  74. /* More data is expected */
  75. return 0;
  76. }
  77. dr = td->op->oer_decoder(opt_codec_ctx, td, constraints, struct_ptr,
  78. (const uint8_t *)bufptr + len_len, container_len);
  79. if(dr.code == RC_OK) {
  80. return len_len + container_len;
  81. } else {
  82. /* Even if RC_WMORE, we can't get more data into a closed container. */
  83. td->op->free_struct(td, *struct_ptr, dispose_method);
  84. *struct_ptr = NULL;
  85. return -1;
  86. }
  87. }
  88. asn_dec_rval_t
  89. oer_decode_primitive(const asn_codec_ctx_t *opt_codec_ctx,
  90. const asn_TYPE_descriptor_t *td,
  91. const asn_oer_constraints_t *constraints, void **sptr,
  92. const void *ptr, size_t size) {
  93. ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr;
  94. asn_dec_rval_t rval = {RC_OK, 0};
  95. size_t expected_length = 0;
  96. ssize_t len_len;
  97. (void)td;
  98. (void)opt_codec_ctx;
  99. (void)constraints;
  100. if(!st) {
  101. st = (ASN__PRIMITIVE_TYPE_t *)(*sptr = CALLOC(
  102. 1, sizeof(ASN__PRIMITIVE_TYPE_t)));
  103. if(!st) ASN__DECODE_FAILED;
  104. }
  105. /*
  106. * X.696 (08/2015) #27.2
  107. * Encode length determinant as _number of octets_, but only
  108. * if upper bound is not equal to lower bound.
  109. */
  110. len_len = oer_fetch_length(ptr, size, &expected_length);
  111. if(len_len > 0) {
  112. rval.consumed = len_len;
  113. ptr = (const char *)ptr + len_len;
  114. size -= len_len;
  115. } else if(len_len == 0) {
  116. ASN__DECODE_STARVED;
  117. } else if(len_len < 0) {
  118. ASN__DECODE_FAILED;
  119. }
  120. if(size < expected_length) {
  121. ASN__DECODE_STARVED;
  122. } else {
  123. uint8_t *buf = MALLOC(expected_length + 1);
  124. if(buf == NULL) {
  125. ASN__DECODE_FAILED;
  126. } else {
  127. memcpy(buf, ptr, expected_length);
  128. buf[expected_length] = '\0';
  129. }
  130. FREEMEM(st->buf);
  131. st->buf = buf;
  132. st->size = expected_length;
  133. rval.consumed += expected_length;
  134. return rval;
  135. }
  136. }