summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/mspack/szddd.c
blob: af77f15565fed43d611daae3f74e0384e1a6d938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* This file is part of libmspack.
 * (C) 2003-2010 Stuart Caie.
 *
 * SZDD is a format used in the MS-DOS commands COMPRESS.EXE and
 * EXPAND.EXE. The compression method is attributed to Steven Zeck,
 * however it's pretty much identical to LZSS.
 *
 * libmspack is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License (LGPL) version 2.1
 *
 * For further details, see the file COPYING.LIB distributed with libmspack
 */

/* SZDD decompression implementation */

#include "system-mspack.h"
#include "szdd.h"

/* prototypes */
static struct msszddd_header *szddd_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);

/***************************************
 * MSPACK_CREATE_SZDD_DECOMPRESSOR
 ***************************************
 * constructor
 */
struct msszdd_decompressor *
    mspack_create_szdd_decompressor(struct mspack_system *sys)
{
  struct msszdd_decompressor_p *self = NULL;

  if (!sys) sys = mspack_default_system;
  if (!mspack_valid_system(sys)) return NULL;

  if ((self = (struct msszdd_decompressor_p *) sys->alloc(sys, sizeof(struct msszdd_decompressor_p)))) {
    self->base.open       = &szddd_open;
    self->base.close      = &szddd_close;
    self->base.extract    = &szddd_extract;
    self->base.decompress = &szddd_decompress;
    self->base.last_error = &szddd_error;
    self->system          = sys;
    self->error           = MSPACK_ERR_OK;
  }
  return (struct msszdd_decompressor *) self;
}

/***************************************
 * MSPACK_DESTROY_SZDD_DECOMPRESSOR
 ***************************************
 * destructor
 */
void mspack_destroy_szdd_decompressor(struct msszdd_decompressor *base)
{
    struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
    if (self) {
	struct mspack_system *sys = self->system;
	sys->free(self);
    }
}

/***************************************
 * SZDDD_OPEN
 ***************************************
 * opens an SZDD file without decompressing, reads header
 */
static struct msszddd_header *szddd_open(struct msszdd_decompressor *base,
					 const char *filename)
{
    struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
    struct msszddd_header *hdr;
    struct mspack_system *sys;
    struct mspack_file *fh;

    if (!self) return NULL;
    sys = self->system;

    fh  = sys->open(sys, filename, MSPACK_SYS_OPEN_READ);
    hdr = (struct msszddd_header *) sys->alloc(sys, sizeof(struct msszddd_header_p));
    if (fh && hdr) {
	((struct msszddd_header_p *) hdr)->fh = fh;
	self->error = szddd_read_headers(sys, fh, hdr);
    }
    else {
	if (!fh)  self->error = MSPACK_ERR_OPEN;
	if (!hdr) self->error = MSPACK_ERR_NOMEMORY;
    }
    
    if (self->error) {
	if (fh)  sys->close(fh);
	if (hdr) sys->free(hdr);
	hdr = NULL;
    }

    return hdr;
}

/***************************************
 * SZDDD_CLOSE
 ***************************************
 * closes an SZDD file
 */
static void szddd_close(struct msszdd_decompressor *base,
			struct msszddd_header *hdr)
{
    struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
    struct msszddd_header_p *hdr_p = (struct msszddd_header_p *) hdr;

    if (!self || !self->system) return;

    /* close the file handle associated */
    self->system->close(hdr_p->fh);

    /* free the memory associated */
    self->system->free(hdr);

    self->error = MSPACK_ERR_OK;
}

/***************************************
 * SZDDD_READ_HEADERS
 ***************************************
 * reads the headers of an SZDD format file
 */
static unsigned char szdd_signature_expand[8] = {
    0x53, 0x5A, 0x44, 0x44, 0x88, 0xF0, 0x27, 0x33
};
static unsigned char szdd_signature_qbasic[8] = {
    0x53, 0x5A, 0x20, 0x88, 0xF0, 0x27, 0x33, 0xD1
};

static int szddd_read_headers(struct mspack_system *sys,
			      struct mspack_file *fh,
			      struct msszddd_header *hdr)
{
    unsigned char buf[8];

    /* read and check signature */
    if (sys->read(fh, buf, 8) != 8) return MSPACK_ERR_READ;

    if ((mspack_memcmp(buf, szdd_signature_expand, 8) == 0)) {
	/* common SZDD */
	hdr->format = MSSZDD_FMT_NORMAL;

	/* read the rest of the header */
	if (sys->read(fh, buf, 6) != 6) return MSPACK_ERR_READ;
	if (buf[0] != 0x41) return MSPACK_ERR_DATAFORMAT;
	hdr->missing_char = buf[1];
	hdr->length = EndGetI32(&buf[2]);
    }
    else if ((mspack_memcmp(buf, szdd_signature_qbasic, 8) == 0)) {
	/* special QBasic SZDD */
	hdr->format = MSSZDD_FMT_QBASIC;
	if (sys->read(fh, buf, 4) != 4) return MSPACK_ERR_READ;
	hdr->missing_char = '\0';
	hdr->length = EndGetI32(buf);
    }
    else {
	return MSPACK_ERR_SIGNATURE;
    }
    return MSPACK_ERR_OK;
}

/***************************************
 * SZDDD_EXTRACT
 ***************************************
 * decompresses an SZDD file
 */
static int szddd_extract(struct msszdd_decompressor *base,
			 struct msszddd_header *hdr, const char *filename)
{
    struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
    struct mspack_file *fh, *outfh;
    struct mspack_system *sys;
    off_t data_offset;

    if (!self) return MSPACK_ERR_ARGS;
    if (!hdr)  return self->error = MSPACK_ERR_ARGS;
    sys = self->system;

    fh = ((struct msszddd_header_p *) hdr)->fh;

    /* seek to the compressed data */
    data_offset = (hdr->format == MSSZDD_FMT_NORMAL) ? 14 : 12;
    if (sys->seek(fh, data_offset, MSPACK_SYS_SEEK_START)) {
	return self->error = MSPACK_ERR_SEEK;
    }

    /* open file for output */
    if (!(outfh = sys->open(sys, filename, MSPACK_SYS_OPEN_WRITE))) {
	return self->error = MSPACK_ERR_OPEN;
    }

    /* decompress the data */
    self->error = lzss_decompress(sys, fh, outfh, SZDD_INPUT_SIZE,
				  hdr->format == MSSZDD_FMT_NORMAL
				  ? LZSS_MODE_EXPAND
				  : LZSS_MODE_QBASIC);

    /* close output file */
    sys->close(outfh);

    return self->error;
}

/***************************************
 * SZDDD_DECOMPRESS
 ***************************************
 * unpacks directly from input to output
 */
static int szddd_decompress(struct msszdd_decompressor *base,
			    const char *input, const char *output)
{
    struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
    struct msszddd_header *hdr;
    int error;

    if (!self) return MSPACK_ERR_ARGS;

    if (!(hdr = szddd_open(base, input))) return self->error;
    error = szddd_extract(base, hdr, output);
    szddd_close(base, hdr);
    return self->error = error;
}

/***************************************
 * SZDDD_ERROR
 ***************************************
 * returns the last error that occurred
 */
static int szddd_error(struct msszdd_decompressor *base)
{
    struct msszdd_decompressor_p *self = (struct msszdd_decompressor_p *) base;
    return (self) ? self->error : MSPACK_ERR_ARGS;
}