zip_extra_field.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. zip_extra_field.c -- manipulate extra fields
  3. Copyright (C) 2012-2015 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_extra_field_t *
  34. _zip_ef_clone(const zip_extra_field_t *ef, zip_error_t *error)
  35. {
  36. zip_extra_field_t *head, *prev, *def;
  37. head = prev = NULL;
  38. while (ef) {
  39. if ((def=_zip_ef_new(ef->id, ef->size, ef->data, ef->flags)) == NULL) {
  40. zip_error_set(error, ZIP_ER_MEMORY, 0);
  41. _zip_ef_free(head);
  42. return NULL;
  43. }
  44. if (head == NULL)
  45. head = def;
  46. if (prev)
  47. prev->next = def;
  48. prev = def;
  49. ef = ef->next;
  50. }
  51. return head;
  52. }
  53. zip_extra_field_t *
  54. _zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags)
  55. {
  56. zip_extra_field_t *head, *prev;
  57. int i;
  58. i = 0;
  59. head = ef;
  60. prev = NULL;
  61. for (; ef; ef=(prev ? prev->next : head)) {
  62. if ((ef->flags & flags & ZIP_EF_BOTH) && ((ef->id == id) || (id == ZIP_EXTRA_FIELD_ALL))) {
  63. if (id_idx == ZIP_EXTRA_FIELD_ALL || i == id_idx) {
  64. ef->flags &= ~(flags & ZIP_EF_BOTH);
  65. if ((ef->flags & ZIP_EF_BOTH) == 0) {
  66. if (prev)
  67. prev->next = ef->next;
  68. else
  69. head = ef->next;
  70. ef->next = NULL;
  71. _zip_ef_free(ef);
  72. if (id_idx == ZIP_EXTRA_FIELD_ALL)
  73. continue;
  74. }
  75. }
  76. i++;
  77. if (i > id_idx)
  78. break;
  79. }
  80. prev = ef;
  81. }
  82. return head;
  83. }
  84. void
  85. _zip_ef_free(zip_extra_field_t *ef)
  86. {
  87. zip_extra_field_t *ef2;
  88. while (ef) {
  89. ef2 = ef->next;
  90. free(ef->data);
  91. free(ef);
  92. ef = ef2;
  93. }
  94. }
  95. const zip_uint8_t *
  96. _zip_ef_get_by_id(const zip_extra_field_t *ef, zip_uint16_t *lenp, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags, zip_error_t *error)
  97. {
  98. static const zip_uint8_t empty[1] = { '\0' };
  99. int i;
  100. i = 0;
  101. for (; ef; ef=ef->next) {
  102. if (ef->id == id && (ef->flags & flags & ZIP_EF_BOTH)) {
  103. if (i < id_idx) {
  104. i++;
  105. continue;
  106. }
  107. if (lenp)
  108. *lenp = ef->size;
  109. if (ef->size > 0)
  110. return ef->data;
  111. else
  112. return empty;
  113. }
  114. }
  115. zip_error_set(error, ZIP_ER_NOENT, 0);
  116. return NULL;
  117. }
  118. zip_extra_field_t *
  119. _zip_ef_merge(zip_extra_field_t *to, zip_extra_field_t *from)
  120. {
  121. zip_extra_field_t *ef2, *tt, *tail;
  122. int duplicate;
  123. if (to == NULL)
  124. return from;
  125. for (tail=to; tail->next; tail=tail->next)
  126. ;
  127. for (; from; from=ef2) {
  128. ef2 = from->next;
  129. duplicate = 0;
  130. for (tt=to; tt; tt=tt->next) {
  131. if (tt->id == from->id && tt->size == from->size && memcmp(tt->data, from->data, tt->size) == 0) {
  132. tt->flags |= (from->flags & ZIP_EF_BOTH);
  133. duplicate = 1;
  134. break;
  135. }
  136. }
  137. from->next = NULL;
  138. if (duplicate)
  139. _zip_ef_free(from);
  140. else
  141. tail = tail->next = from;
  142. }
  143. return to;
  144. }
  145. zip_extra_field_t *
  146. _zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags)
  147. {
  148. zip_extra_field_t *ef;
  149. if ((ef=(zip_extra_field_t *)malloc(sizeof(*ef))) == NULL)
  150. return NULL;
  151. ef->next = NULL;
  152. ef->flags = flags;
  153. ef->id = id;
  154. ef->size = size;
  155. if (size > 0) {
  156. if ((ef->data=(zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) {
  157. free(ef);
  158. return NULL;
  159. }
  160. }
  161. else
  162. ef->data = NULL;
  163. return ef;
  164. }
  165. bool
  166. _zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, zip_extra_field_t **ef_head_p, zip_error_t *error)
  167. {
  168. zip_buffer_t *buffer;
  169. zip_extra_field_t *ef, *ef2, *ef_head;
  170. if ((buffer = _zip_buffer_new((zip_uint8_t *)data, len)) == NULL) {
  171. zip_error_set(error, ZIP_ER_MEMORY, 0);
  172. return false;
  173. }
  174. ef_head = ef = NULL;
  175. while (_zip_buffer_ok(buffer) && _zip_buffer_left(buffer) >= 4) {
  176. zip_uint16_t fid, flen;
  177. zip_uint8_t *ef_data;
  178. fid = _zip_buffer_get_16(buffer);
  179. flen = _zip_buffer_get_16(buffer);
  180. ef_data = _zip_buffer_get(buffer, flen);
  181. if (ef_data == NULL) {
  182. zip_error_set(error, ZIP_ER_INCONS, 0);
  183. _zip_buffer_free(buffer);
  184. _zip_ef_free(ef_head);
  185. return false;
  186. }
  187. if ((ef2=_zip_ef_new(fid, flen, ef_data, flags)) == NULL) {
  188. zip_error_set(error, ZIP_ER_MEMORY, 0);
  189. _zip_buffer_free(buffer);
  190. _zip_ef_free(ef_head);
  191. return false;
  192. }
  193. if (ef_head) {
  194. ef->next = ef2;
  195. ef = ef2;
  196. }
  197. else
  198. ef_head = ef = ef2;
  199. }
  200. if (!_zip_buffer_eof(buffer)) {
  201. /* Android APK files align stored file data with padding in extra fields; ignore. */
  202. /* see https://android.googlesource.com/platform/build/+/master/tools/zipalign/ZipAlign.cpp */
  203. size_t glen = _zip_buffer_left(buffer);
  204. zip_uint8_t *garbage;
  205. garbage = _zip_buffer_get(buffer, glen);
  206. if (glen >= 4 || garbage == NULL || memcmp(garbage, "\0\0\0", glen) != 0) {
  207. zip_error_set(error, ZIP_ER_INCONS, 0);
  208. _zip_buffer_free(buffer);
  209. _zip_ef_free(ef_head);
  210. return false;
  211. }
  212. }
  213. _zip_buffer_free(buffer);
  214. if (ef_head_p) {
  215. *ef_head_p = ef_head;
  216. }
  217. else {
  218. _zip_ef_free(ef_head);
  219. }
  220. return true;
  221. }
  222. zip_extra_field_t *
  223. _zip_ef_remove_internal(zip_extra_field_t *ef)
  224. {
  225. zip_extra_field_t *ef_head;
  226. zip_extra_field_t *prev, *next;
  227. ef_head = ef;
  228. prev = NULL;
  229. while (ef) {
  230. if (ZIP_EF_IS_INTERNAL(ef->id)) {
  231. next = ef->next;
  232. if (ef_head == ef)
  233. ef_head = next;
  234. ef->next = NULL;
  235. _zip_ef_free(ef);
  236. if (prev)
  237. prev->next = next;
  238. ef = next;
  239. }
  240. else {
  241. prev = ef;
  242. ef = ef->next;
  243. }
  244. }
  245. return ef_head;
  246. }
  247. zip_uint16_t
  248. _zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags)
  249. {
  250. zip_uint16_t size;
  251. size = 0;
  252. for (; ef; ef=ef->next) {
  253. if (ef->flags & flags & ZIP_EF_BOTH)
  254. size = (zip_uint16_t)(size+4+ef->size);
  255. }
  256. return size;
  257. }
  258. int
  259. _zip_ef_write(zip_t *za, const zip_extra_field_t *ef, zip_flags_t flags)
  260. {
  261. zip_uint8_t b[4];
  262. zip_buffer_t *buffer = _zip_buffer_new(b, sizeof(b));
  263. if (buffer == NULL) {
  264. return -1;
  265. }
  266. for (; ef; ef=ef->next) {
  267. if (ef->flags & flags & ZIP_EF_BOTH) {
  268. _zip_buffer_set_offset(buffer, 0);
  269. _zip_buffer_put_16(buffer, ef->id);
  270. _zip_buffer_put_16(buffer, ef->size);
  271. if (!_zip_buffer_ok(buffer)) {
  272. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  273. _zip_buffer_free(buffer);
  274. return -1;
  275. }
  276. if (_zip_write(za, b, 4) < 0) {
  277. _zip_buffer_free(buffer);
  278. return -1;
  279. }
  280. if (ef->size > 0) {
  281. if (_zip_write(za, ef->data, ef->size) < 0) {
  282. _zip_buffer_free(buffer);
  283. return -1;
  284. }
  285. }
  286. }
  287. }
  288. _zip_buffer_free(buffer);
  289. return 0;
  290. }
  291. int
  292. _zip_read_local_ef(zip_t *za, zip_uint64_t idx)
  293. {
  294. zip_entry_t *e;
  295. unsigned char b[4];
  296. zip_buffer_t *buffer;
  297. zip_uint16_t fname_len, ef_len;
  298. if (idx >= za->nentry) {
  299. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  300. return -1;
  301. }
  302. e = za->entry+idx;
  303. if (e->orig == NULL || e->orig->local_extra_fields_read)
  304. return 0;
  305. if (e->orig->offset + 26 > ZIP_INT64_MAX) {
  306. zip_error_set(&za->error, ZIP_ER_SEEK, EFBIG);
  307. return -1;
  308. }
  309. if (zip_source_seek(za->src, (zip_int64_t)(e->orig->offset + 26), SEEK_SET) < 0) {
  310. _zip_error_set_from_source(&za->error, za->src);
  311. return -1;
  312. }
  313. if ((buffer = _zip_buffer_new_from_source(za->src, sizeof(b), b, &za->error)) == NULL) {
  314. return -1;
  315. }
  316. fname_len = _zip_buffer_get_16(buffer);
  317. ef_len = _zip_buffer_get_16(buffer);
  318. if (!_zip_buffer_eof(buffer)) {
  319. _zip_buffer_free(buffer);
  320. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  321. return -1;
  322. }
  323. _zip_buffer_free(buffer);
  324. if (ef_len > 0) {
  325. zip_extra_field_t *ef;
  326. zip_uint8_t *ef_raw;
  327. if (zip_source_seek(za->src, fname_len, SEEK_CUR) < 0) {
  328. zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  329. return -1;
  330. }
  331. ef_raw = _zip_read_data(NULL, za->src, ef_len, 0, &za->error);
  332. if (ef_raw == NULL)
  333. return -1;
  334. if (!_zip_ef_parse(ef_raw, ef_len, ZIP_EF_LOCAL, &ef, &za->error)) {
  335. free(ef_raw);
  336. return -1;
  337. }
  338. free(ef_raw);
  339. if (ef) {
  340. ef = _zip_ef_remove_internal(ef);
  341. e->orig->extra_fields = _zip_ef_merge(e->orig->extra_fields, ef);
  342. }
  343. }
  344. e->orig->local_extra_fields_read = 1;
  345. if (e->changes && e->changes->local_extra_fields_read == 0) {
  346. e->changes->extra_fields = e->orig->extra_fields;
  347. e->changes->local_extra_fields_read = 1;
  348. }
  349. return 0;
  350. }