archive_read_open_memory.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. unsigned char *buffer;
  40. 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. #if ARCHIVE_API_VERSION < 2
  46. static ssize_t memory_read_skip(struct archive *, void *, size_t request);
  47. #else
  48. static off_t memory_read_skip(struct archive *, void *, off_t request);
  49. #endif
  50. static ssize_t memory_read(struct archive *, void *, const void **buff);
  51. int
  52. archive_read_open_memory(struct archive *a, void *buff, size_t size)
  53. {
  54. return archive_read_open_memory2(a, buff, size, size);
  55. }
  56. /*
  57. * Don't use _open_memory2() in production code; the archive_read_open_memory()
  58. * version is the one you really want. This is just here so that
  59. * test harnesses can exercise block operations inside the library.
  60. */
  61. int
  62. archive_read_open_memory2(struct archive *a, void *buff,
  63. size_t size, size_t read_size)
  64. {
  65. struct read_memory_data *mine;
  66. mine = (struct read_memory_data *)malloc(sizeof(*mine));
  67. if (mine == NULL) {
  68. archive_set_error(a, ENOMEM, "No memory");
  69. return (ARCHIVE_FATAL);
  70. }
  71. memset(mine, 0, sizeof(*mine));
  72. mine->buffer = (unsigned char *)buff;
  73. mine->end = mine->buffer + size;
  74. mine->read_size = read_size;
  75. return (archive_read_open2(a, mine, memory_read_open,
  76. memory_read, memory_read_skip, memory_read_close));
  77. }
  78. /*
  79. * There's nothing to open.
  80. */
  81. static int
  82. memory_read_open(struct archive *a, void *client_data)
  83. {
  84. (void)a; /* UNUSED */
  85. (void)client_data; /* UNUSED */
  86. return (ARCHIVE_OK);
  87. }
  88. /*
  89. * This is scary simple: Just advance a pointer. Limiting
  90. * to read_size is not technically necessary, but it exercises
  91. * more of the internal logic when used with a small block size
  92. * in a test harness. Production use should not specify a block
  93. * size; then this is much faster.
  94. */
  95. static ssize_t
  96. memory_read(struct archive *a, void *client_data, const void **buff)
  97. {
  98. struct read_memory_data *mine = (struct read_memory_data *)client_data;
  99. ssize_t size;
  100. (void)a; /* UNUSED */
  101. *buff = mine->buffer;
  102. size = mine->end - mine->buffer;
  103. if (size > mine->read_size)
  104. size = mine->read_size;
  105. mine->buffer += size;
  106. return (size);
  107. }
  108. /*
  109. * Advancing is just as simple. Again, this is doing more than
  110. * necessary in order to better exercise internal code when used
  111. * as a test harness.
  112. */
  113. #if ARCHIVE_API_VERSION < 2
  114. static ssize_t
  115. memory_read_skip(struct archive *a, void *client_data, size_t skip)
  116. #else
  117. static off_t
  118. memory_read_skip(struct archive *a, void *client_data, off_t skip)
  119. #endif
  120. {
  121. struct read_memory_data *mine = (struct read_memory_data *)client_data;
  122. (void)a; /* UNUSED */
  123. if ((off_t)skip > (off_t)(mine->end - mine->buffer))
  124. skip = mine->end - mine->buffer;
  125. /* Round down to block size. */
  126. skip /= mine->read_size;
  127. skip *= mine->read_size;
  128. mine->buffer += skip;
  129. return (skip);
  130. }
  131. /*
  132. * Close is just cleaning up our one small bit of data.
  133. */
  134. static int
  135. memory_read_close(struct archive *a, void *client_data)
  136. {
  137. struct read_memory_data *mine = (struct read_memory_data *)client_data;
  138. (void)a; /* UNUSED */
  139. free(mine);
  140. return (ARCHIVE_OK);
  141. }