The following commit has been merged in the master branch:
commit 76f5201800ace04832c293e6f7a2aa50357ed50c
Author: Guillem Jover <[email protected]>
Date:   Thu Aug 27 18:33:48 2009 +0200

    libdpkg: Move buffer I/O declarations to buffer.h

diff --git a/dpkg-deb/build.c b/dpkg-deb/build.c
index 0c66ca5..5969c92 100644
--- a/dpkg-deb/build.c
+++ b/dpkg-deb/build.c
@@ -46,6 +46,7 @@
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
 #include <dpkg/path.h>
+#include <dpkg/buffer.h>
 #include <dpkg/subproc.h>
 #include <dpkg/myopt.h>
 
diff --git a/dpkg-deb/extract.c b/dpkg-deb/extract.c
index a3f7a1c..166b143 100644
--- a/dpkg-deb/extract.c
+++ b/dpkg-deb/extract.c
@@ -42,6 +42,7 @@
 #endif
 
 #include <dpkg/dpkg.h>
+#include <dpkg/buffer.h>
 #include <dpkg/subproc.h>
 #include <dpkg/myopt.h>
 
diff --git a/dpkg-deb/info.c b/dpkg-deb/info.c
index de5445d..f421318 100644
--- a/dpkg-deb/info.c
+++ b/dpkg-deb/info.c
@@ -39,6 +39,7 @@
 
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/buffer.h>
 #include <dpkg/subproc.h>
 #include <dpkg/myopt.h>
 
diff --git a/lib/dpkg/Makefile.am b/lib/dpkg/Makefile.am
index a380a5f..428c95f 100644
--- a/lib/dpkg/Makefile.am
+++ b/lib/dpkg/Makefile.am
@@ -19,6 +19,7 @@ libdpkg_a_SOURCES = \
        dpkg.h \
        dpkg-db.h \
        dlist.h \
+       buffer.c buffer.h \
        cleanup.c \
        compression.c \
        database.c \
diff --git a/lib/dpkg/buffer.c b/lib/dpkg/buffer.c
new file mode 100644
index 0000000..29f39b3
--- /dev/null
+++ b/lib/dpkg/buffer.c
@@ -0,0 +1,278 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * buffer.c - buffer I/O handling routines
+ *
+ * Copyright © 1999, 2000 Wichert Akkerman <[email protected]>
+ * Copyright © 2000-2003 Adam Heath <[email protected]>
+ * Copyright © 2008, 2009 Guillem Jover <[email protected]>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with dpkg; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <dpkg/i18n.h>
+
+#include <sys/types.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <dpkg/dpkg.h>
+#include <dpkg/varbuf.h>
+#include <dpkg/md5.h>
+#include <dpkg/buffer.h>
+
+struct buffer_write_md5ctx {
+       struct MD5Context ctx;
+       unsigned char **hash;
+};
+
+off_t
+buffer_write(buffer_data_t data, void *buf, off_t length, const char *desc)
+{
+       off_t ret = length;
+
+       if (data->type & BUFFER_WRITE_SETUP) {
+               switch (data->type ^ BUFFER_WRITE_SETUP) {
+               case BUFFER_WRITE_MD5:
+               {
+                       struct buffer_write_md5ctx *ctx;
+
+                       ctx = m_malloc(sizeof(struct buffer_write_md5ctx));
+                       ctx->hash = data->data.ptr;
+                       data->data.ptr = ctx;
+                       MD5Init(&ctx->ctx);
+               }
+                       break;
+               }
+               return 0;
+       }
+
+       if (data->type & BUFFER_WRITE_SHUTDOWN) {
+               switch (data->type ^ BUFFER_WRITE_SHUTDOWN) {
+               case BUFFER_WRITE_MD5:
+               {
+                       struct buffer_write_md5ctx *ctx;
+                       unsigned char digest[16], *p = digest;
+                       unsigned char *hash;
+                       int i;
+
+                       ctx = (struct buffer_write_md5ctx *)data->data.ptr;
+                       *ctx->hash = hash = m_malloc(MD5HASHLEN + 1);
+                       MD5Final(digest, &ctx->ctx);
+                       for (i = 0; i < 16; ++i) {
+                               sprintf((char *)hash, "%02x", *p++);
+                               hash += 2;
+                       }
+                       *hash = '\0';
+                       free(ctx);
+               }
+                       break;
+               }
+               return 0;
+       }
+
+       switch (data->type) {
+       case BUFFER_WRITE_BUF:
+               memcpy(data->data.ptr, buf, length);
+               data->data.ptr += length;
+               break;
+       case BUFFER_WRITE_VBUF:
+               varbufaddbuf((struct varbuf *)data->data.ptr, buf, length);
+               break;
+       case BUFFER_WRITE_FD:
+               ret = write(data->data.i, buf, length);
+               if (ret < 0 && errno != EINTR)
+                       ohshite(_("failed in buffer_write(fd) (%i, ret=%li): 
%s"),
+                               data->data.i, (long)ret, desc);
+               break;
+       case BUFFER_WRITE_NULL:
+               break;
+       case BUFFER_WRITE_STREAM:
+               ret = fwrite(buf, 1, length, (FILE *)data->data.ptr);
+               if (feof((FILE *)data->data.ptr))
+                       ohshite(_("eof in buffer_write(stream): %s"), desc);
+               if(ferror((FILE *)data->data.ptr))
+                       ohshite(_("error in buffer_write(stream): %s"), desc);
+               break;
+       case BUFFER_WRITE_MD5:
+               MD5Update(&(((struct buffer_write_md5ctx 
*)data->data.ptr)->ctx), buf, length);
+               break;
+       default:
+               fprintf(stderr, _("unknown data type `%i' in buffer_write\n"),
+                       data->type);
+       }
+
+       return ret;
+}
+
+off_t
+buffer_read(buffer_data_t data, void *buf, off_t length, const char *desc)
+{
+       off_t ret = length;
+
+       if (data->type & BUFFER_READ_SETUP)
+               return 0;
+       if (data->type & BUFFER_READ_SHUTDOWN)
+               return 0;
+
+       switch (data->type) {
+       case BUFFER_READ_FD:
+               ret = read(data->data.i, buf, length);
+               if(ret < 0 && errno != EINTR)
+                       ohshite(_("failed in buffer_read(fd): %s"), desc);
+               break;
+       case BUFFER_READ_STREAM:
+               ret = fread(buf, 1, length, (FILE *)data->data.ptr);
+               if (feof((FILE *)data->data.ptr))
+                       return ret;
+               if (ferror((FILE *)data->data.ptr))
+                       ohshite(_("error in buffer_read(stream): %s"), desc);
+               break;
+       default:
+               fprintf(stderr, _("unknown data type `%i' in buffer_read\n"),
+                       data->type);
+       }
+
+       return ret;
+}
+
+off_t
+buffer_copy_setup(buffer_arg argIn, int typeIn, void *procIn,
+                  buffer_arg argOut, int typeOut, void *procOut,
+                  off_t limit, const char *desc)
+{
+       struct buffer_data read_data = { procIn, argIn, typeIn },
+                          write_data = { procOut, argOut, typeOut };
+       off_t ret;
+
+       if (procIn == NULL)
+               read_data.proc = buffer_read;
+       if (procOut == NULL)
+               write_data.proc = buffer_write;
+
+       read_data.type |= BUFFER_READ_SETUP;
+       read_data.proc(&read_data, NULL, 0, desc);
+       read_data.type = typeIn;
+
+       write_data.type |= BUFFER_WRITE_SETUP;
+       write_data.proc(&write_data, NULL, 0, desc);
+       write_data.type = typeOut;
+
+       ret = buffer_copy(&read_data, &write_data, limit, desc);
+
+       write_data.type |= BUFFER_WRITE_SHUTDOWN;
+       write_data.proc(&write_data, NULL, 0, desc);
+
+       read_data.type |= BUFFER_READ_SHUTDOWN;
+       read_data.proc(&read_data, NULL, 0, desc);
+
+       return ret;
+}
+
+
+#define buffer_copy_setup_dual(name, type1, name1, type2, name2) \
+off_t \
+buffer_copy_setup_##name(type1 n1, int typeIn, void *procIn, \
+                         type2 n2, int typeOut, void *procOut, \
+                         off_t limit, const char *desc, ...) \
+{ \
+       va_list al; \
+       buffer_arg a1, a2; \
+       struct varbuf v = VARBUF_INIT; \
+       off_t ret; \
+\
+       a1.name1 = n1; \
+       a2.name2 = n2; \
+       va_start(al, desc); \
+       varbufvprintf(&v, desc, al); \
+       va_end(al); \
+\
+       ret = buffer_copy_setup(a1, typeIn, procIn, \
+                               a2, typeOut, procOut, \
+                               limit, v.buf); \
+       varbuffree(&v); \
+\
+       return ret; \
+}
+
+buffer_copy_setup_dual(IntInt, int, i, int, i);
+buffer_copy_setup_dual(IntPtr, int, i, void *, ptr);
+buffer_copy_setup_dual(PtrInt, void *, ptr, int, i);
+buffer_copy_setup_dual(PtrPtr, void *, ptr, void *, ptr);
+
+off_t
+buffer_copy(buffer_data_t read_data, buffer_data_t write_data,
+            off_t limit, const char *desc)
+{
+       char *buf, *writebuf;
+       int bufsize = 32768;
+       long bytesread = 0, byteswritten = 0;
+       off_t totalread = 0, totalwritten = 0;
+
+       if ((limit != -1) && (limit < bufsize))
+               bufsize = limit;
+       if (bufsize == 0)
+               return 0;
+
+       writebuf = buf = m_malloc(bufsize);
+
+       while (bytesread >= 0 && byteswritten >= 0 && bufsize > 0) {
+               bytesread = read_data->proc(read_data, buf, bufsize, desc);
+               if (bytesread < 0) {
+                       if (errno == EINTR || errno == EAGAIN)
+                               continue;
+                       break;
+               }
+               if (bytesread == 0)
+                       break;
+
+               totalread += bytesread;
+               if (limit != -1) {
+                       limit -= bytesread;
+                       if (limit < bufsize)
+                               bufsize = limit;
+               }
+               writebuf = buf;
+               while (bytesread) {
+                       byteswritten = write_data->proc(write_data, writebuf, 
bytesread, desc);
+                       if (byteswritten == -1) {
+                               if (errno == EINTR || errno == EAGAIN)
+                                       continue;
+                               break;
+                       }
+                       if (byteswritten == 0)
+                               break;
+
+                       bytesread -= byteswritten;
+                       totalwritten += byteswritten;
+                       writebuf += byteswritten;
+               }
+       }
+
+       if (bytesread < 0 || byteswritten < 0)
+               ohshite(_("failed in buffer_copy (%s)"), desc);
+       if (limit > 0)
+               ohshit(_("short read in buffer_copy (%s)"), desc);
+
+       free(buf);
+
+       return totalread;
+}
+
diff --git a/lib/dpkg/buffer.h b/lib/dpkg/buffer.h
new file mode 100644
index 0000000..ca47e39
--- /dev/null
+++ b/lib/dpkg/buffer.h
@@ -0,0 +1,177 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * buffer.h - buffer I/O handling routines
+ *
+ * Copyright © 1999, 2000 Wichert Akkerman <[email protected]>
+ * Copyright © 2000-2003 Adam Heath <[email protected]>
+ * Copyright © 2005 Scott James Remnant
+ * Copyright © 2008, 2009 Guillem Jover <[email protected]>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with dpkg; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef DPKG_BUFFER_H
+#define DPKG_BUFFER_H
+
+#include <sys/types.h>
+
+#include <dpkg/macros.h>
+
+DPKG_BEGIN_DECLS
+
+#define BUFFER_WRITE_BUF               0
+#define BUFFER_WRITE_VBUF              1
+#define BUFFER_WRITE_FD                        2
+#define BUFFER_WRITE_NULL              3
+#define BUFFER_WRITE_STREAM            4
+#define BUFFER_WRITE_MD5               5
+
+#define BUFFER_READ_FD                 0
+#define BUFFER_READ_STREAM             1
+
+#define BUFFER_WRITE_SETUP             1 << 16
+#define BUFFER_READ_SETUP              1 << 17
+#define BUFFER_WRITE_SHUTDOWN          1 << 18
+#define BUFFER_READ_SHUTDOWN           1 << 19
+
+typedef struct buffer_data *buffer_data_t;
+
+typedef off_t (*buffer_proc_t)(buffer_data_t data, void *buf, off_t size,
+                               const char *desc);
+
+typedef union buffer_arg {
+       void *ptr;
+       int i;
+} buffer_arg;
+
+struct buffer_data {
+       buffer_proc_t proc;
+       buffer_arg data;
+       int type;
+};
+
+#if HAVE_C99
+# define fd_md5(fd, hash, limit, ...) \
+       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
+                                hash, BUFFER_WRITE_MD5, NULL, \
+                                limit, __VA_ARGS__)
+# define stream_md5(file, hash, limit, ...) \
+       buffer_copy_setup_PtrPtr(file, BUFFER_READ_STREAM, NULL, \
+                                hash, BUFFER_WRITE_MD5, NULL, \
+                                limit, __VA_ARGS__)
+# define fd_fd_copy(fd1, fd2, limit, ...) \
+       buffer_copy_setup_IntInt(fd1, BUFFER_READ_FD, NULL, \
+                                fd2, BUFFER_WRITE_FD, NULL, \
+                                limit, __VA_ARGS__)
+# define fd_buf_copy(fd, buf, limit, ...) \
+       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
+                                buf, BUFFER_WRITE_BUF, NULL, \
+                                limit, __VA_ARGS__)
+# define fd_vbuf_copy(fd, buf, limit, ...) \
+       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
+                                buf, BUFFER_WRITE_VBUF, NULL, \
+                                limit, __VA_ARGS__)
+# define fd_null_copy(fd, limit, ...) \
+       if (lseek(fd, limit, SEEK_CUR) == -1) { \
+               if (errno != ESPIPE) \
+                       ohshite(__VA_ARGS__); \
+               buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
+                                        NULL, BUFFER_WRITE_NULL, NULL, \
+                                        limit, __VA_ARGS__); \
+       }
+# define stream_null_copy(file, limit, ...) \
+       if (fseek(file, limit, SEEK_CUR) == -1) { \
+               if (errno != EBADF) \
+                       ohshite(__VA_ARGS__); \
+               buffer_copy_setup_PtrPtr(file, BUFFER_READ_STREAM, NULL, \
+                                        NULL, BUFFER_WRITE_NULL, NULL, \
+                                        limit, __VA_ARGS__); \
+       }
+# define stream_fd_copy(file, fd, limit, ...) \
+       buffer_copy_setup_PtrInt(file, BUFFER_READ_STREAM, NULL, \
+                                fd, BUFFER_WRITE_FD, NULL, \
+                                limit, __VA_ARGS__)
+#else /* HAVE_C99 */
+# define fd_md5(fd, hash, limit, desc...) \
+       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
+                                hash, BUFFER_WRITE_MD5, NULL, \
+                                limit, desc)
+# define stream_md5(file, hash, limit, desc...) \
+       buffer_copy_setup_PtrPtr(file, BUFFER_READ_STREAM, NULL, \
+                                hash, BUFFER_WRITE_MD5, NULL, \
+                                limit, desc)
+# define fd_fd_copy(fd1, fd2, limit, desc...) \
+       buffer_copy_setup_IntInt(fd1, BUFFER_READ_FD, NULL, \
+                                fd2, BUFFER_WRITE_FD, NULL, \
+                                limit, desc)
+# define fd_buf_copy(fd, buf, limit, desc...) \
+       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
+                                buf, BUFFER_WRITE_BUF, NULL, \
+                                limit, desc)
+# define fd_vbuf_copy(fd, buf, limit, desc...) \
+       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
+                                buf, BUFFER_WRITE_VBUF, NULL, \
+                                limit, desc)
+# define fd_null_copy(fd, limit, desc...) \
+       if (lseek(fd, limit, SEEK_CUR) == -1) { \
+               if (errno != ESPIPE) \
+                       ohshite(desc); \
+               buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
+                                        NULL, BUFFER_WRITE_NULL, NULL, \
+                                        limit, desc); \
+       }
+# define stream_null_copy(file, limit, desc...) \
+       if (fseek(file, limit, SEEK_CUR) == -1) { \
+               if (errno != EBADF) \
+                       ohshite(desc); \
+               buffer_copy_setup_PtrPtr(file, BUFFER_READ_STREAM, NULL, \
+                                        NULL, BUFFER_WRITE_NULL, NULL, \
+                                        limit, desc); \
+       }
+# define stream_fd_copy(file, fd, limit, desc...)\
+       buffer_copy_setup_PtrInt(file, BUFFER_READ_STREAM, NULL, \
+                                fd, BUFFER_WRITE_FD, NULL, \
+                                limit, desc)
+#endif /* HAVE_C99 */
+
+off_t buffer_copy_setup_PtrInt(void *p, int typeIn, void *procIn,
+                               int i, int typeOut, void *procOut,
+                               off_t limit, const char *desc,
+                               ...) DPKG_ATTR_PRINTF(8);
+off_t buffer_copy_setup_PtrPtr(void *p1, int typeIn, void *procIn,
+                               void *p2, int typeOut, void *procOut,
+                               off_t limit, const char *desc,
+                               ...) DPKG_ATTR_PRINTF(8);
+off_t buffer_copy_setup_IntPtr(int i, int typeIn, void *procIn,
+                               void *p, int typeOut, void *procOut,
+                               off_t limit, const char *desc,
+                               ...) DPKG_ATTR_PRINTF(8);
+off_t buffer_copy_setup_IntInt(int i1, int typeIn, void *procIn,
+                               int i2, int typeOut, void *procOut,
+                               off_t limit, const char *desc,
+                               ...) DPKG_ATTR_PRINTF(8);
+off_t buffer_copy_setup(buffer_arg argIn, int typeIn, void *procIn,
+                        buffer_arg argOut, int typeOut, void *procOut,
+                        off_t limit, const char *desc);
+off_t buffer_write(buffer_data_t data, void *buf,
+                   off_t length, const char *desc);
+off_t buffer_read(buffer_data_t data, void *buf,
+                  off_t length, const char *desc);
+off_t buffer_copy(buffer_data_t read_data, buffer_data_t write_data,
+                  off_t limit, const char *desc);
+
+DPKG_END_DECLS
+
+#endif /* DPKG_BUFFER_H */
diff --git a/lib/dpkg/compression.c b/lib/dpkg/compression.c
index 3886ee1..2fa0476 100644
--- a/lib/dpkg/compression.c
+++ b/lib/dpkg/compression.c
@@ -17,6 +17,7 @@
 
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/buffer.h>
 
 static void
 fd_fd_filter(int fd_in, int fd_out,
diff --git a/lib/dpkg/dpkg.h b/lib/dpkg/dpkg.h
index 89be055..38dbc10 100644
--- a/lib/dpkg/dpkg.h
+++ b/lib/dpkg/dpkg.h
@@ -206,132 +206,6 @@ int m_fork(void);
 void m_dup2(int oldfd, int newfd);
 void m_pipe(int fds[2]);
 
-#define BUFFER_WRITE_BUF 0
-#define BUFFER_WRITE_VBUF 1
-#define BUFFER_WRITE_FD 2
-#define BUFFER_WRITE_NULL 3
-#define BUFFER_WRITE_STREAM 4
-#define BUFFER_WRITE_MD5 5
-#define BUFFER_READ_FD 0
-#define BUFFER_READ_STREAM 1
-
-#define BUFFER_WRITE_SETUP 1 << 16
-#define BUFFER_READ_SETUP 1 << 17
-#define BUFFER_WRITE_SHUTDOWN 1 << 18
-#define BUFFER_READ_SHUTDOWN 1 << 19
-
-typedef struct buffer_data *buffer_data_t;
-typedef off_t (*buffer_proc_t)(buffer_data_t data, void *buf, off_t size, 
const char *desc);
-typedef union buffer_arg {
-  void *ptr;
-  int i;
-} buffer_arg;
-
-struct buffer_data {
-  buffer_proc_t proc;
-  buffer_arg data;
-  int type;
-};
-
-#if HAVE_C99
-# define fd_md5(fd, hash, limit, ...)\
-       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
-                                hash, BUFFER_WRITE_MD5, NULL, \
-                                limit, __VA_ARGS__)
-# define stream_md5(file, hash, limit, ...)\
-       buffer_copy_setup_PtrPtr(file, BUFFER_READ_STREAM, NULL, \
-                                hash, BUFFER_WRITE_MD5, NULL, \
-                                limit, __VA_ARGS__)
-# define fd_fd_copy(fd1, fd2, limit, ...)\
-       buffer_copy_setup_IntInt(fd1, BUFFER_READ_FD, NULL, \
-                                fd2, BUFFER_WRITE_FD, NULL, \
-                                limit, __VA_ARGS__)
-# define fd_buf_copy(fd, buf, limit, ...)\
-       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
-                                buf, BUFFER_WRITE_BUF, NULL, \
-                                limit, __VA_ARGS__)
-# define fd_vbuf_copy(fd, buf, limit, ...)\
-       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
-                                buf, BUFFER_WRITE_VBUF, NULL, \
-                                limit, __VA_ARGS__)
-# define fd_null_copy(fd, limit, ...) \
-       if (lseek(fd, limit, SEEK_CUR) == -1) { \
-           if(errno != ESPIPE) ohshite(__VA_ARGS__); \
-           buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
-                                    NULL, BUFFER_WRITE_NULL, NULL, \
-                                    limit, __VA_ARGS__);\
-       }
-# define stream_null_copy(file, limit, ...) \
-       if (fseek(file, limit, SEEK_CUR) == -1) { \
-           if(errno != EBADF) ohshite(__VA_ARGS__); \
-           buffer_copy_setup_PtrPtr(file, BUFFER_READ_STREAM, NULL, \
-                                    NULL, BUFFER_WRITE_NULL, NULL, \
-                                    limit, __VA_ARGS__);\
-       }
-# define stream_fd_copy(file, fd, limit, ...)\
-       buffer_copy_setup_PtrInt(file, BUFFER_READ_STREAM, NULL, \
-                                fd, BUFFER_WRITE_FD, NULL, \
-                                limit, __VA_ARGS__)
-#else /* HAVE_C99 */
-# define fd_md5(fd, hash, limit, desc...)\
-       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
-                                hash, BUFFER_WRITE_MD5, NULL, \
-                                limit, desc)
-# define stream_md5(file, hash, limit, desc...)\
-       buffer_copy_setup_PtrPtr(file, BUFFER_READ_STREAM, NULL, \
-                                hash, BUFFER_WRITE_MD5, NULL, \
-                                limit, desc)
-# define fd_fd_copy(fd1, fd2, limit, desc...)\
-       buffer_copy_setup_IntInt(fd1, BUFFER_READ_FD, NULL, \
-                                fd2, BUFFER_WRITE_FD, NULL, \
-                                limit, desc)
-# define fd_buf_copy(fd, buf, limit, desc...)\
-       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
-                                buf, BUFFER_WRITE_BUF, NULL, \
-                                limit, desc)
-# define fd_vbuf_copy(fd, buf, limit, desc...)\
-       buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
-                                buf, BUFFER_WRITE_VBUF, NULL, \
-                                limit, desc)
-# define fd_null_copy(fd, limit, desc...) \
-       if (lseek(fd, limit, SEEK_CUR) == -1) { \
-           if(errno != ESPIPE) ohshite(desc); \
-           buffer_copy_setup_IntPtr(fd, BUFFER_READ_FD, NULL, \
-                                    NULL, BUFFER_WRITE_NULL, NULL, \
-                                    limit, desc);\
-       }
-# define stream_null_copy(file, limit, desc...) \
-       if (fseek(file, limit, SEEK_CUR) == -1) { \
-           if(errno != EBADF) ohshite(desc); \
-           buffer_copy_setup_PtrPtr(file, BUFFER_READ_STREAM, NULL, \
-                                    NULL, BUFFER_WRITE_NULL, NULL, \
-                                    limit, desc);\
-       }
-# define stream_fd_copy(file, fd, limit, desc...)\
-       buffer_copy_setup_PtrInt(file, BUFFER_READ_STREAM, NULL, \
-                                fd, BUFFER_WRITE_FD, NULL, \
-                                limit, desc)
-#endif /* HAVE_C99 */
-
-off_t buffer_copy_setup_PtrInt(void *p, int typeIn, void *procIn,
-                                       int i, int typeOut, void *procOut,
-                               off_t limit, const char *desc, ...) 
DPKG_ATTR_PRINTF(8);
-off_t buffer_copy_setup_PtrPtr(void *p1, int typeIn, void *procIn,
-                                       void *p2, int typeOut, void *procOut,
-                               off_t limit, const char *desc, ...) 
DPKG_ATTR_PRINTF(8);
-off_t buffer_copy_setup_IntPtr(int i, int typeIn, void *procIn,
-                                       void *p, int typeOut, void *procOut,
-                               off_t limit, const char *desc, ...) 
DPKG_ATTR_PRINTF(8);
-off_t buffer_copy_setup_IntInt(int i1, int typeIn, void *procIn,
-                                       int i2, int typeOut, void *procOut,
-                               off_t limit, const char *desc, ...) 
DPKG_ATTR_PRINTF(8);
-off_t buffer_copy_setup(buffer_arg argIn, int typeIn, void *procIn,
-                      buffer_arg argOut, int typeOut, void *procOut,
-                      off_t limit, const char *desc);
-off_t buffer_write(buffer_data_t data, void *buf, off_t length, const char 
*desc);
-off_t buffer_read(buffer_data_t data, void *buf, off_t length, const char 
*desc);
-off_t buffer_copy(buffer_data_t read_data, buffer_data_t write_data, off_t 
limit, const char *desc);
-
 /*** from utils.c ***/
 
 int cisdigit(int c);
diff --git a/lib/dpkg/mlib.c b/lib/dpkg/mlib.c
index abe4d01..71a6889 100644
--- a/lib/dpkg/mlib.c
+++ b/lib/dpkg/mlib.c
@@ -36,7 +36,6 @@
 
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
-#include <dpkg/md5.h>
 
 void *m_malloc(size_t amount) {
 #ifdef MDEBUG
@@ -126,196 +125,3 @@ void setcloexec(int fd, const char* fn) {
     ohshite(_("unable to set close-on-exec flag for %.250s"),fn);
 }
 
-struct buffer_write_md5ctx {
-  struct MD5Context ctx;
-  unsigned char **hash;
-};
-off_t buffer_write(buffer_data_t data, void *buf, off_t length, const char 
*desc) {
-  off_t ret= length;
-  if(data->type & BUFFER_WRITE_SETUP) {
-    switch(data->type ^ BUFFER_WRITE_SETUP) {
-      case BUFFER_WRITE_MD5:
-       {
-         struct buffer_write_md5ctx *ctx;
-
-         ctx = m_malloc(sizeof(struct buffer_write_md5ctx));
-         ctx->hash = data->data.ptr;
-         data->data.ptr = ctx;
-         MD5Init(&ctx->ctx);
-       }
-       break;
-    }
-    return 0;
-  }
-  if(data->type & BUFFER_WRITE_SHUTDOWN) {
-    switch(data->type ^ BUFFER_WRITE_SHUTDOWN) {
-      case BUFFER_WRITE_MD5:
-       {
-         int i;
-         unsigned char digest[16], *p = digest;
-         struct buffer_write_md5ctx *ctx = (struct buffer_write_md5ctx 
*)data->data.ptr;
-         unsigned char *hash = *ctx->hash = m_malloc(MD5HASHLEN + 1);
-         MD5Final(digest, &ctx->ctx);
-         for (i = 0; i < 16; ++i) {
-           sprintf((char *)hash, "%02x", *p++);
-           hash += 2;
-         }
-         *hash = '\0';
-         free(ctx);
-       }
-       break;
-    }
-    return 0;
-  }
-  switch(data->type) {
-    case BUFFER_WRITE_BUF:
-      memcpy(data->data.ptr, buf, length);
-      data->data.ptr += length;
-      break;
-    case BUFFER_WRITE_VBUF:
-      varbufaddbuf((struct varbuf *)data->data.ptr, buf, length);
-      break;
-    case BUFFER_WRITE_FD:
-      if((ret= write(data->data.i, buf, length)) < 0 && errno != EINTR)
-       ohshite(_("failed in buffer_write(fd) (%i, ret=%li): %s"), 
data->data.i, (long)ret, desc);
-      break;
-    case BUFFER_WRITE_NULL:
-      break;
-    case BUFFER_WRITE_STREAM:
-      ret= fwrite(buf, 1, length, (FILE *)data->data.ptr);
-      if(feof((FILE *)data->data.ptr))
-       ohshite(_("eof in buffer_write(stream): %s"), desc);
-      if(ferror((FILE *)data->data.ptr))
-       ohshite(_("error in buffer_write(stream): %s"), desc);
-      break;
-    case BUFFER_WRITE_MD5:
-      MD5Update(&(((struct buffer_write_md5ctx *)data->data.ptr)->ctx), buf, 
length);
-      break;
-    default:
-      fprintf(stderr, _("unknown data type `%i' in buffer_write\n"), 
data->type);
-   }
-   return ret;
-}
-
-off_t buffer_read(buffer_data_t data, void *buf, off_t length, const char 
*desc) {
-  off_t ret= length;
-  if(data->type & BUFFER_READ_SETUP) {
-    return 0;
-  }
-  if(data->type & BUFFER_READ_SHUTDOWN) {
-    return 0;
-  }
-  switch(data->type) {
-    case BUFFER_READ_FD:
-      if((ret= read(data->data.i, buf, length)) < 0 && errno != EINTR)
-       ohshite(_("failed in buffer_read(fd): %s"), desc);
-      break;
-    case BUFFER_READ_STREAM:
-      ret= fread(buf, 1, length, (FILE *)data->data.ptr);
-      if(feof((FILE *)data->data.ptr))
-       return ret;
-      if(ferror((FILE *)data->data.ptr))
-       ohshite(_("error in buffer_read(stream): %s"), desc);
-      break;
-    default:
-      fprintf(stderr, _("unknown data type `%i' in buffer_read\n"), 
data->type);
-   }
-   return ret;
-}
-
-#define buffer_copy_setup_dual(name, type1, name1, type2, name2) \
-off_t buffer_copy_setup_##name(type1 n1, int typeIn, void *procIn,\
-                                       type2 n2, int typeOut, void *procOut,\
-                                       off_t limit, const char *desc, ...)\
-{\
-  va_list al;\
-  buffer_arg a1, a2;\
-  struct varbuf v = VARBUF_INIT;\
-  off_t ret;\
-  a1.name1 = n1; a2.name2 = n2;\
-  va_start(al,desc);\
-  varbufvprintf(&v, desc, al);\
-  va_end(al);\
-  ret = buffer_copy_setup(a1, typeIn, procIn,\
-                          a2, typeOut, procOut,\
-                          limit, v.buf);\
-  varbuffree(&v);\
-  return ret;\
-}
-
-buffer_copy_setup_dual(IntInt, int, i, int, i);
-buffer_copy_setup_dual(IntPtr, int, i, void *, ptr);
-buffer_copy_setup_dual(PtrInt, void *, ptr, int, i);
-buffer_copy_setup_dual(PtrPtr, void *, ptr, void *, ptr);
-
-off_t buffer_copy_setup(buffer_arg argIn, int typeIn, void *procIn,
-                      buffer_arg argOut, int typeOut, void *procOut,
-                      off_t limit, const char *desc)
-{
-  struct buffer_data read_data = { procIn, argIn, typeIn },
-                    write_data = { procOut, argOut, typeOut };
-  off_t ret;
-
-  if ( procIn == NULL )
-    read_data.proc = buffer_read;
-  if ( procOut == NULL )
-    write_data.proc = buffer_write;
-  read_data.type |= BUFFER_READ_SETUP;
-  read_data.proc(&read_data, NULL, 0, desc);
-  read_data.type = typeIn;
-  write_data.type |= BUFFER_WRITE_SETUP;
-  write_data.proc(&write_data, NULL, 0, desc);
-  write_data.type = typeOut;
-  ret = buffer_copy(&read_data, &write_data, limit, desc);
-  write_data.type |= BUFFER_WRITE_SHUTDOWN;
-  write_data.proc(&write_data, NULL, 0, desc);
-  read_data.type |= BUFFER_READ_SHUTDOWN;
-  read_data.proc(&read_data, NULL, 0, desc);
-  return ret;
-}
-
-off_t buffer_copy(buffer_data_t read_data, buffer_data_t write_data, off_t 
limit, const char *desc) {
-  char *buf, *writebuf;
-  long bytesread= 0, byteswritten= 0;
-  int bufsize= 32768;
-  off_t totalread= 0, totalwritten= 0;
-  if((limit!=-1) && (limit < bufsize)) bufsize= limit;
-  if(bufsize == 0)
-    return 0;
-  writebuf = buf= m_malloc(bufsize);
-
-  while(bytesread >= 0 && byteswritten >= 0 && bufsize > 0) {
-    bytesread= read_data->proc(read_data, buf, bufsize, desc);
-    if (bytesread<0) {
-      if (errno==EINTR || errno==EAGAIN) continue;
-      break;
-    }
-    if (bytesread==0)
-      break;
-
-    totalread+= bytesread;
-    if(limit != -1) {
-      limit-= bytesread;
-      if(limit<bufsize)
-       bufsize=limit;
-    }
-    writebuf= buf;
-    while(bytesread) {
-      byteswritten= write_data->proc(write_data, writebuf, bytesread, desc);
-      if(byteswritten == -1) {
-       if(errno == EINTR || errno==EAGAIN) continue;
-       break;
-      }
-      if(byteswritten==0)
-       break;
-      bytesread-= byteswritten;
-      totalwritten+= byteswritten;
-      writebuf+= byteswritten;
-    }
-  }
-  if (bytesread<0 || byteswritten<0) ohshite(_("failed in buffer_copy (%s)"), 
desc);
-  if (limit > 0) ohshit(_("short read in buffer_copy (%s)"), desc);
-
-  free(buf);
-  return totalread;
-}
diff --git a/lib/dpkg/myopt-util.c b/lib/dpkg/myopt-util.c
index 781bd31..318d4f5 100644
--- a/lib/dpkg/myopt-util.c
+++ b/lib/dpkg/myopt-util.c
@@ -28,6 +28,7 @@
 #include <fcntl.h>
 
 #include <dpkg/dpkg.h>
+#include <dpkg/buffer.h>
 #include <dpkg/myopt.h>
 
 void
diff --git a/lib/dpkg/parse.c b/lib/dpkg/parse.c
index 84ac195..1297e4f 100644
--- a/lib/dpkg/parse.c
+++ b/lib/dpkg/parse.c
@@ -40,6 +40,7 @@
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
 #include <dpkg/parsedump.h>
+#include <dpkg/buffer.h>
 
 #ifdef HAVE_MMAP
 #include <sys/mman.h>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 0f1765d..1a63c5c 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -1,5 +1,6 @@
 # This is the list of all source files with translatable strings.
 
+lib/dpkg/buffer.c
 lib/dpkg/cleanup.c
 lib/dpkg/compression.c
 lib/dpkg/database.c
diff --git a/src/archives.c b/src/archives.c
index 2a5765b..598b5df 100644
--- a/src/archives.c
+++ b/src/archives.c
@@ -43,6 +43,7 @@
 
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/buffer.h>
 #include <dpkg/subproc.h>
 #include <dpkg/tarfn.h>
 #include <dpkg/myopt.h>
diff --git a/src/configure.c b/src/configure.c
index 9a55870..68c5fad 100644
--- a/src/configure.c
+++ b/src/configure.c
@@ -45,6 +45,7 @@
 #include <dpkg/macros.h>
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/buffer.h>
 
 #include "filesdb.h"
 #include "main.h"
diff --git a/src/filesdb.c b/src/filesdb.c
index da8cde2..214fadb 100644
--- a/src/filesdb.c
+++ b/src/filesdb.c
@@ -39,6 +39,7 @@
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
 #include <dpkg/path.h>
+#include <dpkg/buffer.h>
 #include <dpkg/progress.h>
 
 #include "filesdb.h"
diff --git a/src/processarc.c b/src/processarc.c
index 9024681..1d40f81 100644
--- a/src/processarc.c
+++ b/src/processarc.c
@@ -40,6 +40,7 @@
 
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/buffer.h>
 #include <dpkg/subproc.h>
 #include <dpkg/tarfn.h>
 #include <dpkg/myopt.h>
diff --git a/src/statdb.c b/src/statdb.c
index 0ad9ddc..4e847d8 100644
--- a/src/statdb.c
+++ b/src/statdb.c
@@ -38,6 +38,7 @@
 
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
+#include <dpkg/buffer.h>
 
 #include "filesdb.h"
 #include "main.h"

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to