Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 99414dc | 2004-09-29 13:21:23 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <assert.h> |
| 7 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 8 | #undef ADVANCE |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 9 | #define ADVANCE(num_bytes) do { \ |
| 10 | size_t num = num_bytes; \ |
| 11 | ptr = ((char *)ptr) + num; \ |
| 12 | size -= num; \ |
| 13 | consumed_myself += num; \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | } while(0) |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 15 | #undef RETURN |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 16 | #define RETURN(_code) do { \ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 17 | asn_dec_rval_t rval; \ |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 18 | rval.code = _code; \ |
| 19 | if(opt_ctx) opt_ctx->step = step; /* Save context */ \ |
| 20 | if(_code == RC_OK || opt_ctx) \ |
| 21 | rval.consumed = consumed_myself; \ |
| 22 | else \ |
| 23 | rval.consumed = 0; /* Context-free */ \ |
| 24 | return rval; \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | } while(0) |
| 26 | |
| 27 | /* |
| 28 | * The BER decoder of any type. |
| 29 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 30 | asn_dec_rval_t |
Lev Walkin | 99414dc | 2004-09-29 13:21:23 +0000 | [diff] [blame] | 31 | ber_decode(asn_codec_ctx_t *opt_codec_ctx, |
| 32 | asn_TYPE_descriptor_t *type_descriptor, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 33 | void **struct_ptr, void *ptr, size_t size) { |
Lev Walkin | 99414dc | 2004-09-29 13:21:23 +0000 | [diff] [blame] | 34 | asn_codec_ctx_t s_codec_ctx; |
| 35 | |
| 36 | /* |
| 37 | * Satisfy the requirement that the codec context |
| 38 | * must be allocated on the stack. |
| 39 | */ |
| 40 | if(opt_codec_ctx && opt_codec_ctx->max_stack_size) { |
| 41 | s_codec_ctx = *opt_codec_ctx; |
| 42 | opt_codec_ctx = &s_codec_ctx; |
| 43 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * Invoke type-specific decoder. |
| 47 | */ |
Lev Walkin | 99414dc | 2004-09-29 13:21:23 +0000 | [diff] [blame] | 48 | return type_descriptor->ber_decoder(opt_codec_ctx, type_descriptor, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | struct_ptr, /* Pointer to the destination structure */ |
| 50 | ptr, size, /* Buffer and its size */ |
| 51 | 0 /* Default tag mode is 0 */ |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Check the set of <TL<TL<TL...>>> tags matches the definition. |
| 57 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 58 | asn_dec_rval_t |
Lev Walkin | 99414dc | 2004-09-29 13:21:23 +0000 | [diff] [blame] | 59 | ber_check_tags(asn_codec_ctx_t *opt_codec_ctx, |
| 60 | asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 61 | void *ptr, size_t size, int tag_mode, int last_tag_form, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 62 | ber_tlv_len_t *last_length, int *opt_tlv_form) { |
| 63 | ssize_t consumed_myself = 0; |
| 64 | ssize_t tag_len; |
| 65 | ssize_t len_len; |
| 66 | ber_tlv_tag_t tlv_tag; |
| 67 | ber_tlv_len_t tlv_len; |
| 68 | ber_tlv_len_t limit_len = -1; |
| 69 | int expect_00_terminators = 0; |
| 70 | int tlv_constr = -1; /* If CHOICE, opt_tlv_form is not given */ |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 71 | int step = opt_ctx ? opt_ctx->step : 0; /* Where we left previously */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | int tagno; |
| 73 | |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 74 | /* |
| 75 | * Make sure we didn't exceed the maximum stack size. |
| 76 | */ |
Lev Walkin | 99414dc | 2004-09-29 13:21:23 +0000 | [diff] [blame] | 77 | if(opt_codec_ctx && opt_codec_ctx->max_stack_size) { |
| 78 | ptrdiff_t usedstack = ((char *)opt_codec_ctx - (char *)&size); |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 79 | /* double negative is required to avoid int wrap-around */ |
Lev Walkin | 99414dc | 2004-09-29 13:21:23 +0000 | [diff] [blame] | 80 | if(usedstack > 0) usedstack = -usedstack; |
| 81 | ASN_DEBUG("Current stack size %ld", -(long)usedstack); |
| 82 | if(usedstack < -(ptrdiff_t)opt_codec_ctx->max_stack_size) { |
| 83 | ASN_DEBUG("Stack limit %ld reached", |
| 84 | (long)opt_codec_ctx->max_stack_size); |
| 85 | RETURN(RC_FAIL); |
| 86 | } |
| 87 | } |
| 88 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 89 | /* |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 90 | * So what does all this implicit skip stuff mean? |
Lev Walkin | 26e2222 | 2004-06-06 07:59:35 +0000 | [diff] [blame] | 91 | * Imagine two types, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 92 | * A ::= [5] IMPLICIT T |
| 93 | * B ::= [2] EXPLICIT T |
| 94 | * Where T is defined as |
| 95 | * T ::= [4] IMPLICIT SEQUENCE { ... } |
| 96 | * |
| 97 | * Let's say, we are starting to decode type A, given the |
| 98 | * following TLV stream: <5> <0>. What does this mean? |
| 99 | * It means that the type A contains type T which is, |
| 100 | * in turn, empty. |
| 101 | * Remember though, that we are still in A. We cannot |
| 102 | * just pass control to the type T decoder. Why? Because |
| 103 | * the type T decoder expects <4> <0>, not <5> <0>. |
| 104 | * So, we must make sure we are going to receive <5> while |
| 105 | * still in A, then pass control to the T decoder, indicating |
| 106 | * that the tag <4> was implicitly skipped. The decoder of T |
| 107 | * hence will be prepared to treat <4> as valid tag, and decode |
| 108 | * it appropriately. |
| 109 | */ |
| 110 | |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 111 | tagno = step /* Continuing where left previously */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | + (tag_mode==1?-1:0) |
| 113 | ; |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 114 | ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)", |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 115 | td->name, (long)size, tag_mode, step, tagno); |
Lev Walkin | 99414dc | 2004-09-29 13:21:23 +0000 | [diff] [blame] | 116 | /* assert(td->tags_count >= 1) May not be the case for CHOICE or ANY */ |
Lev Walkin | 7210fdb | 2004-09-04 04:44:30 +0000 | [diff] [blame] | 117 | |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 118 | if(tag_mode == 0 && tagno == td->tags_count) { |
Lev Walkin | 7210fdb | 2004-09-04 04:44:30 +0000 | [diff] [blame] | 119 | /* |
| 120 | * This must be the _untagged_ ANY type, |
| 121 | * which outermost tag isn't known in advance. |
| 122 | * Fetch the tag and length separately. |
| 123 | */ |
| 124 | tag_len = ber_fetch_tag(ptr, size, &tlv_tag); |
| 125 | switch(tag_len) { |
| 126 | case -1: RETURN(RC_FAIL); |
| 127 | case 0: RETURN(RC_WMORE); |
| 128 | } |
| 129 | tlv_constr = BER_TLV_CONSTRUCTED(ptr); |
| 130 | len_len = ber_fetch_length(tlv_constr, |
| 131 | (char *)ptr + tag_len, size - tag_len, &tlv_len); |
| 132 | switch(len_len) { |
| 133 | case -1: RETURN(RC_FAIL); |
| 134 | case 0: RETURN(RC_WMORE); |
| 135 | } |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 136 | ASN_DEBUG("Advancing %ld in ANY case", |
| 137 | (long)(tag_len + len_len)); |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 138 | ADVANCE(tag_len + len_len); |
Lev Walkin | 7210fdb | 2004-09-04 04:44:30 +0000 | [diff] [blame] | 139 | } else { |
| 140 | assert(tagno < td->tags_count); /* At least one loop */ |
| 141 | } |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 142 | for((void)tagno; tagno < td->tags_count; tagno++, step++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Fetch and process T from TLV. |
| 146 | */ |
| 147 | tag_len = ber_fetch_tag(ptr, size, &tlv_tag); |
Lev Walkin | 00d3602 | 2004-10-05 06:35:43 +0000 | [diff] [blame] | 148 | ASN_DEBUG("Fetching tag from {%p,%ld}: " |
| 149 | "len %ld, step %d, tagno %d got %s", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 150 | ptr, (long)size, |
Lev Walkin | 00d3602 | 2004-10-05 06:35:43 +0000 | [diff] [blame] | 151 | (long)tag_len, step, tagno, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 152 | ber_tlv_tag_string(tlv_tag)); |
| 153 | switch(tag_len) { |
| 154 | case -1: RETURN(RC_FAIL); |
| 155 | case 0: RETURN(RC_WMORE); |
| 156 | } |
| 157 | |
| 158 | tlv_constr = BER_TLV_CONSTRUCTED(ptr); |
| 159 | |
| 160 | /* |
| 161 | * If {I}, don't check anything. |
| 162 | * If {I,B,C}, check B and C unless we're at I. |
| 163 | */ |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 164 | if(tag_mode != 0 && step == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 165 | /* |
| 166 | * We don't expect tag to match here. |
| 167 | * It's just because we don't know how the tag |
| 168 | * is supposed to look like. |
| 169 | */ |
| 170 | } else { |
| 171 | assert(tagno >= 0); /* Guaranteed by the code above */ |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 172 | if(tlv_tag != td->tags[tagno]) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 173 | /* |
| 174 | * Unexpected tag. Too bad. |
| 175 | */ |
| 176 | ASN_DEBUG("Expected: %s, expectation failed", |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 177 | ber_tlv_tag_string(td->tags[tagno])); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 178 | RETURN(RC_FAIL); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Attention: if there are more tags expected, |
| 184 | * ensure that the current tag is presented |
| 185 | * in constructed form (it contains other tags!). |
| 186 | * If this one is the last one, check that the tag form |
| 187 | * matches the one given in descriptor. |
| 188 | */ |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 189 | if(tagno < (td->tags_count - 1)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 190 | if(tlv_constr == 0) { |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 191 | ASN_DEBUG("tlv_constr = %d, expfail", |
| 192 | tlv_constr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 193 | RETURN(RC_FAIL); |
| 194 | } |
| 195 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 196 | if(last_tag_form != tlv_constr |
| 197 | && last_tag_form != -1) { |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 198 | ASN_DEBUG("last_tag_form %d != %d", |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 199 | last_tag_form, tlv_constr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 200 | RETURN(RC_FAIL); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Fetch and process L from TLV. |
| 206 | */ |
| 207 | len_len = ber_fetch_length(tlv_constr, |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 208 | (char *)ptr + tag_len, size - tag_len, &tlv_len); |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 209 | ASN_DEBUG("Fetchinig len = %ld", (long)len_len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | switch(len_len) { |
| 211 | case -1: RETURN(RC_FAIL); |
| 212 | case 0: RETURN(RC_WMORE); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * FIXME |
| 217 | * As of today, the chain of tags |
| 218 | * must either contain several indefinite length TLVs, |
| 219 | * or several definite length ones. |
| 220 | * No mixing is allowed. |
| 221 | */ |
| 222 | if(tlv_len == -1) { |
| 223 | /* |
| 224 | * Indefinite length. |
| 225 | */ |
| 226 | if(limit_len == -1) { |
| 227 | expect_00_terminators++; |
| 228 | } else { |
| 229 | ASN_DEBUG("Unexpected indefinite length " |
| 230 | "in a chain of definite lengths"); |
| 231 | RETURN(RC_FAIL); |
| 232 | } |
| 233 | ADVANCE(tag_len + len_len); |
| 234 | continue; |
| 235 | } else { |
| 236 | if(expect_00_terminators) { |
| 237 | ASN_DEBUG("Unexpected definite length " |
| 238 | "in a chain of indefinite lengths"); |
| 239 | RETURN(RC_FAIL); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Check that multiple TLVs specify ever decreasing length, |
| 245 | * which is consistent. |
| 246 | */ |
| 247 | if(limit_len == -1) { |
| 248 | limit_len = tlv_len + tag_len + len_len; |
Lev Walkin | 99414dc | 2004-09-29 13:21:23 +0000 | [diff] [blame] | 249 | if(limit_len < 0) { |
| 250 | /* Too great tlv_len value? */ |
| 251 | RETURN(RC_FAIL); |
| 252 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 253 | } else if(limit_len != tlv_len + tag_len + len_len) { |
| 254 | /* |
| 255 | * Inner TLV specifies length which is inconsistent |
| 256 | * with the outer TLV's length value. |
| 257 | */ |
| 258 | ASN_DEBUG("Outer TLV is %d and inner is %d", |
| 259 | limit_len, tlv_len); |
| 260 | RETURN(RC_FAIL); |
| 261 | } |
| 262 | |
| 263 | ADVANCE(tag_len + len_len); |
| 264 | |
| 265 | limit_len -= (tag_len + len_len); |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 266 | if((ssize_t)size > limit_len) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 267 | /* |
| 268 | * Make sure that we won't consume more bytes |
Lev Walkin | 5ccf1eb | 2004-09-24 20:58:47 +0000 | [diff] [blame] | 269 | * from the parent frame than the inferred limit. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 270 | */ |
| 271 | size = limit_len; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if(opt_tlv_form) |
| 276 | *opt_tlv_form = tlv_constr; |
| 277 | if(expect_00_terminators) |
| 278 | *last_length = -expect_00_terminators; |
| 279 | else |
| 280 | *last_length = tlv_len; |
| 281 | |
| 282 | RETURN(RC_OK); |
| 283 | } |