Advertisement
Joshua_V_P

lzssDecomp

Feb 4th, 2014
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace lzssDecomp
  8. {
  9.     class Program
  10.     {
  11.         static BinaryWriter b1;
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.  
  16.             // Only get relevant files
  17.             var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.*", SearchOption.AllDirectories)
  18.                 .Where(s => s.EndsWith("._DT"));
  19.  
  20.             foreach (string filename in files)
  21.             {
  22.                 if (!filename.Contains("_decom"))
  23.                 {
  24.                     DecompFiles(filename);
  25.                 }
  26.             }
  27.         }
  28.  
  29.         static void DecompFiles(String filename)
  30.         {
  31.             byte[] header = new byte[8];
  32.  
  33.             if (!File.Exists(filename))
  34.             {
  35.                 Console.WriteLine("File does not exist!");
  36.                 Environment.Exit(-1);
  37.             }
  38.  
  39.             StringBuilder sb = new StringBuilder();
  40.             sb.Append(Path.GetDirectoryName(filename));
  41.             sb.Append("\\");
  42.             sb.Append(Path.GetFileNameWithoutExtension(filename));
  43.             sb.Append("._decom");
  44.  
  45.  
  46.             FileStream comp = File.OpenRead(filename);
  47.             FileStream uncomp = null;
  48.             byte[] InBuf;
  49.  
  50.             if ("._DT" == Path.GetExtension(filename))
  51.             {
  52.  
  53.                 comp.Seek(0, SeekOrigin.Begin);
  54.                 comp.Read(header, 0, 8);
  55.  
  56.                 uncomp = File.Create(sb.ToString());
  57.                 comp.Seek(8, SeekOrigin.Begin);
  58.                 b1 = new BinaryWriter(uncomp);
  59.                 InBuf = new byte[comp.Length - 8];
  60.                 comp.Read(InBuf, 0, InBuf.Length);
  61.                 DecompressBlock(InBuf);
  62.             }
  63.  
  64.             Console.WriteLine("Decompressed: {0}", filename);
  65.             comp.Close();
  66.             uncomp.Close();
  67.         }
  68.  
  69.         static void DecompressBlock(byte[] InBufi)
  70.         {
  71.             const byte NBits = 12;
  72.             const byte FBits = 4;
  73.             const byte Threshold = 3;
  74.             uint N;
  75.             uint F;
  76.             int Offset;
  77.             uint Len;
  78.             byte Flags,
  79.             b;
  80.  
  81.             uint i, j,
  82.             InBufPos,
  83.             HisBufPos;
  84.             byte[] HisBuf, InBuf = InBufi;
  85.  
  86.             // initialize LZSS parameters for this block
  87.             N = (1 << NBits) - 1;
  88.             F = (1 << FBits) - 1;
  89.             HisBufPos = N - F - 2;
  90.             HisBuf = new byte[N + 1];
  91.             for (int k = 0; k < N + 1; k++)
  92.             {
  93.                 HisBuf[k] = 0;
  94.             }
  95.  
  96.             InBufPos = 0;
  97.  
  98.             // start extracting block contents
  99.             while (InBufPos < InBuf.Length)
  100.             {
  101.  
  102.                 // read bit field
  103.                 Flags = InBuf[InBufPos];
  104.                 InBufPos += 1;
  105.  
  106.                 // process bit field
  107.                 for (i = 1; i < 9; i++)
  108.                 {
  109.                     //Console.Write(i);
  110.                     if (InBufPos < InBuf.Length)
  111.                     {
  112.                         // check for buffer window reference
  113.                         if ((Flags & 1) == 0)
  114.                         {
  115.                             // read offset and length
  116.                             Len = (InBuf[InBufPos + 1] & F) + Threshold;
  117.                             Offset = (((InBuf[InBufPos + 1] >> FBits) << 8) + InBuf[InBufPos]);
  118.                             InBufPos += 2;
  119.                             for (j = 0; j < Len; j++)
  120.                             {
  121.                                 b = HisBuf[(Offset + j) & N];
  122.                                 write_byte(b);
  123.                                 HisBuf[HisBufPos] = b;
  124.                                 HisBufPos = (HisBufPos + 1) & N;
  125.                             }
  126.  
  127.                         }
  128.                         else
  129.                         { // copy literally
  130.                             b = InBuf[InBufPos];
  131.                             InBufPos += 1;
  132.                             write_byte(b);
  133.                             HisBuf[HisBufPos] = b;
  134.                             HisBufPos = (HisBufPos + 1) & N;
  135.                         }
  136.  
  137.                         Flags >>= 1;
  138.  
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.  
  144.         public static void write_byte(int b)
  145.         {
  146.             b1.Write((byte)b);
  147.         }
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement