buffer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * buffer.h - buffer I/O handling routines
  4. *
  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>
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This is distributed in the hope that it will be useful,
  16. * but 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.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #ifndef LIBDPKG_BUFFER_H
  24. #define LIBDPKG_BUFFER_H
  25. #include <sys/types.h>
  26. #include <dpkg/macros.h>
  27. DPKG_BEGIN_DECLS
  28. #define BUFFER_WRITE_BUF 0
  29. #define BUFFER_WRITE_VBUF 1
  30. #define BUFFER_WRITE_FD 2
  31. #define BUFFER_WRITE_NULL 3
  32. #define BUFFER_WRITE_MD5 5
  33. #define BUFFER_READ_FD 0
  34. struct buffer_data {
  35. union {
  36. void *ptr;
  37. int i;
  38. } arg;
  39. int type;
  40. };
  41. # define buffer_md5(buf, hash, limit) \
  42. buffer_hash(buf, hash, BUFFER_WRITE_MD5, limit)
  43. # define fd_md5(fd, hash, limit, ...) \
  44. buffer_copy_IntPtr(fd, BUFFER_READ_FD, hash, BUFFER_WRITE_MD5, \
  45. limit, __VA_ARGS__)
  46. # define fd_fd_copy(fd1, fd2, limit, ...) \
  47. buffer_copy_IntInt(fd1, BUFFER_READ_FD, fd2, BUFFER_WRITE_FD, \
  48. limit, __VA_ARGS__)
  49. # define fd_buf_copy(fd, buf, limit, ...) \
  50. buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_BUF, \
  51. limit, __VA_ARGS__)
  52. # define fd_vbuf_copy(fd, buf, limit, ...) \
  53. buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_VBUF, \
  54. limit, __VA_ARGS__)
  55. # define fd_null_copy(fd, limit, ...) \
  56. if (lseek(fd, limit, SEEK_CUR) == -1) { \
  57. if (errno != ESPIPE) \
  58. ohshite(__VA_ARGS__); \
  59. buffer_copy_IntPtr(fd, BUFFER_READ_FD, \
  60. NULL, BUFFER_WRITE_NULL, \
  61. limit, __VA_ARGS__); \
  62. }
  63. off_t buffer_copy_PtrInt(void *p, int typeIn, int i, int typeOut,
  64. off_t limit, const char *desc,
  65. ...) DPKG_ATTR_PRINTF(6);
  66. off_t buffer_copy_PtrPtr(void *p1, int typeIn, void *p2, int typeOut,
  67. off_t limit, const char *desc,
  68. ...) DPKG_ATTR_PRINTF(6);
  69. off_t buffer_copy_IntPtr(int i, int typeIn, void *p, int typeOut,
  70. off_t limit, const char *desc,
  71. ...) DPKG_ATTR_PRINTF(6);
  72. off_t buffer_copy_IntInt(int i1, int typeIn, int i2, int typeOut,
  73. off_t limit, const char *desc,
  74. ...) DPKG_ATTR_PRINTF(6);
  75. off_t buffer_hash(const void *buf, void *hash, int typeOut, off_t length);
  76. off_t buffer_write(struct buffer_data *data, const void *buf, off_t length);
  77. off_t buffer_read(struct buffer_data *data, void *buf, off_t length);
  78. off_t buffer_init(struct buffer_data *read_data,
  79. struct buffer_data *write_data);
  80. off_t buffer_done(struct buffer_data *read_data,
  81. struct buffer_data *write_data);
  82. off_t buffer_copy(struct buffer_data *read_data,
  83. struct buffer_data *write_data,
  84. off_t limit, const char *desc);
  85. DPKG_END_DECLS
  86. #endif /* LIBDPKG_BUFFER_H */