public void Pack(MemoryStream stream)
 {
     LittleEndianUtilities.WriteUShort(stream, HostNamePosition);
     LittleEndianUtilities.WriteUShort(stream, HostNameLength);
     LittleEndianUtilities.WriteUShort(stream, UserNamePosition);
     LittleEndianUtilities.WriteUShort(stream, UserNameLength);
     LittleEndianUtilities.WriteUShort(stream, PasswordPosition);
     LittleEndianUtilities.WriteUShort(stream, PasswordLength);
     LittleEndianUtilities.WriteUShort(stream, AppNamePosition);
     LittleEndianUtilities.WriteUShort(stream, AppNameLength);
     LittleEndianUtilities.WriteUShort(stream, ServerNamePosition);
     LittleEndianUtilities.WriteUShort(stream, ServerNameLength);
     LittleEndianUtilities.WriteUShort(stream, 0); // Extension unsupported
     LittleEndianUtilities.WriteUShort(stream, 0); // Extension unsupported
     LittleEndianUtilities.WriteUShort(stream, CltIntNamePosition);
     LittleEndianUtilities.WriteUShort(stream, CltIntNameLength);
     LittleEndianUtilities.WriteUShort(stream, LanguagePosition);
     LittleEndianUtilities.WriteUShort(stream, LanguageLength);
     LittleEndianUtilities.WriteUShort(stream, DatabasePosition);
     LittleEndianUtilities.WriteUShort(stream, DatabaseLength);
     stream.Write(ClientID, 0, 6);
     LittleEndianUtilities.WriteUShort(stream, SSPIPosition);
     LittleEndianUtilities.WriteUShort(stream, SSPILength);
     LittleEndianUtilities.WriteUShort(stream, AtchDBFilePosition);
     LittleEndianUtilities.WriteUShort(stream, AtchDBFileLength);
     LittleEndianUtilities.WriteUShort(stream, ChangePasswordPosition);
     LittleEndianUtilities.WriteUShort(stream, ChangePasswordLength);
     LittleEndianUtilities.WriteUInt(stream, 0); // Long SSPI not supported
 }
        public void WriteUShortToMemoryStream()
        {
            var stream = new MemoryStream();

            LittleEndianUtilities.WriteUShort(stream, 0x0123);

            CollectionAssert.AreEqual(new byte[] { 0x23, 0x01 }, stream.ToArray());
        }
        /// <summary>
        /// Writes TDS Login7 options to a stream
        /// </summary>
        /// <param name="stream">MemoryStream to write to</param>
        /// <param name="options">Options to write to the stream</param>
        /// <param name="clientID">ClientID to write to the stream</param>
        public static void WriteOptionsToStream(MemoryStream stream, List <TDSLogin7Option> options, byte[] clientID)
        {
            ushort currentPos = 94;

            foreach (var option in OptionOrder)
            {
                if (option != "ClientID")
                {
                    LittleEndianUtilities.WriteUShort(stream, currentPos);

                    var tmp = options.Where(o => o.Name == option);
                    if (tmp.Any())
                    {
                        var opt = tmp.First();

                        LittleEndianUtilities.WriteUShort(stream, opt.Length);
                        opt.Position = currentPos;
                        currentPos  += opt.TrueLength;
                    }
                    else
                    {
                        LittleEndianUtilities.WriteUShort(stream, 0);
                    }
                }
                else
                {
                    stream.Write(clientID, 0, 6);
                }
            }

            // Ignore LongSSPI, not supported
            LittleEndianUtilities.WriteUInt(stream, 0);

            options = options.OrderBy(opt => opt.Position).ToList();

            foreach (var option in options)
            {
                option.Pack(stream);
            }
        }