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