diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-11-30 19:19:59 +0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-11-30 19:19:59 +0000 |
commit | 40b3f64b55f98e03b3173541b8d94cd924099223 (patch) | |
tree | 78a14654425eab2729e3c8077860573766a794c0 /src/libutil/serialise.cc | |
parent | 9adc074dc3e135356c2390038bf72264c29c1e03 (diff) | |
download | guix-40b3f64b55f98e03b3173541b8d94cd924099223.tar.gz |
* Skeleton of the privileged worker program.
* Some refactoring: put the NAR archive integer/string serialisation code in a separate file so it can be reused by the worker protocol implementation.
Diffstat (limited to 'src/libutil/serialise.cc')
-rw-r--r-- | src/libutil/serialise.cc | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc new file mode 100644 index 0000000000..aa11c590af --- /dev/null +++ b/src/libutil/serialise.cc @@ -0,0 +1,87 @@ +#include "serialise.hh" +#include "util.hh" + + +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)); +} + + +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); +} + + +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); +} + + +string readString(Source & source) +{ + unsigned int len = readInt(source); + char buf[len]; + source((unsigned char *) buf, len); + readPadding(len, source); + return string(buf, len); +} + + +} |