zip_buffer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. zip_buffer.c -- bounds checked access to memory buffer
  3. Copyright (C) 2014 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <libzip@nih.at>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "zipint.h"
  33. zip_uint8_t *
  34. _zip_buffer_data(zip_buffer_t *buffer)
  35. {
  36. return buffer->data;
  37. }
  38. void
  39. _zip_buffer_free(zip_buffer_t *buffer)
  40. {
  41. if (buffer == NULL) {
  42. return;
  43. }
  44. if (buffer->free_data) {
  45. free(buffer->data);
  46. }
  47. free(buffer);
  48. }
  49. bool
  50. _zip_buffer_eof(zip_buffer_t *buffer)
  51. {
  52. return buffer->ok && buffer->offset == buffer->size;
  53. }
  54. zip_uint8_t *
  55. _zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length)
  56. {
  57. zip_uint8_t *data;
  58. if (!buffer->ok || buffer->offset + length < length || buffer->offset + length > buffer->size) {
  59. buffer->ok = false;
  60. return NULL;
  61. }
  62. data = buffer->data + buffer->offset;
  63. buffer->offset += length;
  64. return data;
  65. }
  66. zip_uint16_t
  67. _zip_buffer_get_16(zip_buffer_t *buffer)
  68. {
  69. zip_uint8_t *data = _zip_buffer_get(buffer, 2);
  70. if (data == NULL) {
  71. return 0;
  72. }
  73. return (zip_uint16_t)(data[0] + (data[1] << 8));
  74. }
  75. zip_uint32_t
  76. _zip_buffer_get_32(zip_buffer_t *buffer)
  77. {
  78. zip_uint8_t *data = _zip_buffer_get(buffer, 4);
  79. if (data == NULL) {
  80. return 0;
  81. }
  82. return ((((((zip_uint32_t)data[3] << 8) + data[2]) << 8) + data[1]) << 8) + data[0];
  83. }
  84. zip_uint64_t
  85. _zip_buffer_get_64(zip_buffer_t *buffer)
  86. {
  87. zip_uint8_t *data = _zip_buffer_get(buffer, 8);
  88. if (data == NULL) {
  89. return 0;
  90. }
  91. return ((zip_uint64_t)data[7] << 56) + ((zip_uint64_t)data[6] << 48) + ((zip_uint64_t)data[5] << 40) + ((zip_uint64_t)data[4] << 32) + ((zip_uint64_t)data[3] << 24) + ((zip_uint64_t)data[2] << 16) + ((zip_uint64_t)data[1] << 8) + (zip_uint64_t)data[0];
  92. }
  93. zip_uint8_t
  94. _zip_buffer_get_8(zip_buffer_t *buffer)
  95. {
  96. zip_uint8_t *data = _zip_buffer_get(buffer, 1);
  97. if (data == NULL) {
  98. return 0;
  99. }
  100. return data[0];
  101. }
  102. zip_uint64_t
  103. _zip_buffer_left(zip_buffer_t *buffer)
  104. {
  105. return buffer->ok ? buffer->size - buffer->offset : 0;
  106. }
  107. zip_buffer_t *
  108. _zip_buffer_new(zip_uint8_t *data, zip_uint64_t size)
  109. {
  110. bool free_data = (data == NULL);
  111. zip_buffer_t *buffer;
  112. if (data == NULL) {
  113. if ((data = (zip_uint8_t *)malloc(size)) == NULL) {
  114. return NULL;
  115. }
  116. }
  117. if ((buffer = (zip_buffer_t *)malloc(sizeof(*buffer))) == NULL) {
  118. if (free_data) {
  119. free(data);
  120. }
  121. return NULL;
  122. }
  123. buffer->ok = true;
  124. buffer->data = data;
  125. buffer->size = size;
  126. buffer->offset = 0;
  127. buffer->free_data = free_data;
  128. return buffer;
  129. }
  130. zip_buffer_t *
  131. _zip_buffer_new_from_source(zip_source_t *src, zip_uint64_t size, zip_uint8_t *buf, zip_error_t *error)
  132. {
  133. zip_buffer_t *buffer;
  134. if ((buffer = _zip_buffer_new(buf, size)) == NULL) {
  135. zip_error_set(error, ZIP_ER_MEMORY, 0);
  136. return NULL;
  137. }
  138. if (_zip_read(src, buffer->data, size, error) < 0) {
  139. _zip_buffer_free(buffer);
  140. return NULL;
  141. }
  142. return buffer;
  143. }
  144. zip_uint64_t
  145. _zip_buffer_offset(zip_buffer_t *buffer)
  146. {
  147. return buffer->ok ? buffer->offset : 0;
  148. }
  149. bool
  150. _zip_buffer_ok(zip_buffer_t *buffer)
  151. {
  152. return buffer->ok;
  153. }
  154. int
  155. _zip_buffer_put(zip_buffer_t *buffer, const void *src, size_t length)
  156. {
  157. zip_uint8_t *dst = _zip_buffer_get(buffer, length);
  158. if (dst == NULL) {
  159. return -1;
  160. }
  161. memcpy(dst, src, length);
  162. return 0;
  163. }
  164. int
  165. _zip_buffer_put_16(zip_buffer_t *buffer, zip_uint16_t i)
  166. {
  167. zip_uint8_t *data = _zip_buffer_get(buffer, 2);
  168. if (data == NULL) {
  169. return -1;
  170. }
  171. data[0] = (zip_uint8_t)(i & 0xff);
  172. data[1] = (zip_uint8_t)((i >> 8) & 0xff);
  173. return 0;
  174. }
  175. int
  176. _zip_buffer_put_32(zip_buffer_t *buffer, zip_uint32_t i)
  177. {
  178. zip_uint8_t *data = _zip_buffer_get(buffer, 4);
  179. if (data == NULL) {
  180. return -1;
  181. }
  182. data[0] = (zip_uint8_t)(i & 0xff);
  183. data[1] = (zip_uint8_t)((i >> 8) & 0xff);
  184. data[2] = (zip_uint8_t)((i >> 16) & 0xff);
  185. data[3] = (zip_uint8_t)((i >> 24) & 0xff);
  186. return 0;
  187. }
  188. int
  189. _zip_buffer_put_64(zip_buffer_t *buffer, zip_uint64_t i)
  190. {
  191. zip_uint8_t *data = _zip_buffer_get(buffer, 8);
  192. if (data == NULL) {
  193. return -1;
  194. }
  195. data[0] = (zip_uint8_t)(i & 0xff);
  196. data[1] = (zip_uint8_t)((i >> 8) & 0xff);
  197. data[2] = (zip_uint8_t)((i >> 16) & 0xff);
  198. data[3] = (zip_uint8_t)((i >> 24) & 0xff);
  199. data[4] = (zip_uint8_t)((i >> 32) & 0xff);
  200. data[5] = (zip_uint8_t)((i >> 40) & 0xff);
  201. data[6] = (zip_uint8_t)((i >> 48) & 0xff);
  202. data[7] = (zip_uint8_t)((i >> 56) & 0xff);
  203. return 0;
  204. }
  205. int
  206. _zip_buffer_put_8(zip_buffer_t *buffer, zip_uint8_t i)
  207. {
  208. zip_uint8_t *data = _zip_buffer_get(buffer, 1);
  209. if (data == NULL) {
  210. return -1;
  211. }
  212. data[0] = i;
  213. return 0;
  214. }
  215. int
  216. _zip_buffer_set_offset(zip_buffer_t *buffer, zip_uint64_t offset)
  217. {
  218. if (offset > buffer->size) {
  219. buffer->ok = false;
  220. return -1;
  221. }
  222. buffer->ok = true;
  223. buffer->offset = offset;
  224. return 0;
  225. }
  226. int
  227. _zip_buffer_skip(zip_buffer_t *buffer, zip_uint64_t length) {
  228. zip_uint64_t offset = buffer->offset + length;
  229. if (offset < buffer->offset) {
  230. buffer->ok = false;
  231. return -1;
  232. }
  233. return _zip_buffer_set_offset(buffer, offset);
  234. }
  235. zip_uint64_t
  236. _zip_buffer_size(zip_buffer_t *buffer)
  237. {
  238. return buffer->size;
  239. }