BlobHeap.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // Author:
  3. // Jb Evain (jbevain@gmail.com)
  4. //
  5. // Copyright (c) 2008 - 2015 Jb Evain
  6. // Copyright (c) 2008 - 2011 Novell, Inc.
  7. //
  8. // Licensed under the MIT/X11 license.
  9. //
  10. using System;
  11. namespace ILRuntime.Mono.Cecil.Metadata {
  12. sealed class BlobHeap : Heap {
  13. public BlobHeap (byte [] data)
  14. : base (data)
  15. {
  16. }
  17. public byte [] Read (uint index)
  18. {
  19. if (index == 0 || index > this.data.Length - 1)
  20. return Empty<byte>.Array;
  21. int position = (int) index;
  22. int length = (int) data.ReadCompressedUInt32 (ref position);
  23. if (length > data.Length - position)
  24. return Empty<byte>.Array;
  25. var buffer = new byte [length];
  26. Buffer.BlockCopy (data, position, buffer, 0, length);
  27. return buffer;
  28. }
  29. public void GetView (uint signature, out byte [] buffer, out int index, out int length)
  30. {
  31. if (signature == 0 || signature > data.Length - 1) {
  32. buffer = null;
  33. index = length = 0;
  34. return;
  35. }
  36. buffer = data;
  37. index = (int) signature;
  38. length = (int) buffer.ReadCompressedUInt32 (ref index);
  39. }
  40. }
  41. }