archive_read_open_memory.c 5.4 KB

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