Hi, This patch fixes BinaryReader/Writer so they always read/write doubles, floats and decimals in little endian format, even in non-little-endian platforms. The other data types are already being written in little endian format.
OK to commit? Lluis.
Index: BinaryReader.cs =================================================================== --- BinaryReader.cs (revision 41899) +++ BinaryReader.cs (working copy) @@ -32,6 +32,7 @@ using System; using System.Text; using System.Globalization; +using Mono.Security; namespace System.IO { public class BinaryReader : IDisposable { @@ -348,29 +349,47 @@ decimal ret; byte* ret_ptr = (byte *)&ret; - for (int i = 0; i < 16; i++) { - - /* - * internal representation of decimal is - * ss32, hi32, lo32, mi32, - * but in stream it is - * lo32, mi32, hi32, ss32 - * So we have to rerange this int32 values - */ - - if (i < 4) { - // lo 8 - 12 - ret_ptr [i + 8] = m_buffer [i]; - } else if (i < 8) { - // mid 12 - 16 - ret_ptr [i + 8] = m_buffer [i]; - } else if (i < 12) { - // hi 4 - 8 - ret_ptr [i - 4] = m_buffer [i]; - } else if (i < 16) { - // ss 0 - 4 - ret_ptr [i - 12] = m_buffer [i]; - } + + /* + * internal representation of decimal is + * ss32, hi32, lo32, mi32, + * but in stream it is + * lo32, mi32, hi32, ss32 + * So we have to rerange this int32 values + */ + + if (BitConverter.IsLittleEndian) { + for (int i = 0; i < 16; i++) { + if (i < 4) { + // lo 8 - 12 + ret_ptr [i + 8] = m_buffer [i]; + } else if (i < 8) { + // mid 12 - 16 + ret_ptr [i + 8] = m_buffer [i]; + } else if (i < 12) { + // hi 4 - 8 + ret_ptr [i - 4] = m_buffer [i]; + } else if (i < 16) { + // ss 0 - 4 + ret_ptr [i - 12] = m_buffer [i]; + } + } + } else { + for (int i = 0; i < 16; i++) { + if (i < 4) { + // lo 8 - 12 + ret_ptr [11 - i] = m_buffer [i]; + } else if (i < 8) { + // mid 12 - 16 + ret_ptr [19 - i] = m_buffer [i]; + } else if (i < 12) { + // hi 4 - 8 + ret_ptr [15 - i] = m_buffer [i]; + } else if (i < 16) { + // ss 0 - 4 + ret_ptr [15 - i] = m_buffer [i]; + } + } } return ret; @@ -379,7 +398,7 @@ public virtual double ReadDouble() { FillBuffer(8); - return(BitConverter.ToDouble(m_buffer, 0)); + return(BitConverterLE.ToDouble(m_buffer, 0)); } public virtual short ReadInt16() { @@ -433,7 +452,7 @@ public virtual float ReadSingle() { FillBuffer(4); - return(BitConverter.ToSingle(m_buffer, 0)); + return(BitConverterLE.ToSingle(m_buffer, 0)); } [CLSCompliant(false)] Index: BinaryWriter.cs =================================================================== --- BinaryWriter.cs (revision 41899) +++ BinaryWriter.cs (working copy) @@ -31,6 +31,7 @@ using System; using System.Text; using System.Globalization; +using Mono.Security; namespace System.IO { [Serializable] @@ -170,20 +171,34 @@ throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter"); byte* value_ptr = (byte *)&value; - for (int i = 0; i < 16; i++) { - - /* - * decimal in stream is lo32, mi32, hi32, ss32 - * but its internal structure si ss32, hi32, lo32, mi32 - */ - if (i < 4) - buffer [i + 12] = value_ptr [i]; - else if (i < 8) - buffer [i + 4] = value_ptr [i]; - else if (i < 12) - buffer [i - 8] = value_ptr [i]; - else - buffer [i - 8] = value_ptr [i]; + + /* + * decimal in stream is lo32, mi32, hi32, ss32 + * but its internal structure si ss32, hi32, lo32, mi32 + */ + + if (BitConverter.IsLittleEndian) { + for (int i = 0; i < 16; i++) { + if (i < 4) + buffer [i + 12] = value_ptr [i]; + else if (i < 8) + buffer [i + 4] = value_ptr [i]; + else if (i < 12) + buffer [i - 8] = value_ptr [i]; + else + buffer [i - 8] = value_ptr [i]; + } + } else { + for (int i = 0; i < 16; i++) { + if (i < 4) + buffer [15 - i] = value_ptr [i]; + else if (i < 8) + buffer [15 - i] = value_ptr [i]; + else if (i < 12) + buffer [11 - i] = value_ptr [i]; + else + buffer [19 - i] = value_ptr [i]; + } } OutStream.Write(buffer, 0, 16); @@ -194,7 +209,7 @@ if (disposed) throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter"); - OutStream.Write(BitConverter.GetBytes(value), 0, 8); + OutStream.Write(BitConverterLE.GetBytes(value), 0, 8); } public virtual void Write(short value) { @@ -244,7 +259,7 @@ if (disposed) throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter"); - OutStream.Write(BitConverter.GetBytes(value), 0, 4); + OutStream.Write(BitConverterLE.GetBytes(value), 0, 4); } byte [] stringBuffer; Index: ChangeLog =================================================================== --- ChangeLog (revision 41899) +++ ChangeLog (working copy) @@ -1,3 +1,8 @@ +2005-03-16 Lluis Sanchez Gual <[EMAIL PROTECTED]> + + * BinaryReader.cs, BinaryWriter.cs: Read/write dobules, floats and + decimals in little endian format. + 2005-03-15 Sebastien Pouliot <[EMAIL PROTECTED]> * FileStream.cs: Anonymize part of the path when exceptions are throw