Rizin
unix-like reverse engineering framework and cli tools
kwajd.c File Reference
#include <system.h>
#include <kwaj.h>
#include <mszip.h>
#include <readbits.h>
#include <readhuff.h>

Go to the source code of this file.

Macros

#define BITS_TYPE   struct kwajd_stream
 
#define BITS_VAR   lzh
 
#define BITS_ORDER_MSB
 
#define BITS_NO_READ_INPUT
 
#define READ_BYTES
 
#define TABLEBITS(tbl)   KWAJ_TABLEBITS
 
#define MAXSYMBOLS(tbl)   KWAJ_##tbl##_SYMS
 
#define HUFF_TABLE(tbl, idx)   lzh->tbl##_table[idx]
 
#define HUFF_LEN(tbl, idx)   lzh->tbl##_len[idx]
 
#define HUFF_ERROR   return MSPACK_ERR_DATAFORMAT
 
#define READ_BITS_SAFE(val, n)
 
#define READ_HUFFSYM_SAFE(tbl, val)
 
#define BUILD_TREE(tbl, type)
 
#define WRITE_BYTE
 

Functions

static struct mskwajd_headerkwajd_open (struct mskwaj_decompressor *base, const char *filename)
 
static void kwajd_close (struct mskwaj_decompressor *base, struct mskwajd_header *hdr)
 
static int kwajd_read_headers (struct mspack_system *sys, struct mspack_file *fh, struct mskwajd_header *hdr)
 
static int kwajd_extract (struct mskwaj_decompressor *base, struct mskwajd_header *hdr, const char *filename)
 
static int kwajd_decompress (struct mskwaj_decompressor *base, const char *input, const char *output)
 
static int kwajd_error (struct mskwaj_decompressor *base)
 
static struct kwajd_streamlzh_init (struct mspack_system *sys, struct mspack_file *in, struct mspack_file *out)
 
static int lzh_decompress (struct kwajd_stream *kwaj)
 
static void lzh_free (struct kwajd_stream *kwaj)
 
static int lzh_read_lens (struct kwajd_stream *kwaj, unsigned int type, unsigned int numsyms, unsigned char *lens)
 
static int lzh_read_input (struct kwajd_stream *kwaj)
 
struct mskwaj_decompressormspack_create_kwaj_decompressor (struct mspack_system *sys)
 
void mspack_destroy_kwaj_decompressor (struct mskwaj_decompressor *base)
 

Macro Definition Documentation

◆ BITS_NO_READ_INPUT

#define BITS_NO_READ_INPUT

Definition at line 367 of file kwajd.c.

◆ BITS_ORDER_MSB

#define BITS_ORDER_MSB

Definition at line 366 of file kwajd.c.

◆ BITS_TYPE

#define BITS_TYPE   struct kwajd_stream

Definition at line 364 of file kwajd.c.

◆ BITS_VAR

#define BITS_VAR   lzh

Definition at line 365 of file kwajd.c.

◆ BUILD_TREE

#define BUILD_TREE (   tbl,
  type 
)
Value:
err = lzh_read_lens(lzh, type, MAXSYMBOLS(tbl), &HUFF_LEN(tbl,0)); \
if (err) return err; \
RESTORE_BITS; \
&HUFF_LEN(tbl,0), &HUFF_TABLE(tbl,0))) \
static bool err
Definition: armass.c:435
#define MSPACK_ERR_DATAFORMAT
Definition: mspack.h:501
#define STORE_BITS
Definition: readbits.h:111
static int make_decode_table(unsigned int nsyms, unsigned int nbits, unsigned char *length, unsigned short *table)
Definition: readhuff.h:78
#define HUFF_TABLE(tbl, idx)
Definition: kwajd.c:381
#define MAXSYMBOLS(tbl)
Definition: kwajd.c:380
#define HUFF_LEN(tbl, idx)
Definition: kwajd.c:382
static int lzh_read_lens(struct kwajd_stream *kwaj, unsigned int type, unsigned int numsyms, unsigned char *lens)
Definition: kwajd.c:500
#define TABLEBITS(tbl)
Definition: kwajd.c:379
int type
Definition: mipsasm.c:17

Definition at line 408 of file kwajd.c.

◆ HUFF_ERROR

#define HUFF_ERROR   return MSPACK_ERR_DATAFORMAT

Definition at line 383 of file kwajd.c.

◆ HUFF_LEN

#define HUFF_LEN (   tbl,
  idx 
)    lzh->tbl##_len[idx]

Definition at line 382 of file kwajd.c.

◆ HUFF_TABLE

#define HUFF_TABLE (   tbl,
  idx 
)    lzh->tbl##_table[idx]

Definition at line 381 of file kwajd.c.

◆ MAXSYMBOLS

#define MAXSYMBOLS (   tbl)    KWAJ_##tbl##_SYMS

Definition at line 380 of file kwajd.c.

◆ READ_BITS_SAFE

#define READ_BITS_SAFE (   val,
  n 
)
Value:
do { \
READ_BITS(val, n); \
if (lzh->input_end && bits_left < lzh->input_end) \
return MSPACK_ERR_OK; \
} while (0)
ut16 val
Definition: armass64_const.h:6
#define MSPACK_ERR_OK
Definition: mspack.h:485
int n
Definition: mipsasm.c:19

Definition at line 396 of file kwajd.c.

◆ READ_BYTES

#define READ_BYTES
Value:
do { \
if (i_ptr >= i_end) { \
if ((err = lzh_read_input(lzh))) return err; \
i_ptr = lzh->i_ptr; \
i_end = lzh->i_end; \
} \
INJECT_BITS(*i_ptr++, 8); \
} while (0)
static int lzh_read_input(struct kwajd_stream *kwaj)
Definition: kwajd.c:545

Definition at line 368 of file kwajd.c.

◆ READ_HUFFSYM_SAFE

#define READ_HUFFSYM_SAFE (   tbl,
  val 
)
Value:
do { \
READ_HUFFSYM(tbl, val); \
if (lzh->input_end && bits_left < lzh->input_end) \
return MSPACK_ERR_OK; \
} while (0)

Definition at line 402 of file kwajd.c.

◆ TABLEBITS

#define TABLEBITS (   tbl)    KWAJ_TABLEBITS

Definition at line 379 of file kwajd.c.

◆ WRITE_BYTE

#define WRITE_BYTE
Value:
do { \
if (lzh->sys->write(lzh->output, &lzh->window[pos], 1) != 1) \
return MSPACK_ERR_WRITE; \
} while (0)
#define MSPACK_ERR_WRITE
Definition: mspack.h:493
int pos
Definition: main.c:11

Definition at line 417 of file kwajd.c.

Function Documentation

◆ kwajd_close()

static void kwajd_close ( struct mskwaj_decompressor base,
struct mskwajd_header hdr 
)
static

Definition at line 129 of file kwajd.c.

131 {
132  struct mskwaj_decompressor_p *self = (struct mskwaj_decompressor_p *) base;
133  struct mskwajd_header_p *hdr_p = (struct mskwajd_header_p *) hdr;
134 
135  if (!self || !self->system) return;
136 
137  /* close the file handle associated */
138  self->system->close(hdr_p->fh);
139 
140  /* free the memory associated */
141  self->system->free(hdr);
142 
143  self->error = MSPACK_ERR_OK;
144 }
struct mskwaj_decompressor base
Definition: kwaj.h:36
struct mspack_file * fh
Definition: kwaj.h:43

References mskwaj_decompressor_p::base, mskwajd_header_p::fh, and MSPACK_ERR_OK.

Referenced by kwajd_decompress(), and mspack_create_kwaj_decompressor().

◆ kwajd_decompress()

static int kwajd_decompress ( struct mskwaj_decompressor base,
const char *  input,
const char *  output 
)
static

Definition at line 331 of file kwajd.c.

333 {
334  struct mskwaj_decompressor_p *self = (struct mskwaj_decompressor_p *) base;
335  struct mskwajd_header *hdr;
336  int error;
337 
338  if (!self) return MSPACK_ERR_ARGS;
339 
340  if (!(hdr = kwajd_open(base, input))) return self->error;
341  error = kwajd_extract(base, hdr, output);
342  kwajd_close(base, hdr);
343  return self->error = error;
344 }
#define MSPACK_ERR_ARGS
Definition: mspack.h:487
static struct mskwajd_header * kwajd_open(struct mskwaj_decompressor *base, const char *filename)
Definition: kwajd.c:93
static int kwajd_extract(struct mskwaj_decompressor *base, struct mskwajd_header *hdr, const char *filename)
Definition: kwajd.c:253
static void kwajd_close(struct mskwaj_decompressor *base, struct mskwajd_header *hdr)
Definition: kwajd.c:129
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 mskwaj_decompressor_p::base, error(), input(), kwajd_close(), kwajd_extract(), kwajd_open(), MSPACK_ERR_ARGS, and output.

Referenced by mspack_create_kwaj_decompressor().

◆ kwajd_error()

static int kwajd_error ( struct mskwaj_decompressor base)
static

Definition at line 351 of file kwajd.c.

352 {
353  struct mskwaj_decompressor_p *self = (struct mskwaj_decompressor_p *) base;
354  return (self) ? self->error : MSPACK_ERR_ARGS;
355 }

References mskwaj_decompressor_p::base, and MSPACK_ERR_ARGS.

Referenced by mspack_create_kwaj_decompressor().

◆ kwajd_extract()

static int kwajd_extract ( struct mskwaj_decompressor base,
struct mskwajd_header hdr,
const char *  filename 
)
static

Definition at line 253 of file kwajd.c.

255 {
256  struct mskwaj_decompressor_p *self = (struct mskwaj_decompressor_p *) base;
257  struct mspack_system *sys;
258  struct mspack_file *fh, *outfh;
259 
260  if (!self) return MSPACK_ERR_ARGS;
261  if (!hdr) return self->error = MSPACK_ERR_ARGS;
262 
263  sys = self->system;
264  fh = ((struct mskwajd_header_p *) hdr)->fh;
265 
266  /* seek to the compressed data */
267  if (sys->seek(fh, hdr->data_offset, MSPACK_SYS_SEEK_START)) {
268  return self->error = MSPACK_ERR_SEEK;
269  }
270 
271  /* open file for output */
272  if (!(outfh = sys->open(sys, filename, MSPACK_SYS_OPEN_WRITE))) {
273  return self->error = MSPACK_ERR_OPEN;
274  }
275 
276  self->error = MSPACK_ERR_OK;
277 
278  /* decompress based on format */
279  if (hdr->comp_type == MSKWAJ_COMP_NONE ||
280  hdr->comp_type == MSKWAJ_COMP_XOR)
281  {
282  /* NONE is a straight copy. XOR is a copy xored with 0xFF */
283  unsigned char *buf = (unsigned char *) sys->alloc(sys, (size_t) KWAJ_INPUT_SIZE);
284  if (buf) {
285  int read, i;
286  while ((read = sys->read(fh, buf, KWAJ_INPUT_SIZE)) > 0) {
287  if (hdr->comp_type == MSKWAJ_COMP_XOR) {
288  for (i = 0; i < read; i++) buf[i] ^= 0xFF;
289  }
290  if (sys->write(outfh, buf, read) != read) {
291  self->error = MSPACK_ERR_WRITE;
292  break;
293  }
294  }
295  if (read < 0) self->error = MSPACK_ERR_READ;
296  sys->free(buf);
297  }
298  else {
299  self->error = MSPACK_ERR_NOMEMORY;
300  }
301  }
302  else if (hdr->comp_type == MSKWAJ_COMP_SZDD) {
303  self->error = lzss_decompress(sys, fh, outfh, KWAJ_INPUT_SIZE,
305  }
306  else if (hdr->comp_type == MSKWAJ_COMP_LZH) {
307  struct kwajd_stream *lzh = lzh_init(sys, fh, outfh);
308  self->error = (lzh) ? lzh_decompress(lzh) : MSPACK_ERR_NOMEMORY;
309  lzh_free(lzh);
310  }
311  else if (hdr->comp_type == MSKWAJ_COMP_MSZIP) {
312  struct mszipd_stream *zip = mszipd_init(sys,fh,outfh,KWAJ_INPUT_SIZE,0);
313  self->error = (zip) ? mszipd_decompress_kwaj(zip) : MSPACK_ERR_NOMEMORY;
314  mszipd_free(zip);
315  }
316  else {
317  self->error = MSPACK_ERR_DATAFORMAT;
318  }
319 
320  /* close output file */
321  sys->close(outfh);
322 
323  return self->error;
324 }
lzma_index ** i
Definition: index.h:629
#define MSPACK_ERR_SEEK
Definition: mspack.h:495
#define MSKWAJ_COMP_SZDD
Definition: mspack.h:1982
#define MSKWAJ_COMP_XOR
Definition: mspack.h:1980
#define MSPACK_ERR_OPEN
Definition: mspack.h:489
#define MSPACK_SYS_OPEN_WRITE
Definition: mspack.h:460
#define MSKWAJ_COMP_NONE
Definition: mspack.h:1978
#define MSPACK_SYS_SEEK_START
Definition: mspack.h:467
#define MSKWAJ_COMP_MSZIP
Definition: mspack.h:1986
#define MSPACK_ERR_READ
Definition: mspack.h:491
#define MSKWAJ_COMP_LZH
Definition: mspack.h:1984
#define MSPACK_ERR_NOMEMORY
Definition: mspack.h:497
void mszipd_free(struct mszipd_stream *zip)
Definition: mszipd.c:508
struct mszipd_stream * mszipd_init(struct mspack_system *system, struct mspack_file *input, struct mspack_file *output, int input_buffer_size, int repair_mode)
Definition: mszipd.c:342
int mszipd_decompress_kwaj(struct mszipd_stream *zip)
Definition: mszipd.c:469
FILE * fh
Definition: cabinfo.c:52
const char * filename
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
#define KWAJ_INPUT_SIZE
Definition: kwaj.h:47
static void lzh_free(struct kwajd_stream *kwaj)
Definition: kwajd.c:492
static int lzh_decompress(struct kwajd_stream *kwaj)
Definition: kwajd.c:436
static struct kwajd_stream * lzh_init(struct mspack_system *sys, struct mspack_file *in, struct mspack_file *out)
Definition: kwajd.c:422
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
struct mspack_system * sys
Definition: kwaj.h:89
unsigned short comp_type
Definition: mspack.h:2015
off_t data_offset
Definition: mspack.h:2018
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
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 *(* alloc)(struct mspack_system *self, size_t bytes)
Definition: mspack.h:421
struct mspack_system * sys
Definition: mszip.h:46
Definition: zipint.h:278
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References mspack_system::alloc, mskwaj_decompressor_p::base, mspack_system::close, mskwajd_header::comp_type, mskwajd_header::data_offset, fh, mspack_system::free, i, KWAJ_INPUT_SIZE, lzh_decompress(), lzh_free(), lzh_init(), lzss_decompress(), LZSS_MODE_EXPAND, MSKWAJ_COMP_LZH, MSKWAJ_COMP_MSZIP, MSKWAJ_COMP_NONE, MSKWAJ_COMP_SZDD, MSKWAJ_COMP_XOR, MSPACK_ERR_ARGS, MSPACK_ERR_DATAFORMAT, MSPACK_ERR_NOMEMORY, MSPACK_ERR_OK, MSPACK_ERR_OPEN, MSPACK_ERR_READ, MSPACK_ERR_SEEK, MSPACK_ERR_WRITE, MSPACK_SYS_OPEN_WRITE, MSPACK_SYS_SEEK_START, mszipd_decompress_kwaj(), mszipd_free(), mszipd_init(), mspack_system::open, read(), mspack_system::read, mspack_system::seek, mszipd_stream::sys, kwajd_stream::sys, and mspack_system::write.

Referenced by kwajd_decompress(), and mspack_create_kwaj_decompressor().

◆ kwajd_open()

static struct mskwajd_header * kwajd_open ( struct mskwaj_decompressor base,
const char *  filename 
)
static

Definition at line 93 of file kwajd.c.

95 {
96  struct mskwaj_decompressor_p *self = (struct mskwaj_decompressor_p *) base;
97  struct mskwajd_header *hdr;
98  struct mspack_system *sys;
99  struct mspack_file *fh;
100 
101  if (!self) return NULL;
102  sys = self->system;
103 
104  fh = sys->open(sys, filename, MSPACK_SYS_OPEN_READ);
105  hdr = (struct mskwajd_header *) sys->alloc(sys, sizeof(struct mskwajd_header_p));
106  if (fh && hdr) {
107  ((struct mskwajd_header_p *) hdr)->fh = fh;
108  self->error = kwajd_read_headers(sys, fh, hdr);
109  }
110  else {
111  if (!fh) self->error = MSPACK_ERR_OPEN;
112  if (!hdr) self->error = MSPACK_ERR_NOMEMORY;
113  }
114 
115  if (self->error) {
116  if (fh) sys->close(fh);
117  sys->free(hdr);
118  hdr = NULL;
119  }
120 
121  return hdr;
122 }
#define MSPACK_SYS_OPEN_READ
Definition: mspack.h:458
#define NULL
Definition: cris-opc.c:27
static int kwajd_read_headers(struct mspack_system *sys, struct mspack_file *fh, struct mskwajd_header *hdr)
Definition: kwajd.c:151

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

Referenced by kwajd_decompress(), and mspack_create_kwaj_decompressor().

◆ kwajd_read_headers()

static int kwajd_read_headers ( struct mspack_system sys,
struct mspack_file fh,
struct mskwajd_header hdr 
)
static

Definition at line 151 of file kwajd.c.

154 {
155  unsigned char buf[16];
156  int i;
157 
158  /* read in the header */
159  if (sys->read(fh, &buf[0], kwajh_SIZEOF) != kwajh_SIZEOF) {
160  return MSPACK_ERR_READ;
161  }
162 
163  /* check for "KWAJ" signature */
164  if (((unsigned int) EndGetI32(&buf[kwajh_Signature1]) != 0x4A41574B) ||
165  ((unsigned int) EndGetI32(&buf[kwajh_Signature2]) != 0xD127F088))
166  {
167  return MSPACK_ERR_SIGNATURE;
168  }
169 
170  /* basic header fields */
173  hdr->headers = EndGetI16(&buf[kwajh_Flags]);
174  hdr->length = 0;
175  hdr->filename = NULL;
176  hdr->extra = NULL;
177  hdr->extra_length = 0;
178 
179  /* optional headers */
180 
181  /* 4 bytes: length of unpacked file */
182  if (hdr->headers & MSKWAJ_HDR_HASLENGTH) {
183  if (sys->read(fh, &buf[0], 4) != 4) return MSPACK_ERR_READ;
184  hdr->length = EndGetI32(&buf[0]);
185  }
186 
187  /* 2 bytes: unknown purpose */
188  if (hdr->headers & MSKWAJ_HDR_HASUNKNOWN1) {
189  if (sys->read(fh, &buf[0], 2) != 2) return MSPACK_ERR_READ;
190  }
191 
192  /* 2 bytes: length of section, then [length] bytes: unknown purpose */
193  if (hdr->headers & MSKWAJ_HDR_HASUNKNOWN2) {
194  if (sys->read(fh, &buf[0], 2) != 2) return MSPACK_ERR_READ;
195  i = EndGetI16(&buf[0]);
196  if (sys->seek(fh, (off_t)i, MSPACK_SYS_SEEK_CUR)) return MSPACK_ERR_SEEK;
197  }
198 
199  /* filename and extension */
201  int len;
202  /* allocate memory for maximum length filename */
203  char *fn = (char *) sys->alloc(sys, (size_t) 13);
204  if (!(hdr->filename = fn)) return MSPACK_ERR_NOMEMORY;
205 
206  /* copy filename if present */
207  if (hdr->headers & MSKWAJ_HDR_HASFILENAME) {
208  /* read and copy up to 9 bytes of a null terminated string */
209  if ((len = sys->read(fh, &buf[0], 9)) < 2) return MSPACK_ERR_READ;
210  for (i = 0; i < len; i++) if (!(*fn++ = buf[i])) break;
211  /* if string was 9 bytes with no null terminator, reject it */
212  if (i == 9 && buf[8] != '\0') return MSPACK_ERR_DATAFORMAT;
213  /* seek to byte after string ended in file */
214  if (sys->seek(fh, (off_t)(i + 1 - len), MSPACK_SYS_SEEK_CUR))
215  return MSPACK_ERR_SEEK;
216  fn--; /* remove the null terminator */
217  }
218 
219  /* copy extension if present */
220  if (hdr->headers & MSKWAJ_HDR_HASFILEEXT) {
221  *fn++ = '.';
222  /* read and copy up to 4 bytes of a null terminated string */
223  if ((len = sys->read(fh, &buf[0], 4)) < 2) return MSPACK_ERR_READ;
224  for (i = 0; i < len; i++) if (!(*fn++ = buf[i])) break;
225  /* if string was 4 bytes with no null terminator, reject it */
226  if (i == 4 && buf[3] != '\0') return MSPACK_ERR_DATAFORMAT;
227  /* seek to byte after string ended in file */
228  if (sys->seek(fh, (off_t)(i + 1 - len), MSPACK_SYS_SEEK_CUR))
229  return MSPACK_ERR_SEEK;
230  fn--; /* remove the null terminator */
231  }
232  *fn = '\0';
233  }
234 
235  /* 2 bytes: extra text length then [length] bytes of extra text data */
236  if (hdr->headers & MSKWAJ_HDR_HASEXTRATEXT) {
237  if (sys->read(fh, &buf[0], 2) != 2) return MSPACK_ERR_READ;
238  i = EndGetI16(&buf[0]);
239  hdr->extra = (char *) sys->alloc(sys, (size_t)i+1);
240  if (! hdr->extra) return MSPACK_ERR_NOMEMORY;
241  if (sys->read(fh, hdr->extra, i) != i) return MSPACK_ERR_READ;
242  hdr->extra[i] = '\0';
243  hdr->extra_length = i;
244  }
245  return MSPACK_ERR_OK;
246 }
size_t len
Definition: 6502dis.c:15
#define MSKWAJ_HDR_HASUNKNOWN1
Definition: mspack.h:1992
#define MSKWAJ_HDR_HASLENGTH
Definition: mspack.h:1989
#define MSKWAJ_HDR_HASFILENAME
Definition: mspack.h:1998
#define MSPACK_ERR_SIGNATURE
Definition: mspack.h:499
#define MSKWAJ_HDR_HASFILEEXT
Definition: mspack.h:2001
#define MSPACK_SYS_SEEK_CUR
Definition: mspack.h:469
#define MSKWAJ_HDR_HASEXTRATEXT
Definition: mspack.h:2004
#define MSKWAJ_HDR_HASUNKNOWN2
Definition: mspack.h:1995
#define kwajh_Flags
Definition: kwaj.h:20
#define kwajh_DataOffset
Definition: kwaj.h:19
#define kwajh_Signature2
Definition: kwaj.h:17
#define kwajh_CompMethod
Definition: kwaj.h:18
#define kwajh_Signature1
Definition: kwaj.h:16
#define kwajh_SIZEOF
Definition: kwaj.h:21
#define EndGetI16(a)
Definition: macros.h:38
#define EndGetI32(a)
Definition: macros.h:37
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
int off_t
Definition: sftypes.h:41
unsigned short extra_length
Definition: mspack.h:2035
char * filename
Definition: mspack.h:2027
char * extra
Definition: mspack.h:2032
off_t length
Definition: mspack.h:2024

References mspack_system::alloc, mskwajd_header::comp_type, mskwajd_header::data_offset, EndGetI16, EndGetI32, mskwajd_header::extra, mskwajd_header::extra_length, fh, mskwajd_header::filename, fn, mskwajd_header::headers, i, kwajh_CompMethod, kwajh_DataOffset, kwajh_Flags, kwajh_Signature1, kwajh_Signature2, kwajh_SIZEOF, len, mskwajd_header::length, MSKWAJ_HDR_HASEXTRATEXT, MSKWAJ_HDR_HASFILEEXT, MSKWAJ_HDR_HASFILENAME, MSKWAJ_HDR_HASLENGTH, MSKWAJ_HDR_HASUNKNOWN1, MSKWAJ_HDR_HASUNKNOWN2, MSPACK_ERR_DATAFORMAT, MSPACK_ERR_NOMEMORY, MSPACK_ERR_OK, MSPACK_ERR_READ, MSPACK_ERR_SEEK, MSPACK_ERR_SIGNATURE, MSPACK_SYS_SEEK_CUR, NULL, mspack_system::read, and mspack_system::seek.

Referenced by kwajd_open().

◆ lzh_decompress()

static int lzh_decompress ( struct kwajd_stream kwaj)
static

Definition at line 436 of file kwajd.c.

437 {
438  register unsigned int bit_buffer;
439  register int bits_left, i;
440  register unsigned short sym;
441  unsigned char *i_ptr, *i_end, lit_run = 0;
442  int j, pos = 0, len, offset, err;
443  unsigned int types[6];
444 
445  /* reset global state */
446  INIT_BITS;
447  RESTORE_BITS;
448  memset(&lzh->window[0], LZSS_WINDOW_FILL, (size_t) LZSS_WINDOW_SIZE);
449 
450  /* read 6 encoding types (for byte alignment) but only 5 are needed */
451  for (i = 0; i < 6; i++) READ_BITS_SAFE(types[i], 4);
452 
453  /* read huffman table symbol lengths and build huffman trees */
454  BUILD_TREE(MATCHLEN1, types[0]);
455  BUILD_TREE(MATCHLEN2, types[1]);
456  BUILD_TREE(LITLEN, types[2]);
457  BUILD_TREE(OFFSET, types[3]);
458  BUILD_TREE(LITERAL, types[4]);
459 
460  while (!lzh->input_end) {
461  if (lit_run) READ_HUFFSYM_SAFE(MATCHLEN2, len);
462  else READ_HUFFSYM_SAFE(MATCHLEN1, len);
463 
464  if (len > 0) {
465  len += 2;
466  lit_run = 0; /* not the end of a literal run */
467  READ_HUFFSYM_SAFE(OFFSET, j); offset = j << 6;
468  READ_BITS_SAFE(j, 6); offset |= j;
469 
470  /* copy match as output and into the ring buffer */
471  while (len-- > 0) {
472  lzh->window[pos] = lzh->window[(pos+4096-offset) & 4095];
473  WRITE_BYTE;
474  pos++; pos &= 4095;
475  }
476  }
477  else {
478  READ_HUFFSYM_SAFE(LITLEN, len); len++;
479  lit_run = (len == 32) ? 0 : 1; /* end of a literal run? */
480  while (len-- > 0) {
481  READ_HUFFSYM_SAFE(LITERAL, j);
482  /* copy as output and into the ring buffer */
483  lzh->window[pos] = j;
484  WRITE_BYTE;
485  pos++; pos &= 4095;
486  }
487  }
488  }
489  return MSPACK_ERR_OK;
490 }
#define INIT_BITS
Definition: readbits.h:103
#define RESTORE_BITS
Definition: readbits.h:118
voidpf uLong offset
Definition: ioapi.h:144
#define READ_BITS_SAFE(val, n)
Definition: kwajd.c:396
#define READ_HUFFSYM_SAFE(tbl, val)
Definition: kwajd.c:402
#define WRITE_BYTE
Definition: kwajd.c:417
#define BUILD_TREE(tbl, type)
Definition: kwajd.c:408
return memset(p, 0, total)
@ OFFSET
#define LZSS_WINDOW_FILL
Definition: lzss.h:20
#define LZSS_WINDOW_SIZE
Definition: lzss.h:19
insn_type_descr_t types[]
Definition: or1k_disas.c:7

References kwajd_stream::bit_buffer, kwajd_stream::bits_left, BUILD_TREE, err, i, kwajd_stream::i_end, kwajd_stream::i_ptr, INIT_BITS, kwajd_stream::input_end, len, LZSS_WINDOW_FILL, LZSS_WINDOW_SIZE, memset(), MSPACK_ERR_OK, OFFSET, pos, READ_BITS_SAFE, READ_HUFFSYM_SAFE, RESTORE_BITS, types, kwajd_stream::window, and WRITE_BYTE.

Referenced by kwajd_extract().

◆ lzh_free()

static void lzh_free ( struct kwajd_stream kwaj)
static

Definition at line 492 of file kwajd.c.

493 {
494  struct mspack_system *sys;
495  if (!lzh || !lzh->sys) return;
496  sys = lzh->sys;
497  sys->free(lzh);
498 }

References mspack_system::free, and kwajd_stream::sys.

Referenced by kwajd_extract().

◆ lzh_init()

static struct kwajd_stream * lzh_init ( struct mspack_system sys,
struct mspack_file in,
struct mspack_file out 
)
static

Definition at line 422 of file kwajd.c.

424 {
425  struct kwajd_stream *lzh;
426 
427  if (!sys || !in || !out) return NULL;
428  if (!(lzh = (struct kwajd_stream *) sys->alloc(sys, sizeof(struct kwajd_stream)))) return NULL;
429 
430  lzh->sys = sys;
431  lzh->input = in;
432  lzh->output = out;
433  return lzh;
434 }
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
struct mspack_file * output
Definition: kwaj.h:91
struct mspack_file * input
Definition: kwaj.h:90

References mspack_system::alloc, in, kwajd_stream::input, NULL, out, kwajd_stream::output, and kwajd_stream::sys.

Referenced by kwajd_extract().

◆ lzh_read_input()

static int lzh_read_input ( struct kwajd_stream kwaj)
static

Definition at line 545 of file kwajd.c.

545  {
546  int read;
547  if (lzh->input_end) {
548  lzh->input_end += 8;
549  lzh->inbuf[0] = 0;
550  read = 1;
551  }
552  else {
553  read = lzh->sys->read(lzh->input, &lzh->inbuf[0], KWAJ_INPUT_SIZE);
554  if (read < 0) return MSPACK_ERR_READ;
555  if (read == 0) {
556  lzh->input_end = 8;
557  lzh->inbuf[0] = 0;
558  read = 1;
559  }
560  }
561 
562  /* update i_ptr and i_end */
563  lzh->i_ptr = &lzh->inbuf[0];
564  lzh->i_end = &lzh->inbuf[read];
565  return MSPACK_ERR_OK;
566 }

References kwajd_stream::i_end, kwajd_stream::i_ptr, kwajd_stream::inbuf, kwajd_stream::input, kwajd_stream::input_end, KWAJ_INPUT_SIZE, MSPACK_ERR_OK, MSPACK_ERR_READ, read(), mspack_system::read, and kwajd_stream::sys.

◆ lzh_read_lens()

static int lzh_read_lens ( struct kwajd_stream kwaj,
unsigned int  type,
unsigned int  numsyms,
unsigned char *  lens 
)
static

Definition at line 500 of file kwajd.c.

503 {
504  register unsigned int bit_buffer;
505  register int bits_left;
506  unsigned char *i_ptr, *i_end;
507  unsigned int i, c, sel;
508  int err;
509 
510  RESTORE_BITS;
511  switch (type) {
512  case 0:
513  i = numsyms; c = (i==16)?4: (i==32)?5: (i==64)?6: (i==256)?8 :0;
514  for (i = 0; i < numsyms; i++) lens[i] = c;
515  break;
516 
517  case 1:
518  READ_BITS_SAFE(c, 4); lens[0] = c;
519  for (i = 1; i < numsyms; i++) {
520  READ_BITS_SAFE(sel, 1); if (sel == 0) lens[i] = c;
521  else { READ_BITS_SAFE(sel, 1); if (sel == 0) lens[i] = ++c;
522  else { READ_BITS_SAFE(c, 4); lens[i] = c; }}
523  }
524  break;
525 
526  case 2:
527  READ_BITS_SAFE(c, 4); lens[0] = c;
528  for (i = 1; i < numsyms; i++) {
529  READ_BITS_SAFE(sel, 2);
530  if (sel == 3) READ_BITS_SAFE(c, 4); else c += (char) sel-1;
531  lens[i] = c;
532  }
533  break;
534 
535  case 3:
536  for (i = 0; i < numsyms; i++) {
537  READ_BITS_SAFE(c, 4); lens[i] = c;
538  }
539  break;
540  }
541  STORE_BITS;
542  return MSPACK_ERR_OK;
543 }
static RzILOpEffect * sel(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:1869
#define c(i)
Definition: sha256.c:43

References c, err, i, MSPACK_ERR_OK, READ_BITS_SAFE, RESTORE_BITS, sel(), STORE_BITS, and type.

◆ mspack_create_kwaj_decompressor()

struct mskwaj_decompressor* mspack_create_kwaj_decompressor ( struct mspack_system sys)

Creates a new KWAJ decompressor.

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

Definition at line 55 of file kwajd.c.

56 {
57  struct mskwaj_decompressor_p *self = NULL;
58 
59  if (!sys) sys = mspack_default_system;
60  if (!mspack_valid_system(sys)) return NULL;
61 
62  if ((self = (struct mskwaj_decompressor_p *) sys->alloc(sys, sizeof(struct mskwaj_decompressor_p)))) {
63  self->base.open = &kwajd_open;
64  self->base.close = &kwajd_close;
65  self->base.extract = &kwajd_extract;
66  self->base.decompress = &kwajd_decompress;
67  self->base.last_error = &kwajd_error;
68  self->system = sys;
69  self->error = MSPACK_ERR_OK;
70  }
71  return (struct mskwaj_decompressor *) self;
72 }
struct mspack_system * mspack_default_system
Definition: system.c:238
int mspack_valid_system(struct mspack_system *sys)
Definition: system.c:58
static int kwajd_decompress(struct mskwaj_decompressor *base, const char *input, const char *output)
Definition: kwajd.c:331
static int kwajd_error(struct mskwaj_decompressor *base)
Definition: kwajd.c:351

References mspack_system::alloc, kwajd_close(), kwajd_decompress(), kwajd_error(), kwajd_extract(), kwajd_open(), mspack_default_system, MSPACK_ERR_OK, mspack_valid_system(), and NULL.

Referenced by main().

◆ mspack_destroy_kwaj_decompressor()

void mspack_destroy_kwaj_decompressor ( struct mskwaj_decompressor self)

Destroys an existing KWAJ decompressor.

Parameters
selfthe mskwaj_decompressor to destroy

Definition at line 79 of file kwajd.c.

80 {
81  struct mskwaj_decompressor_p *self = (struct mskwaj_decompressor_p *) base;
82  if (self) {
83  struct mspack_system *sys = self->system;
84  sys->free(self);
85  }
86 }

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

Referenced by main().