aboutsummaryrefslogtreecommitdiff
path: root/engines/cine/unpack.h
blob: f0a7ee38040b8486148f8f47e4bd2ac6e451d5d1 (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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#ifndef CINE_UNPACK_H
#define CINE_UNPACK_H


#include "common/scummsys.h"

namespace Cine {

/**
 * A LZ77 style decompressor for Delphine's data files
 * used in at least Future Wars and Operation Stealth.
 * @note Works backwards in the source and destination buffers.
 * @note Can work with source and destination in the same buffer if there's space.
 */
class CineUnpacker {
public:
	/** Returns true if unpacking was successful, otherwise false. */
	bool unpack(byte *dst, const byte *src, int srcLen);
private:
	/** Reads a single big endian 32-bit integer from the source and goes backwards 4 bytes. */
	uint32 readSource();

	/**
	 * Shifts the current internal 32-bit chunk to the right by one.
	 * Puts input carry into internal chunk's topmost (i.e. leftmost) bit.
	 * Returns the least significant bit that was shifted out.
	 */
	int rcr(int inputCarry);
	int nextBit();
	uint16 getBits(byte numBits);

	/**
	 * Copy raw bytes from the input stream and write them to the destination stream.
	 * This is used when no adequately long match is found in the sliding window.
	 * @param numBytes Amount of bytes to copy from the input stream
	 */
	void unpackRawBytes(uint16 numBytes);

	/**
	 * Copy bytes from the sliding window in the destination buffer.
	 * This is used when a match of two bytes or longer is found.
	 * @param offset Offset in the sliding window
	 * @param numBytes Amount of bytes to copy
	 */
	void copyRelocatedBytes(uint16 offset, uint16 numBytes);
private:
	int _datasize;    //!< Bytes left to write into the unpacked data stream
	uint32 _crc;      //!< Error-detecting code
	uint32 _chunk32b; //!< The current internal 32-bit chunk
	byte *_dst;       //!< Destination buffer pointer
	const byte *_src; //!< Source buffer pointer
};

} // End of namespace Cine

#endif