Last active
April 21, 2018 05:34
-
-
Save mjs3339/4f95cb1acd51d863a0fb8870d9e6e9cf to your computer and use it in GitHub Desktop.
C# Collection of Miscellaneous Byte Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MiscByte | |
{ | |
public static byte[] Add(this byte[] left, byte[] right) | |
{ | |
var l1 = left.Length; | |
var l2 = right.Length; | |
if (l1 > 0 && l2 > 0) | |
{ | |
var ret = new byte[l1 + l2]; | |
Buffer.BlockCopy(left, 0, ret, 0, l1); | |
Buffer.BlockCopy(right, 0, ret, l1, l2); | |
return ret; | |
} | |
if (l1 > 0 && l2 == 0) | |
return left; | |
if (l2 > 0 && l1 == 0) | |
return right; | |
return new byte[0]; | |
} | |
public static byte[] XOR(this byte[] left, byte[] right) | |
{ | |
var l1 = left.Length; | |
var l2 = right.Length; | |
if (l1 != l2) | |
throw new Exception("Error: left and right arrays lengths must be equal."); | |
var ret = new byte[l1]; | |
for (var i = 0; i < l1; ++i) | |
ret[i] = (byte)((left[i] ^ right[i]) & 0xff); | |
return ret; | |
} | |
public static byte[] OR(this byte[] left, byte[] right) | |
{ | |
var l1 = left.Length; | |
var l2 = right.Length; | |
if (l1 != l2) | |
throw new Exception("Error: left and right arrays lengths must be equal."); | |
var ret = new byte[l1]; | |
for (var i = 0; i < l1; ++i) | |
ret[i] = (byte)((left[i] | right[i]) & 0xff); | |
return ret; | |
} | |
public static byte[] AND(this byte[] left, byte[] right) | |
{ | |
var l1 = left.Length; | |
var l2 = right.Length; | |
if (l1 != l2) | |
throw new Exception("Error: left and right arrays lengths must be equal."); | |
var ret = new byte[l1]; | |
for (var i = 0; i < l1; ++i) | |
ret[i] = (byte)(left[i] & right[i] & 0xff); | |
return ret; | |
} | |
public static byte[] NOT(this byte[] left) | |
{ | |
var l1 = left.Length; | |
var ret = new byte[l1]; | |
for (var i = 0; i < l1; ++i) | |
ret[i] = (byte)((ushort)~left[i] & 0xff); | |
return ret; | |
} | |
[SecuritySafeCritical] | |
public static unsafe byte[] Clone(this byte[] a1) | |
{ | |
if (a1 == null) | |
return null; | |
var a2 = new byte[a1.Length]; | |
fixed (byte* p1 = a1, p2 = a2) | |
{ | |
var Len = a1.Length; | |
byte* x1 = p1, x2 = p2; | |
while (Len > 7) | |
{ | |
*(long*)x2 = *(long*)x1; | |
x1 += 8; | |
x2 += 8; | |
Len -= 8; | |
} | |
switch (Len % 8) | |
{ | |
case 0: | |
break; | |
case 7: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*(short*)x2 = *(short*)x1; | |
x1 += 2; | |
x2 += 2; | |
*x2 = *x1; | |
break; | |
case 6: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*(short*)x2 = *(short*)x1; | |
break; | |
case 5: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*x2 = *x1; | |
break; | |
case 4: | |
*(int*)x2 = *(int*)x1; | |
break; | |
case 3: | |
*(short*)x2 = *(short*)x1; | |
x1 += 2; | |
x2 += 2; | |
*x2 = *x1; | |
break; | |
case 2: | |
*(short*)x2 = *(short*)x1; | |
break; | |
case 1: | |
*x2 = *x1; | |
break; | |
} | |
return a2; | |
} | |
} | |
[SecuritySafeCritical] | |
public static unsafe bool Copy(this byte[] a1, int aindex, byte[] a2, int bindex, int length) | |
{ | |
if (a1 == null || a2 == null) | |
return false; | |
fixed (byte* p1 = a1, p2 = a2) | |
{ | |
var Len = length; | |
byte* x1 = p1 + aindex, x2 = p2 + bindex; | |
while (Len > 7) | |
{ | |
*(long*)x2 = *(long*)x1; | |
x1 += 8; | |
x2 += 8; | |
Len -= 8; | |
} | |
switch (Len % 8) | |
{ | |
case 0: | |
break; | |
case 7: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*(short*)x2 = *(short*)x1; | |
x1 += 2; | |
x2 += 2; | |
*x2 = *x1; | |
break; | |
case 6: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*(short*)x2 = *(short*)x1; | |
break; | |
case 5: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*x2 = *x1; | |
break; | |
case 4: | |
*(int*)x2 = *(int*)x1; | |
break; | |
case 3: | |
*(short*)x2 = *(short*)x1; | |
x1 += 2; | |
x2 += 2; | |
*x2 = *x1; | |
break; | |
case 2: | |
*(short*)x2 = *(short*)x1; | |
break; | |
case 1: | |
*x2 = *x1; | |
break; | |
} | |
return true; | |
} | |
} | |
[SecuritySafeCritical] | |
public static unsafe byte[] SubByte(this byte[] a1, int aindex, int length) | |
{ | |
if (a1 == null) | |
return null; | |
if (aindex + length > a1.Length) | |
throw new Exception("Error: SubByte - index + length exceed source array length."); | |
var a2 = new byte[length]; | |
fixed (byte* p1 = a1, p2 = a2) | |
{ | |
var Len = length; | |
byte* x1 = p1 + aindex, x2 = p2; | |
while (Len > 7) | |
{ | |
*(long*)x2 = *(long*)x1; | |
x1 += 8; | |
x2 += 8; | |
Len -= 8; | |
} | |
switch (Len % 8) | |
{ | |
case 0: | |
break; | |
case 7: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*(short*)x2 = *(short*)x1; | |
x1 += 2; | |
x2 += 2; | |
*x2 = *x1; | |
break; | |
case 6: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*(short*)x2 = *(short*)x1; | |
break; | |
case 5: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*x2 = *x1; | |
break; | |
case 4: | |
*(int*)x2 = *(int*)x1; | |
break; | |
case 3: | |
*(short*)x2 = *(short*)x1; | |
x1 += 2; | |
x2 += 2; | |
*x2 = *x1; | |
break; | |
case 2: | |
*(short*)x2 = *(short*)x1; | |
break; | |
case 1: | |
*x2 = *x1; | |
break; | |
} | |
return a2; | |
} | |
} | |
[SecuritySafeCritical] | |
public static byte[] CloneTo(this byte[] a1) | |
{ | |
var a1l = a1.Length; | |
var copy = new byte[a1l]; | |
a1.Copy(copy); | |
return copy; | |
} | |
[SecuritySafeCritical] | |
public static unsafe bool Copy(this byte[] a1, byte[] a2) | |
{ | |
if (a1 == null || a2 == null || a1.Length != a2.Length) | |
return false; | |
fixed (byte* p1 = a1, p2 = a2) | |
{ | |
var Len = a1.Length; | |
byte* x1 = p1, x2 = p2; | |
while (Len > 7) | |
{ | |
*(long*)x2 = *(long*)x1; | |
x1 += 8; | |
x2 += 8; | |
Len -= 8; | |
} | |
switch (Len % 8) | |
{ | |
case 0: | |
break; | |
case 7: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*(short*)x2 = *(short*)x1; | |
x1 += 2; | |
x2 += 2; | |
*x2 = *x1; | |
break; | |
case 6: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*(short*)x2 = *(short*)x1; | |
break; | |
case 5: | |
*(int*)x2 = *(int*)x1; | |
x1 += 4; | |
x2 += 4; | |
*x2 = *x1; | |
break; | |
case 4: | |
*(int*)x2 = *(int*)x1; | |
break; | |
case 3: | |
*(short*)x2 = *(short*)x1; | |
x1 += 2; | |
x2 += 2; | |
*x2 = *x1; | |
break; | |
case 2: | |
*(short*)x2 = *(short*)x1; | |
break; | |
case 1: | |
*x2 = *x1; | |
break; | |
} | |
return true; | |
} | |
} | |
public static bool Compare(this short[] a1, short[] a2) | |
{ | |
var b1 = a1.GetBytes(); | |
var b2 = a2.GetBytes(); | |
return b1.Compare(b2); | |
} | |
public static bool Compare(this byte[] a1, short[] a2) | |
{ | |
var b2 = a2.GetBytes(); | |
return a1.Compare(b2); | |
} | |
public static bool Compare(this ushort[] a1, ushort[] a2) | |
{ | |
var b1 = a1.GetBytes(); | |
var b2 = a2.GetBytes(); | |
return b1.Compare(b2); | |
} | |
public static bool Compare(this int[] a1, int[] a2) | |
{ | |
var b1 = a1.GetBytes(); | |
var b2 = a2.GetBytes(); | |
return b1.Compare(b2); | |
} | |
public static bool Compare(this uint[] a1, uint[] a2) | |
{ | |
var b1 = a1.GetBytes(); | |
var b2 = a2.GetBytes(); | |
return b1.Compare(b2); | |
} | |
public static bool Compare(this long[] a1, long[] a2) | |
{ | |
var b1 = a1.GetBytes(); | |
var b2 = a2.GetBytes(); | |
return b1.Compare(b2); | |
} | |
public static bool Compare(this ulong[] a1, ulong[] a2) | |
{ | |
var b1 = a1.GetBytes(); | |
var b2 = a2.GetBytes(); | |
return b1.Compare(b2); | |
} | |
public static bool Compare(this double[] a1, double[] a2) | |
{ | |
var b1 = a1.GetBytes(); | |
var b2 = a2.GetBytes(); | |
return b1.Compare(b2); | |
} | |
public static bool Compare(this float[] a1, float[] a2) | |
{ | |
var b1 = a1.GetBytes(); | |
var b2 = a2.GetBytes(); | |
return b1.Compare(b2); | |
} | |
[SecuritySafeCritical] | |
public static unsafe bool Compare(this byte[] a1, byte[] a2) | |
{ | |
if (a1 == null && a2 == null) | |
return true; | |
if (a1 == null || a2 == null || a1.Length != a2.Length) | |
return false; | |
fixed (byte* p1 = a1, p2 = a2) | |
{ | |
var Len = a1.Length; | |
byte* x1 = p1, x2 = p2; | |
while (Len > 7) | |
{ | |
if (*(long*)x2 != *(long*)x1) | |
return false; | |
x1 += 8; | |
x2 += 8; | |
Len -= 8; | |
} | |
switch (Len % 8) | |
{ | |
case 0: | |
break; | |
case 7: | |
if (*(int*)x2 != *(int*)x1) | |
return false; | |
x1 += 4; | |
x2 += 4; | |
if (*(short*)x2 != *(short*)x1) | |
return false; | |
x1 += 2; | |
x2 += 2; | |
if (*x2 != *x1) | |
return false; | |
break; | |
case 6: | |
if (*(int*)x2 != *(int*)x1) | |
return false; | |
x1 += 4; | |
x2 += 4; | |
if (*(short*)x2 != *(short*)x1) | |
return false; | |
break; | |
case 5: | |
if (*(int*)x2 != *(int*)x1) | |
return false; | |
x1 += 4; | |
x2 += 4; | |
if (*x2 != *x1) | |
return false; | |
break; | |
case 4: | |
if (*(int*)x2 != *(int*)x1) | |
return false; | |
break; | |
case 3: | |
if (*(short*)x2 != *(short*)x1) | |
return false; | |
x1 += 2; | |
x2 += 2; | |
if (*x2 != *x1) | |
return false; | |
break; | |
case 2: | |
if (*(short*)x2 != *(short*)x1) | |
return false; | |
break; | |
case 1: | |
if (*x2 != *x1) | |
return false; | |
break; | |
} | |
return true; | |
} | |
} | |
[SecuritySafeCritical] | |
public static unsafe bool Fill(this byte[] a1, byte b1) | |
{ | |
if (a1 == null) | |
return false; | |
byte[] fbl = { b1, b1, b1, b1, b1, b1, b1, b1 }; | |
byte[] fbi = { b1, b1, b1, b1 }; | |
byte[] fbs = { b1, b1 }; | |
byte[] fbb = { b1 }; | |
fixed (byte* p1 = a1, p8 = fbl, p4 = fbi, p2 = fbs, p0 = fbb) | |
{ | |
var Len = a1.Length; | |
var x1 = p1; | |
while (Len > 7) | |
{ | |
*(long*)x1 = *(long*)p8; | |
x1 += 8; | |
Len -= 8; | |
} | |
switch (Len % 8) | |
{ | |
case 0: | |
break; | |
case 7: | |
*(int*)x1 = *(int*)p4; | |
x1 += 4; | |
*(short*)x1 = *(short*)p2; | |
x1 += 2; | |
*x1 = *p0; | |
break; | |
case 6: | |
*(int*)x1 = *(int*)p4; | |
x1 += 4; | |
*(short*)x1 = *(short*)p2; | |
break; | |
case 5: | |
*(int*)x1 = *(int*)p4; | |
x1 += 4; | |
*x1 = *p0; | |
break; | |
case 4: | |
*(int*)x1 = *(int*)p4; | |
break; | |
case 3: | |
*(short*)x1 = *(short*)p2; | |
x1 += 2; | |
*x1 = *p0; | |
break; | |
case 2: | |
*(short*)x1 = *(short*)p2; | |
break; | |
case 1: | |
*x1 = *p0; | |
break; | |
} | |
return true; | |
} | |
} | |
[SecuritySafeCritical] | |
public static unsafe bool Fill(this int[] a1, int i1) | |
{ | |
if (a1 == null) | |
return false; | |
int[] fbl = { i1, i1 }; | |
int[] fbi = { i1 }; | |
fixed (int* p1 = a1, p8 = fbl, p4 = fbi) | |
{ | |
var Len = a1.Length; | |
var x1 = p1; | |
while (Len > 1) | |
{ | |
*(long*)x1 = *(long*)p8; | |
x1 += 2; | |
Len -= 2; | |
} | |
if (Len == 1) | |
*x1 = *p4; | |
return true; | |
} | |
} | |
[SecuritySafeCritical] | |
public static unsafe bool Fill(this long[] a1, long i1) | |
{ | |
if (a1 == null) | |
return false; | |
long[] fbi = { i1 }; | |
fixed (long* p1 = a1, p = fbi) | |
{ | |
var Len = a1.Length; | |
var x1 = p1; | |
while (Len > 1) | |
{ | |
*x1 = *p; | |
x1 += 1; | |
Len -= 1; | |
} | |
return true; | |
} | |
} | |
[SecuritySafeCritical] | |
public static unsafe bool Fill(this ulong[] a1, ulong i1) | |
{ | |
if (a1 == null) | |
return false; | |
ulong[] fbi = { i1 }; | |
fixed (ulong* p1 = a1, p = fbi) | |
{ | |
var Len = a1.Length; | |
var x1 = p1; | |
while (Len > 1) | |
{ | |
*x1 = *p; | |
x1 += 1; | |
Len -= 1; | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment