buffer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * buffer.c - 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 © 2008-2012 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/types.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <dpkg/i18n.h>
  30. #include <dpkg/dpkg.h>
  31. #include <dpkg/varbuf.h>
  32. #include <dpkg/md5.h>
  33. #include <dpkg/fdio.h>
  34. #include <dpkg/buffer.h>
  35. struct buffer_md5_ctx {
  36. struct MD5Context ctx;
  37. char *hash;
  38. };
  39. static void
  40. buffer_md5_init(struct buffer_data *data)
  41. {
  42. struct buffer_md5_ctx *ctx;
  43. ctx = m_malloc(sizeof(*ctx));
  44. ctx->hash = data->arg.ptr;
  45. data->arg.ptr = ctx;
  46. MD5Init(&ctx->ctx);
  47. }
  48. static off_t
  49. buffer_filter_init(struct buffer_data *data)
  50. {
  51. switch (data->type) {
  52. case BUFFER_FILTER_NULL:
  53. break;
  54. case BUFFER_FILTER_MD5:
  55. buffer_md5_init(data);
  56. break;
  57. }
  58. return 0;
  59. }
  60. static off_t
  61. buffer_filter_update(struct buffer_data *filter, const void *buf, off_t length)
  62. {
  63. off_t ret = length;
  64. switch (filter->type) {
  65. case BUFFER_FILTER_NULL:
  66. break;
  67. case BUFFER_FILTER_MD5:
  68. MD5Update(&(((struct buffer_md5_ctx *)filter->arg.ptr)->ctx),
  69. buf, length);
  70. break;
  71. default:
  72. internerr("unknown data type %i", filter->type);
  73. }
  74. return ret;
  75. }
  76. static void
  77. buffer_md5_done(struct buffer_data *data)
  78. {
  79. struct buffer_md5_ctx *ctx;
  80. unsigned char digest[16], *p = digest;
  81. char *hash;
  82. int i;
  83. ctx = (struct buffer_md5_ctx *)data->arg.ptr;
  84. hash = ctx->hash;
  85. MD5Final(digest, &ctx->ctx);
  86. for (i = 0; i < 16; ++i) {
  87. sprintf(hash, "%02x", *p++);
  88. hash += 2;
  89. }
  90. *hash = '\0';
  91. free(ctx);
  92. }
  93. static off_t
  94. buffer_filter_done(struct buffer_data *data)
  95. {
  96. switch (data->type) {
  97. case BUFFER_FILTER_NULL:
  98. break;
  99. case BUFFER_FILTER_MD5:
  100. buffer_md5_done(data);
  101. break;
  102. }
  103. return 0;
  104. }
  105. static off_t
  106. buffer_write(struct buffer_data *data, const void *buf, off_t length)
  107. {
  108. off_t ret = length;
  109. switch (data->type) {
  110. case BUFFER_WRITE_VBUF:
  111. varbuf_add_buf((struct varbuf *)data->arg.ptr, buf, length);
  112. break;
  113. case BUFFER_WRITE_FD:
  114. ret = fd_write(data->arg.i, buf, length);
  115. break;
  116. case BUFFER_WRITE_NULL:
  117. break;
  118. default:
  119. internerr("unknown data type %i", data->type);
  120. }
  121. return ret;
  122. }
  123. static off_t
  124. buffer_read(struct buffer_data *data, void *buf, off_t length)
  125. {
  126. off_t ret;
  127. switch (data->type) {
  128. case BUFFER_READ_FD:
  129. ret = fd_read(data->arg.i, buf, length);
  130. break;
  131. default:
  132. internerr("unknown data type %i", data->type);
  133. }
  134. return ret;
  135. }
  136. off_t
  137. buffer_filter(const void *input, void *output, int type, off_t limit)
  138. {
  139. struct buffer_data data = { .arg.ptr = output, .type = type };
  140. off_t ret;
  141. buffer_filter_init(&data);
  142. ret = buffer_filter_update(&data, input, limit);
  143. buffer_filter_done(&data);
  144. return ret;
  145. }
  146. static off_t
  147. buffer_copy(struct buffer_data *read_data,
  148. struct buffer_data *filter,
  149. struct buffer_data *write_data,
  150. off_t limit, const char *desc)
  151. {
  152. char *buf;
  153. int bufsize = 32768;
  154. off_t bytesread = 0, byteswritten = 0;
  155. off_t totalread = 0, totalwritten = 0;
  156. if ((limit != -1) && (limit < bufsize))
  157. bufsize = limit;
  158. if (bufsize == 0)
  159. buf = NULL;
  160. else
  161. buf = m_malloc(bufsize);
  162. buffer_filter_init(filter);
  163. while (bufsize > 0) {
  164. bytesread = buffer_read(read_data, buf, bufsize);
  165. if (bytesread < 0)
  166. ohshite(_("failed to read on buffer copy for %s"), desc);
  167. if (bytesread == 0)
  168. break;
  169. totalread += bytesread;
  170. if (limit != -1) {
  171. limit -= bytesread;
  172. if (limit < bufsize)
  173. bufsize = limit;
  174. }
  175. buffer_filter_update(filter, buf, bytesread);
  176. byteswritten = buffer_write(write_data, buf, bytesread);
  177. if (byteswritten < 0)
  178. ohshite(_("failed in write on buffer copy for %s"), desc);
  179. if (byteswritten == 0)
  180. break;
  181. totalwritten += byteswritten;
  182. }
  183. if (limit > 0)
  184. ohshit(_("short read on buffer copy for %s"), desc);
  185. buffer_filter_done(filter);
  186. free(buf);
  187. return totalread;
  188. }
  189. off_t
  190. buffer_copy_IntInt(int Iin, int Tin,
  191. void *Pfilter, int Tfilter,
  192. int Iout, int Tout,
  193. off_t limit, const char *desc, ...)
  194. {
  195. va_list args;
  196. struct buffer_data read_data = { .type = Tin, .arg.i = Iin };
  197. struct buffer_data filter = { .type = Tfilter, .arg.ptr = Pfilter };
  198. struct buffer_data write_data = { .type = Tout, .arg.i = Iout };
  199. struct varbuf v = VARBUF_INIT;
  200. off_t ret;
  201. va_start(args, desc);
  202. varbuf_vprintf(&v, desc, args);
  203. va_end(args);
  204. ret = buffer_copy(&read_data, &filter, &write_data, limit, v.buf);
  205. varbuf_destroy(&v);
  206. return ret;
  207. }
  208. off_t
  209. buffer_copy_IntPtr(int Iin, int Tin,
  210. void *Pfilter, int Tfilter,
  211. void *Pout, int Tout,
  212. off_t limit, const char *desc, ...)
  213. {
  214. va_list args;
  215. struct buffer_data read_data = { .type = Tin, .arg.i = Iin };
  216. struct buffer_data filter = { .type = Tfilter, .arg.ptr = Pfilter };
  217. struct buffer_data write_data = { .type = Tout, .arg.ptr = Pout };
  218. struct varbuf v = VARBUF_INIT;
  219. off_t ret;
  220. va_start(args, desc);
  221. varbuf_vprintf(&v, desc, args);
  222. va_end(args);
  223. ret = buffer_copy(&read_data, &filter, &write_data, limit, v.buf);
  224. varbuf_destroy(&v);
  225. return ret;
  226. }
  227. static off_t
  228. buffer_skip(struct buffer_data *input, off_t limit, const char *desc)
  229. {
  230. struct buffer_data output;
  231. struct buffer_data filter;
  232. switch (input->type) {
  233. case BUFFER_READ_FD:
  234. if (lseek(input->arg.i, limit, SEEK_CUR) != -1)
  235. return limit;
  236. if (errno != ESPIPE)
  237. ohshite(_("failed to seek %s"), desc);
  238. break;
  239. default:
  240. internerr("unknown data type %i", input->type);
  241. }
  242. output.type = BUFFER_WRITE_NULL;
  243. output.arg.ptr = NULL;
  244. filter.type = BUFFER_FILTER_NULL;
  245. filter.arg.ptr = NULL;
  246. return buffer_copy(input, &filter, &output, limit, desc);
  247. }
  248. off_t
  249. buffer_skip_Int(int I, int T, off_t limit, const char *desc_fmt, ...)
  250. {
  251. va_list args; \
  252. struct buffer_data input = { .type = T, .arg.i = I };
  253. struct varbuf v = VARBUF_INIT;
  254. off_t ret;
  255. va_start(args, desc_fmt);
  256. varbuf_vprintf(&v, desc_fmt, args);
  257. va_end(args);
  258. ret = buffer_skip(&input, limit, v.buf);
  259. varbuf_destroy(&v);
  260. return ret;
  261. }