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

Go to the source code of this file.

Functions

int _zip_set_name (zip_t *za, zip_uint64_t idx, const char *name, zip_flags_t flags)
 

Function Documentation

◆ _zip_set_name()

int _zip_set_name ( zip_t za,
zip_uint64_t  idx,
const char *  name,
zip_flags_t  flags 
)

Definition at line 42 of file zip_set_name.c.

42  {
43  zip_entry_t *e;
45  bool same_as_orig;
46  zip_int64_t i;
47  const zip_uint8_t *old_name, *new_name;
48  zip_string_t *old_str;
49 
50  if (idx >= za->nentry) {
52  return -1;
53  }
54 
55  if (ZIP_IS_RDONLY(za)) {
57  return -1;
58  }
59 
60  if (name && name[0] != '\0') {
61  /* TODO: check for string too long */
62  if ((str = _zip_string_new((const zip_uint8_t *)name, (zip_uint16_t)strlen(name), flags, &za->error)) == NULL)
63  return -1;
65  str->encoding = ZIP_ENCODING_UTF8_KNOWN;
66  }
67  else
68  str = NULL;
69 
70  /* TODO: encoding flags needed for CP437? */
71  if ((i = _zip_name_locate(za, name, 0, NULL)) >= 0 && (zip_uint64_t)i != idx) {
74  return -1;
75  }
76 
77  /* no effective name change */
78  if (i >= 0 && (zip_uint64_t)i == idx) {
80  return 0;
81  }
82 
83  e = za->entry + idx;
84 
85  if (e->orig)
86  same_as_orig = _zip_string_equal(e->orig->filename, str);
87  else
88  same_as_orig = false;
89 
90  if (!same_as_orig && e->changes == NULL) {
91  if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) {
94  return -1;
95  }
96  }
97 
98  if ((new_name = _zip_string_get(same_as_orig ? e->orig->filename : str, NULL, 0, &za->error)) == NULL) {
100  return -1;
101  }
102 
103  if (e->changes) {
104  old_str = e->changes->filename;
105  }
106  else if (e->orig) {
107  old_str = e->orig->filename;
108  }
109  else {
110  old_str = NULL;
111  }
112 
113  if (old_str) {
114  if ((old_name = _zip_string_get(old_str, NULL, 0, &za->error)) == NULL) {
116  return -1;
117  }
118  }
119  else {
120  old_name = NULL;
121  }
122 
123  if (_zip_hash_add(za->names, new_name, idx, 0, &za->error) == false) {
125  return -1;
126  }
127  if (old_name) {
128  _zip_hash_delete(za->names, old_name, NULL);
129  }
130 
131  if (same_as_orig) {
132  if (e->changes) {
133  if (e->changes->changed & ZIP_DIRENT_FILENAME) {
134  _zip_string_free(e->changes->filename);
135  e->changes->changed &= ~ZIP_DIRENT_FILENAME;
136  if (e->changes->changed == 0) {
137  _zip_dirent_free(e->changes);
138  e->changes = NULL;
139  }
140  else {
141  /* TODO: what if not cloned? can that happen? */
142  e->changes->filename = e->orig->filename;
143  }
144  }
145  }
147  }
148  else {
149  if (e->changes->changed & ZIP_DIRENT_FILENAME) {
150  _zip_string_free(e->changes->filename);
151  }
152  e->changes->changed |= ZIP_DIRENT_FILENAME;
153  e->changes->filename = str;
154  }
155 
156  return 0;
157 }
#define e(frag)
lzma_index ** i
Definition: index.h:629
#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_EXISTS
Definition: zip.h:115
#define ZIP_FL_ENC_GUESS
Definition: zip.h:82
#define ZIP_ER_RDONLY
Definition: zip.h:130
#define ZIP_ER_INVAL
Definition: zip.h:123
int idx
Definition: setup.py:197
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
Definition: z80asm.h:102
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_hash_t * names
Definition: zipint.h:300
zip_dirent_t * _zip_dirent_clone(const zip_dirent_t *sde)
Definition: zip_dirent.c:215
void _zip_dirent_free(zip_dirent_t *zde)
Definition: zip_dirent.c:258
bool _zip_hash_delete(zip_hash_t *hash, const zip_uint8_t *name, zip_error_t *error)
Definition: zip_hash.c:264
bool _zip_hash_add(zip_hash_t *hash, const zip_uint8_t *name, zip_uint64_t index, zip_flags_t flags, zip_error_t *error)
Definition: zip_hash.c:205
zip_int64_t _zip_name_locate(zip_t *za, const char *fname, zip_flags_t flags, zip_error_t *error)
zip_string_t * _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error)
Definition: zip_string.c:121
void _zip_string_free(zip_string_t *s)
Definition: zip_string.c:69
const zip_uint8_t * _zip_string_get(zip_string_t *string, zip_uint32_t *lenp, zip_flags_t flags, zip_error_t *error)
Definition: zip_string.c:80
int _zip_string_equal(const zip_string_t *a, const zip_string_t *b)
Definition: zip_string.c:55
zip_encoding_type_t _zip_guess_encoding(zip_string_t *str, zip_encoding_type_t expected_encoding)
Definition: zip_utf-8.c:100
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint8_t zip_uint8_t
Definition: zipconf.h:33
uint16_t zip_uint16_t
Definition: zipconf.h:35
int64_t zip_int64_t
Definition: zipconf.h:38
#define ZIP_IS_RDONLY(za)
Definition: zipint.h:479
#define ZIP_DIRENT_FILENAME
Definition: zipint.h:317
#define ZIP_FL_ENCODING_ALL
Definition: zipint.h:249
@ ZIP_ENCODING_UTF8_GUESSED
Definition: zipint.h:257
@ ZIP_ENCODING_UTF8_KNOWN
Definition: zipint.h:256
@ ZIP_ENCODING_UNKNOWN
Definition: zipint.h:254
zip_t * za
Definition: ziptool.c:79

References _zip_dirent_clone(), _zip_dirent_free(), _zip_guess_encoding(), _zip_hash_add(), _zip_hash_delete(), _zip_name_locate(), _zip_string_equal(), _zip_string_free(), _zip_string_get(), _zip_string_new(), e, zip::entry, zip::error, flags, i, setup::idx, zip::names, zip::nentry, NULL, cmd_descs_generate::str, za, ZIP_DIRENT_FILENAME, ZIP_ENCODING_UNKNOWN, ZIP_ENCODING_UTF8_GUESSED, ZIP_ENCODING_UTF8_KNOWN, ZIP_ER_EXISTS, ZIP_ER_INVAL, ZIP_ER_MEMORY, ZIP_ER_RDONLY, zip_error_set(), ZIP_FL_ENC_GUESS, ZIP_FL_ENCODING_ALL, and ZIP_IS_RDONLY.

Referenced by _zip_file_replace(), and zip_file_rename().