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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 ============================================================================
 Name        : decompress.c
 Author      : nanash1
 Version     : 0.9
 Description : Decompresses Grandia Saturn graphics files
 ============================================================================
 */

/**
* This software is supplied "AS IS" without any warranties of
* any kind. The author, nanash1, assumes no responsibility
* or liability for the use of the software, conveys no license or rights under any
* patent, copyright, mask work right, or any other intellectual property rights in
* or to any products. The author also makes no
* representation or warranty that such application will be suitable for the
* specified use without further testing or modification.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted. This copyright, permission, and disclaimer
* notice must appear in all copies of this code.
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <byteswap.h>

#include "compression.h"

const uint8_t lut_seed[9] = {0, 128, 64, 32, 16, 8, 4, 2, 1};
static uint16_t decomp_lut[256];

/**
 * @brief  	Gets the next byte of encoded data and advances the pointer
 * @param	p_offset_code		Next bit offset to advance the pointer
 * @param	pp_lut_data			Pointer to current input data pointer
 * @return	Encoded data
 */
static uint16_t extract_lut_data(uint16_t* p_offset_code, uint8_t** pp_lut_data)
{
	uint16_t shift_code = *p_offset_code >> 4;
	uint32_t fetched_source_data;

	if (shift_code > 31){
		*pp_lut_data += 4;
		shift_code -= 32;
		*p_offset_code = shift_code << 4;
	}

	uint8_t* p_source_data = *pp_lut_data;

	if (shift_code < 25){
		fetched_source_data = __bswap_32(*((uint32_t*) p_source_data)) >> (24 - shift_code);
	} else {
		fetched_source_data = __bswap_32(*((uint32_t*) p_source_data)) << (shift_code - 24);
		fetched_source_data |= __bswap_32(*((uint32_t*) (p_source_data + 4))) >> (56 - shift_code);	// or in bits from next pointer
	}

	return (uint16_t) ((fetched_source_data & 0xff) << 1);
}

/**
 * @brief	Generates the decompression lut
 * @param	p_comp_lut		Pointer to the memory the table is written at
 * @return	Status 0:Success 1:Error
 */
static uint8_t generate_lut(uint8_t* p_comp_lut)
{
	uint8_t rtn = 0;
	uint8_t bit_num;
	uint8_t max_bit_num;
	uint8_t decode_char;
	uint16_t lut_elem;
	uint16_t *p_decomp_lut = &decomp_lut[0];

	for(uint_fast8_t j = 0; j < 17; j++){
		max_bit_num = 0;
		for(uint_fast8_t i = 0; i < 17; i++){
			if (i%2){
				bit_num = *(p_comp_lut + (i/2)) & 0xf;
			} else {
				bit_num = *(p_comp_lut + (i/2)) >> 4;
			}

			if (bit_num > max_bit_num){
				max_bit_num = bit_num;
				decode_char = i;
			}
		}

		if (max_bit_num > 8){
			rtn = 1;
			break;
		} else if (max_bit_num == 0){
			break;
		}

		if (decode_char%2){
			*(p_comp_lut + (decode_char/2)) &= 0xf0;
		} else {
			*(p_comp_lut + (decode_char/2)) &= 0xf;
		}

		lut_elem = decode_char << 8;
		lut_elem |= max_bit_num << 4;
		if (decode_char == 16){
			lut_elem++;
		}
		for (uint_fast8_t i = 0; i < lut_seed[max_bit_num]; i++){

			*p_decomp_lut = lut_elem;
			p_decomp_lut++;
		}
	}

	/* check if the table is within bounds */
	if (p_decomp_lut != &decomp_lut[0] + 256){
		rtn = 1;
	}
	return rtn;

}

/**
 * @brief	Decompresses the compressed data from the Grandia iso
 * @param	input					Input file name as string
 * @param	output					Output file name as string
 * @param	comp_data_offset		Start offset of the compressed data in the input file
 * @param	p_mode					The used decompression mode is written to this pointer
 * @param	p_lut					The used look up table is written to this pointer
 * @param	p_rle_limit				The used RLE limit is written to this pointer
 * @return	Status
 */
compression_status_t decompress(
		const char* input,
		const char* output,
		uint32_t comp_data_offset,
		enum comp_mode* p_mode,
		uint8_t* p_lut,
		uint8_t* p_rle_limit)
{
	uint8_t* p_iso;


	/* read dumped file into memory */
	int size = 0;
	FILE *f = fopen(input, "rb");

	if (f == NULL){
		return FILE_NOT_FOUND;
	}

	fseek(f, 0, SEEK_END);
	size = ftell(f);
	fseek(f, 0, SEEK_SET);

	p_iso = (uint8_t *) malloc(size+1);

	if (size != fread(p_iso, sizeof(int8_t), size, f))
	{
		free(p_iso);
		return INSUFFICIENT_MEM;<--- Resource leak: f
	}
	fclose(f);

	/* read header */
	uint8_t* p_rle_data = p_iso + comp_data_offset;
	uint16_t head_data;
	head_data = __bswap_16(*((uint16_t*) p_rle_data));
	if ( (head_data >> 8) & 0x20){		// if the second bit is set
		free(p_iso);
		return INVALID_HEADER;
	}

	/* get mode */
	uint8_t mode_bits = head_data >> 14;
	enum comp_mode mode;
	switch (mode_bits){
	case 0b10:
		mode = SUB_MODE;
		break;
	case 0b00:
		mode = MOV_MODE;
		break;
	case 0b01:
		mode = XOR_MODE;
		break;
	default:
		free(p_iso);
		return INVALID_HEADER;
	}
	*p_mode = mode;

	/* write decompression table */
	uint16_t table_offset = head_data;
	table_offset = table_offset & 0x1fff;
	uint8_t* p_table_data = p_rle_data + table_offset + 2;
	memcpy(p_lut, p_table_data, 9);
	if (generate_lut(p_table_data)){
		free(p_iso);
		return INVALID_TABLE;
	}

	/* reserve memory for decompressed data */
	uint8_t* p_decompressed = (uint8_t*) calloc(64000, sizeof(uint8_t));
	uint8_t* p_output = p_decompressed;

	/* get pointer to lut encoded data */
	uint8_t* p_lut_data = p_rle_data + table_offset + 0xb;
	p_lut_data = (uint8_t*) (((uint32_t) p_lut_data) & 0xfffffffc);	// probably to align for 32bit memory access, TODO: check if this works here

	/* advance pointer to first RLE data */
	p_rle_data += 2;

	/* initialize decompression vars */
	uint8_t rle_zeros_cntr = 0;
	uint8_t rle_limit = 0;
	uint16_t zero_num = 0;
	uint16_t zero_diff;
	uint32_t decoded_bits_num = 0;

	uint16_t shift_code = ((table_offset + 0xb) & 0x3) << 7;
	uint16_t decompressed_bits = 0;
	uint8_t rle_prefix;
	uint32_t lut_index = 0;
	uint16_t lut_data;
	uint_fast8_t is_aligned = 0;

	uint_fast32_t run_cntr = 0;
	while(1){

		run_cntr++;

		if (run_cntr > MAX_DECOMP_LEN){
			free(p_decompressed);
			free(p_iso);
			return RUNAWAY_DATA;
		}

		/* get next rle bits */
		if (is_aligned){
			rle_prefix = *p_rle_data & 0xf;
			p_rle_data++;
			is_aligned = 0;
		} else {
			rle_prefix = *p_rle_data >> 4;
			is_aligned = 1;
		}

		/* check the prefix code */
		switch (rle_prefix){
			case 0xD:	// read next 4 bits as extended delta information
				if (is_aligned){
					zero_diff = *p_rle_data & 0xf;
					is_aligned = 0;
					p_rle_data++;
				} else {
					zero_diff = *p_rle_data >> 4;
					is_aligned = 1;
				}
				if (zero_diff >= 8){
					zero_num += (zero_diff - 1);
				} else {
					zero_num += zero_diff - 14;
				}
				break;

			case 0xE:	// read next 8 bits as number of zeros
				if (is_aligned){
					zero_num = (*p_rle_data & 0xf) << 4;
					p_rle_data++;
					zero_num |= *p_rle_data  >> 4;
				} else {
					zero_num = *p_rle_data;
					p_rle_data++;
				}
				break;

			case 0xF:	// read next 16 bits as number of zeros
				if (is_aligned){
					zero_num = (*p_rle_data & 0xf) << 12;
					p_rle_data++;
					zero_num |= *p_rle_data << 4;
					p_rle_data++;
					zero_num |= (*p_rle_data & 0xf0) >> 4;
				} else {
					zero_num = *p_rle_data << 8;
					p_rle_data++;
					zero_num |= *p_rle_data;
					p_rle_data++;
				}
				break;

			default:	// read prefix as number of zeros offset
				zero_num += rle_prefix - 6;
				break;
		}


		/* write the determined number of zeros */
		if (zero_num){

			if (decoded_bits_num % 2){
				p_output += (zero_num - 1) / 2;
			} else {
				p_output += zero_num / 2;
			}
			decoded_bits_num += zero_num;

		} else {
			break;
		}

		while (1){

			/* use the generated table to read actual data from memory */
			lut_index = extract_lut_data(&shift_code, &p_lut_data);
			lut_data = decomp_lut[lut_index/2];

			/* look for stop condition and write data to output */
			if (!(lut_data & 0x1)){
				shift_code += lut_data & 0xff;
				lut_data >>= 8;
				switch (mode){
				case SUB_MODE:
					decompressed_bits = (decompressed_bits - lut_data) & 0xf;
					break;
				case MOV_MODE:
					decompressed_bits = lut_data & 0xf;
					break;
				case XOR_MODE:
					decompressed_bits = (decompressed_bits ^ lut_data) & 0xf;
					break;
				}

				if (decompressed_bits == 0){
					rle_zeros_cntr++;
				} else {
					if (rle_limit < rle_zeros_cntr){
						rle_limit = rle_zeros_cntr;
					}
					rle_zeros_cntr = 0;
				}

				if (decoded_bits_num % 2){
					*p_output |= decompressed_bits;
					p_output++;
					decoded_bits_num++;
				} else {
					*p_output = decompressed_bits << 4;
					decoded_bits_num++;
				}

			} else {
				if (decoded_bits_num % 2){
					p_output++;
				}
				shift_code += lut_data & 0xf0;
				break;
			}
		}
	}

	f = fopen(output, "wb");
	fwrite(p_decompressed, sizeof(uint8_t), (int32_t) (p_output - p_decompressed), f);
	fclose(f);

	free(p_decompressed);
	free(p_iso);

	*p_rle_limit = rle_limit + 1;

	return SUCCESS;
}