corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
4 *
5 * - Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 using System;
28 using System.IO;
29  
30 namespace OpenMetaverse.Rendering
31 {
32 /// <summary>
33 /// Binary reader, which is endian aware
34 /// </summary>
35 public class EndianAwareBinaryReader : BinaryReader
36 {
37 /// What is the format of the source file
38 public enum SourceFormat
39 {
40 BigEndian, //!< The stream is big endian, SPARC, Arm and friends
41 LittleEndian //!< x86 and friends
42 }
43  
44 private byte[] m_a16 = new byte[2]; //!< Temporary storage area for 2 byte values
45 private byte[] m_a32 = new byte[4]; //!< Temporary storage area for 4 byte values
46 private byte[] m_a64 = new byte[8]; //!< Temporary storage area for 8 byte values
47  
48 private readonly bool m_shouldReverseOrder; //!< true if the file is in a different endian format than the system
49  
50 /// <summary>
51 /// Construct a reader from a stream
52 /// </summary>
53 /// <param name="stream">The stream to read from</param>
54 public EndianAwareBinaryReader(Stream stream)
55 : this(stream, SourceFormat.LittleEndian) {}
56  
57 /// <summary>
58 /// Construct a reader from a stream
59 /// </summary>
60 /// <param name="stream">The stream to read from</param>
61 /// <param name="format">What is the format of the file, assumes PC and similar architecture</param>
62 public EndianAwareBinaryReader(Stream stream, SourceFormat format)
63 : base(stream)
64 {
65 if ((format == SourceFormat.BigEndian && BitConverter.IsLittleEndian) ||
66 (format == SourceFormat.LittleEndian && !BitConverter.IsLittleEndian))
67 m_shouldReverseOrder = true;
68 }
69  
70 /// <summary>
71 /// Read a 32 bit integer
72 /// </summary>
73 /// <returns>A 32 bit integer in the system's endianness</returns>
74 public override int ReadInt32()
75 {
76 m_a32 = base.ReadBytes(4);
77 if (m_shouldReverseOrder)
78 Array.Reverse(m_a32);
79 return BitConverter.ToInt32(m_a32, 0);
80 }
81  
82 /// <summary>
83 /// Read a 16 bit integer
84 /// </summary>
85 /// <returns>A 16 bit integer in the system's endianness</returns>
86 public override Int16 ReadInt16()
87 {
88 m_a16 = base.ReadBytes(2);
89 if (m_shouldReverseOrder)
90 Array.Reverse(m_a16);
91 return BitConverter.ToInt16(m_a16, 0);
92 }
93  
94 /// <summary>
95 /// Read a 64 bit integer
96 /// </summary>
97 /// <returns>A 64 bit integer in the system's endianness</returns>
98 public override Int64 ReadInt64()
99 {
100 m_a64 = base.ReadBytes(8);
101 if (m_shouldReverseOrder)
102 Array.Reverse(m_a64);
103 return BitConverter.ToInt64(m_a64, 0);
104 }
105  
106 /// <summary>
107 /// Read an unsigned 32 bit integer
108 /// </summary>
109 /// <returns>A 32 bit unsigned integer in the system's endianness</returns>
110 public override UInt32 ReadUInt32()
111 {
112 m_a32 = base.ReadBytes(4);
113 if (m_shouldReverseOrder)
114 Array.Reverse(m_a32);
115 return BitConverter.ToUInt32(m_a32, 0);
116 }
117  
118 /// <summary>
119 /// Read a single precision floating point value
120 /// </summary>
121 /// <returns>A single precision floating point value in the system's endianness</returns>
122 public override float ReadSingle()
123 {
124 m_a32 = base.ReadBytes(4);
125 if (m_shouldReverseOrder)
126 Array.Reverse(m_a32);
127 return BitConverter.ToSingle(m_a32, 0);
128 }
129  
130 /// <summary>
131 /// Read a double precision floating point value
132 /// </summary>
133 /// <returns>A double precision floating point value in the system's endianness</returns>
134 public override double ReadDouble()
135 {
136 m_a64 = base.ReadBytes(8);
137 if (m_shouldReverseOrder)
138 Array.Reverse(m_a64);
139 return BitConverter.ToDouble(m_a64, 0);
140 }
141  
142 /// <summary>
143 /// Read a UTF-8 string
144 /// </summary>
145 /// <returns>A standard system string</returns>
146 public override string ReadString()
147 {
148 using (MemoryStream ms = new MemoryStream())
149 {
150 byte b = ReadByte();
151 while (b != 0)
152 {
153 ms.WriteByte(b);
154 b = ReadByte();
155 }
156 return System.Text.Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position);
157 }
158 }
159  
160 /// <summary>
161 /// Read a UTF-8 string
162 /// </summary>
163 /// <param name="size">length of string to read</param>
164 /// <returns>A standard system string</returns>
165 public string ReadString(int size)
166 {
167 byte[] buffer = ReadBytes(size);
168 return System.Text.Encoding.UTF8.GetString(buffer).Trim();
169 }
170 }
171 }