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