Пример #1
0
 private static void ValidateInt(MsgPackItem item, List <ValidationItem> issues)
 {
     try {
         MpInt intItem = (MpInt)item;
         item.Settings.DynamicallyCompact = true;
         MsgPackTypeId calcType = intItem.TypeId;
         if (calcType != intItem.PreservedType)
         {
             int used      = MpInt.GetByteLengthForType(intItem.PreservedType);
             int potential = MpInt.GetByteLengthForType(calcType);
             if (potential < used)
             {
                 issues.Add(new ValidationItem(item, ValidationSeverity.Warning, (used - potential),
                                               "This integer should have been stored in a smaller container (", MsgPackItem.GetOfficialTypeName(calcType), " instead of ",
                                               MsgPackItem.GetOfficialTypeName(intItem.PreservedType), "). This would have saved ", (used - potential), " bytes."));
             }
             else if (MpInt.SignedTypeIds.Contains(intItem.PreservedType) && !MpInt.SignedTypeIds.Contains(calcType))
             {
                 issues.Add(new ValidationItem(item, ValidationSeverity.Comment, 0,
                                               "A positive integer has no need of a signing bit and can potentially be stored in a smaller container (in this case it did not matter)."));
             }
         }
     } finally {
         item.Settings.DynamicallyCompact = false;
     }
 }
Пример #2
0
        public override MsgPackItem Read(MsgPackTypeId typeId, System.IO.Stream data)
        {
            this.typeId = typeId;
            byte[] buffer;

            if (this.typeId == MsgPackTypeId.MpFloat)
            {
                buffer = new byte[4];
                data.Read(buffer, 0, 4);
            }
            else
            {
                buffer = new byte[8];
                data.Read(buffer, 0, 8);
            }

            ReorderIfLittleEndian(Settings, buffer);

            if (this.typeId == MsgPackTypeId.MpFloat)
            {
                f32value = BitConverter.ToSingle(buffer, 0);
            }
            else
            {
                f64value = BitConverter.ToDouble(buffer, 0);
            }

            return(this);
        }
Пример #3
0
        public override MsgPackItem Read(MsgPackTypeId typeId, Stream data)
        {
            this.typeId = typeId;
            long len;

            switch (typeId)
            {
            case MsgPackTypeId.MpFExt1: len = 1; break;

            case MsgPackTypeId.MpFExt2: len = 2; break;

            case MsgPackTypeId.MpFExt4: len = 4; break;

            case MsgPackTypeId.MpFExt8: len = 8; break;

            case MsgPackTypeId.MpFExt16: len = 16; break;

            case MsgPackTypeId.MpExt8:   len = ReadLen(data, 1); break;

            case MsgPackTypeId.MpExt16:  len = ReadLen(data, 2); break;

            case MsgPackTypeId.MpExt32:  len = ReadLen(data, 4); break;

            default: throw new MsgPackException(string.Concat("Ext does not support a type ID of ", GetOfficialTypeName(typeId), "."), data.Position - 1, typeId);
            }
            typeSpecifier = (sbyte)data.ReadByte();
            value         = ReadBytes(data, len);

            if (typeSpecifier == -1)
            {
                return(new MpDateTime(this));
            }

            return(this);
        }
Пример #4
0
 public static bool AreInSameFamily(MsgPackTypeId a, MsgPackTypeId b, bool NullIsEqual = true)
 {
     if (a == b)
     {
         return(true);
     }
     if (a == MsgPackTypeId.MpNull || b == MsgPackTypeId.MpNull)
     {
         return(NullIsEqual);
     }
     for (int t = typeFamilys.Length - 1; t >= 0; t--)
     {
         for (int ta = typeFamilys[t].Length - 1; ta >= 0; ta--)
         {
             if (typeFamilys[t][ta] == a)
             {
                 for (int tb = typeFamilys[t].Length - 1; tb >= 0; tb--)
                 {
                     if (typeFamilys[t][tb] == b)
                     {
                         return(true);
                     }
                 }
                 return(false);
             }
         }
     }
     return(false);
 }
Пример #5
0
        public override MsgPackItem Read(MsgPackTypeId typeId, Stream data)
        {
            long len;

            if (!IsMasked(MsgPackTypeId.MpArray4, typeId, 0x0F, out len))
            {
                switch (typeId)
                {
                case MsgPackTypeId.MpArray16: len = ReadLen(data, 2); break;

                case MsgPackTypeId.MpArray32: len = ReadLen(data, 4); break;

                default: throw new MsgPackException(string.Concat("MpArray does not support a type ID of ", GetOfficialTypeName(typeId), "."), data.Position - 1, typeId);
                }
            }

            value = new object[len];
            for (int t = 0; t < len; t++)
            {
                MsgPackItem item = Unpack(data);
                value[t] = item.Value;
            }

            return(this);
        }
Пример #6
0
        public override MsgPackItem Read(MsgPackTypeId typeId, Stream data)
        {
            long len;

            if (!IsMasked(MsgPackTypeId.MpMap4, typeId, 0x0F, out len))
            {
                switch (typeId)
                {
                case MsgPackTypeId.MpMap16: len = ReadLen(data, 2); break;

                case MsgPackTypeId.MpMap32: len = ReadLen(data, 4); break;

                default: throw new MsgPackException(string.Concat("MpMap does not support a type ID of ", GetOfficialTypeName(typeId), "."), data.Position - 1, typeId);
                }
            }

            value = new KeyValuePair <object, object> [len];

            for (int t = 0; t < len; t++)
            {
                MsgPackItem key = MsgPackItem.Unpack(data);
                MsgPackItem val = MsgPackItem.Unpack(data);
                value[t] = new KeyValuePair <object, object>(key.Value, val.Value);
            }

            return(this);
        }
Пример #7
0
        public void BinaryLengths(int length, int expectedBytes, MsgPackTypeId expedctedType)
        {
            Randomizer rnd = new Randomizer();

            byte[] test = new byte[length];
            rnd.NextBytes(test);
            MsgPackTests.RoundTripTest <MpExt, byte[]>(test, expectedBytes, expedctedType, (sbyte)(rnd.Next(255) - 128));
        }
Пример #8
0
        public void EnumTest <T>(T enumDef, MsgPackTypeId expectedType)
        {
            var vals = Enum.GetValues(typeof(T));

            for (int t = vals.Length - 1; t >= 0; t--)
            {
                MsgPackTests.RoundTripTest <MpInt, T>((T)vals.GetValue(t), -1, expectedType);
            }
        }
Пример #9
0
 protected void CopyBaseDataFrom(MpExt generic)
 {
     storedOffset  = generic.storedOffset;
     storedLength  = generic.storedLength;
     _settings     = generic._settings;
     typeId        = generic.typeId;
     typeSpecifier = generic.typeSpecifier;
     value         = generic.value;
 }
Пример #10
0
 public void PreserveTypeTest <T>(T value, int expectedLength, MsgPackTypeId expectedType)
 {
     MsgPackTests.DynamicallyCompactValue = false;
     try {
         MsgPackTests.RoundTripTest <MpInt, T>(value, expectedLength, expectedType);
     } finally {
         MsgPackTests.DynamicallyCompactValue = true;
     }
 }
Пример #11
0
 public static string GetOfficialTypeName(MsgPackTypeId typeId)
 {
     MsgPackMeta.PackDef def;
     if (MsgPackMeta.FromTypeId(typeId, out def))
     {
         return(def.OfficialName);
     }
     //if(typeId == MsgPackTypeId.NeverUsed) return "[\"Officially never used\"] (0xC1)";
     return(string.Concat("Undefined (0x", ((int)typeId).ToString("X"), ")"));
 }
Пример #12
0
 public static string GetOfficialTypeName(MsgPackTypeId typeId)
 {
     MsgPackMeta.PackDef def;
     if (MsgPackMeta.FromTypeId.TryGetValue(typeId, out def))
     {
         return(def.OfficialName);
     }
     //if(typeId == MsgPackTypeId.NeverUsed) return "[\"Officially never used\"] (0xC1)";
     return(string.Concat("Undefined (0x", BitConverter.ToString(new byte[] { (byte)typeId }), ")"));
 }
Пример #13
0
 internal static MsgPackMeta.PackDef GetTypeDescriptor(MsgPackTypeId typeId)
 {
     MsgPackMeta.PackDef def;
     if (MsgPackMeta.FromTypeId.TryGetValue(typeId, out def))
     {
         return(def);
     }
     return(new MsgPackMeta.PackDef(typeId, string.Concat("Undefined (0x", BitConverter.ToString(new byte[] { (byte)typeId }), ")"),
                                    "This value is either invalid or new to the specification since the implementation of this library. Check the specification and check for updates if the value is defined."));
 }
Пример #14
0
        public override byte[] ToBytes()
        {
            List <byte>   bytes  = new List <byte>(value.Length + 5); // current max length limit is 4 bytes + identifier
            MsgPackTypeId typeId = GetTypeId(value.LongLength);

            bytes.Add((byte)typeId);
            bytes.AddRange(GetLengthBytes(value.LongLength, SupportedLengths.All));
            bytes.AddRange(value);
            return(bytes.ToArray());
        }
Пример #15
0
        public override byte[] ToBytes()
        {
            ArrayList     bytes  = new ArrayList();
            MsgPackTypeId typeId = GetTypeId(value.Length);

            bytes.Add((byte)typeId);
            bytes.AddRange(GetLengthBytes(value.Length, SupportedLengths.All));
            bytes.AddRange(value);
            return((byte[])bytes.ToArray(typeof(byte)));
        }
Пример #16
0
        public override void ToStream(Stream stream)
        {
            MsgPackTypeId typeId = GetTypeId(value.LongLength);

            stream.WriteByte((byte)typeId);

            var lenBytes = GetLengthBytes(value.LongLength, SupportedLengths.All);

            stream.Write(lenBytes, 0, lenBytes.Length);
            stream.Write(value, 0, value.Length);
        }
Пример #17
0
        public void BinaryLengths(int length, int expectedBytes, MsgPackTypeId expedctedType)
        {
            Randomizer rnd = new Randomizer();

            byte[] test = new byte[length];
            rnd.NextBytes(test);
            if (test.Length > 0)
            {
                test[0] = 150; // prevent using implemented extension!
            }
            MsgPackTests.RoundTripTest <MpExt, byte[]>(test, expectedBytes, expedctedType, (sbyte)(rnd.Next(255) - 128));
        }
Пример #18
0
 public static bool FromTypeId(MsgPackTypeId id, out PackDef def)
 {
     for (int t = AllPacks.Length - 1; t >= 0; t--)
     {
         if (AllPacks[t].TypeId == id)
         {
             def = AllPacks[t];
             return(true);
         }
     }
     def = null;
     return(false);
 }
Пример #19
0
        public override MsgPackItem Read(MsgPackTypeId typeId, Stream data)
        {
            long len;

            if (!IsMasked(MsgPackTypeId.MpArray4, typeId, 0x0F, out len))
            {
                switch (typeId)
                {
                case MsgPackTypeId.MpArray16: len = ReadLen(data, 2); break;

                case MsgPackTypeId.MpArray32: len = ReadLen(data, 4); break;

                default: throw new MsgPackException(string.Concat("MpArray does not support a type ID of ", GetOfficialTypeName(typeId), "."), data.Position - 1, typeId);
                }
            }

            value       = new object[len];
            packedItems = new MsgPackItem[len];
            bool errorOccurred = false; // keep a local copy in order not to wrap all items after an error in error nodes (just the one the error occurred in, and all parents)

            for (int t = 0; t < len; t++)
            {
                MsgPackItem item = Unpack(data, _settings);
                if (_settings._preservePackages)
                {
                    packedItems[t] = item;
                }
                value[t] = item.Value;
                if (item is MpError)
                {
                    if (_settings.ContinueProcessingOnBreakingError)
                    {
                        _settings.FileContainsErrors = true;
                        errorOccurred = true;
                        if (data.Position >= data.Length)
                        {
                            return(new MpError(_settings, this));
                        }
                    }
                    else
                    {
                        return(new MpError(_settings, this));
                    }
                }
            }
            if (errorOccurred)
            {
                return(new MpError(_settings, this));
            }
            return(this);
        }
Пример #20
0
        public static MsgPackItem RoundTripTest <Typ, T>(T value, int expectedLength, MsgPackTypeId expectedMsgPackType, sbyte extensionType = 0) where Typ : MsgPackItem
        {
            bool preservingType = !DynamicallyCompactValue;

            MsgPackItem item;

            if (typeof(Typ) == typeof(MpExt))
            {
                item = new MpExt()
                {
                    Value         = value,
                    TypeSpecifier = extensionType
                };
            }
            else
            {
                item = MsgPackItem.Pack(value, new MsgPackSettings()
                {
                    DynamicallyCompact = DynamicallyCompactValue,
                    PreservePackages   = false,
                    ContinueProcessingOnBreakingError = false
                });
            }
            byte[] buffer       = item.ToBytes();
            Type   expectedType = typeof(Typ);
            Type   foundType    = item.GetType();

            MsgPackTypeId resultType    = preservingType && item is MpInt ? ((MpInt)item).PreservedType : item.TypeId;
            bool          typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;

            Assert.IsTrue(typesAreEqual, string.Concat("Expected packed type of ", expectedMsgPackType, " but received the type ", resultType));
            Assert.True(foundType == expectedType, string.Concat("Expected type of ", expectedType.ToString(), " but received the type ", foundType.ToString()));
            if (expectedLength >= 0)
            {
                Assert.AreEqual(expectedLength, buffer.Length, string.Concat("Expected a packed length of ", expectedLength.ToString(), " bytes but got ", buffer.Length.ToString(), " bytes."));
            }

            MsgPackItem recreate = MsgPackItem.Unpack(new MemoryStream(buffer));

            resultType    = preservingType && item is MpInt ? ((MpInt)recreate).PreservedType : recreate.TypeId;
            typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;
            Assert.IsTrue(typesAreEqual, string.Concat("Expected unpacked type of ", expectedMsgPackType, " but received the type ", resultType));

            T ret = recreate.GetTypedValue <T>();

            Assert.AreEqual(value, ret, "The returned value ", ret, " differs from the input value ", value);

            return(recreate);
        }
Пример #21
0
 private void UpdateTypeId()
 {
     if (isSigned)
     {
         if ((svalue >= -0x1F) && ((svalue <= 0x1F)))
         {
             typeId = MsgPackTypeId.MpSBytePart;
         }
         else if ((svalue >= sbyte.MinValue) && (svalue <= sbyte.MaxValue))
         {
             typeId = MsgPackTypeId.MpSByte;
         }
         else if ((svalue >= short.MinValue) && (svalue <= short.MaxValue))
         {
             typeId = MsgPackTypeId.MpShort;
         }
         else if ((svalue >= int.MinValue) && (svalue <= int.MaxValue))
         {
             typeId = MsgPackTypeId.MpInt;
         }
         else
         {
             typeId = MsgPackTypeId.MpLong;
         }
     }
     else
     {
         if (uvalue <= 0x7F)
         {
             typeId = MsgPackTypeId.MpBytePart;
         }
         else if (uvalue <= 255)
         {
             typeId = MsgPackTypeId.MpUByte;
         }
         else if (uvalue <= 0xFFFF)
         {
             typeId = MsgPackTypeId.MpUShort;
         }
         else if (uvalue <= 0xFFFFFFFF)
         {
             typeId = MsgPackTypeId.MpUInt;
         }
         else
         {
             typeId = MsgPackTypeId.MpULong;
         }
     }
 }
Пример #22
0
        public override long CalcBytesSize()
        {
            long retVal = 2;

            if (typeId == MsgPackTypeId.NeverUsed)
            {
                typeId = GetTypeId(value.LongLength);
            }
            if (VarLenExtTypes.Contains(typeId))
            {
                retVal += GetLengthBytesSize(value.LongLength, SupportedLengths.All);
            }
            retVal += value.LongLength;
            return(retVal);
        }
Пример #23
0
        public override long CalcBytesSize()
        {
            long          bytesCount = encoding.GetByteCount(value);
            MsgPackTypeId typeId     = GetTypeId(bytesCount);

            if (typeId == MsgPackTypeId.MpStr5)
            {
                bytesCount++;
            }
            else
            {
                bytesCount += GetLengthBytesSize(bytesCount, SupportedLengths.All) + 1;
            }
            return(bytesCount);
        }
Пример #24
0
        public override MsgPackItem Read(MsgPackTypeId typeId, Stream data)
        {
            MsgPackItem item = UnpackMultiple(data, Settings);

            if (item is MpRoot)
            {
                packedItems = ((MpRoot)item).packedItems;
            }
            else
            {
                packedItems = new List <MsgPackItem>(1);
                packedItems.Add(item);
            }

            return(item);
        }
Пример #25
0
        // [TestCase(0x7FEFFFF9, 0x7FEFFFF9 + 6, MsgPackTypeId.MpArray32)] // Out of memory on my machine
        public void ArrayLengths(int length, int expectedBytes, MsgPackTypeId expedctedType)
        {
            object[] test            = new object[length];
            int      additionalBytes = FillArrayWithRandomNumbers(test);

            additionalBytes -= test.Length;
            MsgPackItem item = MsgPackTests.RoundTripTest <MpArray, object[]>(test, expectedBytes + additionalBytes, expedctedType);

            object[] ret = item.GetTypedValue <object[]>();

            Assert.AreEqual(length, ret.Length, string.Concat("Expected ", length, " items but got ", ret.Length, " items in the array."));
            for (int t = ret.Length - 1; t >= 0; t--)
            {
                Assert.AreEqual(test[t], ret[t], string.Concat("Expected ", test[t], " but got ", ret[t], " at index ", t));
            }
        }
Пример #26
0
        protected bool IsMasked(MsgPackTypeId definition, MsgPackTypeId value, byte valueMask, out long len)
        {
            byte def = (byte)definition;
            byte val = (byte)value;

            len = val & valueMask;

            if ((val - len) == def)
            {
                return(true);
            }
            else
            {
                len = 0;
                return(false);
            }
        }
Пример #27
0
        public override void ToStream(Stream stream)
        {
            if (typeId == MsgPackTypeId.NeverUsed)
            {
                typeId = GetTypeId(value.LongLength);
            }
            stream.WriteByte((byte)typeId);

            if (VarLenExtTypes.Contains(typeId))
            {
                var lenBytes = GetLengthBytes(value.LongLength, SupportedLengths.All);
                stream.Write(lenBytes, 0, lenBytes.Length);
            }

            stream.WriteByte((byte)typeSpecifier);
            stream.Write(value, 0, value.Length);
        }
Пример #28
0
        public override byte[] ToBytes()
        {
            List <byte> bytes = new List <byte>(value.Length + 6); // current max length limit is 4 bytes + specifier + identifier

            if (typeId == MsgPackTypeId.NeverUsed)
            {
                typeId = GetTypeId(value.LongLength);
            }
            bytes.Add((byte)typeId);
            if (VarLenExtTypes.Contains(typeId))
            {
                bytes.AddRange(GetLengthBytes(value.LongLength, SupportedLengths.All));
            }
            bytes.Add((byte)typeSpecifier);
            bytes.AddRange(value);
            return(bytes.ToArray());
        }
Пример #29
0
        /*public override byte[] ToBytes()
         * {
         *  List<byte> bytes = new List<byte>(value.Length + 5); // current max length limit is 4 bytes + identifier
         *  MsgPackTypeId typeId = GetTypeId(value.LongLength);
         *  bytes.Add((byte)typeId);
         *  bytes.AddRange(GetLengthBytes(value.LongLength, SupportedLengths.All));
         *  bytes.AddRange(value);
         *  return bytes.ToArray();
         * }*/

        public override MsgPackItem Read(MsgPackTypeId typeId, Stream data)
        {
            long len;

            switch (typeId)
            {
            case MsgPackTypeId.MpBin8: len = ReadLen(data, 1); break;

            case MsgPackTypeId.MpBin16: len = ReadLen(data, 2); break;

            case MsgPackTypeId.MpBin32: len = ReadLen(data, 4); break;

            default: throw new MsgPackException(string.Concat("MpBin does not support a type ID of ", GetOfficialTypeName(typeId), "."), data.Position - 1, typeId);
            }
            value = ReadBytes(data, len);
            return(this);
        }
Пример #30
0
        public override byte[] ToBytes()
        {
            byte[]        strBytes = StrAsBytes;
            ArrayList     bytes    = new ArrayList(); // current max length limit is 4 bytes + string identifier
            MsgPackTypeId typeId   = GetTypeId(strBytes.Length);

            if (typeId == MsgPackTypeId.MpStr5)
            {
                bytes.Add(GetLengthBytes(typeId, strBytes.Length));
            }
            else
            {
                bytes.Add((byte)typeId);
                bytes.AddRange(GetLengthBytes(strBytes.Length, SupportedLengths.All));
            }
            bytes.AddRange(strBytes);
            return((byte[])bytes.ToArray(typeof(byte)));
        }