| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- /*
- * libdpkg - Debian packaging suite library routines
- * buffer.c - buffer I/O handling routines
- *
- * Copyright © 1999, 2000 Wichert Akkerman <wakkerma@debian.org>
- * Copyright © 2000-2003 Adam Heath <doogie@debian.org>
- * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
- *
- * 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;
- };
- static void
- buffer_md5_init(buffer_data_t data)
- {
- 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);
- }
- static void
- buffer_md5_done(buffer_data_t data)
- {
- 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);
- }
- 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:
- buffer_md5_init(data);
- break;
- }
- return 0;
- }
- if (data->type & BUFFER_WRITE_SHUTDOWN) {
- switch (data->type ^ BUFFER_WRITE_SHUTDOWN) {
- case BUFFER_WRITE_MD5:
- buffer_md5_done(data);
- 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;
- }
|