buffer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_STREAM 4
  33. #define BUFFER_WRITE_MD5 5
  34. #define BUFFER_READ_FD 0
  35. #define BUFFER_READ_STREAM 1
  36. struct buffer_data {
  37. union {
  38. void *ptr;
  39. int i;
  40. } arg;
  41. int type;
  42. };
  43. # define buffer_md5(buf, hash, limit) \
  44. buffer_hash(buf, hash, BUFFER_WRITE_MD5, limit)
  45. # define fd_md5(fd, hash, limit, ...) \
  46. buffer_copy_IntPtr(fd, BUFFER_READ_FD, hash, BUFFER_WRITE_MD5, \
  47. limit, __VA_ARGS__)
  48. # define stream_md5(file, hash, limit, ...) \
  49. buffer_copy_PtrPtr(file, BUFFER_READ_STREAM, hash, BUFFER_WRITE_MD5, \
  50. limit, __VA_ARGS__)
  51. # define fd_fd_copy(fd1, fd2, limit, ...) \
  52. buffer_copy_IntInt(fd1, BUFFER_READ_FD, fd2, BUFFER_WRITE_FD, \
  53. limit, __VA_ARGS__)
  54. # define fd_buf_copy(fd, buf, limit, ...) \
  55. buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_BUF, \
  56. limit, __VA_ARGS__)
  57. # define fd_vbuf_copy(fd, buf, limit, ...) \
  58. buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_VBUF, \
  59. limit, __VA_ARGS__)
  60. # define fd_null_copy(fd, limit, ...) \
  61. if (lseek(fd, limit, SEEK_CUR) == -1) { \
  62. if (errno != ESPIPE) \
  63. ohshite(__VA_ARGS__); \
  64. buffer_copy_IntPtr(fd, BUFFER_READ_FD, \
  65. NULL, BUFFER_WRITE_NULL, \
  66. limit, __VA_ARGS__); \
  67. }
  68. # define stream_null_copy(file, limit, ...) \
  69. if (fseek(file, limit, SEEK_CUR) == -1) { \
  70. if (errno != EBADF) \
  71. ohshite(__VA_ARGS__); \
  72. buffer_copy_PtrPtr(file, BUFFER_READ_STREAM, \
  73. NULL, BUFFER_WRITE_NULL, \
  74. limit, __VA_ARGS__); \
  75. }
  76. # define stream_fd_copy(file, fd, limit, ...) \
  77. buffer_copy_PtrInt(file, BUFFER_READ_STREAM, fd, BUFFER_WRITE_FD, \
  78. limit, __VA_ARGS__)
  79. off_t buffer_copy_PtrInt(void *p, int typeIn, int i, int typeOut,
  80. off_t limit, const char *desc,
  81. ...) DPKG_ATTR_PRINTF(6);
  82. off_t buffer_copy_PtrPtr(void *p1, int typeIn, void *p2, int typeOut,
  83. off_t limit, const char *desc,
  84. ...) DPKG_ATTR_PRINTF(6);
  85. off_t buffer_copy_IntPtr(int i, int typeIn, void *p, int typeOut,
  86. off_t limit, const char *desc,
  87. ...) DPKG_ATTR_PRINTF(6);
  88. off_t buffer_copy_IntInt(int i1, int typeIn, int i2, int typeOut,
  89. off_t limit, const char *desc,
  90. ...) DPKG_ATTR_PRINTF(6);
  91. off_t buffer_hash(const void *buf, void *hash, int typeOut, off_t length);
  92. off_t buffer_write(struct buffer_data *data, const void *buf, off_t length);
  93. off_t buffer_read(struct buffer_data *data, void *buf, off_t length);
  94. off_t buffer_init(struct buffer_data *read_data,
  95. struct buffer_data *write_data);
  96. off_t buffer_done(struct buffer_data *read_data,
  97. struct buffer_data *write_data);
  98. off_t buffer_copy(struct buffer_data *read_data,
  99. struct buffer_data *write_data,
  100. off_t limit, const char *desc);
  101. DPKG_END_DECLS
  102. #endif /* LIBDPKG_BUFFER_H */