2 // Mono.Security.BitConverterLE.cs
3 // Like System.BitConverter but always little endian
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 namespace Mono
.Security
36 internal sealed class BitConverterLE
38 private BitConverterLE ()
42 unsafe private static byte[] GetUShortBytes (byte *bytes
)
44 if (BitConverter
.IsLittleEndian
)
45 return new byte [] { bytes [0], bytes [1] }
;
47 return new byte [] { bytes [1], bytes [0] }
;
50 unsafe private static byte[] GetUIntBytes (byte *bytes
)
52 if (BitConverter
.IsLittleEndian
)
53 return new byte [] { bytes [0], bytes [1], bytes [2], bytes [3] }
;
55 return new byte [] { bytes [3], bytes [2], bytes [1], bytes [0] }
;
58 unsafe private static byte[] GetULongBytes (byte *bytes
)
60 if (BitConverter
.IsLittleEndian
)
61 return new byte [] { bytes
[0], bytes
[1], bytes
[2], bytes
[3],
62 bytes
[4], bytes
[5], bytes
[6], bytes
[7] };
64 return new byte [] { bytes
[7], bytes
[6], bytes
[5], bytes
[4],
65 bytes
[3], bytes
[2], bytes
[1], bytes
[0] };
68 unsafe internal static byte[] GetBytes (bool value)
70 return new byte [] { value ? (byte)1 : (byte)0 }
;
73 unsafe internal static byte[] GetBytes (char value)
75 return GetUShortBytes ((byte *) &value);
78 unsafe internal static byte[] GetBytes (short value)
80 return GetUShortBytes ((byte *) &value);
83 unsafe internal static byte[] GetBytes (int value)
85 return GetUIntBytes ((byte *) &value);
88 unsafe internal static byte[] GetBytes (long value)
90 return GetULongBytes ((byte *) &value);
93 unsafe internal static byte[] GetBytes (ushort value)
95 return GetUShortBytes ((byte *) &value);
98 unsafe internal static byte[] GetBytes (uint value)
100 return GetUIntBytes ((byte *) &value);
103 unsafe internal static byte[] GetBytes (ulong value)
105 return GetULongBytes ((byte *) &value);
108 unsafe internal static byte[] GetBytes (float value)
110 return GetUIntBytes ((byte *) &value);
113 unsafe internal static byte[] GetBytes (double value)
115 return GetULongBytes ((byte *) &value);
118 unsafe private static void UShortFromBytes (byte *dst
, byte[] src
, int startIndex
)
120 if (BitConverter
.IsLittleEndian
) {
121 dst
[0] = src
[startIndex
];
122 dst
[1] = src
[startIndex
+ 1];
124 dst
[0] = src
[startIndex
+ 1];
125 dst
[1] = src
[startIndex
];
129 unsafe private static void UIntFromBytes (byte *dst
, byte[] src
, int startIndex
)
131 if (BitConverter
.IsLittleEndian
) {
132 dst
[0] = src
[startIndex
];
133 dst
[1] = src
[startIndex
+ 1];
134 dst
[2] = src
[startIndex
+ 2];
135 dst
[3] = src
[startIndex
+ 3];
137 dst
[0] = src
[startIndex
+ 3];
138 dst
[1] = src
[startIndex
+ 2];
139 dst
[2] = src
[startIndex
+ 1];
140 dst
[3] = src
[startIndex
];
144 unsafe private static void ULongFromBytes (byte *dst
, byte[] src
, int startIndex
)
146 if (BitConverter
.IsLittleEndian
) {
147 for (int i
= 0; i
< 8; ++i
)
148 dst
[i
] = src
[startIndex
+ i
];
150 for (int i
= 0; i
< 8; ++i
)
151 dst
[i
] = src
[startIndex
+ (7 - i
)];
155 unsafe internal static bool ToBoolean (byte[] value, int startIndex
)
157 return value [startIndex
] != 0;
160 unsafe internal static char ToChar (byte[] value, int startIndex
)
164 UShortFromBytes ((byte *) &ret
, value, startIndex
);
169 unsafe internal static short ToInt16 (byte[] value, int startIndex
)
173 UShortFromBytes ((byte *) &ret
, value, startIndex
);
178 unsafe internal static int ToInt32 (byte[] value, int startIndex
)
182 UIntFromBytes ((byte *) &ret
, value, startIndex
);
187 unsafe internal static long ToInt64 (byte[] value, int startIndex
)
191 ULongFromBytes ((byte *) &ret
, value, startIndex
);
196 unsafe internal static ushort ToUInt16 (byte[] value, int startIndex
)
200 UShortFromBytes ((byte *) &ret
, value, startIndex
);
205 unsafe internal static uint ToUInt32 (byte[] value, int startIndex
)
209 UIntFromBytes ((byte *) &ret
, value, startIndex
);
214 unsafe internal static ulong ToUInt64 (byte[] value, int startIndex
)
218 ULongFromBytes ((byte *) &ret
, value, startIndex
);
223 unsafe internal static float ToSingle (byte[] value, int startIndex
)
227 UIntFromBytes ((byte *) &ret
, value, startIndex
);
232 unsafe internal static double ToDouble (byte[] value, int startIndex
)
236 ULongFromBytes ((byte *) &ret
, value, startIndex
);