Пример #1
0
        /// <summary>
        /// Inflate the token
        /// NOTE: This operation is not continuable and assumes that the entire token is available in the stream
        /// </summary>
        /// <param name="source">Stream to inflate the token from</param>
        public override bool Inflate(Stream source)
        {
            // Read transaction descriptor
            TransactionDescriptor = TDSUtilities.ReadULong(source);

            // Read outstanding request count
            OutstandingRequestCount = TDSUtilities.ReadUInt(source);

            return(true);
        }
        /// <summary>
        /// Inflate the token
        /// NOTE: This operation is not continuable and assumes that the entire token is available in the stream
        /// </summary>
        /// <param name="source">Stream to inflate the token from</param>
        /// <returns>TRUE if inflation is complete</returns>
        public override bool Inflate(Stream source)
        {
            // Read status
            Status = (TDSDoneTokenStatusType)TDSUtilities.ReadUShort(source);

            // Read command
            Command = (TDSDoneTokenCommandType)TDSUtilities.ReadUShort(source);

            // Read row count
            RowCount = TDSUtilities.ReadULong(source);

            return(true);
        }
Пример #3
0
        public static object InflateColumnData(Stream source, TDSDataType column, Object dataTypeSpecific)
        {
            // Dispatch further reading based on the type
            switch (column)
            {
            case TDSDataType.Null:
            {
                // No data associated with it
                return(null);
            }

            case TDSDataType.DateTime:
            {
                int  daypart  = TDSUtilities.ReadInt(source);
                uint timepart = TDSUtilities.ReadUInt(source);
                return(null);
            }

            case TDSDataType.DateTime2N:
            {
                var length  = (byte)source.ReadByte();
                var rawDate = new byte[length];
                source.Read(rawDate, 0, length);
                uint secondIncrements = 0;
                for (int i = 0; i < length - 3; i++)
                {
                    secondIncrements += (uint)(rawDate[i] << (8 * i));
                }
                var days = (uint)rawDate[length - 3] + (uint)(rawDate[length - 2] << 8) +
                           (uint)(rawDate[length - 1] << 16);
                var date = new DateTime(1, 1, 1).AddDays(days).AddSeconds(secondIncrements * Math.Pow(10, -1 * 5));
                return(date);
            }

            case TDSDataType.Bit:
            case TDSDataType.Int1:
            {
                // Bit, 1 byte data representation
                return((byte)source.ReadByte());
            }

            case TDSDataType.Int2:
            {
                // SmallInt, 2 byte data representation
                return(unchecked ((short)TDSUtilities.ReadUShort(source)));
            }

            case TDSDataType.Int4:
            {
                // Int, 4 byte data representation
                return(unchecked ((int)TDSUtilities.ReadUInt(source)));
            }

            case TDSDataType.Float8:
            {
                // Float (8 byte data representation)
                return(BitConverter.ToDouble(BitConverter.GetBytes(TDSUtilities.ReadULong(source)), 0));
            }

            case TDSDataType.Int8:
            {
                // BigInt (8 byte data representation)
                return(unchecked ((long)TDSUtilities.ReadULong(source)));
            }

            case TDSDataType.BitN:
            {
                // Read length
                byte length = (byte)source.ReadByte();

                // Check if null
                if (length == 0)
                {
                    // No data
                    return(null);
                }

                // Bit, 1 byte data representation
                return((byte)source.ReadByte());
            }

            case TDSDataType.IntN:
            {
                // Read length
                byte length = (byte)source.ReadByte();

                // Check integer length
                switch (length)
                {
                case 0:
                {
                    // No data
                    return(null);
                }

                case 1:
                {
                    // Bit data
                    return((byte)source.ReadByte());
                }

                case 2:
                {
                    // Short data
                    return(unchecked ((short)TDSUtilities.ReadUShort(source)));
                }

                case 4:
                {
                    // Integer data
                    return(unchecked ((int)TDSUtilities.ReadUInt(source)));
                }

                case 8:
                {
                    // Integer data
                    return(unchecked ((long)TDSUtilities.ReadULong(source)));
                }

                default:
                {
                    // We don't know how to inflate this integer
                    throw new InvalidDataException(string.Format("Unable to inflate integer of {0} bytes", length));
                }
                }
            }

            case TDSDataType.NumericN:
            {
                // Read length
                byte length = (byte)source.ReadByte();

                // Check if null
                if (length == 0)
                {
                    // No data
                    return(null);
                }

                // Allocate value container
                byte[] guidBytes = new byte[length];

                // Read value
                source.Read(guidBytes, 0, guidBytes.Length);

                // Instantiate type
                return(guidBytes);
            }

            case TDSDataType.Guid:
            {
                // Read the length
                byte length = (byte)source.ReadByte();

                // Check if we have any data
                if (length == 0x0000)
                {
                    // No data
                    return(null);
                }

                // Allocate value container
                byte[] guidBytes = new byte[length];

                // Read value
                source.Read(guidBytes, 0, guidBytes.Length);

                // Convert to type
                return(new Guid(guidBytes));
            }

            case TDSDataType.BigVarChar:
            case TDSDataType.BigChar:
            {
                // Read length
                ushort length = TDSUtilities.ReadUShort(source);

                // Check if we have any data
                if (length == 0xFFFF)
                {
                    // No data
                    return(null);
                }

                // Allocate value container
                byte[] textBytes = new byte[length];

                // Read value
                source.Read(textBytes, 0, textBytes.Length);

                // Convert to type
                return(Encoding.ASCII.GetString(textBytes));
            }

            case TDSDataType.NVarChar:
            {
                // Check if MAX type
                if ((dataTypeSpecific as TDSShilohVarCharColumnSpecific).Length == 0xFFFF)
                {
                    // Read the length of partialy length-prefixed type
                    ulong length = TDSUtilities.ReadULong(source);

                    // Check the value
                    if (length == 0xFFFFFFFFFFFFFFFF)
                    {
                        // There's no data
                        return(null);
                    }
                    else if (length == 0xFFFFFFFFFFFFFFFE)
                    {
                        throw new NotImplementedException("UNKNOWN_PLP_LEN is not implemented yet");
                    }
                    else
                    {
                        // Allocate a memory stream
                        MemoryStream chunks = new MemoryStream();

                        // Size of the last chunk
                        uint chunkLength = 0;

                        // Iterate until we read the whole data
                        do
                        {
                            // Read the length of the chunk
                            chunkLength = TDSUtilities.ReadUInt(source);

                            // Allocate chunk container
                            byte[] chunk = new byte[chunkLength];

                            // Read value
                            source.Read(chunk, 0, chunk.Length);

                            // Append into the stream
                            chunks.Write(chunk, 0, chunk.Length);
                        }while (chunkLength > 0);

                        // Convert to byte array
                        byte[] byteString = chunks.ToArray();

                        // Serialize the data into the string
                        return(Encoding.Unicode.GetString(byteString, 0, byteString.Length));
                    }
                }
                else
                {
                    // Read length
                    ushort length = TDSUtilities.ReadUShort(source);

                    // Check if we have any data
                    if (length == 0xFFFF)
                    {
                        // No data
                        return(null);
                    }

                    // Read the whole string at once
                    return(TDSUtilities.ReadString(source, (ushort)length));
                }
            }

            case TDSDataType.BigVarBinary:
            {
                byte[] bytes = null;

                // Check if MAX type
                if ((ushort)dataTypeSpecific == 0xFFFF)
                {
                    // Read the length of partialy length-prefixed type
                    ulong length = TDSUtilities.ReadULong(source);

                    // Check the value
                    if (length == 0xFFFFFFFFFFFFFFFF)
                    {
                        // There's no data
                        return(null);
                    }
                    else if (length == 0xFFFFFFFFFFFFFFFE)
                    {
                        throw new NotImplementedException("UNKNOWN_PLP_LEN is not implemented yet");
                    }
                    else
                    {
                        // Allocate a memory stream
                        MemoryStream chunks = new MemoryStream();

                        // Size of the last chunk
                        uint chunkLength = 0;

                        // Iterate until we read the whole data
                        do
                        {
                            // Read the length of the chunk
                            chunkLength = TDSUtilities.ReadUInt(source);

                            // Allocate chunk container
                            byte[] chunk = new byte[chunkLength];

                            // Read value
                            source.Read(chunk, 0, chunk.Length);

                            // Append into the stream
                            chunks.Write(chunk, 0, chunk.Length);
                        }while (chunkLength > 0);

                        // Serialize to byte array
                        bytes = chunks.ToArray();
                    }
                }
                else
                {
                    // Read length
                    ushort length = TDSUtilities.ReadUShort(source);

                    // Check if we have any data
                    if (length == 0xFFFF)
                    {
                        // No data
                        return(null);
                    }

                    // Allocate value container
                    bytes = new byte[length];

                    // Read value
                    source.Read(bytes, 0, bytes.Length);
                }

                // Convert to type
                return(bytes);
            }

            case TDSDataType.BigBinary:
            {
                // Read length
                ushort length = TDSUtilities.ReadUShort(source);

                // Check if we have any data
                if (length == 0xFFFF)
                {
                    // No data
                    return(null);
                }

                // Allocate value container
                byte[] bytes = new byte[length];

                // Read value
                source.Read(bytes, 0, bytes.Length);

                // Convert to type
                return(bytes);
            }

            default:
            {
                // We don't know this type
                throw new NotImplementedException(string.Format("Unrecognized data type {0} for inflation", column));
            }
            }
        }