Rizin
unix-like reverse engineering framework and cli tools
cabd_memory.c
Go to the documentation of this file.
1 /* An implementation of the mspack_system interface using only memory */
2 
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <mspack.h>
11 
12 /* use a pointer to a mem_buf structure as "filenames" */
13 struct mem_buf {
14  void *data;
15  size_t length;
16 };
17 
18 struct mem_file {
19  char *data;
20  size_t length, posn;
21 };
22 
23 static void *mem_alloc(struct mspack_system *self, size_t bytes) {
24  /* put your memory allocator here */
25  return malloc(bytes);
26 }
27 
28 static void mem_free(void *buffer) {
29  /* put your memory deallocator here */
30  free(buffer);
31 }
32 
33 static void mem_copy(void *src, void *dest, size_t bytes) {
34  /* put your own memory copy routine here */
35  memcpy(dest, src, bytes);
36 }
37 
38 static void mem_msg(struct mem_file *file, const char *format, ...) {
39  /* put your own printf-type routine here, or leave it empty */
40 }
41 
42 static struct mem_file *mem_open(struct mspack_system *self,
43  struct mem_buf *fn, int mode)
44 {
45  struct mem_file *fh;
46  if (!fn || !fn->data || !fn->length) return NULL;
47  if ((fh = (struct mem_file *) mem_alloc(self, sizeof(struct mem_file)))) {
48  fh->data = (char *) fn->data;
49  fh->length = fn->length;
50  fh->posn = (mode == MSPACK_SYS_OPEN_APPEND) ? fn->length : 0;
51  }
52  return fh;
53 }
54 
55 static void mem_close(struct mem_file *fh) {
56  if (fh) mem_free(fh);
57 }
58 
59 static int mem_read(struct mem_file *fh, void *buffer, int bytes) {
60  int todo;
61  if (!fh || !buffer || bytes < 0) return -1;
62  todo = fh->length - fh->posn;
63  if (todo > bytes) todo = bytes;
64  if (todo > 0) mem_copy(&fh->data[fh->posn], buffer, (size_t) todo);
65  fh->posn += todo; return todo;
66 }
67 
68 static int mem_write(struct mem_file *fh, void *buffer, int bytes) {
69  int todo;
70  if (!fh || !buffer || bytes < 0) return -1;
71  todo = fh->length - fh->posn;
72  if (todo > bytes) todo = bytes;
73  if (todo > 0) mem_copy(buffer, &fh->data[fh->posn], (size_t) todo);
74  fh->posn += todo; return todo;
75 }
76 
77 static int mem_seek(struct mem_file *fh, off_t offset, int mode) {
78  if (!fh) return 1;
79  switch (mode) {
80  case MSPACK_SYS_SEEK_START: break;
81  case MSPACK_SYS_SEEK_CUR: offset += (off_t) fh->posn; break;
82  case MSPACK_SYS_SEEK_END: offset += (off_t) fh->length; break;
83  default: return 1;
84  }
85  if ((offset < 0) || (offset > (off_t) fh->length)) return 1;
86  fh->posn = (size_t) offset;
87  return 0;
88 }
89 
90 static off_t mem_tell(struct mem_file *fh) {
91  return (fh) ? (off_t) fh->posn : -1;
92 }
93 
94 static struct mspack_system mem_system = {
95  (struct mspack_file * (*)(struct mspack_system *, const char *, int)) &mem_open,
96  (void (*)(struct mspack_file *)) &mem_close,
97  (int (*)(struct mspack_file *, void *, int)) &mem_read,
98  (int (*)(struct mspack_file *, void *, int)) &mem_write,
99  (int (*)(struct mspack_file *, off_t, int)) &mem_seek,
100  (off_t (*)(struct mspack_file *)) &mem_tell,
101  (void (*)(struct mspack_file *, const char *, ...)) &mem_msg,
102  &mem_alloc,
103  &mem_free,
104  &mem_copy,
105  NULL
106 };
107 
108 /* example of usage with mscab_decompressor */
109 
110 /* a simple cabinet */
111 static unsigned char embedded_cab[] = {
112  0x4D,0x53,0x43,0x46,0x00,0x00,0x00,0x00,0xFD,0x00,0x00,0x00,0x00,0x00,0x00,
113  0x00,0x2C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x01,0x01,0x00,0x02,0x00,
114  0x00,0x00,0x22,0x06,0x00,0x00,0x5E,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x4D,
115  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6C,0x22,0xBA,0x59,0x20,0x00,
116  0x68,0x65,0x6C,0x6C,0x6F,0x2E,0x63,0x00,0x4A,0x00,0x00,0x00,0x4D,0x00,0x00,
117  0x00,0x00,0x00,0x6C,0x22,0xE7,0x59,0x20,0x00,0x77,0x65,0x6C,0x63,0x6F,0x6D,
118  0x65,0x2E,0x63,0x00,0xBD,0x5A,0xA6,0x30,0x97,0x00,0x97,0x00,0x23,0x69,0x6E,
119  0x63,0x6C,0x75,0x64,0x65,0x20,0x3C,0x73,0x74,0x64,0x69,0x6F,0x2E,0x68,0x3E,
120  0x0D,0x0A,0x0D,0x0A,0x76,0x6F,0x69,0x64,0x20,0x6D,0x61,0x69,0x6E,0x28,0x76,
121  0x6F,0x69,0x64,0x29,0x0D,0x0A,0x7B,0x0D,0x0A,0x20,0x20,0x20,0x20,0x70,0x72,
122  0x69,0x6E,0x74,0x66,0x28,0x22,0x48,0x65,0x6C,0x6C,0x6F,0x2C,0x20,0x77,0x6F,
123  0x72,0x6C,0x64,0x21,0x5C,0x6E,0x22,0x29,0x3B,0x0D,0x0A,0x7D,0x0D,0x0A,0x23,
124  0x69,0x6E,0x63,0x6C,0x75,0x64,0x65,0x20,0x3C,0x73,0x74,0x64,0x69,0x6F,0x2E,
125  0x68,0x3E,0x0D,0x0A,0x0D,0x0A,0x76,0x6F,0x69,0x64,0x20,0x6D,0x61,0x69,0x6E,
126  0x28,0x76,0x6F,0x69,0x64,0x29,0x0D,0x0A,0x7B,0x0D,0x0A,0x20,0x20,0x20,0x20,
127  0x70,0x72,0x69,0x6E,0x74,0x66,0x28,0x22,0x57,0x65,0x6C,0x63,0x6F,0x6D,0x65,
128  0x21,0x5C,0x6E,0x22,0x29,0x3B,0x0D,0x0A,0x7D,0x0D,0x0A,0x0D,0x0A
129 };
130 
131 int main() {
132  struct mscab_decompressor *cabd;
133  struct mscabd_cabinet *cab;
134  struct mscabd_file *file;
135  struct mem_buf source = { &embedded_cab[0], sizeof(embedded_cab) };
136  struct mem_buf output;
137  int err;
138 
139  /* if self-test reveals an error */
141  if (err) return 1;
142 
143  /* create a cab decompressor using our custom mspack_system interface */
145 
146  /* open a cab file direct from memory */
147  if ((cab = cabd->open(cabd, (char *) &source))) {
148 
149  /* for all files */
150  for (file = cab->files; file; file = file->next) {
151  /* fill out our "filename" (memory pointer and length) */
152  output.data = (char *) malloc(file->length);
153  output.length = file->length;
154 
155  /* let cabd extract this file to our memory buffer */
156  if (output.data && cabd->extract(cabd, file, (char *) &output)) {
157  exit(1);
158  }
159 
160  /* dump the memory buffer to stdout (for display purposes) */
161  printf("Filename: %s\nContents:\n", file->filename);
162  fwrite(output.data, 1, output.length, stdout);
163 
164  /* free our buffer */
165  free(output.data);
166  }
167  cabd->close(cabd, cab);
168  }
169  else {
170  fprintf(stderr, "can't open cabinet (%d)\n", cabd->last_error(cabd));
171  }
173  }
174  else {
175  fprintf(stderr, "can't make decompressor\n");
176  }
177  return 0;
178 
179 }
lzma_index * src
Definition: index.h:567
static bool err
Definition: armass.c:435
static ut8 bytes[32]
Definition: asm_arc.c:23
static void * mem_alloc(struct mspack_system *self, size_t bytes)
Definition: cabd_memory.c:23
static void mem_free(void *buffer)
Definition: cabd_memory.c:28
static struct mspack_system mem_system
Definition: cabd_memory.c:94
static int mem_seek(struct mem_file *fh, off_t offset, int mode)
Definition: cabd_memory.c:77
static int mem_read(struct mem_file *fh, void *buffer, int bytes)
Definition: cabd_memory.c:59
static int mem_write(struct mem_file *fh, void *buffer, int bytes)
Definition: cabd_memory.c:68
static void mem_copy(void *src, void *dest, size_t bytes)
Definition: cabd_memory.c:33
static unsigned char embedded_cab[]
Definition: cabd_memory.c:111
static struct mem_file * mem_open(struct mspack_system *self, struct mem_buf *fn, int mode)
Definition: cabd_memory.c:42
static void mem_close(struct mem_file *fh)
Definition: cabd_memory.c:55
static void mem_msg(struct mem_file *file, const char *format,...)
Definition: cabd_memory.c:38
static off_t mem_tell(struct mem_file *fh)
Definition: cabd_memory.c:90
int main()
Definition: cabd_memory.c:131
#define MSPACK_SYS_OPEN_APPEND
Definition: mspack.h:464
#define MSPACK_SYS_SEEK_END
Definition: mspack.h:471
#define MSPACK_SYS_SELFTEST(result)
Definition: mspack.h:191
#define MSPACK_SYS_SEEK_START
Definition: mspack.h:467
#define MSPACK_SYS_SEEK_CUR
Definition: mspack.h:469
struct mscab_decompressor * cabd
Definition: cabextract.c:126
FILE * fh
Definition: cabinfo.c:52
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
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
void * malloc(size_t size)
Definition: malloc.c:123
static const char struct stat static buf struct stat static buf static idle const char static path static fd const char static len const void static prot const char struct module static image struct kernel_sym static table unsigned char static buf static fsuid unsigned struct dirent unsigned static count const struct iovec static count static pid const void static len static flags const struct sched_param static p static pid static policy struct timespec static tp static suid unsigned fn
Definition: sflib.h:186
const char * source
Definition: lz4.h:699
char * dest
Definition: lz4.h:697
static int
Definition: sfsocketcall.h:114
int size_t
Definition: sftypes.h:40
int off_t
Definition: sftypes.h:41
Definition: buffer.h:15
Definition: gzappend.c:170
z_const unsigned char * next
Definition: gzappend.c:175
size_t length
Definition: cabd_memory.c:15
void * data
Definition: cabd_memory.c:14
char * data
Definition: cabd_memory.c:19
size_t length
Definition: cabd_memory.c:20
size_t posn
Definition: cabd_memory.c:20
int(* last_error)(struct mscab_decompressor *self)
Definition: mspack.h:1179
struct mscabd_cabinet *(* open)(struct mscab_decompressor *self, const char *filename)
Definition: mspack.h:978
int(* extract)(struct mscab_decompressor *self, struct mscabd_file *file, const char *filename)
Definition: mspack.h:1138
void(* close)(struct mscab_decompressor *self, struct mscabd_cabinet *cab)
Definition: mspack.h:1010
struct mscabd_file * files
Definition: mspack.h:743
static int file
Definition: z80asm.c:58
diff_output_t output
Definition: zipcmp.c:237