Rizin
unix-like reverse engineering framework and cli tools
zip_extra_field_api.c File Reference
#include "zipint.h"

Go to the source code of this file.

Functions

ZIP_EXTERN int zip_file_extra_field_delete (zip_t *za, zip_uint64_t idx, zip_uint16_t ef_idx, zip_flags_t flags)
 
ZIP_EXTERN int 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)
 
ZIP_EXTERN const zip_uint8_tzip_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)
 
ZIP_EXTERN const zip_uint8_tzip_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)
 
ZIP_EXTERN zip_int16_t zip_file_extra_fields_count (zip_t *za, zip_uint64_t idx, zip_flags_t flags)
 
ZIP_EXTERN zip_int16_t zip_file_extra_fields_count_by_id (zip_t *za, zip_uint64_t idx, zip_uint16_t ef_id, zip_flags_t flags)
 
ZIP_EXTERN int 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)
 
int _zip_file_extra_field_prepare_for_change (zip_t *za, zip_uint64_t idx)
 

Function Documentation

◆ _zip_file_extra_field_prepare_for_change()

int _zip_file_extra_field_prepare_for_change ( zip_t za,
zip_uint64_t  idx 
)

Definition at line 323 of file zip_extra_field_api.c.

323  {
324  zip_entry_t *e;
325 
326  if (idx >= za->nentry) {
328  return -1;
329  }
330 
331  e = za->entry + idx;
332 
333  if (e->changes && (e->changes->changed & ZIP_DIRENT_EXTRA_FIELD))
334  return 0;
335 
336  if (e->orig) {
337  if (_zip_read_local_ef(za, idx) < 0)
338  return -1;
339  }
340 
341  if (e->changes == NULL) {
342  if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) {
344  return -1;
345  }
346  }
347 
348  if (e->orig && e->orig->extra_fields) {
349  if ((e->changes->extra_fields = _zip_ef_clone(e->orig->extra_fields, &za->error)) == NULL)
350  return -1;
351  }
352  e->changes->changed |= ZIP_DIRENT_EXTRA_FIELD;
353 
354  return 0;
355 }
#define e(frag)
#define NULL
Definition: cris-opc.c:27
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_MEMORY
Definition: zip.h:119
#define ZIP_ER_INVAL
Definition: zip.h:123
int idx
Definition: setup.py:197
Definition: zipint.h:408
zip_error_t error
Definition: zipint.h:281
zip_entry_t * entry
Definition: zipint.h:294
zip_uint64_t nentry
Definition: zipint.h:292
zip_dirent_t * _zip_dirent_clone(const zip_dirent_t *sde)
Definition: zip_dirent.c:215
int _zip_read_local_ef(zip_t *za, zip_uint64_t idx)
zip_extra_field_t * _zip_ef_clone(const zip_extra_field_t *ef, zip_error_t *error)
#define ZIP_DIRENT_EXTRA_FIELD
Definition: zipint.h:319
zip_t * za
Definition: ziptool.c:79

References _zip_dirent_clone(), _zip_ef_clone(), _zip_read_local_ef(), e, zip::entry, zip::error, setup::idx, zip::nentry, NULL, za, ZIP_DIRENT_EXTRA_FIELD, ZIP_ER_INVAL, ZIP_ER_MEMORY, and zip_error_set().

Referenced by zip_file_extra_field_delete(), zip_file_extra_field_delete_by_id(), and zip_file_extra_field_set().

◆ zip_file_extra_field_delete()

ZIP_EXTERN int zip_file_extra_field_delete ( zip_t za,
zip_uint64_t  idx,
zip_uint16_t  ef_idx,
zip_flags_t  flags 
)

Definition at line 39 of file zip_extra_field_api.c.

39  {
40  zip_dirent_t *de;
41 
42  if ((flags & ZIP_EF_BOTH) == 0) {
44  return -1;
45  }
46 
47  if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) {
49  return -1;
50  }
51 
52  if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
53  return -1;
54 
55  if (ZIP_IS_RDONLY(za)) {
57  return -1;
58  }
59 
61  return -1;
62 
63  de = za->entry[idx].changes;
64 
66  return 0;
67 }
#define ZIP_EXTRA_FIELD_ALL
Definition: zip.h:99
#define ZIP_ER_RDONLY
Definition: zip.h:130
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
zip_extra_field_t * extra_fields
Definition: zipint.h:342
zip_dirent_t * changes
Definition: zipint.h:410
zip_dirent_t * _zip_get_dirent(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *error)
Definition: zip_dirent.c:1064
zip_extra_field_t * _zip_ef_delete_by_id(zip_extra_field_t *ef, zip_uint16_t id, zip_uint16_t id_idx, zip_flags_t flags)
int _zip_file_extra_field_prepare_for_change(zip_t *za, zip_uint64_t idx)
#define ZIP_IS_RDONLY(za)
Definition: zipint.h:479
#define ZIP_EF_BOTH
Definition: zipint.h:245

References _zip_ef_delete_by_id(), _zip_file_extra_field_prepare_for_change(), _zip_get_dirent(), zip_entry::changes, zip::entry, zip::error, zip_dirent::extra_fields, flags, setup::idx, NULL, za, ZIP_EF_BOTH, ZIP_ER_INVAL, ZIP_ER_RDONLY, zip_error_set(), ZIP_EXTRA_FIELD_ALL, and ZIP_IS_RDONLY.

Referenced by delete_extra().

◆ zip_file_extra_field_delete_by_id()

ZIP_EXTERN int 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 
)

Definition at line 71 of file zip_extra_field_api.c.

71  {
72  zip_dirent_t *de;
73 
74  if ((flags & ZIP_EF_BOTH) == 0) {
76  return -1;
77  }
78 
79  if (((flags & ZIP_EF_BOTH) == ZIP_EF_BOTH) && (ef_idx != ZIP_EXTRA_FIELD_ALL)) {
81  return -1;
82  }
83 
84  if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
85  return -1;
86 
87  if (ZIP_IS_RDONLY(za)) {
89  return -1;
90  }
91 
93  return -1;
94 
95  de = za->entry[idx].changes;
96 
97  de->extra_fields = _zip_ef_delete_by_id(de->extra_fields, ef_id, ef_idx, flags);
98  return 0;
99 }

References _zip_ef_delete_by_id(), _zip_file_extra_field_prepare_for_change(), _zip_get_dirent(), zip_entry::changes, zip::entry, zip::error, zip_dirent::extra_fields, flags, setup::idx, NULL, za, ZIP_EF_BOTH, ZIP_ER_INVAL, ZIP_ER_RDONLY, zip_error_set(), ZIP_EXTRA_FIELD_ALL, and ZIP_IS_RDONLY.

Referenced by delete_extra_by_id().

◆ zip_file_extra_field_get()

ZIP_EXTERN const zip_uint8_t* 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 
)

Definition at line 103 of file zip_extra_field_api.c.

103  {
104  static const zip_uint8_t empty[1] = {'\0'};
105 
106  zip_dirent_t *de;
108  int i;
109 
110  if ((flags & ZIP_EF_BOTH) == 0) {
112  return NULL;
113  }
114 
115  if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
116  return NULL;
117 
118  if (flags & ZIP_FL_LOCAL)
119  if (_zip_read_local_ef(za, idx) < 0)
120  return NULL;
121 
122  i = 0;
123  for (ef = de->extra_fields; ef; ef = ef->next) {
124  if (ef->flags & flags & ZIP_EF_BOTH) {
125  if (i < ef_idx) {
126  i++;
127  continue;
128  }
129 
130  if (idp)
131  *idp = ef->id;
132  if (lenp)
133  *lenp = ef->size;
134  if (ef->size > 0)
135  return ef->data;
136  else
137  return empty;
138  }
139  }
140 
142  return NULL;
143 }
#define ef(frag,...)
lzma_index ** i
Definition: index.h:629
#define ZIP_ER_NOENT
Definition: zip.h:114
#define ZIP_FL_LOCAL
Definition: zip.h:85
Definition: zipcmp.c:69
zip_uint16_t size
Definition: zipcmp.c:73
zip_uint16_t id
Definition: zipcmp.c:72
const zip_uint8_t * data
Definition: zipcmp.c:74
zip_uint16_t flags
Definition: zipcmp.c:71
uint8_t zip_uint8_t
Definition: zipconf.h:33

References _zip_get_dirent(), _zip_read_local_ef(), ef::data, ef, zip::error, zip_dirent::extra_fields, flags, ef::flags, i, ef::id, setup::idx, NULL, ef::size, za, ZIP_EF_BOTH, ZIP_ER_INVAL, ZIP_ER_NOENT, zip_error_set(), and ZIP_FL_LOCAL.

Referenced by ef_read(), and get_extra().

◆ zip_file_extra_field_get_by_id()

ZIP_EXTERN const zip_uint8_t* 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 
)

Definition at line 147 of file zip_extra_field_api.c.

147  {
148  zip_dirent_t *de;
149 
150  if ((flags & ZIP_EF_BOTH) == 0) {
152  return NULL;
153  }
154 
155  if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
156  return NULL;
157 
158  if (flags & ZIP_FL_LOCAL)
159  if (_zip_read_local_ef(za, idx) < 0)
160  return NULL;
161 
162  return _zip_ef_get_by_id(de->extra_fields, lenp, ef_id, ef_idx, flags, &za->error);
163 }
const zip_uint8_t * _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)

References _zip_ef_get_by_id(), _zip_get_dirent(), _zip_read_local_ef(), zip::error, zip_dirent::extra_fields, flags, setup::idx, NULL, za, ZIP_EF_BOTH, ZIP_ER_INVAL, zip_error_set(), and ZIP_FL_LOCAL.

Referenced by get_extra_by_id().

◆ zip_file_extra_field_set()

ZIP_EXTERN int 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 
)

Definition at line 221 of file zip_extra_field_api.c.

221  {
222  zip_dirent_t *de;
223  zip_uint16_t ls, cs;
224  zip_extra_field_t *ef, *ef_prev, *ef_new;
225  int i, found, new_len;
226 
227  if ((flags & ZIP_EF_BOTH) == 0) {
229  return -1;
230  }
231 
232  if (_zip_get_dirent(za, idx, 0, NULL) == NULL)
233  return -1;
234 
235  if (ZIP_IS_RDONLY(za)) {
237  return -1;
238  }
239 
240  if (ZIP_EF_IS_INTERNAL(ef_id)) {
242  return -1;
243  }
244 
246  return -1;
247 
248  de = za->entry[idx].changes;
249 
250  ef = de->extra_fields;
251  ef_prev = NULL;
252  i = 0;
253  found = 0;
254 
255  for (; ef; ef = ef->next) {
256  if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH)) {
257  if (i == ef_idx) {
258  found = 1;
259  break;
260  }
261  i++;
262  }
263  ef_prev = ef;
264  }
265 
266  if (i < ef_idx && ef_idx != ZIP_EXTRA_FIELD_NEW) {
268  return -1;
269  }
270 
271  if (flags & ZIP_EF_LOCAL)
273  else
274  ls = 0;
275  if (flags & ZIP_EF_CENTRAL)
277  else
278  cs = 0;
279 
280  new_len = ls > cs ? ls : cs;
281  if (found)
282  new_len -= ef->size + 4;
283  new_len += len + 4;
284 
285  if (new_len > ZIP_UINT16_MAX) {
287  return -1;
288  }
289 
290  if ((ef_new = _zip_ef_new(ef_id, len, data, flags)) == NULL) {
292  return -1;
293  }
294 
295  if (found) {
296  if ((ef->flags & ZIP_EF_BOTH) == (flags & ZIP_EF_BOTH)) {
297  ef_new->next = ef->next;
298  ef->next = NULL;
299  _zip_ef_free(ef);
300  if (ef_prev)
301  ef_prev->next = ef_new;
302  else
303  de->extra_fields = ef_new;
304  }
305  else {
306  ef->flags &= ~(flags & ZIP_EF_BOTH);
307  ef_new->next = ef->next;
308  ef->next = ef_new;
309  }
310  }
311  else if (ef_prev) {
312  ef_new->next = ef_prev->next;
313  ef_prev->next = ef_new;
314  }
315  else
316  de->extra_fields = ef_new;
317 
318  return 0;
319 }
size_t len
Definition: 6502dis.c:15
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
#define ZIP_EXTRA_FIELD_NEW
Definition: zip.h:100
zip_extra_field_t * next
Definition: zipint.h:368
zip_uint16_t _zip_ef_size(const zip_extra_field_t *ef, zip_flags_t flags)
zip_extra_field_t * _zip_ef_new(zip_uint16_t id, zip_uint16_t size, const zip_uint8_t *data, zip_flags_t flags)
void _zip_ef_free(zip_extra_field_t *ef)
#define ZIP_UINT16_MAX
Definition: zipconf.h:47
uint16_t zip_uint16_t
Definition: zipconf.h:35
#define ZIP_EF_CENTRAL
Definition: zipint.h:244
#define ZIP_EF_LOCAL
Definition: zipint.h:243
#define ZIP_EF_IS_INTERNAL(id)
Definition: zipint.h:90

References _zip_ef_free(), _zip_ef_new(), _zip_ef_size(), _zip_file_extra_field_prepare_for_change(), _zip_get_dirent(), zip_entry::changes, test_evm::cs, ef, zip::entry, zip::error, zip_dirent::extra_fields, flags, ef::flags, found, i, ef::id, setup::idx, len, zip_extra_field::next, NULL, ef::size, za, ZIP_EF_BOTH, ZIP_EF_CENTRAL, ZIP_EF_IS_INTERNAL, ZIP_EF_LOCAL, ZIP_ER_INVAL, ZIP_ER_MEMORY, ZIP_ER_RDONLY, zip_error_set(), ZIP_EXTRA_FIELD_NEW, ZIP_IS_RDONLY, and ZIP_UINT16_MAX.

Referenced by set_extra().

◆ zip_file_extra_fields_count()

ZIP_EXTERN zip_int16_t zip_file_extra_fields_count ( zip_t za,
zip_uint64_t  idx,
zip_flags_t  flags 
)

Definition at line 167 of file zip_extra_field_api.c.

167  {
168  zip_dirent_t *de;
170  zip_uint16_t n;
171 
172  if ((flags & ZIP_EF_BOTH) == 0) {
174  return -1;
175  }
176 
177  if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
178  return -1;
179 
180  if (flags & ZIP_FL_LOCAL)
181  if (_zip_read_local_ef(za, idx) < 0)
182  return -1;
183 
184  n = 0;
185  for (ef = de->extra_fields; ef; ef = ef->next)
186  if (ef->flags & flags & ZIP_EF_BOTH)
187  n++;
188 
189  return (zip_int16_t)n;
190 }
int n
Definition: mipsasm.c:19
int16_t zip_int16_t
Definition: zipconf.h:34

References _zip_get_dirent(), _zip_read_local_ef(), ef, zip::error, zip_dirent::extra_fields, flags, ef::flags, setup::idx, n, NULL, za, ZIP_EF_BOTH, ZIP_ER_INVAL, zip_error_set(), and ZIP_FL_LOCAL.

Referenced by count_extra(), and ef_read().

◆ zip_file_extra_fields_count_by_id()

ZIP_EXTERN zip_int16_t zip_file_extra_fields_count_by_id ( zip_t za,
zip_uint64_t  idx,
zip_uint16_t  ef_id,
zip_flags_t  flags 
)

Definition at line 194 of file zip_extra_field_api.c.

194  {
195  zip_dirent_t *de;
197  zip_uint16_t n;
198 
199  if ((flags & ZIP_EF_BOTH) == 0) {
201  return -1;
202  }
203 
204  if ((de = _zip_get_dirent(za, idx, flags, &za->error)) == NULL)
205  return -1;
206 
207  if (flags & ZIP_FL_LOCAL)
208  if (_zip_read_local_ef(za, idx) < 0)
209  return -1;
210 
211  n = 0;
212  for (ef = de->extra_fields; ef; ef = ef->next)
213  if (ef->id == ef_id && (ef->flags & flags & ZIP_EF_BOTH))
214  n++;
215 
216  return (zip_int16_t)n;
217 }

References _zip_get_dirent(), _zip_read_local_ef(), ef, zip::error, zip_dirent::extra_fields, flags, ef::flags, ef::id, setup::idx, n, NULL, za, ZIP_EF_BOTH, ZIP_ER_INVAL, zip_error_set(), and ZIP_FL_LOCAL.

Referenced by count_extra_by_id().