Rizin
unix-like reverse engineering framework and cli tools
cabrip.c
Go to the documentation of this file.
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/stat.h>
8 #include <mspack.h>
9 #include "mspack/macros.h"
10 
11 #if HAVE_FSEEKO
12 # define fseek fseeko
13 #endif
14 
15 #define BUF_SIZE (1024*4096)
16 char buf[BUF_SIZE];
17 
18 void rip(char *fname, off_t offset, unsigned int length) {
19  static unsigned int counter = 1;
20  struct stat st_buf;
21  char outname[13];
22  FILE *in = NULL, *out = NULL;
23 
24  /* find an unused output filename */
25  do {
26  snprintf(outname, 13, "%08u.cab", counter++);
27  } while (stat(outname, &st_buf) == 0);
28 
29  printf("ripping %s offset %" LD " length %u to %s\n",
30  fname, offset, length, outname);
31 
32  if (!(in = fopen(fname, "rb"))) {
33  perror(fname);
34  goto cleanup;
35  }
36  if (!(out = fopen(outname, "wb"))) {
37  perror(outname);
38  goto cleanup;
39  }
40  if (fseek(in, offset, SEEK_SET)) {
41  fprintf(stderr, "%s: can't seek to cab offset %"LD"\n", fname, offset);
42  goto cleanup;
43  }
44  while (length) {
45  size_t run = (length > BUF_SIZE) ? BUF_SIZE : length;
46  size_t actual = fread(&buf[0], 1, run, in);
47  if (actual < run) {
48  fprintf(stderr, "%s: file %u bytes shorter than expected\n",
49  fname, length - (unsigned int)(run - actual));
50  length = run = actual;
51  }
52  if (fwrite(&buf[0], 1, run, out) != run) {
53  perror(outname);
54  break;
55  }
56  length -= run;
57  }
58 
59 cleanup:
60  if (in) fclose(in);
61  if (out) fclose(out);
62 }
63 
64 int main(int argc, char *argv[]) {
65  struct mscab_decompressor *cabd;
66  struct mscabd_cabinet *cab, *c;
67  int err;
68 
70  if (err) return 0;
71 
74  for (argv++; *argv; argv++) {
75  if ((cab = cabd->search(cabd, *argv))) {
76  for (c = cab; c; c = c->next) {
77  rip(*argv, c->base_offset, c->length);
78  }
79  cabd->close(cabd, cab);
80  }
81  }
83  }
84  return 0;
85 }
static bool err
Definition: armass.c:435
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define MSPACK_SYS_SELFTEST(result)
Definition: mspack.h:191
#define MSCABD_PARAM_SALVAGE
Definition: mspack.h:943
struct mscab_decompressor * cabd
Definition: cabextract.c:126
int main(int argc, char *argv[])
Definition: cabrip.c:64
#define BUF_SIZE
Definition: cabrip.c:15
void rip(char *fname, off_t offset, unsigned int length)
Definition: cabrip.c:18
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
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
void cleanup(void)
Definition: enough.c:244
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
void mspack_destroy_cab_decompressor(struct mscab_decompressor *base)
Definition: cabd.c:173
struct mscab_decompressor * mspack_create_cab_decompressor(struct mspack_system *sys)
Definition: cabd.c:140
#define LD
Definition: macros.h:27
static stat
Definition: sflib.h:131
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
string FILE
Definition: benchmark.py:21
static int run(int i, const char *arg)
Definition: rz-bb.c:19
int off_t
Definition: sftypes.h:41
#define c(i)
Definition: sha256.c:43
struct mscabd_cabinet *(* search)(struct mscab_decompressor *self, const char *filename)
Definition: mspack.h:1047
void(* close)(struct mscab_decompressor *self, struct mscabd_cabinet *cab)
Definition: mspack.h:1010
int(* set_param)(struct mscab_decompressor *self, int param, int value)
Definition: mspack.h:1164
Definition: sftypes.h:80
int64_t counter
Definition: main.c:4
#define SEEK_SET
Definition: zip.c:88