2 // Mono.Security.BitConverterLE.cs
3 // Like System.BitConverter but always little endian
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 namespace Mono
.Security
34 internal sealed class BitConverterLE
36 private BitConverterLE ()
40 unsafe private static byte[] GetUShortBytes (byte *bytes
)
42 if (BitConverter
.IsLittleEndian
)
43 return new byte [] { bytes [0], bytes [1] }
;
45 return new byte [] { bytes [1], bytes [0] }
;
48 unsafe private static byte[] GetUIntBytes (byte *bytes
)
50 if (BitConverter
.IsLittleEndian
)
51 return new byte [] { bytes [0], bytes [1], bytes [2], bytes [3] }
;
53 return new byte [] { bytes [3], bytes [2], bytes [1], bytes [0] }
;
56 unsafe private static byte[] GetULongBytes (byte *bytes
)
58 if (BitConverter
.IsLittleEndian
)
59 return new byte [] { bytes
[0], bytes
[1], bytes
[2], bytes
[3],
60 bytes
[4], bytes
[5], bytes
[6], bytes
[7] };
62 return new byte [] { bytes
[7], bytes
[6], bytes
[5], bytes
[4],
63 bytes
[3], bytes
[2], bytes
[1], bytes
[0] };
66 unsafe internal static byte[] GetBytes (bool value)
68 return new byte [] { value ? (byte)1 : (byte)0 }
;
71 unsafe internal static byte[] GetBytes (char value)
73 return GetUShortBytes ((byte *) &value);
76 unsafe internal static byte[] GetBytes (short value)
78 return GetUShortBytes ((byte *) &value);
81 unsafe internal static byte[] GetBytes (int value)
83 return GetUIntBytes ((byte *) &value);
86 unsafe internal static byte[] GetBytes (long value)
88 return GetULongBytes ((byte *) &value);
91 unsafe internal static byte[] GetBytes (ushort value)
93 return GetUShortBytes ((byte *) &value);
96 unsafe internal static byte[] GetBytes (uint value)
98 return GetUIntBytes ((byte *) &value);
101 unsafe internal static byte[] GetBytes (ulong value)
103 return GetULongBytes ((byte *) &value);
106 unsafe internal static byte[] GetBytes (float value)
108 return GetUIntBytes ((byte *) &value);
111 unsafe internal static byte[] GetBytes (double value)
113 return GetULongBytes ((byte *) &value);
116 unsafe private static void UShortFromBytes (byte *dst
, byte[] src
, int startIndex
)
118 if (BitConverter
.IsLittleEndian
) {
119 dst
[0] = src
[startIndex
];
120 dst
[1] = src
[startIndex
+ 1];
122 dst
[0] = src
[startIndex
+ 1];
123 dst
[1] = src
[startIndex
];
127 unsafe private static void UIntFromBytes (byte *dst
, byte[] src
, int startIndex
)
129 if (BitConverter
.IsLittleEndian
) {
130 dst
[0] = src
[startIndex
];
131 dst
[1] = src
[startIndex
+ 1];
132 dst
[2] = src
[startIndex
+ 2];
133 dst
[3] = src
[startIndex
+ 3];
135 dst
[0] = src
[startIndex
+ 3];
136 dst
[1] = src
[startIndex
+ 2];
137 dst
[2] = src
[startIndex
+ 1];
138 dst
[3] = src
[startIndex
];
142 unsafe private static void ULongFromBytes (byte *dst
, byte[] src
, int startIndex
)
144 if (BitConverter
.IsLittleEndian
) {
145 for (int i
= 0; i
< 8; ++i
)
146 dst
[i
] = src
[startIndex
+ i
];
148 for (int i
= 0; i
< 8; ++i
)
149 dst
[i
] = src
[startIndex
+ (7 - i
)];
153 unsafe internal static bool ToBoolean (byte[] value, int startIndex
)
155 return value [startIndex
] != 0;
158 unsafe internal static char ToChar (byte[] value, int startIndex
)
162 UShortFromBytes ((byte *) &ret
, value, startIndex
);
167 unsafe internal static short ToInt16 (byte[] value, int startIndex
)
171 UShortFromBytes ((byte *) &ret
, value, startIndex
);
176 unsafe internal static int ToInt32 (byte[] value, int startIndex
)
180 UIntFromBytes ((byte *) &ret
, value, startIndex
);
185 unsafe internal static long ToInt64 (byte[] value, int startIndex
)
189 ULongFromBytes ((byte *) &ret
, value, startIndex
);
194 unsafe internal static ushort ToUInt16 (byte[] value, int startIndex
)
198 UShortFromBytes ((byte *) &ret
, value, startIndex
);
203 unsafe internal static uint ToUInt32 (byte[] value, int startIndex
)
207 UIntFromBytes ((byte *) &ret
, value, startIndex
);
212 unsafe internal static ulong ToUInt64 (byte[] value, int startIndex
)
216 ULongFromBytes ((byte *) &ret
, value, startIndex
);
221 unsafe internal static float ToSingle (byte[] value, int startIndex
)
225 UIntFromBytes ((byte *) &ret
, value, startIndex
);
230 unsafe internal static double ToDouble (byte[] value, int startIndex
)
234 ULongFromBytes ((byte *) &ret
, value, startIndex
);