ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 1 | /* |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 2 | * Copyright 2019 AT&T Intellectual Property |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 15 | */ |
| 16 | |
nm755n | 2e26814 | 2019-11-28 16:40:23 +0000 | [diff] [blame] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
| 22 | |
ss412g | 1a79bdf | 2019-10-24 12:03:05 +0300 | [diff] [blame] | 23 | |
| 24 | #include <asn_application.h> |
| 25 | #include <asn_internal.h> |
| 26 | #include <per_decoder.h> |
| 27 | |
| 28 | /* |
| 29 | * Decode a "Production of a complete encoding", X.691#10.1. |
| 30 | * The complete encoding contains at least one byte, and is an integral |
| 31 | * multiple of 8 bytes. |
| 32 | */ |
| 33 | asn_dec_rval_t |
| 34 | uper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx, |
| 35 | const asn_TYPE_descriptor_t *td, void **sptr, |
| 36 | const void *buffer, size_t size) { |
| 37 | asn_dec_rval_t rval; |
| 38 | |
| 39 | rval = uper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0); |
| 40 | if(rval.consumed) { |
| 41 | /* |
| 42 | * We've always given 8-aligned data, |
| 43 | * so convert bits to integral bytes. |
| 44 | */ |
| 45 | rval.consumed += 7; |
| 46 | rval.consumed >>= 3; |
| 47 | } else if(rval.code == RC_OK) { |
| 48 | if(size) { |
| 49 | if(((const uint8_t *)buffer)[0] == 0) { |
| 50 | rval.consumed = 1; /* 1 byte */ |
| 51 | } else { |
| 52 | ASN_DEBUG("Expecting single zeroed byte"); |
| 53 | rval.code = RC_FAIL; |
| 54 | } |
| 55 | } else { |
| 56 | /* Must contain at least 8 bits. */ |
| 57 | rval.code = RC_WMORE; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return rval; |
| 62 | } |
| 63 | |
| 64 | asn_dec_rval_t |
| 65 | uper_decode(const asn_codec_ctx_t *opt_codec_ctx, |
| 66 | const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer, |
| 67 | size_t size, int skip_bits, int unused_bits) { |
| 68 | asn_codec_ctx_t s_codec_ctx; |
| 69 | asn_dec_rval_t rval; |
| 70 | asn_per_data_t pd; |
| 71 | |
| 72 | if(skip_bits < 0 || skip_bits > 7 |
| 73 | || unused_bits < 0 || unused_bits > 7 |
| 74 | || (unused_bits > 0 && !size)) |
| 75 | ASN__DECODE_FAILED; |
| 76 | |
| 77 | /* |
| 78 | * Stack checker requires that the codec context |
| 79 | * must be allocated on the stack. |
| 80 | */ |
| 81 | if(opt_codec_ctx) { |
| 82 | if(opt_codec_ctx->max_stack_size) { |
| 83 | s_codec_ctx = *opt_codec_ctx; |
| 84 | opt_codec_ctx = &s_codec_ctx; |
| 85 | } |
| 86 | } else { |
| 87 | /* If context is not given, be security-conscious anyway */ |
| 88 | memset(&s_codec_ctx, 0, sizeof(s_codec_ctx)); |
| 89 | s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX; |
| 90 | opt_codec_ctx = &s_codec_ctx; |
| 91 | } |
| 92 | |
| 93 | /* Fill in the position indicator */ |
| 94 | memset(&pd, 0, sizeof(pd)); |
| 95 | pd.buffer = (const uint8_t *)buffer; |
| 96 | pd.nboff = skip_bits; |
| 97 | pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */ |
| 98 | if(pd.nboff > pd.nbits) |
| 99 | ASN__DECODE_FAILED; |
| 100 | |
| 101 | /* |
| 102 | * Invoke type-specific decoder. |
| 103 | */ |
| 104 | if(!td->op->uper_decoder) |
| 105 | ASN__DECODE_FAILED; /* PER is not compiled in */ |
| 106 | rval = td->op->uper_decoder(opt_codec_ctx, td, 0, sptr, &pd); |
| 107 | if(rval.code == RC_OK) { |
| 108 | /* Return the number of consumed bits */ |
| 109 | rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3) |
| 110 | + pd.nboff - skip_bits; |
| 111 | ASN_DEBUG("PER decoding consumed %ld, counted %ld", |
| 112 | (long)rval.consumed, (long)pd.moved); |
| 113 | assert(rval.consumed == pd.moved); |
| 114 | } else { |
| 115 | /* PER codec is not a restartable */ |
| 116 | rval.consumed = 0; |
| 117 | } |
| 118 | return rval; |
| 119 | } |
| 120 | |
| 121 | asn_dec_rval_t |
| 122 | aper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx, |
| 123 | const asn_TYPE_descriptor_t *td, void **sptr, |
| 124 | const void *buffer, size_t size) { |
| 125 | asn_dec_rval_t rval; |
| 126 | |
| 127 | rval = aper_decode(opt_codec_ctx, td, sptr, buffer, size, 0, 0); |
| 128 | if(rval.consumed) { |
| 129 | /* |
| 130 | * We've always given 8-aligned data, |
| 131 | * so convert bits to integral bytes. |
| 132 | */ |
| 133 | rval.consumed += 7; |
| 134 | rval.consumed >>= 3; |
| 135 | } else if(rval.code == RC_OK) { |
| 136 | if(size) { |
| 137 | if(((const uint8_t *)buffer)[0] == 0) { |
| 138 | rval.consumed = 1; /* 1 byte */ |
| 139 | } else { |
| 140 | ASN_DEBUG("Expecting single zeroed byte"); |
| 141 | rval.code = RC_FAIL; |
| 142 | } |
| 143 | } else { |
| 144 | /* Must contain at least 8 bits. */ |
| 145 | rval.code = RC_WMORE; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return rval; |
| 150 | } |
| 151 | |
| 152 | asn_dec_rval_t |
| 153 | aper_decode(const asn_codec_ctx_t *opt_codec_ctx, |
| 154 | const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer, |
| 155 | size_t size, int skip_bits, int unused_bits) { |
| 156 | asn_codec_ctx_t s_codec_ctx; |
| 157 | asn_dec_rval_t rval; |
| 158 | asn_per_data_t pd; |
| 159 | |
| 160 | if(skip_bits < 0 || skip_bits > 7 |
| 161 | || unused_bits < 0 || unused_bits > 7 |
| 162 | || (unused_bits > 0 && !size)) |
| 163 | ASN__DECODE_FAILED; |
| 164 | |
| 165 | /* |
| 166 | * Stack checker requires that the codec context |
| 167 | * must be allocated on the stack. |
| 168 | */ |
| 169 | if(opt_codec_ctx) { |
| 170 | if(opt_codec_ctx->max_stack_size) { |
| 171 | s_codec_ctx = *opt_codec_ctx; |
| 172 | opt_codec_ctx = &s_codec_ctx; |
| 173 | } |
| 174 | } else { |
| 175 | /* If context is not given, be security-conscious anyway */ |
| 176 | memset(&s_codec_ctx, 0, sizeof(s_codec_ctx)); |
| 177 | s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX; |
| 178 | opt_codec_ctx = &s_codec_ctx; |
| 179 | } |
| 180 | |
| 181 | /* Fill in the position indicator */ |
| 182 | memset(&pd, 0, sizeof(pd)); |
| 183 | pd.buffer = (const uint8_t *)buffer; |
| 184 | pd.nboff = skip_bits; |
| 185 | pd.nbits = 8 * size - unused_bits; /* 8 is CHAR_BIT from <limits.h> */ |
| 186 | if(pd.nboff > pd.nbits) |
| 187 | ASN__DECODE_FAILED; |
| 188 | |
| 189 | /* |
| 190 | * Invoke type-specific decoder. |
| 191 | */ |
| 192 | if(!td->op->aper_decoder) |
| 193 | ASN__DECODE_FAILED; /* PER is not compiled in */ |
| 194 | rval = td->op->aper_decoder(opt_codec_ctx, td, 0, sptr, &pd); |
| 195 | if(rval.code == RC_OK) { |
| 196 | /* Return the number of consumed bits */ |
| 197 | rval.consumed = ((pd.buffer - (const uint8_t *)buffer) << 3) |
| 198 | + pd.nboff - skip_bits; |
| 199 | ASN_DEBUG("PER decoding consumed %zu, counted %zu", |
| 200 | rval.consumed, pd.moved); |
| 201 | assert(rval.consumed == pd.moved); |
| 202 | } else { |
| 203 | /* PER codec is not a restartable */ |
| 204 | rval.consumed = 0; |
| 205 | } |
| 206 | return rval; |
| 207 | } |
| 208 | |