Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing/inserting bytes to a hex editor #10

Open
kb-1000 opened this issue Nov 10, 2024 · 3 comments
Open

Removing/inserting bytes to a hex editor #10

kb-1000 opened this issue Nov 10, 2024 · 3 comments

Comments

@kb-1000
Copy link

kb-1000 commented Nov 10, 2024

CanRemove and RemoveBytes exist, but appear to be unused.

@kb-1000
Copy link
Author

kb-1000 commented Nov 11, 2024

Inserting also seems to behave quite weirdly in general - so currently more or less everything but viewing and editing existing bytes appears to be broken. Unfortunately there's also no builtin resizable IBinaryDocument, though currently there wouldn't be much of a point anyways. I created my own using a List<byte>, could PR it if this gets fixed.
This screenshot shows some (but not all) of the weirdness, created by repeatedly typing A in varying locations:
image
It only really lets you edit one of the two half-bytes in insert mode. It also doesn't let you place the caret behind the last half-byte, so you can't append bytes.

DynamicBinaryDocument

        public class DynamicBinaryDocument : IBinaryDocument
        {
            private readonly List<byte> _data;

            public event EventHandler<BinaryDocumentChange>? Changed;

            public DynamicBinaryDocument(byte[] data)
            {
                _data = [..data];
                _validRanges = new BitRangeUnion([new BitRange(0UL, Length)]);
            }
    
            public ulong Length => (ulong)_data.Count;
            public bool IsReadOnly => false;
            public bool CanInsert => true;
            public bool CanRemove => true;
            private readonly BitRangeUnion _validRanges;
            public IReadOnlyBitRangeUnion ValidRanges => _validRanges;

            public void ReadBytes(ulong offset, Span<byte> buffer)
            {
                CollectionsMarshal.AsSpan(_data).Slice((int)offset, buffer.Length).CopyTo(buffer);
            }

            public void WriteBytes(ulong offset, ReadOnlySpan<byte> buffer)
            {
                buffer.CopyTo(CollectionsMarshal.AsSpan(_data).Slice((int)offset, buffer.Length));
                OnChanged(new BinaryDocumentChange(BinaryDocumentChangeType.Modify, new BitRange(offset, offset + (ulong) buffer.Length)));
            }

            public void InsertBytes(ulong offset, ReadOnlySpan<byte> buffer)
            {
                _data.InsertRange((int)offset, buffer);
                _validRanges.Add(new BitRange(Length - (ulong)buffer.Length, Length));
                OnChanged(new BinaryDocumentChange(BinaryDocumentChangeType.Insert, new BitRange(offset, offset + (ulong) buffer.Length)));
            }

            public void RemoveBytes(ulong offset, ulong length)
            {
                _data.RemoveRange((int)offset, (int)length);
                _validRanges.Remove(new BitRange(Length, Length + length));
                OnChanged(new BinaryDocumentChange(BinaryDocumentChangeType.Insert, new BitRange(offset, offset + length)));
            }

            protected virtual void OnChanged(BinaryDocumentChange e)
            {
                Changed?.Invoke(this, e);
            }
        }

@kb-1000 kb-1000 changed the title Removing bytes from a hex editor Removing/inserting bytes to a hex editor Nov 11, 2024
@Washi1337
Copy link
Owner

This is correct. Currently the control only fully supports viewing and editing existing bytes. This is also one of the reasons there is no dynamically resizable binary document implementation yet. The interface methods are currently there as a WIP / future work.

Ideally, for a dynamically resizable document we should not use List<byte>, as this would result in insert and remove becoming O(n) operations, which is really bad for large documents of several 100s of megabytes to gigabytes.

@Washi1337
Copy link
Owner

Status update: Initial implementation of dynamic sized documents can be found in #11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants