Merge changes.
[gem5.git] / base / compression / lzss_compression.cc
1 /*
2 * Copyright (c) 2003-2004 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
29 /** @file
30 * LZSSCompression definitions.
31 */
32
33 #include <assert.h>
34
35 #include "base/compression/lzss_compression.hh"
36
37 #include "base/misc.hh" //for fatal
38
39 void
40 LZSSCompression::findSubString(uint8_t *src, int back, int size, uint16_t &L,
41 uint16_t &P)
42 {
43 int front = 0;
44 int max_length = size - back;
45 L = 0;
46 P = back - 1;
47 while (front < back) {
48 while (src[front] != src[back] && front < back) ++front;
49 if (front >= back) {
50 return;
51 }
52 int i = 1;
53 while (src[front+i] == src[back+i] && i < max_length) ++i;
54 if (i >= L) {
55 L = i;
56 P = front;
57 }
58 if (src[front+i] != src[back+i-1]) {
59 // can't find a longer substring until past this point.
60 front += i;
61 } else {
62 ++front;
63 }
64 }
65 }
66
67 int
68 LZSSCompression::emitByte(uint8_t *dest, uint8_t byte)
69 {
70 if ((byte >> 5 & 0x7) == 0 || (byte >> 5 & 0x7) == 7) {
71 // If the top 3 bits are the same, emit 00<6bits>
72 dest[0] = byte & 0x3f;
73 return 1;
74 } else {
75 // emit 01XXXXXX <8 bits>
76 dest[0] = 0x40;
77 dest[1] = byte;
78 return 2;
79 }
80 }
81
82 void
83 LZSSCompression::emitString(uint8_t *dest, uint16_t P, uint16_t L)
84 {
85 // Emit 1<7P> <5P><3L> <8L>
86 dest[0] = 1<<7 | (P >> 5 & 0x7f);
87 dest[1] = ((P & 0x1f) << 3) | (L>>8 & 0x3);
88 dest[2] = L & 0xFF;
89 }
90
91 int
92 LZSSCompression::compress(uint8_t *dest, uint8_t *src, int size)
93 {
94 if (size > 4096) {
95 fatal("Compression can only handle block sizes of 4096 bytes or less");
96 }
97
98 // Encode the first byte.
99 int dest_index = emitByte(dest, src[0]);
100 int i = 1;
101 // A 11 bit field
102 uint16_t L;
103 // A 12 bit field
104 uint16_t P = 0;
105
106 while (i < size && dest_index < size) {
107 L = 0;
108
109 if (dest_index+3 >= size) {
110 dest_index = size;
111 continue;
112 }
113
114 if (i == size - 1) {
115 // Output the character
116 dest_index += emitByte(&dest[dest_index], src[i]);
117 ++i;
118 continue;
119 }
120 findSubString(src, i, size, L, P);
121 if (L > 1) {
122 // Output the string reference
123 emitString(&dest[dest_index], P, L);
124 dest_index += 3;
125 i = i+L;
126 } else {
127 // Output the character
128 dest_index += emitByte(&dest[dest_index], src[i]);
129 ++i;
130 }
131 }
132
133 if (dest_index >= size) {
134 // Have expansion instead of compression, just copy.
135 memcpy(dest,src,size);
136 return size;
137 }
138 return dest_index;
139 }
140
141 int
142 LZSSCompression::uncompress(uint8_t *dest, uint8_t *src, int size)
143 {
144 int index = 0;
145 int i = 0;
146 while (i < size) {
147 if (src[i] & 1<<7 ) {
148 // We have a string
149 // Extract P
150 int start = (src[i] & 0x3f)<<5 | ((src[i+1] >> 3) & 0x1f);
151 // Extract L
152 int len = (src[i+1] & 0x07)<<8 | src[i+2];
153 i += 3;
154 for (int j = start; j < start+len; ++j) {
155 dest[index++] = dest[j];
156 }
157 } else {
158 // We have a character
159 if (src[i] & 1<<6) {
160 // Value is in the next byte
161 dest[index++] = src[i+1];
162 i += 2;
163 } else {
164 // just extend the lower 6 bits
165 dest[index++] = (src[i] & 0x3f) | ((src[i] & 1<<5) ? 0xC0 : 0);
166 ++i;
167 }
168 }
169 }
170 return index;
171 }