public static void ReadFormat(FormatToken.Format f, TdsResponseStream stream) { var t = _types[f.Type]; if (t.FormatFormat == FormatFormat.LenOneByte || t.FormatFormat == FormatFormat.Decimal) { f.Len = stream.Read(); } if (t.FormatFormat == FormatFormat.LenFourBytes || t.FormatFormat == FormatFormat.Image) { f.Len = stream.ReadInt(); } if (t.FormatFormat == FormatFormat.Image) { int nameLen = stream.ReadShort(); f.ObjectName = stream.ReadString(nameLen); } if (t.FormatFormat == FormatFormat.Decimal) { f.Pecision = stream.Read(); f.Scale = stream.Read(); } int localLen = stream.Read(); f.LocaleInfo = stream.ReadString(localLen); // todo: blob }
private TdsToken CreateFormat(TokenDescriptor tokenDesc, TdsResponseStream stream, Encoding encoder, FormatToken lastFormat) { int len; if (tokenDesc.Len == TokenLength.FourByteLen) { len = stream.ReadInt(); } else { len = stream.ReadShort(); } int numFormats = stream.ReadShort(); var formats = new List <FormatToken.Format>(); for (int i = 0; i < numFormats; i++) { var f = new FormatToken.Format { Params = tokenDesc.TokenType == TokenType.TDS_PARAMFMT || tokenDesc.TokenType == TokenType.TDS_PARAMFMT2, }; int nameLen = stream.Read(); f.Name = stream.ReadString(nameLen); if (tokenDesc.TokenType == TokenType.TDS_ROWFMT2) { int catalogNameLen = stream.Read(); f.CatalogName = stream.ReadString(catalogNameLen); int schemaNameLen = stream.Read(); f.SchemaName = stream.ReadString(schemaNameLen); int tableNameLen = stream.Read(); f.TableName = stream.ReadString(tableNameLen); int columnNameLen = stream.Read(); f.ColumnName = stream.ReadString(columnNameLen); } if (tokenDesc.Len == TokenLength.FourByteLen) { f.Status = stream.ReadInt(); } else { f.Status = stream.Read(); } f.UserType = (UserDataType)stream.ReadInt(); f.Type = (TdsType)stream.Read(); TypeReader.ReadFormat(f, stream); formats.Add(f); } return(new FormatToken(tokenDesc.TokenType) { Formats = formats, }); }
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); } }
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); }