From 13e18cf59686319efee0080d523c4bc2d948b591 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 12 May 2021 15:00:44 +0200 Subject: add xdr --- include/lib/xdr16.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 include/lib/xdr16.h (limited to 'include/lib/xdr16.h') diff --git a/include/lib/xdr16.h b/include/lib/xdr16.h new file mode 100644 index 0000000..483efe4 --- /dev/null +++ b/include/lib/xdr16.h @@ -0,0 +1,68 @@ +#ifndef XDR16_H +#define XDR16_H + +#include + +class XDRWriter +{ +private: + XDRWriter(const XDRWriter ©); + char *buffer; + uint16_t next_array_len; + uint16_t pos; + bool is_fixed_length; + +public: + XDRWriter(char *output_buf) : buffer(output_buf), next_array_len(0), pos(0) {} + + inline void setNextArrayLen(uint16_t len) { next_array_len = len; } + inline void setFixedLength() { is_fixed_length = true; } + inline void setVariableLength() { is_fixed_length = false; } + inline void startList() + { + if (!is_fixed_length) + { + put(next_array_len); + } + } + + inline void put(char c) { put((int16_t)c); } + inline void put(unsigned char c) { put((uint16_t)c); } + void put(uint16_t number); + void put(int16_t number); + void put(uint32_t number); + void put(int32_t number); + void put(uint64_t number); + void put(int64_t number); + void put(float number); + void put(double number); + void put(char const *text); + template + void put(char const (&text)[TSize]); + + inline uint16_t size() { return pos; } +}; + +class XDRReader +{ +private: + XDRReader(const XDRReader ©); + char *data; + uint32_t pos; + +public: + XDRReader(char *d) : pos(0) { data = d; } + int16_t get_int16(); + uint16_t get_uint16(); + uint32_t get_uint32(); + int32_t get_int32(); + uint64_t get_uint64(); + int64_t get_int64(); + float get_float(); + double get_double(); + uint32_t get_opaque_length(); + char *get_opaque(uint32_t length); + void get_string(char *target); +}; + +#endif -- cgit v1.2.3