/Mono.Cecil.Metadata/BlobHeap.cs

http://github.com/jbevain/cecil · C# · 54 lines · 32 code · 13 blank · 9 comment · 7 complexity · 0dbc76eb241e39ee93a549fc37024dd5 MD5 · raw file

  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 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. }