1
0

archive_write_open_memory.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. struct write_memory_data {
  32. size_t used;
  33. size_t size;
  34. size_t * client_size;
  35. unsigned char * buff;
  36. };
  37. static int memory_write_close(struct archive *, void *);
  38. static int memory_write_open(struct archive *, void *);
  39. static ssize_t memory_write(struct archive *, void *, const void *buff, size_t);
  40. /*
  41. * Client provides a pointer to a block of memory to receive
  42. * the data. The 'size' param both tells us the size of the
  43. * client buffer and lets us tell the client the final size.
  44. */
  45. int
  46. archive_write_open_memory(struct archive *a, void *buff, size_t buffSize, size_t *used)
  47. {
  48. struct write_memory_data *mine;
  49. mine = (struct write_memory_data *)malloc(sizeof(*mine));
  50. if (mine == NULL) {
  51. archive_set_error(a, ENOMEM, "No memory");
  52. return (ARCHIVE_FATAL);
  53. }
  54. memset(mine, 0, sizeof(*mine));
  55. mine->buff = buff;
  56. mine->size = buffSize;
  57. mine->client_size = used;
  58. return (archive_write_open(a, mine,
  59. memory_write_open, memory_write, memory_write_close));
  60. }
  61. static int
  62. memory_write_open(struct archive *a, void *client_data)
  63. {
  64. struct write_memory_data *mine;
  65. mine = client_data;
  66. mine->used = 0;
  67. if (mine->client_size != NULL)
  68. *mine->client_size = mine->used;
  69. /* Disable padding if it hasn't been set explicitly. */
  70. if (-1 == archive_write_get_bytes_in_last_block(a))
  71. archive_write_set_bytes_in_last_block(a, 1);
  72. return (ARCHIVE_OK);
  73. }
  74. /*
  75. * Copy the data into the client buffer.
  76. * Note that we update mine->client_size on every write.
  77. * In particular, this means the client can follow exactly
  78. * how much has been written into their buffer at any time.
  79. */
  80. static ssize_t
  81. memory_write(struct archive *a, void *client_data, const void *buff, size_t length)
  82. {
  83. struct write_memory_data *mine;
  84. mine = client_data;
  85. if (mine->used + length > mine->size) {
  86. archive_set_error(a, ENOMEM, "Buffer exhausted");
  87. return (ARCHIVE_FATAL);
  88. }
  89. memcpy(mine->buff + mine->used, buff, length);
  90. mine->used += length;
  91. if (mine->client_size != NULL)
  92. *mine->client_size = mine->used;
  93. return (length);
  94. }
  95. static int
  96. memory_write_close(struct archive *a, void *client_data)
  97. {
  98. struct write_memory_data *mine;
  99. (void)a; /* UNUSED */
  100. mine = client_data;
  101. free(mine);
  102. return (ARCHIVE_OK);
  103. }