diff --git a/Endian/Endian.cpp b/Endian/Endian.cpp new file mode 100644 index 0000000..f45e356 --- /dev/null +++ b/Endian/Endian.cpp @@ -0,0 +1,105 @@ +#include "Endian.h" + +using namespace LittleEndian { + + + uint16_t Read16(const void *vp) + { + const uint8_t *p = (const uint8_t *)vp; + return p[0] | (p[1] << 8); + } + + uint32_t Read24(const void *vp) + { + const uint8_t *p = (const uint8_t *)vp; + return (p[0]) | (p[1] << 8) | (p[2] << 16); + } + + + uint32_t Read32(const void *vp) + { + const uint8_t *p = (const uint8_t *)vp; + return (p[0]) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); + } + + + + void Write16(void *vp, uint16_t x) + { + uint8_t *p = (uint8_t *)vp; + p[0] = (x) & 0xff; + p[1] = (x >> 8) & 0xff; + } + + void Write24(void *vp, uint32_t x) + { + uint8_t *p = (uint8_t *)vp; + p[0] = (x) & 0xff; + p[1] = (x >> 8) & 0xff; + p[2] = (x >> 16) & 0xff; + } + + void Write32(void *vp, uint32_t x) + { + uint8_t *p = (uint8_t *)vp; + p[0] = (x) & 0xff; + p[1] = (x >> 8) & 0xff; + p[2] = (x >> 16) & 0xff; + p[3] = (x >> 24) & 0xff; + } + + + + + +} + +using namespace BigEndian { + + + inline uint16_t Read16(const void *vp) + { + const uint8_t *p = (const uint8_t *)vp; + return (p[0] << 8) | (p[1]); + } + + inline uint32_t Read24(const void *vp) + { + const uint8_t *p = (const uint8_t *)vp; + return (p[0] << 16) | (p[1] << 8) | (p[2]); + } + + + inline uint32_t Read32(const void *vp) + { + const uint8_t *p = (const uint8_t *)vp; + return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3]); + } + + + inline void Write16(void *vp, uint16_t x) + { + uint8_t *p = (uint8_t *)vp; + p[0] = (x >> 8) & 0xff; + p[1] = (x) & 0xff; + } + + inline void Write24(void *vp, uint32_t x) + { + uint8_t *p = (uint8_t *)vp; + p[0] = (x >> 16) & 0xff; + p[1] = (x >> 8) & 0xff; + p[2] = (x) & 0xff; + } + + inline void Write32(void *vp, uint32_t x) + { + uint8_t *p = (uint8_t *)vp; + p[0] = (x >> 24) & 0xff; + p[1] = (x >> 16) & 0xff; + p[2] = (x >> 8) & 0xff; + p[3] = (x) & 0xff; + } + + +} \ No newline at end of file diff --git a/Endian/Endian.h b/Endian/Endian.h index 7241f7e..c917a7f 100644 --- a/Endian/Endian.h +++ b/Endian/Endian.h @@ -14,24 +14,11 @@ namespace LittleEndian { return (p[0]); } - inline uint16_t Read16(const void *vp) - { - const uint8_t *p = (const uint8_t *)vp; - return p[0] | (p[1] << 8); - } + uint16_t Read16(const void *vp); - inline uint32_t Read24(const void *vp) - { - const uint8_t *p = (const uint8_t *)vp; - return (p[0]) | (p[1] << 8) | (p[2] << 16); - } + uint32_t Read24(const void *vp); - - inline uint32_t Read32(const void *vp) - { - const uint8_t *p = (const uint8_t *)vp; - return (p[0]) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); - } + uint32_t Read32(const void *vp); inline uint8_t Read8(const void *vp, unsigned offset) @@ -62,29 +49,12 @@ namespace LittleEndian { p[0] = x; } - inline void Write16(void *vp, uint16_t x) - { - uint8_t *p = (uint8_t *)vp; - p[0] = (x) & 0xff; - p[1] = (x >> 8) & 0xff; - } + void Write16(void *vp, uint16_t x); + + void Write24(void *vp, uint32_t x); + + void Write32(void *vp, uint32_t x); - inline void Write24(void *vp, uint32_t x) - { - uint8_t *p = (uint8_t *)vp; - p[0] = (x) & 0xff; - p[1] = (x >> 8) & 0xff; - p[2] = (x >> 16) & 0xff; - } - - inline void Write32(void *vp, uint32_t x) - { - uint8_t *p = (uint8_t *)vp; - p[0] = (x) & 0xff; - p[1] = (x >> 8) & 0xff; - p[2] = (x >> 16) & 0xff; - p[3] = (x >> 24) & 0xff; - } inline void Write8(void *vp, unsigned offset, uint8_t x) { @@ -118,24 +88,11 @@ namespace BigEndian { return p[0]; } - inline uint16_t Read16(const void *vp) - { - const uint8_t *p = (const uint8_t *)vp; - return (p[0] << 8) | (p[1]); - } + uint16_t Read16(const void *vp); - inline uint32_t Read24(const void *vp) - { - const uint8_t *p = (const uint8_t *)vp; - return (p[0] << 16) | (p[1] << 8) | (p[2]); - } + uint32_t Read24(const void *vp); - - inline uint32_t Read32(const void *vp) - { - const uint8_t *p = (const uint8_t *)vp; - return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3]); - } + uint32_t Read32(const void *vp); inline uint8_t Read8(const void *vp, unsigned offset) @@ -167,30 +124,13 @@ namespace BigEndian { p[0] = x; } - inline void Write16(void *vp, uint16_t x) - { - uint8_t *p = (uint8_t *)vp; - p[0] = (x >> 8) & 0xff; - p[1] = (x) & 0xff; - } + void Write16(void *vp, uint16_t x); + + void Write24(void *vp, uint32_t x); + + void Write32(void *vp, uint32_t x); + - inline void Write24(void *vp, uint32_t x) - { - uint8_t *p = (uint8_t *)vp; - p[0] = (x >> 16) & 0xff; - p[1] = (x >> 8) & 0xff; - p[2] = (x) & 0xff; - } - - inline void Write32(void *vp, uint32_t x) - { - uint8_t *p = (uint8_t *)vp; - p[0] = (x >> 24) & 0xff; - p[1] = (x >> 16) & 0xff; - p[2] = (x >> 8) & 0xff; - p[3] = (x) & 0xff; - } - inline void Write8(void *vp, unsigned offset, uint8_t x) { Write8(offset + (uint8_t *)vp, x); diff --git a/Endian/IOBuffer.h b/Endian/IOBuffer.h index a923e0e..77092c9 100644 --- a/Endian/IOBuffer.h +++ b/Endian/IOBuffer.h @@ -1,71 +1,15 @@ #ifndef __IOBUFFER_H__ #define __IOBUFFER_H__ -#include "../Endian.h" - +#include "Endian.h" #include -namespace LittleEndian { - - class IOBuffer { - public: - - IOBuffer(void *vp, unsigned size) - { - _buffer = vp; - _size = size; - _offset = 0; - } - - void write8(uint8_t value) - { - Write8(_buffer, _offset, value); - _offset += 1; - } - void write16(uint16_t value) - { - Write16(_buffer, _offset, value); - _offset += 2; - } - - void write24(uint32_t value) - { - Write24(_buffer, _offset, value); - _offset += 3; - } - void write32(uint32_t value) - { - Write32(_buffer, _offset, value); - _offset += 4; - } - - void writeBytes(const void *value, unsigned count) - { - std::memcpy(_offset + (uint8_t *)_buffer, value, count); - _offset += count; - } - - void writeZero(unsigned count) - { - uint8_t *cp = _offset + (uint8_t *)_buffer; - for (unsigned i = 0; i < count; ++i) - { - cp[i] = 0; - } - _offset += count; - } - - unsigned offset() const { return _offset; } - void setOffset(unsigned offset) { _offset = offset; } - - unsigned size() const { return _size; } - - private: - void *_buffer; - unsigned _size; - unsigned _offset; - - }; +namespace LittleEndian { +#include "IOBuffer.t.cpp" +} + +namespace BigEndian { +#inclue "IOBuffer.t.cpp" } #endif \ No newline at end of file diff --git a/Endian/IOBuffer.t.cpp b/Endian/IOBuffer.t.cpp new file mode 100644 index 0000000..dfd4e6a --- /dev/null +++ b/Endian/IOBuffer.t.cpp @@ -0,0 +1,59 @@ + class IOBuffer { + public: + + IOBuffer(void *vp, unsigned size) + { + _buffer = vp; + _size = size; + _offset = 0; + } + + void write8(uint8_t value) + { + Write8(_buffer, _offset, value); + _offset += 1; + } + void write16(uint16_t value) + { + Write16(_buffer, _offset, value); + _offset += 2; + } + + void write24(uint32_t value) + { + Write24(_buffer, _offset, value); + _offset += 3; + } + void write32(uint32_t value) + { + Write32(_buffer, _offset, value); + _offset += 4; + } + + void writeBytes(const void *value, unsigned count) + { + std::memcpy(_offset + (uint8_t *)_buffer, value, count); + _offset += count; + } + + void writeZero(unsigned count) + { + uint8_t *cp = _offset + (uint8_t *)_buffer; + for (unsigned i = 0; i < count; ++i) + { + cp[i] = 0; + } + _offset += count; + } + + unsigned offset() const { return _offset; } + void setOffset(unsigned offset) { _offset = offset; } + + unsigned size() const { return _size; } + + private: + void *_buffer; + unsigned _size; + unsigned _offset; + + }; \ No newline at end of file