Rizin
unix-like reverse engineering framework and cli tools
szddd.c File Reference
#include <system.h>
#include <szdd.h>

Go to the source code of this file.

Functions

static struct msszddd_headerszddd_open (struct msszdd_decompressor *base, const char *filename)
 
static void szddd_close (struct msszdd_decompressor *base, struct msszddd_header *hdr)
 
static int szddd_read_headers (struct mspack_system *sys, struct mspack_file *fh, struct msszddd_header *hdr)
 
static int szddd_extract (struct msszdd_decompressor *base, struct msszddd_header *hdr, const char *filename)
 
static int szddd_decompress (struct msszdd_decompressor *base, const char *input, const char *output)
 
static int szddd_error (struct msszdd_decompressor *base)
 
struct msszdd_decompressormspack_create_szdd_decompressor (struct mspack_system *sys)
 
void mspack_destroy_szdd_decompressor (struct msszdd_decompressor *base)
 

Variables

static unsigned char szdd_signature_expand [8]
 
static unsigned char szdd_signature_qbasic [8]
 

Function Documentation

◆ mspack_create_szdd_decompressor()

struct msszdd_decompressor* mspack_create_szdd_decompressor ( struct mspack_system sys)

Creates a new SZDD decompressor.

Parameters
sysa custom mspack_system structure, or NULL to use the default
Returns
a msszdd_decompressor or NULL

Definition at line 41 of file szddd.c.

42 {
43  struct msszdd_decompressor_p *self = NULL;
44 
45  if (!sys) sys = mspack_default_system;
46  if (!mspack_valid_system(sys)) return NULL;
47 
48  if ((self = (struct msszdd_decompressor_p *) sys->alloc(sys, sizeof(struct msszdd_decompressor_p)))) {
49  self->base.open = &szddd_open;
50  self->base.close = &szddd_close;
51  self->base.extract = &szddd_extract;
52  self->base.decompress = &szddd_decompress;
53  self->base.last_error = &szddd_error;
54  self->system = sys;
55  self->error = MSPACK_ERR_OK;
56  }
57  return (struct msszdd_decompressor *) self;
58 }
#define MSPACK_ERR_OK
Definition: mspack.h:485
struct mspack_system * mspack_default_system
Definition: system.c:238
int mspack_valid_system(struct mspack_system *sys)
Definition: system.c:58
#define NULL
Definition: cris-opc.c:27
void *(* alloc)(struct mspack_system *self, size_t bytes)
Definition: mspack.h:421
static void szddd_close(struct msszdd_decompressor *base, struct msszddd_header *hdr)
Definition: szddd.c:115
static struct msszddd_header * szddd_open(struct msszdd_decompressor *base, const char *filename)
Definition: szddd.c:79
static int szddd_error(struct msszdd_decompressor *base)
Definition: szddd.c:243
static int szddd_extract(struct msszdd_decompressor *base, struct msszddd_header *hdr, const char *filename)
Definition: szddd.c:181
static int szddd_decompress(struct msszdd_decompressor *base, const char *input, const char *output)
Definition: szddd.c:223

References mspack_system::alloc, mspack_default_system, MSPACK_ERR_OK, mspack_valid_system(), NULL, szddd_close(), szddd_decompress(), szddd_error(), szddd_extract(), and szddd_open().

Referenced by main().

◆ mspack_destroy_szdd_decompressor()

void mspack_destroy_szdd_decompressor ( struct msszdd_decompressor self)

Destroys an existing SZDD decompressor.

Parameters
selfthe msszdd_decompressor to destroy

Definition at line 65 of file szddd.c.

66 {
67  struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
68  if (self) {
69  struct mspack_system *sys = self->system;
70  sys->free(self);
71  }
72 }
void(* free)(void *ptr)
Definition: mspack.h:430
struct msszdd_decompressor base
Definition: szdd.h:29

References msszdd_decompressor_p::base, and mspack_system::free.

Referenced by main().

◆ szddd_close()

static void szddd_close ( struct msszdd_decompressor base,
struct msszddd_header hdr 
)
static

Definition at line 115 of file szddd.c.

117 {
118  struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
119  struct msszddd_header_p *hdr_p = (struct msszddd_header_p *) hdr;
120 
121  if (!self || !self->system) return;
122 
123  /* close the file handle associated */
124  self->system->close(hdr_p->fh);
125 
126  /* free the memory associated */
127  self->system->free(hdr);
128 
129  self->error = MSPACK_ERR_OK;
130 }
struct mspack_file * fh
Definition: szdd.h:36

References msszdd_decompressor_p::base, msszddd_header_p::fh, and MSPACK_ERR_OK.

Referenced by mspack_create_szdd_decompressor(), and szddd_decompress().

◆ szddd_decompress()

static int szddd_decompress ( struct msszdd_decompressor base,
const char *  input,
const char *  output 
)
static

Definition at line 223 of file szddd.c.

225 {
226  struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
227  struct msszddd_header *hdr;
228  int error;
229 
230  if (!self) return MSPACK_ERR_ARGS;
231 
232  if (!(hdr = szddd_open(base, input))) return self->error;
233  error = szddd_extract(base, hdr, output);
234  szddd_close(base, hdr);
235  return self->error = error;
236 }
#define MSPACK_ERR_ARGS
Definition: mspack.h:487
void error(const char *msg)
Definition: untgz.c:593
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237

References msszdd_decompressor_p::base, error(), input(), MSPACK_ERR_ARGS, output, szddd_close(), szddd_extract(), and szddd_open().

Referenced by mspack_create_szdd_decompressor().

◆ szddd_error()

static int szddd_error ( struct msszdd_decompressor base)
static

Definition at line 243 of file szddd.c.

244 {
245  struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
246  return (self) ? self->error : MSPACK_ERR_ARGS;
247 }

References msszdd_decompressor_p::base, and MSPACK_ERR_ARGS.

Referenced by mspack_create_szdd_decompressor().

◆ szddd_extract()

static int szddd_extract ( struct msszdd_decompressor base,
struct msszddd_header hdr,
const char *  filename 
)
static

Definition at line 181 of file szddd.c.

183 {
184  struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
185  struct mspack_file *fh, *outfh;
186  struct mspack_system *sys;
187  off_t data_offset;
188 
189  if (!self) return MSPACK_ERR_ARGS;
190  if (!hdr) return self->error = MSPACK_ERR_ARGS;
191  sys = self->system;
192 
193  fh = ((struct msszddd_header_p *) hdr)->fh;
194 
195  /* seek to the compressed data */
196  data_offset = (hdr->format == MSSZDD_FMT_NORMAL) ? 14 : 12;
197  if (sys->seek(fh, data_offset, MSPACK_SYS_SEEK_START)) {
198  return self->error = MSPACK_ERR_SEEK;
199  }
200 
201  /* open file for output */
202  if (!(outfh = sys->open(sys, filename, MSPACK_SYS_OPEN_WRITE))) {
203  return self->error = MSPACK_ERR_OPEN;
204  }
205 
206  /* decompress the data */
207  self->error = lzss_decompress(sys, fh, outfh, SZDD_INPUT_SIZE,
208  hdr->format == MSSZDD_FMT_NORMAL
210  : LZSS_MODE_QBASIC);
211 
212  /* close output file */
213  sys->close(outfh);
214 
215  return self->error;
216 }
#define MSPACK_ERR_SEEK
Definition: mspack.h:495
#define MSSZDD_FMT_NORMAL
Definition: mspack.h:1758
#define MSPACK_ERR_OPEN
Definition: mspack.h:489
#define MSPACK_SYS_OPEN_WRITE
Definition: mspack.h:460
#define MSPACK_SYS_SEEK_START
Definition: mspack.h:467
FILE * fh
Definition: cabinfo.c:52
const char * filename
Definition: ioapi.h:137
int lzss_decompress(struct mspack_system *system, struct mspack_file *input, struct mspack_file *output, int input_buffer_size, int mode)
Definition: lzssd.c:37
#define LZSS_MODE_EXPAND
Definition: lzss.h:22
#define LZSS_MODE_QBASIC
Definition: lzss.h:24
int off_t
Definition: sftypes.h:41
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
int(* seek)(struct mspack_file *file, off_t offset, int mode)
Definition: mspack.h:380
#define SZDD_INPUT_SIZE
Definition: szdd.h:16

References msszdd_decompressor_p::base, mspack_system::close, fh, msszddd_header::format, lzss_decompress(), LZSS_MODE_EXPAND, LZSS_MODE_QBASIC, MSPACK_ERR_ARGS, MSPACK_ERR_OPEN, MSPACK_ERR_SEEK, MSPACK_SYS_OPEN_WRITE, MSPACK_SYS_SEEK_START, MSSZDD_FMT_NORMAL, mspack_system::open, mspack_system::seek, and SZDD_INPUT_SIZE.

Referenced by mspack_create_szdd_decompressor(), and szddd_decompress().

◆ szddd_open()

static struct msszddd_header * szddd_open ( struct msszdd_decompressor base,
const char *  filename 
)
static

Definition at line 79 of file szddd.c.

81 {
82  struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
83  struct msszddd_header *hdr;
84  struct mspack_system *sys;
85  struct mspack_file *fh;
86 
87  if (!self) return NULL;
88  sys = self->system;
89 
90  fh = sys->open(sys, filename, MSPACK_SYS_OPEN_READ);
91  hdr = (struct msszddd_header *) sys->alloc(sys, sizeof(struct msszddd_header_p));
92  if (fh && hdr) {
93  ((struct msszddd_header_p *) hdr)->fh = fh;
94  self->error = szddd_read_headers(sys, fh, hdr);
95  }
96  else {
97  if (!fh) self->error = MSPACK_ERR_OPEN;
98  if (!hdr) self->error = MSPACK_ERR_NOMEMORY;
99  }
100 
101  if (self->error) {
102  if (fh) sys->close(fh);
103  sys->free(hdr);
104  hdr = NULL;
105  }
106 
107  return hdr;
108 }
#define MSPACK_SYS_OPEN_READ
Definition: mspack.h:458
#define MSPACK_ERR_NOMEMORY
Definition: mspack.h:497
static int szddd_read_headers(struct mspack_system *sys, struct mspack_file *fh, struct msszddd_header *hdr)
Definition: szddd.c:144

References mspack_system::alloc, msszdd_decompressor_p::base, mspack_system::close, fh, mspack_system::free, MSPACK_ERR_NOMEMORY, MSPACK_ERR_OPEN, MSPACK_SYS_OPEN_READ, NULL, mspack_system::open, and szddd_read_headers().

Referenced by mspack_create_szdd_decompressor(), and szddd_decompress().

◆ szddd_read_headers()

static int szddd_read_headers ( struct mspack_system sys,
struct mspack_file fh,
struct msszddd_header hdr 
)
static

Definition at line 144 of file szddd.c.

147 {
148  unsigned char buf[8];
149 
150  /* read and check signature */
151  if (sys->read(fh, buf, 8) != 8) return MSPACK_ERR_READ;
152 
153  if ((memcmp(buf, szdd_signature_expand, 8) == 0)) {
154  /* common SZDD */
155  hdr->format = MSSZDD_FMT_NORMAL;
156 
157  /* read the rest of the header */
158  if (sys->read(fh, buf, 6) != 6) return MSPACK_ERR_READ;
159  if (buf[0] != 0x41) return MSPACK_ERR_DATAFORMAT;
160  hdr->missing_char = buf[1];
161  hdr->length = EndGetI32(&buf[2]);
162  }
163  else if ((memcmp(buf, szdd_signature_qbasic, 8) == 0)) {
164  /* special QBasic SZDD */
165  hdr->format = MSSZDD_FMT_QBASIC;
166  if (sys->read(fh, buf, 4) != 4) return MSPACK_ERR_READ;
167  hdr->missing_char = '\0';
168  hdr->length = EndGetI32(buf);
169  }
170  else {
171  return MSPACK_ERR_SIGNATURE;
172  }
173  return MSPACK_ERR_OK;
174 }
#define MSPACK_ERR_SIGNATURE
Definition: mspack.h:499
#define MSPACK_ERR_DATAFORMAT
Definition: mspack.h:501
#define MSSZDD_FMT_QBASIC
Definition: mspack.h:1761
#define MSPACK_ERR_READ
Definition: mspack.h:491
voidpf void * buf
Definition: ioapi.h:138
#define EndGetI32(a)
Definition: macros.h:37
int(* read)(struct mspack_file *file, void *buffer, int bytes)
Definition: mspack.h:336
off_t length
Definition: mspack.h:1773
char missing_char
Definition: mspack.h:1782
static unsigned char szdd_signature_expand[8]
Definition: szddd.c:137
static unsigned char szdd_signature_qbasic[8]
Definition: szddd.c:140

References EndGetI32, fh, msszddd_header::format, msszddd_header::length, msszddd_header::missing_char, MSPACK_ERR_DATAFORMAT, MSPACK_ERR_OK, MSPACK_ERR_READ, MSPACK_ERR_SIGNATURE, MSSZDD_FMT_NORMAL, MSSZDD_FMT_QBASIC, mspack_system::read, szdd_signature_expand, and szdd_signature_qbasic.

Referenced by szddd_open().

Variable Documentation

◆ szdd_signature_expand

unsigned char szdd_signature_expand[8]
static
Initial value:
= {
0x53, 0x5A, 0x44, 0x44, 0x88, 0xF0, 0x27, 0x33
}

Definition at line 137 of file szddd.c.

Referenced by szddd_read_headers().

◆ szdd_signature_qbasic

unsigned char szdd_signature_qbasic[8]
static
Initial value:
= {
0x53, 0x5A, 0x20, 0x88, 0xF0, 0x27, 0x33, 0xD1
}

Definition at line 140 of file szddd.c.

Referenced by szddd_read_headers().