#pragma once #include "types.hh" namespace nix { /* Abstract destination of binary data. */ struct Sink { virtual ~Sink() { } virtual void operator () (const unsigned char * data, size_t len) = 0; }; /* A buffered abstract sink. */ struct BufferedSink : Sink { size_t bufSize, bufPos; unsigned char * buffer; BufferedSink(size_t bufSize = 32 * 1024) : bufSize(bufSize), bufPos(0), buffer(0) { } ~BufferedSink(); void operator () (const unsigned char * data, size_t len); void flush(); virtual void write(const unsigned char * data, size_t len) = 0; }; /* Abstract source of binary data. */ struct Source { virtual ~Source() { } /* Store exactly ‘len’ bytes in the buffer pointed to by ‘data’. It blocks until all the requested data is available, or throws an error if it is not going to be available. */ void operator () (unsigned char * data, size_t len); /* Store up to ‘len’ in the buffer pointed to by ‘data’, and return the number of bytes stored. If blocks until at least one byte is available. */ virtual size_t read(unsigned char * data, size_t len) = 0; }; /* A buffered abstract source. */ struct BufferedSource : Source { size_t bufSize, bufPosIn, bufPosOut; unsigned char * buffer; BufferedSource(size_t bufSize = 32 * 1024) : bufSize(bufSize), bufPosIn(0), bufPosOut(0), buffer(0) { } ~BufferedSource(); size_t read(unsigned char * data, size_t len); /* Underlying read call, to be overridden. */ virtual size_t readUnbuffered(unsigned char * data, size_t len) = 0; bool hasData(); }; /* A sink that writes data to a file descriptor. */ struct FdSink : BufferedSink { int fd; bool warn; size_t written; FdSink() : fd(-1), warn(false), written(0) { } FdSink(int fd) : fd(fd), warn(false), written(0) { } ~FdSink(); void write(const unsigned char * data, size_t len); }; /* A source that reads data from a file descriptor. */ struct FdSource : BufferedSource { int fd; FdSource() : fd(-1) { } FdSource(int fd) : fd(fd) { } size_t readUnbuffered(unsigned char * data, size_t len); }; /* A sink that writes data to a string. */ struct StringSink : Sink { string s; void operator () (const unsigned char * data, size_t len); }; /* A source that reads data from a string. */ struct StringSource : Source { const string & s; size_t pos; StringSource(const string & _s) : s(_s), pos(0) { } size_t read(unsigned char * data, size_t len); }; void writePadding(size_t len, Sink & sink); void writeInt(unsigned int n, Sink & sink); void writeLongLong(unsigned long long n, Sink & sink); void writeString(const unsigned char * buf, size_t len, Sink & sink); void writeString(const string & s, Sink & sink); template void writeStrings(const T & ss, Sink & sink); void readPadding(size_t len, Source & source); unsigned int readInt(Source & source); unsigned long long readLongLong(Source & source); size_t readString(unsigned char * buf, size_t max, Source & source); string readString(Source & source); template T readStrings(Source & source); MakeError(SerialisationError, Error) } 659e0e7fd5cbf467f844'>maint: Avoid macros obsolete in Autoconf 2.70....Ludovic Courtès 2020-10-22git: Require Guile-Git 0.3.0 or later....Ludovic Courtès 2020-09-08Remove (guix json) and require Guile-JSON 4.3.0+....Ludovic Courtès 2020-08-29build: Remove check for Guile 2.2.1 bug....Ludovic Courtès 2020-08-24Use "guile-zlib" and "guile-lzlib" instead of (guix config)....Mathieu Othacehe 2020-07-20ssh: Speed up RPCs by using #:nodelay....Lars-Dominik Braun 2020-05-28doc: Remove explicit support for mips64el-linux....Efraim Flashner 2020-05-28maint: Check whether Guile-Gcrypt is recent enough....Ludovic Courtès 2020-02-22build: Depend on guile-ssh 0.12.0...Lars-Dominik Braun 2019-08-17build: 'GUIX_CHECK_GUILE_JSON' really checks for Guile-JSON 3.x....Ludovic Courtès 2019-07-25maint: Switch to Guile-JSON 3.x....Ludovic Courtès 2019-05-06Add (guix lzlib)....Pierre Neidhardt 2018-11-23Update Guile-SQLite3 URL everywhere....Ludovic Courtès 2018-09-04Switch to Guile-Gcrypt....Ludovic Courtès 2018-06-01build: Check for Guile-SQLite3....Ludovic Courtès