ByteUtils.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. recast4j copyright (c) 2021 Piotr Piastucki piotr@jtilia.org
  3. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. using DotRecast.Core;
  19. namespace DotRecast.Detour.Dynamic.Io
  20. {
  21. public static class ByteUtils
  22. {
  23. public static int GetInt(byte[] data, int position, RcByteOrder order)
  24. {
  25. return order == RcByteOrder.BIG_ENDIAN ? GetIntBE(data, position) : GetIntLE(data, position);
  26. }
  27. public static int GetIntBE(byte[] data, int position)
  28. {
  29. return ((data[position] & 0xff) << 24) | ((data[position + 1] & 0xff) << 16) | ((data[position + 2] & 0xff) << 8)
  30. | (data[position + 3] & 0xff);
  31. }
  32. public static int GetIntLE(byte[] data, int position)
  33. {
  34. return ((data[position + 3] & 0xff) << 24) | ((data[position + 2] & 0xff) << 16) | ((data[position + 1] & 0xff) << 8)
  35. | (data[position] & 0xff);
  36. }
  37. public static int GetShort(byte[] data, int position, RcByteOrder order)
  38. {
  39. return order == RcByteOrder.BIG_ENDIAN ? GetShortBE(data, position) : GetShortLE(data, position);
  40. }
  41. public static int GetShortBE(byte[] data, int position)
  42. {
  43. return ((data[position] & 0xff) << 8) | (data[position + 1] & 0xff);
  44. }
  45. public static int GetShortLE(byte[] data, int position)
  46. {
  47. return ((data[position + 1] & 0xff) << 8) | (data[position] & 0xff);
  48. }
  49. public static int PutInt(int value, byte[] data, int position, RcByteOrder order)
  50. {
  51. if (order == RcByteOrder.BIG_ENDIAN)
  52. {
  53. data[position] = (byte)((uint)value >> 24);
  54. data[position + 1] = (byte)((uint)value >> 16);
  55. data[position + 2] = (byte)((uint)value >> 8);
  56. data[position + 3] = (byte)(value & 0xFF);
  57. }
  58. else
  59. {
  60. data[position] = (byte)(value & 0xFF);
  61. data[position + 1] = (byte)((uint)value >> 8);
  62. data[position + 2] = (byte)((uint)value >> 16);
  63. data[position + 3] = (byte)((uint)value >> 24);
  64. }
  65. return position + 4;
  66. }
  67. public static int PutShort(int value, byte[] data, int position, RcByteOrder order)
  68. {
  69. if (order == RcByteOrder.BIG_ENDIAN)
  70. {
  71. data[position] = (byte)((uint)value >> 8);
  72. data[position + 1] = (byte)(value & 0xFF);
  73. }
  74. else
  75. {
  76. data[position] = (byte)(value & 0xFF);
  77. data[position + 1] = (byte)((uint)value >> 8);
  78. }
  79. return position + 2;
  80. }
  81. }
  82. }