serialise.hh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #include "types.hh"
  3. namespace nix {
  4. /* Abstract destination of binary data. */
  5. struct Sink
  6. {
  7. virtual ~Sink() { }
  8. virtual void operator () (const unsigned char * data, size_t len) = 0;
  9. };
  10. /* A buffered abstract sink. */
  11. struct BufferedSink : Sink
  12. {
  13. size_t bufSize, bufPos;
  14. unsigned char * buffer;
  15. BufferedSink(size_t bufSize = 32 * 1024)
  16. : bufSize(bufSize), bufPos(0), buffer(0) { }
  17. ~BufferedSink();
  18. void operator () (const unsigned char * data, size_t len);
  19. void flush();
  20. virtual void write(const unsigned char * data, size_t len) = 0;
  21. };
  22. /* Abstract source of binary data. */
  23. struct Source
  24. {
  25. virtual ~Source() { }
  26. /* Store exactly ‘len’ bytes in the buffer pointed to by ‘data’.
  27. It blocks until all the requested data is available, or throws
  28. an error if it is not going to be available. */
  29. void operator () (unsigned char * data, size_t len);
  30. /* Store up to ‘len’ in the buffer pointed to by ‘data’, and
  31. return the number of bytes stored. If blocks until at least
  32. one byte is available. */
  33. virtual size_t read(unsigned char * data, size_t len) = 0;
  34. };
  35. /* A buffered abstract source. */
  36. struct BufferedSource : Source
  37. {
  38. size_t bufSize, bufPosIn, bufPosOut;
  39. unsigned char * buffer;
  40. BufferedSource(size_t bufSize = 32 * 1024)
  41. : bufSize(bufSize), bufPosIn(0), bufPosOut(0), buffer(0) { }
  42. ~BufferedSource();
  43. size_t read(unsigned char * data, size_t len);
  44. /* Underlying read call, to be overridden. */
  45. virtual size_t readUnbuffered(unsigned char * data, size_t len) = 0;
  46. bool hasData();
  47. };
  48. /* A sink that writes data to a file descriptor. */
  49. struct FdSink : BufferedSink
  50. {
  51. int fd;
  52. bool warn;
  53. size_t written;
  54. FdSink() : fd(-1), warn(false), written(0) { }
  55. FdSink(int fd) : fd(fd), warn(false), written(0) { }
  56. ~FdSink();
  57. void write(const unsigned char * data, size_t len);
  58. };
  59. /* A source that reads data from a file descriptor. */
  60. struct FdSource : BufferedSource
  61. {
  62. int fd;
  63. FdSource() : fd(-1) { }
  64. FdSource(int fd) : fd(fd) { }
  65. size_t readUnbuffered(unsigned char * data, size_t len);
  66. };
  67. /* A sink that writes data to a string. */
  68. struct StringSink : Sink
  69. {
  70. string s;
  71. void operator () (const unsigned char * data, size_t len);
  72. };
  73. /* A source that reads data from a string. */
  74. struct StringSource : Source
  75. {
  76. const string & s;
  77. size_t pos;
  78. StringSource(const string & _s) : s(_s), pos(0) { }
  79. size_t read(unsigned char * data, size_t len);
  80. };
  81. void writePadding(size_t len, Sink & sink);
  82. void writeInt(unsigned int n, Sink & sink);
  83. void writeLongLong(unsigned long long n, Sink & sink);
  84. void writeString(const unsigned char * buf, size_t len, Sink & sink);
  85. void writeString(const string & s, Sink & sink);
  86. template<class T> void writeStrings(const T & ss, Sink & sink);
  87. void readPadding(size_t len, Source & source);
  88. unsigned int readInt(Source & source);
  89. unsigned long long readLongLong(Source & source);
  90. size_t readString(unsigned char * buf, size_t max, Source & source);
  91. string readString(Source & source);
  92. template<class T> T readStrings(Source & source);
  93. MakeError(SerialisationError, Error)
  94. }