LZSSold.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. namespace OpenVIII
  3. {
  4. public class LZSSold
  5. {
  6. /// <summary>
  7. /// Decompiles LZSS. You have to know the OutputSize. [DEPRECATED]
  8. /// </summary>
  9. /// <param name="data">buffer</param>
  10. /// <param name="fileSize">Original filesize of compressed file</param>
  11. /// <param name="size">Filesize of final file</param>
  12. /// <returns>Byte array</returns>
  13. [Obsolete("This method proved to be broken. Please use DecompressAllNew")]
  14. public static byte[] DecompressAll(byte[] data, uint fileSize, int size = 0)
  15. {
  16. try
  17. {
  18. bool bDynamic = false;
  19. if (size == 0)
  20. {
  21. size = 0x4000000; //64MB
  22. bDynamic = true;
  23. }
  24. byte[] result = new byte[size];
  25. int curResult = 0;
  26. int curBuff = 4078, flagByte = 0;
  27. int fileData = 4,
  28. endFileData = (int)fileSize;
  29. byte[] textBuf = new byte[4113];
  30. while (true)
  31. {
  32. if (fileData + 1 >= endFileData) return !bDynamic ? result : ReturnDynamic(result, curResult);
  33. if (((flagByte >>= 1) & 256) == 0)
  34. flagByte = data[fileData++] | 0xff00;
  35. if (fileData >= endFileData)
  36. return !bDynamic ? result : ReturnDynamic(result, curResult);
  37. if ((flagByte & 1) > 0)
  38. {
  39. result[curResult] = textBuf[curBuff] = data[fileData++];
  40. curBuff = (curBuff + 1) & 4095;
  41. ++curResult;
  42. }
  43. else
  44. {
  45. if (fileData + 1 >= endFileData)
  46. return !bDynamic ? result : ReturnDynamic(result, curResult);
  47. int offset = (byte)BitConverter.ToChar(data, fileData++);
  48. if (fileData + 1 >= endFileData)
  49. return !bDynamic ? result : ReturnDynamic(result, curResult);
  50. int length = (byte)BitConverter.ToChar(data, fileData++);
  51. offset |= (length & 0xF0) << 4;
  52. length = (length & 0xF) + 2 + offset;
  53. int e;
  54. for (e = offset; e <= length; e++)
  55. {
  56. textBuf[curBuff] = result[curResult] = textBuf[e & 4095];
  57. curBuff = (curBuff + 1) & 4095;
  58. ++curResult;
  59. }
  60. }
  61. }
  62. }
  63. catch
  64. {
  65. return null;
  66. }
  67. }
  68. private static byte[] ReturnDynamic(byte[] result, int curResult)
  69. {
  70. byte[] buffer = new byte[curResult];
  71. Array.Copy(result, buffer, buffer.Length);
  72. return buffer;
  73. }
  74. }
  75. }