/// ************************************************************************ /// Copyright (C) 2001, Patrick Charles and Jonas Lehmann * /// Distributed under the Mozilla Public License * /// http://www.mozilla.org/NPL/MPL-1.1.txt * /// ************************************************************************* /// using System; namespace SharpPcap.Packets.Util { /// Utility functions for populating and manipulating arrays. /// public class ArrayHelper { /// Join two arrays. public static byte[] join(byte[] a, byte[] b) { byte[] bytes = new byte[a.Length + b.Length]; Array.Copy(a, 0, bytes, 0, a.Length); Array.Copy(b, 0, bytes, a.Length, b.Length); return bytes; } public static byte[] copy(byte[] from) { return copy(from, 0); } public static byte[] copy(byte[] from, int start) { return copy(from, start, from.Length); } public static byte[] copy(byte[] from, int start, int len) { byte[] to = new byte[len]; Array.Copy(from, start, to, 0, len); return to; } //FIXME: this routine is broken in the sense that it performs // implicit byte order conversion /// Extract a long from a byte array. /// /// /// an array. /// /// the starting position where the integer is stored. /// /// the number of bytes which contain the integer. /// /// the long, or 0 if the index/length to use /// would cause an ArrayOutOfBoundsException /// public static long extractLong(byte[] bytes, int pos, int cnt) { // commented out because it seems like it might mask a fundamental // problem, if the caller is referencing positions out of bounds and // silently getting back '0'. // if((pos + cnt) > bytes.length) return 0; long value_Renamed = 0; for (int i = 0; i < cnt; i++) value_Renamed |= ((bytes[pos + cnt - i - 1] & 0xffL) << 8 * i); return value_Renamed; } /// Extract an integer from a byte array. /// /// /// an array. /// /// the starting position where the integer is stored. /// /// the number of bytes which contain the integer. /// /// the integer, or 0 if the index/length to use /// would cause an ArrayOutOfBoundsException /// public static int extractInteger(byte[] bytes, int pos, int cnt) { // commented out because it seems like it might mask a fundamental // problem, if the caller is referencing positions out of bounds and // silently getting back '0'. // if((pos + cnt) > bytes.length) return 0; int value_Renamed = 0; for (int i = 0; i < cnt; i++) value_Renamed |= ((bytes[pos + cnt - i - 1] & 0xff) << 8 * i); return value_Renamed; } /// Insert data contained in a long integer into an array. /// /// /// an array. /// /// the long to insert into the array. /// /// the starting position into which the long is inserted. /// /// the number of bytes to insert. /// public static void insertLong(byte[] bytes, long value_Renamed, int pos, int cnt) { for (int i = 0; i < cnt; i++) { bytes[pos + cnt - i - 1] = (byte) (value_Renamed & 0xff); value_Renamed >>= 8; } } public static void insertInt32(byte[] bytes, int value_Renamed, int pos) { insertLong(bytes, value_Renamed, pos, 4); } public static void insertInt16(byte[] bytes, int value_Renamed, int pos) { insertLong(bytes, value_Renamed, pos, 2); } /// Convert a long integer into an array of bytes. /// /// /// the long to convert. /// /// the number of bytes to convert. /// public static byte[] toBytes(long value_Renamed, int cnt) { byte[] bytes = new byte[cnt]; for (int i = 0; i < cnt; i++) { bytes[cnt - i - 1] = (byte) (value_Renamed & 0xff); value_Renamed >>= 8; } return bytes; } /// Convert a long integer into an array of bytes, little endian format. /// (i.e. this does the same thing as toBytes() but returns an array /// in reverse order from the array returned in toBytes(). /// /// the long to convert. /// /// the number of bytes to convert. /// public static byte[] toBytesLittleEndian(long value_Renamed, int cnt) { byte[] bytes = new byte[cnt]; for (int i = 0; i < cnt; i++) { bytes[i] = (byte) (value_Renamed & 0xff); value_Renamed >>= 8; } return bytes; } public static void fillBytes(byte[] byteArray, long value_Renamed, int cnt, int index) { for (int i = 0; i < cnt; i++) { byteArray[cnt - i - 1 + index] = (byte) (value_Renamed & 0xff); value_Renamed >>= 8; } } public static void fillBytesLittleEndian(byte[] byteArray, long value_Renamed, int cnt, int index) { for (int i = 0; i < cnt; i++) { byteArray[index + i] = (byte) (value_Renamed & 0xff); value_Renamed >>= 8; } } public static bool equals(byte[] a1, byte[] a2) { if (a1.Length != a2.Length) return false; for (int i = 0; i < a1.Length; i++) { if (a1[i] != a2[i]) return false; } return true; } } }