buffer.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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, 2009 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
  11. * published by the Free Software Foundation; either version 2,
  12. * or (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful, but
  15. * 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
  20. * License along with dpkg; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <config.h>
  24. #include <compat.h>
  25. #include <dpkg/i18n.h>
  26. #include <sys/types.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. #include <dpkg/dpkg.h>
  33. #include <dpkg/varbuf.h>
  34. #include <dpkg/md5.h>
  35. #include <dpkg/buffer.h>
  36. struct buffer_write_md5ctx {
  37. struct MD5Context ctx;
  38. unsigned char **hash;
  39. };
  40. static void
  41. buffer_md5_init(buffer_data_t data)
  42. {
  43. struct buffer_write_md5ctx *ctx;
  44. ctx = m_malloc(sizeof(struct buffer_write_md5ctx));
  45. ctx->hash = data->data.ptr;
  46. data->data.ptr = ctx;
  47. MD5Init(&ctx->ctx);
  48. }
  49. static void
  50. buffer_md5_done(buffer_data_t data)
  51. {
  52. struct buffer_write_md5ctx *ctx;
  53. unsigned char digest[16], *p = digest;
  54. unsigned char *hash;
  55. int i;
  56. ctx = (struct buffer_write_md5ctx *)data->data.ptr;
  57. *ctx->hash = hash = m_malloc(MD5HASHLEN + 1);
  58. MD5Final(digest, &ctx->ctx);
  59. for (i = 0; i < 16; ++i) {
  60. sprintf((char *)hash, "%02x", *p++);
  61. hash += 2;
  62. }
  63. *hash = '\0';
  64. free(ctx);
  65. }
  66. off_t
  67. buffer_write(buffer_data_t data, void *buf, off_t length, const char *desc)
  68. {
  69. off_t ret = length;
  70. if (data->type & BUFFER_WRITE_SETUP) {
  71. switch (data->type ^ BUFFER_WRITE_SETUP) {
  72. case BUFFER_WRITE_MD5:
  73. buffer_md5_init(data);
  74. break;
  75. }
  76. return 0;
  77. }
  78. if (data->type & BUFFER_WRITE_SHUTDOWN) {
  79. switch (data->type ^ BUFFER_WRITE_SHUTDOWN) {
  80. case BUFFER_WRITE_MD5:
  81. buffer_md5_done(data);
  82. break;
  83. }
  84. return 0;
  85. }
  86. switch (data->type) {
  87. case BUFFER_WRITE_BUF:
  88. memcpy(data->data.ptr, buf, length);
  89. data->data.ptr += length;
  90. break;
  91. case BUFFER_WRITE_VBUF:
  92. varbufaddbuf((struct varbuf *)data->data.ptr, buf, length);
  93. break;
  94. case BUFFER_WRITE_FD:
  95. ret = write(data->data.i, buf, length);
  96. if (ret < 0 && errno != EINTR)
  97. ohshite(_("failed in buffer_write(fd) (%i, ret=%li): %s"),
  98. data->data.i, (long)ret, desc);
  99. break;
  100. case BUFFER_WRITE_NULL:
  101. break;
  102. case BUFFER_WRITE_STREAM:
  103. ret = fwrite(buf, 1, length, (FILE *)data->data.ptr);
  104. if (feof((FILE *)data->data.ptr))
  105. ohshite(_("eof in buffer_write(stream): %s"), desc);
  106. if(ferror((FILE *)data->data.ptr))
  107. ohshite(_("error in buffer_write(stream): %s"), desc);
  108. break;
  109. case BUFFER_WRITE_MD5:
  110. MD5Update(&(((struct buffer_write_md5ctx *)data->data.ptr)->ctx), buf, length);
  111. break;
  112. default:
  113. fprintf(stderr, _("unknown data type `%i' in buffer_write\n"),
  114. data->type);
  115. }
  116. return ret;
  117. }
  118. off_t
  119. buffer_read(buffer_data_t data, void *buf, off_t length, const char *desc)
  120. {
  121. off_t ret = length;
  122. if (data->type & BUFFER_READ_SETUP)
  123. return 0;
  124. if (data->type & BUFFER_READ_SHUTDOWN)
  125. return 0;
  126. switch (data->type) {
  127. case BUFFER_READ_FD:
  128. ret = read(data->data.i, buf, length);
  129. if(ret < 0 && errno != EINTR)
  130. ohshite(_("failed in buffer_read(fd): %s"), desc);
  131. break;
  132. case BUFFER_READ_STREAM:
  133. ret = fread(buf, 1, length, (FILE *)data->data.ptr);
  134. if (feof((FILE *)data->data.ptr))
  135. return ret;
  136. if (ferror((FILE *)data->data.ptr))
  137. ohshite(_("error in buffer_read(stream): %s"), desc);
  138. break;
  139. default:
  140. fprintf(stderr, _("unknown data type `%i' in buffer_read\n"),
  141. data->type);
  142. }
  143. return ret;
  144. }
  145. off_t
  146. buffer_copy_setup(buffer_arg argIn, int typeIn, void *procIn,
  147. buffer_arg argOut, int typeOut, void *procOut,
  148. off_t limit, const char *desc)
  149. {
  150. struct buffer_data read_data = { procIn, argIn, typeIn },
  151. write_data = { procOut, argOut, typeOut };
  152. off_t ret;
  153. if (procIn == NULL)
  154. read_data.proc = buffer_read;
  155. if (procOut == NULL)
  156. write_data.proc = buffer_write;
  157. read_data.type |= BUFFER_READ_SETUP;
  158. read_data.proc(&read_data, NULL, 0, desc);
  159. read_data.type = typeIn;
  160. write_data.type |= BUFFER_WRITE_SETUP;
  161. write_data.proc(&write_data, NULL, 0, desc);
  162. write_data.type = typeOut;
  163. ret = buffer_copy(&read_data, &write_data, limit, desc);
  164. write_data.type |= BUFFER_WRITE_SHUTDOWN;
  165. write_data.proc(&write_data, NULL, 0, desc);
  166. read_data.type |= BUFFER_READ_SHUTDOWN;
  167. read_data.proc(&read_data, NULL, 0, desc);
  168. return ret;
  169. }
  170. #define buffer_copy_setup_dual(name, type1, name1, type2, name2) \
  171. off_t \
  172. buffer_copy_setup_##name(type1 n1, int typeIn, void *procIn, \
  173. type2 n2, int typeOut, void *procOut, \
  174. off_t limit, const char *desc, ...) \
  175. { \
  176. va_list al; \
  177. buffer_arg a1, a2; \
  178. struct varbuf v = VARBUF_INIT; \
  179. off_t ret; \
  180. \
  181. a1.name1 = n1; \
  182. a2.name2 = n2; \
  183. va_start(al, desc); \
  184. varbufvprintf(&v, desc, al); \
  185. va_end(al); \
  186. \
  187. ret = buffer_copy_setup(a1, typeIn, procIn, \
  188. a2, typeOut, procOut, \
  189. limit, v.buf); \
  190. varbuffree(&v); \
  191. \
  192. return ret; \
  193. }
  194. buffer_copy_setup_dual(IntInt, int, i, int, i);
  195. buffer_copy_setup_dual(IntPtr, int, i, void *, ptr);
  196. buffer_copy_setup_dual(PtrInt, void *, ptr, int, i);
  197. buffer_copy_setup_dual(PtrPtr, void *, ptr, void *, ptr);
  198. off_t
  199. buffer_copy(buffer_data_t read_data, buffer_data_t write_data,
  200. off_t limit, const char *desc)
  201. {
  202. char *buf, *writebuf;
  203. int bufsize = 32768;
  204. long bytesread = 0, byteswritten = 0;
  205. off_t totalread = 0, totalwritten = 0;
  206. if ((limit != -1) && (limit < bufsize))
  207. bufsize = limit;
  208. if (bufsize == 0)
  209. return 0;
  210. writebuf = buf = m_malloc(bufsize);
  211. while (bytesread >= 0 && byteswritten >= 0 && bufsize > 0) {
  212. bytesread = read_data->proc(read_data, buf, bufsize, desc);
  213. if (bytesread < 0) {
  214. if (errno == EINTR || errno == EAGAIN)
  215. continue;
  216. break;
  217. }
  218. if (bytesread == 0)
  219. break;
  220. totalread += bytesread;
  221. if (limit != -1) {
  222. limit -= bytesread;
  223. if (limit < bufsize)
  224. bufsize = limit;
  225. }
  226. writebuf = buf;
  227. while (bytesread) {
  228. byteswritten = write_data->proc(write_data, writebuf, bytesread, desc);
  229. if (byteswritten == -1) {
  230. if (errno == EINTR || errno == EAGAIN)
  231. continue;
  232. break;
  233. }
  234. if (byteswritten == 0)
  235. break;
  236. bytesread -= byteswritten;
  237. totalwritten += byteswritten;
  238. writebuf += byteswritten;
  239. }
  240. }
  241. if (bytesread < 0 || byteswritten < 0)
  242. ohshite(_("failed in buffer_copy (%s)"), desc);
  243. if (limit > 0)
  244. ohshit(_("short read in buffer_copy (%s)"), desc);
  245. free(buf);
  246. return totalread;
  247. }