| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* |
| 5 |
* This code is based on the LZF algorithm from Marc Lehmann. It is a |
| 6 |
* re-implementation of the C code: |
| 7 |
* http://cvs.schmorp.de/liblzf/lzf_c.c?view=markup |
| 8 |
* http://cvs.schmorp.de/liblzf/lzf_d.c?view=markup |
| 9 |
* |
| 10 |
* According to a mail from Marc Lehmann, it's OK to use his algorithm: |
| 11 |
* Date: 2010-07-15 15:57 |
| 12 |
* Subject: Re: Question about LZF licensing |
| 13 |
* ... |
| 14 |
* The algorithm is not copyrighted (and cannot be copyrighted afaik) - as long |
| 15 |
* as you wrote everything yourself, without copying my code, that's just fine |
| 16 |
* (looking is of course fine too). |
| 17 |
* ... |
| 18 |
* |
| 19 |
* Still I would like to keep his copyright info: |
| 20 |
* |
| 21 |
* Copyright (c) 2000-2005 Marc Alexander Lehmann <[email protected]> |
| 22 |
* Copyright (c) 2005 Oren J. Maurice <[email protected]> |
| 23 |
* |
| 24 |
* Redistribution and use in source and binary forms, with or without |
| 25 |
* modification, are permitted provided that the following conditions are met: |
| 26 |
* |
| 27 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 28 |
* this list of conditions and the following disclaimer. |
| 29 |
* |
| 30 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 31 |
* notice, this list of conditions and the following disclaimer in the |
| 32 |
* documentation and/or other materials provided with the distribution. |
| 33 |
* |
| 34 |
* 3. The name of the author may not be used to endorse or promote products |
| 35 |
* derived from this software without specific prior written permission. |
| 36 |
* |
| 37 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED |
| 38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 39 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 40 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 42 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 43 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 44 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 45 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 46 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 47 |
*/ |
| 48 |
|
| 49 |
package org.h2.compress; |
| 50 |
|
| 51 |
import java.nio.ByteBuffer; |
| 52 |
|
| 53 |
/** |
| 54 |
* <p> |
| 55 |
* This class implements the LZF lossless data compression algorithm. LZF is a |
| 56 |
* Lempel-Ziv variant with byte-aligned output, and optimized for speed. |
| 57 |
* </p> |
| 58 |
* <p> |
| 59 |
* Safety/Use Notes: |
| 60 |
* </p> |
| 61 |
* <ul> |
| 62 |
* <li>Each instance should be used by a single thread only.</li> |
| 63 |
* <li>The data buffers should be smaller than 1 GB.</li> |
| 64 |
* <li>For performance reasons, safety checks on expansion are omitted.</li> |
| 65 |
* <li>Invalid compressed data can cause an ArrayIndexOutOfBoundsException.</li> |
| 66 |
* </ul> |
| 67 |
* <p> |
| 68 |
* The LZF compressed format knows literal runs and back-references: |
| 69 |
* </p> |
| 70 |
* <ul> |
| 71 |
* <li>Literal run: directly copy bytes from input to output.</li> |
| 72 |
* <li>Back-reference: copy previous data to output stream, with specified |
| 73 |
* offset from location and length. The length is at least 3 bytes.</li> |
| 74 |
* </ul> |
| 75 |
*<p> |
| 76 |
* The first byte of the compressed stream is the control byte. For literal |
| 77 |
* runs, the highest three bits of the control byte are not set, the lower |
| 78 |
* bits are the literal run length, and the next bytes are data to copy directly |
| 79 |
* into the output. For back-references, the highest three bits of the control |
| 80 |
* byte are the back-reference length. If all three bits are set, then the |
| 81 |
* back-reference length is stored in the next byte. The lower bits of the |
| 82 |
* control byte combined with the next byte form the offset for the |
| 83 |
* back-reference. |
| 84 |
* </p> |
| 85 |
*/ |
| 86 |
public final class CompressLZF implements Compressor { |
| 87 |
|
| 88 |
/** |
| 89 |
* The number of entries in the hash table. The size is a trade-off between |
| 90 |
* hash collisions (reduced compression) and speed (amount that fits in CPU |
| 91 |
* cache). |
| 92 |
*/ |
| 93 |
private static final int HASH_SIZE = 1 << 14; |
| 94 |
|
| 95 |
/** |
| 96 |
* The maximum number of literals in a chunk (32). |
| 97 |
*/ |
| 98 |
private static final int MAX_LITERAL = 1 << 5; |
| 99 |
|
| 100 |
/** |
| 101 |
* The maximum offset allowed for a back-reference (8192). |
| 102 |
*/ |
| 103 |
private static final int MAX_OFF = 1 << 13; |
| 104 |
|
| 105 |
/** |
| 106 |
* The maximum back-reference length (264). |
| 107 |
*/ |
| 108 |
private static final int MAX_REF = (1 << 8) + (1 << 3); |
| 109 |
|
| 110 |
/** |
| 111 |
* Hash table for matching byte sequences (reused for performance). |
| 112 |
*/ |
| 113 |
private int[] cachedHashTable; |
| 114 |
|
| 115 |
@Override |
| 116 |
public void setOptions(String options) { |
| 117 |
// nothing to do |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Return the integer with the first two bytes 0, then the bytes at the |
| 122 |
* index, then at index+1. |
| 123 |
*/ |
| 124 |
private static int first(byte[] in, int inPos) { |
| 125 |
return (in[inPos] << 8) | (in[inPos + 1] & 255); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Return the integer with the first two bytes 0, then the bytes at the |
| 130 |
* index, then at index+1. |
| 131 |
*/ |
| 132 |
private static int first(ByteBuffer in, int inPos) { |
| 133 |
return (in.get(inPos) << 8) | (in.get(inPos + 1) & 255); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Shift the value 1 byte left, and add the byte at index inPos+2. |
| 138 |
*/ |
| 139 |
private static int next(int v, byte[] in, int inPos) { |
| 140 |
return (v << 8) | (in[inPos + 2] & 255); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Shift the value 1 byte left, and add the byte at index inPos+2. |
| 145 |
*/ |
| 146 |
private static int next(int v, ByteBuffer in, int inPos) { |
| 147 |
return (v << 8) | (in.get(inPos + 2) & 255); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Compute the address in the hash table. |
| 152 |
*/ |
| 153 |
private static int hash(int h) { |
| 154 |
return ((h * 2777) >> 9) & (HASH_SIZE - 1); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public int compress(byte[] in, int inPos, int inLen, byte[] out, int outPos) { |
| 159 |
int offset = inPos; |
| 160 |
inLen += inPos; |
| 161 |
if (cachedHashTable == null) { |
| 162 |
cachedHashTable = new int[HASH_SIZE]; |
| 163 |
} |
| 164 |
int[] hashTab = cachedHashTable; |
| 165 |
int literals = 0; |
| 166 |
outPos++; |
| 167 |
int future = first(in, inPos); |
| 168 |
while (inPos < inLen - 4) { |
| 169 |
byte p2 = in[inPos + 2]; |
| 170 |
// next |
| 171 |
future = (future << 8) + (p2 & 255); |
| 172 |
int off = hash(future); |
| 173 |
int ref = hashTab[off]; |
| 174 |
hashTab[off] = inPos; |
| 175 |
// if (ref < inPos |
| 176 |
// && ref > 0 |
| 177 |
// && (off = inPos - ref - 1) < MAX_OFF |
| 178 |
// && in[ref + 2] == p2 |
| 179 |
// && (((in[ref] & 255) << 8) | (in[ref + 1] & 255)) == |
| 180 |
// ((future >> 8) & 0xffff)) { |
| 181 |
if (ref < inPos |
| 182 |
&& ref > offset |
| 183 |
&& (off = inPos - ref - 1) < MAX_OFF |
| 184 |
&& in[ref + 2] == p2 |
| 185 |
&& in[ref + 1] == (byte) (future >> 8) |
| 186 |
&& in[ref] == (byte) (future >> 16)) { |
| 187 |
// match |
| 188 |
int maxLen = inLen - inPos - 2; |
| 189 |
if (maxLen > MAX_REF) { |
| 190 |
maxLen = MAX_REF; |
| 191 |
} |
| 192 |
if (literals == 0) { |
| 193 |
// multiple back-references, |
| 194 |
// so there is no literal run control byte |
| 195 |
outPos--; |
| 196 |
} else { |
| 197 |
// set the control byte at the start of the literal run |
| 198 |
// to store the number of literals |
| 199 |
out[outPos - literals - 1] = (byte) (literals - 1); |
| 200 |
literals = 0; |
| 201 |
} |
| 202 |
int len = 3; |
| 203 |
while (len < maxLen && in[ref + len] == in[inPos + len]) { |
| 204 |
len++; |
| 205 |
} |
| 206 |
len -= 2; |
| 207 |
if (len < 7) { |
| 208 |
out[outPos++] = (byte) ((off >> 8) + (len << 5)); |
| 209 |
} else { |
| 210 |
out[outPos++] = (byte) ((off >> 8) + (7 << 5)); |
| 211 |
out[outPos++] = (byte) (len - 7); |
| 212 |
} |
| 213 |
out[outPos++] = (byte) off; |
| 214 |
// move one byte forward to allow for a literal run control byte |
| 215 |
outPos++; |
| 216 |
inPos += len; |
| 217 |
// rebuild the future, and store the last bytes to the |
| 218 |
// hashtable. Storing hashes of the last bytes in back-reference |
| 219 |
// improves the compression ratio and only reduces speed |
| 220 |
// slightly. |
| 221 |
future = first(in, inPos); |
| 222 |
future = next(future, in, inPos); |
| 223 |
hashTab[hash(future)] = inPos++; |
| 224 |
future = next(future, in, inPos); |
| 225 |
hashTab[hash(future)] = inPos++; |
| 226 |
} else { |
| 227 |
// copy one byte from input to output as part of literal |
| 228 |
out[outPos++] = in[inPos++]; |
| 229 |
literals++; |
| 230 |
// at the end of this literal chunk, write the length |
| 231 |
// to the control byte and start a new chunk |
| 232 |
if (literals == MAX_LITERAL) { |
| 233 |
out[outPos - literals - 1] = (byte) (literals - 1); |
| 234 |
literals = 0; |
| 235 |
// move ahead one byte to allow for the |
| 236 |
// literal run control byte |
| 237 |
outPos++; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
// write the remaining few bytes as literals |
| 242 |
while (inPos < inLen) { |
| 243 |
out[outPos++] = in[inPos++]; |
| 244 |
literals++; |
| 245 |
if (literals == MAX_LITERAL) { |
| 246 |
out[outPos - literals - 1] = (byte) (literals - 1); |
| 247 |
literals = 0; |
| 248 |
outPos++; |
| 249 |
} |
| 250 |
} |
| 251 |
// writes the final literal run length to the control byte |
| 252 |
out[outPos - literals - 1] = (byte) (literals - 1); |
| 253 |
if (literals == 0) { |
| 254 |
outPos--; |
| 255 |
} |
| 256 |
return outPos; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Compress a number of bytes. |
| 261 |
* |
| 262 |
* @param in the input data |
| 263 |
* @param inPos the offset at the input buffer |
| 264 |
* @param out the output area |
| 265 |
* @param outPos the offset at the output array |
| 266 |
* @return the end position |
| 267 |
*/ |
| 268 |
public int compress(ByteBuffer in, int inPos, byte[] out, int outPos) { |
| 269 |
int offset = inPos; |
| 270 |
int inLen = in.capacity(); |
| 271 |
if (cachedHashTable == null) { |
| 272 |
cachedHashTable = new int[HASH_SIZE]; |
| 273 |
} |
| 274 |
int[] hashTab = cachedHashTable; |
| 275 |
int literals = 0; |
| 276 |
outPos++; |
| 277 |
int future = first(in, inPos); |
| 278 |
while (inPos < inLen - 4) { |
| 279 |
byte p2 = in.get(inPos + 2); |
| 280 |
// next |
| 281 |
future = (future << 8) + (p2 & 255); |
| 282 |
int off = hash(future); |
| 283 |
int ref = hashTab[off]; |
| 284 |
hashTab[off] = inPos; |
| 285 |
// if (ref < inPos |
| 286 |
// && ref > 0 |
| 287 |
// && (off = inPos - ref - 1) < MAX_OFF |
| 288 |
// && in[ref + 2] == p2 |
| 289 |
// && (((in[ref] & 255) << 8) | (in[ref + 1] & 255)) == |
| 290 |
// ((future >> 8) & 0xffff)) { |
| 291 |
if (ref < inPos |
| 292 |
&& ref > offset |
| 293 |
&& (off = inPos - ref - 1) < MAX_OFF |
| 294 |
&& in.get(ref + 2) == p2 |
| 295 |
&& in.get(ref + 1) == (byte) (future >> 8) |
| 296 |
&& in.get(ref) == (byte) (future >> 16)) { |
| 297 |
// match |
| 298 |
int maxLen = inLen - inPos - 2; |
| 299 |
if (maxLen > MAX_REF) { |
| 300 |
maxLen = MAX_REF; |
| 301 |
} |
| 302 |
if (literals == 0) { |
| 303 |
// multiple back-references, |
| 304 |
// so there is no literal run control byte |
| 305 |
outPos--; |
| 306 |
} else { |
| 307 |
// set the control byte at the start of the literal run |
| 308 |
// to store the number of literals |
| 309 |
out[outPos - literals - 1] = (byte) (literals - 1); |
| 310 |
literals = 0; |
| 311 |
} |
| 312 |
int len = 3; |
| 313 |
while (len < maxLen && in.get(ref + len) == in.get(inPos + len)) { |
| 314 |
len++; |
| 315 |
} |
| 316 |
len -= 2; |
| 317 |
if (len < 7) { |
| 318 |
out[outPos++] = (byte) ((off >> 8) + (len << 5)); |
| 319 |
} else { |
| 320 |
out[outPos++] = (byte) ((off >> 8) + (7 << 5)); |
| 321 |
out[outPos++] = (byte) (len - 7); |
| 322 |
} |
| 323 |
out[outPos++] = (byte) off; |
| 324 |
// move one byte forward to allow for a literal run control byte |
| 325 |
outPos++; |
| 326 |
inPos += len; |
| 327 |
// rebuild the future, and store the last bytes to the |
| 328 |
// hashtable. Storing hashes of the last bytes in back-reference |
| 329 |
// improves the compression ratio and only reduces speed |
| 330 |
// slightly. |
| 331 |
future = first(in, inPos); |
| 332 |
future = next(future, in, inPos); |
| 333 |
hashTab[hash(future)] = inPos++; |
| 334 |
future = next(future, in, inPos); |
| 335 |
hashTab[hash(future)] = inPos++; |
| 336 |
} else { |
| 337 |
// copy one byte from input to output as part of literal |
| 338 |
out[outPos++] = in.get(inPos++); |
| 339 |
literals++; |
| 340 |
// at the end of this literal chunk, write the length |
| 341 |
// to the control byte and start a new chunk |
| 342 |
if (literals == MAX_LITERAL) { |
| 343 |
out[outPos - literals - 1] = (byte) (literals - 1); |
| 344 |
literals = 0; |
| 345 |
// move ahead one byte to allow for the |
| 346 |
// literal run control byte |
| 347 |
outPos++; |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
// write the remaining few bytes as literals |
| 352 |
while (inPos < inLen) { |
| 353 |
out[outPos++] = in.get(inPos++); |
| 354 |
literals++; |
| 355 |
if (literals == MAX_LITERAL) { |
| 356 |
out[outPos - literals - 1] = (byte) (literals - 1); |
| 357 |
literals = 0; |
| 358 |
outPos++; |
| 359 |
} |
| 360 |
} |
| 361 |
// writes the final literal run length to the control byte |
| 362 |
out[outPos - literals - 1] = (byte) (literals - 1); |
| 363 |
if (literals == 0) { |
| 364 |
outPos--; |
| 365 |
} |
| 366 |
return outPos; |
| 367 |
} |
| 368 |
|
| 369 |
@Override |
| 370 |
public void expand(byte[] in, int inPos, int inLen, byte[] out, int outPos, |
| 371 |
int outLen) { |
| 372 |
// if ((inPos | outPos | outLen) < 0) { |
| 373 |
if (inPos < 0 || outPos < 0 || outLen < 0) { |
| 374 |
throw new IllegalArgumentException(); |
| 375 |
} |
| 376 |
do { |
| 377 |
int ctrl = in[inPos++] & 255; |
| 378 |
if (ctrl < MAX_LITERAL) { |
| 379 |
// literal run of length = ctrl + 1, |
| 380 |
ctrl++; |
| 381 |
// copy to output and move forward this many bytes |
| 382 |
// while (ctrl-- > 0) { |
| 383 |
// out[outPos++] = in[inPos++]; |
| 384 |
// } |
| 385 |
System.arraycopy(in, inPos, out, outPos, ctrl); |
| 386 |
outPos += ctrl; |
| 387 |
inPos += ctrl; |
| 388 |
} else { |
| 389 |
// back reference |
| 390 |
// the highest 3 bits are the match length |
| 391 |
int len = ctrl >> 5; |
| 392 |
// if the length is maxed, add the next byte to the length |
| 393 |
if (len == 7) { |
| 394 |
len += in[inPos++] & 255; |
| 395 |
} |
| 396 |
// minimum back-reference is 3 bytes, |
| 397 |
// so 2 was subtracted before storing size |
| 398 |
len += 2; |
| 399 |
|
| 400 |
// ctrl is now the offset for a back-reference... |
| 401 |
// the logical AND operation removes the length bits |
| 402 |
ctrl = -((ctrl & 0x1f) << 8) - 1; |
| 403 |
|
| 404 |
// the next byte augments/increases the offset |
| 405 |
ctrl -= in[inPos++] & 255; |
| 406 |
|
| 407 |
// copy the back-reference bytes from the given |
| 408 |
// location in output to current position |
| 409 |
ctrl += outPos; |
| 410 |
if (outPos + len >= out.length) { |
| 411 |
// reduce array bounds checking |
| 412 |
throw new ArrayIndexOutOfBoundsException(); |
| 413 |
} |
| 414 |
for (int i = 0; i < len; i++) { |
| 415 |
out[outPos++] = out[ctrl++]; |
| 416 |
} |
| 417 |
} |
| 418 |
} while (outPos < outLen); |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Expand a number of compressed bytes. |
| 423 |
* |
| 424 |
* @param in the compressed data |
| 425 |
* @param out the output area |
| 426 |
*/ |
| 427 |
public static void expand(ByteBuffer in, ByteBuffer out) { |
| 428 |
do { |
| 429 |
int ctrl = in.get() & 255; |
| 430 |
if (ctrl < MAX_LITERAL) { |
| 431 |
// literal run of length = ctrl + 1, |
| 432 |
ctrl++; |
| 433 |
// copy to output and move forward this many bytes |
| 434 |
// (maybe slice would be faster) |
| 435 |
for (int i = 0; i < ctrl; i++) { |
| 436 |
out.put(in.get()); |
| 437 |
} |
| 438 |
} else { |
| 439 |
// back reference |
| 440 |
// the highest 3 bits are the match length |
| 441 |
int len = ctrl >> 5; |
| 442 |
// if the length is maxed, add the next byte to the length |
| 443 |
if (len == 7) { |
| 444 |
len += in.get() & 255; |
| 445 |
} |
| 446 |
// minimum back-reference is 3 bytes, |
| 447 |
// so 2 was subtracted before storing size |
| 448 |
len += 2; |
| 449 |
|
| 450 |
// ctrl is now the offset for a back-reference... |
| 451 |
// the logical AND operation removes the length bits |
| 452 |
ctrl = -((ctrl & 0x1f) << 8) - 1; |
| 453 |
|
| 454 |
// the next byte augments/increases the offset |
| 455 |
ctrl -= in.get() & 255; |
| 456 |
|
| 457 |
// copy the back-reference bytes from the given |
| 458 |
// location in output to current position |
| 459 |
// (maybe slice would be faster) |
| 460 |
ctrl += out.position(); |
| 461 |
for (int i = 0; i < len; i++) { |
| 462 |
out.put(out.get(ctrl++)); |
| 463 |
} |
| 464 |
} |
| 465 |
} while (out.position() < out.capacity()); |
| 466 |
} |
| 467 |
|
| 468 |
@Override |
| 469 |
public int getAlgorithm() { |
| 470 |
return Compressor.LZF; |
| 471 |
} |
| 472 |
|
| 473 |
} |