public static object Read(Stream stream, FormatItem format, DbEnvironment env) { switch (format.DataType) { case TdsDataType.TDS_BIT: return(stream.ReadBool()); case TdsDataType.TDS_INT1: return((byte)stream.ReadByte()); case TdsDataType.TDS_SINT1: return((sbyte)stream.ReadByte()); case TdsDataType.TDS_INT2: return(stream.ReadShort()); case TdsDataType.TDS_UINT2: return(stream.ReadUShort()); case TdsDataType.TDS_INT4: return(stream.ReadInt()); case TdsDataType.TDS_UINT4: return(stream.ReadUInt()); case TdsDataType.TDS_INT8: return(stream.ReadLong()); case TdsDataType.TDS_UINT8: return(stream.ReadULong()); case TdsDataType.TDS_INTN: switch (stream.ReadByte()) { case 0: return(DBNull.Value); case 1: return((byte)stream.ReadByte()); //both INTN(1) and UINTN(1) are an INT1. Never an SINT1. case 2: return(stream.ReadShort()); case 4: return(stream.ReadInt()); case 8: return(stream.ReadLong()); } break; case TdsDataType.TDS_UINTN: switch (stream.ReadByte()) { case 0: return(DBNull.Value); case 1: return((byte)stream.ReadByte()); case 2: return(stream.ReadUShort()); case 4: return(stream.ReadUInt()); case 8: return(stream.ReadULong()); } break; case TdsDataType.TDS_FLT4: return(stream.ReadFloat()); case TdsDataType.TDS_FLT8: return(stream.ReadDouble()); case TdsDataType.TDS_FLTN: switch (stream.ReadByte()) { case 0: return(DBNull.Value); case 4: return(stream.ReadFloat()); case 8: return(stream.ReadDouble()); } break; case TdsDataType.TDS_CHAR: case TdsDataType.TDS_VARCHAR: case TdsDataType.TDS_BOUNDARY: case TdsDataType.TDS_SENSITIVITY: return(stream.ReadNullableByteLengthPrefixedString(env.Encoding)); case TdsDataType.TDS_BINARY: case TdsDataType.TDS_VARBINARY: return(stream.ReadNullableByteLengthPrefixedByteArray()); case TdsDataType.TDS_LONGCHAR: return(stream.ReadNullableIntLengthPrefixedString(env.Encoding)); /* * TDS_LONGBINARY serialization 55 serialized java object or instance (i.e. java object) * TDS_LONGBINARY serialized java class 56 serialized java class (i.e. byte code) * TDS_LONGBINARY smallbinary 59 64K max length binary data (ASA) * TDS_LONGBINARY unichar 34 fixed length UTF-16 encoded data * TDS_LONGBINARY univarchar 35 variable length UTF-16 encoded data */ case TdsDataType.TDS_LONGBINARY: { //the UserType can affect how we need to interpret the result data switch (format.UserType) { case 34: case 35: return(stream.ReadNullableIntLengthPrefixedString(Encoding.Unicode)); default: return(stream.ReadNullableIntLengthPrefixedByteArray()); } } case TdsDataType.TDS_DECN: case TdsDataType.TDS_NUMN: { var precision = format.Precision ?? 1; var scale = format.Scale ?? 0; if (env.UseAseDecimal) { var aseDecimal = stream.ReadAseDecimal(precision, scale); return(aseDecimal.HasValue ? env.UseAseDecimal ? (object)aseDecimal.Value : aseDecimal.Value.ToDecimal() : DBNull.Value); } return((object)stream.ReadDecimal(precision, scale) ?? DBNull.Value); } case TdsDataType.TDS_MONEY: return(stream.ReadMoney()); case TdsDataType.TDS_SHORTMONEY: return(stream.ReadSmallMoney()); case TdsDataType.TDS_MONEYN: switch (stream.ReadByte()) { case 0: return(DBNull.Value); case 4: return(stream.ReadSmallMoney()); case 8: return(stream.ReadMoney()); } break; case TdsDataType.TDS_DATETIME: return(stream.ReadIntPartDateTime()); case TdsDataType.TDS_SHORTDATE: return(stream.ReadShortPartDateTime()); case TdsDataType.TDS_DATETIMEN: switch (stream.ReadByte()) { case 0: return(DBNull.Value); case 4: return(stream.ReadShortPartDateTime()); case 8: return(stream.ReadIntPartDateTime()); } break; case TdsDataType.TDS_DATE: return(stream.ReadDate()); case TdsDataType.TDS_DATEN: switch (stream.ReadByte()) { case 0: return(DBNull.Value); case 4: return(stream.ReadDate()); } break; case TdsDataType.TDS_TIME: return(stream.ReadTime()); case TdsDataType.TDS_TIMEN: switch (stream.ReadByte()) { case 0: return(DBNull.Value); case 4: return(stream.ReadTime()); } break; case TdsDataType.TDS_TEXT: case TdsDataType.TDS_XML: { var textPtrLen = (byte)stream.ReadByte(); if (textPtrLen == 0) { return(DBNull.Value); } var textPtr = new byte[textPtrLen]; stream.Read(textPtr, 0, textPtrLen); stream.ReadULong(); //timestamp return(stream.ReadNullableIntLengthPrefixedString(env.Encoding)); } case TdsDataType.TDS_IMAGE: { var textPtrLen = (byte)stream.ReadByte(); if (textPtrLen == 0) { return(DBNull.Value); } var textPtr = new byte[textPtrLen]; stream.Read(textPtr, 0, textPtrLen); stream.ReadULong(); //timestamp var dataLen = stream.ReadInt(); var data = new byte[dataLen]; stream.Read(data, 0, dataLen); return(data); } case TdsDataType.TDS_UNITEXT: { var textPtrLen = (byte)stream.ReadByte(); if (textPtrLen == 0) { return(DBNull.Value); } var textPtr = new byte[textPtrLen]; stream.Read(textPtr, 0, textPtrLen); stream.ReadULong(); //timestamp return(stream.ReadNullableIntLengthPrefixedString(Encoding.Unicode)); } default: Debug.Assert(false, $"Unsupported data type {format.DataType}"); break; } return(DBNull.Value); // Catch-all. }
public static void Write(object value, Stream stream, FormatItem format, Encoding enc) { switch (format.DataType) { case TdsDataType.TDS_BIT: switch (value) { case bool b: stream.WriteBool(b); break; default: stream.WriteByte(0); break; } break; case TdsDataType.TDS_INT1: stream.WriteByte(Cast <byte>(value, format)); break; //no TDS_SINT1, we will transmit as an INTN(2) case TdsDataType.TDS_INT2: stream.WriteShort(Cast <short>(value, format)); break; case TdsDataType.TDS_UINT2: stream.WriteUShort(Cast <ushort>(value, format)); break; case TdsDataType.TDS_INT4: stream.WriteInt(Cast <int>(value, format)); break; case TdsDataType.TDS_UINT4: stream.WriteUInt(Cast <uint>(value, format)); break; case TdsDataType.TDS_INT8: stream.WriteLong(Cast <long>(value, format)); break; case TdsDataType.TDS_UINT8: stream.WriteULong(Cast <ulong>(value, format)); break; case TdsDataType.TDS_INTN: switch (value) { case byte b: stream.WriteByte(1); stream.WriteByte(b); break; case sbyte sb: stream.WriteByte(2); stream.WriteShort(sb); break; case short s: stream.WriteByte(2); stream.WriteShort(s); break; case int i: stream.WriteByte(4); stream.WriteInt(i); break; case long l: stream.WriteByte(8); stream.WriteLong(l); break; //case null: default: stream.WriteByte(0); break; } break; case TdsDataType.TDS_UINTN: switch (value) { case byte b: stream.WriteByte(1); stream.WriteByte(b); break; case ushort s: stream.WriteByte(2); stream.WriteUShort(s); break; case uint i: stream.WriteByte(4); stream.WriteUInt(i); break; case ulong l: stream.WriteByte(8); stream.WriteULong(l); break; //case null: default: stream.WriteByte(0); break; } break; case TdsDataType.TDS_FLT4: stream.WriteFloat(Cast <float>(value, format)); break; case TdsDataType.TDS_FLT8: stream.WriteDouble(Cast <double>(value, format)); break; case TdsDataType.TDS_FLTN: switch (value) { case float f: stream.WriteByte(4); stream.WriteFloat(f); break; case double d: stream.WriteByte(8); stream.WriteDouble(d); break; default: stream.WriteByte(0); break; } break; case TdsDataType.TDS_VARCHAR: if (!stream.TryWriteBytePrefixedNull(value)) { stream.WriteBytePrefixedString(Cast <string>(value, format), enc); } break; case TdsDataType.TDS_LONGCHAR: if (!stream.TryWriteIntPrefixedNull(value)) { stream.WriteIntPrefixedString(Cast <string>(value, format), enc); } break; case TdsDataType.TDS_VARBINARY: case TdsDataType.TDS_BINARY: if (!stream.TryWriteBytePrefixedNull(value)) { stream.WriteBytePrefixedByteArray(Cast <byte[]>(value, format)); } break; case TdsDataType.TDS_LONGBINARY: if (!stream.TryWriteIntPrefixedNull(value)) { switch (value) { case string s: stream.WriteIntPrefixedByteArray(Encoding.Unicode.GetBytes(s)); break; case char c: stream.WriteIntPrefixedByteArray(Encoding.Unicode.GetBytes(new[] { c })); break; case byte[] ba: stream.WriteIntPrefixedByteArray(ba); break; case byte b: stream.WriteIntPrefixedByteArray(new[] { b }); break; default: stream.WriteInt(0); break; } } break; case TdsDataType.TDS_DECN: case TdsDataType.TDS_NUMN: if (!stream.TryWriteBytePrefixedNull(value)) { switch (value) { case AseDecimal ad: stream.WriteDecimal(ad); break; default: stream.WriteDecimal(Cast <decimal>(value, format)); break; } } break; case TdsDataType.TDS_DATETIME: stream.WriteIntPartDateTime(Cast <DateTime>(value, format)); break; case TdsDataType.TDS_DATETIMEN: if (!stream.TryWriteBytePrefixedNull(value)) { stream.WriteIntPartDateTime(Cast <DateTime>(value, format)); } break; case TdsDataType.TDS_DATE: stream.WriteDate(Cast <DateTime>(value, format)); break; case TdsDataType.TDS_DATEN: if (!stream.TryWriteBytePrefixedNull(value)) { stream.WriteDate(Cast <DateTime>(value, format)); } break; case TdsDataType.TDS_TIME: stream.WriteTime(Cast <TimeSpan>(value, format)); break; case TdsDataType.TDS_TIMEN: if (!stream.TryWriteBytePrefixedNull(value)) { stream.WriteTime(Cast <TimeSpan>(value, format)); } break; case TdsDataType.TDS_MONEYN: if (!stream.TryWriteBytePrefixedNull(value)) { stream.WriteMoney(Cast <decimal>(value, format)); } break; default: Debug.Assert(false, $"Unsupported data type {format.DataType}"); break; } }
public static object Read(Stream stream, FormatItem format, DbEnvironment env) { return(ReadInternal(stream, format, env) ?? DBNull.Value); }
private static object ReadTDS_INT4(Stream stream, FormatItem format, DbEnvironment env) { return(stream.ReadInt()); }
private static void WriteTDS_DATE(object value, Stream stream, FormatItem format, Encoding enc) { stream.WriteDate(Cast <DateTime>(value, format, enc)); }
private static void WriteTDS_INT1(object value, Stream stream, FormatItem format, Encoding enc) { stream.WriteByte(Cast <byte>(value, format, enc)); }
private static object ReadTDS_SHORTDATE(Stream stream, FormatItem format, DbEnvironment env) { return(stream.ReadShortPartDateTime()); }
private static void WriteTDS_FLT4(object value, Stream stream, FormatItem format, Encoding enc) { stream.WriteFloat(Cast <float>(value, format, enc)); }
private static object ReadTDS_LONGCHAR(Stream stream, FormatItem format, DbEnvironment env) { return(stream.ReadNullableIntLengthPrefixedString(env.Encoding)); }
private static object ReadTDS_SHORTMONEY(Stream stream, FormatItem format, DbEnvironment env) { return(stream.ReadSmallMoney()); }
private static object ReadTDS_VARBINARY(Stream stream, FormatItem format, DbEnvironment env) { return(stream.ReadNullableByteLengthPrefixedByteArray()); }
private static object ReadTDS_SENSITIVITY(Stream stream, FormatItem format, DbEnvironment env) { return(stream.ReadNullableByteLengthPrefixedString(env.Encoding)); }
private static object ReadTDS_FLT8(Stream stream, FormatItem format, DbEnvironment env) { return(stream.ReadDouble()); }
private static void WriteTDS_INT4(object value, Stream stream, FormatItem format, Encoding enc) { stream.WriteInt(Cast <int>(value, format, enc)); }
private static object ReadTDS_TIME(Stream stream, FormatItem format, DbEnvironment env) { return(stream.ReadTime()); }
private static void WriteTDS_UINT8(object value, Stream stream, FormatItem format, Encoding enc) { stream.WriteULong(Cast <ulong>(value, format, enc)); }
private static object ReadTDS_BIT(Stream stream, FormatItem format, DbEnvironment env) { return(stream.ReadBool()); }
private static void WriteTDS_FLT8(object value, Stream stream, FormatItem format, Encoding enc) { stream.WriteDouble(Cast <double>(value, format, enc)); }
private static object ReadTDS_SINT1(Stream stream, FormatItem format, DbEnvironment env) { return((sbyte)stream.ReadByte()); }
private static void WriteTDS_TIME(object value, Stream stream, FormatItem format, Encoding enc) { stream.WriteTime(Cast <TimeSpan>(value, format, enc)); }
private static object ReadTDS_UINT2(Stream stream, FormatItem format, DbEnvironment env) { return(stream.ReadUShort()); }
private static void WriteTDS_UINT2(object value, Stream stream, FormatItem format, Encoding enc) { stream.WriteUShort(Cast <ushort>(value, format, enc)); }
public static IToken[] BuildEncrypt3Tokens(byte[] encryptedPassword) { var pwdFormat = new FormatItem { DataType = TdsDataType.TDS_LONGBINARY, Length = 512 }; var remPwdVarcharFormat = new FormatItem { DataType = TdsDataType.TDS_VARCHAR, Length = 255, IsNullable = true }; return(new IToken[] { new MessageToken { Status = MessageToken.MsgStatus.TDS_MSG_HASARGS, MessageId = MessageToken.MsgId.TDS_MSG_SEC_LOGPWD3 }, new ParameterFormatToken { Formats = new[] { pwdFormat } }, new ParametersToken { Parameters = new[] { new ParametersToken.Parameter { Value = encryptedPassword, Format = pwdFormat } } }, new MessageToken { Status = MessageToken.MsgStatus.TDS_MSG_HASARGS, MessageId = MessageToken.MsgId.TDS_MSG_SEC_REMPWD3 }, new ParameterFormatToken { Formats = new[] { remPwdVarcharFormat, pwdFormat } }, new ParametersToken { Parameters = new[] { new ParametersToken.Parameter { Value = DBNull.Value, Format = remPwdVarcharFormat }, new ParametersToken.Parameter { Value = encryptedPassword, Format = pwdFormat } } } }); }