Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

endian.cpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*==========================================================================
  2. *
  3. * Project : MDDC
  4. * Author : Terry 'Mongoose' Hendrix II
  5. * Website : http://www.westga.edu/~stu7440
  6. * Email : stu7440@westga.edu
  7. * Object :
  8. * Comments: This is the endian utils.
  9. *
  10. * See file COPYING for license details.
  11. *
  12. * Based on type converting code by:
  13. * Michael Vance <mkv102@psu.edu>
  14. *
  15. *-- History ----------------------------------------------------------
  16. *
  17. * 2000-03-17:
  18. * Mongoose: Created, I combined some endian checking with fread wrapper
  19. ==========================================================================*/
  20. #include <endian.h>
  21. int fread_u_int_small(unsigned int *ptr, FILE *stream)
  22. {
  23. #ifdef BIG_ENDIAN
  24. unsigned char b1, b2, b3, b4;
  25. unsigned int l;
  26. #endif
  27. if (fread(ptr, 4, 1, stream) != 1)
  28. {
  29. printf("FreadSmallInt> Failed fread.\n");
  30. return false;
  31. }
  32. #ifdef BIG_ENDIAN
  33. l = *ptr;
  34. b1 = l & 255;
  35. b2 = ( l >> 8 ) & 255;
  36. b3 = ( l >> 16 ) & 255;
  37. b4 = ( l >> 24 ) & 255;
  38. *ptr = ( (int) b1 << 24 ) + ( (int) b2 << 16 ) + ( (int) b3 << 8 ) + b4;
  39. #endif
  40. return true;
  41. }
  42. int fread_int_small(int *ptr, FILE *stream)
  43. {
  44. #ifdef BIG_ENDIAN
  45. unsigned char b1, b2, b3, b4;
  46. int l;
  47. #endif
  48. if (fread(ptr, 4, 1, stream) != 1)
  49. {
  50. printf("FreadSmallInt> Failed fread.\n");
  51. return false;
  52. }
  53. #ifdef BIG_ENDIAN
  54. l = *ptr;
  55. b1 = l & 255;
  56. b2 = ( l >> 8 ) & 255;
  57. b3 = ( l >> 16 ) & 255;
  58. b4 = ( l >> 24 ) & 255;
  59. *ptr = ( (int) b1 << 24 ) + ( (int) b2 << 16 ) + ( (int) b3 << 8 ) + b4;
  60. #endif
  61. return true;
  62. }
  63. int fread_float_small(float *ptr, FILE *stream)
  64. {
  65. #ifdef BIG_ENDIAN
  66. union
  67. {
  68. unsigned char b[4];
  69. float f;
  70. } in, out;
  71. float l;
  72. #endif
  73. if (fread(ptr, 4, 1, stream) != 1)
  74. {
  75. printf("FreadSmallFloat> Failed fread.\n");
  76. return false;
  77. }
  78. #ifdef BIG_ENDIAN
  79. l = *ptr;
  80. in.f = l;
  81. out.b[0] = in.b[3];
  82. out.b[1] = in.b[2];
  83. out.b[2] = in.b[1];
  84. out.b[3] = in.b[0];
  85. *ptr = out.f;
  86. #endif
  87. return true;
  88. }
  89. #ifdef NEW_API_PLANS
  90. /* FIXME: Add Big Endian support later */
  91. void fread_u8int_small(unsigned char *u, FILE *f)
  92. {
  93. u_int8_t n;
  94. fread(&n, 1, 1, f);
  95. *u = n;
  96. }
  97. void fread_u16int_small(unsigned short *u, FILE *f)
  98. {
  99. u_int16_t n;
  100. fread(&n, 2, 1, f);
  101. *u = n;
  102. }
  103. void fread_u32int_small(unsigned int *u, FILE *f)
  104. {
  105. u_int32_t n;
  106. fread(&n, 4, 1, f);
  107. *u = n;
  108. }
  109. #endif