archive_read_open_memory.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_read_open_memory.c,v 1.6 2007/07/06 15:51:59 kientzle Exp $");
  27. #include <errno.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "archive.h"
  31. /*
  32. * Glue to read an archive from a block of memory.
  33. *
  34. * This is mostly a huge help in building test harnesses;
  35. * test programs can build archives in memory and read them
  36. * back again without having to mess with files on disk.
  37. */
  38. struct read_memory_data {
  39. const unsigned char *start;
  40. const unsigned char *p;
  41. const unsigned char *end;
  42. ssize_t read_size;
  43. };
  44. static int memory_read_close(struct archive *, void *);
  45. static int memory_read_open(struct archive *, void *);
  46. static int64_t memory_read_seek(struct archive *, void *, int64_t offset, int whence);
  47. static int64_t memory_read_skip(struct archive *, void *, int64_t request);
  48. static ssize_t memory_read(struct archive *, void *, const void **buff);
  49. int
  50. archive_read_open_memory(struct archive *a, const void *buff, size_t size)
  51. {
  52. return archive_read_open_memory2(a, buff, size, size);
  53. }
  54. /*
  55. * Don't use _open_memory2() in production code; the archive_read_open_memory()
  56. * version is the one you really want. This is just here so that
  57. * test harnesses can exercise block operations inside the library.
  58. */
  59. int
  60. archive_read_open_memory2(struct archive *a, const void *buff,
  61. size_t size, size_t read_size)
  62. {
  63. struct read_memory_data *mine;
  64. mine = (struct read_memory_data *)calloc(1, sizeof(*mine));
  65. if (mine == NULL) {
  66. archive_set_error(a, ENOMEM, "No memory");
  67. return (ARCHIVE_FATAL);
  68. }
  69. mine->start = mine->p = (const unsigned char *)buff;
  70. mine->end = mine->start + size;
  71. mine->read_size = read_size;
  72. archive_read_set_open_callback(a, memory_read_open);
  73. archive_read_set_read_callback(a, memory_read);
  74. archive_read_set_seek_callback(a, memory_read_seek);
  75. archive_read_set_skip_callback(a, memory_read_skip);
  76. archive_read_set_close_callback(a, memory_read_close);
  77. archive_read_set_callback_data(a, mine);
  78. return (archive_read_open1(a));
  79. }
  80. /*
  81. * There's nothing to open.
  82. */
  83. static int
  84. memory_read_open(struct archive *a, void *client_data)
  85. {
  86. (void)a; /* UNUSED */
  87. (void)client_data; /* UNUSED */
  88. return (ARCHIVE_OK);
  89. }
  90. /*
  91. * This is scary simple: Just advance a pointer. Limiting
  92. * to read_size is not technically necessary, but it exercises
  93. * more of the internal logic when used with a small block size
  94. * in a test harness. Production use should not specify a block
  95. * size; then this is much faster.
  96. */
  97. static ssize_t
  98. memory_read(struct archive *a, void *client_data, const void **buff)
  99. {
  100. struct read_memory_data *mine = (struct read_memory_data *)client_data;
  101. ssize_t size;
  102. (void)a; /* UNUSED */
  103. *buff = mine->p;
  104. size = mine->end - mine->p;
  105. if (size > mine->read_size)
  106. size = mine->read_size;
  107. mine->p += size;
  108. return (size);
  109. }
  110. /*
  111. * Advancing is just as simple. Again, this is doing more than
  112. * necessary in order to better exercise internal code when used
  113. * as a test harness.
  114. */
  115. static int64_t
  116. memory_read_skip(struct archive *a, void *client_data, int64_t skip)
  117. {
  118. struct read_memory_data *mine = (struct read_memory_data *)client_data;
  119. (void)a; /* UNUSED */
  120. if ((int64_t)skip > (int64_t)(mine->end - mine->p))
  121. skip = mine->end - mine->p;
  122. /* Round down to block size. */
  123. skip /= mine->read_size;
  124. skip *= mine->read_size;
  125. mine->p += skip;
  126. return (skip);
  127. }
  128. /*
  129. * Seeking.
  130. */
  131. static int64_t
  132. memory_read_seek(struct archive *a, void *client_data, int64_t offset, int whence)
  133. {
  134. struct read_memory_data *mine = (struct read_memory_data *)client_data;
  135. (void)a; /* UNUSED */
  136. switch (whence) {
  137. case SEEK_SET:
  138. mine->p = mine->start + offset;
  139. break;
  140. case SEEK_CUR:
  141. mine->p += offset;
  142. break;
  143. case SEEK_END:
  144. mine->p = mine->end + offset;
  145. break;
  146. default:
  147. return ARCHIVE_FATAL;
  148. }
  149. if (mine->p < mine->start) {
  150. mine->p = mine->start;
  151. return ARCHIVE_FAILED;
  152. }
  153. if (mine->p > mine->end) {
  154. mine->p = mine->end;
  155. return ARCHIVE_FAILED;
  156. }
  157. return (mine->p - mine->start);
  158. }
  159. /*
  160. * Close is just cleaning up our one small bit of data.
  161. */
  162. static int
  163. memory_read_close(struct archive *a, void *client_data)
  164. {
  165. struct read_memory_data *mine = (struct read_memory_data *)client_data;
  166. (void)a; /* UNUSED */
  167. free(mine);
  168. return (ARCHIVE_OK);
  169. }