commit 738c7fcf6a92242f8b007ccdf6e59f209f9fe963 Author: Prof. 9 Date: Mon Jul 23 00:58:44 2018 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e759b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,330 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ diff --git a/LZOnDecompress.sln b/LZOnDecompress.sln new file mode 100644 index 0000000..05c8e60 --- /dev/null +++ b/LZOnDecompress.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2042 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LZOnDecompress", "LZOnDecompress\LZOnDecompress.csproj", "{DFF70B93-87D4-4BD7-BC96-3AF6D18D40AB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DFF70B93-87D4-4BD7-BC96-3AF6D18D40AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DFF70B93-87D4-4BD7-BC96-3AF6D18D40AB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DFF70B93-87D4-4BD7-BC96-3AF6D18D40AB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DFF70B93-87D4-4BD7-BC96-3AF6D18D40AB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {47F5CDE0-BAAB-474B-9E2C-19E21DC1FDEC} + EndGlobalSection +EndGlobal diff --git a/LZOnDecompress/LZOnDecompress.csproj b/LZOnDecompress/LZOnDecompress.csproj new file mode 100644 index 0000000..e97e719 --- /dev/null +++ b/LZOnDecompress/LZOnDecompress.csproj @@ -0,0 +1,47 @@ + + + + + Debug + AnyCPU + {DFF70B93-87D4-4BD7-BC96-3AF6D18D40AB} + Exe + LZOnDecompress + LZOnDecompress + v4.0 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + none + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LZOnDecompress/Program.cs b/LZOnDecompress/Program.cs new file mode 100644 index 0000000..bdb473e --- /dev/null +++ b/LZOnDecompress/Program.cs @@ -0,0 +1,250 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace LZOnDecompress { + class Program { + static int Main(string[] args) { + bool success = false; + + try { + Console.WriteLine("LZOnDecompress v0.1 by Prof. 9"); + Console.WriteLine(); + + if (args.Length != 1) { + Console.WriteLine("Usage:"); + Console.WriteLine(" LZOnDecompress.exe "); +#if DEBUG + Console.ReadKey(); +#endif + } else { + + MemoryStream input = new MemoryStream(); + using (FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.Read)) { + fs.CopyTo(input); + } + + MemoryStream output = new MemoryStream(); + input.Position = 0; + try { + success = Decompress(input, output); + } catch { } + + if (success) { + Console.WriteLine("File decompressed successfully."); + string outPath = Path.GetFileNameWithoutExtension(args[0]) + ".srl"; + output.Position = 0; + using (FileStream fs = new FileStream(outPath, FileMode.Create, FileAccess.Write, FileShare.None)) { + output.CopyTo(fs); + } + } else { + Console.WriteLine("Could not decompress file."); + } + } + + Console.WriteLine(); + Console.WriteLine("Done."); + } catch (Exception ex) { + Console.WriteLine("FATAL: " + ex.Message); + return 2; + } +#if DEBUG + Console.ReadKey(true); +#endif + + return success ? 0 : 1; + } + + static bool Decompress(MemoryStream input, MemoryStream output) { + int state, len, dist, instr; + + try { + // Check header magic. + if (input.ReadByte() != 'L' || + input.ReadByte() != 'Z' || + input.ReadByte() != 'O' || + input.ReadByte() != 'n' || + input.ReadByte() != 0x00 || + input.ReadByte() != 0x2F || + input.ReadByte() != 0xF1 || + input.ReadByte() != 0x71) { + return false; + } + + // Read sizes. + uint decompressedSize = InputRead32(); + uint compressedSize = InputRead32() + 17; + bool hasInstr = false; + state = -1; + + // Process first byte. + instr = InputRead8(); + if (instr > 17) { + len = instr - 17; + InputCopy(); + state = Math.Min(instr - 17, 4); + } else { + hasInstr = true; + } + + while (true) { + if (input.Position >= compressedSize) { + return false; + } + if (!hasInstr) { + instr = InputRead8(); + } + hasInstr = false; + + if (instr <= 0xF) { + switch (state) { + case 0: + len = ReadLen(0, 4) + 3; + state = 4; + InputCopy(); + break; + case 1: + case 2: + case 3: + len = 2; + dist = ReadOper(2, 2) + 1 + (InputRead8() << 2); + state = ReadOper(0, 2); + DictionaryCopy(); + break; + case 4: + len = 3; + dist = ReadOper(2, 2) + 2049 + (InputRead8() << 2); + state = ReadOper(0, 2); + DictionaryCopy(); + break; + default: + return false; + } + } else if (instr <= 0x1F) { + len = ReadLen(0, 3) + 2; + int x = InputRead16(); + dist = (ReadOper(3, 1) << 14) + 16384 + (x >> 2); + if (dist == 16384) { + break; + } + state = x & 0b11; + DictionaryCopy(); + } else if (instr <= 0x3F) { + len = ReadLen(0, 5) + 2; + int x = InputRead16(); + dist = 1 + (x >> 2); + state = x & 0b11; + DictionaryCopy(); + } else if (instr <= 0x7F) { + len = ReadOper(5, 1) + 3; + int x = InputRead8(); + dist = ReadOper(2, 3) + 1 + (x << 3); + state = ReadOper(0, 2); + DictionaryCopy(); + } else { + len = ReadOper(5, 2) + 5; + dist = ReadOper(2, 3) + 1 + (InputRead8() << 3); + state = ReadOper(0, 2); + DictionaryCopy(); + } + + if (state < 4) { + len = state; + InputCopy(); + } + } + + while (output.Length < decompressedSize) { + output.WriteByte(0); + } + } catch (EndOfStreamException) { + return false; + } + + return true; + + byte InputRead8() { + int b = input.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + return (byte)b; + } + + ushort InputRead16() { + ushort r = 0; + + int b = input.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + r += (ushort)b; + + b = input.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + r += (ushort)(b << 8); + + return r; + } + + uint InputRead32() { + uint r = 0; + + int b = input.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + r += (uint)(b << 24); + + b = input.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + r += (uint)(b << 16); + + b = input.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + r += (uint)(b << 8); + + b = input.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + r += (uint)b; + + return r; + } + + int ReadOper(int shift, int bits) { + return (instr >> shift) & ((1 << bits) - 1); + } + + int ReadLen(int shift, int bits) { + int mask = (1 << bits) - 1; + int r = (instr >> shift) & mask; + if (r == 0) { + r = mask; + int b; + while ((b = input.ReadByte()) == 0) { + r += 255; + } + if (b < 0) throw new EndOfStreamException(); + r += b; + } + return r; + } + + void InputCopy() { + int b; + while (len-- > 0) { + b = input.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + output.WriteByte((byte)b); + } + } + + void DictionaryCopy() { + int b; + while (len-- > 0) { + output.Position -= dist; + b = output.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + output.Position += dist - 1; + output.WriteByte((byte)b); + } + } + } + } +} diff --git a/LZOnDecompress/Properties/AssemblyInfo.cs b/LZOnDecompress/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..612e79f --- /dev/null +++ b/LZOnDecompress/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("LZOnDecompress")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("LZOnDecompress")] +[assembly: AssemblyCopyright("Copyright © Prof. 9 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("dff70b93-87d4-4bd7-bc96-3af6d18d40ab")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..fce6e73 --- /dev/null +++ b/readme.md @@ -0,0 +1,25 @@ +# LZOnDecompress + +Quick and dirty LZOn decompressor. LZOn is the format used for compressed multiboot files in DS Download Station ROMs. + +Usage: + +``` +LZOnDecompress.exe +``` + +Produces filename.nds in the working directory. + +**NO SUPPORT PROVIDED.** Feel free to report bugs though. + +## LZOn reference + +``` +0x00: Magic "LZOn" +0x04: Magic 0x00,0x2F,0xF1,0x71 (presumably) +0x08: Size of decompressed file (32-bit, big endian) +0x0C: Size of compressed file minus 0x11 (32-bit big endian) +0x10: LZO compressed data stream (follows https://www.kernel.org/doc/Documentation/lzo.txt). Note that 16-bit values in the LZO stream are little endian. +``` + +Output generated by decompressing the LZO data stream and then padding to the decompressed file size specified in the header with 0x00 bytes. \ No newline at end of file