private IEnumerable <(DocumentHandle DocumentHandle, SourceTextInfo SourceTextInfo)> GetSourceTextInfoCore()
        {
            var encoding        = GetEncoding();
            var sourceFileCount = GetSourceFileCount();

            foreach (var documentHandle in PdbReader.Documents.Take(sourceFileCount))
            {
                var document = PdbReader.GetDocument(documentHandle);
                var name     = PdbReader.GetString(document.Name);

                var hashAlgorithmGuid = PdbReader.GetGuid(document.HashAlgorithm);
                var hashAlgorithm     =
                    hashAlgorithmGuid == HashAlgorithmSha1
                        ? SourceHashAlgorithm.Sha1
                        : hashAlgorithmGuid == HashAlgorithmSha256
                            ? SourceHashAlgorithm.Sha256
                            : SourceHashAlgorithm.None;

                var hash           = PdbReader.GetBlobBytes(document.Hash);
                var sourceTextInfo = new SourceTextInfo(
                    name,
                    hashAlgorithm,
                    hash.ToImmutableArray(),
                    encoding
                    );
                yield return(documentHandle, sourceTextInfo);
            }
        }
        private EmbeddedSourceTextInfo?ResolveEmbeddedSource(
            DocumentHandle document,
            SourceTextInfo sourceTextInfo
            )
        {
            byte[] bytes = (
                from handle in PdbReader.GetCustomDebugInformation(document)
                let cdi = PdbReader.GetCustomDebugInformation(handle)
                          where PdbReader.GetGuid(cdi.Kind) == EmbeddedSourceGuid
                          select PdbReader.GetBlobBytes(cdi.Value)
                ).SingleOrDefault();

            if (bytes is null)
            {
                return(null);
            }

            int uncompressedSize = BitConverter.ToInt32(bytes, 0);
            var stream           = new MemoryStream(bytes, sizeof(int), bytes.Length - sizeof(int));

            byte[]? compressedHash = null;
            if (uncompressedSize != 0)
            {
                using var algorithm =
                          CryptographicHashProvider.TryGetAlgorithm(sourceTextInfo.HashAlgorithm)
                          ?? throw new InvalidOperationException();
                compressedHash = algorithm.ComputeHash(bytes);

                var decompressed = new MemoryStream(uncompressedSize);

                using (var deflater = new DeflateStream(stream, CompressionMode.Decompress))
                {
                    deflater.CopyTo(decompressed);
                }

                if (decompressed.Length != uncompressedSize)
                {
                    throw new InvalidDataException();
                }

                stream = decompressed;
            }

            using (stream)
            {
                // todo: IVT and EncodedStringText.Create?
                var embeddedText = SourceText.From(
                    stream,
                    encoding: sourceTextInfo.SourceTextEncoding,
                    checksumAlgorithm: sourceTextInfo.HashAlgorithm,
                    canBeEmbedded: true
                    );
                return(new EmbeddedSourceTextInfo(
                           sourceTextInfo,
                           embeddedText,
                           compressedHash?.ToImmutableArray() ?? ImmutableArray <byte> .Empty
                           ));
            }
        }
Exemple #3
0
        private (SourceText?embeddedText, byte[]? compressedHash) ResolveEmbeddedSource(DocumentHandle document, SourceHashAlgorithm hashAlgorithm, Encoding encoding)
        {
            byte[] bytes = (from handle in PdbReader.GetCustomDebugInformation(document)
                            let cdi = PdbReader.GetCustomDebugInformation(handle)
                                      where PdbReader.GetGuid(cdi.Kind) == EmbeddedSourceGuid
                                      select PdbReader.GetBlobBytes(cdi.Value)).SingleOrDefault();

            if (bytes == null)
            {
                return(default);