Rizin
unix-like reverse engineering framework and cli tools
cabd_memory.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mspack.h>

Go to the source code of this file.

Classes

struct  mem_buf
 
struct  mem_file
 

Functions

static void * mem_alloc (struct mspack_system *self, size_t bytes)
 
static void mem_free (void *buffer)
 
static void mem_copy (void *src, void *dest, size_t bytes)
 
static void mem_msg (struct mem_file *file, const char *format,...)
 
static struct mem_filemem_open (struct mspack_system *self, struct mem_buf *fn, int mode)
 
static void mem_close (struct mem_file *fh)
 
static int mem_read (struct mem_file *fh, void *buffer, int bytes)
 
static int mem_write (struct mem_file *fh, void *buffer, int bytes)
 
static int mem_seek (struct mem_file *fh, off_t offset, int mode)
 
static off_t mem_tell (struct mem_file *fh)
 
int main ()
 

Variables

static struct mspack_system mem_system
 
static unsigned char embedded_cab []
 

Function Documentation

◆ main()

int main ( void  )

Definition at line 131 of file cabd_memory.c.

131  {
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 }
static bool err
Definition: armass.c:435
static struct mspack_system mem_system
Definition: cabd_memory.c:94
static unsigned char embedded_cab[]
Definition: cabd_memory.c:111
#define MSPACK_SYS_SELFTEST(result)
Definition: mspack.h:191
struct mscab_decompressor * cabd
Definition: cabextract.c:126
_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
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
const char * source
Definition: lz4.h:699
Definition: gzappend.c:170
z_const unsigned char * next
Definition: gzappend.c:175
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

References cabd, mscab_decompressor::close, embedded_cab, err, test-lz4-list::exit, mscab_decompressor::extract, file, mscabd_cabinet::files, free(), mscab_decompressor::last_error, malloc(), mem_system, mspack_create_cab_decompressor(), mspack_destroy_cab_decompressor(), MSPACK_SYS_SELFTEST, file::next, mscab_decompressor::open, output, printf(), and source.

◆ mem_alloc()

static void* mem_alloc ( struct mspack_system self,
size_t  bytes 
)
static

Definition at line 23 of file cabd_memory.c.

23  {
24  /* put your memory allocator here */
25  return malloc(bytes);
26 }
static ut8 bytes[32]
Definition: asm_arc.c:23

References bytes, and malloc().

Referenced by mem_open().

◆ mem_close()

static void mem_close ( struct mem_file fh)
static

Definition at line 55 of file cabd_memory.c.

55  {
56  if (fh) mem_free(fh);
57 }
static void mem_free(void *buffer)
Definition: cabd_memory.c:28
FILE * fh
Definition: cabinfo.c:52

References fh, and mem_free().

◆ mem_copy()

static void mem_copy ( void *  src,
void *  dest,
size_t  bytes 
)
static

Definition at line 33 of file cabd_memory.c.

33  {
34  /* put your own memory copy routine here */
35  memcpy(dest, src, bytes);
36 }
lzma_index * src
Definition: index.h:567
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
char * dest
Definition: lz4.h:697

References bytes, dest, memcpy(), and src.

Referenced by mem_read(), and mem_write().

◆ mem_free()

static void mem_free ( void *  buffer)
static

Definition at line 28 of file cabd_memory.c.

28  {
29  /* put your memory deallocator here */
30  free(buffer);
31 }
Definition: buffer.h:15

References free().

Referenced by mem_close().

◆ mem_msg()

static void mem_msg ( struct mem_file file,
const char *  format,
  ... 
)
static

Definition at line 38 of file cabd_memory.c.

38  {
39  /* put your own printf-type routine here, or leave it empty */
40 }

◆ mem_open()

static struct mem_file* mem_open ( struct mspack_system self,
struct mem_buf fn,
int  mode 
)
static

Definition at line 42 of file cabd_memory.c.

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 }
static void * mem_alloc(struct mspack_system *self, size_t bytes)
Definition: cabd_memory.c:23
#define MSPACK_SYS_OPEN_APPEND
Definition: mspack.h:464
#define NULL
Definition: cris-opc.c:27
const char int mode
Definition: ioapi.h:137
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

References fh, fn, mem_alloc(), MSPACK_SYS_OPEN_APPEND, and NULL.

◆ mem_read()

static int mem_read ( struct mem_file fh,
void *  buffer,
int  bytes 
)
static

Definition at line 59 of file cabd_memory.c.

59  {
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 }
static void mem_copy(void *src, void *dest, size_t bytes)
Definition: cabd_memory.c:33

References bytes, fh, and mem_copy().

Referenced by rop_classify_arithmetic(), rop_classify_arithmetic_const(), rop_classify_constant(), rop_classify_mov(), and trace_hook_mem_read().

◆ mem_seek()

static int mem_seek ( struct mem_file fh,
off_t  offset,
int  mode 
)
static

Definition at line 77 of file cabd_memory.c.

77  {
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 }
#define MSPACK_SYS_SEEK_END
Definition: mspack.h:471
#define MSPACK_SYS_SEEK_START
Definition: mspack.h:467
#define MSPACK_SYS_SEEK_CUR
Definition: mspack.h:469
voidpf uLong offset
Definition: ioapi.h:144
int size_t
Definition: sftypes.h:40
int off_t
Definition: sftypes.h:41

References fh, MSPACK_SYS_SEEK_CUR, MSPACK_SYS_SEEK_END, and MSPACK_SYS_SEEK_START.

◆ mem_tell()

static off_t mem_tell ( struct mem_file fh)
static

Definition at line 90 of file cabd_memory.c.

90  {
91  return (fh) ? (off_t) fh->posn : -1;
92 }

◆ mem_write()

static int mem_write ( struct mem_file fh,
void *  buffer,
int  bytes 
)
static

Definition at line 68 of file cabd_memory.c.

68  {
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 }

References bytes, fh, and mem_copy().

Referenced by rop_classify_arithmetic(), rop_classify_arithmetic_const(), rop_classify_constant(), rop_classify_mov(), and trace_hook_mem_write().

Variable Documentation

◆ embedded_cab

unsigned char embedded_cab[]
static
Initial value:
= {
0x4D,0x53,0x43,0x46,0x00,0x00,0x00,0x00,0xFD,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x2C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x01,0x01,0x00,0x02,0x00,
0x00,0x00,0x22,0x06,0x00,0x00,0x5E,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x4D,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6C,0x22,0xBA,0x59,0x20,0x00,
0x68,0x65,0x6C,0x6C,0x6F,0x2E,0x63,0x00,0x4A,0x00,0x00,0x00,0x4D,0x00,0x00,
0x00,0x00,0x00,0x6C,0x22,0xE7,0x59,0x20,0x00,0x77,0x65,0x6C,0x63,0x6F,0x6D,
0x65,0x2E,0x63,0x00,0xBD,0x5A,0xA6,0x30,0x97,0x00,0x97,0x00,0x23,0x69,0x6E,
0x63,0x6C,0x75,0x64,0x65,0x20,0x3C,0x73,0x74,0x64,0x69,0x6F,0x2E,0x68,0x3E,
0x0D,0x0A,0x0D,0x0A,0x76,0x6F,0x69,0x64,0x20,0x6D,0x61,0x69,0x6E,0x28,0x76,
0x6F,0x69,0x64,0x29,0x0D,0x0A,0x7B,0x0D,0x0A,0x20,0x20,0x20,0x20,0x70,0x72,
0x69,0x6E,0x74,0x66,0x28,0x22,0x48,0x65,0x6C,0x6C,0x6F,0x2C,0x20,0x77,0x6F,
0x72,0x6C,0x64,0x21,0x5C,0x6E,0x22,0x29,0x3B,0x0D,0x0A,0x7D,0x0D,0x0A,0x23,
0x69,0x6E,0x63,0x6C,0x75,0x64,0x65,0x20,0x3C,0x73,0x74,0x64,0x69,0x6F,0x2E,
0x68,0x3E,0x0D,0x0A,0x0D,0x0A,0x76,0x6F,0x69,0x64,0x20,0x6D,0x61,0x69,0x6E,
0x28,0x76,0x6F,0x69,0x64,0x29,0x0D,0x0A,0x7B,0x0D,0x0A,0x20,0x20,0x20,0x20,
0x70,0x72,0x69,0x6E,0x74,0x66,0x28,0x22,0x57,0x65,0x6C,0x63,0x6F,0x6D,0x65,
0x21,0x5C,0x6E,0x22,0x29,0x3B,0x0D,0x0A,0x7D,0x0D,0x0A,0x0D,0x0A
}

Definition at line 111 of file cabd_memory.c.

Referenced by main().

◆ mem_system

struct mspack_system mem_system
static
Initial value:
= {
(struct mspack_file * (*)(struct mspack_system *, const char *, int)) &mem_open,
(void (*)(struct mspack_file *)) &mem_close,
(int (*)(struct mspack_file *, void *, int)) &mem_read,
(int (*)(struct mspack_file *, void *, int)) &mem_write,
(int (*)(struct mspack_file *, off_t, int)) &mem_seek,
(off_t (*)(struct mspack_file *)) &mem_tell,
(void (*)(struct mspack_file *, const char *, ...)) &mem_msg,
}
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 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
static int
Definition: sfsocketcall.h:114

Definition at line 90 of file cabd_memory.c.

Referenced by main().