zip_extra_field_api.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. zip_extra_field_api.c -- public extra fields API functions
  3. Copyright (C) 2012-2021 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. ZIP_EXTERN int
  32. zip_file_extra_field_delete(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_flags_t flags) {
  33. zip_dirent_t *de;
  34. if ((flags & ZIP_EF_BOTH) == 0) {
  35. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  36. return -1;
  37. }
  38. if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) {
  39. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  40. return -1;
  41. }
  42. if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
  43. return -1;
  44. if (ZIP_IS_RDONLY(za)) {
  45. zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
  46. return -1;
  47. }
  48. if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)
  49. return -1;
  50. de = za->entry[idx].changes;
  51. de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ZIP_EXTRA_FIELD_ALL, ef_idx, flags);
  52. return 0;
  53. }
  54. ZIP_EXTERN int
  55. zip_file_extra_field_delete_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, zip_flags_t flags) {
  56. zip_dirent_t *de;
  57. if ((flags & ZIP_EF_BOTH) == 0) {
  58. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  59. return -1;
  60. }
  61. if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) {
  62. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  63. return -1;
  64. }
  65. if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
  66. return -1;
  67. if (ZIP_IS_RDONLY(za)) {
  68. zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
  69. return -1;
  70. }
  71. if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)
  72. return -1;
  73. de = za->entry[idx].changes;
  74. de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ef_id, ef_idx, flags);
  75. return 0;
  76. }
  77. ZIP_EXTERN const zip_uint8_t *
  78. zip_file_extra_field_get(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_uint16_t *idp, zip_uint16_t *lenp, zip_flags_t flags) {
  79. static const zip_uint8_t empty[1] = {'\0'};
  80. zip_dirent_t *de;
  81. zip_extra_field_t *ef;
  82. int i;
  83. if ((flags & ZIP_EF_BOTH) == 0) {
  84. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  85. return NULL;
  86. }
  87. if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
  88. return NULL;
  89. if (flags & ZIP_FL_LOCAL)
  90. if (_zip_read_local_ef(za, idx) < 0)
  91. return NULL;
  92. i = 0;
  93. for (ef = de->extra_fields; ef; ef = ef->next) {
  94. if (ef->flags & flags & ZIP_EF_BOTH) {
  95. if (i < ef_idx) {
  96. i++;
  97. continue;
  98. }
  99. if (idp)
  100. *idp = ef->id;
  101. if (lenp)
  102. *lenp = ef->size;
  103. if (ef->size > 0)
  104. return ef->data;
  105. else
  106. return empty;
  107. }
  108. }
  109. zip_error_set(&za->error, ZIP_ER_NOENT, 0);
  110. return NULL;
  111. }
  112. ZIP_EXTERN const zip_uint8_t *
  113. zip_file_extra_field_get_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, zip_uint16_t *lenp, zip_flags_t flags) {
  114. zip_dirent_t *de;
  115. if ((flags & ZIP_EF_BOTH) == 0) {
  116. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  117. return NULL;
  118. }
  119. if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
  120. return NULL;
  121. if (flags & ZIP_FL_LOCAL)
  122. if (_zip_read_local_ef(za, idx) < 0)
  123. return NULL;
  124. return _zip_ef_get_by_id(de->extra_fields, lenp, ef_id, ef_idx, flags, &za->error);
  125. }
  126. ZIP_EXTERN zip_int16_t
  127. zip_file_extra_fields_count(zip_t *za, zip_uint64_t idx, zip_flags_t flags) {
  128. zip_dirent_t *de;
  129. zip_extra_field_t *ef;
  130. zip_uint16_t n;
  131. if ((flags & ZIP_EF_BOTH) == 0) {
  132. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  133. return -1;
  134. }
  135. if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
  136. return -1;
  137. if (flags & ZIP_FL_LOCAL)
  138. if (_zip_read_local_ef(za, idx) < 0)
  139. return -1;
  140. n = 0;
  141. for (ef = de->extra_fields; ef; ef = ef->next)
  142. if (ef->flags & flags & ZIP_EF_BOTH)
  143. n++;
  144. return (zip_int16_t)n;
  145. }
  146. ZIP_EXTERN zip_int16_t
  147. zip_file_extra_fields_count_by_id(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_flags_t flags) {
  148. zip_dirent_t *de;
  149. zip_extra_field_t *ef;
  150. zip_uint16_t n;
  151. if ((flags & ZIP_EF_BOTH) == 0) {
  152. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  153. return -1;
  154. }
  155. if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
  156. return -1;
  157. if (flags & ZIP_FL_LOCAL)
  158. if (_zip_read_local_ef(za, idx) < 0)
  159. return -1;
  160. n = 0;
  161. for (ef = de->extra_fields; ef; ef = ef->next)
  162. if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH))
  163. n++;
  164. return (zip_int16_t)n;
  165. }
  166. ZIP_EXTERN int
  167. zip_file_extra_field_set(zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_uint16_t ef_idx, const zip_uint8_t *data, zip_uint16_t len, zip_flags_t flags) {
  168. zip_dirent_t *de;
  169. zip_uint16_t ls, cs;
  170. zip_extra_field_t *ef, *ef_prev, *ef_new;
  171. int i, found, new_len;
  172. if ((flags & ZIP_EF_BOTH) == 0) {
  173. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  174. return -1;
  175. }
  176. if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
  177. return -1;
  178. if (ZIP_IS_RDONLY(za)) {
  179. zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
  180. return -1;
  181. }
  182. if (ZIP_EF_IS_INTERNAL(ef_id)) {
  183. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  184. return -1;
  185. }
  186. if (_zip_file_extra_field_prepare_for_change(za, idx) < 0)
  187. return -1;
  188. de = za->entry[idx].changes;
  189. ef = de->extra_fields;
  190. ef_prev = NULL;
  191. i = 0;
  192. found = 0;
  193. for (; ef; ef = ef->next) {
  194. if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH)) {
  195. if (i == ef_idx) {
  196. found = 1;
  197. break;
  198. }
  199. i++;
  200. }
  201. ef_prev = ef;
  202. }
  203. if (i < ef_idx && ef_idx != ZIP_EXTRA_FIELD_NEW) {
  204. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  205. return -1;
  206. }
  207. if (flags & ZIP_EF_LOCAL)
  208. ls = _zip_ef_size(de->extra_fields, ZIP_EF_LOCAL);
  209. else
  210. ls = 0;
  211. if (flags & ZIP_EF_CENTRAL)
  212. cs = _zip_ef_size(de->extra_fields, ZIP_EF_CENTRAL);
  213. else
  214. cs = 0;
  215. new_len = ls > cs ? ls : cs;
  216. if (found)
  217. new_len -= ef->size + 4;
  218. new_len += len + 4;
  219. if (new_len > ZIP_UINT16_MAX) {
  220. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  221. return -1;
  222. }
  223. if ((ef_new = _zip_ef_new(ef_id, len, data, flags)) == NULL) {
  224. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  225. return -1;
  226. }
  227. if (found) {
  228. if ((ef->flags & ZIP_EF_BOTH) == (flags & ZIP_EF_BOTH)) {
  229. ef_new->next = ef->next;
  230. ef->next = NULL;
  231. _zip_ef_free(ef);
  232. if (ef_prev)
  233. ef_prev->next = ef_new;
  234. else
  235. de->extra_fields = ef_new;
  236. }
  237. else {
  238. ef->flags &= ~(flags & ZIP_EF_BOTH);
  239. ef_new->next = ef->next;
  240. ef->next = ef_new;
  241. }
  242. }
  243. else if (ef_prev) {
  244. ef_new->next = ef_prev->next;
  245. ef_prev->next = ef_new;
  246. }
  247. else
  248. de->extra_fields = ef_new;
  249. return 0;
  250. }
  251. int
  252. _zip_file_extra_field_prepare_for_change(zip_t *za, zip_uint64_t idx) {
  253. zip_entry_t *e;
  254. if (idx >= za->nentry) {
  255. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  256. return -1;
  257. }
  258. e = za->entry + idx;
  259. if (e->changes && (e->changes->changed & ZIP_DIRENT_EXTRA_FIELD))
  260. return 0;
  261. if (e->orig) {
  262. if (_zip_read_local_ef(za, idx) < 0)
  263. return -1;
  264. }
  265. if (e->changes == NULL) {
  266. if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) {
  267. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  268. return -1;
  269. }
  270. }
  271. if (e->orig && e->orig->extra_fields) {
  272. if ((e->changes->extra_fields = _zip_ef_clone(e->orig->extra_fields, &za->error)) == NULL)
  273. return -1;
  274. }
  275. e->changes->changed |= ZIP_DIRENT_EXTRA_FIELD;
  276. return 0;
  277. }