2006-11-30 19:19:59 +00:00
|
|
|
#include "serialise.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
|
2008-05-21 11:17:31 +00:00
|
|
|
#include <cstring>
|
2011-12-15 12:32:08 +00:00
|
|
|
#include <cerrno>
|
2008-05-21 11:17:31 +00:00
|
|
|
|
2006-11-30 19:19:59 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2011-12-15 16:19:53 +00:00
|
|
|
BufferedSink::~BufferedSink()
|
|
|
|
{
|
|
|
|
/* We can't call flush() here, because C++ for some insane reason
|
|
|
|
doesn't allow you to call virtual methods from a destructor. */
|
|
|
|
assert(!bufPos);
|
|
|
|
if (buffer) delete[] buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BufferedSink::operator () (const unsigned char * data, size_t len)
|
2006-11-30 19:19:59 +00:00
|
|
|
{
|
2011-12-14 23:30:06 +00:00
|
|
|
if (!buffer) buffer = new unsigned char[bufSize];
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
/* Optimisation: bypass the buffer if the data exceeds the
|
2011-12-16 19:44:13 +00:00
|
|
|
buffer size. */
|
|
|
|
if (bufPos + len >= bufSize) {
|
|
|
|
flush();
|
2011-12-15 16:19:53 +00:00
|
|
|
write(data, len);
|
2011-12-14 23:30:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Otherwise, copy the bytes to the buffer. Flush the buffer
|
|
|
|
when it's full. */
|
|
|
|
size_t n = bufPos + len > bufSize ? bufSize - bufPos : len;
|
|
|
|
memcpy(buffer + bufPos, data, n);
|
|
|
|
data += n; bufPos += n; len -= n;
|
|
|
|
if (bufPos == bufSize) flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 16:19:53 +00:00
|
|
|
void BufferedSink::flush()
|
2011-12-14 23:30:06 +00:00
|
|
|
{
|
2011-12-15 16:19:53 +00:00
|
|
|
if (bufPos == 0) return;
|
2011-12-16 15:45:42 +00:00
|
|
|
size_t n = bufPos;
|
|
|
|
bufPos = 0; // don't trigger the assert() in ~BufferedSink()
|
|
|
|
write(buffer, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FdSink::~FdSink()
|
|
|
|
{
|
|
|
|
try { flush(); } catch (...) { ignoreException(); }
|
2006-11-30 19:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 16:19:53 +00:00
|
|
|
void FdSink::write(const unsigned char * data, size_t len)
|
|
|
|
{
|
|
|
|
writeFull(fd, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 19:44:13 +00:00
|
|
|
void Source::operator () (unsigned char * data, size_t len)
|
|
|
|
{
|
|
|
|
while (len) {
|
|
|
|
size_t n = read(data, len);
|
|
|
|
data += n; len -= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 16:19:53 +00:00
|
|
|
BufferedSource::~BufferedSource()
|
|
|
|
{
|
|
|
|
if (buffer) delete[] buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 19:44:13 +00:00
|
|
|
size_t BufferedSource::read(unsigned char * data, size_t len)
|
2006-11-30 19:19:59 +00:00
|
|
|
{
|
2011-12-15 12:32:08 +00:00
|
|
|
if (!buffer) buffer = new unsigned char[bufSize];
|
|
|
|
|
2011-12-16 19:44:13 +00:00
|
|
|
if (!bufPosIn) bufPosIn = readUnbuffered(buffer, bufSize);
|
2011-12-15 12:32:08 +00:00
|
|
|
|
2011-12-16 19:44:13 +00:00
|
|
|
/* Copy out the data in the buffer. */
|
|
|
|
size_t n = len > bufPosIn - bufPosOut ? bufPosIn - bufPosOut : len;
|
|
|
|
memcpy(data, buffer + bufPosOut, n);
|
|
|
|
bufPosOut += n;
|
|
|
|
if (bufPosIn == bufPosOut) bufPosIn = bufPosOut = 0;
|
|
|
|
return n;
|
2006-11-30 19:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-07 13:02:14 +00:00
|
|
|
bool BufferedSource::hasData()
|
|
|
|
{
|
|
|
|
return bufPosOut < bufPosIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 19:44:13 +00:00
|
|
|
size_t FdSource::readUnbuffered(unsigned char * data, size_t len)
|
2011-12-15 16:19:53 +00:00
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
do {
|
|
|
|
checkInterrupt();
|
|
|
|
n = ::read(fd, (char *) data, bufSize);
|
|
|
|
} while (n == -1 && errno == EINTR);
|
|
|
|
if (n == -1) throw SysError("reading from file");
|
|
|
|
if (n == 0) throw EndOfFile("unexpected end-of-file");
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 19:44:13 +00:00
|
|
|
size_t StringSource::read(unsigned char * data, size_t len)
|
|
|
|
{
|
|
|
|
if (pos == s.size()) throw EndOfFile("end of string reached");
|
|
|
|
size_t n = s.copy((char *) data, len, pos);
|
|
|
|
pos += n;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 16:19:53 +00:00
|
|
|
void writePadding(size_t len, Sink & sink)
|
2006-11-30 19:19:59 +00:00
|
|
|
{
|
|
|
|
if (len % 8) {
|
|
|
|
unsigned char zero[8];
|
|
|
|
memset(zero, 0, sizeof(zero));
|
|
|
|
sink(zero, 8 - (len % 8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void writeInt(unsigned int n, Sink & sink)
|
|
|
|
{
|
|
|
|
unsigned char buf[8];
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
buf[0] = n & 0xff;
|
|
|
|
buf[1] = (n >> 8) & 0xff;
|
|
|
|
buf[2] = (n >> 16) & 0xff;
|
|
|
|
buf[3] = (n >> 24) & 0xff;
|
|
|
|
sink(buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 09:34:17 +00:00
|
|
|
void writeLongLong(unsigned long long n, Sink & sink)
|
|
|
|
{
|
|
|
|
unsigned char buf[8];
|
|
|
|
buf[0] = n & 0xff;
|
|
|
|
buf[1] = (n >> 8) & 0xff;
|
|
|
|
buf[2] = (n >> 16) & 0xff;
|
|
|
|
buf[3] = (n >> 24) & 0xff;
|
|
|
|
buf[4] = (n >> 32) & 0xff;
|
|
|
|
buf[5] = (n >> 40) & 0xff;
|
|
|
|
buf[6] = (n >> 48) & 0xff;
|
|
|
|
buf[7] = (n >> 56) & 0xff;
|
|
|
|
sink(buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 21:29:46 +00:00
|
|
|
void writeString(const unsigned char * buf, size_t len, Sink & sink)
|
2006-11-30 19:19:59 +00:00
|
|
|
{
|
|
|
|
writeInt(len, sink);
|
2011-12-16 21:29:46 +00:00
|
|
|
sink(buf, len);
|
2006-11-30 19:19:59 +00:00
|
|
|
writePadding(len, sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 21:29:46 +00:00
|
|
|
void writeString(const string & s, Sink & sink)
|
|
|
|
{
|
2012-02-09 17:27:45 +00:00
|
|
|
writeString((const unsigned char *) s.data(), s.size(), sink);
|
2011-12-16 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 22:31:25 +00:00
|
|
|
template<class T> void writeStrings(const T & ss, Sink & sink)
|
2006-11-30 22:43:55 +00:00
|
|
|
{
|
|
|
|
writeInt(ss.size(), sink);
|
2011-12-16 22:31:25 +00:00
|
|
|
foreach (typename T::const_iterator, i, ss)
|
2006-11-30 22:43:55 +00:00
|
|
|
writeString(*i, sink);
|
|
|
|
}
|
|
|
|
|
2011-12-16 22:31:25 +00:00
|
|
|
template void writeStrings(const Paths & ss, Sink & sink);
|
|
|
|
template void writeStrings(const PathSet & ss, Sink & sink);
|
|
|
|
|
2006-11-30 22:43:55 +00:00
|
|
|
|
2011-12-15 16:19:53 +00:00
|
|
|
void readPadding(size_t len, Source & source)
|
2006-11-30 19:19:59 +00:00
|
|
|
{
|
|
|
|
if (len % 8) {
|
|
|
|
unsigned char zero[8];
|
2011-12-15 16:19:53 +00:00
|
|
|
size_t n = 8 - (len % 8);
|
2006-11-30 19:19:59 +00:00
|
|
|
source(zero, n);
|
|
|
|
for (unsigned int i = 0; i < n; i++)
|
2009-03-22 17:36:43 +00:00
|
|
|
if (zero[i]) throw SerialisationError("non-zero padding");
|
2006-11-30 19:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int readInt(Source & source)
|
|
|
|
{
|
|
|
|
unsigned char buf[8];
|
|
|
|
source(buf, sizeof(buf));
|
|
|
|
if (buf[4] || buf[5] || buf[6] || buf[7])
|
2009-03-22 17:36:43 +00:00
|
|
|
throw SerialisationError("implementation cannot deal with > 32-bit integers");
|
2006-11-30 19:19:59 +00:00
|
|
|
return
|
|
|
|
buf[0] |
|
|
|
|
(buf[1] << 8) |
|
|
|
|
(buf[2] << 16) |
|
|
|
|
(buf[3] << 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 09:34:17 +00:00
|
|
|
unsigned long long readLongLong(Source & source)
|
|
|
|
{
|
|
|
|
unsigned char buf[8];
|
|
|
|
source(buf, sizeof(buf));
|
|
|
|
return
|
|
|
|
((unsigned long long) buf[0]) |
|
|
|
|
((unsigned long long) buf[1] << 8) |
|
|
|
|
((unsigned long long) buf[2] << 16) |
|
|
|
|
((unsigned long long) buf[3] << 24) |
|
|
|
|
((unsigned long long) buf[4] << 32) |
|
|
|
|
((unsigned long long) buf[5] << 40) |
|
|
|
|
((unsigned long long) buf[6] << 48) |
|
|
|
|
((unsigned long long) buf[7] << 56);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 21:29:46 +00:00
|
|
|
size_t readString(unsigned char * buf, size_t max, Source & source)
|
|
|
|
{
|
|
|
|
size_t len = readInt(source);
|
|
|
|
if (len > max) throw Error("string is too long");
|
|
|
|
source(buf, len);
|
|
|
|
readPadding(len, source);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-30 19:19:59 +00:00
|
|
|
string readString(Source & source)
|
|
|
|
{
|
2011-12-15 16:19:53 +00:00
|
|
|
size_t len = readInt(source);
|
2006-12-04 17:17:13 +00:00
|
|
|
unsigned char * buf = new unsigned char[len];
|
|
|
|
AutoDeleteArray<unsigned char> d(buf);
|
|
|
|
source(buf, len);
|
2006-11-30 19:19:59 +00:00
|
|
|
readPadding(len, source);
|
2006-12-04 17:17:13 +00:00
|
|
|
return string((char *) buf, len);
|
2006-11-30 19:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 22:31:25 +00:00
|
|
|
template<class T> T readStrings(Source & source)
|
2006-11-30 22:43:55 +00:00
|
|
|
{
|
|
|
|
unsigned int count = readInt(source);
|
2011-12-16 22:31:25 +00:00
|
|
|
T ss;
|
2006-11-30 22:43:55 +00:00
|
|
|
while (count--)
|
2011-12-16 22:31:25 +00:00
|
|
|
ss.insert(ss.end(), readString(source));
|
2006-11-30 22:43:55 +00:00
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
2011-12-16 22:31:25 +00:00
|
|
|
template Paths readStrings(Source & source);
|
|
|
|
template PathSet readStrings(Source & source);
|
|
|
|
|
2006-11-30 22:43:55 +00:00
|
|
|
|
2006-11-30 19:19:59 +00:00
|
|
|
}
|