2
* libdpkg - Debian packaging suite library routines
3
* buffer.h - buffer I/O handling routines
5
* Copyright © 1999, 2000 Wichert Akkerman <wakkerma@debian.org>
6
* Copyright © 2000-2003 Adam Heath <doogie@debian.org>
7
* Copyright © 2005 Scott James Remnant
8
* Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
10
* This is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as
12
* published by the Free Software Foundation; either version 2,
13
* or (at your option) any later version.
15
* This is distributed in the hope that it will be useful, but
16
* WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public
21
* License along with dpkg; if not, write to the Free Software
22
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28
#include <sys/types.h>
30
#include <dpkg/macros.h>
34
#define BUFFER_WRITE_BUF 0
35
#define BUFFER_WRITE_VBUF 1
36
#define BUFFER_WRITE_FD 2
37
#define BUFFER_WRITE_NULL 3
38
#define BUFFER_WRITE_STREAM 4
39
#define BUFFER_WRITE_MD5 5
41
#define BUFFER_READ_FD 0
42
#define BUFFER_READ_STREAM 1
44
typedef struct buffer_data *buffer_data_t;
46
typedef union buffer_arg {
57
# define fd_md5(fd, hash, limit, ...) \
58
buffer_copy_IntPtr(fd, BUFFER_READ_FD, hash, BUFFER_WRITE_MD5, \
60
# define stream_md5(file, hash, limit, ...) \
61
buffer_copy_PtrPtr(file, BUFFER_READ_STREAM, hash, BUFFER_WRITE_MD5, \
63
# define fd_fd_copy(fd1, fd2, limit, ...) \
64
buffer_copy_IntInt(fd1, BUFFER_READ_FD, fd2, BUFFER_WRITE_FD, \
66
# define fd_buf_copy(fd, buf, limit, ...) \
67
buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_BUF, \
69
# define fd_vbuf_copy(fd, buf, limit, ...) \
70
buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_VBUF, \
72
# define fd_null_copy(fd, limit, ...) \
73
if (lseek(fd, limit, SEEK_CUR) == -1) { \
74
if (errno != ESPIPE) \
75
ohshite(__VA_ARGS__); \
76
buffer_copy_IntPtr(fd, BUFFER_READ_FD, \
77
NULL, BUFFER_WRITE_NULL, \
78
limit, __VA_ARGS__); \
80
# define stream_null_copy(file, limit, ...) \
81
if (fseek(file, limit, SEEK_CUR) == -1) { \
83
ohshite(__VA_ARGS__); \
84
buffer_copy_PtrPtr(file, BUFFER_READ_STREAM, \
85
NULL, BUFFER_WRITE_NULL, \
86
limit, __VA_ARGS__); \
88
# define stream_fd_copy(file, fd, limit, ...) \
89
buffer_copy_PtrInt(file, BUFFER_READ_STREAM, fd, BUFFER_WRITE_FD, \
92
# define fd_md5(fd, hash, limit, desc...) \
93
buffer_copy_IntPtr(fd, BUFFER_READ_FD, hash, BUFFER_WRITE_MD5, \
95
# define stream_md5(file, hash, limit, desc...) \
96
buffer_copy_PtrPtr(file, BUFFER_READ_STREAM, hash, BUFFER_WRITE_MD5, \
98
# define fd_fd_copy(fd1, fd2, limit, desc...) \
99
buffer_copy_IntInt(fd1, BUFFER_READ_FD, fd2, BUFFER_WRITE_FD, \
101
# define fd_buf_copy(fd, buf, limit, desc...) \
102
buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_BUF, \
104
# define fd_vbuf_copy(fd, buf, limit, desc...) \
105
buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_VBUF, \
107
# define fd_null_copy(fd, limit, desc...) \
108
if (lseek(fd, limit, SEEK_CUR) == -1) { \
109
if (errno != ESPIPE) \
111
buffer_copy_IntPtr(fd, BUFFER_READ_FD, \
112
NULL, BUFFER_WRITE_NULL, \
115
# define stream_null_copy(file, limit, desc...) \
116
if (fseek(file, limit, SEEK_CUR) == -1) { \
117
if (errno != EBADF) \
119
buffer_copy_PtrPtr(file, BUFFER_READ_STREAM, \
120
NULL, BUFFER_WRITE_NULL, \
123
# define stream_fd_copy(file, fd, limit, desc...)\
124
buffer_copy_PtrInt(file, BUFFER_READ_STREAM, fd, BUFFER_WRITE_FD, \
126
#endif /* HAVE_C99 */
128
off_t buffer_copy_PtrInt(void *p, int typeIn, int i, int typeOut,
129
off_t limit, const char *desc,
130
...) DPKG_ATTR_PRINTF(6);
131
off_t buffer_copy_PtrPtr(void *p1, int typeIn, void *p2, int typeOut,
132
off_t limit, const char *desc,
133
...) DPKG_ATTR_PRINTF(6);
134
off_t buffer_copy_IntPtr(int i, int typeIn, void *p, int typeOut,
135
off_t limit, const char *desc,
136
...) DPKG_ATTR_PRINTF(6);
137
off_t buffer_copy_IntInt(int i1, int typeIn, int i2, int typeOut,
138
off_t limit, const char *desc,
139
...) DPKG_ATTR_PRINTF(6);
141
off_t buffer_write(buffer_data_t data, void *buf,
142
off_t length, const char *desc);
143
off_t buffer_read(buffer_data_t data, void *buf,
144
off_t length, const char *desc);
145
off_t buffer_init(buffer_data_t read_data, buffer_data_t write_data);
146
off_t buffer_done(buffer_data_t read_data, buffer_data_t write_data);
147
off_t buffer_copy(buffer_data_t read_data, buffer_data_t write_data,
148
off_t limit, const char *desc);
152
#endif /* DPKG_BUFFER_H */