Exemple #1
0
        /// <summary>
        /// Test data buffer against a specific value
        /// </summary>
        /// <param name="data">Data to check</param>
        /// <param name="offset">Where to start in data</param>
        /// <param name="value">Specific value to look for</param>
        /// <param name="minlen">Minimum length of check (default=1)</param>
        /// <param name="maxlen">Maximum length of check (default=8)</param>
        /// <param name="sign">Which type to check: -1:signed, 0:both, +1:unsigned</param>
        /// <returns>Type discovered, or null if no match found</returns>
        public static Type IsOfValue(IAccessor <byte> data, long offset, long value, int minlen = 1, int maxlen = 8, int sign = 0)
        {
            if (minlen <= 8 && maxlen >= 8 && offset + 8 <= data.LongCount)
            {
                if (sign >= 0 && SystemType.FromData <ulong>(data, offset) == (ulong)value)
                {
                    return(typeof(ulong));
                }

                if (sign <= 0 && SystemType.FromData <long>(data, offset) == value)
                {
                    return(typeof(long));
                }
            }

            if (minlen <= 4 && maxlen >= 4 && value <= uint.MaxValue && offset + 4 <= data.LongCount)
            {
                if (sign >= 0 && SystemType.FromData <uint>(data, offset) == value)
                {
                    return(typeof(uint));
                }

                if (sign <= 0 && value <= int.MaxValue)
                {
                    if (SystemType.FromData <int>(data, offset) == value)
                    {
                        return(typeof(int));
                    }
                }
            }

            if (minlen <= 2 && maxlen >= 2 && value < ushort.MaxValue && offset + 2 <= data.LongCount)
            {
                if (sign >= 0 && SystemType.FromData <ushort>(data, offset) == value)
                {
                    return(typeof(ushort));
                }

                if (sign <= 0 && value <= short.MaxValue)
                {
                    if (SystemType.FromData <short>(data, offset) == value)
                    {
                        return(typeof(short));
                    }
                }
            }

            if (minlen <= 1 && maxlen >= 1 && value < byte.MaxValue && offset + 1 <= data.LongCount)
            {
                if (sign >= 0 && SystemType.FromData <byte>(data, offset) == value)
                {
                    return(typeof(byte));
                }

                if (sign <= 0 && value <= sbyte.MaxValue)
                {
                    if (SystemType.FromData <sbyte>(data, offset) == value)
                    {
                        return(typeof(sbyte));
                    }
                }
            }

            return(null);
        }