Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1 | #include <asn_application.h> |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 2 | #include <asn_internal.h> |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 3 | #include <per_decoder.h> |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 4 | |
Lev Walkin | 08b30bb | 2007-06-26 08:24:50 +0000 | [diff] [blame] | 5 | /* |
| 6 | * Decode a "Production of a complete encoding", X.691#10.1. |
| 7 | * The complete encoding contains at least one byte, and is an integral |
| 8 | * multiple of 8 bytes. |
| 9 | */ |
| 10 | asn_dec_rval_t |
| 11 | uper_decode_complete(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, void **sptr, const void *buffer, size_t size) { |
| 12 | asn_dec_rval_t rval; |
| 13 | |
| 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) { |
Lev Walkin | fe1ffaf | 2010-10-25 21:07:59 -0700 | [diff] [blame] | 24 | if(((const uint8_t *)buffer)[0] == 0) { |
Lev Walkin | 08b30bb | 2007-06-26 08:24:50 +0000 | [diff] [blame] | 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 | |
| 36 | return rval; |
| 37 | } |
| 38 | |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 39 | asn_dec_rval_t |
Harald Welte | 498c971 | 2015-08-30 16:33:07 +0200 | [diff] [blame^] | 40 | aper_decode_complete(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, void **sptr, const void *buffer, size_t size) { |
| 41 | asn_dec_rval_t rval; |
| 42 | |
| 43 | rval = aper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0); |
| 44 | if(rval.consumed) { |
| 45 | /* |
| 46 | * We've always given 8-aligned data, |
| 47 | * so convert bits to integral bytes. |
| 48 | */ |
| 49 | rval.consumed += 7; |
| 50 | rval.consumed >>= 3; |
| 51 | } else if(rval.code == RC_OK) { |
| 52 | if(size) { |
| 53 | if(((uint8_t *)buffer)[0] == 0) { |
| 54 | rval.consumed = 1; /* 1 byte */ |
| 55 | } else { |
| 56 | ASN_DEBUG("Expecting single zeroed byte"); |
| 57 | rval.code = RC_FAIL; |
| 58 | } |
| 59 | } else { |
| 60 | /* Must contain at least 8 bits. */ |
| 61 | rval.code = RC_WMORE; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return rval; |
| 66 | } |
| 67 | |
| 68 | asn_dec_rval_t |
Lev Walkin | 0a8aa60 | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 69 | uper_decode(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, void **sptr, const void *buffer, size_t size, int skip_bits, int unused_bits) { |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 70 | asn_codec_ctx_t s_codec_ctx; |
Lev Walkin | 21f9277 | 2006-09-17 11:31:55 +0000 | [diff] [blame] | 71 | asn_dec_rval_t rval; |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 72 | asn_per_data_t pd; |
| 73 | |
Lev Walkin | 0a8aa60 | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 74 | if(skip_bits < 0 || skip_bits > 7 |
| 75 | || unused_bits < 0 || unused_bits > 7 |
| 76 | || (unused_bits > 0 && !size)) |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 77 | _ASN_DECODE_FAILED; |
| 78 | |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 79 | /* |
| 80 | * Stack checker requires that the codec context |
| 81 | * must be allocated on the stack. |
| 82 | */ |
| 83 | if(opt_codec_ctx) { |
| 84 | if(opt_codec_ctx->max_stack_size) { |
| 85 | s_codec_ctx = *opt_codec_ctx; |
| 86 | opt_codec_ctx = &s_codec_ctx; |
| 87 | } |
| 88 | } else { |
| 89 | /* If context is not given, be security-conscious anyway */ |
| 90 | memset(&s_codec_ctx, 0, sizeof(s_codec_ctx)); |
| 91 | s_codec_ctx.max_stack_size = _ASN_DEFAULT_STACK_MAX; |
| 92 | opt_codec_ctx = &s_codec_ctx; |
| 93 | } |
| 94 | |
| 95 | /* Fill in the position indicator */ |
Lev Walkin | 5b78e1c | 2007-06-24 06:26:47 +0000 | [diff] [blame] | 96 | memset(&pd, 0, sizeof(pd)); |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 97 | pd.buffer = (const uint8_t *)buffer; |
Lev Walkin | bc69177 | 2006-09-17 11:02:53 +0000 | [diff] [blame] | 98 | pd.nboff = skip_bits; |
Lev Walkin | 0a8aa60 | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 99 | pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */ |
| 100 | if(pd.nboff > pd.nbits) |
| 101 | _ASN_DECODE_FAILED; |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Invoke type-specific decoder. |
| 105 | */ |
| 106 | if(!td->uper_decoder) |
| 107 | _ASN_DECODE_FAILED; /* PER is not compiled in */ |
Lev Walkin | 21f9277 | 2006-09-17 11:31:55 +0000 | [diff] [blame] | 108 | rval = td->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd); |
Lev Walkin | 0a8aa60 | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 109 | if(rval.code == RC_OK) { |
Lev Walkin | 21f9277 | 2006-09-17 11:31:55 +0000 | [diff] [blame] | 110 | /* Return the number of consumed bits */ |
| 111 | rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3) |
| 112 | + pd.nboff - skip_bits; |
Lev Walkin | fe1ffaf | 2010-10-25 21:07:59 -0700 | [diff] [blame] | 113 | ASN_DEBUG("PER decoding consumed %ld, counted %ld", |
| 114 | (long)rval.consumed, (long)pd.moved); |
Lev Walkin | d00657f | 2007-06-26 02:51:10 +0000 | [diff] [blame] | 115 | assert(rval.consumed == pd.moved); |
Lev Walkin | 0a8aa60 | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 116 | } else { |
| 117 | /* PER codec is not a restartable */ |
| 118 | rval.consumed = 0; |
Lev Walkin | 21f9277 | 2006-09-17 11:31:55 +0000 | [diff] [blame] | 119 | } |
| 120 | return rval; |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Harald Welte | 498c971 | 2015-08-30 16:33:07 +0200 | [diff] [blame^] | 123 | asn_dec_rval_t |
| 124 | aper_decode(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, void **sptr, const void *buffer, size_t size, int skip_bits, int unused_bits) { |
| 125 | asn_codec_ctx_t s_codec_ctx; |
| 126 | asn_dec_rval_t rval; |
| 127 | asn_per_data_t pd; |
| 128 | |
| 129 | if(skip_bits < 0 || skip_bits > 7 |
| 130 | || unused_bits < 0 || unused_bits > 7 |
| 131 | || (unused_bits > 0 && !size)) |
| 132 | _ASN_DECODE_FAILED; |
| 133 | |
| 134 | /* |
| 135 | * Stack checker requires that the codec context |
| 136 | * must be allocated on the stack. |
| 137 | */ |
| 138 | if(opt_codec_ctx) { |
| 139 | if(opt_codec_ctx->max_stack_size) { |
| 140 | s_codec_ctx = *opt_codec_ctx; |
| 141 | opt_codec_ctx = &s_codec_ctx; |
| 142 | } |
| 143 | } else { |
| 144 | /* If context is not given, be security-conscious anyway */ |
| 145 | memset(&s_codec_ctx, 0, sizeof(s_codec_ctx)); |
| 146 | s_codec_ctx.max_stack_size = _ASN_DEFAULT_STACK_MAX; |
| 147 | opt_codec_ctx = &s_codec_ctx; |
| 148 | } |
| 149 | |
| 150 | /* Fill in the position indicator */ |
| 151 | memset(&pd, 0, sizeof(pd)); |
| 152 | pd.buffer = (const uint8_t *)buffer; |
| 153 | pd.nboff = skip_bits; |
| 154 | pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */ |
| 155 | if(pd.nboff > pd.nbits) |
| 156 | _ASN_DECODE_FAILED; |
| 157 | |
| 158 | /* |
| 159 | * Invoke type-specific decoder. |
| 160 | */ |
| 161 | if(!td->aper_decoder) |
| 162 | _ASN_DECODE_FAILED; /* PER is not compiled in */ |
| 163 | rval = td->aper_decoder(opt_codec_ctx, td, 0, sptr, &pd); |
| 164 | if(rval.code == RC_OK) { |
| 165 | /* Return the number of consumed bits */ |
| 166 | rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3) |
| 167 | + pd.nboff - skip_bits; |
| 168 | ASN_DEBUG("PER decoding consumed %d, counted %d", |
| 169 | rval.consumed, pd.moved); |
| 170 | assert(rval.consumed == pd.moved); |
| 171 | } else { |
| 172 | /* PER codec is not a restartable */ |
| 173 | rval.consumed = 0; |
| 174 | } |
| 175 | return rval; |
| 176 | } |