archive_write_open_memory.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_open_memory.c,v 1.3 2007/01/09 08:05:56 kientzle Exp $");
  27. #include <errno.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "archive.h"
  31. /*
  32. * This is a little tricky. I used to allow the
  33. * compression handling layer to fork the compressor,
  34. * which means this write function gets invoked in
  35. * a separate process. That would, of course, make it impossible
  36. * to actually use the data stored into memory here.
  37. * Fortunately, none of the compressors fork today and
  38. * I'm reluctant to use that route in the future but, if
  39. * forking compressors ever do reappear, this will have
  40. * to get a lot more complicated.
  41. */
  42. struct write_memory_data {
  43. size_t used;
  44. size_t size;
  45. size_t * client_size;
  46. unsigned char * buff;
  47. };
  48. static int memory_write_close(struct archive *, void *);
  49. static int memory_write_open(struct archive *, void *);
  50. static ssize_t memory_write(struct archive *, void *, const void *buff, size_t);
  51. /*
  52. * Client provides a pointer to a block of memory to receive
  53. * the data. The 'size' param both tells us the size of the
  54. * client buffer and lets us tell the client the final size.
  55. */
  56. int
  57. archive_write_open_memory(struct archive *a, void *buff, size_t buffSize, size_t *used)
  58. {
  59. struct write_memory_data *mine;
  60. mine = (struct write_memory_data *)malloc(sizeof(*mine));
  61. if (mine == NULL) {
  62. archive_set_error(a, ENOMEM, "No memory");
  63. return (ARCHIVE_FATAL);
  64. }
  65. memset(mine, 0, sizeof(*mine));
  66. mine->buff = buff;
  67. mine->size = buffSize;
  68. mine->client_size = used;
  69. return (archive_write_open(a, mine,
  70. memory_write_open, memory_write, memory_write_close));
  71. }
  72. static int
  73. memory_write_open(struct archive *a, void *client_data)
  74. {
  75. struct write_memory_data *mine;
  76. mine = client_data;
  77. mine->used = 0;
  78. if (mine->client_size != NULL)
  79. *mine->client_size = mine->used;
  80. /* Disable padding if it hasn't been set explicitly. */
  81. if (-1 == archive_write_get_bytes_in_last_block(a))
  82. archive_write_set_bytes_in_last_block(a, 1);
  83. return (ARCHIVE_OK);
  84. }
  85. /*
  86. * Copy the data into the client buffer.
  87. * Note that we update mine->client_size on every write.
  88. * In particular, this means the client can follow exactly
  89. * how much has been written into their buffer at any time.
  90. */
  91. static ssize_t
  92. memory_write(struct archive *a, void *client_data, const void *buff, size_t length)
  93. {
  94. struct write_memory_data *mine;
  95. mine = client_data;
  96. if (mine->used + length > mine->size) {
  97. archive_set_error(a, ENOMEM, "Buffer exhausted");
  98. return (ARCHIVE_FATAL);
  99. }
  100. memcpy(mine->buff + mine->used, buff, length);
  101. mine->used += length;
  102. if (mine->client_size != NULL)
  103. *mine->client_size = mine->used;
  104. return (length);
  105. }
  106. static int
  107. memory_write_close(struct archive *a, void *client_data)
  108. {
  109. struct write_memory_data *mine;
  110. (void)a; /* UNUSED */
  111. mine = client_data;
  112. free(mine);
  113. return (ARCHIVE_OK);
  114. }