buffer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 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 <stdio.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/dpkg.h>
  32. #include <dpkg/varbuf.h>
  33. #include <dpkg/md5.h>
  34. #include <dpkg/buffer.h>
  35. struct buffer_write_md5ctx {
  36. struct MD5Context ctx;
  37. char *hash;
  38. };
  39. static void
  40. buffer_md5_init(struct buffer_data *data)
  41. {
  42. struct buffer_write_md5ctx *ctx;
  43. ctx = m_malloc(sizeof(struct buffer_write_md5ctx));
  44. ctx->hash = data->arg.ptr;
  45. data->arg.ptr = ctx;
  46. MD5Init(&ctx->ctx);
  47. }
  48. off_t
  49. buffer_init(struct buffer_data *read_data, struct buffer_data *write_data)
  50. {
  51. switch (write_data->type) {
  52. case BUFFER_WRITE_MD5:
  53. buffer_md5_init(write_data);
  54. break;
  55. }
  56. return 0;
  57. }
  58. static void
  59. buffer_md5_done(struct buffer_data *data)
  60. {
  61. struct buffer_write_md5ctx *ctx;
  62. unsigned char digest[16], *p = digest;
  63. char *hash;
  64. int i;
  65. ctx = (struct buffer_write_md5ctx *)data->arg.ptr;
  66. hash = ctx->hash;
  67. MD5Final(digest, &ctx->ctx);
  68. for (i = 0; i < 16; ++i) {
  69. sprintf(hash, "%02x", *p++);
  70. hash += 2;
  71. }
  72. *hash = '\0';
  73. free(ctx);
  74. }
  75. off_t
  76. buffer_done(struct buffer_data *read_data, struct buffer_data *write_data)
  77. {
  78. switch (write_data->type) {
  79. case BUFFER_WRITE_MD5:
  80. buffer_md5_done(write_data);
  81. break;
  82. }
  83. return 0;
  84. }
  85. off_t
  86. buffer_write(struct buffer_data *data, const void *buf, off_t length,
  87. const char *desc)
  88. {
  89. off_t ret = length;
  90. switch (data->type) {
  91. case BUFFER_WRITE_BUF:
  92. memcpy(data->arg.ptr, buf, length);
  93. data->arg.ptr += length;
  94. break;
  95. case BUFFER_WRITE_VBUF:
  96. varbufaddbuf((struct varbuf *)data->arg.ptr, buf, length);
  97. break;
  98. case BUFFER_WRITE_FD:
  99. ret = write(data->arg.i, buf, length);
  100. if (ret < 0 && errno != EINTR)
  101. ohshite(_("failed in buffer_write(fd) (%i, ret=%li): %s"),
  102. data->arg.i, (long)ret, desc);
  103. break;
  104. case BUFFER_WRITE_NULL:
  105. break;
  106. case BUFFER_WRITE_STREAM:
  107. ret = fwrite(buf, 1, length, (FILE *)data->arg.ptr);
  108. if (feof((FILE *)data->arg.ptr))
  109. ohshite(_("eof in buffer_write(stream): %s"), desc);
  110. if(ferror((FILE *)data->arg.ptr))
  111. ohshite(_("error in buffer_write(stream): %s"), desc);
  112. break;
  113. case BUFFER_WRITE_MD5:
  114. MD5Update(&(((struct buffer_write_md5ctx *)data->arg.ptr)->ctx), buf, length);
  115. break;
  116. default:
  117. internerr("unknown data type '%i' in buffer_write",
  118. data->type);
  119. }
  120. return ret;
  121. }
  122. off_t
  123. buffer_read(struct buffer_data *data, void *buf, off_t length,
  124. const char *desc)
  125. {
  126. off_t ret = length;
  127. switch (data->type) {
  128. case BUFFER_READ_FD:
  129. ret = read(data->arg.i, buf, length);
  130. if(ret < 0 && errno != EINTR)
  131. ohshite(_("failed in buffer_read(fd): %s"), desc);
  132. break;
  133. case BUFFER_READ_STREAM:
  134. ret = fread(buf, 1, length, (FILE *)data->arg.ptr);
  135. if (feof((FILE *)data->arg.ptr))
  136. return ret;
  137. if (ferror((FILE *)data->arg.ptr))
  138. ohshite(_("error in buffer_read(stream): %s"), desc);
  139. break;
  140. default:
  141. internerr("unknown data type '%i' in buffer_read\n",
  142. data->type);
  143. }
  144. return ret;
  145. }
  146. #define buffer_copy_TYPE(name, type1, name1, type2, name2) \
  147. off_t \
  148. buffer_copy_##name(type1 n1, int typeIn, \
  149. type2 n2, int typeOut, \
  150. off_t limit, const char *desc, ...) \
  151. { \
  152. va_list al; \
  153. struct buffer_data read_data, write_data; \
  154. struct varbuf v = VARBUF_INIT; \
  155. off_t ret; \
  156. \
  157. read_data.arg.name1 = n1; \
  158. read_data.type = typeIn; \
  159. write_data.arg.name2 = n2; \
  160. write_data.type = typeOut; \
  161. \
  162. va_start(al, desc); \
  163. varbufvprintf(&v, desc, al); \
  164. va_end(al); \
  165. \
  166. buffer_init(&read_data, &write_data); \
  167. ret = buffer_copy(&read_data, &write_data, limit, v.buf); \
  168. buffer_done(&read_data, &write_data); \
  169. \
  170. varbuffree(&v); \
  171. \
  172. return ret; \
  173. }
  174. buffer_copy_TYPE(IntInt, int, i, int, i);
  175. buffer_copy_TYPE(IntPtr, int, i, void *, ptr);
  176. buffer_copy_TYPE(PtrInt, void *, ptr, int, i);
  177. buffer_copy_TYPE(PtrPtr, void *, ptr, void *, ptr);
  178. off_t
  179. buffer_hash(const void *input, void *output, int type, off_t limit)
  180. {
  181. struct buffer_data data = { { output }, type };
  182. off_t ret;
  183. buffer_init(NULL, &data);
  184. ret = buffer_write(&data, input, limit, NULL);
  185. buffer_done(NULL, &data);
  186. return ret;
  187. }
  188. off_t
  189. buffer_copy(struct buffer_data *read_data, struct buffer_data *write_data,
  190. off_t limit, const char *desc)
  191. {
  192. char *buf, *writebuf;
  193. int bufsize = 32768;
  194. long bytesread = 0, byteswritten = 0;
  195. off_t totalread = 0, totalwritten = 0;
  196. if ((limit != -1) && (limit < bufsize))
  197. bufsize = limit;
  198. if (bufsize == 0)
  199. return 0;
  200. writebuf = buf = m_malloc(bufsize);
  201. while (bytesread >= 0 && byteswritten >= 0 && bufsize > 0) {
  202. bytesread = buffer_read(read_data, buf, bufsize, desc);
  203. if (bytesread < 0) {
  204. if (errno == EINTR || errno == EAGAIN)
  205. continue;
  206. break;
  207. }
  208. if (bytesread == 0)
  209. break;
  210. totalread += bytesread;
  211. if (limit != -1) {
  212. limit -= bytesread;
  213. if (limit < bufsize)
  214. bufsize = limit;
  215. }
  216. writebuf = buf;
  217. while (bytesread) {
  218. byteswritten = buffer_write(write_data, writebuf, bytesread, desc);
  219. if (byteswritten == -1) {
  220. if (errno == EINTR || errno == EAGAIN)
  221. continue;
  222. break;
  223. }
  224. if (byteswritten == 0)
  225. break;
  226. bytesread -= byteswritten;
  227. totalwritten += byteswritten;
  228. writebuf += byteswritten;
  229. }
  230. }
  231. if (bytesread < 0 || byteswritten < 0)
  232. ohshite(_("failed in buffer_copy (%s)"), desc);
  233. if (limit > 0)
  234. ohshit(_("short read in buffer_copy (%s)"), desc);
  235. free(buf);
  236. return totalread;
  237. }