summaryrefslogtreecommitdiff
path: root/libcult/cult/dr/xdr
diff options
context:
space:
mode:
authorJörg Frings-Fürst <jff@merkur>2014-05-18 16:08:14 +0200
committerJörg Frings-Fürst <jff@merkur>2014-05-18 16:08:14 +0200
commita15cf65c44d5c224169c32ef5495b68c758134b7 (patch)
tree3419f58fc8e1b315ba8171910ee044c5d467c162 /libcult/cult/dr/xdr
Imported Upstream version 3.3.0.2upstream/3.3.0.2
Diffstat (limited to 'libcult/cult/dr/xdr')
-rw-r--r--libcult/cult/dr/xdr/input-stream.cxx152
-rw-r--r--libcult/cult/dr/xdr/input-stream.hxx78
-rw-r--r--libcult/cult/dr/xdr/output-stream.cxx222
-rw-r--r--libcult/cult/dr/xdr/output-stream.hxx89
4 files changed, 541 insertions, 0 deletions
diff --git a/libcult/cult/dr/xdr/input-stream.cxx b/libcult/cult/dr/xdr/input-stream.cxx
new file mode 100644
index 0000000..1730763
--- /dev/null
+++ b/libcult/cult/dr/xdr/input-stream.cxx
@@ -0,0 +1,152 @@
+// file : cult/dr/xdr/input-stream.cxx
+// author : Boris Kolpackov <boris@kolpackov.net>
+// copyright : Copyright (c) 2005-2010 Boris Kolpackov
+// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+#include <cult/dr/xdr/input-stream.hxx>
+
+namespace Cult
+{
+ namespace DR
+ {
+ namespace XDR
+ {
+ InputStream::
+ InputStream (Shptr<Buffer> buffer)
+ : buffer_ (buffer)
+ {
+ xdrmem_create (&xdr_, buffer_->data (), buffer_->size (), XDR_DECODE);
+ }
+
+ InputStream::
+ ~InputStream ()
+ {
+ xdr_destroy (&xdr_);
+ }
+
+ InputStream& InputStream::
+ operator>> (Boolean& v)
+ {
+ bool_t b;
+
+ if (!xdr_bool (&xdr_, &b))
+ throw Extraction ();
+
+ v = b;
+
+ return *this;
+ }
+
+ InputStream& InputStream::
+ operator>> (Int8& v)
+ {
+ if (!xdr_int8_t (&xdr_, &v))
+ throw Extraction ();
+
+ return *this;
+ }
+
+ InputStream& InputStream::
+ operator>> (UnsignedInt8& v)
+ {
+ if (!xdr_uint8_t (&xdr_, &v))
+ throw Extraction ();
+
+ return *this;
+ }
+
+ InputStream& InputStream::
+ operator>> (Int16& v)
+ {
+ if (!xdr_int16_t (&xdr_, &v))
+ throw Extraction ();
+
+ return *this;
+ }
+
+ InputStream& InputStream::
+ operator>> (UnsignedInt16& v)
+ {
+ if (!xdr_uint16_t (&xdr_, &v))
+ throw Extraction ();
+
+ return *this;
+ }
+
+ InputStream& InputStream::
+ operator>> (Int32& v)
+ {
+ if (!xdr_int32_t (&xdr_, &v))
+ throw Extraction ();
+
+ return *this;
+ }
+
+ InputStream& InputStream::
+ operator>> (UnsignedInt32& v)
+ {
+ if (!xdr_uint32_t (&xdr_, &v))
+ throw Extraction ();
+
+ return *this;
+ }
+
+ InputStream& InputStream::
+ operator>> (Int64& v)
+ {
+ if (!xdr_int64_t (&xdr_, (int64_t*)&v))
+ throw Extraction ();
+
+ return *this;
+ }
+
+ InputStream& InputStream::
+ operator>> (UnsignedInt64& v)
+ {
+ if (!xdr_uint64_t (&xdr_, (uint64_t*)&v))
+ throw Extraction ();
+
+ return *this;
+ }
+
+ InputStream& InputStream::
+ operator>> (String& v)
+ {
+ UnsignedInt32 size;
+
+ if (!xdr_uint32_t (&xdr_, &size))
+ throw Extraction ();
+
+ // I dare to change the guts!
+ //
+ v.resize (size);
+
+ Char* p (const_cast<Char*> (v.data ()));
+
+ if (!xdr_opaque (&xdr_, p, size))
+ throw Extraction ();
+
+ return *this;
+ }
+
+ Void InputStream::
+ read (Buffer& buffer, Size size)
+ {
+ Size new_size (buffer.position () + size);
+
+ buffer.capacity (new_size);
+
+ if (!xdr_opaque (&xdr_, buffer.data () + buffer.position (), size))
+ throw Extraction ();
+
+ buffer.size (new_size);
+ }
+
+ Boolean InputStream::
+ eos () const
+ {
+ return xdr_getpos (&xdr_) >= buffer_->size ();
+ }
+ }
+ }
+}
diff --git a/libcult/cult/dr/xdr/input-stream.hxx b/libcult/cult/dr/xdr/input-stream.hxx
new file mode 100644
index 0000000..653b228
--- /dev/null
+++ b/libcult/cult/dr/xdr/input-stream.hxx
@@ -0,0 +1,78 @@
+// file : cult/dr/xdr/input-stream.hxx
+// author : Boris Kolpackov <boris@kolpackov.net>
+// copyright : Copyright (c) 2005-2010 Boris Kolpackov
+// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+#ifndef CULT_DR_XDR_INPUT_STREAM_HXX
+#define CULT_DR_XDR_INPUT_STREAM_HXX
+
+#include <cult/types.hxx>
+
+#include <cult/eh/exception.hxx>
+
+#include <cult/mm/buffer.hxx>
+
+#include <rpc/xdr.h>
+
+namespace Cult
+{
+ namespace DR
+ {
+ namespace XDR
+ {
+ struct Extraction: virtual EH::Exception {};
+
+ class InputStream: public NonCopyable
+ {
+ public:
+ InputStream (Shptr<Buffer> buffer);
+
+ ~InputStream ();
+
+ public:
+ InputStream&
+ operator>> (Boolean&);
+
+ InputStream&
+ operator>> (Int8&);
+
+ InputStream&
+ operator>> (UnsignedInt8&);
+
+ InputStream&
+ operator>> (Int16&);
+
+ InputStream&
+ operator>> (UnsignedInt16&);
+
+ InputStream&
+ operator>> (Int32&);
+
+ InputStream&
+ operator>> (UnsignedInt32&);
+
+ InputStream&
+ operator>> (Int64&);
+
+ InputStream&
+ operator>> (UnsignedInt64&);
+
+ InputStream&
+ operator>> (String&);
+
+ public:
+ Void
+ read (Buffer& buffer, Size size);
+
+ Boolean
+ eos () const;
+
+ private:
+ ::XDR xdr_;
+ Shptr<Buffer> buffer_;
+ };
+ }
+ }
+}
+
+#endif // CULT_DR_XDR_INPUT_STREAM_HXX
diff --git a/libcult/cult/dr/xdr/output-stream.cxx b/libcult/cult/dr/xdr/output-stream.cxx
new file mode 100644
index 0000000..acda64c
--- /dev/null
+++ b/libcult/cult/dr/xdr/output-stream.cxx
@@ -0,0 +1,222 @@
+// file : cult/dr/xdr/output-stream.cxx
+// author : Boris Kolpackov <boris@kolpackov.net>
+// copyright : Copyright (c) 2005-2010 Boris Kolpackov
+// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+#include <cult/dr/xdr/output-stream.hxx>
+
+namespace Cult
+{
+ namespace DR
+ {
+ namespace XDR
+ {
+ OutputStream::
+ OutputStream (Size hint)
+ : buffer_ (new Buffer (hint ? hint : 256))
+ {
+ xdrmem_create (&xdr_, buffer_->data (), ~(u_int (0)), XDR_ENCODE);
+ }
+
+
+ OutputStream::
+ ~OutputStream ()
+ {
+ xdr_destroy (&xdr_);
+ }
+
+ Shptr<Buffer> OutputStream::
+ buffer ()
+ {
+ return buffer_;
+ }
+
+ Void OutputStream::
+ ensure_space (Size size)
+ {
+ size += size % 4 ? 4 - size % 4 : 0;
+
+ Size needed (size + xdr_getpos (&xdr_));
+
+ if (needed > buffer_->capacity ())
+ {
+ Size new_size (buffer_->capacity () * 2);
+
+ if (needed > new_size)
+ new_size = needed % 8 ? 8 - new_size % 8 : 0;
+
+ if (buffer_->capacity (new_size))
+ {
+ xdr_destroy (&xdr_);
+
+ xdrmem_create (&xdr_,
+ buffer_->data () + buffer_->size (),
+ ~(u_int (0)),
+ XDR_ENCODE);
+ }
+ }
+ }
+
+ Void OutputStream::
+ update_position (Size position)
+ {
+ // Align to 4-boundary.
+ //
+ position += position % 4 ? 4 - position % 4 : 0;
+
+ buffer_->size (buffer_->size () + position);
+ }
+
+ OutputStream& OutputStream::
+ operator<< (Boolean v)
+ {
+ ensure_space (4);
+
+ bool_t b (v);
+
+ if (!xdr_bool (&xdr_, &b))
+ throw Insertion ();
+
+ update_position (4);
+ return *this;
+ }
+
+ OutputStream& OutputStream::
+ operator<< (Int8 v)
+ {
+ ensure_space (4);
+
+ if (!xdr_int8_t (&xdr_, &v))
+ throw Insertion ();
+
+ update_position (4);
+
+ return *this;
+ }
+
+ OutputStream& OutputStream::
+ operator<< (UnsignedInt8 v)
+ {
+ ensure_space (4);
+
+ if (!xdr_uint8_t (&xdr_, &v))
+ throw Insertion ();
+
+ update_position (4);
+
+ return *this;
+ }
+
+ OutputStream& OutputStream::
+ operator<< (Int16 v)
+ {
+ ensure_space (4);
+
+ if (!xdr_int16_t (&xdr_, &v))
+ throw Insertion ();
+
+ update_position (4);
+
+ return *this;
+ }
+
+ OutputStream& OutputStream::
+ operator<< (UnsignedInt16 v)
+ {
+ ensure_space (4);
+
+ if (!xdr_uint16_t (&xdr_, &v))
+ throw Insertion ();
+
+ update_position (4);
+
+ return *this;
+ }
+
+ OutputStream& OutputStream::
+ operator<< (Int32 v)
+ {
+ ensure_space (4);
+
+ if (!xdr_int32_t (&xdr_, &v))
+ throw Insertion ();
+
+ update_position (4);
+
+ return *this;
+ }
+
+ OutputStream& OutputStream::
+ operator<< (UnsignedInt32 v)
+ {
+ ensure_space (4);
+
+ if (!xdr_uint32_t (&xdr_, &v))
+ throw Insertion ();
+
+ update_position (4);
+
+ return *this;
+ }
+
+ OutputStream& OutputStream::
+ operator<< (Int64 v)
+ {
+ ensure_space (8);
+
+ if (!xdr_int64_t (&xdr_, (int64_t*)&v))
+ throw Insertion ();
+
+ update_position (8);
+
+ return *this;
+ }
+
+ OutputStream& OutputStream::
+ operator<< (UnsignedInt64 v)
+ {
+ ensure_space (8);
+
+ if (!xdr_uint64_t (&xdr_, (uint64_t*)&v))
+ throw Insertion ();
+
+ update_position (8);
+
+ return *this;
+ }
+
+ OutputStream& OutputStream::
+ operator<< (String const& v)
+ {
+ UnsignedInt32 size (v.size ());
+
+ ensure_space (4 + size);
+
+ Char* p (const_cast<Char*>(v.c_str ()));
+
+ if (!xdr_string (&xdr_, &p, size))
+ throw Insertion ();
+
+ update_position (4 + size);
+
+ return *this;
+ }
+
+ Void OutputStream::
+ write (Buffer const& buffer)
+ {
+ Size size (buffer.size () - buffer.position ());
+
+ ensure_space (size);
+
+ if (!xdr_opaque (
+ &xdr_,
+ const_cast<Char*> (buffer.data ()) + buffer.position (),
+ size))
+ throw Insertion ();
+
+ update_position (size);
+ }
+ }
+ }
+}
diff --git a/libcult/cult/dr/xdr/output-stream.hxx b/libcult/cult/dr/xdr/output-stream.hxx
new file mode 100644
index 0000000..72e0c19
--- /dev/null
+++ b/libcult/cult/dr/xdr/output-stream.hxx
@@ -0,0 +1,89 @@
+// file : cult/dr/xdr/output-stream.hxx
+// author : Boris Kolpackov <boris@kolpackov.net>
+// copyright : Copyright (c) 2005-2010 Boris Kolpackov
+// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+#ifndef CULT_DR_XDR_OUTPUT_STREAM_HXX
+#define CULT_DR_XDR_OUTPUT_STREAM_HXX
+
+#include <cult/types.hxx>
+
+#include <cult/eh/exception.hxx>
+
+#include <cult/mm/buffer.hxx>
+
+#include <rpc/xdr.h>
+
+namespace Cult
+{
+ namespace DR
+ {
+ namespace XDR
+ {
+ struct Insertion: virtual EH::Exception {};
+
+ //@@ I think I should use refernce to buffer instead of Shptr.
+ // To keep it simple, so to speak.
+ //
+ class OutputStream: public NonCopyable
+ {
+ public:
+ OutputStream (Size hint = 0);
+
+ ~OutputStream ();
+
+ public:
+ Shptr<Buffer>
+ buffer ();
+
+ public:
+ OutputStream&
+ operator<< (Boolean);
+
+ OutputStream&
+ operator<< (Int8);
+
+ OutputStream&
+ operator<< (UnsignedInt8);
+
+ OutputStream&
+ operator<< (Int16);
+
+ OutputStream&
+ operator<< (UnsignedInt16);
+
+ OutputStream&
+ operator<< (Int32);
+
+ OutputStream&
+ operator<< (UnsignedInt32);
+
+ OutputStream&
+ operator<< (Int64);
+
+ OutputStream&
+ operator<< (UnsignedInt64);
+
+ OutputStream&
+ operator<< (String const&);
+
+ public:
+ Void
+ write (Buffer const& buffer);
+
+ private:
+ Void
+ ensure_space (Size size);
+
+ Void
+ update_position (Size position);
+
+ private:
+ ::XDR xdr_;
+ Shptr<Buffer> buffer_;
+ };
+ }
+ }
+}
+
+#endif // CULT_DR_XDR_OUTPUT_STREAM_HXX