mono-endian.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * \file
  3. *
  4. * Author:
  5. * Mono Project (http://www.mono-project.com)
  6. *
  7. * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
  8. * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  9. * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  10. */
  11. #include <config.h>
  12. #include <mono/utils/mono-compiler.h>
  13. #include "mono-endian.h"
  14. #if NO_UNALIGNED_ACCESS
  15. typedef union {
  16. char c [2];
  17. guint16 i;
  18. } mono_rint16;
  19. typedef union {
  20. char c [4];
  21. guint32 i;
  22. } mono_rint32;
  23. typedef union {
  24. char c [8];
  25. guint64 i;
  26. } mono_rint64;
  27. guint16
  28. mono_read16 (const unsigned char *x)
  29. {
  30. mono_rint16 r;
  31. #if G_BYTE_ORDER == G_LITTLE_ENDIAN
  32. r.c [0] = x [0];
  33. r.c [1] = x [1];
  34. #else
  35. r.c [1] = x [0];
  36. r.c [0] = x [1];
  37. #endif
  38. return r.i;
  39. }
  40. guint32
  41. mono_read32 (const unsigned char *x)
  42. {
  43. mono_rint32 r;
  44. #if G_BYTE_ORDER == G_LITTLE_ENDIAN
  45. r.c [0] = x [0];
  46. r.c [1] = x [1];
  47. r.c [2] = x [2];
  48. r.c [3] = x [3];
  49. #else
  50. r.c [3] = x [0];
  51. r.c [2] = x [1];
  52. r.c [1] = x [2];
  53. r.c [0] = x [3];
  54. #endif
  55. return r.i;
  56. }
  57. guint64
  58. mono_read64 (const unsigned char *x)
  59. {
  60. mono_rint64 r;
  61. #if G_BYTE_ORDER == G_LITTLE_ENDIAN
  62. r.c [0] = x [0];
  63. r.c [1] = x [1];
  64. r.c [2] = x [2];
  65. r.c [3] = x [3];
  66. r.c [4] = x [4];
  67. r.c [5] = x [5];
  68. r.c [6] = x [6];
  69. r.c [7] = x [7];
  70. #else
  71. r.c [7] = x [0];
  72. r.c [6] = x [1];
  73. r.c [5] = x [2];
  74. r.c [4] = x [3];
  75. r.c [3] = x [4];
  76. r.c [2] = x [5];
  77. r.c [1] = x [6];
  78. r.c [0] = x [7];
  79. #endif
  80. return r.i;
  81. }
  82. #else /* NO_UNALIGNED_ACCESS */
  83. MONO_EMPTY_SOURCE_FILE (mono_endian);
  84. #endif