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

Go to the source code of this file.

Classes

struct  mspack_file_p
 

Functions

int mspack_version (int entity)
 
int mspack_sys_selftest_internal (int offt_size)
 
int mspack_valid_system (struct mspack_system *sys)
 
int mspack_sys_filelen (struct mspack_system *system, struct mspack_file *file, off_t *length)
 
static struct mspack_filemsp_open (struct mspack_system *self, const char *filename, int mode)
 
static void msp_close (struct mspack_file *file)
 
static int msp_read (struct mspack_file *file, void *buffer, int bytes)
 
static int msp_write (struct mspack_file *file, void *buffer, int bytes)
 
static int msp_seek (struct mspack_file *file, off_t offset, int mode)
 
static off_t msp_tell (struct mspack_file *file)
 
static void msp_msg (struct mspack_file *file, const char *format,...)
 
static void * msp_alloc (struct mspack_system *self, size_t bytes)
 
static void msp_free (void *buffer)
 
static void msp_copy (void *src, void *dest, size_t bytes)
 

Variables

static struct mspack_system msp_system
 
struct mspack_systemmspack_default_system = &msp_system
 

Function Documentation

◆ msp_alloc()

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

Definition at line 201 of file system.c.

201  {
202 #if DEBUG
203  /* make uninitialised data obvious */
204  char *buf = malloc(bytes + 8);
205  if (buf) memset(buf, 0xDC, bytes);
206  *((size_t *)buf) = bytes;
207  return &buf[8];
208 #else
209  return malloc(bytes);
210 #endif
211 }
static ut8 bytes[32]
Definition: asm_arc.c:23
voidpf void * buf
Definition: ioapi.h:138
return memset(p, 0, total)
void * malloc(size_t size)
Definition: malloc.c:123

References bytes, malloc(), and memset().

◆ msp_close()

static void msp_close ( struct mspack_file file)
static

Definition at line 138 of file system.c.

138  {
139  struct mspack_file_p *self = (struct mspack_file_p *) file;
140  if (self) {
141  fclose(self->fh);
142  free(self);
143  }
144 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
Definition: gzappend.c:170

References free().

◆ msp_copy()

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

Definition at line 229 of file system.c.

229  {
230  memcpy(dest, src, bytes);
231 }
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.

◆ msp_free()

static void msp_free ( void *  buffer)
static

Definition at line 213 of file system.c.

213  {
214 #if DEBUG
215  char *buf = buffer;
216  size_t bytes;
217  if (buf) {
218  buf -= 8;
219  bytes = *((size_t *)buf);
220  /* make freed data obvious */
221  memset(buf, 0xED, bytes);
222  free(buf);
223  }
224 #else
225  free(buffer);
226 #endif
227 }
struct buffer buffer
Definition: buffer.h:15

References bytes, free(), and memset().

◆ msp_msg()

static void msp_msg ( struct mspack_file file,
const char *  format,
  ... 
)
static

Definition at line 191 of file system.c.

191  {
192  va_list ap;
193  if (file) fprintf(stderr, "%s: ", ((struct mspack_file_p *) file)->name);
194  va_start(ap, format);
195  vfprintf(stderr, format, ap);
196  va_end(ap);
197  fputc((int) '\n', stderr);
198  fflush(stderr);
199 }
Definition: z80asm.h:102

◆ msp_open()

static struct mspack_file* msp_open ( struct mspack_system self,
const char *  filename,
int  mode 
)
static

Definition at line 116 of file system.c.

118 {
119  struct mspack_file_p *fh;
120  const char *fmode;
121 
122  switch (mode) {
123  case MSPACK_SYS_OPEN_READ: fmode = "rb"; break;
124  case MSPACK_SYS_OPEN_WRITE: fmode = "wb"; break;
125  case MSPACK_SYS_OPEN_UPDATE: fmode = "r+b"; break;
126  case MSPACK_SYS_OPEN_APPEND: fmode = "ab"; break;
127  default: return NULL;
128  }
129 
130  if ((fh = (struct mspack_file_p *) malloc(sizeof(struct mspack_file_p)))) {
131  fh->name = filename;
132  if ((fh->fh = fopen(filename, fmode))) return (struct mspack_file *) fh;
133  free(fh);
134  }
135  return NULL;
136 }
#define MSPACK_SYS_OPEN_APPEND
Definition: mspack.h:464
#define MSPACK_SYS_OPEN_WRITE
Definition: mspack.h:460
#define MSPACK_SYS_OPEN_READ
Definition: mspack.h:458
#define MSPACK_SYS_OPEN_UPDATE
Definition: mspack.h:462
FILE * fh
Definition: cabinfo.c:52
#define NULL
Definition: cris-opc.c:27
const char * filename
Definition: ioapi.h:137
const char int mode
Definition: ioapi.h:137

References fh, free(), malloc(), MSPACK_SYS_OPEN_APPEND, MSPACK_SYS_OPEN_READ, MSPACK_SYS_OPEN_UPDATE, MSPACK_SYS_OPEN_WRITE, and NULL.

◆ msp_read()

static int msp_read ( struct mspack_file file,
void *  buffer,
int  bytes 
)
static

Definition at line 146 of file system.c.

146  {
147  struct mspack_file_p *self = (struct mspack_file_p *) file;
148  if (self && buffer && bytes >= 0) {
149  size_t count = fread(buffer, 1, (size_t) bytes, self->fh);
150  if (!ferror(self->fh)) return (int) count;
151  }
152  return -1;
153 }
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 count
Definition: sflib.h:98

References bytes, and count.

◆ msp_seek()

static int msp_seek ( struct mspack_file file,
off_t  offset,
int  mode 
)
static

Definition at line 164 of file system.c.

164  {
165  struct mspack_file_p *self = (struct mspack_file_p *) file;
166  if (self) {
167  switch (mode) {
168  case MSPACK_SYS_SEEK_START: mode = SEEK_SET; break;
169  case MSPACK_SYS_SEEK_CUR: mode = SEEK_CUR; break;
170  case MSPACK_SYS_SEEK_END: mode = SEEK_END; break;
171  default: return -1;
172  }
173 #if HAVE_FSEEKO
174  return fseeko(self->fh, offset, mode);
175 #else
176  return fseek(self->fh, offset, mode);
177 #endif
178  }
179  return -1;
180 }
#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
#define fseeko(s, o, w)
Definition: compat.h:121
voidpf uLong offset
Definition: ioapi.h:144
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84

References fseeko, MSPACK_SYS_SEEK_CUR, MSPACK_SYS_SEEK_END, MSPACK_SYS_SEEK_START, SEEK_CUR, SEEK_END, and SEEK_SET.

◆ msp_tell()

static off_t msp_tell ( struct mspack_file file)
static

Definition at line 182 of file system.c.

182  {
183  struct mspack_file_p *self = (struct mspack_file_p *) file;
184 #if HAVE_FSEEKO
185  return (self) ? (off_t) ftello(self->fh) : 0;
186 #else
187  return (self) ? (off_t) ftell(self->fh) : 0;
188 #endif
189 }
#define ftello(s)
Definition: compat.h:125
int off_t
Definition: sftypes.h:41

References ftello.

◆ msp_write()

static int msp_write ( struct mspack_file file,
void *  buffer,
int  bytes 
)
static

Definition at line 155 of file system.c.

155  {
156  struct mspack_file_p *self = (struct mspack_file_p *) file;
157  if (self && buffer && bytes >= 0) {
158  size_t count = fwrite(buffer, 1, (size_t) bytes, self->fh);
159  if (!ferror(self->fh)) return (int) count;
160  }
161  return -1;
162 }

References bytes, and count.

◆ mspack_sys_filelen()

int mspack_sys_filelen ( struct mspack_system system,
struct mspack_file file,
off_t length 
)

Definition at line 66 of file system.c.

68 {
69  off_t current;
70 
71  if (!system || !file || !length) return MSPACK_ERR_OPEN;
72 
73  /* get current offset */
74  current = system->tell(file);
75 
76  /* seek to end of file */
77  if (system->seek(file, (off_t) 0, MSPACK_SYS_SEEK_END)) {
78  return MSPACK_ERR_SEEK;
79  }
80 
81  /* get offset of end of file */
82  *length = system->tell(file);
83 
84  /* seek back to original offset */
85  if (system->seek(file, current, MSPACK_SYS_SEEK_START)) {
86  return MSPACK_ERR_SEEK;
87  }
88 
89  return MSPACK_ERR_OK;
90 }
#define MSPACK_ERR_SEEK
Definition: mspack.h:495
#define MSPACK_ERR_OK
Definition: mspack.h:485
#define MSPACK_ERR_OPEN
Definition: mspack.h:489
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
int(* seek)(struct mspack_file *file, off_t offset, int mode)
Definition: mspack.h:380
off_t(* tell)(struct mspack_file *file)
Definition: mspack.h:391

References length, MSPACK_ERR_OK, MSPACK_ERR_OPEN, MSPACK_ERR_SEEK, MSPACK_SYS_SEEK_END, MSPACK_SYS_SEEK_START, mspack_system::seek, and mspack_system::tell.

Referenced by cabd_search().

◆ mspack_sys_selftest_internal()

int mspack_sys_selftest_internal ( int  offt_size)

Part of the MSPACK_SYS_SELFTEST() macro, must not be used directly.

Definition at line 53 of file system.c.

53  {
54  return (sizeof(off_t) == offt_size) ? MSPACK_ERR_OK : MSPACK_ERR_SEEK;
55 }

◆ mspack_valid_system()

int mspack_valid_system ( struct mspack_system sys)

Definition at line 58 of file system.c.

58  {
59  return (sys != NULL) && (sys->open != NULL) && (sys->close != NULL) &&
60  (sys->read != NULL) && (sys->write != NULL) && (sys->seek != NULL) &&
61  (sys->tell != NULL) && (sys->message != NULL) && (sys->alloc != NULL) &&
62  (sys->free != NULL) && (sys->copy != NULL) && (sys->null_ptr == NULL);
63 }
void(* copy)(void *src, void *dest, size_t bytes)
Definition: mspack.h:444
void(* close)(struct mspack_file *file)
Definition: mspack.h:321
struct mspack_file *(* open)(struct mspack_system *self, const char *filename, int mode)
Definition: mspack.h:310
void(* message)(struct mspack_file *file, const char *format,...)
Definition: mspack.h:407
void(* free)(void *ptr)
Definition: mspack.h:430
int(* read)(struct mspack_file *file, void *buffer, int bytes)
Definition: mspack.h:336
int(* write)(struct mspack_file *file, void *buffer, int bytes)
Definition: mspack.h:353
void * null_ptr
Definition: mspack.h:454
void *(* alloc)(struct mspack_system *self, size_t bytes)
Definition: mspack.h:421

References mspack_system::alloc, mspack_system::close, mspack_system::copy, mspack_system::free, mspack_system::message, NULL, mspack_system::null_ptr, mspack_system::open, mspack_system::read, mspack_system::seek, mspack_system::tell, and mspack_system::write.

Referenced by mspack_create_cab_decompressor(), mspack_create_chm_decompressor(), mspack_create_kwaj_decompressor(), mspack_create_oab_decompressor(), and mspack_create_szdd_decompressor().

◆ mspack_version()

int mspack_version ( int  entity)

Enquire about the binary compatibility version of a specific interface in the library. Currently, the following interfaces are defined:

The result of the function should be interpreted as follows:

  • -1: this interface is completely unknown to the library
  • 0: this interface is known, but non-functioning
  • 1: this interface has all basic functionality
  • 2, 3, ...: this interface has additional functionality, clearly marked in the documentation as "version 2", "version 3" and so on.
Parameters
entitythe interface to request current version of
Returns
the version of the requested interface

Definition at line 16 of file system.c.

16  {
17  switch (entity) {
18  /* CHM decoder version 1 -> 2 changes:
19  * - added mschmd_sec_mscompressed::spaninfo
20  * - added mschmd_header::first_pmgl
21  * - added mschmd_header::last_pmgl
22  * - added mschmd_header::chunk_cache;
23  */
24  case MSPACK_VER_MSCHMD:
25  /* CAB decoder version 1 -> 2 changes:
26  * - added MSCABD_PARAM_SALVAGE
27  */
28  case MSPACK_VER_MSCABD:
29  /* OAB decoder version 1 -> 2 changes:
30  * - added msoab_decompressor::set_param and MSOABD_PARAM_DECOMPBUF
31  */
32  case MSPACK_VER_MSOABD:
33  return 2;
34  case MSPACK_VER_LIBRARY:
35  case MSPACK_VER_SYSTEM:
36  case MSPACK_VER_MSSZDDD:
37  case MSPACK_VER_MSKWAJD:
38  return 1;
39  case MSPACK_VER_MSCABC:
40  case MSPACK_VER_MSCHMC:
41  case MSPACK_VER_MSLITD:
42  case MSPACK_VER_MSLITC:
43  case MSPACK_VER_MSHLPD:
44  case MSPACK_VER_MSHLPC:
45  case MSPACK_VER_MSSZDDC:
46  case MSPACK_VER_MSKWAJC:
47  case MSPACK_VER_MSOABC:
48  return 0;
49  }
50  return -1;
51 }
#define MSPACK_VER_MSOABD
Definition: mspack.h:260
#define MSPACK_VER_MSCABD
Definition: mspack.h:236
#define MSPACK_VER_MSCHMC
Definition: mspack.h:242
#define MSPACK_VER_SYSTEM
Definition: mspack.h:234
#define MSPACK_VER_MSOABC
Definition: mspack.h:262
#define MSPACK_VER_MSKWAJD
Definition: mspack.h:256
#define MSPACK_VER_MSLITD
Definition: mspack.h:244
#define MSPACK_VER_MSKWAJC
Definition: mspack.h:258
#define MSPACK_VER_MSHLPC
Definition: mspack.h:250
#define MSPACK_VER_LIBRARY
Definition: mspack.h:232
#define MSPACK_VER_MSCHMD
Definition: mspack.h:240
#define MSPACK_VER_MSLITC
Definition: mspack.h:246
#define MSPACK_VER_MSCABC
Definition: mspack.h:238
#define MSPACK_VER_MSSZDDD
Definition: mspack.h:252
#define MSPACK_VER_MSHLPD
Definition: mspack.h:248
#define MSPACK_VER_MSSZDDC
Definition: mspack.h:254

Variable Documentation

◆ msp_system

struct mspack_system msp_system
static
Initial value:
= {
}
static struct mspack_file * msp_open(struct mspack_system *self, const char *filename, int mode)
Definition: system.c:116
static void msp_msg(struct mspack_file *file, const char *format,...)
Definition: system.c:191
static void msp_copy(void *src, void *dest, size_t bytes)
Definition: system.c:229
static int msp_write(struct mspack_file *file, void *buffer, int bytes)
Definition: system.c:155
static int msp_read(struct mspack_file *file, void *buffer, int bytes)
Definition: system.c:146
static off_t msp_tell(struct mspack_file *file)
Definition: system.c:182
static int msp_seek(struct mspack_file *file, off_t offset, int mode)
Definition: system.c:164
static void * msp_alloc(struct mspack_system *self, size_t bytes)
Definition: system.c:201
static void msp_free(void *buffer)
Definition: system.c:213
static void msp_close(struct mspack_file *file)
Definition: system.c:138

Definition at line 229 of file system.c.

◆ mspack_default_system