diff options
author | Daniel Friesel <daniel.friesel@uos.de> | 2019-06-26 14:40:30 +0200 |
---|---|---|
committer | Daniel Friesel <daniel.friesel@uos.de> | 2019-06-26 14:40:30 +0200 |
commit | 2c001e655002ee713457342197a7005217cd16b4 (patch) | |
tree | f97ee9055aa89aa065f29bf078426bd27480ccc4 /src/os | |
parent | f4394afb4e7915457f0a901e9c3d9d0f08470593 (diff) |
Add a 16-bit XDR variant
Diffstat (limited to 'src/os')
-rw-r--r-- | src/os/object/xdr16input.cc | 99 | ||||
-rw-r--r-- | src/os/object/xdr16stream.cc | 161 |
2 files changed, 260 insertions, 0 deletions
diff --git a/src/os/object/xdr16input.cc b/src/os/object/xdr16input.cc new file mode 100644 index 0000000..407f6ea --- /dev/null +++ b/src/os/object/xdr16input.cc @@ -0,0 +1,99 @@ +#include "object/xdr16input.h" + +uint16_t XDRInput::get_uint16() +{ + uint32_t ret = ((uint8_t)data[pos]<<8) | (uint8_t)data[pos+1]; + pos += 2; + return ret; +} + +int16_t XDRInput::get_int16() +{ + int32_t ret = (data[pos]<<8) | data[pos+1]; + pos += 2; + return ret; +} + +uint32_t XDRInput::get_uint32() +{ + uint32_t ret = ((uint8_t)data[pos]<<24) | ((uint8_t)data[pos+1]<<16) + | ((uint8_t)data[pos+2]<<8) | (uint8_t)data[pos+3]; + pos += 4; + return ret; +} + +int32_t XDRInput::get_int32() +{ + int32_t ret = (data[pos]<<24) | (data[pos+1]<<16) | (data[pos+2]<<8) | data[pos+3]; + pos += 4; + return ret; +} + +uint64_t XDRInput::get_uint64() +{ + uint64_t ret0 = ((uint8_t)data[pos]<<24) | ((uint8_t)data[pos+1]<<16) + | ((uint8_t)data[pos+2]<<8) | (uint8_t)data[pos+3]; + pos += 4; + uint64_t ret1 = ((uint8_t)data[pos]<<24) | ((uint8_t)data[pos+1]<<16) + | ((uint8_t)data[pos+2]<<8) | (uint8_t)data[pos+3]; + pos += 4; + return (ret0 << 32) | ret1; +} + +int64_t XDRInput::get_int64() +{ + int64_t ret0 = (data[pos]<<24) | (data[pos+1]<<16) | (data[pos+2]<<8) | data[pos+3]; + pos += 4; + int64_t ret1 = (data[pos]<<24) | (data[pos+1]<<16) | (data[pos+2]<<8) | data[pos+3]; + pos += 4; + return (ret0 << 32) | ret1; +} + +float XDRInput::get_float() +{ + union { + uint32_t i; + float f; + } v; + v.i = get_uint32(); + return v.f; +} + +double XDRInput::get_double() +{ + union { + uint64_t i; + double d; + } v; + v.i = get_uint64(); + return v.d; +} + +uint16_t XDRInput::get_opaque_length() +{ + return get_uint16(); +} + +char *XDRInput::get_opaque(uint16_t length) +{ + char *ret = data + pos; + pos += length; + if (length % 2) { + pos += 2 - (length % 2); + } + return ret; +} + +void XDRInput::get_string(char* target) +{ + uint16_t length = get_opaque_length(); + uint16_t i; + for (i = 0; i < length; i++) { + target[i] = data[pos + i]; + } + target[i] = 0; + pos += length; + if (length % 2) { + pos += 2 - (length % 2); + } +} diff --git a/src/os/object/xdr16stream.cc b/src/os/object/xdr16stream.cc new file mode 100644 index 0000000..082b2a3 --- /dev/null +++ b/src/os/object/xdr16stream.cc @@ -0,0 +1,161 @@ +#include "object/xdr16stream.h" + +XDRStream & XDRStream::operator<<(unsigned char c) +{ + *this << (uint16_t)c; + return *this; +} + +XDRStream & XDRStream::operator<<(char c) +{ + *this << (int16_t)c; + return *this; +} + +XDRStream & XDRStream::operator<<(uint16_t number) +{ + put((number >> 8) & 0xffU); + put(number & 0xffU); + return *this; +} + +XDRStream & XDRStream::operator<<(int16_t number) +{ + put((number >> 8) & 0xffU); + put(number & 0xffU); + return *this; +} + +XDRStream & XDRStream::operator<<(uint32_t number) +{ + put((number >> 24) & 0xffU); + put((number >> 16) & 0xffU); + put((number >> 8) & 0xffU); + put(number & 0xffU); + return *this; +} + +XDRStream & XDRStream::operator<<(int32_t number) +{ + put((number >> 24) & 0xffU); + put((number >> 16) & 0xffU); + put((number >> 8) & 0xffU); + put(number & 0xffU); + return *this; +} + +XDRStream & XDRStream::operator<<(uint64_t number) +{ + put((number >> 56) & 0xffU); + put((number >> 48) & 0xffU); + put((number >> 40) & 0xffU); + put((number >> 32) & 0xffU); + put((number >> 24) & 0xffU); + put((number >> 16) & 0xffU); + put((number >> 8) & 0xffU); + put(number & 0xffU); + return *this; +} + +XDRStream & XDRStream::operator<<(int64_t number) +{ + put((number >> 56) & 0xffU); + put((number >> 48) & 0xffU); + put((number >> 40) & 0xffU); + put((number >> 32) & 0xffU); + put((number >> 24) & 0xffU); + put((number >> 16) & 0xffU); + put((number >> 8) & 0xffU); + put(number & 0xffU); + return *this; +} + +XDRStream & XDRStream::operator<<(float number) +{ + union { + uint32_t i; + float f; + } v; + v.f = number; + *this << v.i; + return *this; +} + +XDRStream & XDRStream::operator<<(double number) +{ + union { + uint64_t i; + double d; + } v; + v.d = number; + *this << v.i; + return *this; +} + +XDRStream & XDRStream::operator<<(char const *data){ + if (!is_fixed_length) { + *this << next_array_len; + } + uint32_t i; + for (i = 0; i < next_array_len; i++) { + put(data[i]); + } + while ((i++) % 2 != 0){ + put('\0'); + } + return *this; +} + +template<uint16_t TSize> +XDRStream & XDRStream::operator<<(char const (&data)[TSize]){ + if (!is_fixed_length) { + *this << TSize; + } + uint32_t i; + for (i = 0; i < TSize; i++) { + put(data[i]); + } + while ((i++) % 2 != 0){ + put('\0'); + } + return *this; +} + +XDRStream & XDRStream::operator<<(XDRStream & (*fkt) (XDRStream &)) +{ + return fkt(*this); +} + +// FLUSH +XDRStream & flush(XDRStream & os) +{ + os.flush(); + return os; +} + +// TERM: null-termination +XDRStream & term(XDRStream & os) +{ + os.put('\0'); + os.flush(); + return os; +} + +template<int N> +XDRStream & opaque(XDRStream & os) +{ + os.setNextArrayLen(N); + return os; +} + +XDRStream & fixed(XDRStream & os) +{ + os.setFixedLength(); + return os; +} + +XDRStream & variable(XDRStream & os) +{ + os.setVariableLength(); + return os; +} |