Merge zizzer:/bk/newmem
[gem5.git] / src / base / compression / lzss_compression.hh
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Erik Hallnor
29 */
30
31 #ifndef __LZSS_COMPRESSION_HH__
32 #define __LZSS_COMPRESSION_HH__
33
34 /** @file
35 * LZSSCompression declarations.
36 */
37
38 #include "base/compression/base.hh"
39
40 /**
41 * Simple LZSS compression scheme.
42 */
43 class LZSSCompression : public CompressionAlgorithm
44 {
45 /**
46 * Finds the longest substring for the given offset.
47 * @param src The source block that we search for substrings.
48 * @param back The larger offset.
49 * @param size The size of the source block.
50 * @param L The length of the largest substring.
51 * @param P The starting offset of the largest substring.
52 */
53 void findSubString(uint8_t *src, int back, int size, uint16_t &L,
54 uint16_t &P);
55
56 /**
57 * Emit an encoded byte to the compressed data array. If the 2 high
58 * order bits can be signed extended, use 1 byte encoding, if not use 2
59 * bytes.
60 * @param dest The compressed data.
61 * @param byte The byte to emit.
62 * @return The number of bytes used to encode.
63 */
64 int emitByte(uint8_t *dest, uint8_t byte);
65
66 /**
67 * Emit a string reference to the compressed data array. A string reference
68 * always uses 3 bytes. 1 flag bit, 12 bits for the starting position, and
69 * 11 bits for the length of the string. This allows compression of 4096
70 * byte blocks with string lengths of up to 2048 bytes.
71 * @param dest The compressed data.
72 * @param P The starting position in the uncompressed data.
73 * @param L The length in bytes of the string.
74 */
75 void emitString(uint8_t *dest, uint16_t P, uint16_t L);
76
77 public:
78 /**
79 * Compresses the source block and stores it in the destination block. If
80 * the compressed block grows to larger than the source block, it aborts
81 * and just performs a copy.
82 * @param dest The destination block.
83 * @param src The block to be compressed.
84 * @param size The size of the source block.
85 * @return The size of the compressed block.
86 *
87 * @pre Destination has enough storage to hold the compressed block.
88 */
89 int compress(uint8_t *dest, uint8_t *src, int size);
90
91 /**
92 * Unompresses the source block and stores it in the destination block.
93 * @param dest The destination block.
94 * @param src The block to be uncompressed.
95 * @param size The size of the source block.
96 * @return The size of the uncompressed block.
97 *
98 * @pre Destination has enough storage to hold the uncompressed block.
99 */
100 int uncompress(uint8_t *dest, uint8_t *src, int size);
101 };
102
103 #endif //__LZSS_COMPRESSION_HH__