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

Go to the source code of this file.

Functions

ZIP_EXTERN zip_source_tzip_source_filep (zip_t *za, FILE *file, zip_uint64_t start, zip_int64_t len)
 
ZIP_EXTERN zip_source_tzip_source_filep_create (FILE *file, zip_uint64_t start, zip_int64_t length, zip_error_t *error)
 
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)
 
bool _zip_stdio_op_seek (zip_source_file_context_t *ctx, void *f, zip_int64_t offset, int whence)
 
bool _zip_stdio_op_stat (zip_source_file_context_t *ctx, zip_source_file_stat_t *st)
 
zip_int64_t _zip_stdio_op_tell (zip_source_file_context_t *ctx, void *f)
 
FILE * _zip_fopen_close_on_exec (const char *name, bool writeable)
 

Variables

static zip_source_file_operations_t ops_stdio_read
 

Function Documentation

◆ _zip_fopen_close_on_exec()

FILE* _zip_fopen_close_on_exec ( const char *  name,
bool  writeable 
)

Definition at line 187 of file zip_source_file_stdio.c.

187  {
188  int fd;
189  int flags;
190  FILE *fp;
191 
192  flags = O_CLOEXEC;
193  if (writeable) {
194  flags |= O_RDWR;
195  }
196  else {
197  flags |= O_RDONLY;
198  }
199 
200  /* mode argument needed on Windows */
201  if ((fd = open(name, flags, 0666)) < 0) {
202  return NULL;
203  }
204  if ((fp = fdopen(fd, writeable ? "r+b" : "rb")) == NULL) {
205  return NULL;
206  }
207  return fp;
208 }
#define O_CLOEXEC
Definition: compat.h:80
#define NULL
Definition: cris-opc.c:27
string FILE
Definition: benchmark.py:21
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define O_RDONLY
Definition: sftypes.h:486
#define O_RDWR
Definition: sftypes.h:488
Definition: z80asm.h:102
static const z80_opcode fd[]
Definition: z80_tab.h:997

References fd, benchmark::FILE, flags, NULL, O_CLOEXEC, O_RDONLY, and O_RDWR.

Referenced by _zip_stdio_op_open().

◆ _zip_stdio_op_close()

void _zip_stdio_op_close ( zip_source_file_context_t ctx)

Definition at line 91 of file zip_source_file_stdio.c.

91  {
92  fclose((FILE *)ctx->f);
93 }

References benchmark::FILE.

◆ _zip_stdio_op_read()

zip_int64_t _zip_stdio_op_read ( zip_source_file_context_t ctx,
void *  buf,
zip_uint64_t  len 
)

Definition at line 97 of file zip_source_file_stdio.c.

97  {
98  size_t i;
99  if (len > SIZE_MAX) {
100  len = SIZE_MAX;
101  }
102 
103  if ((i = fread(buf, 1, (size_t)len, ctx->f)) == 0) {
104  if (ferror((FILE *)ctx->f)) {
105  zip_error_set(&ctx->error, ZIP_ER_READ, errno);
106  return -1;
107  }
108  }
109 
110  return (zip_int64_t)i;
111 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
voidpf void * buf
Definition: ioapi.h:138
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_READ
Definition: zip.h:110
#define SIZE_MAX
zip_error_t * error
int64_t zip_int64_t
Definition: zipconf.h:38

References ctx::error, benchmark::FILE, i, len, SIZE_MAX, ZIP_ER_READ, and zip_error_set().

◆ _zip_stdio_op_seek()

bool _zip_stdio_op_seek ( zip_source_file_context_t ctx,
void *  f,
zip_int64_t  offset,
int  whence 
)

Definition at line 115 of file zip_source_file_stdio.c.

115  {
116 #if ZIP_FSEEK_MAX > ZIP_INT64_MAX
119  return false;
120  }
121 #endif
122 
123  if (fseeko((FILE *)f, (off_t)offset, whence) < 0) {
124  zip_error_set(&ctx->error, ZIP_ER_SEEK, errno);
125  return false;
126  }
127  return true;
128 }
#define ZIP_FSEEK_MIN
Definition: compat.h:155
#define fseeko(s, o, w)
Definition: compat.h:121
#define ZIP_FSEEK_MAX
Definition: compat.h:154
#define EOVERFLOW
Definition: compat.h:75
voidpf uLong offset
Definition: ioapi.h:144
#define ZIP_ER_SEEK
Definition: zip.h:109
int off_t
Definition: sftypes.h:41
#define f(i)
Definition: sha256.c:46

References EOVERFLOW, ctx::error, f, benchmark::FILE, fseeko, ZIP_ER_SEEK, zip_error_set(), ZIP_FSEEK_MAX, and ZIP_FSEEK_MIN.

◆ _zip_stdio_op_stat()

bool _zip_stdio_op_stat ( zip_source_file_context_t ctx,
zip_source_file_stat_t st 
)

Definition at line 132 of file zip_source_file_stdio.c.

132  {
133  struct stat sb;
134 
135  int ret;
136 
137  if (ctx->fname) {
138  ret = stat(ctx->fname, &sb);
139  }
140  else {
141  ret = fstat(fileno((FILE *)ctx->f), &sb);
142  }
143 
144  if (ret < 0) {
145  if (errno == ENOENT) {
146  st->exists = false;
147  return true;
148  }
149  zip_error_set(&ctx->error, ZIP_ER_READ, errno);
150  return false;
151  }
152 
153  st->size = (zip_uint64_t)sb.st_size;
154  st->mtime = sb.st_mtime;
155 
156  st->regular_file = S_ISREG(sb.st_mode);
157  st->exists = true;
158 
159  /* We're using UNIX file API, even on Windows; thus, we supply external file attributes with Unix values. */
160  /* TODO: This could be improved on Windows by providing Windows-specific file attributes */
162  ctx->attributes.host_system = ZIP_OPSYS_UNIX;
163  ctx->attributes.external_file_attributes = (((zip_uint32_t)sb.st_mode) << 16) | ((sb.st_mode & S_IWUSR) ? 0 : 1);
164 
165  return true;
166 }
static SblHeader sb
Definition: bin_mbn.c:26
#define S_ISREG(mode)
Definition: compat.h:191
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 fstat
Definition: sflib.h:107
#define ZIP_OPSYS_UNIX
Definition: zip.h:198
#define ZIP_FILE_ATTRIBUTES_HOST_SYSTEM
Definition: zip.h:329
#define ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES
Definition: zip.h:332
static stat
Definition: sflib.h:131
#define ENOENT
Definition: sftypes.h:112
Definition: sftypes.h:80
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint32_t zip_uint32_t
Definition: zipconf.h:37

References ENOENT, ctx::error, zip_source_file_stat::exists, benchmark::FILE, fstat, zip_source_file_stat::mtime, zip_source_file_stat::regular_file, S_ISREG, sb, zip_source_file_stat::size, stat, ZIP_ER_READ, zip_error_set(), ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES, ZIP_FILE_ATTRIBUTES_HOST_SYSTEM, and ZIP_OPSYS_UNIX.

◆ _zip_stdio_op_tell()

zip_int64_t _zip_stdio_op_tell ( zip_source_file_context_t ctx,
void *  f 
)

Definition at line 170 of file zip_source_file_stdio.c.

170  {
171  off_t offset = ftello((FILE *)f);
172 
173  if (offset < 0) {
174  zip_error_set(&ctx->error, ZIP_ER_SEEK, errno);
175  }
176 
177  return offset;
178 }
#define ftello(s)
Definition: compat.h:125

References ctx::error, f, benchmark::FILE, ftello, ZIP_ER_SEEK, and zip_error_set().

◆ zip_source_filep()

ZIP_EXTERN zip_source_t* zip_source_filep ( zip_t za,
FILE *  file,
zip_uint64_t  start,
zip_int64_t  len 
)

Definition at line 70 of file zip_source_file_stdio.c.

70  {
71  if (za == NULL) {
72  return NULL;
73  }
74 
76 }
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
Definition: gzappend.c:170
zip_error_t error
Definition: zipint.h:281
ZIP_EXTERN zip_source_t * zip_source_filep_create(FILE *file, zip_uint64_t start, zip_int64_t length, zip_error_t *error)
zip_t * za
Definition: ziptool.c:79

References zip::error, len, NULL, start, za, and zip_source_filep_create().

Referenced by add_file(), and main().

◆ zip_source_filep_create()

ZIP_EXTERN zip_source_t* zip_source_filep_create ( FILE *  file,
zip_uint64_t  start,
zip_int64_t  length,
zip_error_t error 
)

Definition at line 80 of file zip_source_file_stdio.c.

80  {
81  if (file == NULL || length < -1) {
83  return NULL;
84  }
85 
87 }
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_read

References error(), length, NULL, ops_stdio_read, start, ZIP_ER_INVAL, zip_error_set(), and zip_source_file_common_new().

Referenced by zip_fdopen(), and zip_source_filep().

Variable Documentation

◆ ops_stdio_read

zip_source_file_operations_t ops_stdio_read
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)

Definition at line 51 of file zip_source_file_stdio.c.

Referenced by zip_source_filep_create().