Rizin
unix-like reverse engineering framework and cli tools
system.c
Go to the documentation of this file.
1 /* This file is part of libmspack.
2  * (C) 2003-2004 Stuart Caie.
3  *
4  * libmspack is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License (LGPL) version 2.1
6  *
7  * For further details, see the file COPYING.LIB distributed with libmspack
8  */
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 #include <system.h>
15 
16 int mspack_version(int entity) {
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 }
52 
53 int mspack_sys_selftest_internal(int offt_size) {
54  return (sizeof(off_t) == offt_size) ? MSPACK_ERR_OK : MSPACK_ERR_SEEK;
55 }
56 
57 /* validates a system structure */
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 }
64 
65 /* returns the length of a file opened for reading */
66 int mspack_sys_filelen(struct mspack_system *system,
67  struct mspack_file *file, off_t *length)
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 }
91 
92 
93 
94 /* definition of mspack_default_system -- if the library is compiled with
95  * MSPACK_NO_DEFAULT_SYSTEM, no default system will be provided. Otherwise,
96  * an appropriate default system (e.g. the standard C library, or some native
97  * API calls)
98  */
99 
100 #ifdef MSPACK_NO_DEFAULT_SYSTEM
102 #else
103 
104 /* implementation of mspack_default_system for standard C library */
105 
106 #include <stdio.h>
107 #include <stdlib.h>
108 #include <string.h>
109 #include <stdarg.h>
110 
113  const char *name;
114 };
115 
116 static struct mspack_file *msp_open(struct mspack_system *self,
117  const char *filename, int mode)
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 }
137 
138 static void msp_close(struct mspack_file *file) {
139  struct mspack_file_p *self = (struct mspack_file_p *) file;
140  if (self) {
141  fclose(self->fh);
142  free(self);
143  }
144 }
145 
146 static int msp_read(struct mspack_file *file, void *buffer, int bytes) {
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 }
154 
155 static int msp_write(struct mspack_file *file, void *buffer, int bytes) {
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 }
163 
164 static int msp_seek(struct mspack_file *file, off_t offset, int mode) {
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 }
181 
182 static off_t msp_tell(struct mspack_file *file) {
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 }
190 
191 static void msp_msg(struct mspack_file *file, const char *format, ...) {
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 }
200 
201 static void *msp_alloc(struct mspack_system *self, size_t bytes) {
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 }
212 
213 static void msp_free(void *buffer) {
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 }
228 
229 static void msp_copy(void *src, void *dest, size_t bytes) {
230  memcpy(dest, src, bytes);
231 }
232 
233 static struct mspack_system msp_system = {
236 };
237 
239 
240 #endif
lzma_index * src
Definition: index.h:567
static ut8 bytes[32]
Definition: asm_arc.c:23
struct buffer buffer
#define MSPACK_SYS_OPEN_APPEND
Definition: mspack.h:464
#define MSPACK_ERR_SEEK
Definition: mspack.h:495
#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_SYS_SEEK_END
Definition: mspack.h:471
#define MSPACK_VER_SYSTEM
Definition: mspack.h:234
#define MSPACK_ERR_OK
Definition: mspack.h:485
#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_ERR_OPEN
Definition: mspack.h:489
#define MSPACK_VER_MSKWAJC
Definition: mspack.h:258
#define MSPACK_SYS_OPEN_WRITE
Definition: mspack.h:460
#define MSPACK_VER_MSHLPC
Definition: mspack.h:250
#define MSPACK_SYS_SEEK_START
Definition: mspack.h:467
#define MSPACK_SYS_OPEN_READ
Definition: mspack.h:458
#define MSPACK_VER_LIBRARY
Definition: mspack.h:232
#define MSPACK_VER_MSCHMD
Definition: mspack.h:240
#define MSPACK_SYS_SEEK_CUR
Definition: mspack.h:469
#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_SYS_OPEN_UPDATE
Definition: mspack.h:462
#define MSPACK_VER_MSHLPD
Definition: mspack.h:248
#define MSPACK_VER_MSSZDDC
Definition: mspack.h:254
static struct mspack_file * msp_open(struct mspack_system *self, const char *filename, int mode)
Definition: system.c:116
int mspack_sys_filelen(struct mspack_system *system, struct mspack_file *file, off_t *length)
Definition: system.c:66
int mspack_version(int entity)
Definition: system.c:16
static void msp_msg(struct mspack_file *file, const char *format,...)
Definition: system.c:191
struct mspack_system * mspack_default_system
Definition: system.c:238
int mspack_valid_system(struct mspack_system *sys)
Definition: system.c:58
static struct mspack_system msp_system
Definition: system.c:233
static void msp_copy(void *src, void *dest, size_t bytes)
Definition: system.c:229
int mspack_sys_selftest_internal(int offt_size)
Definition: system.c:53
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
FILE * fh
Definition: cabinfo.c:52
#define fseeko(s, o, w)
Definition: compat.h:121
#define ftello(s)
Definition: compat.h:125
#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 count
Definition: sflib.h:98
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
const char * filename
Definition: ioapi.h:137
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
char * dest
Definition: lz4.h:697
string FILE
Definition: benchmark.py:21
int off_t
Definition: sftypes.h:41
Definition: buffer.h:15
Definition: gzappend.c:170
FILE * fh
Definition: system.c:112
const char * name
Definition: system.c:113
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
int(* seek)(struct mspack_file *file, off_t offset, int mode)
Definition: mspack.h:380
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
off_t(* tell)(struct mspack_file *file)
Definition: mspack.h:391
void *(* alloc)(struct mspack_system *self, size_t bytes)
Definition: mspack.h:421
Definition: z80asm.h:102
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84