Rizin
unix-like reverse engineering framework and cli tools
zip_source_file_stdio_named.c File Reference
#include "zipint.h"
#include "zip_source_file.h"
#include "zip_source_file_stdio.h"
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>

Go to the source code of this file.

Functions

static int create_temp_file (zip_source_file_context_t *ctx, bool create_file)
 
static zip_int64_t _zip_stdio_op_commit_write (zip_source_file_context_t *ctx)
 
static zip_int64_t _zip_stdio_op_create_temp_output (zip_source_file_context_t *ctx)
 
static bool _zip_stdio_op_open (zip_source_file_context_t *ctx)
 
static zip_int64_t _zip_stdio_op_remove (zip_source_file_context_t *ctx)
 
static void _zip_stdio_op_rollback_write (zip_source_file_context_t *ctx)
 
static char * _zip_stdio_op_strdup (zip_source_file_context_t *ctx, const char *string)
 
static zip_int64_t _zip_stdio_op_write (zip_source_file_context_t *ctx, const void *data, zip_uint64_t len)
 
ZIP_EXTERN zip_source_tzip_source_file (zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t len)
 
ZIP_EXTERN zip_source_tzip_source_file_create (const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error)
 

Variables

static zip_source_file_operations_t ops_stdio_named
 

Function Documentation

◆ _zip_stdio_op_commit_write()

static zip_int64_t _zip_stdio_op_commit_write ( zip_source_file_context_t ctx)
static

Definition at line 113 of file zip_source_file_stdio_named.c.

113  {
114  if (fclose(ctx->fout) < 0) {
115  zip_error_set(&ctx->error, ZIP_ER_WRITE, errno);
116  return -1;
117  }
118  if (rename(ctx->tmpname, ctx->fname) < 0) {
120  return -1;
121  }
122 
123  return 0;
124 }
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_WRITE
Definition: zip.h:111
#define ZIP_ER_RENAME
Definition: zip.h:107
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync rename
Definition: sflib.h:69
zip_error_t * error

References ctx::error, rename, ZIP_ER_RENAME, ZIP_ER_WRITE, and zip_error_set().

◆ _zip_stdio_op_create_temp_output()

static zip_int64_t _zip_stdio_op_create_temp_output ( zip_source_file_context_t ctx)
static

Definition at line 128 of file zip_source_file_stdio_named.c.

128  {
129  int fd = create_temp_file(ctx, true);
130 
131  if (fd < 0) {
132  return -1;
133  }
134 
135  if ((ctx->fout = fdopen(fd, "r+b")) == NULL) {
137  close(fd);
138  (void)remove(ctx->tmpname);
139  free(ctx->tmpname);
140  ctx->tmpname = NULL;
141  return -1;
142  }
143 
144  return 0;
145 }
#define NULL
Definition: cris-opc.c:27
static static fork const void static count close
Definition: sflib.h:33
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define ZIP_ER_TMPOPEN
Definition: zip.h:117
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int create_temp_file(zip_source_file_context_t *ctx, bool create_file)

References close, create_temp_file(), ctx::error, fd, free(), NULL, ZIP_ER_TMPOPEN, and zip_error_set().

◆ _zip_stdio_op_open()

static bool _zip_stdio_op_open ( zip_source_file_context_t ctx)
static

Definition at line 241 of file zip_source_file_stdio_named.c.

241  {
242  if ((ctx->f = _zip_fopen_close_on_exec(ctx->fname, false)) == NULL) {
243  zip_error_set(&ctx->error, ZIP_ER_OPEN, errno);
244  return false;
245  }
246  return true;
247 }
#define ZIP_ER_OPEN
Definition: zip.h:116
FILE * _zip_fopen_close_on_exec(const char *name, bool writeable)

References _zip_fopen_close_on_exec(), ctx::error, NULL, ZIP_ER_OPEN, and zip_error_set().

◆ _zip_stdio_op_remove()

static zip_int64_t _zip_stdio_op_remove ( zip_source_file_context_t ctx)
static

Definition at line 251 of file zip_source_file_stdio_named.c.

251  {
252  if (remove(ctx->fname) < 0) {
254  return -1;
255  }
256  return 0;
257 }
#define ZIP_ER_REMOVE
Definition: zip.h:127

References ctx::error, ZIP_ER_REMOVE, and zip_error_set().

◆ _zip_stdio_op_rollback_write()

static void _zip_stdio_op_rollback_write ( zip_source_file_context_t ctx)
static

Definition at line 261 of file zip_source_file_stdio_named.c.

261  {
262  if (ctx->fout) {
263  fclose(ctx->fout);
264  }
265  (void)remove(ctx->tmpname);
266 }

◆ _zip_stdio_op_strdup()

static char * _zip_stdio_op_strdup ( zip_source_file_context_t ctx,
const char *  string 
)
static

Definition at line 269 of file zip_source_file_stdio_named.c.

269  {
270  return strdup(string);
271 }
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")

References strdup().

◆ _zip_stdio_op_write()

static zip_int64_t _zip_stdio_op_write ( zip_source_file_context_t ctx,
const void *  data,
zip_uint64_t  len 
)
static

Definition at line 275 of file zip_source_file_stdio_named.c.

275  {
276  size_t ret;
277 
278  clearerr((FILE *)ctx->fout);
279  ret = fwrite(data, 1, len, (FILE *)ctx->fout);
280  if (ret != len || ferror((FILE *)ctx->fout)) {
281  zip_error_set(&ctx->error, ZIP_ER_WRITE, errno);
282  return -1;
283  }
284 
285  return (zip_int64_t)ret;
286 }
size_t len
Definition: 6502dis.c:15
string FILE
Definition: benchmark.py:21
int64_t zip_int64_t
Definition: zipconf.h:38

References ctx::error, benchmark::FILE, len, ZIP_ER_WRITE, and zip_error_set().

◆ create_temp_file()

static int create_temp_file ( zip_source_file_context_t ctx,
bool  create_file 
)
static

Definition at line 289 of file zip_source_file_stdio_named.c.

289  {
290  char *temp;
291  int mode;
292  struct stat st;
293  int fd;
294  char *start, *end;
295 
296  if (stat(ctx->fname, &st) == 0) {
297  mode = st.st_mode;
298  }
299  else {
300  mode = -1;
301  }
302 
303  if ((temp = (char *)malloc(strlen(ctx->fname) + 13)) == NULL) {
305  return -1;
306  }
307  sprintf(temp, "%s.XXXXXX.part", ctx->fname);
308  end = temp + strlen(temp) - 5;
309  start = end - 6;
310 
311  for (;;) {
313  char *xs = start;
314 
315  while (xs < end) {
316  char digit = value % 36;
317  if (digit < 10) {
318  *(xs++) = digit + '0';
319  }
320  else {
321  *(xs++) = digit - 10 + 'a';
322  }
323  value /= 36;
324  }
325 
326  if (create_file) {
327  if ((fd = open(temp, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, mode == -1 ? 0666 : (mode_t)mode)) >= 0) {
328  if (mode != -1) {
329  /* open() honors umask(), which we don't want in this case */
330 #ifdef HAVE_FCHMOD
331  (void)fchmod(fd, (mode_t)mode);
332 #else
333  (void)chmod(temp, (mode_t)mode);
334 #endif
335  }
336  break;
337  }
338  if (errno != EEXIST) {
340  free(temp);
341  return -1;
342  }
343  }
344  else {
345  if (stat(temp, &st) < 0) {
346  if (errno == ENOENT) {
347  break;
348  }
349  else {
351  free(temp);
352  return -1;
353  }
354  }
355  }
356  }
357 
358  ctx->tmpname = temp;
359 
360  return create_file ? fd : 0;
361 }
static int value
Definition: cmd_api.c:93
#define O_CLOEXEC
Definition: compat.h:80
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 fchmod
Definition: sflib.h:84
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
static static fork const void static count static fd const char const char static newpath const char static path chmod
Definition: sflib.h:35
const char int mode
Definition: ioapi.h:137
sprintf
Definition: kernel.h:365
#define ZIP_ER_MEMORY
Definition: zip.h:119
void * malloc(size_t size)
Definition: malloc.c:123
static stat
Definition: sflib.h:131
#define ENOENT
Definition: sftypes.h:112
#define EEXIST
Definition: sftypes.h:127
#define O_CREAT
Definition: sftypes.h:489
int mode_t
Definition: sftypes.h:42
#define O_EXCL
Definition: sftypes.h:490
#define O_RDWR
Definition: sftypes.h:488
Definition: sftypes.h:80
zip_uint32_t zip_random_uint32(void)
uint32_t zip_uint32_t
Definition: zipconf.h:37

References chmod, EEXIST, test_evm::end, ENOENT, ctx::error, fchmod, fd, free(), malloc(), NULL, O_CLOEXEC, O_CREAT, O_EXCL, O_RDWR, sprintf, start, stat, value, ZIP_ER_MEMORY, ZIP_ER_TMPOPEN, zip_error_set(), and zip_random_uint32().

Referenced by _zip_stdio_op_create_temp_output().

◆ zip_source_file()

ZIP_EXTERN zip_source_t* zip_source_file ( zip_t za,
const char *  fname,
zip_uint64_t  start,
zip_int64_t  len 
)

Definition at line 93 of file zip_source_file_stdio_named.c.

93  {
94  if (za == NULL)
95  return NULL;
96 
98 }
zip_error_t error
Definition: zipint.h:281
ZIP_EXTERN zip_source_t * zip_source_file_create(const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error)
zip_t * za
Definition: ziptool.c:79

◆ zip_source_file_create()

ZIP_EXTERN zip_source_t* zip_source_file_create ( const char *  fname,
zip_uint64_t  start,
zip_int64_t  length,
zip_error_t error 
)

Definition at line 102 of file zip_source_file_stdio_named.c.

102  {
103  if (fname == NULL || length < -1) {
105  return NULL;
106  }
107 
109 }
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 length
Definition: sflib.h:133
#define ZIP_ER_INVAL
Definition: zip.h:123
void error(const char *msg)
Definition: untgz.c:593
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)
static zip_source_file_operations_t ops_stdio_named

Referenced by zip_source_file().

Variable Documentation

◆ ops_stdio_named

zip_source_file_operations_t ops_stdio_named
static
Initial value:
= {
}
void _zip_stdio_op_close(zip_source_file_context_t *ctx)
zip_int64_t _zip_stdio_op_read(zip_source_file_context_t *ctx, void *buf, zip_uint64_t len)
zip_int64_t _zip_stdio_op_tell(zip_source_file_context_t *ctx, void *f)
bool _zip_stdio_op_stat(zip_source_file_context_t *ctx, zip_source_file_stat_t *st)
bool _zip_stdio_op_seek(zip_source_file_context_t *ctx, void *f, zip_int64_t offset, int whence)
static void _zip_stdio_op_rollback_write(zip_source_file_context_t *ctx)
static char * _zip_stdio_op_strdup(zip_source_file_context_t *ctx, const char *string)
static zip_int64_t _zip_stdio_op_commit_write(zip_source_file_context_t *ctx)
static zip_int64_t _zip_stdio_op_remove(zip_source_file_context_t *ctx)
static zip_int64_t _zip_stdio_op_create_temp_output(zip_source_file_context_t *ctx)
static bool _zip_stdio_op_open(zip_source_file_context_t *ctx)
static zip_int64_t _zip_stdio_op_write(zip_source_file_context_t *ctx, const void *data, zip_uint64_t len)

Definition at line 71 of file zip_source_file_stdio_named.c.

Referenced by zip_source_file_create().