Lzs.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #pragma once
  16. namespace Lzs{
  17. /**
  18. * Decompresses LZS data.
  19. *
  20. * @param compressed[in] Compressed data.
  21. * @return Decompressed data.
  22. */
  23. inline std::vector<unsigned char> Decompress(const std::vector<unsigned char>& compressed){
  24. if (compressed.size() < 4) abort();
  25. const unsigned int inpuit_buffer_size = static_cast<unsigned int>(compressed.size());
  26. const unsigned int input_length =
  27. (((compressed[0] & 0xFF) << 0) |
  28. ((compressed[1] & 0xFF) << 8) |
  29. ((compressed[2] & 0xFF) << 16) |
  30. ((compressed[3] & 0xFF) << 24)) + 4;
  31. if (input_length != inpuit_buffer_size) abort();
  32. unsigned int extract_size = (inpuit_buffer_size + 255) & ~255;
  33. std::vector<unsigned char> extract_buffer(extract_size);
  34. unsigned int input_offset = 4;
  35. unsigned int output_offset = 0;
  36. unsigned char control_byte = 0;
  37. unsigned char control_bit = 0;
  38. while (input_offset < inpuit_buffer_size){
  39. if (control_bit == 0){
  40. control_byte = compressed[input_offset ++];
  41. control_bit = 8;
  42. }
  43. if (control_byte & 1){
  44. extract_buffer[output_offset++] = compressed[input_offset ++];
  45. if (output_offset == extract_size){
  46. extract_size += 256;
  47. extract_buffer.resize(extract_size);
  48. }
  49. }
  50. else{
  51. const unsigned char reference1 = compressed[input_offset ++];
  52. const unsigned char reference2 = compressed[input_offset ++];
  53. const unsigned short int reference_offset = reference1 | ((reference2 & 0xF0) << 4);
  54. const unsigned char reference_length = (reference2 & 0xF) + 3;
  55. int real_offset = output_offset - ((output_offset - 18 - reference_offset) & 0xFFF);
  56. for (int j = 0; j < reference_length; ++ j){
  57. if (real_offset < 0) extract_buffer[output_offset ++] = 0;
  58. else extract_buffer[output_offset ++] = extract_buffer[real_offset];
  59. if (output_offset == extract_size) {
  60. extract_size += 256;
  61. extract_buffer.resize(extract_size);
  62. }
  63. ++ real_offset;
  64. }
  65. }
  66. control_byte >>= 1;
  67. control_bit --;
  68. }
  69. // Truncate to exact size.
  70. extract_buffer.resize(output_offset);
  71. return extract_buffer;
  72. }
  73. }