コード例 #1
0
        public SourceText ResolveSource(SourceTextInfo sourceTextInfo)
        {
            var    originalFilePath = sourceTextInfo.OriginalSourceFilePath;
            string?onDiskPath       = null;

            foreach (var link in SourceLinkEntries)
            {
                if (originalFilePath.StartsWith(link.Prefix, FileNameEqualityComparer.StringComparison))
                {
                    onDiskPath = Path.GetFullPath(Path.Combine(Options.SourcePath, originalFilePath.Substring(link.Prefix.Length)));
                    if (File.Exists(onDiskPath))
                    {
                        break;
                    }
                }
            }

            // if no source links exist to let us prefix the source path,
            // then assume the file path in the pdb points to the on-disk location of the file.
            onDiskPath ??= originalFilePath;

            using var fileStream = File.OpenRead(onDiskPath);
            var sourceText = SourceText.From(fileStream, encoding: sourceTextInfo.SourceTextEncoding, checksumAlgorithm: SourceHashAlgorithm.Sha256, canBeEmbedded: false);

            if (!sourceText.GetChecksum().SequenceEqual(sourceTextInfo.Hash))
            {
                throw new Exception($@"File ""{onDiskPath}"" has incorrect hash");
            }

            return(sourceText);
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        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
                           ));
            }
        }
コード例 #4
0
 public SourceText ResolveSourceText(SourceTextInfo sourceTextInfo) =>
 Compilation
 .SyntaxTrees
 .Select(x => x.GetText())
 .Single(x => x.GetChecksum().SequenceEqual(sourceTextInfo.Hash));
コード例 #5
0
 public SourceText ResolveSourceText(SourceTextInfo sourceTextInfo) =>
 SourceResolver.ResolveSource(sourceTextInfo);