Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace lzssDecomp
- {
- class Program
- {
- static BinaryWriter b1;
- static void Main(string[] args)
- {
- // Only get relevant files
- var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.*", SearchOption.AllDirectories)
- .Where(s => s.EndsWith("._DT"));
- foreach (string filename in files)
- {
- if (!filename.Contains("_decom"))
- {
- DecompFiles(filename);
- }
- }
- }
- static void DecompFiles(String filename)
- {
- byte[] header = new byte[8];
- if (!File.Exists(filename))
- {
- Console.WriteLine("File does not exist!");
- Environment.Exit(-1);
- }
- StringBuilder sb = new StringBuilder();
- sb.Append(Path.GetDirectoryName(filename));
- sb.Append("\\");
- sb.Append(Path.GetFileNameWithoutExtension(filename));
- sb.Append("._decom");
- FileStream comp = File.OpenRead(filename);
- FileStream uncomp = null;
- byte[] InBuf;
- if ("._DT" == Path.GetExtension(filename))
- {
- comp.Seek(0, SeekOrigin.Begin);
- comp.Read(header, 0, 8);
- uncomp = File.Create(sb.ToString());
- comp.Seek(8, SeekOrigin.Begin);
- b1 = new BinaryWriter(uncomp);
- InBuf = new byte[comp.Length - 8];
- comp.Read(InBuf, 0, InBuf.Length);
- DecompressBlock(InBuf);
- }
- Console.WriteLine("Decompressed: {0}", filename);
- comp.Close();
- uncomp.Close();
- }
- static void DecompressBlock(byte[] InBufi)
- {
- const byte NBits = 12;
- const byte FBits = 4;
- const byte Threshold = 3;
- uint N;
- uint F;
- int Offset;
- uint Len;
- byte Flags,
- b;
- uint i, j,
- InBufPos,
- HisBufPos;
- byte[] HisBuf, InBuf = InBufi;
- // initialize LZSS parameters for this block
- N = (1 << NBits) - 1;
- F = (1 << FBits) - 1;
- HisBufPos = N - F - 2;
- HisBuf = new byte[N + 1];
- for (int k = 0; k < N + 1; k++)
- {
- HisBuf[k] = 0;
- }
- InBufPos = 0;
- // start extracting block contents
- while (InBufPos < InBuf.Length)
- {
- // read bit field
- Flags = InBuf[InBufPos];
- InBufPos += 1;
- // process bit field
- for (i = 1; i < 9; i++)
- {
- //Console.Write(i);
- if (InBufPos < InBuf.Length)
- {
- // check for buffer window reference
- if ((Flags & 1) == 0)
- {
- // read offset and length
- Len = (InBuf[InBufPos + 1] & F) + Threshold;
- Offset = (((InBuf[InBufPos + 1] >> FBits) << 8) + InBuf[InBufPos]);
- InBufPos += 2;
- for (j = 0; j < Len; j++)
- {
- b = HisBuf[(Offset + j) & N];
- write_byte(b);
- HisBuf[HisBufPos] = b;
- HisBufPos = (HisBufPos + 1) & N;
- }
- }
- else
- { // copy literally
- b = InBuf[InBufPos];
- InBufPos += 1;
- write_byte(b);
- HisBuf[HisBufPos] = b;
- HisBufPos = (HisBufPos + 1) & N;
- }
- Flags >>= 1;
- }
- }
- }
- }
- public static void write_byte(int b)
- {
- b1.Write((byte)b);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement