/* Copyright (C) 2018 de4dot@gmail.com This file is part of Iced. Iced is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Iced is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with Iced. If not, see . */ #if (!NO_DECODER32 || !NO_DECODER64) && !NO_DECODER using System; namespace Iced.Intel { /// /// A that reads data from a byte array /// public sealed class ByteArrayCodeReader : CodeReader { readonly byte[] data; int currentPosition; readonly int endPosition; /// /// Checks if it's possible to read another byte /// public bool CanReadByte => currentPosition < endPosition; /// /// Constructor /// /// Hex bytes encoded in a string public ByteArrayCodeReader(string hexData) : this(HexUtils.ToByteArray(hexData)) { } /// /// Constructor /// /// Data public ByteArrayCodeReader(byte[] data) { this.data = data ?? throw new ArgumentNullException(nameof(data)); currentPosition = 0; endPosition = data.Length; } /// /// Constructor /// /// Data /// Start index /// Number of bytes public ByteArrayCodeReader(byte[] data, int index, int count) { this.data = data ?? throw new ArgumentNullException(nameof(data)); if (index < 0) throw new ArgumentOutOfRangeException(nameof(index)); if (count < 0) throw new ArgumentOutOfRangeException(nameof(count)); if ((ulong)(uint)index + (uint)count > (uint)data.Length) throw new ArgumentOutOfRangeException(nameof(count)); currentPosition = index; endPosition = index + count; } /// /// Reads the next byte or returns less than 0 if there are no more bytes /// /// public override int ReadByte() { if (currentPosition >= endPosition) return -1; return data[currentPosition++]; } } } #endif