BitConverterLE.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // Mono.Security.BitConverterLE.cs
  3. // Like System.BitConverter but always little endian
  4. //
  5. // Author:
  6. // Bernie Solomon
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. namespace Emby.Common.Implementations.Security
  30. {
  31. internal sealed class BitConverterLE
  32. {
  33. private BitConverterLE ()
  34. {
  35. }
  36. unsafe private static byte[] GetUShortBytes (byte *bytes)
  37. {
  38. if (BitConverter.IsLittleEndian)
  39. return new byte [] { bytes [0], bytes [1] };
  40. else
  41. return new byte [] { bytes [1], bytes [0] };
  42. }
  43. unsafe private static byte[] GetUIntBytes (byte *bytes)
  44. {
  45. if (BitConverter.IsLittleEndian)
  46. return new byte [] { bytes [0], bytes [1], bytes [2], bytes [3] };
  47. else
  48. return new byte [] { bytes [3], bytes [2], bytes [1], bytes [0] };
  49. }
  50. unsafe private static byte[] GetULongBytes (byte *bytes)
  51. {
  52. if (BitConverter.IsLittleEndian)
  53. return new byte [] { bytes [0], bytes [1], bytes [2], bytes [3],
  54. bytes [4], bytes [5], bytes [6], bytes [7] };
  55. else
  56. return new byte [] { bytes [7], bytes [6], bytes [5], bytes [4],
  57. bytes [3], bytes [2], bytes [1], bytes [0] };
  58. }
  59. unsafe internal static byte[] GetBytes (bool value)
  60. {
  61. return new byte [] { value ? (byte)1 : (byte)0 };
  62. }
  63. unsafe internal static byte[] GetBytes (char value)
  64. {
  65. return GetUShortBytes ((byte *) &value);
  66. }
  67. unsafe internal static byte[] GetBytes (short value)
  68. {
  69. return GetUShortBytes ((byte *) &value);
  70. }
  71. unsafe internal static byte[] GetBytes (int value)
  72. {
  73. return GetUIntBytes ((byte *) &value);
  74. }
  75. unsafe internal static byte[] GetBytes (long value)
  76. {
  77. return GetULongBytes ((byte *) &value);
  78. }
  79. unsafe internal static byte[] GetBytes (ushort value)
  80. {
  81. return GetUShortBytes ((byte *) &value);
  82. }
  83. unsafe internal static byte[] GetBytes (uint value)
  84. {
  85. return GetUIntBytes ((byte *) &value);
  86. }
  87. unsafe internal static byte[] GetBytes (ulong value)
  88. {
  89. return GetULongBytes ((byte *) &value);
  90. }
  91. unsafe internal static byte[] GetBytes (float value)
  92. {
  93. return GetUIntBytes ((byte *) &value);
  94. }
  95. unsafe internal static byte[] GetBytes (double value)
  96. {
  97. return GetULongBytes ((byte *) &value);
  98. }
  99. unsafe private static void UShortFromBytes (byte *dst, byte[] src, int startIndex)
  100. {
  101. if (BitConverter.IsLittleEndian) {
  102. dst [0] = src [startIndex];
  103. dst [1] = src [startIndex + 1];
  104. } else {
  105. dst [0] = src [startIndex + 1];
  106. dst [1] = src [startIndex];
  107. }
  108. }
  109. unsafe private static void UIntFromBytes (byte *dst, byte[] src, int startIndex)
  110. {
  111. if (BitConverter.IsLittleEndian) {
  112. dst [0] = src [startIndex];
  113. dst [1] = src [startIndex + 1];
  114. dst [2] = src [startIndex + 2];
  115. dst [3] = src [startIndex + 3];
  116. } else {
  117. dst [0] = src [startIndex + 3];
  118. dst [1] = src [startIndex + 2];
  119. dst [2] = src [startIndex + 1];
  120. dst [3] = src [startIndex];
  121. }
  122. }
  123. unsafe private static void ULongFromBytes (byte *dst, byte[] src, int startIndex)
  124. {
  125. if (BitConverter.IsLittleEndian) {
  126. for (int i = 0; i < 8; ++i)
  127. dst [i] = src [startIndex + i];
  128. } else {
  129. for (int i = 0; i < 8; ++i)
  130. dst [i] = src [startIndex + (7 - i)];
  131. }
  132. }
  133. unsafe internal static bool ToBoolean (byte[] value, int startIndex)
  134. {
  135. return value [startIndex] != 0;
  136. }
  137. unsafe internal static char ToChar (byte[] value, int startIndex)
  138. {
  139. char ret;
  140. UShortFromBytes ((byte *) &ret, value, startIndex);
  141. return ret;
  142. }
  143. unsafe internal static short ToInt16 (byte[] value, int startIndex)
  144. {
  145. short ret;
  146. UShortFromBytes ((byte *) &ret, value, startIndex);
  147. return ret;
  148. }
  149. unsafe internal static int ToInt32 (byte[] value, int startIndex)
  150. {
  151. int ret;
  152. UIntFromBytes ((byte *) &ret, value, startIndex);
  153. return ret;
  154. }
  155. unsafe internal static long ToInt64 (byte[] value, int startIndex)
  156. {
  157. long ret;
  158. ULongFromBytes ((byte *) &ret, value, startIndex);
  159. return ret;
  160. }
  161. unsafe internal static ushort ToUInt16 (byte[] value, int startIndex)
  162. {
  163. ushort ret;
  164. UShortFromBytes ((byte *) &ret, value, startIndex);
  165. return ret;
  166. }
  167. unsafe internal static uint ToUInt32 (byte[] value, int startIndex)
  168. {
  169. uint ret;
  170. UIntFromBytes ((byte *) &ret, value, startIndex);
  171. return ret;
  172. }
  173. unsafe internal static ulong ToUInt64 (byte[] value, int startIndex)
  174. {
  175. ulong ret;
  176. ULongFromBytes ((byte *) &ret, value, startIndex);
  177. return ret;
  178. }
  179. unsafe internal static float ToSingle (byte[] value, int startIndex)
  180. {
  181. float ret;
  182. UIntFromBytes ((byte *) &ret, value, startIndex);
  183. return ret;
  184. }
  185. unsafe internal static double ToDouble (byte[] value, int startIndex)
  186. {
  187. double ret;
  188. ULongFromBytes ((byte *) &ret, value, startIndex);
  189. return ret;
  190. }
  191. }
  192. }