ulvis.paste.net - pastebin

Paste Search Dynamic
Recent pastes
_PLATFORM_DEPENDENT_H_
  1. #ifndef _PLATFORM_DEPENDENT_H_
  2.  
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include <stdbool.h>
  8. #include <endian.h>
  9.  
  10. typedef uint8_t u1;
  11. typedef uint16_t u2;
  12. typedef uint32_t u4;
  13. typedef uint64_t u8;
  14.  
  15. static inline u2 u22h(u2 beval){
  16.     #if __BYTE_ORDER == __LITTLE_ENDIAN
  17.         return (((beval) >> 8) & (u2)0x00ff)|(((beval) << 8) & (u2)0xff00);
  18.     #else  
  19.         return beval;
  20.     #endif
  21. }
  22.  
  23. static inline u4 u42h(u4 beval){
  24.     #if __BYTE_ORDER == __LITTLE_ENDIAN
  25.         return ((beval >> 24) & 0x000000ff)|((beval >> 8) & 0x0000ff00)
  26.                 |((beval << 8) & 0x00ff0000)|((beval << 24) & 0xff000000);
  27.     #else  
  28.         return bex
  29.     #endif
  30. }
  31.  
  32. static inline u8 u82h(u8 beval){
  33.     #if __BYTE_ORDER == __LITTLE_ENDIAN
  34.         return ((beval >> 56) & 0x00000000000000ff)|((beval >> 40) & 0x000000000000ff00)|
  35.             ((beval >> 24) & 0x0000000000ff0000)|((beval >> 8) & 0x00000000ff000000)|
  36.             ((beval << 8) & 0x000000ff00000000)|((beval << 24) & 0x0000ff0000000000)|
  37.             ((beval << 40) & 0x00ff000000000000)|((beval << 56) & 0xff00000000000000);
  38.     #else
  39.         return beval
  40.     #endif
  41. }
  42.  
  43. static inline u1 readU1(FILE* filestream){
  44.     u1 result;
  45.     int readed = fread(&result, 1, sizeof(u1), filestream);
  46.     assert(readed == sizeof(u1));
  47.     return result;
  48. }
  49.  
  50. static inline u2 readU2(FILE* filestream){
  51.     u2 result;
  52.     int readed = fread(&result, 1, sizeof(u2), filestream);
  53.     assert(readed == sizeof(u2));
  54.     return u22h(result);
  55. }
  56.  
  57. static inline u4 readU4(FILE* filestream){
  58.     u4 result;
  59.     int readed = fread(&result, 1, sizeof(u4), filestream);
  60.     assert(readed == sizeof(u4));
  61.     return u42h(result);
  62. }
  63.  
  64. static inline u8 readU8(FILE* filestream){
  65.     u8 result;
  66.     int readed = fread(&result, 1, sizeof(u8), filestream);
  67.     assert(readed == sizeof(u8));
  68.     return u82h(result);
  69. }
  70.  
  71. static inline float readFloat(FILE* filestream){
  72.     u4 bits = readU4(filestream);
  73.     float result = *(float*)&bits;
  74.     return result;
  75. }
  76.  
  77. static inline double readDouble(FILE* filestream){
  78.     u8 bits = readU8(filestream);
  79.     float result = *(double*)&bits;
  80.     return result;
  81. }
  82.  
  83. static inline void read(FILE* filestream, u1* buffer, u2 length){
  84.     int readed = fread(buffer, 1, length, filestream);
  85.     assert(readed == length); return;
  86. }
  87.  
  88. static inline void assert_eof(FILE* filestream){
  89.     u1 buf; assert(fread(&buf, 1, 1,filestream) == 0);
  90. }
  91.  
  92. static inline void* assert_malloc(size_t size){
  93.     if(size == 0){return null;}
  94.     void* result = malloc(size);
  95.     assert(result != null);
  96.     return result;
  97. }
  98.  
  99. #define _PLATFORM_DEPENDENT_H_
  100. #endif //_PLATFORM_DEPENDENT_H_
Parsed in 0.017 seconds