/src/gnupg/common/iobuf.h
Line | Count | Source |
1 | | /* iobuf.h - I/O buffer |
2 | | * Copyright (C) 1998, 1999, 2000, 2001, 2003, |
3 | | * 2010 Free Software Foundation, Inc. |
4 | | * |
5 | | * This file is part of GnuPG. |
6 | | * |
7 | | * This file is free software; you can redistribute it and/or modify |
8 | | * it under the terms of either |
9 | | * |
10 | | * - the GNU Lesser General Public License as published by the Free |
11 | | * Software Foundation; either version 3 of the License, or (at |
12 | | * your option) any later version. |
13 | | * |
14 | | * or |
15 | | * |
16 | | * - the GNU General Public License as published by the Free |
17 | | * Software Foundation; either version 2 of the License, or (at |
18 | | * your option) any later version. |
19 | | * |
20 | | * or both in parallel, as here. |
21 | | * |
22 | | * This file is distributed in the hope that it will be useful, |
23 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 | | * GNU General Public License for more details. |
26 | | * |
27 | | * You should have received a copy of the GNU General Public License |
28 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
29 | | */ |
30 | | |
31 | | #ifndef GNUPG_COMMON_IOBUF_H |
32 | | #define GNUPG_COMMON_IOBUF_H |
33 | | |
34 | | /* An iobuf is basically a filter in a pipeline. |
35 | | |
36 | | Consider the following command, which consists of three filters |
37 | | that are chained together: |
38 | | |
39 | | $ cat file | base64 --decode | gunzip |
40 | | |
41 | | The first filter reads the file from the file system and sends that |
42 | | data to the second filter. The second filter decodes |
43 | | base64-encoded data and sends the data to the third and last |
44 | | filter. The last filter decompresses the data and the result is |
45 | | displayed on the terminal. The iobuf system works in the same way |
46 | | where each iobuf is a filter and the individual iobufs can be |
47 | | chained together. |
48 | | |
49 | | There are number of predefined filters. iobuf_open(), for |
50 | | instance, creates a filter that reads from a specified file. And, |
51 | | iobuf_temp_with_content() creates a filter that returns some |
52 | | specified contents. There are also filters for writing content. |
53 | | iobuf_openrw opens a file for writing. iobuf_temp creates a filter |
54 | | that writes data to a fixed-sized buffer. |
55 | | |
56 | | To chain filters together, you use the iobuf_push_filter() |
57 | | function. The filters are chained together using the chain field |
58 | | in the iobuf_t. |
59 | | |
60 | | A pipeline can only be used for reading (IOBUF_INPUT) or for |
61 | | writing (IOBUF_OUTPUT / IOBUF_OUTPUT_TEMP). When reading, data |
62 | | flows from the last filter towards the first. That is, the user |
63 | | calls iobuf_read(), the module reads from the first filter, which |
64 | | gets its input from the second filter, etc. When writing, data |
65 | | flows from the first filter towards the last. In this case, when |
66 | | the user calls iobuf_write(), the data is written to the first |
67 | | filter, which writes the transformed data to the second filter, |
68 | | etc. |
69 | | |
70 | | An iobuf_t contains some state about the filter. For instance, it |
71 | | indicates if the filter has already returned EOF (filter_eof) and |
72 | | the next filter in the pipeline, if any (chain). It also contains |
73 | | a function pointer, filter. This is a generic function. It is |
74 | | called when input is needed or output is available. In this case |
75 | | it is passed a pointer to some filter-specific persistent state |
76 | | (filter_ov), the actual operation, the next filter in the chain, if |
77 | | any, and a buffer that either contains the contents to write, if |
78 | | the pipeline is setup to write data, or is the place to store data, |
79 | | if the pipeline is setup to read data. |
80 | | |
81 | | |
82 | | Unlike a Unix pipeline, an IOBUF pipeline can return EOF multiple |
83 | | times. This is similar to the following: |
84 | | |
85 | | { cat file1; cat file2; } | grep foo |
86 | | |
87 | | However, instead of grep seeing a single stream, grep would see |
88 | | each byte stream followed by an EOF marker. (When a filter returns |
89 | | EOF, the EOF is returned to the user exactly once and then the |
90 | | filter is removed from the pipeline.) */ |
91 | | |
92 | | /* For estream_t. */ |
93 | | #include <gpg-error.h> |
94 | | |
95 | | #include "../common/types.h" |
96 | | #include "../common/sysutils.h" |
97 | | |
98 | 486 | #define DBG_IOBUF iobuf_debug_mode |
99 | | |
100 | | /* Filter control modes. */ |
101 | | enum |
102 | | { |
103 | | IOBUFCTRL_INIT = 1, |
104 | | IOBUFCTRL_FREE = 2, |
105 | | IOBUFCTRL_UNDERFLOW = 3, |
106 | | IOBUFCTRL_FLUSH = 4, |
107 | | IOBUFCTRL_DESC = 5, |
108 | | IOBUFCTRL_CANCEL = 6, |
109 | | IOBUFCTRL_PEEK = 7, |
110 | | IOBUFCTRL_USER = 16 |
111 | | }; |
112 | | |
113 | | |
114 | | /* Command codes for iobuf_ioctl. */ |
115 | | typedef enum |
116 | | { |
117 | | IOBUF_IOCTL_KEEP_OPEN = 1, /* Uses intval. */ |
118 | | IOBUF_IOCTL_INVALIDATE_CACHE = 2, /* Uses ptrval. */ |
119 | | IOBUF_IOCTL_NO_CACHE = 3, /* Uses intval. */ |
120 | | IOBUF_IOCTL_FSYNC = 4, /* Uses ptrval. */ |
121 | | IOBUF_IOCTL_PEEK = 5 /* Uses intval and ptrval. */ |
122 | | } iobuf_ioctl_t; |
123 | | |
124 | | enum iobuf_use |
125 | | { |
126 | | /* Pipeline is in input mode. The data flows from the end to the |
127 | | beginning. That is, when reading from the pipeline, the first |
128 | | filter gets its input from the second filter, etc. */ |
129 | | IOBUF_INPUT, |
130 | | /* Pipeline is in input mode. The last filter in the pipeline is |
131 | | a temporary buffer from which the data is "read". */ |
132 | | IOBUF_INPUT_TEMP, |
133 | | /* Pipeline is in output mode. The data flows from the beginning |
134 | | to the end. That is, when writing to the pipeline, the user |
135 | | writes to the first filter, which transforms the data and sends |
136 | | it to the second filter, etc. */ |
137 | | IOBUF_OUTPUT, |
138 | | /* Pipeline is in output mode. The last filter in the pipeline is |
139 | | a temporary buffer that grows as necessary. */ |
140 | | IOBUF_OUTPUT_TEMP |
141 | | }; |
142 | | |
143 | | |
144 | | typedef struct iobuf_struct *iobuf_t; |
145 | | typedef struct iobuf_struct *IOBUF; /* Compatibility with gpg 1.4. */ |
146 | | |
147 | | /* fixme: we should hide most of this stuff */ |
148 | | struct iobuf_struct |
149 | | { |
150 | | /* The type of filter. Either IOBUF_INPUT, IOBUF_OUTPUT or |
151 | | IOBUF_OUTPUT_TEMP. */ |
152 | | enum iobuf_use use; |
153 | | |
154 | | /* nlimit can be changed using iobuf_set_limit. If non-zero, it is |
155 | | the number of additional bytes that can be read from the filter |
156 | | before EOF is forcefully returned. */ |
157 | | off_t nlimit; |
158 | | /* nbytes if the number of bytes that have been read (using |
159 | | iobuf_get / iobuf_readbyte / iobuf_read) since the last call to |
160 | | iobuf_set_limit. */ |
161 | | off_t nbytes; |
162 | | |
163 | | /* The number of bytes read prior to the last call to |
164 | | iobuf_set_limit. Thus, the total bytes read (i.e., the position |
165 | | of stream) is ntotal + nbytes. */ |
166 | | off_t ntotal; |
167 | | |
168 | | /* Whether we need to read from the filter one byte at a time or |
169 | | whether we can do bulk reads. We need to read one byte at a time |
170 | | if a limit (set via iobuf_set_limit) is active. */ |
171 | | int nofast; |
172 | | |
173 | | /* A buffer for unread/unwritten data. |
174 | | |
175 | | For an output pipeline (IOBUF_OUTPUT), this is the data that has |
176 | | not yet been written to the filter. Consider a simple pipeline |
177 | | consisting of a single stage, which writes to a file. When you |
178 | | write to the pipeline (iobuf_writebyte or iobuf_write), the data |
179 | | is first stored in this buffer. Only when the buffer is full or |
180 | | you call iobuf_flush() is FILTER actually called and the data |
181 | | written to the file. |
182 | | |
183 | | For an input pipeline (IOBUF_INPUT), this is the data that has |
184 | | been read from this filter, but not yet been read from the |
185 | | preceding filter (or the user, if this filter is the head of the |
186 | | pipeline). Again, consider a simple pipeline consisting of a |
187 | | single stage. This stage reads from a file. If you read a |
188 | | single byte (iobuf_get) and the buffer is empty, then FILTER is |
189 | | called to fill the buffer. In this case, a single byte is not |
190 | | requested, but the whole buffer is filled (if possible). */ |
191 | | struct |
192 | | { |
193 | | /* Size of the buffer. */ |
194 | | size_t size; |
195 | | /* Number of bytes at the beginning of the buffer that have |
196 | | already been consumed. (In other words: the index of the first |
197 | | byte that hasn't been consumed.) This is only non-zero for |
198 | | input filters. */ |
199 | | size_t start; |
200 | | /* The number of bytes in the buffer including any bytes that have |
201 | | been consumed. */ |
202 | | size_t len; |
203 | | /* The buffer itself. */ |
204 | | byte *buf; |
205 | | } d; |
206 | | |
207 | | /* A external drain buffer for reading/writing data skipping internal |
208 | | draint buffer D.BUF. This allows zerocopy operation reducing |
209 | | processing overhead across filter stack. |
210 | | |
211 | | Used when by iobuf_read/iobuf_write when internal buffer has been |
212 | | depleted and remaining external buffer length is large enough. |
213 | | */ |
214 | | struct |
215 | | { |
216 | | /* The external buffer provided by iobuf_read/iobuf_write caller. */ |
217 | | byte *buf; |
218 | | /* The number of bytes in the external buffer. */ |
219 | | size_t len; |
220 | | /* The number of bytes that were consumed from the external buffer. */ |
221 | | size_t used; |
222 | | /* Gives hint for processing that the external buffer is preferred and |
223 | | that internal buffer should be consumed early. */ |
224 | | int preferred; |
225 | | } e_d; |
226 | | |
227 | | /* When FILTER is called to read some data, it may read some data |
228 | | and then return EOF. We can't return the EOF immediately. |
229 | | Instead, we note that we observed the EOF and when the buffer is |
230 | | finally empty, we return the EOF. */ |
231 | | int filter_eof; |
232 | | /* Like filter_eof, when FILTER is called to read some data, it may |
233 | | read some data and then return an error. We can't return the |
234 | | error (in the form of an EOF) immediately. Instead, we note that |
235 | | we observed the error and when the buffer is finally empty, we |
236 | | return the EOF. */ |
237 | | int error; |
238 | | |
239 | | /* The callback function to read data from the filter, etc. See |
240 | | iobuf_filter_push for details. */ |
241 | | int (*filter) (void *opaque, int control, |
242 | | iobuf_t chain, byte * buf, size_t * len); |
243 | | /* An opaque pointer that can be used for local filter state. This |
244 | | is passed as the first parameter to FILTER. */ |
245 | | void *filter_ov; |
246 | | /* Whether the iobuf code should free(filter_ov) when destroying the |
247 | | filter. */ |
248 | | int filter_ov_owner; |
249 | | |
250 | | /* When using iobuf_open, iobuf_create, iobuf_openrw to open a file, |
251 | | the file's name is saved here. This is used to delete the file |
252 | | when an output pipeline (IOBUF_OUPUT) is canceled |
253 | | (iobuf_cancel). */ |
254 | | char *real_fname; |
255 | | |
256 | | /* The next filter in the pipeline. */ |
257 | | iobuf_t chain; |
258 | | |
259 | | /* This field is for debugging. Each time a filter is allocated |
260 | | (via iobuf_alloc()), a monotonically increasing counter is |
261 | | incremented and this field is set to the new value. This field |
262 | | should only be accessed via the iobuf_io macro. */ |
263 | | int no; |
264 | | |
265 | | /* The number of filters in the pipeline following (not including) |
266 | | this one. When you call iobuf_push_filter or iobuf_push_filter2, |
267 | | this value is used to check the length of the pipeline if the |
268 | | pipeline already contains 65 stages then these functions fail. |
269 | | This amount of nesting typically indicates corrupted data or an |
270 | | active denial of service attack. */ |
271 | | int subno; |
272 | | }; |
273 | | |
274 | | extern int iobuf_debug_mode; |
275 | | |
276 | | |
277 | | /* Change the default size for all IOBUFs to KILOBYTE. This needs to |
278 | | * be called before any iobufs are used and can only be used once. |
279 | | * Returns the current value. Using 0 has no effect except for |
280 | | * returning the current value. */ |
281 | | unsigned int iobuf_set_buffer_size (unsigned int kilobyte); |
282 | | |
283 | | /* Returns whether the specified filename corresponds to a pipe. In |
284 | | particular, this function checks if FNAME is "-" and, if special |
285 | | filenames are enabled (see check_special_filename), whether |
286 | | FNAME is a special filename. */ |
287 | | int iobuf_is_pipe_filename (const char *fname); |
288 | | |
289 | | /* Allocate a new filter. This filter doesn't have a function |
290 | | assigned to it. Thus you need to manually set IOBUF->FILTER and |
291 | | IOBUF->FILTER_OV, if required. This function is intended to help |
292 | | create a new primary source or primary sink, i.e., the last filter |
293 | | in the pipeline. |
294 | | |
295 | | USE is IOBUF_INPUT, IOBUF_INPUT_TEMP, IOBUF_OUTPUT or |
296 | | IOBUF_OUTPUT_TEMP. |
297 | | |
298 | | BUFSIZE is the desired internal buffer size (that is, the size of |
299 | | the typical read / write request). */ |
300 | | iobuf_t iobuf_alloc (int use, size_t bufsize); |
301 | | |
302 | | /* Create an output filter that simply buffers data written to it. |
303 | | This is useful for collecting data for later processing. The |
304 | | buffer can be written to in the usual way (iobuf_write, etc.). The |
305 | | data can later be extracted using iobuf_write_temp() or |
306 | | iobuf_temp_to_buffer(). */ |
307 | | iobuf_t iobuf_temp (void); |
308 | | |
309 | | /* Create an input filter that contains some data for reading. */ |
310 | | iobuf_t iobuf_temp_with_content (const char *buffer, size_t length); |
311 | | |
312 | | /* Create an input file filter that reads from a file. If FNAME is |
313 | | '-', reads from stdin. If special filenames are enabled |
314 | | (iobuf_enable_special_filenames), then interprets special |
315 | | filenames. */ |
316 | | iobuf_t iobuf_open (const char *fname); |
317 | | |
318 | | /* Create an output file filter that writes to a file. If FNAME is |
319 | | NULL or '-', writes to stdout. If special filenames are enabled |
320 | | (iobuf_enable_special_filenames), then interprets special |
321 | | filenames. If FNAME is not NULL, '-' or a special filename, the |
322 | | file is opened for writing. If the file exists, it is truncated. |
323 | | If MODE700 is TRUE, the file is created with mode 600. Otherwise, |
324 | | mode 666 is used. */ |
325 | | iobuf_t iobuf_create (const char *fname, int mode700); |
326 | | |
327 | | /* Create an output file filter that writes to a specified file. |
328 | | Neither '-' nor special file names are recognized. */ |
329 | | iobuf_t iobuf_openrw (const char *fname); |
330 | | |
331 | | /* Create a file filter using an existing file descriptor. If MODE |
332 | | contains the letter 'w', creates an output filter. Otherwise, |
333 | | creates an input filter. Note: MODE must reflect the file |
334 | | descriptors actual mode! When the filter is destroyed, the file |
335 | | descriptor is closed. */ |
336 | | iobuf_t iobuf_fdopen (gnupg_fd_t fd, const char *mode); |
337 | | |
338 | | /* Like iobuf_fdopen, but doesn't close the file descriptor when the |
339 | | filter is destroyed. */ |
340 | | iobuf_t iobuf_fdopen_nc (gnupg_fd_t fd, const char *mode); |
341 | | |
342 | | /* Create a filter using an existing estream. If MODE contains the |
343 | | letter 'w', creates an output filter. Otherwise, creates an input |
344 | | filter. If KEEP_OPEN is TRUE, then the stream is not closed when |
345 | | the filter is destroyed. Otherwise, the stream is closed when the |
346 | | filter is destroyed. If READLIMIT is not 0 this gives a limit on |
347 | | the number of bytes to read from estream. */ |
348 | | iobuf_t iobuf_esopen (estream_t estream, const char *mode, int keep_open, |
349 | | size_t readlimit); |
350 | | |
351 | | /* Create a filter using an existing socket. On Windows creates a |
352 | | special socket filter. On non-Windows systems simply, this simply |
353 | | calls iobuf_fdopen. */ |
354 | | iobuf_t iobuf_sockopen (int fd, const char *mode); |
355 | | |
356 | | /* Set various options / perform different actions on a PIPELINE. See |
357 | | the IOBUF_IOCTL_* macros above. */ |
358 | | int iobuf_ioctl (iobuf_t a, iobuf_ioctl_t cmd, int intval, void *ptrval); |
359 | | |
360 | | /* Close a pipeline. The filters in the pipeline are first flushed |
361 | | using iobuf_flush, if they are output filters, and then |
362 | | IOBUFCTRL_FREE is called on each filter. |
363 | | |
364 | | If any filter returns a non-zero value in response to the |
365 | | IOBUFCTRL_FREE, that first such non-zero value is returned. Note: |
366 | | processing is not aborted in this case. If all filters are freed |
367 | | successfully, 0 is returned. */ |
368 | | int iobuf_close (iobuf_t iobuf); |
369 | | |
370 | | /* Calls IOBUFCTRL_CANCEL on each filter in the pipeline. Then calls |
371 | | io_close() on the pipeline. Finally, if the pipeline is an output |
372 | | pipeline, deletes the file. Returns the result of calling |
373 | | iobuf_close on the pipeline. */ |
374 | | int iobuf_cancel (iobuf_t iobuf); |
375 | | |
376 | | /* Add a new filter to the front of a pipeline. A is the head of the |
377 | | pipeline. F is the filter implementation. OV is an opaque pointer |
378 | | that is passed to F and is normally used to hold any internal |
379 | | state, such as a file pointer. |
380 | | |
381 | | Note: you may only maintain a reference to an iobuf_t as a |
382 | | reference to the head of the pipeline. That is, don't think about |
383 | | setting a pointer in OV to point to the filter's iobuf_t. This is |
384 | | because when we add a new filter to a pipeline, we memcpy the state |
385 | | in A into new buffer. This has the advantage that there is no need |
386 | | to update any references to the pipeline when a filter is added or |
387 | | removed, but it also means that a filter's state moves around in |
388 | | memory. |
389 | | |
390 | | The behavior of the filter function is determined by the value of |
391 | | the control parameter: |
392 | | |
393 | | IOBUFCTRL_INIT: Called this value just before the filter is |
394 | | linked into the pipeline. This can be used to initialize |
395 | | internal data structures. |
396 | | |
397 | | IOBUFCTRL_FREE: Called with this value just before the filter is |
398 | | removed from the pipeline. Normally used to release internal |
399 | | data structures, close a file handle, etc. |
400 | | |
401 | | IOBUFCTRL_UNDERFLOW: Called with this value to fill the passed |
402 | | buffer with more data. *LEN is the size of the buffer. Before |
403 | | returning, it should be set to the number of bytes which were |
404 | | written into the buffer. The function must return 0 to |
405 | | indicate success, -1 on EOF and a GPG_ERR_xxxxx code for any |
406 | | error. |
407 | | |
408 | | Note: this function may both return data and indicate an error |
409 | | or EOF. In this case, it simply writes the data to BUF, sets |
410 | | *LEN and returns the appropriate return code. The implication |
411 | | is that if an error occurs and no data has yet been written, it |
412 | | is essential that *LEN be set to 0! |
413 | | |
414 | | IOBUFCTRL_FLUSH: Called with this value to write out any |
415 | | collected data. *LEN is the number of bytes in BUF that need |
416 | | to be written out. Returns 0 on success and a GPG_ERR_* code |
417 | | otherwise. *LEN must be set to the number of bytes that were |
418 | | written out. |
419 | | |
420 | | IOBUFCTRL_CANCEL: Called with this value when iobuf_cancel() is |
421 | | called on the pipeline. |
422 | | |
423 | | IOBUFCTRL_DESC: Called with this value to get a human-readable |
424 | | description of the filter. *LEN is the size of the buffer. |
425 | | The description is filled into BUF, NUL-terminated. Always |
426 | | returns 0. |
427 | | */ |
428 | | int iobuf_push_filter (iobuf_t a, int (*f) (void *opaque, int control, |
429 | | iobuf_t chain, byte * buf, |
430 | | size_t * len), void *ov); |
431 | | /* This variant of iobuf_push_filter allows the called to indicate |
432 | | that OV should be freed when this filter is freed. That is, if |
433 | | REL_OV is TRUE, then when the filter is popped or freed OV will be |
434 | | freed after the filter function is called with control set to |
435 | | IOBUFCTRL_FREE. */ |
436 | | int iobuf_push_filter2 (iobuf_t a, |
437 | | int (*f) (void *opaque, int control, iobuf_t chain, |
438 | | byte * buf, size_t * len), void *ov, |
439 | | int rel_ov); |
440 | | |
441 | | /* Pop the top filter. The top filter must have the filter function F |
442 | | and the cookie OV. The cookie check is ignored if OV is NULL. */ |
443 | | int iobuf_pop_filter (iobuf_t a, |
444 | | int (*f) (void *opaque, int control, |
445 | | iobuf_t chain, byte * buf, size_t * len), |
446 | | void *ov); |
447 | | |
448 | | /* Used for debugging. Prints out the chain using log_debug if |
449 | | IOBUF_DEBUG_MODE is not 0. */ |
450 | | int iobuf_print_chain (iobuf_t a); |
451 | | |
452 | | /* Indicate that some error occurred on the specified filter. */ |
453 | | #define iobuf_set_error(a) do { (a)->error = 1; } while(0) |
454 | | |
455 | | /* Return any pending error on filter A. */ |
456 | 6.76k | #define iobuf_error(a) ((a)->error) |
457 | | |
458 | | /* Limit the amount of additional data that may be read from the |
459 | | filter. That is, if you've already read 100 bytes from A and you |
460 | | set the limit to 50, then you can read up to an additional 50 bytes |
461 | | (i.e., a total of 150 bytes) before EOF is forcefully returned. |
462 | | Setting NLIMIT to 0 removes any active limit. |
463 | | |
464 | | Note: using iobuf_seek removes any currently enforced limit! */ |
465 | | void iobuf_set_limit (iobuf_t a, off_t nlimit); |
466 | | |
467 | | /* Returns the number of bytes that have been read from the pipeline. |
468 | | Note: the result is undefined for IOBUF_OUTPUT and IOBUF_OUTPUT_TEMP |
469 | | pipelines! */ |
470 | | off_t iobuf_tell (iobuf_t a); |
471 | | |
472 | | /* There are two cases: |
473 | | |
474 | | - If A is an INPUT or OUTPUT pipeline, then the last filter in the |
475 | | pipeline is found. If that is not a file filter, -1 is returned. |
476 | | Otherwise, an fseek(..., SEEK_SET) is performed on the file |
477 | | descriptor. |
478 | | |
479 | | - If A is a TEMP pipeline and the *first* (and thus only filter) is |
480 | | a TEMP filter, then the "file position" is effectively unchanged. |
481 | | That is, data is appended to the buffer and the seek does not |
482 | | cause the size of the buffer to grow. |
483 | | |
484 | | If no error occurred, then any limit previous set by |
485 | | iobuf_set_limit() is cleared. Further, any error on the filter |
486 | | (the file filter or the temp filter) is cleared. |
487 | | |
488 | | Returns 0 on success and -1 if an error occurs. */ |
489 | | int iobuf_seek (iobuf_t a, off_t newpos); |
490 | | |
491 | | /* Read a single byte. If a filter has no more data, returns -1 to |
492 | | indicate the EOF. Generally, you don't want to use this function, |
493 | | but instead prefer the iobuf_get macro, which is faster if there is |
494 | | data in the internal buffer. */ |
495 | | int iobuf_readbyte (iobuf_t a); |
496 | | |
497 | | /* Get a byte from the iobuf; must check for eof prior to this |
498 | | function. This function returns values in the range 0 .. 255 or -1 |
499 | | to indicate EOF. iobuf_get_noeof() does not return -1 to indicate |
500 | | EOF, but masks the returned value to be in the range 0 .. 255. */ |
501 | | #define iobuf_get(a) \ |
502 | 529k | ( ((a)->nofast || (a)->d.start >= (a)->d.len )? \ |
503 | 529k | iobuf_readbyte((a)) : ( (a)->nbytes++, (a)->d.buf[(a)->d.start++] ) ) |
504 | 39.9k | #define iobuf_get_noeof(a) (iobuf_get((a))&0xff) |
505 | | |
506 | | /* Fill BUF with up to BUFLEN bytes. If a filter has no more data, |
507 | | returns -1 to indicate the EOF. Otherwise returns the number of |
508 | | bytes read. */ |
509 | | int iobuf_read (iobuf_t a, void *buf, unsigned buflen); |
510 | | |
511 | | /* Read a line of input (including the '\n') from the pipeline. |
512 | | |
513 | | The semantics are the same as for fgets(), but if the buffer is too |
514 | | short a larger one will be allocated up to *MAX_LENGTH and the end |
515 | | of the line except the trailing '\n' discarded. (Thus, |
516 | | *ADDR_OF_BUFFER must be allocated using malloc().) If the buffer |
517 | | is enlarged, then *LENGTH_OF_BUFFER will be updated to reflect the |
518 | | new size. If the line is truncated, then *MAX_LENGTH will be set |
519 | | to 0. If *ADDR_OF_BUFFER is NULL, a buffer is allocated using |
520 | | malloc(). |
521 | | |
522 | | A line is considered a byte stream ending in a '\n'. Returns the |
523 | | number of characters written to the buffer (i.e., excluding any |
524 | | discarded characters due to truncation). Thus, use this instead of |
525 | | strlen(buffer) to determine the length of the string as this is |
526 | | unreliable if the input contains NUL characters. |
527 | | |
528 | | EOF is indicated by a line of length zero. |
529 | | |
530 | | The last LF may be missing due to an EOF. */ |
531 | | unsigned iobuf_read_line (iobuf_t a, byte ** addr_of_buffer, |
532 | | unsigned *length_of_buffer, unsigned *max_length); |
533 | | |
534 | | /* Read up to BUFLEN bytes from pipeline A. Note: this function can't |
535 | | return more than the pipeline's internal buffer size. The return |
536 | | value is the number of bytes actually written to BUF. If the |
537 | | filter returns EOF, then this function returns -1. |
538 | | |
539 | | This function does not clear any pending EOF. That is, if the |
540 | | pipeline consists of two filters and the first one returns EOF |
541 | | during the peek, then the subsequent iobuf_read* will still return |
542 | | EOF before returning the data from the second filter. */ |
543 | | int iobuf_peek (iobuf_t a, byte * buf, unsigned buflen); |
544 | | |
545 | | /* Write a byte to the pipeline. Returns 0 on success and an error |
546 | | code otherwise. */ |
547 | | int iobuf_writebyte (iobuf_t a, unsigned c); |
548 | | |
549 | | /* Alias for iobuf_writebyte. */ |
550 | 0 | #define iobuf_put(a,c) iobuf_writebyte(a,c) |
551 | | |
552 | | /* Write a sequence of bytes to the pipeline. Returns 0 on success |
553 | | and an error code otherwise. */ |
554 | | int iobuf_write (iobuf_t a, const void *buf, unsigned buflen); |
555 | | |
556 | | /* Write a string (not including the NUL terminator) to the pipeline. |
557 | | Returns 0 on success and an error code otherwise. */ |
558 | | int iobuf_writestr (iobuf_t a, const char *buf); |
559 | | |
560 | | /* Flushes the pipeline removing all filters but the sink (the last |
561 | | filter) in the process. */ |
562 | | void iobuf_flush_temp (iobuf_t temp); |
563 | | |
564 | | /* Flushes the pipeline SOURCE removing all filters but the sink (the |
565 | | last filter) in the process (i.e., it calls |
566 | | iobuf_flush_temp(source)) and then writes the data to the pipeline |
567 | | DEST. Note: this doesn't free (iobuf_close()) SOURCE. Both SOURCE |
568 | | and DEST must be output pipelines. */ |
569 | | int iobuf_write_temp (iobuf_t dest, iobuf_t source); |
570 | | |
571 | | /* Flushes each filter in the pipeline (i.e., sends any buffered data |
572 | | to the filter by calling IOBUFCTRL_FLUSH). Then, copies up to the |
573 | | first BUFLEN bytes from the last filter's internal buffer (which |
574 | | will only be non-empty if it is a temp filter) to the buffer |
575 | | BUFFER. Returns the number of bytes actually copied. */ |
576 | | size_t iobuf_temp_to_buffer (iobuf_t a, byte * buffer, size_t buflen); |
577 | | |
578 | | /* Copies the data from the input iobuf SOURCE to the output iobuf |
579 | | DEST until either an error is encountered or EOF is reached. |
580 | | Returns the number of bytes successfully written. If an error |
581 | | occurred, then any buffered bytes are not returned to SOURCE and are |
582 | | effectively lost. To check if an error occurred, use |
583 | | iobuf_error. */ |
584 | | size_t iobuf_copy (iobuf_t dest, iobuf_t source); |
585 | | |
586 | | /* Return the size of any underlying file. This only works with |
587 | | file_filter based pipelines. */ |
588 | | uint64_t iobuf_get_filelength (iobuf_t a); |
589 | 0 | #define IOBUF_FILELENGTH_LIMIT 0xffffffff |
590 | | |
591 | | /* Return the file descriptor designating the underlying file. This |
592 | | only works with file_filter based pipelines. */ |
593 | | gnupg_fd_t iobuf_get_fd (iobuf_t a); |
594 | | |
595 | | /* Return the real filename, if available. This only supports |
596 | | pipelines that end in file filters. Returns NULL if not |
597 | | available. */ |
598 | | const char *iobuf_get_real_fname (iobuf_t a); |
599 | | |
600 | | /* Return the filename or a description thereof. For instance, for |
601 | | iobuf_open("-"), this will return "[stdin]". This only supports |
602 | | pipelines that end in file filters. Returns NULL if not |
603 | | available. */ |
604 | | const char *iobuf_get_fname (iobuf_t a); |
605 | | |
606 | | /* Like iobuf_getfname, but instead of returning NULL if no |
607 | | description is available, return "[?]". */ |
608 | | const char *iobuf_get_fname_nonnull (iobuf_t a); |
609 | | |
610 | | /* Pushes a filter on the pipeline that interprets the datastream as |
611 | | an OpenPGP data block whose length is encoded using partial body |
612 | | length headers (see Section 4.2.2.4 of RFC 4880). Concretely, it |
613 | | just returns / writes the data and finishes the packet with an |
614 | | EOF. */ |
615 | | void iobuf_set_partial_body_length_mode (iobuf_t a, size_t len); |
616 | | |
617 | | /* If PARTIAL is set, then read from the pipeline until the first EOF |
618 | | is returned. |
619 | | |
620 | | If PARTIAL is 0, then read up to N bytes or until the first EOF is |
621 | | returned. |
622 | | |
623 | | Recall: a filter can return EOF. In this case, it and all |
624 | | preceding filters are popped from the pipeline and the next read is |
625 | | from the following filter (which may or may not return EOF). */ |
626 | | void iobuf_skip_rest (iobuf_t a, unsigned long n, int partial); |
627 | | |
628 | | /* Check if the file is compressed, by peeking the iobuf. */ |
629 | | int is_file_compressed (iobuf_t inp); |
630 | | |
631 | 13 | #define iobuf_where(a) "[don't know]" |
632 | | |
633 | | /* Each time a filter is allocated (via iobuf_alloc()), a |
634 | | monotonically increasing counter is incremented and this field is |
635 | | set to the new value. This macro returns that number. */ |
636 | 0 | #define iobuf_id(a) ((a)->no) |
637 | | |
638 | 0 | #define iobuf_get_temp_buffer(a) ( (a)->d.buf ) |
639 | 0 | #define iobuf_get_temp_length(a) ( (a)->d.len ) |
640 | | |
641 | | /* Whether the filter uses an in-memory buffer. */ |
642 | | #define iobuf_is_temp(a) ( (a)->use == IOBUF_OUTPUT_TEMP ) |
643 | | |
644 | | #endif /*GNUPG_COMMON_IOBUF_H*/ |