/// <summary> /// Get the bytes of this IBitWindow as though the bits in the window were a single N-bit integer with a layout that matches /// that of the current processor. /// Currently restricted to 1,2,4, and 8 byte sized integers. /// Currently restricted to unsigned or 2's complement format. /// </summary> /// <param name="this"></param> /// <param name="integerSize">The byte width of the resulting integer.</param> /// <param name="signed">Whether or not the resulting integer is signed.</param> /// <returns></returns> public static Byte[] Crystalize(this IBitWindow @this, IntegerFormat format) { if (@this == null) { throw new ArgumentNullException(nameof(@this)); } if (!SupportedIntegerFormats.Contains(format)) { throw new NotSupportedException(); } if (format.BitWidth / 8 < (@this.Length + 7) / 8) { throw new InvalidOperationException(); } Byte[] integerBuffer = new Byte[format.BitWidth / 8]; BitWindow integerView = new BitWindow(integerBuffer, new EndianBitIndexer(format.ByteOrder, format.BitOrder)); @this.WriteTo(integerView, 0, 0, @this.Length); if (format.Signed) { if (@this[@this.Length - 1]) { for (int bitPow = @this.Length; bitPow < integerView.Length; bitPow++) { integerView[bitPow] = true; } } } return(integerBuffer); }
/// <summary> /// Read from another <code>IBitView</code> /// </summary> /// <param name="src">The view to read from.</param> /// <param name="srcOffset">Where to start reading from.</param> /// <param name="dstOffset">Where to start writing to.</param> /// <param name="length">The number of bits to transfer.</param> public void ReadFrom(IBitWindow src, Int32 srcOffset, Int32 dstOffset, Int32 length) { if (srcOffset < 0) { throw new ArgumentOutOfRangeException(nameof(srcOffset)); } if (dstOffset < 0) { throw new ArgumentOutOfRangeException(nameof(dstOffset)); } Int32 bitsRequiredSrc = srcOffset + length; Int32 bitsRequiredDst = dstOffset + length; Int32 bitsInSrc = src.Length; if (bitsInSrc < bitsRequiredSrc) { throw new ArgumentOutOfRangeException(nameof(dstOffset)); } if (Length < bitsRequiredDst) { throw new ArgumentOutOfRangeException(nameof(srcOffset)); } for (Int32 offset = 0; offset < length; offset++) { this[offset + dstOffset] = src[offset + srcOffset]; } }
/// <summary> /// Writes to another <code>IBitView</code>. /// </summary> /// <param name="dst">The view to write to.</param> /// <param name="srcOffset">Where to start reading from.</param> /// <param name="dstOffset">Where to start writing to.</param> /// <param name="length">The number of bits to transfer.</param> public void WriteTo(IBitWindow dst, Int32 srcOffset, Int32 dstOffset, Int32 length) { if (srcOffset < 0) { throw new ArgumentOutOfRangeException(nameof(srcOffset)); } if (dstOffset < 0) { throw new ArgumentOutOfRangeException(nameof(dstOffset)); } Int32 bitsRequiredSrc = srcOffset + length; Int32 bitsRequiredDst = dstOffset + length; Int32 bitsInDst = dst.Length; if (bitsInDst < bitsRequiredDst) { throw new ArgumentOutOfRangeException(nameof(dstOffset)); } if (Length < bitsRequiredSrc) { throw new ArgumentOutOfRangeException(nameof(srcOffset)); } for (Int32 offset = 0; offset < length; offset++) { dst[offset + dstOffset] = this[offset + srcOffset]; } }
/// <summary> /// Creates a sequence of views into an existing view. /// </summary> /// <param name="this">The existing view.</param> /// <param name="sizes">The sequence of view sizes.</param> /// <returns></returns> public static IEnumerable <IBitWindow> Shatter(this IBitWindow @this, IEnumerable <Int32> sizes) { Int32 pos = 0; foreach (var size in sizes) { yield return(@this.Crack(pos, size, @this.Indexer)); pos += size; } }
/// <summary> /// Creates a sequence of formatted views into an existing view. /// </summary> /// <param name="this">The existing view.</param> /// <param name="formats">The sequence of formats that the new views will have.</param> /// <returns></returns> public static IEnumerable <IBitWindow> Shatter(this IBitWindow @this, IEnumerable <IntegerFormat> formats) { Int32 pos = 0; foreach (var fmt in formats) { yield return(@this.Crack(pos, fmt.BitWidth, fmt.GetIndexer())); pos += fmt.BitWidth; } }
/// <summary> /// Creates a view into an existing view. /// </summary> /// <param name="view">The existing view.</param> /// <param name="offset">The bit offset relative to the start of the existing view.</param> /// <param name="length">The length of the new view.</param> /// <param name="indexer">The bit indexer to use.</param> /// <returns></returns> public static IBitWindow Crack(this IBitWindow view, Int32 offset, Int32 length, IBitIndexer indexer) { return(new BitWindow(view.Source, view.Offset + offset, length, indexer)); }
/// <summary> /// Creates a view into an existing view. /// </summary> /// <param name="this">The existing view.</param> /// <param name="offset">The bit offset relative to the start of the existing view.</param> /// <param name="format">A format to follow.</param> /// <returns></returns> public static IBitWindow Crack(this IBitWindow @this, Int32 offset, IntegerFormat format) { return(@this.Crack(offset, format.BitWidth, format.GetIndexer())); }
public static void ReadFrom(this IBitWindow view, Byte[] src, Int32 srcOffset, Int32 dstOffset, Int32 length, IBitIndexer indexer) { IBitWindow srcWindow = new BitWindow(src, indexer); view.ReadFrom(srcWindow, srcOffset, dstOffset, length); }
public static void WriteTo(this IBitWindow view, Byte[] dst, Int32 srcOffset, Int32 dstOffset, Int32 length, IBitIndexer indexer) { IBitWindow dstWindow = new BitWindow(dst, indexer); view.WriteTo(dstWindow, srcOffset, dstOffset, length); }