zip_extra_field.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "zipint.h"
  31. #include <errno.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. struct zip_extra_field *
  35. _zip_ef_clone(const struct zip_extra_field *ef, struct zip_error *error)
  36. {
  37. struct zip_extra_field *head, *prev, *def;
  38. head = prev = NULL;
  39. while (ef) {
  40. if ((def=_zip_ef_new(ef->id, ef->size, ef->data, ef->flags)) == NULL) {
  41. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  42. _zip_ef_free(head);
  43. return NULL;
  44. }
  45. if (head == NULL)
  46. head = def;
  47. if (prev)
  48. prev->next = def;
  49. prev = def;
  50. ef = ef->next;
  51. }
  52. return head;
  53. }
  54. struct zip_extra_field *
  55. _zip_ef_delete_by_id(struct zip_extra_field *ef, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags)
  56. {
  57. struct zip_extra_field *head, *prev;
  58. int i;
  59. i = 0;
  60. head = ef;
  61. prev = NULL;
  62. for (; ef; ef=(prev ? prev->next : head)) {
  63. if ((ef->flags & flags & ZIP_EF_BOTH) && ((ef->id == id) || (id == ZIP_EXTRA_FIELD_ALL))) {
  64. if (id_idx == ZIP_EXTRA_FIELD_ALL || i == id_idx) {
  65. ef->flags &= ~(flags & ZIP_EF_BOTH);
  66. if ((ef->flags & ZIP_EF_BOTH) == 0) {
  67. if (prev)
  68. prev->next = ef->next;
  69. else
  70. head = ef->next;
  71. ef->next = NULL;
  72. _zip_ef_free(ef);
  73. if (id_idx == ZIP_EXTRA_FIELD_ALL)
  74. continue;
  75. }
  76. }
  77. i++;
  78. if (i > id_idx)
  79. break;
  80. }
  81. prev = ef;
  82. }
  83. return head;
  84. }
  85. void
  86. _zip_ef_free(struct zip_extra_field *ef)
  87. {
  88. struct zip_extra_field *ef2;
  89. while (ef) {
  90. ef2 = ef->next;
  91. free(ef->data);
  92. free(ef);
  93. ef = ef2;
  94. }
  95. }
  96. const zip_uint8_t *
  97. _zip_ef_get_by_id(const struct zip_extra_field *ef, zip_uint16_t *lenp, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags, struct zip_error *error)
  98. {
  99. static const zip_uint8_t empty[1] = { '\0' };
  100. int i;
  101. i = 0;
  102. for (; ef; ef=ef->next) {
  103. if (ef->id == id && (ef->flags & flags & ZIP_EF_BOTH)) {
  104. if (i < id_idx) {
  105. i++;
  106. continue;
  107. }
  108. if (lenp)
  109. *lenp = ef->size;
  110. if (ef->size > 0)
  111. return ef->data;
  112. else
  113. return empty;
  114. }
  115. }
  116. _zip_error_set(error, ZIP_ER_NOENT, 0);
  117. return NULL;
  118. }
  119. struct zip_extra_field *
  120. _zip_ef_merge(struct zip_extra_field *to, struct zip_extra_field *from)
  121. {
  122. struct zip_extra_field *ef2, *tt, *tail;
  123. int duplicate;
  124. if (to == NULL)
  125. return from;
  126. for (tail=to; tail->next; tail=tail->next)
  127. ;
  128. for (; from; from=ef2) {
  129. ef2 = from->next;
  130. duplicate = 0;
  131. for (tt=to; tt; tt=tt->next) {
  132. if (tt->id == from->id && tt->size == from->size && memcmp(tt->data, from->data, tt->size) == 0) {
  133. tt->flags |= (from->flags & ZIP_EF_BOTH);
  134. duplicate = 1;
  135. break;
  136. }
  137. }
  138. from->next = NULL;
  139. if (duplicate)
  140. _zip_ef_free(from);
  141. else
  142. tail = tail->next = from;
  143. }
  144. return to;
  145. }
  146. struct zip_extra_field *
  147. _zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags)
  148. {
  149. struct zip_extra_field *ef;
  150. if ((ef=(struct zip_extra_field *)malloc(sizeof(*ef))) == NULL)
  151. return NULL;
  152. ef->next = NULL;
  153. ef->flags = flags;
  154. ef->id = id;
  155. ef->size = size;
  156. if (size > 0) {
  157. if ((ef->data=(zip_uint8_t *)_zip_memdup(data, size, NULL)) == NULL) {
  158. free(ef);
  159. return NULL;
  160. }
  161. }
  162. else
  163. ef->data = NULL;
  164. return ef;
  165. }
  166. struct zip_extra_field *
  167. _zip_ef_parse(const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags, struct zip_error *error)
  168. {
  169. struct zip_extra_field *ef, *ef2, *ef_head;
  170. const zip_uint8_t *p;
  171. zip_uint16_t fid, flen;
  172. ef_head = NULL;
  173. for (p=data; p<data+len; p+=flen) {
  174. if (p+4 > data+len) {
  175. _zip_error_set(error, ZIP_ER_INCONS, 0);
  176. _zip_ef_free(ef_head);
  177. return NULL;
  178. }
  179. fid = _zip_read2(&p);
  180. flen = _zip_read2(&p);
  181. if (p+flen > data+len) {
  182. _zip_error_set(error, ZIP_ER_INCONS, 0);
  183. _zip_ef_free(ef_head);
  184. return NULL;
  185. }
  186. if ((ef2=_zip_ef_new(fid, flen, p, flags)) == NULL) {
  187. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  188. _zip_ef_free(ef_head);
  189. return NULL;
  190. }
  191. if (ef_head) {
  192. ef->next = ef2;
  193. ef = ef2;
  194. }
  195. else
  196. ef_head = ef = ef2;
  197. }
  198. return ef_head;
  199. }
  200. struct zip_extra_field *
  201. _zip_ef_remove_internal(struct zip_extra_field *ef)
  202. {
  203. struct zip_extra_field *ef_head;
  204. struct zip_extra_field *prev, *next;
  205. ef_head = ef;
  206. prev = NULL;
  207. while (ef) {
  208. if (ZIP_EF_IS_INTERNAL(ef->id)) {
  209. next = ef->next;
  210. if (ef_head == ef)
  211. ef_head = next;
  212. ef->next = NULL;
  213. _zip_ef_free(ef);
  214. if (prev)
  215. prev->next = next;
  216. ef = next;
  217. }
  218. else {
  219. prev = ef;
  220. ef = ef->next;
  221. }
  222. }
  223. return ef_head;
  224. }
  225. zip_uint16_t
  226. _zip_ef_size(const struct zip_extra_field *ef, zip_flags_t flags)
  227. {
  228. zip_uint16_t size;
  229. size = 0;
  230. for (; ef; ef=ef->next) {
  231. if (ef->flags & flags & ZIP_EF_BOTH)
  232. size += 4+ef->size;
  233. }
  234. return size;
  235. }
  236. void
  237. _zip_ef_write(const struct zip_extra_field *ef, zip_flags_t flags, FILE *f)
  238. {
  239. for (; ef; ef=ef->next) {
  240. if (ef->flags & flags & ZIP_EF_BOTH) {
  241. _zip_write2(ef->id, f);
  242. _zip_write2(ef->size, f);
  243. if (ef->size > 0)
  244. fwrite(ef->data, ef->size, 1, f);
  245. }
  246. }
  247. }
  248. int
  249. _zip_read_local_ef(struct zip *za, zip_uint64_t idx)
  250. {
  251. struct zip_entry *e;
  252. unsigned char b[4];
  253. const unsigned char *p;
  254. zip_uint16_t fname_len, ef_len;
  255. if (idx >= za->nentry) {
  256. _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  257. return -1;
  258. }
  259. e = za->entry+idx;
  260. if (e->orig == NULL || e->orig->local_extra_fields_read)
  261. return 0;
  262. if (fseeko(za->zp, (off_t)(e->orig->offset + 26), SEEK_SET) < 0) {
  263. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  264. return -1;
  265. }
  266. if (fread(b, sizeof(b), 1, za->zp) != 1) {
  267. _zip_error_set(&za->error, ZIP_ER_READ, errno);
  268. return -1;
  269. }
  270. p = b;
  271. fname_len = _zip_read2(&p);
  272. ef_len = _zip_read2(&p);
  273. if (ef_len > 0) {
  274. struct zip_extra_field *ef;
  275. zip_uint8_t *ef_raw;
  276. if (fseek(za->zp, fname_len, SEEK_CUR) < 0) {
  277. _zip_error_set(&za->error, ZIP_ER_SEEK, errno);
  278. return -1;
  279. }
  280. ef_raw = _zip_read_data(NULL, za->zp, ef_len, 0, &za->error);
  281. if (ef_raw == NULL)
  282. return -1;
  283. if ((ef=_zip_ef_parse(ef_raw, ef_len, ZIP_EF_LOCAL, &za->error)) == NULL) {
  284. free(ef_raw);
  285. return -1;
  286. }
  287. free(ef_raw);
  288. ef = _zip_ef_remove_internal(ef);
  289. e->orig->extra_fields = _zip_ef_merge(e->orig->extra_fields, ef);
  290. }
  291. e->orig->local_extra_fields_read = 1;
  292. if (e->changes && e->changes->local_extra_fields_read == 0) {
  293. e->changes->extra_fields = e->orig->extra_fields;
  294. e->changes->local_extra_fields_read = 1;
  295. }
  296. return 0;
  297. }