Beispiel #1
0
        /// <summary>Byte swap a given structure a number of times over a range of bytes</summary>
        /// <param name="definition">Structure definition in terms of byte swap codes</param>
        /// <param name="buffer">Buffer containing the bytes of an instance of the definition</param>
        /// <param name="startIndex">Where to start processing the definition in the buffer</param>
        /// <param name="count">Number of times to process the definition on the buffer</param>
        /// <returns>Offset in <paramref name="buffer"/> where processing ended</returns>
        public static int SwapData(IByteSwappable definition, byte[] buffer,
                                   int startIndex = 0, int count = 1)
        {
            Contract.Requires <ArgumentNullException>(definition != null);
            Contract.Requires <ArgumentNullException>(buffer != null);
            Contract.Requires <ArgumentOutOfRangeException>(startIndex >= 0);
            Contract.Requires <ArgumentOutOfRangeException>(startIndex <= buffer.Length);
            Contract.Requires <ArgumentOutOfRangeException>(count > 0);
            Contract.Requires <ArgumentOutOfRangeException>((count * definition.SizeOf) <= (buffer.Length - startIndex),
                                                            "buffer doesn't have enough data for the given byte swap parameters");
            Contract.Ensures(Contract.Result <int>() >= 0);

            if (count == 0)
            {
                return(startIndex);
            }

            var swap         = new Swapper(definition);
            int buffer_index = startIndex;

            for (int elements_remaining = count; elements_remaining > 0; elements_remaining--)
            {
                buffer_index = swap.SwapData(buffer, buffer_index);
            }

            Contract.Assert(buffer_index == (startIndex + (definition.SizeOf * count)));
            return(buffer_index);
        }
            public Swapper(IByteSwappable definition)
            {
                Contract.Requires <ArgumentNullException>(definition != null);

                kCodes = definition.ByteSwapCodes;
            }