/* * rfc4251.h -- implements types from RFC 4251, section 5 * * 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 * * Copyright (C) 2013-2015,2021 Timo Weingärtner * * This file is part of ssh-agent-filter. * * ssh-agent-filter is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ssh-agent-filter is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ssh-agent-filter. If not, see . */ #include #include #include #include #include #include #include #include #include #include namespace rfc4251 { namespace internal { template constexpr inline auto host_to_network (T value) noexcept { static_assert(std::is_unsigned_v, "shift is only supported on unsigned"); std::array ret; for (auto it{rbegin(ret)}; it != rend(ret); ++it) { *it = value & 0xff; if constexpr (sizeof(T) > 1) value >>= 8; } return ret; } template constexpr inline auto network_to_host (std::array const buf) noexcept { static_assert(std::is_unsigned_v, "shift is only supported on unsigned"); T ret{0}; for (auto it{cbegin(buf)}; it != cend(buf); ++it) { if constexpr (sizeof(T) > 1) ret <<= 8; ret |= static_cast(*it); } return ret; } template struct fixed_length { static_assert(std::is_unsigned_v, "support for signed types might need more work"); std::array buf{}; constexpr fixed_length () noexcept = default; constexpr explicit fixed_length (T const v) noexcept : buf{host_to_network(v)} {} explicit fixed_length (std::istream & is) { is >> *this; } constexpr operator T () const noexcept { return network_to_host(buf); } friend std::istream & operator>> (std::istream & is, fixed_length & x) { return is.read(x.buf.data(), x.buf.size()); } friend std::ostream & operator<< (std::ostream & os, fixed_length const & x) { return os.write(x.buf.data(), x.buf.size()); } }; } using byte = internal::fixed_length; using boolean = internal::fixed_length; using uint32 = internal::fixed_length; using uint64 = internal::fixed_length; struct string : boost::totally_ordered { std::vector value; 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 noexcept { return value.size(); } char const * data () const noexcept { return value.data(); } char * data () noexcept { return value.data(); } operator std::string () const { return {value.begin(), value.end()}; } operator std::vector () const; operator mpz_class () const; }; inline string::string (char const * s, size_t l) { if (l > std::numeric_limits::max()) 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, string & s) { s.value.clear(); if (uint32_t const len{uint32{is}}; is) { s.value.resize(len); is.read(s.value.data(), len); } return is; } 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 rfc4251::string exceeded"}; if (os << uint32{static_cast(s.value.size())}) os.write(s.value.data(), s.value.size()); return os; } inline string::string (std::istream & is) { is >> *this; } inline bool operator== (string const & l, string const & r) { return l.value == r.value; } inline bool operator< (string const & l, string const & r) { return l.value < r.value; } }