Adding initial code jy.oak@samsung.com
[ric-app/kpimon.git] / asn1c_defs / all-defs / ber_decoder.c
diff --git a/asn1c_defs/all-defs/ber_decoder.c b/asn1c_defs/all-defs/ber_decoder.c
new file mode 100755 (executable)
index 0000000..e913152
--- /dev/null
@@ -0,0 +1,283 @@
+/*-\r
+ * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.\r
+ * Redistribution and modifications are permitted subject to BSD license.\r
+ */\r
+#include <asn_internal.h>\r
+\r
+#undef ADVANCE\r
+#define        ADVANCE(num_bytes)      do {                                    \\r
+               size_t num = num_bytes;                                 \\r
+               ptr = ((const char *)ptr) + num;                        \\r
+               size -= num;                                            \\r
+               consumed_myself += num;                                 \\r
+       } while(0)\r
+#undef RETURN\r
+#define        RETURN(_code)   do {                                            \\r
+               asn_dec_rval_t rval;                                    \\r
+               rval.code = _code;                                      \\r
+               if(opt_ctx) opt_ctx->step = step; /* Save context */    \\r
+               if(_code == RC_OK || opt_ctx)                           \\r
+                       rval.consumed = consumed_myself;                \\r
+               else                                                    \\r
+                       rval.consumed = 0;      /* Context-free */      \\r
+               return rval;                                            \\r
+       } while(0)\r
+\r
+/*\r
+ * The BER decoder of any type.\r
+ */\r
+asn_dec_rval_t\r
+ber_decode(const asn_codec_ctx_t *opt_codec_ctx,\r
+           const asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr,\r
+           const void *ptr, size_t size) {\r
+    asn_codec_ctx_t s_codec_ctx;\r
+\r
+       /*\r
+        * Stack checker requires that the codec context\r
+        * must be allocated on the stack.\r
+        */\r
+       if(opt_codec_ctx) {\r
+               if(opt_codec_ctx->max_stack_size) {\r
+                       s_codec_ctx = *opt_codec_ctx;\r
+                       opt_codec_ctx = &s_codec_ctx;\r
+               }\r
+       } else {\r
+               /* If context is not given, be security-conscious anyway */\r
+               memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));\r
+               s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;\r
+               opt_codec_ctx = &s_codec_ctx;\r
+       }\r
+\r
+       /*\r
+        * Invoke type-specific decoder.\r
+        */\r
+       return type_descriptor->op->ber_decoder(opt_codec_ctx, type_descriptor,\r
+               struct_ptr,     /* Pointer to the destination structure */\r
+               ptr, size,      /* Buffer and its size */\r
+               0               /* Default tag mode is 0 */\r
+               );\r
+}\r
+\r
+/*\r
+ * Check the set of <TL<TL<TL...>>> tags matches the definition.\r
+ */\r
+asn_dec_rval_t\r
+ber_check_tags(const asn_codec_ctx_t *opt_codec_ctx,\r
+               const asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx,\r
+               const void *ptr, size_t size, int tag_mode, int last_tag_form,\r
+               ber_tlv_len_t *last_length, int *opt_tlv_form) {\r
+    ssize_t consumed_myself = 0;\r
+       ssize_t tag_len;\r
+       ssize_t len_len;\r
+       ber_tlv_tag_t tlv_tag;\r
+       ber_tlv_len_t tlv_len;\r
+       ber_tlv_len_t limit_len = -1;\r
+       int expect_00_terminators = 0;\r
+       int tlv_constr = -1;    /* If CHOICE, opt_tlv_form is not given */\r
+       int step = opt_ctx ? opt_ctx->step : 0; /* Where we left previously */\r
+       int tagno;\r
+\r
+       /*\r
+        * Make sure we didn't exceed the maximum stack size.\r
+        */\r
+       if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))\r
+               RETURN(RC_FAIL);\r
+\r
+       /*\r
+        * So what does all this implicit skip stuff mean?\r
+        * Imagine two types,\r
+        *      A ::= [5] IMPLICIT      T\r
+        *      B ::= [2] EXPLICIT      T\r
+        * Where T is defined as\r
+        *      T ::= [4] IMPLICIT SEQUENCE { ... }\r
+        * \r
+        * Let's say, we are starting to decode type A, given the\r
+        * following TLV stream: <5> <0>. What does this mean?\r
+        * It means that the type A contains type T which is,\r
+        * in turn, empty.\r
+        * Remember though, that we are still in A. We cannot\r
+        * just pass control to the type T decoder. Why? Because\r
+        * the type T decoder expects <4> <0>, not <5> <0>.\r
+        * So, we must make sure we are going to receive <5> while\r
+        * still in A, then pass control to the T decoder, indicating\r
+        * that the tag <4> was implicitly skipped. The decoder of T\r
+        * hence will be prepared to treat <4> as valid tag, and decode\r
+        * it appropriately.\r
+        */\r
+\r
+       tagno = step    /* Continuing where left previously */\r
+               + (tag_mode==1?-1:0)\r
+               ;\r
+       ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",\r
+               td->name, (long)size, tag_mode, step, tagno);\r
+       /* assert(td->tags_count >= 1) May not be the case for CHOICE or ANY */\r
+\r
+       if(tag_mode == 0 && tagno == (int)td->tags_count) {\r
+               /*\r
+                * This must be the _untagged_ ANY type,\r
+                * which outermost tag isn't known in advance.\r
+                * Fetch the tag and length separately.\r
+                */\r
+               tag_len = ber_fetch_tag(ptr, size, &tlv_tag);\r
+               switch(tag_len) {\r
+               case -1: RETURN(RC_FAIL);\r
+               case 0: RETURN(RC_WMORE);\r
+               }\r
+               tlv_constr = BER_TLV_CONSTRUCTED(ptr);\r
+               len_len = ber_fetch_length(tlv_constr,\r
+                       (const char *)ptr + tag_len, size - tag_len, &tlv_len);\r
+               switch(len_len) {\r
+               case -1: RETURN(RC_FAIL);\r
+               case 0: RETURN(RC_WMORE);\r
+               }\r
+               ASN_DEBUG("Advancing %ld in ANY case",\r
+                       (long)(tag_len + len_len));\r
+               ADVANCE(tag_len + len_len);\r
+       } else {\r
+               assert(tagno < (int)td->tags_count);    /* At least one loop */\r
+       }\r
+       for((void)tagno; tagno < (int)td->tags_count; tagno++, step++) {\r
+\r
+               /*\r
+                * Fetch and process T from TLV.\r
+                */\r
+               tag_len = ber_fetch_tag(ptr, size, &tlv_tag);\r
+                       ASN_DEBUG("Fetching tag from {%p,%ld}: "\r
+                               "len %ld, step %d, tagno %d got %s",\r
+                               ptr, (long)size,\r
+                               (long)tag_len, step, tagno,\r
+                               ber_tlv_tag_string(tlv_tag));\r
+               switch(tag_len) {\r
+               case -1: RETURN(RC_FAIL);\r
+               case 0: RETURN(RC_WMORE);\r
+               }\r
+\r
+               tlv_constr = BER_TLV_CONSTRUCTED(ptr);\r
+\r
+               /*\r
+                * If {I}, don't check anything.\r
+                * If {I,B,C}, check B and C unless we're at I.\r
+                */\r
+               if(tag_mode != 0 && step == 0) {\r
+                       /*\r
+                        * We don't expect tag to match here.\r
+                        * It's just because we don't know how the tag\r
+                        * is supposed to look like.\r
+                        */\r
+               } else {\r
+                   assert(tagno >= 0); /* Guaranteed by the code above */\r
+                   if(tlv_tag != td->tags[tagno]) {\r
+                       /*\r
+                        * Unexpected tag. Too bad.\r
+                        */\r
+                       ASN_DEBUG("Expected: %s, "\r
+                               "expectation failed (tn=%d, tm=%d)",\r
+                               ber_tlv_tag_string(td->tags[tagno]),\r
+                               tagno, tag_mode\r
+                       );\r
+                       RETURN(RC_FAIL);\r
+                   }\r
+               }\r
+\r
+               /*\r
+                * Attention: if there are more tags expected,\r
+                * ensure that the current tag is presented\r
+                * in constructed form (it contains other tags!).\r
+                * If this one is the last one, check that the tag form\r
+                * matches the one given in descriptor.\r
+                */\r
+               if(tagno < ((int)td->tags_count - 1)) {\r
+                       if(tlv_constr == 0) {\r
+                               ASN_DEBUG("tlv_constr = %d, expfail",\r
+                                       tlv_constr);\r
+                               RETURN(RC_FAIL);\r
+                       }\r
+               } else {\r
+                       if(last_tag_form != tlv_constr\r
+                       && last_tag_form != -1) {\r
+                               ASN_DEBUG("last_tag_form %d != %d",\r
+                                       last_tag_form, tlv_constr);\r
+                               RETURN(RC_FAIL);\r
+                       }\r
+               }\r
+\r
+               /*\r
+                * Fetch and process L from TLV.\r
+                */\r
+               len_len = ber_fetch_length(tlv_constr,\r
+                       (const char *)ptr + tag_len, size - tag_len, &tlv_len);\r
+               ASN_DEBUG("Fetching len = %ld", (long)len_len);\r
+               switch(len_len) {\r
+               case -1: RETURN(RC_FAIL);\r
+               case 0: RETURN(RC_WMORE);\r
+               }\r
+\r
+               /*\r
+                * FIXME\r
+                * As of today, the chain of tags\r
+                * must either contain several indefinite length TLVs,\r
+                * or several definite length ones.\r
+                * No mixing is allowed.\r
+                */\r
+               if(tlv_len == -1) {\r
+                       /*\r
+                        * Indefinite length.\r
+                        */\r
+                       if(limit_len == -1) {\r
+                               expect_00_terminators++;\r
+                       } else {\r
+                               ASN_DEBUG("Unexpected indefinite length "\r
+                                       "in a chain of definite lengths");\r
+                               RETURN(RC_FAIL);\r
+                       }\r
+                       ADVANCE(tag_len + len_len);\r
+                       continue;\r
+               } else {\r
+                       if(expect_00_terminators) {\r
+                               ASN_DEBUG("Unexpected definite length "\r
+                                       "in a chain of indefinite lengths");\r
+                               RETURN(RC_FAIL);\r
+                       }\r
+               }\r
+\r
+               /*\r
+                * Check that multiple TLVs specify ever decreasing length,\r
+                * which is consistent.\r
+                */\r
+               if(limit_len == -1) {\r
+                       limit_len    = tlv_len + tag_len + len_len;\r
+                       if(limit_len < 0) {\r
+                               /* Too great tlv_len value? */\r
+                               RETURN(RC_FAIL);\r
+                       }\r
+               } else if(limit_len != tlv_len + tag_len + len_len) {\r
+                       /*\r
+                        * Inner TLV specifies length which is inconsistent\r
+                        * with the outer TLV's length value.\r
+                        */\r
+                       ASN_DEBUG("Outer TLV is %ld and inner is %ld",\r
+                               (long)limit_len, (long)tlv_len);\r
+                       RETURN(RC_FAIL);\r
+               }\r
+\r
+               ADVANCE(tag_len + len_len);\r
+\r
+               limit_len -= (tag_len + len_len);\r
+               if((ssize_t)size > limit_len) {\r
+                       /*\r
+                        * Make sure that we won't consume more bytes\r
+                        * from the parent frame than the inferred limit.\r
+                        */\r
+                       size = limit_len;\r
+               }\r
+       }\r
+\r
+       if(opt_tlv_form)\r
+               *opt_tlv_form = tlv_constr;\r
+       if(expect_00_terminators)\r
+               *last_length = -expect_00_terminators;\r
+       else\r
+               *last_length = tlv_len;\r
+\r
+       RETURN(RC_OK);\r
+}\r