Rizin
unix-like reverse engineering framework and cli tools
source_hole.c
Go to the documentation of this file.
1 /*
2  source_hole.c -- source for handling huge files that are mostly NULs
3  Copyright (C) 2014-2021 Dieter Baron and Thomas Klausner
4 
5  This file is part of libzip, a library to manipulate ZIP archives.
6  The authors can be contacted at <libzip@nih.at>
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in
15  the documentation and/or other materials provided with the
16  distribution.
17  3. The names of the authors may not be used to endorse or promote
18  products derived from this software without specific prior
19  written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "zip.h"
40 
41 /* public API */
42 
43 zip_source_t *source_hole_create(const char *, int flags, zip_error_t *);
44 
45 
46 #ifndef EFTYPE
47 #define EFTYPE EINVAL
48 #endif
49 
50 
51 #define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
52 
53 #define FRAGMENT_SIZE (8 * 1024)
54 
55 #define MARK_BEGIN "NiH0"
56 #define MARK_DATA "NiH1"
57 #define MARK_NUL "NiH2"
58 
59 
60 typedef struct buffer {
67 
68 static void buffer_free(buffer_t *buffer);
69 static buffer_t *buffer_from_file(const char *fname, int flags, zip_error_t *error);
70 static buffer_t *buffer_new(void);
74 static int buffer_to_file(buffer_t *buffer, const char *fname, zip_error_t *error);
76 static zip_uint64_t get_u64(const zip_uint8_t *b);
77 static int only_nul(const zip_uint8_t *data, zip_uint64_t length);
78 static int write_nuls(zip_uint64_t n, FILE *f);
79 static int write_u64(zip_uint64_t u64, FILE *f);
80 
81 
82 typedef struct hole {
84  char *fname;
88 
89 static hole_t *hole_new(const char *fname, int flags, zip_error_t *error);
91 
92 
95  hole_t *ud = hole_new(fname, flags, error);
96 
97  if (ud == NULL) {
98  return NULL;
99  }
101 }
102 
103 
104 static void
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 }
120 
121 
122 static buffer_t *
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 }
151 
152 
153 static buffer_t *
154 buffer_new(void) {
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 }
169 
170 
171 static zip_int64_t
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 }
205 
206 
207 static int
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 }
281 
282 static zip_int64_t
285 
286  if (new_offset < 0) {
287  return -1;
288  }
289 
290  buffer->offset = (zip_uint64_t)new_offset;
291  return 0;
292 }
293 
294 
295 static int
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 }
337 
338 
339 static zip_int64_t
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 }
401 
402 
403 static zip_uint64_t
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 }
411 
412 
413 static int
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 }
425 
426 
427 static int
429  if (fwrite(MARK_NUL, 4, 1, f) != 1) {
430  return -1;
431  }
432  return write_u64(n, f);
433 }
434 
435 
436 static int
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 }
451 
452 
453 static void
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 }
464 
465 
466 static hole_t *
467 hole_new(const char *fname, int flags, zip_error_t *error) {
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 }
491 
492 
493 static zip_int64_t
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 }
lzma_index ** i
Definition: index.h:629
struct buffer buffer
#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 length
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
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_OPEN
Definition: zip.h:116
#define ZIP_ER_MEMORY
Definition: zip.h:119
#define ZIP_STAT_SIZE
Definition: zip.h: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)
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
#define ZIP_CREATE
Definition: zip.h:67
enum zip_source_cmd zip_source_cmd_t
Definition: zip.h:241
@ 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 void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52
ZIP_EXTERN zip_source_t *_Nullable zip_source_function_create(zip_source_callback _Nonnull, void *_Nullable, zip_error_t *_Nullable)
ZIP_EXTERN zip_int64_t zip_source_make_command_bitmap(zip_source_cmd_t,...)
#define ZIP_TRUNCATE
Definition: zip.h:70
#define ZIP_ER_OPNOTSUPP
Definition: zip.h:133
#define ZIP_ER_INCONS
Definition: zip.h:126
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_ER_READ
Definition: zip.h:110
#define ZIP_SOURCE_GET_ARGS(type, data, len, error)
Definition: zip.h:279
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
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")
int n
Definition: mipsasm.c:19
string FILE
Definition: benchmark.py:21
int idx
Definition: setup.py:197
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define ENOENT
Definition: sftypes.h:112
#define b(i)
Definition: sha256.c:42
#define f(i)
Definition: sha256.c:46
#define MARK_NUL
Definition: source_hole.c:57
static int buffer_to_file(buffer_t *buffer, const char *fname, zip_error_t *error)
Definition: source_hole.c:296
#define MARK_DATA
Definition: source_hole.c:56
#define FRAGMENT_SIZE
Definition: source_hole.c:53
#define MY_MIN(a, b)
Definition: source_hole.c:51
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 buffer_t * buffer_new(void)
Definition: source_hole.c:154
static int only_nul(const zip_uint8_t *data, zip_uint64_t length)
Definition: source_hole.c:414
zip_source_t * source_hole_create(const char *, int flags, zip_error_t *)
Definition: source_hole.c:94
static zip_uint64_t get_u64(const zip_uint8_t *b)
Definition: source_hole.c:404
static int buffer_read_file(buffer_t *buffer, FILE *f, zip_error_t *error)
Definition: source_hole.c:208
static int write_u64(zip_uint64_t u64, FILE *f)
Definition: source_hole.c:437
static void buffer_free(buffer_t *buffer)
Definition: source_hole.c:105
struct hole hole_t
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
#define EFTYPE
Definition: source_hole.c:47
#define MARK_BEGIN
Definition: source_hole.c:55
static buffer_t * buffer_from_file(const char *fname, int flags, zip_error_t *error)
Definition: source_hole.c:123
static hole_t * hole_new(const char *fname, int flags, zip_error_t *error)
Definition: source_hole.c:467
static int write_nuls(zip_uint64_t n, FILE *f)
Definition: source_hole.c:428
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
struct buffer buffer_t
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
#define SIZE_MAX
Definition: buffer.h:15
zip_uint64_t offset
zip_uint64_t nfragments
zip_uint64_t size
zip_uint64_t fragment_size
Definition: source_hole.c:61
zip_uint8_t ** fragment
Definition: source_hole.c:62
ZSTD_outBuffer out
zip_error_t * error
ZSTD_inBuffer in
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
Definition: zip.h:284
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
void error(const char *msg)
Definition: untgz.c:593
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint8_t zip_uint8_t
Definition: zipconf.h:33
#define ZIP_INT64_MAX
Definition: zipconf.h:54
int64_t zip_int64_t
Definition: zipconf.h:38