From 8f675da301eafe79897f3ad67ff5450fcc397f78 Mon Sep 17 00:00:00 2001 From: Timo Weingärtner Date: Mon, 31 Aug 2015 20:22:37 +0200 Subject: move rfc4251 types into their own namespace --- rfc4251.H | 110 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 57 insertions(+), 53 deletions(-) (limited to 'rfc4251.H') diff --git a/rfc4251.H b/rfc4251.H index d9ac93e..7284f20 100644 --- a/rfc4251.H +++ b/rfc4251.H @@ -1,11 +1,11 @@ /* * rfc4251.h -- implements types from RFC 4251, section 5 * - * rfc4251byte byte - * rfc4251bool bool - * rfc4251uint32 uint32 - * rfc4251uint64 uint64 - * rfc4251string string, incl. mpint and name-list + * rfc4251::byte byte + * rfc4251::boolean boolean + * rfc4251::uint32 uint32 + * rfc4251::uint64 uint64 + * rfc4251::string string, incl. mpint and name-list * * those structs contain the objects in their RFC 4251 representation, * conversions are provided via constructors and cast operators @@ -37,102 +37,104 @@ #include #include -struct rfc4251byte { +namespace rfc4251 { + +struct byte { union { uint8_t value; char buf[1]; }; - rfc4251byte () = default; - explicit rfc4251byte (uint8_t v) : value(v) {} - inline explicit rfc4251byte (std::istream &); + byte () = default; + explicit byte (uint8_t v) : value(v) {} + inline explicit byte (std::istream &); operator uint8_t () const { return value; } }; -inline std::istream & operator>> (std::istream & is, rfc4251byte & x) { +inline std::istream & operator>> (std::istream & is, byte & x) { return is.read(x.buf, sizeof(x.buf)); } -inline std::ostream & operator<< (std::ostream & os, rfc4251byte const & x) { +inline std::ostream & operator<< (std::ostream & os, byte const & x) { return os.write(x.buf, sizeof(x.buf)); } -inline rfc4251byte::rfc4251byte (std::istream & is) { +inline byte::byte (std::istream & is) { is >> *this; } -struct rfc4251bool { +struct boolean { union { bool value; char buf[1]; }; - rfc4251bool () = default; - explicit rfc4251bool (uint8_t v) : value(v) {} - inline explicit rfc4251bool (std::istream &); + boolean () = default; + explicit boolean (uint8_t v) : value(v) {} + inline explicit boolean (std::istream &); operator uint8_t () const { return value; } }; -inline std::istream & operator>> (std::istream & is, rfc4251bool & x) { +inline std::istream & operator>> (std::istream & is, boolean & x) { return is.read(x.buf, sizeof(x.buf)); } -inline std::ostream & operator<< (std::ostream & os, rfc4251bool const & x) { +inline std::ostream & operator<< (std::ostream & os, boolean const & x) { return os.write(x.buf, sizeof(x.buf)); } -inline rfc4251bool::rfc4251bool (std::istream & is) { +inline boolean::boolean (std::istream & is) { is >> *this; } -struct rfc4251uint32 { +struct uint32 { union { uint32_t value; char buf[4]; }; - rfc4251uint32 () = default; - explicit rfc4251uint32 (uint32_t v) { value = htonl(v); } - inline explicit rfc4251uint32 (std::istream &); + uint32 () = default; + explicit uint32 (uint32_t v) { value = htonl(v); } + inline explicit uint32 (std::istream &); operator uint32_t () const { return ntohl(value); } }; -inline std::istream & operator>> (std::istream & is, rfc4251uint32 & x) { +inline std::istream & operator>> (std::istream & is, uint32 & x) { return is.read(x.buf, sizeof(x.buf)); } -inline std::ostream & operator<< (std::ostream & os, rfc4251uint32 const & x) { +inline std::ostream & operator<< (std::ostream & os, uint32 const & x) { return os.write(x.buf, sizeof(x.buf)); } -inline rfc4251uint32::rfc4251uint32 (std::istream & is) { +inline uint32::uint32 (std::istream & is) { is >> *this; } -struct rfc4251uint64 { +struct uint64 { union { uint64_t value; char buf[8]; }; - rfc4251uint64 () = default; - inline explicit rfc4251uint64 (uint64_t v); - inline explicit rfc4251uint64 (std::istream &); + uint64 () = default; + inline explicit uint64 (uint64_t v); + inline explicit uint64 (std::istream &); inline explicit operator uint64_t () const; }; -inline rfc4251uint64::rfc4251uint64 (uint64_t v) { +inline uint64::uint64 (uint64_t v) { for (int_fast8_t i{7}; i >= 0; --i) { buf[i] = v & 0xff; v >>= 8; } } -inline rfc4251uint64::operator uint64_t () const { +inline uint64::operator uint64_t () const { uint64_t ret{0}; for (uint_fast8_t i{0}; i < 8; ++i) { ret <<= 8; @@ -141,28 +143,28 @@ inline rfc4251uint64::operator uint64_t () const { return ret; } -inline std::istream & operator>> (std::istream & is, rfc4251uint64 & x) { +inline std::istream & operator>> (std::istream & is, uint64 & x) { return is.read(x.buf, sizeof(x.buf)); } -inline std::ostream & operator<< (std::ostream & os, rfc4251uint64 const & x) { +inline std::ostream & operator<< (std::ostream & os, uint64 const & x) { return os.write(x.buf, sizeof(x.buf)); } -inline rfc4251uint64::rfc4251uint64 (std::istream & is) { +inline uint64::uint64 (std::istream & is) { is >> *this; } -struct rfc4251string : boost::totally_ordered { +struct string : boost::totally_ordered { std::vector value; - rfc4251string () = default; - inline explicit rfc4251string (char const *, size_t); - explicit rfc4251string (std::string const & s) : rfc4251string{s.data(), s.size()} {} - explicit rfc4251string (std::vector const &); - explicit rfc4251string (mpz_srcptr); - explicit rfc4251string (mpz_class const & x) : rfc4251string{x.get_mpz_t()} {} - inline explicit rfc4251string (std::istream &); + string () = default; + inline explicit string (char const *, size_t); + explicit string (std::string const & s) : string{s.data(), s.size()} {} + explicit string (std::vector const &); + explicit string (mpz_srcptr); + explicit string (mpz_class const & x) : string{x.get_mpz_t()} {} + inline explicit string (std::istream &); size_t size () const { return value.size(); } char const * data () const { return value.data(); } @@ -173,15 +175,15 @@ struct rfc4251string : boost::totally_ordered { operator mpz_class () const; }; -inline rfc4251string::rfc4251string (char const * s, size_t l) { +inline string::string (char const * s, size_t l) { if (l > std::numeric_limits::max()) - throw std::length_error{"32-bit limit for rfc4251string exceeded"}; + throw std::length_error{"32-bit limit for rfc4251::string exceeded"}; value.insert(value.end(), s, s + l); } -inline std::istream & operator>> (std::istream & is, rfc4251string & s) { +inline std::istream & operator>> (std::istream & is, string & s) { s.value.clear(); - rfc4251uint32 len; + uint32 len; if (is >> len) { s.value.resize(len); is.read(s.value.data(), len); @@ -189,22 +191,24 @@ inline std::istream & operator>> (std::istream & is, rfc4251string & s) { return is; } -inline std::ostream & operator<< (std::ostream & os, rfc4251string const & s) { +inline std::ostream & operator<< (std::ostream & os, string const & s) { if (s.value.size() > std::numeric_limits::max()) - throw std::length_error{"32-bit limit for rfc4251string exceeded"}; - if (os << rfc4251uint32{static_cast(s.value.size())}) + throw std::length_error{"32-bit limit for rfc4251::string exceeded"}; + if (os << uint32{static_cast(s.value.size())}) os.write(s.value.data(), s.value.size()); return os; } -inline rfc4251string::rfc4251string (std::istream & is) { +inline string::string (std::istream & is) { is >> *this; } -inline bool operator== (rfc4251string const & l, rfc4251string const & r) { +inline bool operator== (string const & l, string const & r) { return l.value == r.value; } -inline bool operator< (rfc4251string const & l, rfc4251string const & r) { +inline bool operator< (string const & l, string const & r) { return l.value < r.value; } + +} -- cgit v1.2.3