File: lzs_decoder.c

package info (click to toggle)
lhasa 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,364 kB
  • sloc: ansic: 6,796; sh: 1,938; makefile: 168; python: 129
file content (155 lines) | stat: -rw-r--r-- 3,920 bytes parent folder | download
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
/*

Copyright (c) 2011, 2012, Simon Howard

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

 */

#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "lha_decoder.h"

#include "bit_stream_reader.c"

// Parameters for ring buffer, used for storing history.  This acts
// as the dictionary for copy operations.

#define RING_BUFFER_SIZE 2048
#define START_OFFSET 17

// Threshold offset.  In the copy operation, the copy length is a 4-bit
// value, giving a range 0..15.  The threshold offsets this so that it
// is interpreted as 2..17 - a more useful range.

#define THRESHOLD 2

// Size of output buffer.  Must be large enough to hold the results of
// the maximum copy operation.

#define OUTPUT_BUFFER_SIZE (15 + THRESHOLD)

// Decoder for the -lzs- compression method used by old versions of LArc.
//
// The input stream consists of commands, each of which is either "output
// a literal byte value" or "copy block". A bit at the start of each
// command signals which command it is.

typedef struct {
	BitStreamReader bit_stream_reader;
	uint8_t ringbuf[RING_BUFFER_SIZE];
	unsigned int ringbuf_pos;
} LHALZSDecoder;

static int lha_lzs_init(void *data, LHADecoderCallback callback,
                        void *callback_data)
{
	LHALZSDecoder *decoder = data;

	memset(decoder->ringbuf, ' ', RING_BUFFER_SIZE);
	decoder->ringbuf_pos = RING_BUFFER_SIZE - START_OFFSET;
	bit_stream_reader_init(&decoder->bit_stream_reader, callback,
	                       callback_data);

	return 1;
}

// Add a single byte to the output buffer.

static void output_byte(LHALZSDecoder *decoder, uint8_t *buf,
                        size_t *buf_len, uint8_t b)
{
	buf[*buf_len] = b;
	++*buf_len;

	decoder->ringbuf[decoder->ringbuf_pos] = b;
	decoder->ringbuf_pos = (decoder->ringbuf_pos + 1) % RING_BUFFER_SIZE;
}

// Output a "block" of data from the specified range in the ring buffer.

static void output_block(LHALZSDecoder *decoder,
                         uint8_t *buf,
                         size_t *buf_len,
                         unsigned int start,
                         unsigned int len)
{
	unsigned int i;

	for (i = 0; i < len; ++i) {
		output_byte(decoder, buf, buf_len,
		            decoder->ringbuf[(start + i) % RING_BUFFER_SIZE]);
	}
}

// Process a single command from the LZS input stream.

static size_t lha_lzs_read(void *data, uint8_t *buf)
{
	LHALZSDecoder *decoder = data;
	int bit;
	size_t result;

	// Start from an empty buffer.

	result = 0;

	// Each command starts with a bit that signals the type:

	bit = read_bit(&decoder->bit_stream_reader);

	if (bit < 0) {
		return 0;
	}

	// What type of command is this?

	if (bit) {
		int b;

		b = read_bits(&decoder->bit_stream_reader, 8);

		if (b < 0) {
			return 0;
		}

		output_byte(decoder, buf, &result, (uint8_t) b);
	} else {
		int pos, len;

		pos = read_bits(&decoder->bit_stream_reader, 11);
		len = read_bits(&decoder->bit_stream_reader, 4);

		if (pos < 0 || len < 0) {
			return 0;
		}

		output_block(decoder, buf, &result, (unsigned int) pos,
		             (unsigned int) len + THRESHOLD);
	}

	return result;
}

const LHADecoderType lha_lzs_decoder = {
	lha_lzs_init,
	NULL,
	lha_lzs_read,
	sizeof(LHALZSDecoder),
	OUTPUT_BUFFER_SIZE,
	RING_BUFFER_SIZE
};