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/object/xdr16stream.cc | |
parent | f4394afb4e7915457f0a901e9c3d9d0f08470593 (diff) |
Add a 16-bit XDR variant
Diffstat (limited to 'src/os/object/xdr16stream.cc')
-rw-r--r-- | src/os/object/xdr16stream.cc | 161 |
1 files changed, 161 insertions, 0 deletions
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; +} |