Rizin
unix-like reverse engineering framework and cli tools
zip_add_entry.c File Reference
#include <stdlib.h>
#include "zipint.h"

Go to the source code of this file.

Functions

zip_int64_t _zip_add_entry (zip_t *za)
 

Function Documentation

◆ _zip_add_entry()

zip_int64_t _zip_add_entry ( zip_t za)

Definition at line 43 of file zip_add_entry.c.

43  {
45 
46  if (za->nentry + 1 >= za->nentry_alloc) {
47  zip_entry_t *rentries;
48  zip_uint64_t nalloc = za->nentry_alloc;
49  zip_uint64_t additional_entries = 2 * nalloc;
50  zip_uint64_t realloc_size;
51 
52  if (additional_entries < 16) {
53  additional_entries = 16;
54  }
55  else if (additional_entries > 1024) {
56  additional_entries = 1024;
57  }
58  /* neither + nor * overflows can happen: nentry_alloc * sizeof(struct zip_entry) < UINT64_MAX */
59  nalloc += additional_entries;
60  realloc_size = sizeof(struct zip_entry) * (size_t)nalloc;
61 
62  if (sizeof(struct zip_entry) * (size_t)za->nentry_alloc > realloc_size) {
64  return -1;
65  }
66  rentries = (zip_entry_t *)realloc(za->entry, sizeof(struct zip_entry) * (size_t)nalloc);
67  if (!rentries) {
69  return -1;
70  }
71  za->entry = rentries;
72  za->nentry_alloc = nalloc;
73  }
74 
75  idx = za->nentry++;
76 
78 
79  return (zip_int64_t)idx;
80 }
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
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
int idx
Definition: setup.py:197
int size_t
Definition: sftypes.h:40
Definition: zipint.h:408
zip_error_t error
Definition: zipint.h:281
zip_entry_t * entry
Definition: zipint.h:294
zip_uint64_t nentry_alloc
Definition: zipint.h:293
zip_uint64_t nentry
Definition: zipint.h:292
void _zip_entry_init(zip_entry_t *e)
Definition: zip_entry.c:46
uint64_t zip_uint64_t
Definition: zipconf.h:39
int64_t zip_int64_t
Definition: zipconf.h:38
zip_t * za
Definition: ziptool.c:79

References _zip_entry_init(), zip::entry, zip::error, setup::idx, zip::nentry, zip::nentry_alloc, realloc(), za, ZIP_ER_MEMORY, and zip_error_set().

Referenced by _zip_file_replace().