V-Gears 0
Free Final Fantasy VII engine.
Lzs.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The V-Gears Team
3 *
4 * This file is part of V-Gears
5 *
6 * V-Gears is free software: you can redistribute it and/or modify it under
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3.0 (GPLv3) of the License.
9 *
10 * V-Gears is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#pragma once
17
18namespace Lzs{
19
26 inline std::vector<unsigned char> Decompress(const std::vector<unsigned char>& compressed){
27 if (compressed.size() < 4) abort();
28 const unsigned int inpuit_buffer_size = static_cast<unsigned int>(compressed.size());
29 const unsigned int input_length =
30 (((compressed[0] & 0xFF) << 0) |
31 ((compressed[1] & 0xFF) << 8) |
32 ((compressed[2] & 0xFF) << 16) |
33 ((compressed[3] & 0xFF) << 24)) + 4;
34 if (input_length != inpuit_buffer_size) abort();
35 unsigned int extract_size = (inpuit_buffer_size + 255) & ~255;
36 std::vector<unsigned char> extract_buffer(extract_size);
37 unsigned int input_offset = 4;
38 unsigned int output_offset = 0;
39 unsigned char control_byte = 0;
40 unsigned char control_bit = 0;
41 while (input_offset < inpuit_buffer_size){
42 if (control_bit == 0){
43 control_byte = compressed[input_offset ++];
44 control_bit = 8;
45 }
46 if (control_byte & 1){
47 extract_buffer[output_offset++] = compressed[input_offset ++];
48 if (output_offset == extract_size){
49 extract_size += 256;
50 extract_buffer.resize(extract_size);
51 }
52 }
53 else{
54 const unsigned char reference1 = compressed[input_offset ++];
55 const unsigned char reference2 = compressed[input_offset ++];
56 const unsigned short int reference_offset = reference1 | ((reference2 & 0xF0) << 4);
57 const unsigned char reference_length = (reference2 & 0xF) + 3;
58 int real_offset = output_offset - ((output_offset - 18 - reference_offset) & 0xFFF);
59 for (int j = 0; j < reference_length; ++ j){
60 if (real_offset < 0) extract_buffer[output_offset ++] = 0;
61 else extract_buffer[output_offset ++] = extract_buffer[real_offset];
62 if (output_offset == extract_size) {
63 extract_size += 256;
64 extract_buffer.resize(extract_size);
65 }
66 ++ real_offset;
67 }
68 }
69 control_byte >>= 1;
70 control_bit --;
71 }
72 // Truncate to exact size.
73 extract_buffer.resize(output_offset);
74 return extract_buffer;
75 }
76}
Definition: Lzs.h:18
std::vector< unsigned char > Decompress(const std::vector< unsigned char > &compressed)
Decompresses LZS data.
Definition: Lzs.h:26