per_decoder.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <asn_application.h>
  2. #include <asn_internal.h>
  3. #include <per_decoder.h>
  4. /*
  5. * Decode a "Production of a complete encoding", X.691#10.1.
  6. * The complete encoding contains at least one byte, and is an integral
  7. * multiple of 8 bytes.
  8. */
  9. asn_dec_rval_t
  10. uper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
  11. const asn_TYPE_descriptor_t *td, void **sptr,
  12. const void *buffer, size_t size) {
  13. asn_dec_rval_t rval;
  14. rval = uper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0);
  15. if(rval.consumed) {
  16. /*
  17. * We've always given 8-aligned data,
  18. * so convert bits to integral bytes.
  19. */
  20. rval.consumed += 7;
  21. rval.consumed >>= 3;
  22. } else if(rval.code == RC_OK) {
  23. if(size) {
  24. if(((const uint8_t *)buffer)[0] == 0) {
  25. rval.consumed = 1; /* 1 byte */
  26. } else {
  27. ASN_DEBUG("Expecting single zeroed byte");
  28. rval.code = RC_FAIL;
  29. }
  30. } else {
  31. /* Must contain at least 8 bits. */
  32. rval.code = RC_WMORE;
  33. }
  34. }
  35. return rval;
  36. }
  37. asn_dec_rval_t
  38. uper_decode(const asn_codec_ctx_t *opt_codec_ctx,
  39. const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
  40. size_t size, int skip_bits, int unused_bits) {
  41. asn_codec_ctx_t s_codec_ctx;
  42. asn_dec_rval_t rval;
  43. asn_per_data_t pd;
  44. if(skip_bits < 0 || skip_bits > 7
  45. || unused_bits < 0 || unused_bits > 7
  46. || (unused_bits > 0 && !size))
  47. ASN__DECODE_FAILED;
  48. /*
  49. * Stack checker requires that the codec context
  50. * must be allocated on the stack.
  51. */
  52. if(opt_codec_ctx) {
  53. if(opt_codec_ctx->max_stack_size) {
  54. s_codec_ctx = *opt_codec_ctx;
  55. opt_codec_ctx = &s_codec_ctx;
  56. }
  57. } else {
  58. /* If context is not given, be security-conscious anyway */
  59. memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
  60. s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
  61. opt_codec_ctx = &s_codec_ctx;
  62. }
  63. /* Fill in the position indicator */
  64. memset(&pd, 0, sizeof(pd));
  65. pd.buffer = (const uint8_t *)buffer;
  66. pd.nboff = skip_bits;
  67. pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */
  68. if(pd.nboff > pd.nbits)
  69. ASN__DECODE_FAILED;
  70. /*
  71. * Invoke type-specific decoder.
  72. */
  73. if(!td->op->uper_decoder)
  74. ASN__DECODE_FAILED; /* PER is not compiled in */
  75. rval = td->op->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd);
  76. if(rval.code == RC_OK) {
  77. /* Return the number of consumed bits */
  78. rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3)
  79. + pd.nboff - skip_bits;
  80. ASN_DEBUG("PER decoding consumed %ld, counted %ld",
  81. (long)rval.consumed, (long)pd.moved);
  82. assert(rval.consumed == pd.moved);
  83. } else {
  84. /* PER codec is not a restartable */
  85. rval.consumed = 0;
  86. }
  87. return rval;
  88. }