buffer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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
  12. * published by the Free Software Foundation; either version 2,
  13. * or (at your option) any later version.
  14. *
  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.
  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. #if HAVE_C99
  46. # define fd_md5(fd, hash, limit, ...) \
  47. buffer_copy_IntPtr(fd, BUFFER_READ_FD, hash, BUFFER_WRITE_MD5, \
  48. limit, __VA_ARGS__)
  49. # define stream_md5(file, hash, limit, ...) \
  50. buffer_copy_PtrPtr(file, BUFFER_READ_STREAM, hash, BUFFER_WRITE_MD5, \
  51. limit, __VA_ARGS__)
  52. # define fd_fd_copy(fd1, fd2, limit, ...) \
  53. buffer_copy_IntInt(fd1, BUFFER_READ_FD, fd2, BUFFER_WRITE_FD, \
  54. limit, __VA_ARGS__)
  55. # define fd_buf_copy(fd, buf, limit, ...) \
  56. buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_BUF, \
  57. limit, __VA_ARGS__)
  58. # define fd_vbuf_copy(fd, buf, limit, ...) \
  59. buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_VBUF, \
  60. limit, __VA_ARGS__)
  61. # define fd_null_copy(fd, limit, ...) \
  62. if (lseek(fd, limit, SEEK_CUR) == -1) { \
  63. if (errno != ESPIPE) \
  64. ohshite(__VA_ARGS__); \
  65. buffer_copy_IntPtr(fd, BUFFER_READ_FD, \
  66. NULL, BUFFER_WRITE_NULL, \
  67. limit, __VA_ARGS__); \
  68. }
  69. # define stream_null_copy(file, limit, ...) \
  70. if (fseek(file, limit, SEEK_CUR) == -1) { \
  71. if (errno != EBADF) \
  72. ohshite(__VA_ARGS__); \
  73. buffer_copy_PtrPtr(file, BUFFER_READ_STREAM, \
  74. NULL, BUFFER_WRITE_NULL, \
  75. limit, __VA_ARGS__); \
  76. }
  77. # define stream_fd_copy(file, fd, limit, ...) \
  78. buffer_copy_PtrInt(file, BUFFER_READ_STREAM, fd, BUFFER_WRITE_FD, \
  79. limit, __VA_ARGS__)
  80. #else /* HAVE_C99 */
  81. # define fd_md5(fd, hash, limit, desc...) \
  82. buffer_copy_IntPtr(fd, BUFFER_READ_FD, hash, BUFFER_WRITE_MD5, \
  83. limit, desc)
  84. # define stream_md5(file, hash, limit, desc...) \
  85. buffer_copy_PtrPtr(file, BUFFER_READ_STREAM, hash, BUFFER_WRITE_MD5, \
  86. limit, desc)
  87. # define fd_fd_copy(fd1, fd2, limit, desc...) \
  88. buffer_copy_IntInt(fd1, BUFFER_READ_FD, fd2, BUFFER_WRITE_FD, \
  89. limit, desc)
  90. # define fd_buf_copy(fd, buf, limit, desc...) \
  91. buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_BUF, \
  92. limit, desc)
  93. # define fd_vbuf_copy(fd, buf, limit, desc...) \
  94. buffer_copy_IntPtr(fd, BUFFER_READ_FD, buf, BUFFER_WRITE_VBUF, \
  95. limit, desc)
  96. # define fd_null_copy(fd, limit, desc...) \
  97. if (lseek(fd, limit, SEEK_CUR) == -1) { \
  98. if (errno != ESPIPE) \
  99. ohshite(desc); \
  100. buffer_copy_IntPtr(fd, BUFFER_READ_FD, \
  101. NULL, BUFFER_WRITE_NULL, \
  102. limit, desc); \
  103. }
  104. # define stream_null_copy(file, limit, desc...) \
  105. if (fseek(file, limit, SEEK_CUR) == -1) { \
  106. if (errno != EBADF) \
  107. ohshite(desc); \
  108. buffer_copy_PtrPtr(file, BUFFER_READ_STREAM, \
  109. NULL, BUFFER_WRITE_NULL, \
  110. limit, desc); \
  111. }
  112. # define stream_fd_copy(file, fd, limit, desc...)\
  113. buffer_copy_PtrInt(file, BUFFER_READ_STREAM, fd, BUFFER_WRITE_FD, \
  114. limit, desc)
  115. #endif /* HAVE_C99 */
  116. off_t buffer_copy_PtrInt(void *p, int typeIn, int i, int typeOut,
  117. off_t limit, const char *desc,
  118. ...) DPKG_ATTR_PRINTF(6);
  119. off_t buffer_copy_PtrPtr(void *p1, int typeIn, void *p2, int typeOut,
  120. off_t limit, const char *desc,
  121. ...) DPKG_ATTR_PRINTF(6);
  122. off_t buffer_copy_IntPtr(int i, int typeIn, void *p, int typeOut,
  123. off_t limit, const char *desc,
  124. ...) DPKG_ATTR_PRINTF(6);
  125. off_t buffer_copy_IntInt(int i1, int typeIn, int i2, int typeOut,
  126. off_t limit, const char *desc,
  127. ...) DPKG_ATTR_PRINTF(6);
  128. off_t buffer_hash(const void *buf, void *hash, int typeOut, off_t length);
  129. off_t buffer_write(struct buffer_data *data, const void *buf,
  130. off_t length, const char *desc);
  131. off_t buffer_read(struct buffer_data *data, void *buf,
  132. off_t length, const char *desc);
  133. off_t buffer_init(struct buffer_data *read_data,
  134. struct buffer_data *write_data);
  135. off_t buffer_done(struct buffer_data *read_data,
  136. struct buffer_data *write_data);
  137. off_t buffer_copy(struct buffer_data *read_data,
  138. struct buffer_data *write_data,
  139. off_t limit, const char *desc);
  140. DPKG_END_DECLS
  141. #endif /* LIBDPKG_BUFFER_H */