Rizin
unix-like reverse engineering framework and cli tools
source_hole.c File Reference
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zip.h"

Go to the source code of this file.

Classes

struct  buffer
 
struct  hole
 

Macros

#define EFTYPE   EINVAL
 
#define MY_MIN(a, b)   ((a) < (b) ? (a) : (b))
 
#define FRAGMENT_SIZE   (8 * 1024)
 
#define MARK_BEGIN   "NiH0"
 
#define MARK_DATA   "NiH1"
 
#define MARK_NUL   "NiH2"
 

Typedefs

typedef struct buffer buffer_t
 
typedef struct hole hole_t
 

Functions

zip_source_tsource_hole_create (const char *, int flags, zip_error_t *)
 
static void buffer_free (buffer_t *buffer)
 
static buffer_tbuffer_from_file (const char *fname, int flags, zip_error_t *error)
 
static buffer_tbuffer_new (void)
 
static zip_int64_t buffer_read (buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length, zip_error_t *error)
 
static int buffer_read_file (buffer_t *buffer, FILE *f, zip_error_t *error)
 
static zip_int64_t buffer_seek (buffer_t *buffer, void *data, zip_uint64_t length, zip_error_t *error)
 
static int buffer_to_file (buffer_t *buffer, const char *fname, zip_error_t *error)
 
static zip_int64_t buffer_write (buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error)
 
static zip_uint64_t get_u64 (const zip_uint8_t *b)
 
static int only_nul (const zip_uint8_t *data, zip_uint64_t length)
 
static int write_nuls (zip_uint64_t n, FILE *f)
 
static int write_u64 (zip_uint64_t u64, FILE *f)
 
static hole_thole_new (const char *fname, int flags, zip_error_t *error)
 
static zip_int64_t source_hole_cb (void *ud, void *data, zip_uint64_t length, zip_source_cmd_t command)
 
static void hole_free (hole_t *hole)
 

Macro Definition Documentation

◆ EFTYPE

#define EFTYPE   EINVAL

Definition at line 47 of file source_hole.c.

◆ FRAGMENT_SIZE

#define FRAGMENT_SIZE   (8 * 1024)

Definition at line 53 of file source_hole.c.

◆ MARK_BEGIN

#define MARK_BEGIN   "NiH0"

Definition at line 55 of file source_hole.c.

◆ MARK_DATA

#define MARK_DATA   "NiH1"

Definition at line 56 of file source_hole.c.

◆ MARK_NUL

#define MARK_NUL   "NiH2"

Definition at line 57 of file source_hole.c.

◆ MY_MIN

#define MY_MIN (   a,
  b 
)    ((a) < (b) ? (a) : (b))

Definition at line 51 of file source_hole.c.

Typedef Documentation

◆ buffer_t

typedef struct buffer buffer_t

◆ hole_t

typedef struct hole hole_t

Function Documentation

◆ buffer_free()

static void buffer_free ( buffer_t buffer)
static

Definition at line 105 of file source_hole.c.

105  {
106  zip_uint64_t i;
107 
108  if (buffer == NULL) {
109  return;
110  }
111 
112  if (buffer->fragment) {
113  for (i = 0; i < buffer->nfragments; i++) {
114  free(buffer->fragment[i]);
115  }
116  free(buffer->fragment);
117  }
118  free(buffer);
119 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
Definition: buffer.h:15
zip_uint64_t nfragments
zip_uint8_t ** fragment
Definition: source_hole.c:62
uint64_t zip_uint64_t
Definition: zipconf.h:39

References buffer::fragment, free(), i, buffer::nfragments, and NULL.

Referenced by buffer_from_file(), hole_free(), and source_hole_cb().

◆ buffer_from_file()

static buffer_t * buffer_from_file ( const char *  fname,
int  flags,
zip_error_t error 
)
static

Definition at line 123 of file source_hole.c.

123  {
124  buffer_t *buffer;
125  FILE *f;
126 
127  if ((buffer = buffer_new()) == NULL) {
129  return NULL;
130  }
131 
132  if ((flags & ZIP_TRUNCATE) == 0) {
133  if ((f = fopen(fname, "rb")) == NULL) {
134  if (!(errno == ENOENT && (flags & ZIP_CREATE))) {
136  return NULL;
137  }
138  }
139  else {
140  if (buffer_read_file(buffer, f, error) < 0) {
142  fclose(f);
143  return NULL;
144  }
145  fclose(f);
146  }
147  }
148 
149  return buffer;
150 }
struct buffer buffer
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_CREATE
Definition: zip.h:67
#define ZIP_TRUNCATE
Definition: zip.h:70
string FILE
Definition: benchmark.py:21
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define ENOENT
Definition: sftypes.h:112
#define f(i)
Definition: sha256.c:46
static buffer_t * buffer_new(void)
Definition: source_hole.c:154
static int buffer_read_file(buffer_t *buffer, FILE *f, zip_error_t *error)
Definition: source_hole.c:208
static void buffer_free(buffer_t *buffer)
Definition: source_hole.c:105
void error(const char *msg)
Definition: untgz.c:593

References buffer_free(), buffer_new(), buffer_read_file(), ENOENT, error(), f, benchmark::FILE, flags, create_tags_rz::fname, NULL, ZIP_CREATE, ZIP_ER_MEMORY, zip_error_set(), and ZIP_TRUNCATE.

Referenced by hole_new().

◆ buffer_new()

static buffer_t * buffer_new ( void  )
static

Definition at line 154 of file source_hole.c.

154  {
155  buffer_t *buffer;
156 
157  if ((buffer = (buffer_t *)malloc(sizeof(*buffer))) == NULL) {
158  return NULL;
159  }
160 
161  buffer->fragment = NULL;
162  buffer->nfragments = 0;
164  buffer->size = 0;
165  buffer->offset = 0;
166 
167  return buffer;
168 }
void * malloc(size_t size)
Definition: malloc.c:123
#define FRAGMENT_SIZE
Definition: source_hole.c:53
zip_uint64_t offset
zip_uint64_t size
zip_uint64_t fragment_size
Definition: source_hole.c:61

References buffer::fragment, FRAGMENT_SIZE, buffer::fragment_size, malloc(), buffer::nfragments, NULL, buffer::offset, and buffer::size.

Referenced by buffer_from_file(), and source_hole_cb().

◆ buffer_read()

static zip_int64_t buffer_read ( buffer_t buffer,
zip_uint8_t data,
zip_uint64_t  length,
zip_error_t error 
)
static

Definition at line 172 of file source_hole.c.

172  {
173  zip_uint64_t n, i, fragment_offset;
174 
176 
177  if (length == 0) {
178  return 0;
179  }
180  if (length > ZIP_INT64_MAX) {
181  return -1;
182  }
183 
185  fragment_offset = buffer->offset % buffer->fragment_size;
186  n = 0;
187  while (n < length) {
188  zip_uint64_t left = MY_MIN(length - n, buffer->fragment_size - fragment_offset);
189 
190  if (buffer->fragment[i]) {
191  memcpy(data + n, buffer->fragment[i] + fragment_offset, left);
192  }
193  else {
194  memset(data + n, 0, left);
195  }
196 
197  n += left;
198  i++;
199  fragment_offset = 0;
200  }
201 
202  buffer->offset += n;
203  return (zip_int64_t)n;
204 }
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
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
int n
Definition: mipsasm.c:19
#define MY_MIN(a, b)
Definition: source_hole.c:51
#define ZIP_INT64_MAX
Definition: zipconf.h:54
int64_t zip_int64_t
Definition: zipconf.h:38

References buffer::fragment, buffer::fragment_size, i, length, memcpy(), memset(), MY_MIN, n, buffer::offset, buffer::size, and ZIP_INT64_MAX.

Referenced by source_hole_cb().

◆ buffer_read_file()

static int buffer_read_file ( buffer_t buffer,
FILE *  f,
zip_error_t error 
)
static

Definition at line 208 of file source_hole.c.

208  {
209  zip_uint8_t b[20];
210  zip_uint64_t i;
211 
212  if (fread(b, 20, 1, f) != 1) {
214  return -1;
215  }
216 
217  if (memcmp(b, MARK_BEGIN, 4) != 0) {
219  return -1;
220  }
221 
222  buffer->fragment_size = get_u64(b + 4);
223  buffer->size = get_u64(b + 12);
224 
225  if (buffer->fragment_size == 0) {
227  return -1;
228  }
229 
231  if (buffer->size % buffer->fragment_size != 0) {
232  buffer->nfragments += 1;
233  }
234 
235  if ((buffer->nfragments > SIZE_MAX / sizeof(buffer->fragment[0])) || ((buffer->fragment = (zip_uint8_t **)malloc(sizeof(buffer->fragment[0]) * buffer->nfragments)) == NULL)) {
237  return -1;
238  }
239 
240  for (i = 0; i < buffer->nfragments; i++) {
241  buffer->fragment[i] = NULL;
242  }
243 
244  i = 0;
245  while (i < buffer->nfragments) {
246  if (fread(b, 4, 1, f) != 1) {
248  return -1;
249  }
250 
251  if (memcmp(b, MARK_DATA, 4) == 0) {
252  if (buffer->fragment_size > SIZE_MAX) {
254  return -1;
255  }
258  return -1;
259  }
260  if (fread(buffer->fragment[i], buffer->fragment_size, 1, f) != 1) {
262  return -1;
263  }
264  i++;
265  }
266  else if (memcmp(b, MARK_NUL, 4) == 0) {
267  if (fread(b, 8, 1, f) != 1) {
269  return -1;
270  }
271  i += get_u64(b);
272  }
273  else {
275  return -1;
276  }
277  }
278 
279  return 0;
280 }
#define ZIP_ER_INCONS
Definition: zip.h:126
#define ZIP_ER_READ
Definition: zip.h:110
#define b(i)
Definition: sha256.c:42
#define MARK_NUL
Definition: source_hole.c:57
#define MARK_DATA
Definition: source_hole.c:56
static zip_uint64_t get_u64(const zip_uint8_t *b)
Definition: source_hole.c:404
#define EFTYPE
Definition: source_hole.c:47
#define MARK_BEGIN
Definition: source_hole.c:55
#define SIZE_MAX
uint8_t zip_uint8_t
Definition: zipconf.h:33

References b, EFTYPE, error(), f, buffer::fragment, buffer::fragment_size, get_u64(), i, malloc(), MARK_BEGIN, MARK_DATA, MARK_NUL, buffer::nfragments, NULL, buffer::size, SIZE_MAX, ZIP_ER_INCONS, ZIP_ER_MEMORY, ZIP_ER_READ, and zip_error_set().

Referenced by buffer_from_file().

◆ buffer_seek()

static zip_int64_t buffer_seek ( buffer_t buffer,
void *  data,
zip_uint64_t  length,
zip_error_t error 
)
static

Definition at line 283 of file source_hole.c.

283  {
285 
286  if (new_offset < 0) {
287  return -1;
288  }
289 
290  buffer->offset = (zip_uint64_t)new_offset;
291  return 0;
292 }
ZIP_EXTERN zip_int64_t zip_source_seek_compute_offset(zip_uint64_t, zip_uint64_t, void *_Nonnull, zip_uint64_t, zip_error_t *_Nullable)

References error(), length, buffer::offset, buffer::size, and zip_source_seek_compute_offset().

Referenced by source_hole_cb().

◆ buffer_to_file()

static int buffer_to_file ( buffer_t buffer,
const char *  fname,
zip_error_t error 
)
static

Definition at line 296 of file source_hole.c.

296  {
297  FILE *f = fopen(fname, "wb");
298  zip_uint64_t i;
299  zip_uint64_t nul_run;
300 
301  if (f == NULL) {
303  return -1;
304  }
305 
306  fwrite(MARK_BEGIN, 4, 1, f);
308  write_u64(buffer->size, f);
309 
310  nul_run = 0;
311  for (i = 0; i * buffer->fragment_size < buffer->size; i++) {
313  nul_run++;
314  }
315  else {
316  if (nul_run > 0) {
317  write_nuls(nul_run, f);
318  nul_run = 0;
319  }
320  fwrite(MARK_DATA, 4, 1, f);
321 
322  fwrite(buffer->fragment[i], 1, buffer->fragment_size, f);
323  }
324  }
325 
326  if (nul_run > 0) {
327  write_nuls(nul_run, f);
328  }
329 
330  if (fclose(f) != 0) {
332  return -1;
333  }
334 
335  return 0;
336 }
#define ZIP_ER_WRITE
Definition: zip.h:111
#define ZIP_ER_OPEN
Definition: zip.h:116
static int only_nul(const zip_uint8_t *data, zip_uint64_t length)
Definition: source_hole.c:414
static int write_u64(zip_uint64_t u64, FILE *f)
Definition: source_hole.c:437
static int write_nuls(zip_uint64_t n, FILE *f)
Definition: source_hole.c:428

References error(), f, benchmark::FILE, create_tags_rz::fname, buffer::fragment, buffer::fragment_size, i, MARK_BEGIN, MARK_DATA, NULL, only_nul(), buffer::size, write_nuls(), write_u64(), ZIP_ER_OPEN, ZIP_ER_WRITE, and zip_error_set().

Referenced by source_hole_cb().

◆ buffer_write()

static zip_int64_t buffer_write ( buffer_t buffer,
const zip_uint8_t data,
zip_uint64_t  length,
zip_error_t error 
)
static

Definition at line 340 of file source_hole.c.

340  {
341  zip_uint8_t **fragment;
343  zip_uint64_t needed_fragments = (buffer->offset + length + buffer->fragment_size - 1) / buffer->fragment_size;
344  zip_uint64_t new_capacity = buffer->nfragments;
345  zip_uint64_t i;
346 
347  if (new_capacity == 0) {
348  new_capacity = 4;
349  }
350  while (new_capacity < needed_fragments) {
351  new_capacity *= 2;
352  }
353 
354  fragment = realloc(buffer->fragment, new_capacity * sizeof(*fragment));
355 
356  if (fragment == NULL) {
358  return -1;
359  }
360 
361  for (i = buffer->nfragments; i < new_capacity; i++) {
362  fragment[i] = NULL;
363  }
364 
365  buffer->fragment = fragment;
366  buffer->nfragments = new_capacity;
367  }
368 
369  if (!only_nul(data, length)) {
370  zip_uint64_t idx, n, fragment_offset;
371 
373  fragment_offset = buffer->offset % buffer->fragment_size;
374  n = 0;
375 
376  while (n < length) {
377  zip_uint64_t left = MY_MIN(length - n, buffer->fragment_size - fragment_offset);
378 
379  if (buffer->fragment[idx] == NULL) {
382  return -1;
383  }
385  }
386  memcpy(buffer->fragment[idx] + fragment_offset, data + n, left);
387 
388  n += left;
389  idx++;
390  fragment_offset = 0;
391  }
392  }
393 
394  buffer->offset += length;
395  if (buffer->offset > buffer->size) {
396  buffer->size = buffer->offset;
397  }
398 
399  return (zip_int64_t)length;
400 }
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
int idx
Definition: setup.py:197
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References error(), buffer::fragment, buffer::fragment_size, i, setup::idx, if(), length, malloc(), memcpy(), memset(), MY_MIN, n, buffer::nfragments, NULL, buffer::offset, only_nul(), realloc(), buffer::size, ZIP_ER_MEMORY, and zip_error_set().

Referenced by source_hole_cb().

◆ get_u64()

static zip_uint64_t get_u64 ( const zip_uint8_t b)
static

Definition at line 404 of file source_hole.c.

404  {
405  zip_uint64_t i;
406 
407  i = (zip_uint64_t)b[0] << 56 | (zip_uint64_t)b[1] << 48 | (zip_uint64_t)b[2] << 40 | (zip_uint64_t)b[3] << 32 | (zip_uint64_t)b[4] << 24 | (zip_uint64_t)b[5] << 16 | (zip_uint64_t)b[6] << 8 | (zip_uint64_t)b[7];
408 
409  return i;
410 }

References b, and i.

Referenced by buffer_read_file().

◆ hole_free()

static void hole_free ( hole_t hole)
static

Definition at line 454 of file source_hole.c.

454  {
455  if (hole == NULL) {
456  return;
457  }
459  buffer_free(hole->in);
460  buffer_free(hole->out);
461  free(hole->fname);
462  free(hole);
463 }
ZIP_EXTERN void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52
char * fname
Definition: source_hole.c:84
buffer_t * out
Definition: source_hole.c:86
buffer_t * in
Definition: source_hole.c:85
zip_error_t error
Definition: source_hole.c:83

References buffer_free(), hole::error, hole::fname, free(), hole::in, NULL, hole::out, and zip_error_fini().

Referenced by source_hole_cb().

◆ hole_new()

static hole_t * hole_new ( const char *  fname,
int  flags,
zip_error_t error 
)
static

Definition at line 467 of file source_hole.c.

467  {
468  hole_t *ctx = (hole_t *)malloc(sizeof(*ctx));
469 
470  if (ctx == NULL) {
472  return NULL;
473  }
474 
475  if ((ctx->fname = strdup(fname)) == NULL) {
476  free(ctx);
478  return NULL;
479  }
480 
481  if ((ctx->in = buffer_from_file(fname, flags, error)) == NULL) {
482  free(ctx);
483  return NULL;
484  }
485 
487  ctx->out = NULL;
488 
489  return ctx;
490 }
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
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")
static buffer_t * buffer_from_file(const char *fname, int flags, zip_error_t *error)
Definition: source_hole.c:123
ZSTD_outBuffer out
zip_error_t * error
ZSTD_inBuffer in

References buffer_from_file(), ctx::error, error(), flags, create_tags_rz::fname, free(), ctx::in, malloc(), NULL, ctx::out, strdup(), ZIP_ER_MEMORY, zip_error_init(), and zip_error_set().

Referenced by source_hole_create().

◆ only_nul()

static int only_nul ( const zip_uint8_t data,
zip_uint64_t  length 
)
static

Definition at line 414 of file source_hole.c.

414  {
415  zip_uint64_t i;
416 
417  for (i = 0; i < length; i++) {
418  if (data[i] != '\0') {
419  return 0;
420  }
421  }
422 
423  return 1;
424 }

References i, and length.

Referenced by buffer_to_file(), and buffer_write().

◆ source_hole_cb()

static zip_int64_t source_hole_cb ( void *  ud,
void *  data,
zip_uint64_t  length,
zip_source_cmd_t  command 
)
static

Definition at line 494 of file source_hole.c.

494  {
495  hole_t *ctx = (hole_t *)ud;
496 
497  switch (command) {
499  ctx->out = buffer_new();
500  return 0;
501 
502  case ZIP_SOURCE_CLOSE:
503  return 0;
504 
506  if (buffer_to_file(ctx->out, ctx->fname, &ctx->error) < 0) {
507  return -1;
508  }
509  buffer_free(ctx->in);
510  ctx->in = ctx->out;
511  ctx->out = NULL;
512  return 0;
513 
514  case ZIP_SOURCE_ERROR:
515  return zip_error_to_data(&ctx->error, data, length);
516 
517  case ZIP_SOURCE_FREE:
518  hole_free(ctx);
519  return 0;
520 
521  case ZIP_SOURCE_OPEN:
522  ctx->in->offset = 0;
523  return 0;
524 
525  case ZIP_SOURCE_READ:
526  return buffer_read(ctx->in, data, length, &ctx->error);
527 
528  case ZIP_SOURCE_REMOVE:
529  buffer_free(ctx->in);
530  ctx->in = buffer_new();
531  buffer_free(ctx->out);
532  ctx->out = NULL;
533  (void)remove(ctx->fname);
534  return 0;
535 
537  buffer_free(ctx->out);
538  ctx->out = NULL;
539  return 0;
540 
541  case ZIP_SOURCE_SEEK:
542  return buffer_seek(ctx->in, data, length, &ctx->error);
543 
545  return buffer_seek(ctx->out, data, length, &ctx->error);
546 
547  case ZIP_SOURCE_STAT: {
549 
550  if (st == NULL) {
551  return -1;
552  }
553 
554  /* TODO: return ENOENT if fname doesn't exist */
555 
556  st->valid |= ZIP_STAT_SIZE;
557  st->size = ctx->in->size;
558  return 0;
559  }
560 
561  case ZIP_SOURCE_TELL:
562  return (zip_int64_t)ctx->in->offset;
563 
565  return (zip_int64_t)ctx->out->offset;
566 
567  case ZIP_SOURCE_WRITE:
568  return buffer_write(ctx->out, data, length, &ctx->error);
569 
570  case ZIP_SOURCE_SUPPORTS:
572 
573  default:
575  return -1;
576  }
577 }
#define ZIP_STAT_SIZE
Definition: zip.h:292
@ ZIP_SOURCE_CLOSE
Definition: zip.h:222
@ ZIP_SOURCE_READ
Definition: zip.h:221
@ ZIP_SOURCE_FREE
Definition: zip.h:225
@ ZIP_SOURCE_SEEK
Definition: zip.h:226
@ ZIP_SOURCE_SEEK_WRITE
Definition: zip.h:232
@ ZIP_SOURCE_SUPPORTS
Definition: zip.h:234
@ ZIP_SOURCE_STAT
Definition: zip.h:223
@ ZIP_SOURCE_TELL
Definition: zip.h:227
@ ZIP_SOURCE_OPEN
Definition: zip.h:220
@ ZIP_SOURCE_REMOVE
Definition: zip.h:235
@ ZIP_SOURCE_ROLLBACK_WRITE
Definition: zip.h:230
@ ZIP_SOURCE_TELL_WRITE
Definition: zip.h:233
@ ZIP_SOURCE_BEGIN_WRITE
Definition: zip.h:228
@ ZIP_SOURCE_WRITE
Definition: zip.h:231
@ ZIP_SOURCE_ERROR
Definition: zip.h:224
@ ZIP_SOURCE_COMMIT_WRITE
Definition: zip.h:229
ZIP_EXTERN zip_int64_t zip_source_make_command_bitmap(zip_source_cmd_t,...)
#define ZIP_ER_OPNOTSUPP
Definition: zip.h:133
ZIP_EXTERN zip_int64_t zip_error_to_data(const zip_error_t *_Nonnull, void *_Nonnull, zip_uint64_t)
Definition: zip_error.c:141
#define ZIP_SOURCE_GET_ARGS(type, data, len, error)
Definition: zip.h:279
static int buffer_to_file(buffer_t *buffer, const char *fname, zip_error_t *error)
Definition: source_hole.c:296
static zip_int64_t buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length, zip_error_t *error)
Definition: source_hole.c:172
static zip_int64_t buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error)
Definition: source_hole.c:340
static void hole_free(hole_t *hole)
Definition: source_hole.c:454
static zip_int64_t buffer_seek(buffer_t *buffer, void *data, zip_uint64_t length, zip_error_t *error)
Definition: source_hole.c:283
Definition: zip.h:300
zip_uint64_t valid
Definition: zip.h:301
zip_uint64_t size
Definition: zip.h:304
const char * command
Definition: main.c:7

References buffer_free(), buffer_new(), buffer_read(), buffer_seek(), buffer_to_file(), buffer_write(), command, ctx::error, hole_free(), ctx::in, length, NULL, ctx::out, zip_stat::size, zip_stat::valid, ZIP_ER_OPNOTSUPP, zip_error_set(), zip_error_to_data(), ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_CLOSE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_GET_ARGS, zip_source_make_command_bitmap(), ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_REMOVE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_STAT, ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_WRITE, and ZIP_STAT_SIZE.

Referenced by source_hole_create().

◆ source_hole_create()

zip_source_t* source_hole_create ( const char *  fname,
int  flags,
zip_error_t error 
)

Definition at line 94 of file source_hole.c.

94  {
95  hole_t *ud = hole_new(fname, flags, error);
96 
97  if (ud == NULL) {
98  return NULL;
99  }
101 }
ZIP_EXTERN zip_source_t *_Nullable zip_source_function_create(zip_source_callback _Nonnull, void *_Nullable, zip_error_t *_Nullable)
static zip_int64_t source_hole_cb(void *ud, void *data, zip_uint64_t length, zip_source_cmd_t command)
Definition: source_hole.c:494
static hole_t * hole_new(const char *fname, int flags, zip_error_t *error)
Definition: source_hole.c:467

References error(), flags, create_tags_rz::fname, hole_new(), NULL, source_hole_cb(), and zip_source_function_create().

Referenced by open_compressed(), and read_hole().

◆ write_nuls()

static int write_nuls ( zip_uint64_t  n,
FILE *  f 
)
static

Definition at line 428 of file source_hole.c.

428  {
429  if (fwrite(MARK_NUL, 4, 1, f) != 1) {
430  return -1;
431  }
432  return write_u64(n, f);
433 }

References f, MARK_NUL, n, and write_u64().

Referenced by buffer_to_file().

◆ write_u64()

static int write_u64 ( zip_uint64_t  u64,
FILE *  f 
)
static

Definition at line 437 of file source_hole.c.

437  {
438  zip_uint8_t b[8];
439 
440  b[0] = (zip_uint8_t)((u64 >> 56) & 0xff);
441  b[1] = (zip_uint8_t)((u64 >> 48) & 0xff);
442  b[2] = (zip_uint8_t)((u64 >> 40) & 0xff);
443  b[3] = (zip_uint8_t)((u64 >> 32) & 0xff);
444  b[4] = (zip_uint8_t)((u64 >> 24) & 0xff);
445  b[5] = (zip_uint8_t)((u64 >> 16) & 0xff);
446  b[6] = (zip_uint8_t)((u64 >> 8) & 0xff);
447  b[7] = (zip_uint8_t)(u64 & 0xff);
448 
449  return fwrite(b, 8, 1, f) == 1 ? 0 : -1;
450 }

References b, and f.

Referenced by buffer_to_file(), and write_nuls().