archive_write_set_compression_none.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_set_compression_none.c,v 1.16 2007/12/30 04:58:22 kientzle Exp $");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #ifdef HAVE_STRING_H
  34. #include <string.h>
  35. #endif
  36. #include "archive.h"
  37. #include "archive_private.h"
  38. #include "archive_write_private.h"
  39. static int archive_compressor_none_finish(struct archive_write *a);
  40. static int archive_compressor_none_init(struct archive_write *);
  41. static int archive_compressor_none_write(struct archive_write *,
  42. const void *, size_t);
  43. struct archive_none {
  44. char *buffer;
  45. ssize_t buffer_size;
  46. char *next; /* Current insert location */
  47. ssize_t avail; /* Free space left in buffer */
  48. };
  49. int
  50. archive_write_set_compression_none(struct archive *_a)
  51. {
  52. struct archive_write *a = (struct archive_write *)_a;
  53. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  54. ARCHIVE_STATE_NEW, "archive_write_set_compression_none");
  55. a->compressor.init = &archive_compressor_none_init;
  56. return (0);
  57. }
  58. /*
  59. * Setup callback.
  60. */
  61. static int
  62. archive_compressor_none_init(struct archive_write *a)
  63. {
  64. int ret;
  65. struct archive_none *state;
  66. a->archive.compression_code = ARCHIVE_COMPRESSION_NONE;
  67. a->archive.compression_name = "none";
  68. if (a->client_opener != NULL) {
  69. ret = (a->client_opener)(&a->archive, a->client_data);
  70. if (ret != 0)
  71. return (ret);
  72. }
  73. state = (struct archive_none *)malloc(sizeof(*state));
  74. if (state == NULL) {
  75. archive_set_error(&a->archive, ENOMEM,
  76. "Can't allocate data for output buffering");
  77. return (ARCHIVE_FATAL);
  78. }
  79. memset(state, 0, sizeof(*state));
  80. state->buffer_size = a->bytes_per_block;
  81. if (state->buffer_size != 0) {
  82. state->buffer = (char *)malloc(state->buffer_size);
  83. if (state->buffer == NULL) {
  84. archive_set_error(&a->archive, ENOMEM,
  85. "Can't allocate output buffer");
  86. free(state);
  87. return (ARCHIVE_FATAL);
  88. }
  89. }
  90. state->next = state->buffer;
  91. state->avail = state->buffer_size;
  92. a->compressor.data = state;
  93. a->compressor.write = archive_compressor_none_write;
  94. a->compressor.finish = archive_compressor_none_finish;
  95. return (ARCHIVE_OK);
  96. }
  97. /*
  98. * Write data to the stream.
  99. */
  100. static int
  101. archive_compressor_none_write(struct archive_write *a, const void *vbuff,
  102. size_t length)
  103. {
  104. const char *buff;
  105. ssize_t remaining, to_copy;
  106. ssize_t bytes_written;
  107. struct archive_none *state;
  108. state = (struct archive_none *)a->compressor.data;
  109. buff = (const char *)vbuff;
  110. if (a->client_writer == NULL) {
  111. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  112. "No write callback is registered? "
  113. "This is probably an internal programming error.");
  114. return (ARCHIVE_FATAL);
  115. }
  116. remaining = length;
  117. /*
  118. * If there is no buffer for blocking, just pass the data
  119. * straight through to the client write callback. In
  120. * particular, this supports "no write delay" operation for
  121. * special applications. Just set the block size to zero.
  122. */
  123. if (state->buffer_size == 0) {
  124. while (remaining > 0) {
  125. bytes_written = (a->client_writer)(&a->archive,
  126. a->client_data, buff, remaining);
  127. if (bytes_written <= 0)
  128. return (ARCHIVE_FATAL);
  129. a->archive.raw_position += bytes_written;
  130. remaining -= bytes_written;
  131. buff += bytes_written;
  132. }
  133. a->archive.file_position += length;
  134. return (ARCHIVE_OK);
  135. }
  136. /* If the copy buffer isn't empty, try to fill it. */
  137. if (state->avail < state->buffer_size) {
  138. /* If buffer is not empty... */
  139. /* ... copy data into buffer ... */
  140. to_copy = (remaining > state->avail) ?
  141. state->avail : remaining;
  142. memcpy(state->next, buff, to_copy);
  143. state->next += to_copy;
  144. state->avail -= to_copy;
  145. buff += to_copy;
  146. remaining -= to_copy;
  147. /* ... if it's full, write it out. */
  148. if (state->avail == 0) {
  149. bytes_written = (a->client_writer)(&a->archive,
  150. a->client_data, state->buffer, state->buffer_size);
  151. if (bytes_written <= 0)
  152. return (ARCHIVE_FATAL);
  153. /* XXX TODO: if bytes_written < state->buffer_size */
  154. a->archive.raw_position += bytes_written;
  155. state->next = state->buffer;
  156. state->avail = state->buffer_size;
  157. }
  158. }
  159. while (remaining > state->buffer_size) {
  160. /* Write out full blocks directly to client. */
  161. bytes_written = (a->client_writer)(&a->archive,
  162. a->client_data, buff, state->buffer_size);
  163. if (bytes_written <= 0)
  164. return (ARCHIVE_FATAL);
  165. a->archive.raw_position += bytes_written;
  166. buff += bytes_written;
  167. remaining -= bytes_written;
  168. }
  169. if (remaining > 0) {
  170. /* Copy last bit into copy buffer. */
  171. memcpy(state->next, buff, remaining);
  172. state->next += remaining;
  173. state->avail -= remaining;
  174. }
  175. a->archive.file_position += length;
  176. return (ARCHIVE_OK);
  177. }
  178. /*
  179. * Finish the compression.
  180. */
  181. static int
  182. archive_compressor_none_finish(struct archive_write *a)
  183. {
  184. ssize_t block_length;
  185. ssize_t target_block_length;
  186. ssize_t bytes_written;
  187. int ret;
  188. struct archive_none *state;
  189. state = (struct archive_none *)a->compressor.data;
  190. ret = ARCHIVE_OK;
  191. if (a->client_writer == NULL) {
  192. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  193. "No write callback is registered? "
  194. "This is probably an internal programming error.");
  195. return (ARCHIVE_FATAL);
  196. }
  197. /* If there's pending data, pad and write the last block */
  198. if (state->next != state->buffer) {
  199. block_length = state->buffer_size - state->avail;
  200. /* Tricky calculation to determine size of last block */
  201. if (a->bytes_in_last_block <= 0)
  202. /* Default or Zero: pad to full block */
  203. target_block_length = a->bytes_per_block;
  204. else
  205. /* Round to next multiple of bytes_in_last_block. */
  206. target_block_length = a->bytes_in_last_block *
  207. ( (block_length + a->bytes_in_last_block - 1) /
  208. a->bytes_in_last_block);
  209. if (target_block_length > a->bytes_per_block)
  210. target_block_length = a->bytes_per_block;
  211. if (block_length < target_block_length) {
  212. memset(state->next, 0,
  213. target_block_length - block_length);
  214. block_length = target_block_length;
  215. }
  216. bytes_written = (a->client_writer)(&a->archive,
  217. a->client_data, state->buffer, block_length);
  218. if (bytes_written <= 0)
  219. ret = ARCHIVE_FATAL;
  220. else {
  221. a->archive.raw_position += bytes_written;
  222. ret = ARCHIVE_OK;
  223. }
  224. }
  225. if (state->buffer)
  226. free(state->buffer);
  227. free(state);
  228. a->compressor.data = NULL;
  229. return (ret);
  230. }