예제 #1
0
        public void DangerousCreateFromUnderlyingArrayNegativeTests()
        {
            byte[] array = null;
            ImmutableArray <byte> immutable = ImmutableByteArrayInterop.DangerousCreateFromUnderlyingArray(ref array);

            Assert.True(immutable.IsDefault);
        }
예제 #2
0
        // internal for testing
        internal static unsafe ImmutableArray <byte> DecodeEmbeddedPortablePdbDebugDirectoryData(AbstractMemoryBlock block)
        {
            byte[] decompressed;

            const int headerSize = 2 * sizeof(int);

            var headerReader = new BlobReader(block.Pointer, headerSize);

            if (headerReader.ReadUInt32() != PortablePdbVersions.DebugDirectoryEmbeddedSignature)
            {
                throw new BadImageFormatException(SR.UnexpectedEmbeddedPortablePdbDataSignature);
            }

            int decompressedSize = headerReader.ReadInt32();

            try
            {
                decompressed = new byte[decompressedSize];
            }
            catch
            {
                throw new BadImageFormatException(SR.DataTooBig);
            }

            var compressed = new ReadOnlyUnmanagedMemoryStream(block.Pointer + headerSize, block.Size - headerSize);
            var deflate    = new DeflateStream(compressed, CompressionMode.Decompress, leaveOpen: true);

            if (decompressedSize > 0)
            {
                int actualLength;

                try
                {
                    actualLength = deflate.TryReadAll(decompressed, 0, decompressed.Length);
                }
                catch (InvalidDataException e)
                {
                    throw new BadImageFormatException(e.Message, e.InnerException);
                }

                if (actualLength != decompressed.Length)
                {
                    throw new BadImageFormatException(SR.SizeMismatch);
                }
            }

            // Check that there is no more compressed data left,
            // in case the decompressed size specified in the header is smaller
            // than the actual decompressed size of the data.
            if (deflate.ReadByte() != -1)
            {
                throw new BadImageFormatException(SR.SizeMismatch);
            }

            return(ImmutableByteArrayInterop.DangerousCreateFromUnderlyingArray(ref decompressed));
        }
예제 #3
0
        // internal for testing
        internal static PdbChecksumDebugDirectoryData DecodePdbChecksumDebugDirectoryData(AbstractMemoryBlock block)
        {
            var reader = block.GetReader();

            var algorithmName = reader.ReadUtf8NullTerminated();

            byte[]? checksum = reader.ReadBytes(reader.RemainingBytes);
            if (algorithmName.Length == 0 || checksum.Length == 0)
            {
                throw new BadImageFormatException(SR.InvalidPdbChecksumDataFormat);
            }

            return(new PdbChecksumDebugDirectoryData(
                       algorithmName,
                       ImmutableByteArrayInterop.DangerousCreateFromUnderlyingArray(ref checksum)));
        }
예제 #4
0
        public void DangerousCreateFromUnderlyingArray()
        {
            byte[] array = new byte[3] {
                1, 2, 3
            };
            byte[] arrayCopy = array;
            ImmutableArray <byte> immutable = ImmutableByteArrayInterop.DangerousCreateFromUnderlyingArray(ref array);

            // DangerousCreateFromUnderlyingArray clears the given parameter as a signal that
            // the mutable array should no longer be modified through mutable references.
            Assert.Null(array);

            Assert.Equal(3, immutable.Length);
            Assert.Equal(1, immutable[0]);
            Assert.Equal(2, immutable[1]);
            Assert.Equal(3, immutable[2]);

            arrayCopy[0] = 9;

            Assert.Equal(9, immutable[0]);
        }
예제 #5
0
 public ImmutableArray <byte> GetILContent()
 {
     byte[] bytes = GetILBytes();
     return(ImmutableByteArrayInterop.DangerousCreateFromUnderlyingArray(ref bytes));
 }
예제 #6
0
        /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name="byteCount"/> falls outside of the bounds of the buffer content.</exception>
        /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with another one.</exception>
        public ImmutableArray <byte> ToImmutableArray(int start, int byteCount)
        {
            var array = ToArray(start, byteCount);

            return(ImmutableByteArrayInterop.DangerousCreateFromUnderlyingArray(ref array));
        }
예제 #7
0
 public static ImmutableArray <byte> ReadImmutableBytes(byte *buffer, int byteCount)
 {
     byte[] bytes = ReadBytes(buffer, byteCount);
     return(ImmutableByteArrayInterop.DangerousCreateFromUnderlyingArray(ref bytes));
 }