2006-11-30 21:19:59 +02:00
|
|
|
#include "serialise.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
|
2008-05-21 14:17:31 +03:00
|
|
|
#include <cstring>
|
|
|
|
|
2006-11-30 21:19:59 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
void FdSink::operator () (const unsigned char * data, unsigned int len)
|
|
|
|
{
|
|
|
|
writeFull(fd, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FdSource::operator () (unsigned char * data, unsigned int len)
|
|
|
|
{
|
|
|
|
readFull(fd, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void writePadding(unsigned int len, Sink & sink)
|
|
|
|
{
|
|
|
|
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 12:34:17 +03: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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-30 21:19:59 +02:00
|
|
|
void writeString(const string & s, Sink & sink)
|
|
|
|
{
|
|
|
|
unsigned int len = s.length();
|
|
|
|
writeInt(len, sink);
|
|
|
|
sink((const unsigned char *) s.c_str(), len);
|
|
|
|
writePadding(len, sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-01 00:43:55 +02:00
|
|
|
void writeStringSet(const StringSet & ss, Sink & sink)
|
|
|
|
{
|
|
|
|
writeInt(ss.size(), sink);
|
|
|
|
for (StringSet::iterator i = ss.begin(); i != ss.end(); ++i)
|
|
|
|
writeString(*i, sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-30 21:19:59 +02:00
|
|
|
void readPadding(unsigned int len, Source & source)
|
|
|
|
{
|
|
|
|
if (len % 8) {
|
|
|
|
unsigned char zero[8];
|
|
|
|
unsigned int n = 8 - (len % 8);
|
|
|
|
source(zero, n);
|
|
|
|
for (unsigned int i = 0; i < n; i++)
|
|
|
|
if (zero[i]) throw Error("non-zero padding");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int readInt(Source & source)
|
|
|
|
{
|
|
|
|
unsigned char buf[8];
|
|
|
|
source(buf, sizeof(buf));
|
|
|
|
if (buf[4] || buf[5] || buf[6] || buf[7])
|
|
|
|
throw Error("implementation cannot deal with > 32-bit integers");
|
|
|
|
return
|
|
|
|
buf[0] |
|
|
|
|
(buf[1] << 8) |
|
|
|
|
(buf[2] << 16) |
|
|
|
|
(buf[3] << 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 12:34:17 +03: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-30 21:19:59 +02:00
|
|
|
string readString(Source & source)
|
|
|
|
{
|
|
|
|
unsigned int len = readInt(source);
|
2006-12-04 19:17:13 +02:00
|
|
|
unsigned char * buf = new unsigned char[len];
|
|
|
|
AutoDeleteArray<unsigned char> d(buf);
|
|
|
|
source(buf, len);
|
2006-11-30 21:19:59 +02:00
|
|
|
readPadding(len, source);
|
2006-12-04 19:17:13 +02:00
|
|
|
return string((char *) buf, len);
|
2006-11-30 21:19:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-01 00:43:55 +02:00
|
|
|
StringSet readStringSet(Source & source)
|
|
|
|
{
|
|
|
|
unsigned int count = readInt(source);
|
|
|
|
StringSet ss;
|
|
|
|
while (count--)
|
|
|
|
ss.insert(readString(source));
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-30 21:19:59 +02:00
|
|
|
}
|