├── .gitattributes
├── .gitignore
├── App.config
├── DeltaDownloader.csproj
├── DeltaDownloader.sln
├── LICENSE.txt
├── MsDelta
├── BitReader.cs
├── BitScanner.cs
├── CliMetadata.cs
├── CryptAlg.cs
├── DeltaFile.cs
├── DeltaFlags.cs
├── FileTypeCode.cs
├── Helpers.cs
├── IntFormat.cs
├── PreProcess.cs
├── RiftTable.cs
└── StaticHuffman
│ ├── Codes.cs
│ └── DecoderTable.cs
├── Program.cs
├── Properties
└── AssemblyInfo.cs
├── README.md
└── packages.config
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Ww][Ii][Nn]32/
27 | [Aa][Rr][Mm]/
28 | [Aa][Rr][Mm]64/
29 | bld/
30 | [Bb]in/
31 | [Oo]bj/
32 | [Ll]og/
33 | [Ll]ogs/
34 |
35 | # Visual Studio 2015/2017 cache/options directory
36 | .vs/
37 | # Uncomment if you have tasks that create the project's static files in wwwroot
38 | #wwwroot/
39 |
40 | # Visual Studio 2017 auto generated files
41 | Generated\ Files/
42 |
43 | # MSTest test Results
44 | [Tt]est[Rr]esult*/
45 | [Bb]uild[Ll]og.*
46 |
47 | # NUnit
48 | *.VisualState.xml
49 | TestResult.xml
50 | nunit-*.xml
51 |
52 | # Build Results of an ATL Project
53 | [Dd]ebugPS/
54 | [Rr]eleasePS/
55 | dlldata.c
56 |
57 | # Benchmark Results
58 | BenchmarkDotNet.Artifacts/
59 |
60 | # .NET Core
61 | project.lock.json
62 | project.fragment.lock.json
63 | artifacts/
64 |
65 | # ASP.NET Scaffolding
66 | ScaffoldingReadMe.txt
67 |
68 | # StyleCop
69 | StyleCopReport.xml
70 |
71 | # Files built by Visual Studio
72 | *_i.c
73 | *_p.c
74 | *_h.h
75 | *.ilk
76 | *.meta
77 | *.obj
78 | *.iobj
79 | *.pch
80 | *.pdb
81 | *.ipdb
82 | *.pgc
83 | *.pgd
84 | *.rsp
85 | *.sbr
86 | *.tlb
87 | *.tli
88 | *.tlh
89 | *.tmp
90 | *.tmp_proj
91 | *_wpftmp.csproj
92 | *.log
93 | *.tlog
94 | *.vspscc
95 | *.vssscc
96 | .builds
97 | *.pidb
98 | *.svclog
99 | *.scc
100 |
101 | # Chutzpah Test files
102 | _Chutzpah*
103 |
104 | # Visual C++ cache files
105 | ipch/
106 | *.aps
107 | *.ncb
108 | *.opendb
109 | *.opensdf
110 | *.sdf
111 | *.cachefile
112 | *.VC.db
113 | *.VC.VC.opendb
114 |
115 | # Visual Studio profiler
116 | *.psess
117 | *.vsp
118 | *.vspx
119 | *.sap
120 |
121 | # Visual Studio Trace Files
122 | *.e2e
123 |
124 | # TFS 2012 Local Workspace
125 | $tf/
126 |
127 | # Guidance Automation Toolkit
128 | *.gpState
129 |
130 | # ReSharper is a .NET coding add-in
131 | _ReSharper*/
132 | *.[Rr]e[Ss]harper
133 | *.DotSettings.user
134 |
135 | # TeamCity is a build add-in
136 | _TeamCity*
137 |
138 | # DotCover is a Code Coverage Tool
139 | *.dotCover
140 |
141 | # AxoCover is a Code Coverage Tool
142 | .axoCover/*
143 | !.axoCover/settings.json
144 |
145 | # Coverlet is a free, cross platform Code Coverage Tool
146 | coverage*.json
147 | coverage*.xml
148 | coverage*.info
149 |
150 | # Visual Studio code coverage results
151 | *.coverage
152 | *.coveragexml
153 |
154 | # NCrunch
155 | _NCrunch_*
156 | .*crunch*.local.xml
157 | nCrunchTemp_*
158 |
159 | # MightyMoose
160 | *.mm.*
161 | AutoTest.Net/
162 |
163 | # Web workbench (sass)
164 | .sass-cache/
165 |
166 | # Installshield output folder
167 | [Ee]xpress/
168 |
169 | # DocProject is a documentation generator add-in
170 | DocProject/buildhelp/
171 | DocProject/Help/*.HxT
172 | DocProject/Help/*.HxC
173 | DocProject/Help/*.hhc
174 | DocProject/Help/*.hhk
175 | DocProject/Help/*.hhp
176 | DocProject/Help/Html2
177 | DocProject/Help/html
178 |
179 | # Click-Once directory
180 | publish/
181 |
182 | # Publish Web Output
183 | *.[Pp]ublish.xml
184 | *.azurePubxml
185 | # Note: Comment the next line if you want to checkin your web deploy settings,
186 | # but database connection strings (with potential passwords) will be unencrypted
187 | *.pubxml
188 | *.publishproj
189 |
190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
191 | # checkin your Azure Web App publish settings, but sensitive information contained
192 | # in these scripts will be unencrypted
193 | PublishScripts/
194 |
195 | # NuGet Packages
196 | *.nupkg
197 | # NuGet Symbol Packages
198 | *.snupkg
199 | # The packages folder can be ignored because of Package Restore
200 | **/[Pp]ackages/*
201 | # except build/, which is used as an MSBuild target.
202 | !**/[Pp]ackages/build/
203 | # Uncomment if necessary however generally it will be regenerated when needed
204 | #!**/[Pp]ackages/repositories.config
205 | # NuGet v3's project.json files produces more ignorable files
206 | *.nuget.props
207 | *.nuget.targets
208 |
209 | # Microsoft Azure Build Output
210 | csx/
211 | *.build.csdef
212 |
213 | # Microsoft Azure Emulator
214 | ecf/
215 | rcf/
216 |
217 | # Windows Store app package directories and files
218 | AppPackages/
219 | BundleArtifacts/
220 | Package.StoreAssociation.xml
221 | _pkginfo.txt
222 | *.appx
223 | *.appxbundle
224 | *.appxupload
225 |
226 | # Visual Studio cache files
227 | # files ending in .cache can be ignored
228 | *.[Cc]ache
229 | # but keep track of directories ending in .cache
230 | !?*.[Cc]ache/
231 |
232 | # Others
233 | ClientBin/
234 | ~$*
235 | *~
236 | *.dbmdl
237 | *.dbproj.schemaview
238 | *.jfm
239 | *.pfx
240 | *.publishsettings
241 | orleans.codegen.cs
242 |
243 | # Including strong name files can present a security risk
244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
245 | #*.snk
246 |
247 | # Since there are multiple workflows, uncomment next line to ignore bower_components
248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
249 | #bower_components/
250 |
251 | # RIA/Silverlight projects
252 | Generated_Code/
253 |
254 | # Backup & report files from converting an old project file
255 | # to a newer Visual Studio version. Backup files are not needed,
256 | # because we have git ;-)
257 | _UpgradeReport_Files/
258 | Backup*/
259 | UpgradeLog*.XML
260 | UpgradeLog*.htm
261 | ServiceFabricBackup/
262 | *.rptproj.bak
263 |
264 | # SQL Server files
265 | *.mdf
266 | *.ldf
267 | *.ndf
268 |
269 | # Business Intelligence projects
270 | *.rdl.data
271 | *.bim.layout
272 | *.bim_*.settings
273 | *.rptproj.rsuser
274 | *- [Bb]ackup.rdl
275 | *- [Bb]ackup ([0-9]).rdl
276 | *- [Bb]ackup ([0-9][0-9]).rdl
277 |
278 | # Microsoft Fakes
279 | FakesAssemblies/
280 |
281 | # GhostDoc plugin setting file
282 | *.GhostDoc.xml
283 |
284 | # Node.js Tools for Visual Studio
285 | .ntvs_analysis.dat
286 | node_modules/
287 |
288 | # Visual Studio 6 build log
289 | *.plg
290 |
291 | # Visual Studio 6 workspace options file
292 | *.opt
293 |
294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
295 | *.vbw
296 |
297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.)
298 | *.vbp
299 |
300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project)
301 | *.dsw
302 | *.dsp
303 |
304 | # Visual Studio 6 technical files
305 | *.ncb
306 | *.aps
307 |
308 | # Visual Studio LightSwitch build output
309 | **/*.HTMLClient/GeneratedArtifacts
310 | **/*.DesktopClient/GeneratedArtifacts
311 | **/*.DesktopClient/ModelManifest.xml
312 | **/*.Server/GeneratedArtifacts
313 | **/*.Server/ModelManifest.xml
314 | _Pvt_Extensions
315 |
316 | # Paket dependency manager
317 | .paket/paket.exe
318 | paket-files/
319 |
320 | # FAKE - F# Make
321 | .fake/
322 |
323 | # CodeRush personal settings
324 | .cr/personal
325 |
326 | # Python Tools for Visual Studio (PTVS)
327 | __pycache__/
328 | *.pyc
329 |
330 | # Cake - Uncomment if you are using it
331 | # tools/**
332 | # !tools/packages.config
333 |
334 | # Tabs Studio
335 | *.tss
336 |
337 | # Telerik's JustMock configuration file
338 | *.jmconfig
339 |
340 | # BizTalk build output
341 | *.btp.cs
342 | *.btm.cs
343 | *.odx.cs
344 | *.xsd.cs
345 |
346 | # OpenCover UI analysis results
347 | OpenCover/
348 |
349 | # Azure Stream Analytics local run output
350 | ASALocalRun/
351 |
352 | # MSBuild Binary and Structured Log
353 | *.binlog
354 |
355 | # NVidia Nsight GPU debugger configuration file
356 | *.nvuser
357 |
358 | # MFractors (Xamarin productivity tool) working folder
359 | .mfractor/
360 |
361 | # Local History for Visual Studio
362 | .localhistory/
363 |
364 | # Visual Studio History (VSHistory) files
365 | .vshistory/
366 |
367 | # BeatPulse healthcheck temp database
368 | healthchecksdb
369 |
370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
371 | MigrationBackup/
372 |
373 | # Ionide (cross platform F# VS Code tools) working folder
374 | .ionide/
375 |
376 | # Fody - auto-generated XML schema
377 | FodyWeavers.xsd
378 |
379 | # VS Code files for those working on multiple tools
380 | .vscode/*
381 | !.vscode/settings.json
382 | !.vscode/tasks.json
383 | !.vscode/launch.json
384 | !.vscode/extensions.json
385 | *.code-workspace
386 |
387 | # Local History for Visual Studio Code
388 | .history/
389 |
390 | # Windows Installer files from build outputs
391 | *.cab
392 | *.msi
393 | *.msix
394 | *.msm
395 | *.msp
396 |
397 | # JetBrains Rider
398 | *.sln.iml
399 |
--------------------------------------------------------------------------------
/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/DeltaDownloader.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {3AAF9EAF-4C60-4D90-8138-1FFC74770B4E}
8 | Exe
9 | DeltaDownloader
10 | DeltaDownloader
11 | v4.7.2
12 | 512
13 | true
14 | true
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 | true
26 |
27 |
28 | AnyCPU
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 | true
36 |
37 |
38 |
39 |
40 | packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll
41 |
42 |
43 |
44 | packages\System.Memory.4.5.5\lib\net461\System.Memory.dll
45 |
46 |
47 |
48 | packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll
49 |
50 |
51 | packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 | packages\SoftUni.Wintellect.PowerCollections.2.0.0\lib\net20\Wintellect.PowerCollections.dll
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/DeltaDownloader.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.2.32616.157
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeltaDownloader", "DeltaDownloader.csproj", "{3AAF9EAF-4C60-4D90-8138-1FFC74770B4E}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3AAF9EAF-4C60-4D90-8138-1FFC74770B4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3AAF9EAF-4C60-4D90-8138-1FFC74770B4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3AAF9EAF-4C60-4D90-8138-1FFC74770B4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3AAF9EAF-4C60-4D90-8138-1FFC74770B4E}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {A0E88900-2630-409B-B218-E52FC1699D70}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) [year] [fullname]
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/MsDelta/BitReader.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace MsDelta
9 | {
10 | public class BitReader
11 | {
12 | private ReadOnlySpan BufferImpl => new ReadOnlySpan(m_Array, m_Offset, m_Array.Length - m_Offset);
13 | private ReadOnlySpan BufferCurrent => new ReadOnlySpan(m_Array, m_Offset + m_Current, m_End - m_Current);
14 |
15 | private ReadOnlySpan BufferEnd => new ReadOnlySpan(m_Array, m_Offset + m_End, m_Array.Length - m_Offset - m_End);
16 |
17 | public int CurrentOffset => m_Offset + m_LastSeekOffset;
18 | public bool AtEnd => m_End == m_Current;
19 |
20 | private byte UseByte()
21 | {
22 | var ret = BufferCurrent[0];
23 | m_Current++;
24 | m_LastSeekOffset++;
25 | return ret;
26 | }
27 |
28 | private uint UseDword()
29 | {
30 | uint ret = UseByte();
31 | ret |= (uint)UseByte() << 8;
32 | ret |= (uint)UseByte() << 16;
33 | ret |= (uint)UseByte() << 24;
34 | return ret;
35 | }
36 |
37 | private readonly byte[] m_Array;
38 | int m_Offset;
39 | int m_LastSeekOffset;
40 | int m_Current;
41 | int m_End;
42 | uint m_ExtraBits;
43 | internal ulong m_ShiftRegister { get; private set; }
44 | uint m_ValidLength;
45 | uint m_BitPadding;
46 |
47 | public BitReader(byte[] buffer) : this(new ArraySegment(buffer)) { }
48 |
49 | public BitReader(byte[] buffer, int offset) : this(new ArraySegment(buffer, offset, buffer.Length - offset)) { }
50 | public BitReader(ArraySegment buffer)
51 | {
52 | m_ExtraBits = 0;
53 | m_ShiftRegister = 0;
54 | m_ValidLength = 0;
55 | m_Current = 0;
56 | m_End = 0;
57 | m_Array = buffer.Array;
58 | if (m_Array.Length == 0) throw new IndexOutOfRangeException();
59 | m_Offset = buffer.Offset;
60 | m_LastSeekOffset = 0;
61 | m_BitPadding = (uint) (BufferImpl[0] & 0b111);
62 | if (BufferImpl.Length == 1 && m_BitPadding > 5) throw new InvalidDataException();
63 | Seek(0);
64 | Read(3);
65 | }
66 |
67 | public void Seek(int offset)
68 | {
69 | int remaining = BufferImpl.Length - offset;
70 | int unknown = (-offset) & 0b11;
71 | int realRemaining = Math.Min(unknown, remaining);
72 |
73 | m_ShiftRegister = 0;
74 | m_ValidLength = 0;
75 | m_LastSeekOffset = offset;
76 | switch (realRemaining)
77 | {
78 | case 3:
79 | m_ShiftRegister |= (uint)BufferImpl[offset + 2] << 16;
80 | goto case 2;
81 | case 2:
82 | m_ShiftRegister |= (uint)BufferImpl[offset + 1] << 8;
83 | goto case 1;
84 | case 1:
85 | m_ShiftRegister |= BufferImpl[offset + 0];
86 | break;
87 | }
88 |
89 |
90 | if (realRemaining == remaining)
91 | {
92 | m_Current = 0;
93 | m_End = 0;
94 | m_ValidLength = (8 * (uint)realRemaining) - m_BitPadding;
95 | m_ExtraBits = 0;
96 | return;
97 | }
98 |
99 | int diff = remaining - unknown;
100 | m_Current = offset + unknown;
101 | m_ValidLength = 8 * (uint)unknown;
102 | int diff2 = (diff - 1) & ~0b11;
103 | m_ExtraBits = 8 * (uint)(diff - diff2) - m_BitPadding;
104 | m_End = 4 * (diff2 >> 2) + unknown + offset;
105 | Consume(0);
106 | }
107 |
108 | private void CheckLength(int length)
109 | {
110 | if ((uint)length > m_ValidLength) throw new IndexOutOfRangeException();
111 | }
112 |
113 | public void Consume(int length)
114 | {
115 | CheckLength(length);
116 | InternalConsume(length);
117 | }
118 |
119 | private void InternalConsume(int length)
120 | {
121 | m_ShiftRegister >>= length;
122 | m_ValidLength -= (uint)length;
123 | if (m_ValidLength >= 32) return;
124 |
125 | if (m_End != m_Current)
126 | {
127 | var used = UseDword();
128 | m_ShiftRegister |= (ulong)used << (byte)m_ValidLength;
129 | m_ValidLength += 32;
130 | return;
131 | }
132 | var extra = (m_ExtraBits + 7) >> 3;
133 |
134 | ulong nextShift = 0;
135 | switch (extra)
136 | {
137 | case 4:
138 | nextShift |= (ulong)BufferEnd[3] << 24;
139 | goto case 3;
140 | case 3:
141 | nextShift |= (ulong)BufferEnd[2] << 16;
142 | goto case 2;
143 | case 2:
144 | nextShift |= (ulong)BufferEnd[1] << 8;
145 | goto case 1;
146 | case 1:
147 | nextShift |= BufferEnd[0];
148 | break;
149 | }
150 |
151 | m_ShiftRegister |= nextShift << (int)m_ValidLength;
152 | m_ValidLength += m_ExtraBits;
153 | m_ExtraBits = 0;
154 | }
155 |
156 | public uint Read(int length)
157 | {
158 | CheckLength(length);
159 |
160 | uint ret = 0;
161 | if (length != 0)
162 | {
163 | ulong val = ~1ul;
164 | val <<= (length - 1);
165 | ret = (uint)(m_ShiftRegister & ~val);
166 | }
167 | InternalConsume(length);
168 | return ret;
169 | }
170 |
171 | public int ReadNibble() => (int)Read(4);
172 | public int ReadByte() => (int)Read(8);
173 |
174 | public int ReadNumber(uint baseBitsNumber)
175 | {
176 | if (m_ShiftRegister == 0) throw new InvalidOperationException();
177 | var LowestSetBit = BitScanner.BitScanForward((int)baseBitsNumber);
178 | if ((uint)(31 - LowestSetBit) < 8) throw new IndexOutOfRangeException();
179 | var val = LowestSetBit + 8;
180 | Consume(LowestSetBit + 1);
181 | return (int)Read(val) | (1 << val);
182 | }
183 |
184 | public ulong ReadInt()
185 | {
186 | var NibbleCount = BitScanner.BitScanForward((int)m_ShiftRegister | 0x10000);
187 | if (NibbleCount == 16) throw new InvalidOperationException();
188 | NibbleCount++;
189 | Consume(NibbleCount);
190 | if (NibbleCount < 8) return (ulong)Read(4 * NibbleCount);
191 | ulong ret = (ulong)Read(32);
192 | ret |= ((ulong)Read(4 * (NibbleCount - 8)) << 32);
193 | return ret;
194 | }
195 |
196 | public ulong Read64(int length)
197 | {
198 | if (length <= 32) return (ulong)Read(length);
199 | ulong ret = (ulong)Read(32);
200 | ret |= ((ulong)Read(length - 32) << 32);
201 | return ret;
202 | }
203 |
204 | private int GetCurrentOffsetIntoBuffer()
205 | {
206 | if (m_Current != m_End || m_ExtraBits != 0)
207 | {
208 | return
209 | BufferImpl.Length - ((m_End - m_Current) & ~3)
210 | - (int)((m_ExtraBits + m_BitPadding) >> 3)
211 | - (int)(m_ValidLength >> 3);
212 | }
213 | else
214 | {
215 | return BufferImpl.Length - (int)(m_ValidLength >> 3);
216 | }
217 | }
218 |
219 | public byte[] ReadBuffer()
220 | {
221 | var length64 = ReadInt();
222 | if (length64 > int.MaxValue) throw new InvalidDataException();
223 | var length = (int)length64;
224 | m_ValidLength &= ~7u;
225 | var BufferOffset = GetCurrentOffsetIntoBuffer();
226 |
227 | byte[] ret = new byte[length];
228 | Buffer.BlockCopy(m_Array, m_Offset + BufferOffset, ret, 0, length);
229 | Seek(BufferOffset + length);
230 | return ret;
231 | }
232 |
233 | ///
234 | /// Reads exactly 32 bits.
235 | ///
236 | ///
237 | public uint ReadU32() => (uint)Read(32);
238 | ///
239 | /// Reads exactly 1 bit.
240 | ///
241 | ///
242 | public bool ReadBool() => Read(1) != 0;
243 | }
244 | }
245 |
--------------------------------------------------------------------------------
/MsDelta/BitScanner.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace MsDelta
8 | {
9 | public struct BitScanner
10 | {
11 | private static int[] _table = {
12 | 0, 1, 16, 2, 29, 17, 3, 22,
13 | 30, 20, 18, 11, 13, 4, 7, 23,
14 | 31, 15, 28, 21, 19, 10, 12, 6,
15 | 14, 27, 9, 5, 26, 8, 25, 24,
16 | };
17 |
18 | private static int[] _tableReverse =
19 | {
20 | 0, 1, 28, 2, 29, 14, 24, 3,
21 | 30, 22, 20, 15, 25, 17, 4, 8,
22 | 31, 27, 13, 23, 21, 19, 16, 7,
23 | 26, 12, 18, 6, 11, 5, 10, 9
24 | };
25 |
26 | private const int DeBruijnSequence = 0x6EB14F9;
27 | private const int DeBruijnSequenceReverse = 0x77CB531;
28 |
29 | private static int IsolateLsb(uint x) => (int)(x & -x);
30 | private static int IsolateMsb(uint x)
31 | {
32 | x |= (x >> 1);
33 | x |= (x >> 2);
34 | x |= (x >> 4);
35 | x |= (x >> 8);
36 | x |= (x >> 16);
37 | x = (x >> 1) + 1;
38 | return (int)x;
39 | }
40 | public static int BitScanForward(int x)
41 | => _table[(uint)(IsolateLsb((uint)x) * DeBruijnSequence) >> 27];
42 |
43 | public static int BitScanReverse(int x)
44 | => _tableReverse[(uint)(IsolateMsb((uint)x) * DeBruijnSequenceReverse) >> 27];
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/MsDelta/CliMetadata.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Runtime.InteropServices;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace MsDelta
9 | {
10 | public class CliMetadata
11 | {
12 | public readonly bool m_Valid = false;
13 | public readonly uint
14 | m_StartOffset, m_Size, m_BaseRva, m_StreamsNumber, m_StreamHeadersOffset, m_StringsStreamOffset, m_StringsStreamSize, m_USStreamOffset, m_USStreamSize,
15 | m_BlobStreamOffset, m_BlobStreamSize, m_GuidStreamOffset, m_GuidStreamSize, m_TablesStreamOffset, m_TablesStreamSize;
16 | public readonly bool m_LongStringsStream, m_LongGuidStream, m_LongBlobStream;
17 | public readonly ulong m_ValidTables;
18 |
19 | [StructLayout(LayoutKind.Explicit, Pack = 1, Size = sizeof(uint) * 64)]
20 | private struct RowsNumberBuffer { }
21 |
22 | private RowsNumberBuffer m_Buffer = default;
23 | Span m_RowsNumber => Helpers.AsSpan(ref m_Buffer);
24 |
25 | public CliMetadata(BitReader reader)
26 | {
27 | m_Valid = reader.ReadBool();
28 | if (!m_Valid) return;
29 |
30 | m_StartOffset = reader.ReadU32();
31 | m_Size = reader.ReadU32();
32 | m_BaseRva = reader.ReadU32();
33 | m_StreamsNumber = reader.ReadU32();
34 | m_StreamHeadersOffset = reader.ReadU32();
35 | m_StringsStreamOffset = reader.ReadU32();
36 | m_StringsStreamSize = reader.ReadU32();
37 | m_USStreamOffset = reader.ReadU32();
38 | m_USStreamSize = reader.ReadU32();
39 | m_BlobStreamOffset = reader.ReadU32();
40 | m_BlobStreamSize = reader.ReadU32();
41 | m_GuidStreamOffset = reader.ReadU32();
42 | m_GuidStreamSize = reader.ReadU32();
43 | m_TablesStreamOffset = reader.ReadU32();
44 | m_TablesStreamSize = reader.ReadU32();
45 |
46 | m_LongStringsStream = reader.ReadBool();
47 | m_LongGuidStream = reader.ReadBool();
48 | m_LongBlobStream = reader.ReadBool();
49 |
50 | m_ValidTables = reader.Read64(64);
51 |
52 | var tables = m_ValidTables;
53 |
54 | for (int i = 0; i < 64; i++, tables >>= 1)
55 | {
56 | uint rowNumber = 0;
57 | if ((tables & 1) != 0)
58 | {
59 | rowNumber = reader.ReadU32();
60 | }
61 | m_RowsNumber[i] = rowNumber;
62 | }
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/MsDelta/CryptAlg.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace MsDelta
8 | {
9 | ///
10 | /// Source: WinCrypt.h
11 | ///
12 | public enum CryptAlgClass : uint
13 | {
14 | ALG_CLASS_ANY = (0),
15 | ALG_CLASS_SIGNATURE = (1 << 13),
16 | ALG_CLASS_MSG_ENCRYPT = (2 << 13),
17 | ALG_CLASS_DATA_ENCRYPT = (3 << 13),
18 | ALG_CLASS_HASH = (4 << 13),
19 | ALG_CLASS_KEY_EXCHANGE = (5 << 13),
20 | ALG_CLASS_ALL = (7 << 13)
21 | }
22 |
23 | ///
24 | /// Source: WinCrypt.h
25 | ///
26 | public enum CryptAlgType : uint
27 | {
28 | ALG_TYPE_ANY = (0),
29 | ALG_TYPE_DSS = (1 << 9),
30 | ALG_TYPE_RSA = (2 << 9),
31 | ALG_TYPE_BLOCK = (3 << 9),
32 | ALG_TYPE_STREAM = (4 << 9),
33 | ALG_TYPE_DH = (5 << 9),
34 | ALG_TYPE_SECURECHANNEL = (6 << 9)
35 | }
36 |
37 | ///
38 | /// Source: WinCrypt.h
39 | ///
40 | public enum CryptAlgSID : uint
41 | {
42 | ALG_SID_ANY = (0),
43 | ALG_SID_RSA_ANY = 0,
44 | ALG_SID_RSA_PKCS = 1,
45 | ALG_SID_RSA_MSATWORK = 2,
46 | ALG_SID_RSA_ENTRUST = 3,
47 | ALG_SID_RSA_PGP = 4,
48 | ALG_SID_DSS_ANY = 0,
49 | ALG_SID_DSS_PKCS = 1,
50 | ALG_SID_DSS_DMS = 2,
51 | ALG_SID_ECDSA = 3,
52 | ALG_SID_DES = 1,
53 | ALG_SID_3DES = 3,
54 | ALG_SID_DESX = 4,
55 | ALG_SID_IDEA = 5,
56 | ALG_SID_CAST = 6,
57 | ALG_SID_SAFERSK64 = 7,
58 | ALG_SID_SAFERSK128 = 8,
59 | ALG_SID_3DES_112 = 9,
60 | ALG_SID_CYLINK_MEK = 12,
61 | ALG_SID_RC5 = 13,
62 | ALG_SID_AES_128 = 14,
63 | ALG_SID_AES_192 = 15,
64 | ALG_SID_AES_256 = 16,
65 | ALG_SID_AES = 17,
66 | ALG_SID_SKIPJACK = 10,
67 | ALG_SID_TEK = 11,
68 | ALG_SID_RC2 = 2,
69 | ALG_SID_RC4 = 1,
70 | ALG_SID_SEAL = 2,
71 | ALG_SID_DH_SANDF = 1,
72 | ALG_SID_DH_EPHEM = 2,
73 | ALG_SID_AGREED_KEY_ANY = 3,
74 | ALG_SID_KEA = 4,
75 | ALG_SID_ECDH = 5,
76 | ALG_SID_MD2 = 1,
77 | ALG_SID_MD4 = 2,
78 | ALG_SID_MD5 = 3,
79 | ALG_SID_SHA = 4,
80 | ALG_SID_SHA1 = 4,
81 | ALG_SID_MAC = 5,
82 | ALG_SID_RIPEMD = 6,
83 | ALG_SID_RIPEMD160 = 7,
84 | ALG_SID_SSL3SHAMD5 = 8,
85 | ALG_SID_HMAC = 9,
86 | ALG_SID_TLS1PRF = 10,
87 | ALG_SID_HASH_REPLACE_OWF = 11,
88 | ALG_SID_SHA_256 = 12,
89 | ALG_SID_SHA_384 = 13,
90 | ALG_SID_SHA_512 = 14,
91 | ALG_SID_SSL3_MASTER = 1,
92 | ALG_SID_SCHANNEL_MASTER_HASH = 2,
93 | ALG_SID_SCHANNEL_MAC_KEY = 3,
94 | ALG_SID_PCT1_MASTER = 4,
95 | ALG_SID_SSL2_MASTER = 5,
96 | ALG_SID_TLS1_MASTER = 6,
97 | ALG_SID_SCHANNEL_ENC_KEY = 7,
98 | ALG_SID_ECMQV = 1
99 | }
100 |
101 | ///
102 | /// Source: WinCrypt.h
103 | ///
104 | public enum CryptAlg : uint
105 | {
106 | CALG_MD2 = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_MD2),
107 | CALG_MD4 = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_MD4),
108 | CALG_MD5 = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_MD5),
109 | CALG_SHA = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_SHA),
110 | CALG_SHA1 = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_SHA1),
111 | CALG_MAC = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_MAC),
112 | CALG_RSA_SIGN = (CryptAlgClass.ALG_CLASS_SIGNATURE | CryptAlgType.ALG_TYPE_RSA | CryptAlgSID.ALG_SID_RSA_ANY),
113 | CALG_DSS_SIGN = (CryptAlgClass.ALG_CLASS_SIGNATURE | CryptAlgType.ALG_TYPE_DSS | CryptAlgSID.ALG_SID_DSS_ANY),
114 | CALG_NO_SIGN = (CryptAlgClass.ALG_CLASS_SIGNATURE | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_ANY),
115 | CALG_RSA_KEYX = (CryptAlgClass.ALG_CLASS_KEY_EXCHANGE | CryptAlgType.ALG_TYPE_RSA | CryptAlgSID.ALG_SID_RSA_ANY),
116 | CALG_DES = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_DES),
117 | CALG_3DES_112 = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_3DES_112),
118 | CALG_3DES = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_3DES),
119 | CALG_DESX = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_DESX),
120 | CALG_RC2 = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_RC2),
121 | CALG_RC4 = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_STREAM | CryptAlgSID.ALG_SID_RC4),
122 | CALG_SEAL = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_STREAM | CryptAlgSID.ALG_SID_SEAL),
123 | CALG_DH_SF = (CryptAlgClass.ALG_CLASS_KEY_EXCHANGE | CryptAlgType.ALG_TYPE_DH | CryptAlgSID.ALG_SID_DH_SANDF),
124 | CALG_DH_EPHEM = (CryptAlgClass.ALG_CLASS_KEY_EXCHANGE | CryptAlgType.ALG_TYPE_DH | CryptAlgSID.ALG_SID_DH_EPHEM),
125 | CALG_AGREEDKEY_ANY = (CryptAlgClass.ALG_CLASS_KEY_EXCHANGE | CryptAlgType.ALG_TYPE_DH | CryptAlgSID.ALG_SID_AGREED_KEY_ANY),
126 | CALG_KEA_KEYX = (CryptAlgClass.ALG_CLASS_KEY_EXCHANGE | CryptAlgType.ALG_TYPE_DH | CryptAlgSID.ALG_SID_KEA),
127 | CALG_HUGHES_MD5 = (CryptAlgClass.ALG_CLASS_KEY_EXCHANGE | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_MD5),
128 | CALG_SKIPJACK = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_SKIPJACK),
129 | CALG_TEK = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_TEK),
130 | CALG_CYLINK_MEK = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_CYLINK_MEK),
131 | CALG_SSL3_SHAMD5 = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_SSL3SHAMD5),
132 | CALG_SSL3_MASTER = (CryptAlgClass.ALG_CLASS_MSG_ENCRYPT | CryptAlgType.ALG_TYPE_SECURECHANNEL | CryptAlgSID.ALG_SID_SSL3_MASTER),
133 | CALG_SCHANNEL_MASTER_HASH = (CryptAlgClass.ALG_CLASS_MSG_ENCRYPT | CryptAlgType.ALG_TYPE_SECURECHANNEL | CryptAlgSID.ALG_SID_SCHANNEL_MASTER_HASH),
134 | CALG_SCHANNEL_MAC_KEY = (CryptAlgClass.ALG_CLASS_MSG_ENCRYPT | CryptAlgType.ALG_TYPE_SECURECHANNEL | CryptAlgSID.ALG_SID_SCHANNEL_MAC_KEY),
135 | CALG_SCHANNEL_ENC_KEY = (CryptAlgClass.ALG_CLASS_MSG_ENCRYPT | CryptAlgType.ALG_TYPE_SECURECHANNEL | CryptAlgSID.ALG_SID_SCHANNEL_ENC_KEY),
136 | CALG_PCT1_MASTER = (CryptAlgClass.ALG_CLASS_MSG_ENCRYPT | CryptAlgType.ALG_TYPE_SECURECHANNEL | CryptAlgSID.ALG_SID_PCT1_MASTER),
137 | CALG_SSL2_MASTER = (CryptAlgClass.ALG_CLASS_MSG_ENCRYPT | CryptAlgType.ALG_TYPE_SECURECHANNEL | CryptAlgSID.ALG_SID_SSL2_MASTER),
138 | CALG_TLS1_MASTER = (CryptAlgClass.ALG_CLASS_MSG_ENCRYPT | CryptAlgType.ALG_TYPE_SECURECHANNEL | CryptAlgSID.ALG_SID_TLS1_MASTER),
139 | CALG_RC5 = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_RC5),
140 | CALG_HMAC = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_HMAC),
141 | CALG_TLS1PRF = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_TLS1PRF),
142 | CALG_HASH_REPLACE_OWF = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_HASH_REPLACE_OWF),
143 | CALG_AES_128 = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_AES_128),
144 | CALG_AES_192 = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_AES_192),
145 | CALG_AES_256 = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_AES_256),
146 | CALG_AES = (CryptAlgClass.ALG_CLASS_DATA_ENCRYPT | CryptAlgType.ALG_TYPE_BLOCK | CryptAlgSID.ALG_SID_AES),
147 | CALG_SHA_256 = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_SHA_256),
148 | CALG_SHA_384 = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_SHA_384),
149 | CALG_SHA_512 = (CryptAlgClass.ALG_CLASS_HASH | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_SHA_512),
150 | CALG_ECDH = (CryptAlgClass.ALG_CLASS_KEY_EXCHANGE | CryptAlgType.ALG_TYPE_DH | CryptAlgSID.ALG_SID_ECDH),
151 | CALG_ECMQV = (CryptAlgClass.ALG_CLASS_KEY_EXCHANGE | CryptAlgType.ALG_TYPE_ANY | CryptAlgSID.ALG_SID_ECMQV),
152 | CALG_ECDSA = (CryptAlgClass.ALG_CLASS_SIGNATURE | CryptAlgType.ALG_TYPE_DSS | CryptAlgSID.ALG_SID_ECDSA)
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/MsDelta/DeltaFile.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace MsDelta
10 | {
11 | public class DeltaFile
12 | {
13 | public readonly DateTime FileTime;
14 | public readonly ulong Version;
15 | public readonly FileTypeCode Code;
16 | public readonly DeltaFlags Flags;
17 | public readonly ulong TargetSize;
18 | public readonly CryptAlg HashAlgorithm;
19 | public readonly byte[] Hash;
20 |
21 | // Don't really care about this? For PA31 v1 it's 0x88.
22 | public readonly ulong HeaderInfoSize = 0x50; // sizeof(DELTA_HEADER_INFO)
23 | // Must be 1 for PA31, if 0 then UpdateCompression!DeltaPatch::CreatePatch creates a PA30.
24 | public readonly ulong IsPa31 = 0;
25 | // Observed to be 0 for PA31. Checked (on read and write) to make sure it's <= 1.
26 | public readonly ulong DeltaClientMinVersion = 0;
27 | // Not sure from just static analysis. Hash of source file maybe? The same hash algorithm as the other hash is used.
28 | public readonly byte[] AdditionalHash = new byte[0];
29 |
30 | public readonly PreProcess FileTypeHeader;
31 |
32 | private enum DeltaFileFormat
33 | {
34 | PA30,
35 | PA31
36 | }
37 |
38 | private static bool HasPA30SignatureAtOffset(byte[] bytes, int offset)
39 | {
40 | return bytes[offset + 0] == (byte)'P' && bytes[offset + 1] == (byte)'A' && bytes[offset + 2] == (byte)'3' && bytes[offset + 3] == (byte)'0';
41 | }
42 |
43 | private static bool HasPA31SignatureAtOffset(byte[] bytes, int offset)
44 | {
45 | return bytes[offset + 0] == (byte)'P' && bytes[offset + 1] == (byte)'A' && bytes[offset + 2] == (byte)'3' && bytes[offset + 3] == (byte)'1';
46 | }
47 |
48 | private static int FindOffsetOfDelta(byte[] bytes, int startOffset = 0)
49 | {
50 | while (!HasPA30SignatureAtOffset(bytes, startOffset) && !HasPA31SignatureAtOffset(bytes, startOffset))
51 | {
52 | startOffset += 4;
53 | if (startOffset >= bytes.Length) throw new InvalidDataException();
54 | }
55 | return startOffset;
56 | }
57 |
58 | private static void ParseHeaderPA30(BitReader reader,
59 | out ulong version,
60 | out FileTypeCode code,
61 | out DeltaFlags flags,
62 | out ulong targetSize,
63 | out CryptAlg hashAlgorithm,
64 | out byte[] hash)
65 | {
66 | version = reader.ReadInt();
67 | code = (FileTypeCode)reader.ReadInt();
68 | flags = (DeltaFlags)reader.ReadInt();
69 | targetSize = reader.ReadInt();
70 | hashAlgorithm = (CryptAlg)reader.ReadInt();
71 | hash = reader.ReadBuffer();
72 | }
73 |
74 | private static void ParseHeaderPA31(BitReader reader,
75 | out ulong version,
76 | out FileTypeCode code,
77 | out DeltaFlags flags,
78 | out ulong targetSize,
79 | out CryptAlg hashAlgorithm,
80 | out byte[] hash,
81 | out ulong headerInfoSize,
82 | out ulong isPa31,
83 | out ulong deltaClientMinVersion,
84 | out byte[] additionalHash)
85 | {
86 | ParseHeaderPA30(reader,
87 | out version,
88 | out code,
89 | out flags,
90 | out targetSize,
91 | out hashAlgorithm,
92 | out hash);
93 |
94 | headerInfoSize = reader.ReadInt();
95 | isPa31 = reader.ReadInt();
96 | deltaClientMinVersion = reader.ReadInt();
97 | additionalHash = reader.ReadBuffer();
98 | }
99 |
100 | public DeltaFile(byte[] bytes) : this(bytes, FindOffsetOfDelta(bytes))
101 | {
102 |
103 | }
104 |
105 | public DeltaFile(byte[] bytes, int startOffset)
106 | {
107 | DeltaFileFormat fileFormat;
108 | if (HasPA30SignatureAtOffset(bytes, startOffset))
109 | {
110 | fileFormat = DeltaFileFormat.PA30;
111 | }
112 | else if (HasPA31SignatureAtOffset(bytes, startOffset))
113 | {
114 | fileFormat = DeltaFileFormat.PA31;
115 | }
116 | else
117 | {
118 | throw new InvalidDataException();
119 | }
120 |
121 | int offset = startOffset + 4;
122 | long ft64 = 0;
123 | using (var br = new BinaryReader(new MemoryStream(bytes, offset, bytes.Length - offset)))
124 | {
125 | ft64 = br.ReadInt64();
126 | }
127 | offset += sizeof(ulong);
128 | FileTime = DateTime.FromFileTimeUtc(ft64);
129 |
130 | var reader = new BitReader(bytes, offset);
131 |
132 | // header
133 | switch (fileFormat)
134 | {
135 | case DeltaFileFormat.PA30:
136 | ParseHeaderPA30(reader,
137 | out Version,
138 | out Code,
139 | out Flags,
140 | out TargetSize,
141 | out HashAlgorithm,
142 | out Hash);
143 | break;
144 |
145 | case DeltaFileFormat.PA31:
146 | var headerBytes = reader.ReadBuffer();
147 | var headerReader = new BitReader(headerBytes);
148 | ParseHeaderPA31(headerReader,
149 | out Version,
150 | out Code,
151 | out Flags,
152 | out TargetSize,
153 | out HashAlgorithm,
154 | out Hash,
155 | out HeaderInfoSize,
156 | out IsPa31,
157 | out DeltaClientMinVersion,
158 | out AdditionalHash);
159 | Debug.Assert(headerReader.AtEnd);
160 | break;
161 | }
162 |
163 | // buffers
164 | var preProcessBuffer = reader.ReadBuffer();
165 | var patchBuffer = reader.ReadBuffer();
166 | Debug.Assert(reader.AtEnd);
167 |
168 | FileTypeHeader = new PreProcess(Code, preProcessBuffer);
169 | }
170 | }
171 | }
172 |
--------------------------------------------------------------------------------
/MsDelta/DeltaFlags.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace MsDelta
8 | {
9 | [Flags]
10 | public enum DeltaFlags : ulong
11 | {
12 | /** No flags. */
13 | DELTA_FLAG_NONE = (0x00000000),
14 | /** Allow application of legacy PA19 deltas by mspatcha.dll. */
15 | DELTA_APPLY_FLAG_ALLOW_PA19 = (0x00000001),
16 | /** Transform E8 pieces (relative calls in x86), of target file . */
17 | DELTA_FLAG_E8 = (0x00000001), /* flags[ 0 ] */
18 | /** Mark non-executable parts of source PE. */
19 | DELTA_FLAG_MARK = (0x00000002), /* flags[ 1 ] */
20 | /** Transform imports of source PE. */
21 | DELTA_FLAG_IMPORTS = (0x00000004), /* flags[ 2 ] */
22 | /** Transform exports of source PE. */
23 | DELTA_FLAG_EXPORTS = (0x00000008), /* flags[ 3 ] */
24 | /** Transform resources of source PE. */
25 | DELTA_FLAG_RESOURCES = (0x00000010), /* flags[ 4 ] */
26 | /** Transform relocations of source PE. */
27 | DELTA_FLAG_RELOCS = (0x00000020), /* flags[ 5 ] */
28 | /** Smash lock prefixes of source PE. */
29 | DELTA_FLAG_I386_SMASHLOCK = (0x00000040), /* flags[ 6 ] */
30 | /** Transform relative jumps of source I386 (x86), PE. */
31 | DELTA_FLAG_I386_JMPS = (0x00000080), /* flags[ 7 ] */
32 | /** Transform relative calls of source I386 (x86), PE. */
33 | DELTA_FLAG_I386_CALLS = (0x00000100), /* flags[ 8 ] */
34 | /** Transform instructions of source AMD64 (x86-64), PE. */
35 | DELTA_FLAG_AMD64_DISASM = (0x00000200), /* flags[ 9 ] */
36 | /** Transform pdata of source AMD64 (x86-64), PE. */
37 | DELTA_FLAG_AMD64_PDATA = (0x00000400), /* flags[ 10 ] */
38 | /** Transform intstructions of source IA64 (Itanium), PE. */
39 | DELTA_FLAG_IA64_DISASM = (0x00000800), /* flags[ 11 ] */
40 | /** Transform pdata of source IA64 (Itanium), PE. */
41 | DELTA_FLAG_IA64_PDATA = (0x00001000), /* flags[ 12 ] */
42 | /** Unbind source PE. */
43 | DELTA_FLAG_UNBIND = (0x00002000), /* flags[ 13 ] */
44 | /** Transform CLI instructions of source PE. */
45 | DELTA_FLAG_CLI_DISASM = (0x00004000), /* flags[ 14 ] */
46 | /** Transform CLI Metadata of source PE. */
47 | DELTA_FLAG_CLI_METADATA = (0x00008000), /* flags[ 15 ] */
48 | /** Transform headers of source PE. */
49 | DELTA_FLAG_HEADERS = (0x00010000), /* flags[ 16 ] */
50 | /** Allow source or target file or buffer to exceed its default size limit. */
51 | DELTA_FLAG_IGNORE_FILE_SIZE_LIMIT = (0x00020000), /* flags[ 17 ] */
52 | /** Allow options buffer or file to exceeed its default size limit. */
53 | DELTA_FLAG_IGNORE_OPTIONS_SIZE_LIMIT = (0x00040000), /* flags[ 18 ] */
54 | /** Transform instructions of source ARM PE. */
55 | DELTA_FLAG_ARM_DISASM = (0x00080000), /* flags[ 19 ] */
56 | /** Transform pdata of source ARM PE. */
57 | DELTA_FLAG_ARM_PDATA = (0x00100000), /* flags[ 20 ] */
58 | /** Transform CLI4 Metadata of source PE. */
59 | DELTA_FLAG_CLI4_METADATA = (0x00200000), /* flags[ 21 ] */
60 | /** Transform CLI4 instructions of source PE. */
61 | DELTA_FLAG_CLI4_DISASM = (0x00400000), /* flags[ 22 ] */
62 | /** Transform instructions of source ARM PE. */
63 | DELTA_FLAG_ARM64_DISASM = (0x00800000), /* flags[ 23 ] */
64 | /** Transform pdata of source ARM PE. */
65 | DELTA_FLAG_ARM64_PDATA = (0x01000000), /* flags[ 24 ] */
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/MsDelta/FileTypeCode.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace MsDelta
8 | {
9 | internal enum FileTypeCodeBit
10 | {
11 | Raw,
12 | I386,
13 | IA64,
14 | AMD64,
15 | CLI4_I386,
16 | CLI4_AMD64,
17 | CLI4_ARM,
18 | CLI4_ARM64
19 | }
20 | [Flags]
21 | public enum FileTypeCode : ulong
22 | {
23 | Raw = (1 << FileTypeCodeBit.Raw),
24 | I386 = (1 << FileTypeCodeBit.I386),
25 | IA64 = (1 << FileTypeCodeBit.IA64),
26 | AMD64 = (1 << FileTypeCodeBit.AMD64),
27 | CLI4_I386 = (1 << FileTypeCodeBit.CLI4_I386),
28 | CLI4_AMD64 = (1 << FileTypeCodeBit.CLI4_AMD64),
29 | CLI4_ARM = (1 << FileTypeCodeBit.CLI4_ARM),
30 | CLI4_ARM64 = (1 << FileTypeCodeBit.CLI4_ARM64)
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/MsDelta/Helpers.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Runtime.CompilerServices;
7 |
8 | namespace MsDelta
9 | {
10 | internal static class Helpers
11 | {
12 | internal static void ArraySet(this T[] arr, T value, uint offset, uint length)
13 | where T : struct, IEquatable
14 | {
15 | if (offset >= arr.LongLength) throw new IndexOutOfRangeException();
16 | length = (uint) Math.Min(length, arr.LongLength - offset);
17 | if (value.Equals(default(T)) && length > 76)
18 | Array.Clear(arr, (int)offset, (int)length);
19 | else
20 | for (uint i = 0; i < length; i++) arr[offset + i] = value;
21 |
22 | }
23 |
24 | internal static void ArraySet(this Span arr, T value, uint offset, uint length)
25 | {
26 | arr.Slice((int)offset, (int)length).Fill(value);
27 | }
28 |
29 | internal static Span AsSpan(ref TBuffer ptr)
30 | where T : unmanaged
31 | where TBuffer : unmanaged
32 | {
33 | unsafe
34 | {
35 | return new Span(Unsafe.AsPointer(ref ptr), sizeof(TBuffer) / sizeof(T));
36 | }
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/MsDelta/IntFormat.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Runtime.InteropServices;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace MsDelta
10 | {
11 | public class IntFormat
12 | {
13 | private StaticHuffman.Codes m_Codes;
14 | private StaticHuffman.DecoderTable m_DecoderTable;
15 |
16 | [StructLayout(LayoutKind.Explicit, Pack = 1, Size = sizeof(uint) * 252)]
17 | private struct WeightsBuffer { }
18 |
19 | private WeightsBuffer m_Buffer = default;
20 | Span m_Weights => Helpers.AsSpan(ref m_Buffer);
21 |
22 | public IntFormat()
23 | {
24 | m_Codes = new StaticHuffman.Codes(0xFC, 0x10, false);
25 | m_DecoderTable = new StaticHuffman.DecoderTable(m_Codes);
26 | m_Weights.Fill(0);
27 | }
28 |
29 | public IntFormat(BitReader bitReader) : this()
30 | {
31 | var length1 = bitReader.ReadByte();
32 | if (length1 >= 0x7F) throw new InvalidDataException();
33 | var length2 = bitReader.ReadByte();
34 | if (length2 >= 0x7F) throw new InvalidDataException();
35 | var length3 = bitReader.ReadByte();
36 | if (0xFC - length2 - length1 < length3) throw new InvalidDataException();
37 |
38 | Span lengths = stackalloc byte[252];
39 |
40 | int item = 0;
41 | for (int i = 0; length1 > i; i++)
42 | {
43 | item = bitReader.ReadNibble() + 1;
44 | if (item > 0x10) throw new InvalidDataException();
45 | lengths[i] = (byte)item;
46 | }
47 |
48 | for (int i = 0; length2 > i; i++)
49 | {
50 | item = bitReader.ReadNibble() + 1;
51 | if (item > 0x10) throw new InvalidDataException();
52 | lengths[i + 0x7E] = (byte)item;
53 | }
54 |
55 | var entry = bitReader.ReadNibble() + 1;
56 | if (entry > 0x10) throw new InvalidDataException();
57 | var entry8 = (byte)entry;
58 | for (int i = length1; i < 0x7E; i++)
59 | {
60 | var isZero = length3 == 0;
61 | length3--;
62 | if (isZero)
63 | {
64 | entry8--;
65 | length3 = 0xFC - i - length2;
66 | }
67 | lengths[i] = entry8;
68 | }
69 | for (int i = length2; i < 0x7E; i++)
70 | {
71 | var isZero = length3 == 0;
72 | length3--;
73 | if (isZero)
74 | {
75 | entry8--;
76 | length3 = 0x7E - i;
77 | }
78 | lengths[i + 0x7E] = entry8;
79 | }
80 |
81 | m_Codes.SetLengths(0xFC, lengths);
82 | m_DecoderTable = new StaticHuffman.DecoderTable(m_Codes);
83 | m_Weights.Fill(0);
84 | }
85 |
86 | public ulong ReadNumber(BitReader bitReader)
87 | {
88 | var decoded = m_DecoderTable.ReadDecode(bitReader, m_Codes);
89 |
90 | var overSize = decoded >= 0x7E;
91 |
92 | if (overSize) decoded -= 0x7E;
93 |
94 | ulong decoded64 = decoded;
95 |
96 | if (decoded >= 4)
97 | {
98 | var bitLen = (decoded >> 1) - 1;
99 | var bitZero = (long)(decoded & 1) + 2;
100 | var nextBits = bitReader.Read64((int)bitLen);
101 | bitZero <<= (int)bitLen;
102 | decoded64 = (ulong)((long)nextBits | bitZero);
103 | }
104 |
105 | if (overSize) decoded64 = ~decoded64;
106 | return decoded64;
107 | }
108 |
109 |
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/MsDelta/PreProcess.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace MsDelta
9 | {
10 | public class PreProcess
11 | {
12 | public ulong ImageBase { get; }
13 | public uint GlobalPointer { get; }
14 | public uint TimeStamp { get; }
15 | public Dictionary RiftTable { get; }
16 | public CliMetadata CliMetadata { get; }
17 | public PreProcess(FileTypeCode code, byte[] buffer) : this(code, new BitReader(buffer)) { }
18 |
19 | public PreProcess(FileTypeCode code, BitReader buffer)
20 | {
21 | //bool IsNet40 = false;
22 | switch (code)
23 | {
24 | case FileTypeCode.Raw:
25 | // nothing
26 | return;
27 | case FileTypeCode.I386:
28 | case FileTypeCode.IA64:
29 | case FileTypeCode.AMD64:
30 | break;
31 | default:
32 | //IsNet40 = true;
33 | break;
34 | }
35 |
36 | ImageBase = buffer.Read64(64);
37 | GlobalPointer = buffer.ReadU32();
38 | TimeStamp = buffer.ReadU32();
39 | var riftTable = new RiftTable(buffer);
40 | if (riftTable.Valid)
41 | {
42 | RiftTable = new Dictionary();
43 | foreach (var pair in riftTable.Dictionary.KeyValuePairs)
44 | {
45 | RiftTable.Add(pair.Key, pair.Value);
46 | }
47 | }
48 | CliMetadata = new CliMetadata(buffer);
49 | if (!CliMetadata.m_Valid) CliMetadata = null;
50 | //Debug.Assert(buffer.AtEnd);
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/MsDelta/RiftTable.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using Wintellect.PowerCollections;
8 |
9 | namespace MsDelta
10 | {
11 | public class RiftTable
12 | {
13 | public bool Valid { get; }
14 | public OrderedMultiDictionary Dictionary { get; } = new OrderedMultiDictionary(true);
15 |
16 | public RiftTable(BitReader reader)
17 | {
18 | Valid = reader.ReadBool();
19 | if (!Valid) return;
20 |
21 | var leftFormat = new IntFormat(reader);
22 | var rightFormat = new IntFormat(reader);
23 |
24 | var count = reader.ReadInt();
25 | if (count > 0x0FFF_FFFF) throw new InvalidDataException();
26 |
27 | ulong key = 0;
28 | ulong value = 0;
29 | for (; count != 0; count--)
30 | {
31 | key += leftFormat.ReadNumber(reader);
32 | value += rightFormat.ReadNumber(reader);
33 | Dictionary.Add(key, key + value);
34 | }
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/MsDelta/StaticHuffman/Codes.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace MsDelta.StaticHuffman
9 | {
10 | internal class Codes
11 | {
12 | internal readonly uint m_IndexNumber;
13 | internal readonly byte[] m_Lengths;
14 | internal readonly uint[] m_Codes;
15 | internal readonly byte m_MaxLength;
16 |
17 | private static void CheckParams(uint IndexNumber, byte MaxLength)
18 | {
19 | if (IndexNumber == 0 || IndexNumber > 0x10000 || MaxLength == 0 || MaxLength > 0x1F) throw new IndexOutOfRangeException();
20 | }
21 |
22 | internal static void ResetLengths(uint IndexNumber, byte MaxLength, Span Lengths, bool SkipUnused)
23 | {
24 | CheckParams(IndexNumber, MaxLength);
25 |
26 | if (SkipUnused)
27 | {
28 | if (IndexNumber == 0) return;
29 | Lengths.ArraySet(0, 0, IndexNumber);
30 | return;
31 | }
32 |
33 | if (IndexNumber >= 3)
34 | {
35 | byte Value = (byte)(BitScanner.BitScanReverse((int)IndexNumber - 1) + 1);
36 | byte Value2 = Value;
37 | Value2--;
38 | if (Value >= MaxLength) throw new IndexOutOfRangeException();
39 | uint Offset = 0;
40 | if ((1 << Value) != IndexNumber)
41 | {
42 | Offset = (uint)(1 << Value) - IndexNumber;
43 | Lengths.ArraySet(Value2, 0, Offset);
44 | }
45 | if (IndexNumber > Offset)
46 | Lengths.ArraySet(Value, Offset, IndexNumber - Offset);
47 | return;
48 | }
49 |
50 | if (MaxLength == 0) throw new IndexOutOfRangeException();
51 |
52 | if (IndexNumber != 0) Lengths.ArraySet(1, 0, IndexNumber);
53 | }
54 |
55 | internal Codes(uint IndexNumber, byte MaxLength, bool SkipUnused)
56 | {
57 | CheckParams(IndexNumber, MaxLength);
58 | m_IndexNumber = IndexNumber;
59 | m_MaxLength = MaxLength;
60 | m_Lengths = new byte[IndexNumber];
61 | m_Codes = new uint[IndexNumber];
62 | ResetLengths(IndexNumber, MaxLength, m_Lengths, SkipUnused);
63 | CalculateCodes();
64 | }
65 |
66 | private uint[] InitBlock()
67 | {
68 | var totalLength = m_MaxLength + 1;
69 | var lengthsCounts = new uint[totalLength];
70 | for (uint i = 0; i < m_IndexNumber; i++)
71 | {
72 | Debug.Assert(m_Lengths[i] <= m_MaxLength);
73 | lengthsCounts[m_Lengths[i]]++;
74 | }
75 |
76 | // Check lengthsCounts
77 | for (uint bitVal = 2, i = 1; i <= m_MaxLength; bitVal = 2 * (bitVal - lengthsCounts[i]), i++)
78 | {
79 | if (lengthsCounts[i] > bitVal) throw new InvalidOperationException();
80 | }
81 |
82 | var Block = new uint[totalLength];
83 | uint count = 0;
84 | for (uint i = 0; i <= m_MaxLength; i++)
85 | {
86 | Block[totalLength - i - 1] = count;
87 | count = (count + lengthsCounts[totalLength - i - 1]) >> 1;
88 | }
89 |
90 | return Block;
91 | }
92 |
93 | private void CalculateCodes()
94 | {
95 | var Block = InitBlock();
96 |
97 | for (uint i = 0; i < m_IndexNumber; i++)
98 | {
99 | var length = m_Lengths[i];
100 | if (length == 0)
101 | {
102 | m_Codes[i] = 0;
103 | continue;
104 | }
105 |
106 | var oldBlock = Block[length];
107 | Block[length]++;
108 | uint newCode = 0;
109 | for (uint l = 0; l < length; l++)
110 | {
111 | byte isOdd = (byte)(oldBlock & 1);
112 | oldBlock >>= 1;
113 | newCode <<= 1;
114 | newCode |= isOdd;
115 | }
116 | m_Codes[i] = newCode;
117 | }
118 | }
119 |
120 | internal void SetLengths(uint indexNumber, ReadOnlySpan lengths)
121 | {
122 | if (m_IndexNumber == 0 || indexNumber != m_IndexNumber) throw new IndexOutOfRangeException();
123 | for (uint i = 0; i < m_IndexNumber; i++) m_Lengths[i] = lengths[(int)i];
124 | CalculateCodes();
125 | }
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/MsDelta/StaticHuffman/DecoderTable.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Runtime.InteropServices;
5 | using System.Runtime.CompilerServices;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace MsDelta.StaticHuffman
10 | {
11 | internal class DecoderTable
12 | {
13 | struct PrefixEntry
14 | {
15 | internal const int SIZE = sizeof(uint) * 2;
16 | internal uint indexMask;
17 | internal uint decodeBase;
18 | }
19 |
20 | [StructLayout(LayoutKind.Explicit, Pack = 1, Size = PrefixEntry.SIZE * 32)]
21 | private struct PrefixEntryBuffer { }
22 |
23 | private PrefixEntryBuffer m_Buffer = default;
24 | Span m_PrefixTable => Helpers.AsSpan(ref m_Buffer);
25 |
26 | ushort[] m_DecodeEntry;
27 |
28 | internal DecoderTable(Codes codes)
29 | {
30 | if (codes == null) throw new ArgumentNullException(nameof(codes));
31 |
32 | Span indexWidth = stackalloc uint[32];
33 | indexWidth.Fill(0);
34 |
35 | uint PrefixTableLength = 0;
36 | var expectedIndex = 0u;
37 | if (codes.m_IndexNumber != 0)
38 | {
39 | expectedIndex = codes.m_IndexNumber;
40 | for (uint i = 0; i < codes.m_IndexNumber; i++)
41 | {
42 | if (codes.m_Lengths[i] == 0) continue;
43 | if (codes.m_Codes[i] == 0)
44 | {
45 | if (codes.m_IndexNumber != expectedIndex) throw new IndexOutOfRangeException();
46 | expectedIndex = i;
47 | } else
48 | {
49 | var LowestSetBit = (byte) BitScanner.BitScanForward((int)codes.m_Codes[i]);
50 | if (codes.m_Lengths[i] <= LowestSetBit) throw new IndexOutOfRangeException();
51 | if (LowestSetBit >= 32) throw new IndexOutOfRangeException();
52 | var Width = (uint)codes.m_Lengths[i] - LowestSetBit - 1;
53 | PrefixTableLength = Math.Max(PrefixTableLength, LowestSetBit);
54 | indexWidth[LowestSetBit] = Math.Max(indexWidth[LowestSetBit], Width);
55 | }
56 | }
57 | }
58 |
59 | uint EntryLength = 1;
60 | for (int i = 0; i <= PrefixTableLength; i++) EntryLength += 1u << (byte)indexWidth[i];
61 |
62 | m_DecodeEntry = new ushort[EntryLength];
63 |
64 | uint decodeBase = 0u;
65 | for (int i = 0; i <= (int)PrefixTableLength; i++)
66 | {
67 | var width = (byte)indexWidth[i];
68 | m_PrefixTable[i].decodeBase = decodeBase;
69 | decodeBase += 1u << width;
70 | m_PrefixTable[i].indexMask = (1u << width) - 1u;
71 | }
72 |
73 | for (int i = (int)PrefixTableLength + 1; i < 32; i++)
74 | {
75 | m_PrefixTable[i].decodeBase = decodeBase;
76 | m_PrefixTable[i].indexMask = 0;
77 | }
78 |
79 | decodeBase++;
80 |
81 | m_DecodeEntry.ArraySet(0, 0, decodeBase);
82 |
83 | if (codes.m_IndexNumber != 0)
84 | {
85 | for (uint i = 0; i < codes.m_IndexNumber; i++)
86 | {
87 | var code = codes.m_Codes[i];
88 | if (code == 0) continue;
89 | var LowestSetBit = (byte)BitScanner.BitScanForward((int)code);
90 | var decodeOff = m_PrefixTable[LowestSetBit].decodeBase + (code >> (LowestSetBit + 1));
91 | var bitOff = codes.m_Lengths[i] - LowestSetBit - 1;
92 | var incOff = 1u << bitOff;
93 | var len = 1u << (byte)(indexWidth[LowestSetBit] - bitOff);
94 |
95 | for (uint itLen = 0; itLen < len; itLen++, decodeOff += incOff)
96 | {
97 | m_DecodeEntry[decodeOff] = (ushort)i;
98 | }
99 | }
100 | }
101 | if (codes.m_IndexNumber > expectedIndex) m_DecodeEntry[decodeBase - 1] = (ushort)expectedIndex;
102 | }
103 |
104 | internal uint Decode(uint code)
105 | {
106 | code |= 0x80000000;
107 | var LowestSetBit = (byte)BitScanner.BitScanForward((int)code);
108 | ref var Prefix = ref m_PrefixTable[LowestSetBit];
109 | return m_DecodeEntry[Prefix.decodeBase + (Prefix.indexMask & (code >> (LowestSetBit + 1)))];
110 | }
111 |
112 | internal uint ReadDecode(BitReader reader, Codes codes)
113 | {
114 | var ret = Decode((uint)reader.m_ShiftRegister);
115 | reader.Consume(codes.m_Lengths[ret]);
116 | return ret;
117 | }
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.IO;
7 | using MsDelta;
8 | using System.Net.Http;
9 | using System.Collections.Concurrent;
10 |
11 | namespace DeltaDownloader
12 | {
13 | internal class Program
14 | {
15 | private const uint PAGE_SIZE = 0x1000;
16 | private static readonly HttpClientHandler s_clientHandler = new HttpClientHandler() { AllowAutoRedirect = false };
17 | private static readonly HttpClient s_client = new HttpClient(s_clientHandler);
18 | private static readonly ConcurrentBag s_urls = new ConcurrentBag();
19 | private static readonly ConcurrentBag<(string, string, DeltaFile)> s_files = new ConcurrentBag<(string, string, DeltaFile)>();
20 | private static async Task SymbolUrlValid(string url)
21 | {
22 | var request = new HttpRequestMessage(HttpMethod.Head, url);
23 | while (true)
24 | {
25 | var response = await s_client.SendAsync(request);
26 | var intStatus = (int)response.StatusCode;
27 | if (intStatus >= 500 && intStatus < 600)
28 | {
29 | await Task.Delay(100);
30 | continue;
31 | }
32 | return response.StatusCode == System.Net.HttpStatusCode.Found;
33 | }
34 | }
35 |
36 | private static ulong GetMappedSize(ulong size)
37 | {
38 | const ulong PAGE_MASK = (PAGE_SIZE - 1);
39 | var page = size & ~PAGE_MASK;
40 | if (page == size) return page;
41 | return page + PAGE_SIZE;
42 | }
43 |
44 | private static string CreateUrl(string filename, uint TimeDateStamp, uint SizeOfImage)
45 | {
46 | return string.Format("https://msdl.microsoft.com/download/symbols/{0}/{1:x8}{2:x}/{0}", filename, TimeDateStamp, SizeOfImage);
47 | }
48 |
49 | private static void ParseDelta(string path, string dir)
50 | {
51 | try
52 | {
53 | var bytes = File.ReadAllBytes(path);
54 | var delta = new DeltaFile(bytes);
55 | var filename = Path.GetFileName(path);
56 | // https://msdl.microsoft.com/download/symbols/cdboot_noprompt.efi/{0:X8}{1:X}/cdboot_noprompt.efi, TimeDateStamp, SizeOfImage
57 | if ((delta.Code & FileTypeCode.Raw) != 0) return;
58 | if (delta.FileTypeHeader?.RiftTable == null) return;
59 | s_files.Add((dir, filename, delta));
60 | } catch { }
61 | }
62 |
63 | private static async Task GetUrlFromDelta((string, string, DeltaFile) deltaPair)
64 | {
65 | var filename = deltaPair.Item2;
66 | var delta = deltaPair.Item3;
67 |
68 | var timeDateStamp = delta.FileTypeHeader.TimeStamp;
69 | // We use the rift table (VirtualAddress,PointerToRawData pairs for each section) and the target file size to calculate the SizeOfImage.
70 | var lastSection = delta.FileTypeHeader.RiftTable.Last();
71 | var lastSectionAndSignatureSize = delta.TargetSize - lastSection.Value;
72 | var lastSectionMapped = lastSection.Key;
73 | var lastSectionAndSignatureMappedSize = GetMappedSize(lastSectionMapped + lastSectionAndSignatureSize);
74 |
75 | uint sizeOfImage = (uint)lastSectionAndSignatureMappedSize;
76 | uint lowestSizeOfImage = (uint)lastSectionMapped + PAGE_SIZE;
77 |
78 | var urls = new List();
79 | var tasks = new List>();
80 | for (uint size = sizeOfImage; size >= lowestSizeOfImage; size -= PAGE_SIZE)
81 | {
82 | string url = CreateUrl(filename, timeDateStamp, size);
83 | urls.Add(url);
84 | tasks.Add(Task.Run(() => SymbolUrlValid(url)));
85 | }
86 | await Task.WhenAll(tasks);
87 | for (int i = 0; i < tasks.Count; i++)
88 | {
89 | if (tasks[i].Result) return urls[i];
90 | }
91 | throw new InvalidDataException();
92 | }
93 |
94 | static async Task ProcessDelta((string, string, DeltaFile) delta)
95 | {
96 | try
97 | {
98 | var url = await GetUrlFromDelta(delta);
99 | var sb = new StringBuilder(url);
100 | sb.AppendLine();
101 | sb.AppendFormat(" out={0}", delta.Item2);
102 | s_urls.Add(sb.ToString());
103 | }
104 | catch { }
105 | }
106 |
107 | static void Process(string directory, string parent, bool inDir = false)
108 | {
109 | try
110 | {
111 | foreach (var folder in Directory.EnumerateDirectories(directory))
112 | {
113 | Process(folder, directory, inDir || folder.Substring(directory.Length + 1) == "f");
114 | }
115 | if (!inDir) return;
116 | foreach (var file in Directory.EnumerateFiles(directory))
117 | {
118 | if (Path.GetExtension(file) == "mui") continue;
119 | ParseDelta(file, parent);
120 | }
121 | Console.Write(".");
122 | } catch { }
123 | }
124 |
125 | static async Task Main(string[] args)
126 | {
127 | if (args.Length != 1)
128 | {
129 | Console.WriteLine("Usage: {0} ", Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location));
130 | Console.WriteLine();
131 | Console.WriteLine("Given a folder containing delta compressed PE files from a Windows update package,");
132 | Console.WriteLine("uses the data in the delta compression header to search for the PE files on the MS symbol server.");
133 | Console.WriteLine();
134 | return;
135 | }
136 | var tasks = new List();
137 | Console.Write("Searching for delta files");
138 | Process(args[0], args[0]);
139 | Console.WriteLine();
140 | Console.WriteLine("Attempting to find {0} files on the Microsoft Symbol Server...", s_files.Count);
141 | foreach (var file in s_files)
142 | {
143 | tasks.Add(Task.Run(() => ProcessDelta(file)));
144 | }
145 | var whenAll = Task.WhenAll(tasks);
146 | while (true)
147 | {
148 | await Task.WhenAny(whenAll, Task.Delay(1000));
149 | if (whenAll.IsCompleted) break;
150 | var tasksCompletedCount = tasks.Where((t) => t.IsCompleted).Count();
151 | double percent = 100.0 * tasksCompletedCount / tasks.Count;
152 | Console.Write("{0}% complete ({1}/{2})\r", Math.Round(percent, 2), tasksCompletedCount, tasks.Count);
153 | }
154 | Console.WriteLine();
155 |
156 | var sb = new StringBuilder();
157 | foreach (var aria in s_urls)
158 | {
159 | sb.AppendLine(aria);
160 | }
161 | var ariaPath = Path.Combine(args[0], "aria2.txt");
162 | File.WriteAllText(ariaPath, sb.ToString());
163 | Console.WriteLine("Written aria2 input file to {0}", ariaPath);
164 | }
165 | }
166 | }
167 |
--------------------------------------------------------------------------------
/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("DeltaDownloader")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("DeltaDownloader")]
13 | [assembly: AssemblyCopyright("Copyright © 2022")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("3aaf9eaf-4c60-4d90-8138-1ffc74770b4e")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DeltaDownloader
2 |
3 | Given a directory containing delta compressed PE files from a Windows update package, uses the data in the delta compression header to search for the PE files on the Microsoft symbol server. All found URLs will be written to an aria2 input text file.
4 |
5 | ## Theory of operation
6 |
7 | The delta compression header for a PE file contains the following information:
8 | - Size of output file
9 | - `TimeDateStamp` of output file
10 | - `(VirtualAddress, PointerToRawData)` for each section of output file
11 |
12 | PE files on the Microsoft symbols server are stored with keys of `(OriginalFileName, TimeDateStamp, SizeOfImage)`.
13 |
14 | Two of those are known (the original file name being the filename of the delta compressed file, the TimeDateStamp in the delta compression header).
15 |
16 | The other can be determined from the size of the output file and the VirtualAddress/PointerToRawData of the last section:
17 | - `OutputSize - Sections.Last().PointerToRawData` is the size of the last section plus PE signatures.
18 | - This value can be rounded up to a page size, leading to a low number of potential `SizeOfImage` values to check.
19 | - These can be checked by HEAD requests to the Microsoft symbols server: `302` means the correct value was discovered.
20 |
21 | Enough of the functionality of `msdelta.dll` has been reimplemented in C# to allow for obtaining the required values from the delta compression header.
22 |
23 | ## Practical details
24 |
25 | Running this on the extracted update package is possible, but unwise (several thousand binaries would take some time to check, `HttpClient` is not DirBuster).
26 |
27 | Running this on the specific directories of the update package containing components you want to bindiff is the better option.
28 |
29 |
--------------------------------------------------------------------------------
/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------