101 lines
3.0 KiB
C#
101 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LeatherApp.Utils
|
|
{
|
|
public static class HexUtil
|
|
{
|
|
public static byte[] joinByteList(List<byte[]> buffList)
|
|
{
|
|
int len = 0;
|
|
foreach (byte[] b in buffList)
|
|
len += b.Length;
|
|
|
|
byte[] result = new byte[len];
|
|
len = 0;
|
|
foreach (byte[] b in buffList)
|
|
{
|
|
b.CopyTo(result, len);
|
|
len += b.Length;
|
|
}
|
|
return result;
|
|
}
|
|
public static byte[] subBytes(byte[] buff, int start, int len = 0)
|
|
{
|
|
if (len < 1)
|
|
return buff.Skip(start).ToArray();
|
|
else
|
|
return buff.Skip(start).Take(len).ToArray();
|
|
}
|
|
public static string toHexString(this byte[] bytes)
|
|
{
|
|
string byteStr = string.Empty;
|
|
if (bytes != null || bytes.Length > 0)
|
|
{
|
|
foreach (var item in bytes)
|
|
{
|
|
byteStr += string.Format("{0:X2}", item);
|
|
}
|
|
}
|
|
return byteStr;
|
|
}
|
|
public static bool Equals(byte[] buff1, byte[] buff2)
|
|
{
|
|
if (buff1 == null || buff1 == null || buff1.Length != buff2.Length)
|
|
return false;
|
|
for (int i = 0; i < buff1.Length; i++)
|
|
if (buff1[i] != buff2[i]) return false;
|
|
return true;
|
|
|
|
}
|
|
public static IEnumerable<long> IndexOf(byte[] source, int start, byte[] pattern)
|
|
{
|
|
if (source == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(source));
|
|
}
|
|
|
|
if (pattern == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(pattern));
|
|
}
|
|
|
|
long valueLength = source.LongLength;
|
|
long patternLength = pattern.LongLength;
|
|
if ((valueLength == 0) || (patternLength == 0) || (patternLength > valueLength))
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
var badCharacters = new long[256];
|
|
for (var i = 0; i < 256; i++)
|
|
{
|
|
badCharacters[i] = patternLength;
|
|
}
|
|
|
|
var lastPatternByte = patternLength - 1;
|
|
for (long i = 0; i < lastPatternByte; i++)
|
|
{
|
|
badCharacters[pattern[i]] = lastPatternByte - i;
|
|
}
|
|
|
|
long index = start;
|
|
while (index <= valueLength - patternLength)
|
|
{
|
|
for (var i = lastPatternByte; source[index + i] == pattern[i]; i--)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
yield return index;
|
|
break;
|
|
}
|
|
}
|
|
|
|
index += badCharacters[source[index + lastPatternByte]];
|
|
}
|
|
}
|
|
}
|
|
} |