// Telenor Inpli TFTP Server Module
//
// Copyright 2018 Telenor Inpli AS Norway
using System;
internal static class BufferPrimitives
{
///
/// Read a 16-bit big endian value from a buffer at a given offset
///
/// The buffer to read from
/// The offset in the buffer
/// A 16 bit value
/// If reading past the end of the buffer
public static int Get16BE(this byte [] buffer, long offset)
{
if ((offset + 2) >= buffer.Length)
throw new IndexOutOfRangeException();
return ((int)(buffer[offset]) << 8) | (int)buffer[offset+1];
}
///
/// Writes a 16-bit big endian to a buffer at a given offset
///
/// The buffer to write into
/// The offset within the buffer to write to
/// The value to write
/// If attempting to write past the end
public static void Write16BE(this byte [] buffer, long offset, int value)
{
if ((offset + 2) >= buffer.Length)
throw new IndexOutOfRangeException();
buffer[offset] = (byte)((value >> 8) & 0xff);
buffer[offset + 1] = (byte)(value & 0xff);
}
}