Exemplo n.º 1
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));
        }
        internal static unsafe ImmutableArray <byte> DecodeEmbeddedPortablePdbDebugDirectoryData(AbstractMemoryBlock block)
        {
            byte[] decompressed;

            var headerReader = block.GetReader();

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

            int decompressedSize = headerReader.ReadInt32();

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

            var compressed = new ReadOnlyUnmanagedMemoryStream(headerReader.CurrentPointer, headerReader.RemainingBytes);
            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("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("SizeMismatch");
            }

            return(new ImmutableArray <byte>(decompressed));
        }
Exemplo n.º 3
0
        // internal for testing
        internal static unsafe NativeHeapMemoryBlock DecodeEmbeddedPortablePdbDebugDirectoryData(AbstractMemoryBlock block)
        {
            NativeHeapMemoryBlock?decompressed;

            var headerReader = block.GetReader();

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

            int decompressedSize = headerReader.ReadInt32();

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

            bool success = false;

            try
            {
                var compressed = new ReadOnlyUnmanagedMemoryStream(headerReader.CurrentPointer, headerReader.RemainingBytes);
                var deflate    = new DeflateStream(compressed, CompressionMode.Decompress, leaveOpen: true);

                if (decompressedSize > 0)
                {
                    int actualLength;

                    try
                    {
#if NETCOREAPP3_0_OR_GREATER
                        actualLength = deflate.TryReadAll(new Span <byte>(decompressed.Pointer, decompressed.Size));
#else
                        using var decompressedStream = new UnmanagedMemoryStream(decompressed.Pointer, decompressed.Size, decompressed.Size, FileAccess.Write);
                        deflate.CopyTo(decompressedStream);
                        actualLength = (int)decompressedStream.Position;
#endif
                    }
                    catch (Exception e)
                    {
                        throw new BadImageFormatException(e.Message, e.InnerException);
                    }

                    if (actualLength != decompressed.Size)
                    {
                        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);
                }

                success = true;
            }
            finally
            {
                if (!success)
                {
                    decompressed.Dispose();
                }
            }

            return(decompressed);
        }