Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
maxton committed Mar 27, 2017
1 parent 9f395f5 commit 25153ea
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions Library/Ark/ProtectedFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ public override long Position
}

/// <summary>
/// Constructs a new offset stream on the given base stream with the given offset and length.
/// Constructs a new protected file stream from the given base stream.
/// </summary>
/// <param name="package">The base stream</param>
/// <param name="offset">Offset into the base stream where this stream starts</param>
/// <param name="length">Number of bytes in this stream</param>
public ProtectedFileStream(Stream package)
{
this.pkg = package;
Expand All @@ -68,7 +66,7 @@ public ProtectedFileStream(Stream package)
package.Seek(-size, SeekOrigin.End);
var metadata = new byte[size];
package.Read(metadata, 0, size);
initialKey = init_prot_data(metadata);
initialKey = CalculateKeyByte(metadata);

data_offset = 0;
Length = package.Length - size;
Expand Down Expand Up @@ -131,7 +129,7 @@ public override long Seek(long offset, SeekOrigin origin)
return _position;
}

private static uint rol(uint value, int count)
private static uint RotL(uint value, int count)
{
const int bits = 32;
count %= bits;
Expand All @@ -147,7 +145,7 @@ private static byte BYTE(int num, uint value)
return (byte)(value >> (num * 8));
}

private static uint mangle(byte[] bytes, int offset, int count)
private static uint Mangle(byte[] bytes, int offset, int count)
{
var mangled = 0U;
for(var i = 0; i < count; i++)
Expand All @@ -157,22 +155,22 @@ private static uint mangle(byte[] bytes, int offset, int count)
return mangled;
}

private static uint collapse(uint value)
private static uint Fold(uint value)
{
return (uint)(BYTE(0, value) + BYTE(1, value) + BYTE(2, value) + BYTE(3, value));
}

public static uint do_hash(byte[] key, int offset, long count)
private static uint Hash(byte[] key, int offset, long count)
{
uint tmp;

byte counter = 0;
var seed = 0xE3AFEC21;
for (var i = 0L; i < count; i++)
{
tmp = (key[offset + i] ^ collapse(seed));
tmp = (key[offset + i] ^ Fold(seed));
key[offset + i] = (byte)tmp;
seed = rol((tmp | ((tmp | ((tmp | (tmp << 8)) << 8)) << 8)) + rol(seed, (int)(tmp & 0x1F)), 1);
seed = RotL((tmp | ((tmp | ((tmp | (tmp << 8)) << 8)) << 8)) + RotL(seed, (int)(tmp & 0x1F)), 1);
if (counter > 16)
{
seed = (2 * seed);
Expand All @@ -183,22 +181,22 @@ public static uint do_hash(byte[] key, int offset, long count)
return seed;
}

public static byte init_prot_data(byte[] metadata)
private static byte CalculateKeyByte(byte[] metadata)
{
var word_0xE = BitConverter.ToUInt16(metadata, 0xE);

byte mangled = (byte)collapse(
mangle(metadata, 4, 9) +
mangle(metadata, 0, 4) +
mangle(metadata, 13, 1) +
mangle(metadata, 16, 4) +
(word_0xE == 0 ? 0 : mangle(metadata, 24, word_0xE)));

do_hash(metadata, 24, word_0xE);
do_hash(metadata, 13, 1);
do_hash(metadata, 16, 4);
do_hash(metadata, 0, 4);
do_hash(metadata, 4, 9);
byte mangled = (byte)Fold(
Mangle(metadata, 4, 9) +
Mangle(metadata, 0, 4) +
Mangle(metadata, 13, 1) +
Mangle(metadata, 16, 4) +
Mangle(metadata, 24, word_0xE));

Hash(metadata, 24, word_0xE);
Hash(metadata, 13, 1);
Hash(metadata, 16, 4);
Hash(metadata, 0, 4);
Hash(metadata, 4, 9);

return (byte)(metadata[5] ^ mangled);
}
Expand Down

0 comments on commit 25153ea

Please sign in to comment.