1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#ifndef ArchAbstractionPointer_h #define ArchAbstractionPointer_h
#include <stdint.h> #include <libkern/OSByteOrder.h>
#ifdef __OPTIMIZE__ #define INLINE __attribute__((always_inline)) #else #define INLINE #endif
class BigEndian { public: static uint16_t get16(const uint16_t& from) INLINE { return OSReadBigInt(&from, 0);} static void set16(uint16_t& info, uint16_t value) INLINE { OSWriteBigInt16(&info, 0, value);}
static uint32_t get32(const uint32_t& from) INLINE { return OSReadBigInt32(&from, 0); } static void set32(uint32_t& info, uint32_t value) INLINE { OSWriteBigInt32(&info, 0, value); }
static uint64_t get64(const uint64_t& from) INLINE { return OSReadBigInt64(&from, 0); } static void set64(uint64_t& info, uint64_t value) INLINE { OSWriteBigInt64(&info, 0, value); }
static uint32_t getBits(const uint32_t& from, uint8_t firstBit, uint8_t bitCount) { return getBitsRaw(get32(from), firstBit, bitCount); }
static void setBits(uint32_t& info, uint32_t value, uint8_t firstBit, uint8_t bitCount) { uint32_t tmp = get32(info); setBitsRaw(tmp, value, firstBit, bitCount); }
static uint32_t getBitsRaw(const uint32_t& from, uint8_t firstBit, uint8_t bitCount) INLINE { return ((from>>(32-firstBit-bitCount)) & ((1<<bitCount)-1)); }
static void setBitsRaw(uint32_t& info, uint32_t value, uint8_t firstBit, uint8_t bitCount) INLINE { uint32_t tmp = info; const uint32_t mask = ((1<<bitCount)-1); tmp &= ~(mask<<(32-firstBit-bitCount)); tmp |= ((value&mask)<<(32-firstBit-bitCount)); info = tmp; }
enum { little_endian = 0 }; };
class LittleEndian { public: static uint16_t get16(const uint16_t& from) INLINE {return OSReadLittleInt16(&from, 0);} static void set16(uint16_t& info, uint16_t value) INLINE {OSWriteLittleInt16(&info, 0, value);}
static uint32_t get32(const uint32_t& from) INLINE {return OSReadLittleInt32(&from, 0);} static void set32(uint32_t& info, uint32_t value) INLINE { OSWriteLittleInt32(&info, 0, value); }
static uint64_t get64(const uint64_t& from) INLINE {return OSReadLittleInt64(&from, 0);} static void set64(uint64_t& info, uint64_t value) INLINE { OSWriteLittleInt64(&info, 0, value); }
static uint32_t getBitRaw(const uint32_t& from, uint8_t firstBit, uint8_t bitCount) INLINE { return (from>>firstBit) & ((1<<bitCount)-1); }
static void setBitRaw(uint32_t& from, uint32_t value, uint8_t firstBit, uint8_t bitCount) { uint32_t tmp = from; const uint32_t mask = (1<<bitCount)-1; tmp &= ~(mask << firstBit); tmp |= (value&mask) << firstBit; from = tmp; }
enum { little_endian = 1 }; };
template <typename _E> class Pointer32 { public: typedef uint32_t uint_t; typedef int32_t sint_t; typedef _E E;
static uint64_t getP(const uint_t& from) INLINE { return _E::get32(from); } static void setP(uint_t& from, uint64_t value) INLINE { _E::set32(from, value); } };
template <typename _E> class Pointer64 { public: typedef uint64_t uint_t; typedef int64_t sint_t; typedef _E E;
static uint64_t getP(const uint_t& from) INLINE { return _E::get64(from); } static uint64_t setP(uint_t& from, uint64_t value) INLINE { return _E::set64(from, value); } };
#endif
|