Updates to Utilities

This commit is contained in:
Tal Aloni 2016-12-27 10:08:22 +02:00
parent 6cb61ca63e
commit 5508c749ce
13 changed files with 417 additions and 13 deletions

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
@ -33,6 +32,35 @@ namespace Utilities
return true;
}
public static byte[] XOR(byte[] array1, byte[] array2)
{
if (array1.Length == array2.Length)
{
return XOR(array1, 0, array2, 0, array1.Length);
}
else
{
throw new ArgumentException("Arrays must be of equal length");
}
}
public static byte[] XOR(byte[] array1, int offset1, byte[] array2, int offset2, int length)
{
if (offset1 + length <= array1.Length && offset2 + length <= array2.Length)
{
byte[] result = new byte[length];
for (int index = 0; index < length; index++)
{
result[index] = (byte)(array1[offset1 + index] ^ array2[offset2 + index]);
}
return result;
}
else
{
throw new ArgumentOutOfRangeException();
}
}
public static long CopyStream(Stream input, Stream output)
{
// input may not support seeking, so don't use input.Position
@ -41,7 +69,7 @@ namespace Utilities
public static long CopyStream(Stream input, Stream output, long count)
{
const int MaxBufferSize = 4194304; // 4 MB
const int MaxBufferSize = 1048576; // 1 MB
int bufferSize = (int)Math.Min(MaxBufferSize, count);
byte[] buffer = new byte[bufferSize];
long totalBytesRead = 0;

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

View file

@ -1,23 +1,41 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Utilities
{
public class LittleEndianReader
{
public static short ReadInt16(byte[] buffer, ref int offset)
{
offset += 2;
return LittleEndianConverter.ToInt16(buffer, offset - 2);
}
public static ushort ReadUInt16(byte[] buffer, ref int offset)
{
offset += 2;
return LittleEndianConverter.ToUInt16(buffer, offset - 2);
}
public static int ReadInt32(byte[] buffer, ref int offset)
{
offset += 4;
return LittleEndianConverter.ToInt32(buffer, offset - 4);
}
public static uint ReadUInt32(byte[] buffer, ref int offset)
{
offset += 4;
return LittleEndianConverter.ToUInt32(buffer, offset - 4);
}
public static long ReadInt64(byte[] buffer, ref int offset)
{
offset += 8;
return LittleEndianConverter.ToInt64(buffer, offset - 8);
}
public static ulong ReadUInt64(byte[] buffer, ref int offset)
{
offset += 8;
@ -29,5 +47,54 @@ namespace Utilities
offset += 16;
return LittleEndianConverter.ToGuid(buffer, offset - 16);
}
public static short ReadInt16(Stream stream)
{
byte[] buffer = new byte[2];
stream.Read(buffer, 0, 2);
return LittleEndianConverter.ToInt16(buffer, 0);
}
public static ushort ReadUInt16(Stream stream)
{
byte[] buffer = new byte[2];
stream.Read(buffer, 0, 2);
return LittleEndianConverter.ToUInt16(buffer, 0);
}
public static int ReadInt32(Stream stream)
{
byte[] buffer = new byte[4];
stream.Read(buffer, 0, 4);
return LittleEndianConverter.ToInt32(buffer, 0);
}
public static uint ReadUInt32(Stream stream)
{
byte[] buffer = new byte[4];
stream.Read(buffer, 0, 4);
return LittleEndianConverter.ToUInt32(buffer, 0);
}
public static long ReadInt64(Stream stream)
{
byte[] buffer = new byte[8];
stream.Read(buffer, 0, 8);
return LittleEndianConverter.ToInt64(buffer, 0);
}
public static ulong ReadUInt64(Stream stream)
{
byte[] buffer = new byte[8];
stream.Read(buffer, 0, 8);
return LittleEndianConverter.ToUInt64(buffer, 0);
}
public static Guid ReadGuidBytes(Stream stream)
{
byte[] buffer = new byte[16];
stream.Read(buffer, 0, 16);
return LittleEndianConverter.ToGuid(buffer, 0);
}
}
}

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;