Exemple #1
0
        /// <summary>
        /// Convertes a primitve object (that is not of type object!!) to its byte[] representative
        /// </summary>
        /// <param name="conv"></param>
        /// <param name="obj"></param>
        protected byte[] ConvertPrimAsByte(TLVConv conv, object obj, bool isValue = false)
        {
            int lenBefore = mStream.GetBuffer().Length;
            int len = 0;

            object o;
            if (isValue)
            {
                o = obj;
            }
            else
            {
                o = conv.PropInfo.GetValue(obj);
            }

            switch (conv.Typ.Name)
            {
                case "Int32":
                    int tmp = (int)o;
                    bWriter.Write(tmp);
                    len = sizeof(int);
                    break;
                case "UInt32":
                    uint tmp2 = (uint)o;
                    bWriter.Write(tmp2);
                    len = sizeof(uint);
                    break;
                case "Byte":
                    byte b = (byte)o;
                    return new byte[] { b };
                default:
                    throw new InvalidDataException("Unsupported object to convert");
            }
            byte[] buffy = new byte[len];

            bWriter.Flush();
            //byte[] buffy = br.ReadBytes(len);
            br.BaseStream.Position = 0;
            br.Read(buffy, 0, len);

            mStream.SetLength(0);

            //mStream.Position = 0;
            //mStream.Read(buffy, 0, len);
            //mStream.Flush();

            int i = buffy.Length - 1;
            lenBefore = -1; // use as last index
            while (i >= 0)
            {
                if (buffy[i] == 0x00 && lenBefore < 0) { }
                else
                {
                    if (lenBefore < 0) lenBefore = i;
                }
                --i;
            }

            return buffy.Take(lenBefore + 1).ToArray();
        }
Exemple #2
0
 private static int SetT(TLVConv conv, List<byte> arr, int len, bool isReverse)
 {
     Add(arr, true, conv.Identifier);
     len += conv.Identifier.Length;
     return len;
 }
Exemple #3
0
        protected object Parse(byte[] arr, ref int idx, TLVConv conv, object obj)
        {
            TypeWrapper type = new TypeWrapper();
            if ((arr[idx] & TLV_PRIM_CONSTR_TYPE_MAKS) == TLV_PRIM_CONSTR_TYPE_MAKS)
            {
                // When constructed, the first bit does not contain any info beside domain/application
                type.IsConstructed = true;

                // Only incr. when bits [4:0] set
                //if ((arr[idx] & 0x1F) == 0x1F) ++idx;
            }
            ParseIdentifier(arr, ref idx, type);

            if (!IsPrimitive(type) && conv == null)
            {
                conv = TransMap[type.Identifier];
                obj = ((TLVCompositeConv)conv).CreateInstance.Invoke(emptyCtorArgs);
            }

            uint length = GetLength(arr, ref idx);

            if (IsPrimitive(type))
            {
                var value = ParsePrimitive(arr, ref idx, length, type.Identifier);

                // For 'normal'
                if (conv != null) conv.PropInfo.SetValue(obj, value);
                // For primitive in a list (is added in iteration)
                else obj = value;

                return obj;
            }

            if (IsList(conv))
            {
                Console.WriteLine("isList");
                ParseList(arr, ref idx, length, (TLVListConv)conv, obj);
            }
            else
            {
                // Create object
                ParseConstructed(arr, ref idx, length, conv as TLVCompositeConv, obj);
                //TLVCompositeConv conv = TransMap[type.Identifier];
            }

            return obj;
        }
Exemple #4
0
        protected static bool IsList(TLVConv conv)
        {
            if (conv is TLVListConv) return true;

            return false;
        }
Exemple #5
0
 private static void SetCommonConvData(PropertyInfo prop, TLVConv c, TLVAttr a)
 {
     c.Typ = prop.PropertyType;
     c.PropInfo = prop;
     c.Sorting = a.Sorting;
 }
Exemple #6
0
        private static byte[] GetIdentifierOctet(TLVConv conv)
        {
            byte first = 0x0;
            switch (conv.Typ.Name.ToString())
            {
                case "Byte":
                case "int":
                case "Int32":
                case "UInt32":
                    first |= 0x2;
                    break;
                default:
                    // todo
                    break;
            }

            // For primitives
            if (first != 0x0)
            {
                return new byte[] { first };
            }

            // For composites
            first |= TLV_PRIM_CONSTR_TYPE_MAKS;
            first |= TLV_CLASS_TAG_MASK;

            TLVCompositeConv c = conv as TLVCompositeConv;

            if (c == null)
            {
                throw new UnsupportedTLVType("Cannot covert conv type to TLVCompositeConv, identifier is: " + conv.Identifier);
            }

            uint ident = c.CompTypeAttr.StructId;
            if (ident < 31)
            {
                first |= (byte)ident;
                return new byte[] { first };
            }

            first |= TLV_IDENTIFIER_CONSTRUCTED_MAKS;

            byte[] identifier = ByteHelper.SetIntInRange(ident, 6);
            byte[] retArr = new byte[identifier.Length + 1];
            retArr[0] = first;

            for (int i = 0; i < identifier.Length; i++)
            {
                retArr[i + 1] = identifier[i];

                // set bit [7] of all identifier octets, only the last octet with bit [7] unset
                if (i > identifier.Length - 1) retArr[i + 1] |= 0x80;
            }

            //Array.Copy(identifier, 0, retArr, 0, identifier.Length);

            return retArr;
        }
Exemple #7
0
        private static TLVConv EnrichRek(TLVConv conv, PropertyInfo prop, TLVConv c)
        {
            TLVCompositeAttr compAttr = prop.GetCustomAttribute<TLVCompositeAttr>(false);
            TLVPrimitiveAttr primAttr = prop.GetCustomAttribute<TLVPrimitiveAttr>(false);
            TLVListAttr listAttr = prop.GetCustomAttribute<TLVListAttr>(false);

            if (compAttr != null)
            {
                c = EnrichCompositeType(prop, compAttr);
            }
            else if (listAttr != null)
            {
                // TODO
                c = new TLVListConv();
                TLVListConv lConv = c as TLVListConv;
                SetCommonConvData(prop, c, listAttr); // todo: check this line
                lConv.Identifier = TLV_IDENTIFIER_LIST;
                lConv.ListType = listAttr.ListType != null ? listAttr.ListType : lConv.PropInfo.PropertyType;
                lConv.IsList = true;
                //lConv.Contains = EnrichRek(lConv, null, lConv.Contains);
                //listAttr.ListType.GetGenericTypeDefinition();

            }
            else if (primAttr != null)
            {
                c = EnrichPrimitiveConv(prop, primAttr);
            }

            if (c != null)
            {
                listAllConvs.Add(c);
            }

            return c;
        }