Example #1
0
        /// <summary>
        /// Note: this doesn't keep the SecureString secure.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="secure"></param>
        public static void Write(this BinaryWriter writer, SecureString secure)
        {
            var bwx = new BinaryWriterEx(writer.BaseStream);

            byte[] utf16 = new byte[secure.Length * 2];

            var ptr = Marshal.SecureStringToBSTR(secure);
            var len = Marshal.ReadInt32(ptr, -4);

            for (int i = 0; i < len; i += 2)
            {
                utf16[i] = Marshal.ReadByte(ptr, i);
            }

            Marshal.ZeroFreeBSTR(ptr);

            byte[] utf8 = UTF8Encoding.Convert(Encoding.Unicode, Encoding.UTF8, utf16);

            for (int i = 0; i < utf16.Length; i++)
            {
                utf16[i] = 0; // clear memory
            }

            bwx.Write7BitEncodedInt(utf8.Length);
            for (int i = 0; i < utf8.Length; i++)
            {
                bwx.Write(utf8[i]);
                utf8[i] = 0;
            }
        }
Example #2
0
 private static void WriteParameters(BinaryWriterEx writer, d3dx_parameter[] parameters, int count)
 {
     writer.Write7BitEncodedInt(count);
     for (var i = 0; i < count; i++)
     {
         WriteParameter(writer, parameters[i]);
     }
 }