private static object GetDecimalValue(TdsResponseStream stream, TdsType type, FormatToken.Format f) { int len = stream.Read(); if (len > 0) { var scale = Math.Pow(10, f.Scale ?? 0); bool sign = stream.Read() != 0; var bytes = stream.ReadBytes(len - 1); var bi = new BigInteger(bytes.Reverse().ToArray()); var d = (decimal)bi; if (sign) { d = -d; } return(d / (decimal)scale); } else { return(null); } }
private TdsToken CreateLoginAck(TokenDescriptor tokenDesc, TdsResponseStream stream, Encoding encoder, FormatToken lastFormat) { int len = stream.ReadShort(); int status = stream.Read(); var version = stream.ReadBytes(4); int nameLen = stream.Read(); var name = stream.ReadString(nameLen); var progVersion = stream.ReadBytes(4); return(new LoginAckToken(tokenDesc.TokenType) { Succeed = status == 5, Fail = status == 6, Negotiate = status == 7, TdsVersion = $"{version[0]}.{version[1]}.{version[2]}.{version[3]}", ServerProgram = name, ServerVersion = $"{progVersion[0]}.{progVersion[1]}.{progVersion[2]}.{progVersion[3]}", }); }
private TdsToken CreateTextMessage(TokenDescriptor tokenDesc, TdsResponseStream stream, Encoding encoder, FormatToken lastFormat) { int len = stream.ReadShort(); int msgNumber = stream.ReadInt(); int state = stream.Read(); int severity = stream.Read(); int sqlStatusLen = stream.Read(); var sqlStatus = stream.ReadBytes(sqlStatusLen); int status = stream.Read(); int tranState = stream.ReadShort(); // discarded int msgLen = stream.ReadShort(); var msg = stream.ReadString(msgLen); int srvNameLen = stream.Read(); var serverName = stream.ReadString(srvNameLen); int procNameLen = stream.Read(); var procName = stream.ReadString(procNameLen); int lineNumber = stream.ReadShort(); return(new TextMessageToken(tokenDesc.TokenType) { MessageNumber = msgNumber, State = state, Severity = severity, ParamsFollows = status == 1, Message = msg, ServerName = serverName, ProcName = procName, LineNumber = lineNumber, }); }
public static object ReadData(FormatToken.Format f, TdsResponseStream stream, Encoding encoder) { int len; switch (f.Type) { case TdsType.TDS_INTN: case TdsType.TDS_UINTN: // lets ignore unsigned for now switch (stream.Read()) { case 1: return(stream.Read()); case 2: return(stream.ReadShort()); case 4: return(stream.ReadInt()); case 8: return(stream.ReadLong()); } break; case TdsType.TDS_INT1: case TdsType.TDS_UINT1: return(stream.Read()); case TdsType.TDS_INT2: case TdsType.TDS_UINT2: return(stream.ReadShort()); case TdsType.TDS_INT4: case TdsType.TDS_UINT4: return(stream.ReadInt()); case TdsType.TDS_INT8: case TdsType.TDS_UINT8: return(stream.ReadLong()); case TdsType.TDS_BIT: return(stream.Read() != 0); case TdsType.TDS_DATETIMEN: case TdsType.TDS_DATETIME: case TdsType.TDS_SHORTDATE: return(GetDatetimeValue(stream, f.Type)); case TdsType.TDS_FLT4: return(BitConverter.Int32BitsToSingle(stream.ReadInt())); case TdsType.TDS_FLT8: return(BitConverter.Int64BitsToDouble(stream.ReadLong())); case TdsType.TDS_FLTN: len = stream.Read(); if (len == 4) { return(BitConverter.Int32BitsToSingle(stream.ReadInt())); } else if (len == 8) { return(BitConverter.Int64BitsToDouble(stream.ReadLong())); } break; case TdsType.TDS_SHORTMONEY: case TdsType.TDS_MONEY: case TdsType.TDS_MONEYN: return(GetMoneyValue(stream, f.Type)); case TdsType.TDS_DECN: case TdsType.TDS_NUMN: return(GetDecimalValue(stream, f.Type, f)); case TdsType.TDS_LONGBINARY: len = stream.ReadInt(); return(stream.ReadBytes(len)); case TdsType.TDS_LONGCHAR: len = stream.ReadInt(); if (len > 0) { return(encoder.GetString(stream.ReadBytes(len))); } break; case TdsType.TDS_IMAGE: len = stream.Read(); if (len > 0) { var txtPtr = stream.ReadBytes(len); var timeStamp = stream.ReadBytes(8); int dataLen = stream.ReadInt(); return(stream.ReadBytes(dataLen)); } break; case TdsType.TDS_TEXT: len = stream.Read(); if (len > 0) { var txtPtr = stream.ReadBytes(len); var timeStamp = stream.ReadBytes(8); int dataLen = stream.ReadInt(); return(encoder.GetString(stream.ReadBytes(dataLen))); } break; case TdsType.TDS_CHAR: case TdsType.TDS_VARCHAR: len = stream.Read(); if (len > 0) { return(encoder.GetString(stream.ReadBytes(len))); } break; case TdsType.TDS_BINARY: case TdsType.TDS_VARBINARY: case TdsType.TDS_BOUNDARY: case TdsType.TDS_SENSITIVITY: len = stream.Read(); return(stream.ReadBytes(len)); case TdsType.TDS_VOID: case TdsType.TDS_BLOB: default: throw new NotImplementedException(); } return(null); }