libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
io.hh File Reference

Low-level binary reading and writing helpers. More...

#include <istream>
#include <memory>
#include <ostream>
#include <string>
#include <vector>
#include "exception.hh"
#include "libkeepass/export.hh"
#include "libkeepass/stream.hh"

Go to the source code of this file.

Namespaces

namespace  keepass::io
 Stable names for the reusable stream adapters.
 

Functions

template<typename T >
T keepass::consume (std::istream &src)
 Reads and returns a value of type T from the stream.
 
LIBKEEPASS_API std::streamsize keepass::RemainingBytes (std::istream &src)
 Returns the number of bytes available from the current position of the stream up to its end, or -1 if the stream does not support or cannot report seeking.
 
template<>
LIBKEEPASS_API std::string keepass::consume< std::string > (std::istream &src)
 Reads a length-prefixed string from the stream.
 
template<>
LIBKEEPASS_API std::vector< char > keepass::consume< std::vector< char > > (std::istream &src)
 Reads a length-prefixed char vector from the stream.
 
template<>
LIBKEEPASS_API std::vector< uint8_t > keepass::consume< std::vector< uint8_t > > (std::istream &src)
 Reads a length-prefixed byte vector from the stream.
 
template<typename T >
void keepass::conserve (std::ostream &dst, const T &val)
 Writes a value of type T to the stream.
 
template<>
LIBKEEPASS_API void keepass::conserve< std::string > (std::ostream &dst, const std::string &val)
 Writes a length-prefixed string to the stream.
 
template<>
LIBKEEPASS_API void keepass::conserve< std::vector< char > > (std::ostream &dst, const std::vector< char > &val)
 Writes a length-prefixed char vector to the stream.
 
template<>
LIBKEEPASS_API void keepass::conserve< std::vector< uint8_t > > (std::ostream &dst, const std::vector< uint8_t > &val)
 Writes a length-prefixed byte vector to the stream.
 

Detailed Description

Low-level binary reading and writing helpers.

Function Documentation

◆ conserve()

template<typename T >
void keepass::conserve ( std::ostream &  dst,
const T &  val 
)

Writes a value of type T to the stream.

Template Parameters
Tthe type to write (POD by default, specialized for std::string and byte vectors).
Parameters
dstthe output stream to write to.
valthe value to write.

◆ consume()

template<typename T >
T keepass::consume ( std::istream &  src)
inline

Reads and returns a value of type T from the stream.

Template Parameters
Tthe type to read (POD by default, specialized for std::string and byte vectors).
Parameters
srcthe input stream to read from.
Returns
the value read from the stream.
Exceptions
IoErrorif the stream cannot be read.

◆ RemainingBytes()

LIBKEEPASS_API std::streamsize keepass::RemainingBytes ( std::istream &  src)

Returns the number of bytes available from the current position of the stream up to its end, or -1 if the stream does not support or cannot report seeking.

Intended to bound reads driven by untrusted length fields without having to cap them at an arbitrary value.