Esempio n. 1
0
        public static MsgPackItem Unpack(Stream stream, MsgPackSettings settings)
        {
            int typeByte = stream.ReadByte();

            if (typeByte < 0)
            {
                return(new MpError(settings, stream.Position, MsgPackTypeId.NeverUsed, "Unexpected end of data."));
            }
            MsgPackItem item = null;

            try {
                MsgPackTypeId type = (MsgPackTypeId)typeByte;
                switch (type)
                {
                case MsgPackTypeId.MpNull: item = new MpNull(settings); break;

                case MsgPackTypeId.MpBoolFalse:
                case MsgPackTypeId.MpBoolTrue: item = new MpBool(settings); break;

                //case MsgPackTypes.MpBytePart:
                //case MsgPackTypes.MpSBytePart:
                case MsgPackTypeId.MpSByte:
                case MsgPackTypeId.MpShort:
                case MsgPackTypeId.MpInt:
                case MsgPackTypeId.MpLong:
                case MsgPackTypeId.MpUByte:
                case MsgPackTypeId.MpUShort:
                case MsgPackTypeId.MpUInt:
                case MsgPackTypeId.MpULong: item = new MpInt(settings); break;

                case MsgPackTypeId.MpFloat:
                case MsgPackTypeId.MpDouble: item = new MpFloat(settings); break;

                //case MsgPackTypeId.MpStr5:
                case MsgPackTypeId.MpStr8:
                case MsgPackTypeId.MpStr16:
                case MsgPackTypeId.MpStr32: item = new MpString(settings); break;

                case MsgPackTypeId.MpBin8:
                case MsgPackTypeId.MpBin16:
                case MsgPackTypeId.MpBin32: item = new MpBin(settings); break;

                //case MsgPackTypeId.MpArray4:
                case MsgPackTypeId.MpArray16:
                case MsgPackTypeId.MpArray32: item = new MpArray(settings); break;

                //case MsgPackTypeId.MpMap4:
                case MsgPackTypeId.MpMap16:
                case MsgPackTypeId.MpMap32: item = new MpMap(settings); break;

                case MsgPackTypeId.MpFExt1:
                case MsgPackTypeId.MpFExt2:
                case MsgPackTypeId.MpFExt4:
                case MsgPackTypeId.MpFExt8:
                case MsgPackTypeId.MpFExt16:
                case MsgPackTypeId.MpExt8:
                case MsgPackTypeId.MpExt16:
                case MsgPackTypeId.MpExt32: item = new MpExt(settings); break;

                case MsgPackTypeId.NeverUsed:
                {
                    long pos = stream.Position - 1;
                    if (settings.ContinueProcessingOnBreakingError)
                    {
                        FindNextValidTypeId(stream);
                    }
                    return(new MpError(settings, pos, MsgPackTypeId.NeverUsed, "The specification specifically states that the value 0xC1 should never be used.")
                        {
                            storedLength = (stream.Position - pos)
                        });
                }
                }

                if (ReferenceEquals(item, null))
                {
                    if (((byte)type & 0xE0) == 0xE0 || (((byte)type & 0x80) == 0))
                    {
                        item = new MpInt(settings);
                    }
                    else if (((byte)type & 0xA0) == 0xA0)
                    {
                        item = new MpString(settings);
                    }
                    else if (((byte)type & 0x90) == 0x90)
                    {
                        item = new MpArray(settings);
                    }
                    else if (((byte)type & 0x80) == 0x80)
                    {
                        item = new MpMap(settings);
                    }
                }

                if (!ReferenceEquals(item, null))
                {
                    item.storedOffset = stream.Position - 1;
                    item._settings    = settings; // maybe redundent, but want to be sure
                    MsgPackItem ret = item.Read(type, stream);
                    item.storedLength = stream.Position - item.storedOffset;
                    if (!ReferenceEquals(item, ret))
                    {
                        ret.storedLength = item.storedLength;
                    }
                    return(ret);
                }
                else
                {
                    long pos = stream.Position - 1;
                    if (settings.ContinueProcessingOnBreakingError)
                    {
                        FindNextValidTypeId(stream);
                    }
                    return(new MpError(settings, pos, type, "The type identifier with value 0x", BitConverter.ToString(new byte[] { (byte)type }),
                                       " is either new or invalid. It is not (yet) implemented in this version of LsMsgPack.")
                    {
                        storedLength = (stream.Position - pos)
                    });
                }
            }catch (Exception ex) {
                long pos = stream.Position - 1;
                if (settings.ContinueProcessingOnBreakingError)
                {
                    FindNextValidTypeId(stream);
                }
                return(new MpError(settings, new MsgPackException("Error while reading data.", ex, stream.Position, (MsgPackTypeId)typeByte))
                {
                    storedOffset = pos,
                    storedLength = (stream.Position - pos),
                    PartialItem = item
                });
            }
        }
Esempio n. 2
0
        public static MsgPackItem Unpack(Stream stream)
        {
            int typeByte = stream.ReadByte();

            if (typeByte < 0)
            {
                throw new MsgPackException("Unexpected end of data.", stream.Position);
            }
            MsgPackItem item = null;

            try {
                MsgPackTypeId type = (MsgPackTypeId)typeByte;
                switch (type)
                {
                case MsgPackTypeId.MpNull: item = new MpNull(); break;

                case MsgPackTypeId.MpBoolFalse:
                case MsgPackTypeId.MpBoolTrue: item = new MpBool(); break;

                //case MsgPackTypes.MpBytePart:
                //case MsgPackTypes.MpSBytePart:
                case MsgPackTypeId.MpSByte:
                case MsgPackTypeId.MpShort:
                case MsgPackTypeId.MpInt:
                case MsgPackTypeId.MpLong:
                case MsgPackTypeId.MpUByte:
                case MsgPackTypeId.MpUShort:
                case MsgPackTypeId.MpUInt:
                case MsgPackTypeId.MpULong: item = new MpInt(); break;

                case MsgPackTypeId.MpFloat:
                case MsgPackTypeId.MpDouble: item = new MpFloat(); break;

                //case MsgPackTypeId.MpStr5:
                case MsgPackTypeId.MpStr8:
                case MsgPackTypeId.MpStr16:
                case MsgPackTypeId.MpStr32: item = new MpString(); break;

                case MsgPackTypeId.MpBin8:
                case MsgPackTypeId.MpBin16:
                case MsgPackTypeId.MpBin32: item = new MpBin(); break;

                //case MsgPackTypeId.MpArray4:
                case MsgPackTypeId.MpArray16:
                case MsgPackTypeId.MpArray32: item = new MpArray(); break;

                //case MsgPackTypeId.MpMap4:
                case MsgPackTypeId.MpMap16:
                case MsgPackTypeId.MpMap32: item = new MpMap(); break;

                case MsgPackTypeId.MpFExt1:
                case MsgPackTypeId.MpFExt2:
                case MsgPackTypeId.MpFExt4:
                case MsgPackTypeId.MpFExt8:
                case MsgPackTypeId.MpFExt16:
                case MsgPackTypeId.MpExt8:
                case MsgPackTypeId.MpExt16:
                case MsgPackTypeId.MpExt32: item = new MpExt(); break;

                case MsgPackTypeId.NeverUsed: throw new MsgPackException("The specification specifically states that the value 0xC1 should never be used.",
                                                                         stream.Position - 1, MsgPackTypeId.NeverUsed);
                }

                if (ReferenceEquals(item, null))
                {
                    if (((byte)type & 0xE0) == 0xE0 || (((byte)type & 0x80) == 0))
                    {
                        item = new MpInt();
                    }
                    else if (((byte)type & 0xA0) == 0xA0)
                    {
                        item = new MpString();
                    }
                    else if (((byte)type & 0x90) == 0x90)
                    {
                        item = new MpArray();
                    }
                    else if (((byte)type & 0x80) == 0x80)
                    {
                        item = new MpMap();
                    }
                }

                if (!ReferenceEquals(item, null))
                {
                    return(item.Read(type, stream));
                }
                else
                {
                    throw new MsgPackException(string.Concat("The type identifier with value 0x", BitConverter.ToString(new byte[] { (byte)type }),
                                                             " is either new or invalid. It is not (yet) implemented in this version of LsMsgPack."), stream.Position, type);
                }
            }catch (Exception ex) {
                if (!(ex is MsgPackException))
                {
                    MsgPackException mpex = new MsgPackException("Error while reading data.", ex, stream.Position, (MsgPackTypeId)typeByte);
                    if (!ReferenceEquals(item, null))
                    {
                        mpex.Data.Add("PartialMessage", item);
                    }
                    throw mpex;
                }
                else
                {
                    throw;
                }
            }
        }