/// <summary> /// Used to unpack IPackageable from a stream. /// </summary> /// <param name="stream">MemoryStream from which to unpack IPackageable.</param> /// <returns>Returns true if successful.</returns> public override bool Unpack(MemoryStream stream) { LittleEndianUtilities.ReadUShort(stream); this.Number = (int)LittleEndianUtilities.ReadUInt(stream); this.State = Convert.ToByte(stream.ReadByte()); this.Class = Convert.ToByte(stream.ReadByte()); int length = LittleEndianUtilities.ReadUShort(stream) * 2; var buffer = new byte[length]; stream.Read(buffer, 0, length); this.MsgText = Encoding.Unicode.GetString(buffer); length = stream.ReadByte() * 2; buffer = new byte[length]; stream.Read(buffer, 0, length); this.ServerName = Encoding.Unicode.GetString(buffer); length = stream.ReadByte() * 2; buffer = new byte[length]; stream.Read(buffer, 0, length); this.ProcName = Encoding.Unicode.GetString(buffer); this.LineNumber = LittleEndianUtilities.ReadUInt(stream); return(true); }
public bool Unpack(MemoryStream stream) { HostNamePosition = LittleEndianUtilities.ReadUShort(stream); HostNameLength = LittleEndianUtilities.ReadUShort(stream); UserNamePosition = LittleEndianUtilities.ReadUShort(stream); UserNameLength = LittleEndianUtilities.ReadUShort(stream); PasswordPosition = LittleEndianUtilities.ReadUShort(stream); PasswordLength = LittleEndianUtilities.ReadUShort(stream); AppNamePosition = LittleEndianUtilities.ReadUShort(stream); AppNameLength = LittleEndianUtilities.ReadUShort(stream); ServerNamePosition = LittleEndianUtilities.ReadUShort(stream); ServerNameLength = LittleEndianUtilities.ReadUShort(stream); ExtensionPosition = LittleEndianUtilities.ReadUShort(stream); ExtensionLength = LittleEndianUtilities.ReadUShort(stream); CltIntNamePosition = LittleEndianUtilities.ReadUShort(stream); CltIntNameLength = LittleEndianUtilities.ReadUShort(stream); LanguagePosition = LittleEndianUtilities.ReadUShort(stream); LanguageLength = LittleEndianUtilities.ReadUShort(stream); DatabasePosition = LittleEndianUtilities.ReadUShort(stream); DatabaseLength = LittleEndianUtilities.ReadUShort(stream); stream.Read(ClientID, 0, 6); SSPIPosition = LittleEndianUtilities.ReadUShort(stream); SSPILength = LittleEndianUtilities.ReadUShort(stream); AtchDBFilePosition = LittleEndianUtilities.ReadUShort(stream); AtchDBFileLength = LittleEndianUtilities.ReadUShort(stream); ChangePasswordPosition = LittleEndianUtilities.ReadUShort(stream); ChangePasswordLength = LittleEndianUtilities.ReadUShort(stream); SSPILengthLong = LittleEndianUtilities.ReadUInt(stream); return(true); }
/// <summary> /// Skips a token within a stream based on token type /// </summary> /// <param name="tokenType">Type of the token to ignore</param> /// <param name="stream">Stream that contains the token</param> private static void IgnoreToken(TDSTokenType tokenType, MemoryStream stream) { switch (((byte)tokenType >> 4) & 0x3) { // Variable count token case 0: { throw new NotSupportedException(); } // Zero length token case 1: { return; } // Variable length token case 2: { if (tokenType == TDSTokenType.DataClassification) { throw new NotSupportedException(); } ushort length = LittleEndianUtilities.ReadUShort(stream); for (int i = 0; i < length; i++) { stream.ReadByte(); } return; } // Fixed length token case 3: { var bytesToRead = Math.Pow(2, ((byte)tokenType >> 2) & 0x3); if (tokenType == TDSTokenType.Done || tokenType == TDSTokenType.DoneInProc || tokenType == TDSTokenType.DoneProc) { bytesToRead = 12; // Untill support is added } for (int i = 0; i < bytesToRead; i++) { stream.ReadByte(); } return; } default: { throw new InvalidOperationException(); } } }
public void ReadUShortFromMemoryStream() { var stream = new MemoryStream(); stream.Write(new byte[] { 0x01, 0x23 }); stream.Seek(0, SeekOrigin.Begin); var res = LittleEndianUtilities.ReadUShort(stream); Assert.AreEqual(0x2301, res); }
/// <summary> /// Used to unpack IPackageable from a stream. /// </summary> /// <param name="stream">MemoryStream containing the object that needs to be unpacked.</param> /// <returns>Returns true if successful.</returns> public override bool Unpack(MemoryStream stream) { var length = LittleEndianUtilities.ReadUShort(stream); this.Type = (TDSEnvChangeType)stream.ReadByte(); switch (this.Type) { case TDSEnvChangeType.Routing: var routingDataValueLength = LittleEndianUtilities.ReadUShort(stream); if (routingDataValueLength == 0 || stream.ReadByte() != 0) { throw new InvalidOperationException(); } var protocolProperty = LittleEndianUtilities.ReadUShort(stream); if (protocolProperty == 0) { throw new InvalidOperationException(); } int strLength = LittleEndianUtilities.ReadUShort(stream) * 2; var temp = new byte[strLength]; stream.Read(temp, 0, strLength); this.Values["ProtocolProperty"] = string.Format("{0}", protocolProperty); this.Values["AlternateServer"] = Encoding.Unicode.GetString(temp); for (int i = 0; i < length - routingDataValueLength - sizeof(byte) - sizeof(ushort); i++) { // Ignore oldValue stream.ReadByte(); } break; default: { for (int i = 0; i < length - sizeof(byte); i++) { // Ignore unsupported types stream.ReadByte(); } return(false); } } return(true); }
/// <summary> /// Reads TDS Login7 options from a stream /// </summary> /// <param name="stream">Stream containing the Login7 packet</param> /// <returns>ClientID, TDS Login7 options pair</returns> public static Tuple <List <TDSLogin7Option>, byte[]> ReadOptionsFromStream(MemoryStream stream) { var options = new List <TDSLogin7Option>(); var clientID = new byte[6]; foreach (var option in OptionOrder) { ushort position = 0; ushort length = 0; switch (option) { case "HostName": case "UserName": case "AppName": case "ServerName": case "CltIntName": case "Language": case "Database": { position = LittleEndianUtilities.ReadUShort(stream); length = LittleEndianUtilities.ReadUShort(stream); if (length != 0) { options.Add(new TDSLogin7TextOption(option, position, length)); } break; } case "ClientID": { stream.Read(clientID, 0, 6); break; } case "Password": case "ChangePassword": { position = LittleEndianUtilities.ReadUShort(stream); length = LittleEndianUtilities.ReadUShort(stream); if (length != 0) { options.Add(new TDSLogin7PasswordOption(option, position, length)); } break; } default: { position = LittleEndianUtilities.ReadUShort(stream); length = LittleEndianUtilities.ReadUShort(stream); if (length != 0) { throw new NotSupportedException(); } break; } } } // Ignore LongSSPI, not supported LittleEndianUtilities.ReadUInt(stream); options = options.OrderBy(opt => opt.Position).ToList(); foreach (var option in options) { option.Unpack(stream); } return(new Tuple <List <TDSLogin7Option>, byte[]>(options, clientID)); }