| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace KernelCommunication |
| | 4 | | { |
| | 5 | | public static class ByteUtils |
| | 6 | | { |
| | 7 | | public static unsafe int PointerToInt32(byte* ptr) |
| | 8 | | { |
| 109 | 9 | | return *(int*)ptr; |
| | 10 | | } |
| | 11 | |
|
| | 12 | | public static unsafe long PointerToInt64(byte* ptr) |
| | 13 | | { |
| 0 | 14 | | return *(long*)ptr; |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public static unsafe int ReadInt32(ReadOnlySpan<byte> span, int position) |
| | 18 | | { |
| 103 | 19 | | if ((long)(uint)position >= (long)span.Length) |
| 0 | 20 | | throw new IndexOutOfRangeException("offset is bigger than bytes array lenght"); |
| | 21 | |
|
| 103 | 22 | | if (position > span.Length - 4) |
| 0 | 23 | | throw new IndexOutOfRangeException("bytes.Length is not large enough to contain a valid Int32"); |
| | 24 | |
|
| 103 | 25 | | fixed (byte* numPtr = &span[position]) |
| | 26 | | { |
| 103 | 27 | | return PointerToInt32(numPtr); |
| | 28 | | } |
| | 29 | | } |
| | 30 | |
|
| | 31 | | public static unsafe long ReadInt64(ReadOnlySpan<byte> span, int position) |
| | 32 | | { |
| 0 | 33 | | if ((long)(uint)position >= (long)span.Length) |
| 0 | 34 | | throw new IndexOutOfRangeException("offset is bigger than bytes array lenght"); |
| | 35 | |
|
| 0 | 36 | | if (position > span.Length - 8) |
| 0 | 37 | | throw new IndexOutOfRangeException("bytes.Length is not large enough to contain a valid Int64"); |
| | 38 | |
|
| 0 | 39 | | fixed (byte* numPtr = &span[position]) |
| | 40 | | { |
| 0 | 41 | | return PointerToInt64(numPtr); |
| | 42 | | } |
| | 43 | | } |
| | 44 | | } |
| | 45 | | } |