1355 lines · 68.1 KB · C#
RawDownload
//******************************************************************************************************//  BigEndian.cs - Gbtc////  Copyright © 2014, Grid Protection Alliance.  All Rights Reserved.////  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See//  the NOTICE file distributed with this work for additional information regarding copyright ownership.//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may//  not use this file except in compliance with the License. You may obtain a copy of the License at:////      http://www.opensource.org/licenses/MIT////  Unless agreed to in writing, the subject software distributed under the License is distributed on an//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the//  License for the specific language governing permissions and limitations.////  Code Modification History://  ----------------------------------------------------------------------------------------------------//  05/06/2014 - Steven E. Chisholm//       Generated original version of source code based on EndianOrder.cs//  08/20/2014 - Steven E. Chisholm//       Added encoding for decimal numbers and support for pointer methods.////****************************************************************************************************** #region [ Contributor License Agreements ] /**************************************************************************\   Copyright © 2009 - J. Ritchie Carroll   All rights reserved.     Redistribution and use in source and binary forms, with or without   modification, are permitted provided that the following conditions   are met:        * Redistributions of source code must retain the above copyright        notice, this list of conditions and the following disclaimer.             * Redistributions in binary form must reproduce the above        copyright notice, this list of conditions and the following        disclaimer in the documentation and/or other materials provided        with the distribution.     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY   OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  \**************************************************************************/ #endregion using System;using System.Runtime.CompilerServices; namespace GSF{    /// <summary>    /// Defines a set of big-endian byte order interoperability functions.    /// </summary>    /// <remarks>    /// This class is setup to support aggressive in-lining of big endian conversions. Bounds    /// will not be checked as part of this function call, if bounds are violated, the exception    /// will be thrown at the <see cref="Array"/> level.    /// </remarks>    public static class BigEndian    {        #region [ ToValue Pointer ]         /// <summary>        /// Returns a <see cref="Boolean"/> value converted from one byte at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes.</param>        /// <returns>true if the byte at startIndex in value is nonzero; otherwise, false.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe bool ToBoolean(byte* buffer)        {            return buffer[0] != 0;        }         /// <summary>        /// Returns a Unicode character converted from two bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A character formed by two bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe char ToChar(byte* buffer)        {            return (char)ToInt16(buffer);        }         /// <summary>        /// Returns a double-precision floating point number converted from eight bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A double-precision floating point number formed by eight bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe double ToDouble(byte* buffer)        {            long int64 = ToInt64(buffer);            return *(double*)&int64;        }         /// <summary>        /// Returns a 16-bit signed integer converted from two bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A 16-bit signed integer formed by two bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe short ToInt16(byte* buffer)        {            return (short)((int)buffer[0] << 8 | (int)buffer[1]);        }         /// <summary>        /// Returns a 24-bit signed integer converted from three bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A 24-bit signed integer formed by three bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe Int24 ToInt24(byte* buffer)        {            int int32 = buffer[0] << 16 |                        buffer[1] << 8 |                        buffer[2];             // Check bit 23, the sign bit in a signed 24-bit integer            if ((int32 & 0x00800000) > 0)            {                // If the sign-bit is set, this number will be negative - set all high-byte bits (keeps 32-bit number in 24-bit range)                int32 |= Int24.BitMask;            }            else            {                // If the sign-bit is not set, this number will be positive - clear all high-byte bits (keeps 32-bit number in 24-bit range)                int32 &= ~Int24.BitMask;            }             return (Int24)int32;        }         /// <summary>        /// Returns a 32-bit signed integer converted from four bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A 32-bit signed integer formed by four bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe int ToInt32(byte* buffer)        {            return (int)buffer[0] << 24 |                   (int)buffer[1] << 16 |                   (int)buffer[2] << 8 |                   (int)buffer[3];        }         /// <summary>        /// Returns a 64-bit signed integer converted from eight bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A 64-bit signed integer formed by eight bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe long ToInt64(byte* buffer)        {            return (long)buffer[0] << 56 |                   (long)buffer[1] << 48 |                   (long)buffer[2] << 40 |                   (long)buffer[3] << 32 |                   (long)buffer[4] << 24 |                   (long)buffer[5] << 16 |                   (long)buffer[6] << 8 |                   (long)buffer[7];        }         /// <summary>        /// Returns a single-precision floating point number converted from four bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A single-precision floating point number formed by four bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe float ToSingle(byte* buffer)        {            int int32 = ToInt32(buffer);            return *(float*)&int32;        }         /// <summary>        /// Returns a 16-bit unsigned integer converted from two bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A 16-bit unsigned integer formed by two bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe ushort ToUInt16(byte* buffer)        {            return (ushort)ToInt16(buffer);        }         /// <summary>        /// Returns a 24-bit unsigned integer converted from three bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A 24-bit unsigned integer formed by three bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe UInt24 ToUInt24(byte* buffer)        {            return (UInt24)((uint)buffer[0] << 16 |                            (uint)buffer[1] << 8 |                            (uint)buffer[2]);        }         /// <summary>        /// Returns a 32-bit unsigned integer converted from four bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A 32-bit unsigned integer formed by four bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe uint ToUInt32(byte* buffer)        {            return (uint)ToInt32(buffer);        }         /// <summary>        /// Returns a 64-bit unsigned integer converted from eight bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A 64-bit unsigned integer formed by eight bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe ulong ToUInt64(byte* buffer)        {            return (ulong)ToInt64(buffer);        }         /// <summary>        /// Returns a 128-bit decimal converted from 16 bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <returns>A 128-bit decimal formed by 16 bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe decimal ToDecimal(byte* buffer)        {            if (!BitConverter.IsLittleEndian)                return *(decimal*)(buffer);             decimal returnValue;            byte* destination = (byte*)&returnValue;            //int flags            destination[0] = buffer[3];            destination[1] = buffer[2];            destination[2] = buffer[1];            destination[3] = buffer[0];            //int high            destination[4] = buffer[7];            destination[5] = buffer[6];            destination[6] = buffer[5];            destination[7] = buffer[4];            //int low            destination[8] = buffer[11];            destination[9] = buffer[10];            destination[10] = buffer[9];            destination[11] = buffer[8];            //int mid            destination[12] = buffer[15];            destination[13] = buffer[14];            destination[14] = buffer[13];            destination[15] = buffer[12];            return returnValue;        }         #endregion          #region [ ToValue Array ]         /// <summary>        /// Returns a <see cref="Boolean"/> value converted from one byte at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes.</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>true if the byte at startIndex in value is nonzero; otherwise, false.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static bool ToBoolean(byte[] buffer, int startIndex)        {            return buffer[startIndex] != 0;        }         /// <summary>        /// Returns a Unicode character converted from two bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A character formed by two bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static char ToChar(byte[] buffer, int startIndex)        {            return (char)ToInt16(buffer, startIndex);        }         /// <summary>        /// Returns a double-precision floating point number converted from eight bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A double-precision floating point number formed by eight bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe double ToDouble(byte[] buffer, int startIndex)        {            long int64 = ToInt64(buffer, startIndex);            return *(double*)&int64;        }         /// <summary>        /// Returns a 16-bit signed integer converted from two bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A 16-bit signed integer formed by two bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static short ToInt16(byte[] buffer, int startIndex)        {            return (short)((int)buffer[startIndex] << 8 | (int)buffer[startIndex + 1]);        }         /// <summary>        /// Returns a 24-bit signed integer converted from three bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A 24-bit signed integer formed by three bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static Int24 ToInt24(byte[] buffer, int startIndex)        {            int int32 = buffer[startIndex + 0] << 16 |                        buffer[startIndex + 1] << 8 |                        buffer[startIndex + 2];             // Check bit 23, the sign bit in a signed 24-bit integer            if ((int32 & 0x00800000) > 0)            {                // If the sign-bit is set, this number will be negative - set all high-byte bits (keeps 32-bit number in 24-bit range)                int32 |= Int24.BitMask;            }            else            {                // If the sign-bit is not set, this number will be positive - clear all high-byte bits (keeps 32-bit number in 24-bit range)                int32 &= ~Int24.BitMask;            }             return (Int24)int32;        }         /// <summary>        /// Returns a 32-bit signed integer converted from four bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A 32-bit signed integer formed by four bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static int ToInt32(byte[] buffer, int startIndex)        {            return (int)buffer[startIndex + 0] << 24 |                   (int)buffer[startIndex + 1] << 16 |                   (int)buffer[startIndex + 2] << 8 |                   (int)buffer[startIndex + 3];        }         /// <summary>        /// Returns a 64-bit signed integer converted from eight bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A 64-bit signed integer formed by eight bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static long ToInt64(byte[] buffer, int startIndex)        {            return (long)buffer[startIndex + 0] << 56 |                   (long)buffer[startIndex + 1] << 48 |                   (long)buffer[startIndex + 2] << 40 |                   (long)buffer[startIndex + 3] << 32 |                   (long)buffer[startIndex + 4] << 24 |                   (long)buffer[startIndex + 5] << 16 |                   (long)buffer[startIndex + 6] << 8 |                   (long)buffer[startIndex + 7];        }         /// <summary>        /// Returns a single-precision floating point number converted from four bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A single-precision floating point number formed by four bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static unsafe float ToSingle(byte[] buffer, int startIndex)        {            int int32 = ToInt32(buffer, startIndex);            return *(float*)&int32;        }         /// <summary>        /// Returns a 16-bit unsigned integer converted from two bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A 16-bit unsigned integer formed by two bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static ushort ToUInt16(byte[] buffer, int startIndex)        {            return (ushort)ToInt16(buffer, startIndex);        }         /// <summary>        /// Returns a 24-bit unsigned integer converted from three bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A 24-bit unsigned integer formed by three bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static UInt24 ToUInt24(byte[] buffer, int startIndex)        {            return (UInt24)((uint)buffer[startIndex + 0] << 16 |                            (uint)buffer[startIndex + 1] << 8 |                            (uint)buffer[startIndex + 2]);        }         /// <summary>        /// Returns a 32-bit unsigned integer converted from four bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A 32-bit unsigned integer formed by four bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static uint ToUInt32(byte[] buffer, int startIndex)        {            return (uint)ToInt32(buffer, startIndex);        }         /// <summary>        /// Returns a 64-bit unsigned integer converted from eight bytes, accounting for target endian-order, at a specified position in a byte array.        /// </summary>        /// <param name="buffer">An array of bytes (i.e., buffer containing binary image of value).</param>        /// <param name="startIndex">The starting position within value.</param>        /// <returns>A 64-bit unsigned integer formed by eight bytes beginning at startIndex.</returns>        /// <exception cref="ArgumentNullException">value is null.</exception>        /// <exception cref="ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception>        [MethodImpl(MethodImplOptions.AggressiveInlining)]        public static ulong ToUInt64(byte[] buffer, int startIndex)        {

Lines 1–500 of 1355.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t