Skip to content

Commit 852b086

Browse files
committed
Add IS-CAB obfuscation code
1 parent b7fceee commit 852b086

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

SabreTools.Serialization/Wrappers/InstallShieldCabinet.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Text.RegularExpressions;
4+
using SabreTools.Hashing;
45
using SabreTools.IO.Compression.zlib;
56
using SabreTools.Models.InstallShieldCabinet;
67
using static SabreTools.Models.InstallShieldCabinet.Constants;
@@ -76,6 +77,11 @@ public int MajorVersion
7677

7778
#region Constants
7879

80+
/// <summary>
81+
/// Default buffer size
82+
/// </summary>
83+
private const int BUFFER_SIZE = 64 * 1024;
84+
7985
/// <summary>
8086
/// Maximum size of the window in bits
8187
/// </summary>
@@ -556,5 +562,63 @@ public unsafe static int UncompressOld(byte[] dest, ref ulong destLen, byte[] so
556562
}
557563

558564
#endregion
565+
566+
#region Obfuscation
567+
568+
/// <summary>
569+
/// Deobfuscate a buffer
570+
/// </summary>
571+
private void Deobfuscate(byte[] buffer, long size, ref uint offset)
572+
{
573+
offset = Deobfuscate(buffer, size, offset);
574+
}
575+
576+
/// <summary>
577+
/// Deobfuscate a buffer with a seed value
578+
/// </summary>
579+
/// <remarks>Seed is 0 at file start</remarks>
580+
private static uint Deobfuscate(byte[] buffer, long size, uint seed)
581+
{
582+
for (int i = 0; size > 0; size--, i++, seed++)
583+
{
584+
buffer[i] = (byte)(ROR8(buffer[i] ^ 0xd5, 2) - (seed % 0x47));
585+
}
586+
587+
return seed;
588+
}
589+
590+
/// <summary>
591+
/// Obfuscate a buffer
592+
/// </summary>
593+
private void Obfuscate(byte[] buffer, long size, ref uint offset)
594+
{
595+
offset = Obfuscate(buffer, size, offset);
596+
}
597+
598+
/// <summary>
599+
/// Obfuscate a buffer with a seed value
600+
/// </summary>
601+
/// <remarks>Seed is 0 at file start</remarks>
602+
private static uint Obfuscate(byte[] buffer, long size, uint seed)
603+
{
604+
for (int i = 0; size > 0; size--, i++, seed++)
605+
{
606+
buffer[i] = (byte)(ROL8(buffer[i] ^ 0xd5, 2) + (seed % 0x47));
607+
}
608+
609+
return seed;
610+
}
611+
612+
/// <summary>
613+
/// Rotate Right 8
614+
/// </summary>
615+
private static int ROR8(int x, byte n) => (x >> n) | (x << (8 - n));
616+
617+
/// <summary>
618+
/// Rotate Left 8
619+
/// </summary>
620+
private static int ROL8(int x, byte n) => (x << n) | (x >> (8 - n));
621+
622+
#endregion
559623
}
560624
}

0 commit comments

Comments
 (0)