Beispiel #1
0
        private static List <Tuple <int, byte[]> > GetVariableWidthMemberBytes <T>(ComplianceSerializationDescription <T> description, T inputObject, ComplianceSerializer.VariableWidthType widthType) where T : class, new()
        {
            List <Tuple <int, byte[]> > list = new List <Tuple <int, byte[]> >();
            byte b = (byte)description.TotalStringFields;

            if (widthType == ComplianceSerializer.VariableWidthType.Blob)
            {
                b = (byte)description.TotalBlobFields;
            }
            for (byte b2 = 0; b2 < b; b2 += 1)
            {
                int    item  = 0;
                byte[] array = null;
                string text;
                if (widthType == ComplianceSerializer.VariableWidthType.Blob)
                {
                    if (inputObject != null && description.TryGetBlobProperty(inputObject, b2, out array) && array != null)
                    {
                        item = array.Length;
                    }
                }
                else if (inputObject != null && description.TryGetStringProperty(inputObject, b2, out text) && text != null)
                {
                    array = Encoding.UTF8.GetBytes(text);
                    item  = array.Length;
                }
                list.Add(new Tuple <int, byte[]>(item, array));
            }
            return(list);
        }
Beispiel #2
0
        private static bool TryWriteVariableWidthMembersToObject <T>(ref ComplianceSerializationDescription <T> description, ref T parsedObject, byte[] blob, int startIndex, int totalLength, ComplianceSerializer.VariableWidthType widthType, bool continueDeserialization, out int index, ref StringBuilder errorBuilder) where T : class, new()
        {
            if (!continueDeserialization)
            {
                index = startIndex;
                return(continueDeserialization);
            }
            index = startIndex;
            if (startIndex >= totalLength)
            {
                errorBuilder.AppendFormat("StartIndex:{0} is bigger than blob length:{1}", startIndex, totalLength);
                return(false);
            }
            byte b = blob[index++];

            if (b > 0)
            {
                List <int> list = new List <int>();
                for (byte b2 = 0; b2 < b; b2 += 1)
                {
                    if (index + 4 > totalLength)
                    {
                        errorBuilder.AppendFormat("Blob length:{0} is not sufficient to read the field-width from index:{1}.", totalLength, index);
                        return(false);
                    }
                    int item = ComplianceSerializer.ReadIntFromBlob(blob, index);
                    list.Add(item);
                    index += 4;
                }
                byte b3 = 0;
                foreach (int num in list)
                {
                    if (num > 0)
                    {
                        if (index + num > totalLength)
                        {
                            errorBuilder.AppendFormat("Blob length:{0} is not sufficient to read the field of size:{1} from index:{2}.", totalLength, num, index);
                            return(false);
                        }
                        if (widthType == ComplianceSerializer.VariableWidthType.String)
                        {
                            string @string = Encoding.UTF8.GetString(blob, index, num);
                            description.TrySetStringProperty(parsedObject, b3, @string);
                            index += num;
                        }
                        else
                        {
                            byte[] array = new byte[num];
                            Array.Copy(blob, index, array, 0, num);
                            description.TrySetBlobProperty(parsedObject, b3, array);
                            index += num;
                        }
                    }
                    b3 += 1;
                }
                return(true);
            }
            return(true);
        }
Beispiel #3
0
        public static byte[] Serialize <T>(ComplianceSerializationDescription <T> description, T inputObject) where T : class, new()
        {
            if (inputObject == null)
            {
                return(null);
            }
            int num = 1;

            num = num + 1 + description.TotalByteFields;
            num = num + 1 + 2 * description.TotalShortFields;
            num = num + 1 + 4 * description.TotalIntegerFields;
            num = num + 1 + 8 * description.TotalLongFields;
            num = num + 1 + 8 * description.TotalDoubleFields;
            num = num + 1 + 16 * description.TotalGuidFields;
            num = num + 1 + 4 * description.TotalStringFields;
            List <Tuple <int, byte[]> > variableWidthMemberBytes = ComplianceSerializer.GetVariableWidthMemberBytes <T>(description, inputObject, ComplianceSerializer.VariableWidthType.String);

            foreach (Tuple <int, byte[]> tuple in variableWidthMemberBytes)
            {
                num += tuple.Item1;
            }
            num = num + 1 + 4 * description.TotalBlobFields;
            List <Tuple <int, byte[]> > variableWidthMemberBytes2 = ComplianceSerializer.GetVariableWidthMemberBytes <T>(description, inputObject, ComplianceSerializer.VariableWidthType.Blob);

            foreach (Tuple <int, byte[]> tuple2 in variableWidthMemberBytes2)
            {
                num += tuple2.Item1;
            }
            num++;
            List <ComplianceSerializer.CollectionField> list = new List <ComplianceSerializer.CollectionField>();
            byte b = 0;

            while ((int)b < description.TotalCollectionFields)
            {
                CollectionItemType itemType = CollectionItemType.NotDefined;
                if (description.TryGetCollectionPropertyItemType(b, out itemType))
                {
                    IEnumerable <object> collectionItems = description.GetCollectionItems(inputObject, b);
                    ComplianceSerializer.CollectionField collectionField = ComplianceSerializer.CollectionField.GetCollectionField(itemType, collectionItems);
                    list.Add(collectionField);
                    num += collectionField.GetSizeOfSerializedCollectionField();
                }
                b += 1;
            }
            byte[] array = new byte[num];
            array[0] = description.ComplianceStructureId;
            int num2 = ComplianceSerializer.WriteFixedWidthFieldsToBlob <T>(ref description, ref inputObject, array, 1, 1, ComplianceSerializer.FixedWidthType.Byte, description.TotalByteFields);

            num2 = ComplianceSerializer.WriteFixedWidthFieldsToBlob <T>(ref description, ref inputObject, array, num2, 2, ComplianceSerializer.FixedWidthType.Short, description.TotalShortFields);
            num2 = ComplianceSerializer.WriteFixedWidthFieldsToBlob <T>(ref description, ref inputObject, array, num2, 4, ComplianceSerializer.FixedWidthType.Int, description.TotalIntegerFields);
            num2 = ComplianceSerializer.WriteFixedWidthFieldsToBlob <T>(ref description, ref inputObject, array, num2, 8, ComplianceSerializer.FixedWidthType.Long, description.TotalLongFields);
            num2 = ComplianceSerializer.WriteFixedWidthFieldsToBlob <T>(ref description, ref inputObject, array, num2, 8, ComplianceSerializer.FixedWidthType.Double, description.TotalDoubleFields);
            num2 = ComplianceSerializer.WriteFixedWidthFieldsToBlob <T>(ref description, ref inputObject, array, num2, 16, ComplianceSerializer.FixedWidthType.Guid, description.TotalGuidFields);
            num2 = ComplianceSerializer.WriteVariableWidthFieldsToBlob(array, num2, variableWidthMemberBytes);
            num2 = ComplianceSerializer.WriteVariableWidthFieldsToBlob(array, num2, variableWidthMemberBytes2);
            num2 = ComplianceSerializer.WriteCollectionsToBlob(array, num2, list);
            return(array);
        }
Beispiel #4
0
        public static T DeSerialize <T>(ComplianceSerializationDescription <T> description, byte[] blob) where T : class, new()
        {
            T result = default(T);
            FaultDefinition faultDefinition = null;

            if (!ComplianceSerializer.TryDeserialize <T>(description, blob, out result, out faultDefinition, "DeSerialize", "f:\\15.00.1497\\sources\\dev\\EDiscovery\\src\\TaskDistributionSystem\\TaskDistributionCommon\\Serialization\\ComplianceSerializer.cs", 77))
            {
                throw new BadStructureFormatException();
            }
            return(result);
        }
Beispiel #5
0
        private static bool TryWriteCollectionsToObject <T>(ref ComplianceSerializationDescription <T> description, ref T parsedObject, byte[] blob, int startIndex, int totalLength, out int index, ref StringBuilder errorBuilder) where T : class, new()
        {
            index = startIndex;
            if (startIndex >= totalLength)
            {
                errorBuilder.AppendFormat("StartIndex:{0} is bigger than blob length:{1}", startIndex, totalLength);
                return(false);
            }
            byte b = blob[index++];

            for (byte b2 = 0; b2 < b; b2 += 1)
            {
                Type typeFromHandle = typeof(CollectionItemType);
                CollectionItemType collectionItemType = (CollectionItemType)Enum.ToObject(typeFromHandle, blob[index++]);
                if (!Enum.IsDefined(typeFromHandle, collectionItemType))
                {
                    errorBuilder.AppendFormat("Byte value:{0} at index:{1} does not represent a valid CollectionItemType", collectionItemType, index - 1);
                    return(false);
                }
                if (index + 4 > totalLength)
                {
                    errorBuilder.AppendFormat("Blob length:{0} is not sufficient to read the field count:{1} at index:{2}.", totalLength, b, index);
                    return(false);
                }
                int num = ComplianceSerializer.ReadIntFromBlob(blob, index);
                index += 4;
                List <object> list = new List <object>();
                for (int i = 0; i < num; i++)
                {
                    ComplianceSerializer.CollectionItem collectionItem = null;
                    if (!ComplianceSerializer.CollectionItem.TryGetCollectionItemFromBlob(collectionItemType, blob, index, totalLength, out collectionItem, ref errorBuilder))
                    {
                        return(false);
                    }
                    list.Add(ComplianceSerializer.CollectionItem.GetObject(collectionItemType, collectionItem));
                    index += collectionItem.GetSerializedSize();
                }
                description.TrySetCollectionItems(parsedObject, b2, list);
            }
            return(true);
        }
Beispiel #6
0
        public static bool TryDeserialize <T>(ComplianceSerializationDescription <T> description, byte[] blob, out T parsedObject, out FaultDefinition faultDefinition, [CallerMemberName] string callerMember = null, [CallerFilePath] string callerFilePath = null, [CallerLineNumber] int callerLineNumber = 0) where T : class, new()
        {
            parsedObject = Activator.CreateInstance <T>();
            int totalLength = blob.Length;

            if (description.ComplianceStructureId != blob[0])
            {
                faultDefinition = FaultDefinition.FromErrorString("Parsing wrong structure", callerMember, callerFilePath, callerLineNumber);
                return(false);
            }
            int           startIndex    = 1;
            StringBuilder stringBuilder = new StringBuilder();
            bool          flag          = true;

            flag = ComplianceSerializer.TryWriteFixedWidthFieldsToObject <T>(ref description, ref parsedObject, blob, startIndex, 1, ComplianceSerializer.FixedWidthType.Byte, totalLength, flag, out startIndex, ref stringBuilder);
            flag = ComplianceSerializer.TryWriteFixedWidthFieldsToObject <T>(ref description, ref parsedObject, blob, startIndex, 2, ComplianceSerializer.FixedWidthType.Short, totalLength, flag, out startIndex, ref stringBuilder);
            flag = ComplianceSerializer.TryWriteFixedWidthFieldsToObject <T>(ref description, ref parsedObject, blob, startIndex, 4, ComplianceSerializer.FixedWidthType.Int, totalLength, flag, out startIndex, ref stringBuilder);
            flag = ComplianceSerializer.TryWriteFixedWidthFieldsToObject <T>(ref description, ref parsedObject, blob, startIndex, 8, ComplianceSerializer.FixedWidthType.Long, totalLength, flag, out startIndex, ref stringBuilder);
            flag = ComplianceSerializer.TryWriteFixedWidthFieldsToObject <T>(ref description, ref parsedObject, blob, startIndex, 8, ComplianceSerializer.FixedWidthType.Double, totalLength, flag, out startIndex, ref stringBuilder);
            flag = ComplianceSerializer.TryWriteFixedWidthFieldsToObject <T>(ref description, ref parsedObject, blob, startIndex, 16, ComplianceSerializer.FixedWidthType.Guid, totalLength, flag, out startIndex, ref stringBuilder);
            flag = ComplianceSerializer.TryWriteVariableWidthMembersToObject <T>(ref description, ref parsedObject, blob, startIndex, totalLength, ComplianceSerializer.VariableWidthType.String, flag, out startIndex, ref stringBuilder);
            flag = ComplianceSerializer.TryWriteVariableWidthMembersToObject <T>(ref description, ref parsedObject, blob, startIndex, totalLength, ComplianceSerializer.VariableWidthType.Blob, flag, out startIndex, ref stringBuilder);
            if (flag)
            {
                flag = ComplianceSerializer.TryWriteCollectionsToObject <T>(ref description, ref parsedObject, blob, startIndex, totalLength, out startIndex, ref stringBuilder);
            }
            if (flag)
            {
                faultDefinition = null;
            }
            else
            {
                faultDefinition = FaultDefinition.FromErrorString(stringBuilder.ToString(), callerMember, callerFilePath, callerLineNumber);
            }
            return(flag);
        }
Beispiel #7
0
        private static bool TryWriteFixedWidthFieldsToObject <T>(ref ComplianceSerializationDescription <T> description, ref T parsedObject, byte[] blob, int startIndex, int width, ComplianceSerializer.FixedWidthType widthType, int totalLength, bool continueDeserialization, out int index, ref StringBuilder errorBuilder) where T : class, new()
        {
            if (!continueDeserialization)
            {
                index = startIndex;
                return(continueDeserialization);
            }
            index = startIndex;
            if (startIndex >= totalLength)
            {
                errorBuilder.AppendFormat("StartIndex:{0} is bigger than blob length:{1}", startIndex, totalLength);
                return(false);
            }
            byte b = blob[startIndex];

            index++;
            for (byte b2 = 0; b2 < b; b2 += 1)
            {
                if (index + width > totalLength)
                {
                    errorBuilder.AppendFormat("Blob length:{0} is not sufficient to read the field from index:{1}.", totalLength, index);
                    return(false);
                }
                switch (widthType)
                {
                case ComplianceSerializer.FixedWidthType.Byte:
                    description.TrySetByteProperty(parsedObject, b2, blob[index]);
                    break;

                case ComplianceSerializer.FixedWidthType.Short:
                {
                    short value = ComplianceSerializer.ReadShortFromBlob(blob, index);
                    description.TrySetShortProperty(parsedObject, b2, value);
                    break;
                }

                case ComplianceSerializer.FixedWidthType.Int:
                {
                    int value2 = ComplianceSerializer.ReadIntFromBlob(blob, index);
                    description.TrySetIntegerProperty(parsedObject, b2, value2);
                    break;
                }

                case ComplianceSerializer.FixedWidthType.Long:
                {
                    long value3 = ComplianceSerializer.ReadLongFromBlob(blob, index);
                    description.TrySetLongProperty(parsedObject, b2, value3);
                    break;
                }

                case ComplianceSerializer.FixedWidthType.Double:
                {
                    double value4 = ComplianceSerializer.ReadDoubleFromBlob(blob, index);
                    description.TrySetDoubleProperty(parsedObject, b2, value4);
                    break;
                }

                case ComplianceSerializer.FixedWidthType.Guid:
                {
                    Guid value5 = ComplianceSerializer.ReadGuidFromBlob(blob, index);
                    description.TrySetGuidProperty(parsedObject, b2, value5);
                    break;
                }

                default:
                    throw new ArgumentException("widthType");
                }
                index += width;
            }
            return(true);
        }
Beispiel #8
0
        private static int WriteFixedWidthFieldsToBlob <T>(ref ComplianceSerializationDescription <T> description, ref T inputObject, byte[] blob, int startIndex, int width, ComplianceSerializer.FixedWidthType widthType, int totalFields) where T : class, new()
        {
            byte b = Convert.ToByte(totalFields);

            blob[startIndex] = b;
            int num = startIndex + 1;

            for (byte b2 = 0; b2 < b; b2 += 1)
            {
                switch (widthType)
                {
                case ComplianceSerializer.FixedWidthType.Byte:
                {
                    byte b3 = 0;
                    description.TryGetByteProperty(inputObject, b2, out b3);
                    blob[num] = b3;
                    break;
                }

                case ComplianceSerializer.FixedWidthType.Short:
                {
                    short shortValue = 0;
                    description.TryGetShortProperty(inputObject, b2, out shortValue);
                    ComplianceSerializer.WriteShortToBlob(blob, num, shortValue);
                    break;
                }

                case ComplianceSerializer.FixedWidthType.Int:
                {
                    int intValue = 0;
                    description.TryGetIntegerProperty(inputObject, b2, out intValue);
                    ComplianceSerializer.WriteIntToBlob(blob, num, intValue);
                    break;
                }

                case ComplianceSerializer.FixedWidthType.Long:
                {
                    long longValue = 0L;
                    description.TryGetLongProperty(inputObject, b2, out longValue);
                    ComplianceSerializer.WriteLongToBlob(blob, num, longValue);
                    break;
                }

                case ComplianceSerializer.FixedWidthType.Double:
                {
                    double doubleValue = 0.0;
                    description.TryGetDoubleProperty(inputObject, b2, out doubleValue);
                    ComplianceSerializer.WriteDoubleToBlob(blob, num, doubleValue);
                    break;
                }

                case ComplianceSerializer.FixedWidthType.Guid:
                {
                    Guid empty = Guid.Empty;
                    description.TryGetGuidProperty(inputObject, b2, out empty);
                    ComplianceSerializer.WriteGuidToBlob(blob, num, empty);
                    break;
                }

                default:
                    throw new ArgumentException("widthType");
                }
                num += width;
            }
            return(num);
        }
 public void RegisterComplexPropertyAsBlobGetterAndSetter <I>(byte fieldIndex, Func <T, I> getter, Action <T, I> setter, ComplianceSerializationDescription <I> itemDescription) where I : class, new()
 {
     this.RegisterBlobPropertyGetterAndSetter(fieldIndex, (T item) => ComplianceSerializer.Serialize <I>(itemDescription, getter(item)), delegate(T item, byte[] obj)
     {
         setter(item, ComplianceSerializer.DeSerialize <I>(itemDescription, obj));
     });
 }
 public void RegisterComplexCollectionAccessor <I>(byte fieldIndex, Func <T, int> itemCountGetter, Func <T, int, I> itemGetter, Action <T, I, int> itemAdder, ComplianceSerializationDescription <I> listItemDescription) where I : class, new()
 {
     this.RegisterCollectionPropertyAccessors(fieldIndex, () => CollectionItemType.Blob, itemCountGetter, (T item, int index) => ComplianceSerializer.Serialize <I>(listItemDescription, itemGetter(item, index)), delegate(T item, object obj, int index)
     {
         itemAdder(item, ComplianceSerializer.DeSerialize <I>(listItemDescription, (byte[])obj), index);
     });
 }