Rizin
unix-like reverse engineering framework and cli tools
zip_source_file.h File Reference

Go to the source code of this file.

Classes

struct  zip_source_file_stat
 
struct  zip_source_file_context
 
struct  zip_source_file_operations
 

Typedefs

typedef struct zip_source_file_context zip_source_file_context_t
 
typedef struct zip_source_file_operations zip_source_file_operations_t
 
typedef struct zip_source_file_stat zip_source_file_stat_t
 

Functions

zip_source_tzip_source_file_common_new (const char *fname, void *file, zip_uint64_t start, zip_int64_t len, const zip_stat_t *st, zip_source_file_operations_t *ops, void *ops_userdata, zip_error_t *error)
 

Typedef Documentation

◆ zip_source_file_context_t

Definition at line 1 of file zip_source_file.h.

◆ zip_source_file_operations_t

Definition at line 1 of file zip_source_file.h.

◆ zip_source_file_stat_t

Definition at line 1 of file zip_source_file.h.

Function Documentation

◆ zip_source_file_common_new()

zip_source_t* zip_source_file_common_new ( const char *  fname,
void *  file,
zip_uint64_t  start,
zip_int64_t  len,
const zip_stat_t st,
zip_source_file_operations_t ops,
void *  ops_userdata,
zip_error_t error 
)

Definition at line 53 of file zip_source_file_common.c.

53  {
55  zip_source_t *zs;
57 
58  if (ops == NULL) {
60  return NULL;
61  }
62 
63  if (ops->close == NULL || ops->read == NULL || ops->seek == NULL || ops->stat == NULL) {
65  return NULL;
66  }
67 
68  if (ops->write != NULL && (ops->commit_write == NULL || ops->create_temp_output == NULL || ops->remove == NULL || ops->rollback_write == NULL || ops->tell == NULL)) {
70  return NULL;
71  }
72 
73  if (fname != NULL) {
74  if (ops->open == NULL || ops->string_duplicate == NULL) {
76  return NULL;
77  }
78  }
79  else if (file == NULL) {
81  return NULL;
82  }
83 
84  if (len < 0) {
85  len = 0;
86  }
87 
90  return NULL;
91  }
92 
95  return NULL;
96  }
97 
98  ctx->ops = ops;
99  ctx->ops_userdata = ops_userdata;
100  ctx->fname = NULL;
101  if (fname) {
102  if ((ctx->fname = ops->string_duplicate(ctx, fname)) == NULL) {
104  free(ctx);
105  return NULL;
106  }
107  }
108  ctx->f = file;
109  ctx->start = start;
110  ctx->len = (zip_uint64_t)len;
111  if (st) {
112  memcpy(&ctx->st, st, sizeof(ctx->st));
113  ctx->st.name = NULL;
114  ctx->st.valid &= ~ZIP_STAT_NAME;
115  }
116  else {
117  zip_stat_init(&ctx->st);
118  }
119 
120  if (ctx->len > 0) {
121  ctx->st.size = ctx->len;
122  ctx->st.valid |= ZIP_STAT_SIZE;
123  }
124 
125  zip_error_init(&ctx->stat_error);
126 
127  ctx->tmpname = NULL;
128  ctx->fout = NULL;
129 
131  zip_file_attributes_init(&ctx->attributes);
132 
134 
136  if (!ops->stat(ctx, &sb)) {
138  free(ctx->fname);
139  free(ctx);
140  return NULL;
141  }
142 
143  if (!sb.exists) {
144  if (ctx->fname && ctx->start == 0 && ctx->len == 0 && ops->write != NULL) {
145  ctx->supports = ZIP_SOURCE_SUPPORTS_WRITABLE;
146  /* zip_open_from_source checks for this to detect non-existing files */
147  zip_error_set(&ctx->stat_error, ZIP_ER_READ, ENOENT);
148  }
149  else {
150  zip_error_set(&ctx->stat_error, ZIP_ER_READ, ENOENT);
151  free(ctx->fname);
152  free(ctx);
153  return NULL;
154  }
155  }
156  else {
157  if ((ctx->st.valid & ZIP_STAT_MTIME) == 0) {
158  ctx->st.mtime = sb.mtime;
159  ctx->st.valid |= ZIP_STAT_MTIME;
160  }
161  if (sb.regular_file) {
162  ctx->supports = ZIP_SOURCE_SUPPORTS_SEEKABLE;
163 
164  if (ctx->start + ctx->len > sb.size) {
166  free(ctx->fname);
167  free(ctx);
168  return NULL;
169  }
170 
171  if (ctx->len == 0) {
172  ctx->len = sb.size - ctx->start;
173  ctx->st.size = ctx->len;
174  ctx->st.valid |= ZIP_STAT_SIZE;
175 
176  /* when using a partial file, don't allow writing */
177  if (ctx->fname && start == 0 && ops->write != NULL) {
178  ctx->supports = ZIP_SOURCE_SUPPORTS_WRITABLE;
179  }
180  }
181  }
182 
184  }
185 
187  if (ops->create_temp_output_cloning != NULL) {
190  }
191  }
192 
194  free(ctx->fname);
195  free(ctx);
196  return NULL;
197  }
198 
199  return zs;
200 }
size_t len
Definition: 6502dis.c:15
static struct @29 ops[]
static SblHeader sb
Definition: bin_mbn.c:26
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define ZIP_ER_INTERNAL
Definition: zip.h:125
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_STAT_NAME
Definition: zip.h:290
#define ZIP_SOURCE_SUPPORTS_SEEKABLE
Definition: zip.h:256
#define ZIP_SOURCE_SUPPORTS_READABLE
Definition: zip.h:249
ZIP_EXTERN void zip_file_attributes_init(zip_file_attributes_t *_Nonnull)
#define ZIP_SOURCE_SUPPORTS_WRITABLE
Definition: zip.h:261
#define ZIP_ER_MEMORY
Definition: zip.h:119
#define ZIP_STAT_SIZE
Definition: zip.h:292
ZIP_EXTERN void zip_stat_init(zip_stat_t *_Nonnull)
Definition: zip_stat_init.c:40
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
#define ZIP_SOURCE_MAKE_COMMAND_BITMASK(cmd)
Definition: zip.h:243
@ ZIP_SOURCE_GET_FILE_ATTRIBUTES
Definition: zip.h:239
@ ZIP_SOURCE_ACCEPT_EMPTY
Definition: zip.h:238
@ ZIP_SOURCE_BEGIN_WRITE_CLONING
Definition: zip.h:237
@ ZIP_SOURCE_SUPPORTS
Definition: zip.h:234
@ ZIP_SOURCE_TELL
Definition: zip.h:227
@ ZIP_SOURCE_BEGIN_WRITE
Definition: zip.h:228
ZIP_EXTERN zip_source_t *_Nullable zip_source_function_create(zip_source_callback _Nonnull, void *_Nullable, zip_error_t *_Nullable)
#define ZIP_ER_INVAL
Definition: zip.h:123
ZIP_EXTERN zip_int64_t zip_source_make_command_bitmap(zip_source_cmd_t,...)
#define ZIP_STAT_MTIME
Definition: zip.h:294
#define ZIP_ER_READ
Definition: zip.h:110
void * malloc(size_t size)
Definition: malloc.c:123
#define ENOENT
Definition: sftypes.h:112
zip_error_t * error
Definition: gzappend.c:170
void error(const char *msg)
Definition: untgz.c:593
static int file
Definition: z80asm.c:58
void _zip_error_copy(zip_error_t *dst, const zip_error_t *src)
Definition: zip_error.c:102
static zip_int64_t read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd)
static void zip_source_file_stat_init(zip_source_file_stat_t *st)
uint64_t zip_uint64_t
Definition: zipconf.h:39
#define ZIP_INT64_MAX
Definition: zipconf.h:54

References _zip_error_copy(), ENOENT, ctx::error, error(), file, create_tags_rz::fname, free(), len, malloc(), memcpy(), NULL, ops, read_file(), sb, start, ZIP_ER_INTERNAL, ZIP_ER_INVAL, ZIP_ER_MEMORY, ZIP_ER_READ, zip_error_init(), zip_error_set(), zip_file_attributes_init(), ZIP_INT64_MAX, ZIP_SOURCE_ACCEPT_EMPTY, ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_BEGIN_WRITE_CLONING, zip_source_file_stat_init(), zip_source_function_create(), ZIP_SOURCE_GET_FILE_ATTRIBUTES, zip_source_make_command_bitmap(), ZIP_SOURCE_MAKE_COMMAND_BITMASK, ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_SUPPORTS_READABLE, ZIP_SOURCE_SUPPORTS_SEEKABLE, ZIP_SOURCE_SUPPORTS_WRITABLE, ZIP_SOURCE_TELL, zip_stat_init(), ZIP_STAT_MTIME, ZIP_STAT_NAME, and ZIP_STAT_SIZE.

Referenced by zip_source_file_create(), zip_source_filep_create(), zip_source_win32a_create(), zip_source_win32handle_create(), and zip_source_win32w_create().