mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-05-14 08:27:05 +02:00
SMBServer v1.0.5
This commit is contained in:
parent
b75820452d
commit
bd1006cb81
400 changed files with 28062 additions and 0 deletions
120
Utilities/ByteUtils/ByteReader.cs
Normal file
120
Utilities/ByteUtils/ByteReader.cs
Normal file
|
@ -0,0 +1,120 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Utilities
|
||||
{
|
||||
public class ByteReader
|
||||
{
|
||||
public static byte ReadByte(byte[] buffer, int offset)
|
||||
{
|
||||
return buffer[offset];
|
||||
}
|
||||
|
||||
public static byte ReadByte(byte[] buffer, ref int offset)
|
||||
{
|
||||
offset++;
|
||||
return buffer[offset - 1];
|
||||
}
|
||||
|
||||
public static byte[] ReadBytes(byte[] buffer, int offset, int length)
|
||||
{
|
||||
byte[] result = new byte[length];
|
||||
Array.Copy(buffer, offset, result, 0, length);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] ReadBytes(byte[] buffer, ref int offset, int length)
|
||||
{
|
||||
offset += length;
|
||||
return ReadBytes(buffer, offset - length, length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will return the ANSI string stored in the buffer
|
||||
/// </summary>
|
||||
public static string ReadAnsiString(byte[] buffer, int offset, int count)
|
||||
{
|
||||
// ASCIIEncoding.ASCII.GetString will convert some values to '?' (byte value of 63)
|
||||
// Any codepage will do, but the only one that Mono supports is 28591.
|
||||
return ASCIIEncoding.GetEncoding(28591).GetString(buffer, offset, count);
|
||||
}
|
||||
|
||||
public static string ReadAnsiString(byte[] buffer, ref int offset, int count)
|
||||
{
|
||||
offset += count;
|
||||
return ReadAnsiString(buffer, offset - count, count);
|
||||
}
|
||||
|
||||
public static string ReadUTF16String(byte[] buffer, int offset, int numberOfCharacters)
|
||||
{
|
||||
int numberOfBytes = numberOfCharacters * 2;
|
||||
return Encoding.Unicode.GetString(buffer, offset, numberOfBytes);
|
||||
}
|
||||
|
||||
public static string ReadUTF16String(byte[] buffer, ref int offset, int numberOfCharacters)
|
||||
{
|
||||
int numberOfBytes = numberOfCharacters * 2;
|
||||
offset += numberOfBytes;
|
||||
return ReadUTF16String(buffer, offset - numberOfBytes, numberOfCharacters);
|
||||
}
|
||||
|
||||
public static string ReadNullTerminatedAnsiString(byte[] buffer, int offset)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
char c = (char)ByteReader.ReadByte(buffer, offset);
|
||||
while (c != '\0')
|
||||
{
|
||||
builder.Append(c);
|
||||
offset++;
|
||||
c = (char)ByteReader.ReadByte(buffer, offset);
|
||||
}
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
public static string ReadNullTerminatedUTF16String(byte[] buffer, int offset)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
char c = (char)LittleEndianConverter.ToUInt16(buffer, offset);
|
||||
while (c != 0)
|
||||
{
|
||||
builder.Append(c);
|
||||
offset += 2;
|
||||
c = (char)LittleEndianConverter.ToUInt16(buffer, offset);
|
||||
}
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
public static string ReadNullTerminatedAnsiString(byte[] buffer, ref int offset)
|
||||
{
|
||||
string result = ReadNullTerminatedAnsiString(buffer, offset);
|
||||
offset += result.Length + 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string ReadNullTerminatedUTF16String(byte[] buffer, ref int offset)
|
||||
{
|
||||
string result = ReadNullTerminatedUTF16String(buffer, offset);
|
||||
offset += result.Length * 2 + 2;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] ReadBytes(Stream stream, int count)
|
||||
{
|
||||
MemoryStream temp = new MemoryStream();
|
||||
ByteUtils.CopyStream(stream, temp, count);
|
||||
return temp.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return all bytes from current stream position to the end of the stream
|
||||
/// </summary>
|
||||
public static byte[] ReadAllBytes(Stream stream)
|
||||
{
|
||||
MemoryStream temp = new MemoryStream();
|
||||
ByteUtils.CopyStream(stream, temp);
|
||||
return temp.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue