| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Builder of a Metadata Root to be embedded in a Portable Executable image. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Metadata root constitutes of a metadata header followed by metadata streams (#~, #Strings, #US, #Guid and #Blob) |
| | | 15 | | /// </remarks> |
| | | 16 | | public sealed class MetadataRootBuilder |
| | | 17 | | { |
| | | 18 | | private const string DefaultMetadataVersionString = "v4.0.30319"; |
| | | 19 | | |
| | | 20 | | // internal for testing |
| | 0 | 21 | | internal static readonly ImmutableArray<int> EmptyRowCounts = ImmutableArray.Create(new int[MetadataTokens.Table |
| | | 22 | | |
| | | 23 | | private readonly MetadataBuilder _tablesAndHeaps; |
| | | 24 | | private readonly SerializedMetadata _serializedMetadata; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Metadata version string. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public string MetadataVersion { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// True to suppresses basic validation of metadata tables. |
| | | 33 | | /// The validation verifies that entries in the tables were added in order required by the ECMA specification. |
| | | 34 | | /// It does not enforce all specification requirements on metadata tables. |
| | | 35 | | /// </summary> |
| | 0 | 36 | | public bool SuppressValidation { get; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Creates a builder of a metadata root. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="tablesAndHeaps"> |
| | | 42 | | /// Builder populated with metadata entities stored in tables and values stored in heaps. |
| | | 43 | | /// The entities and values will be enumerated when serializing the metadata root. |
| | | 44 | | /// </param> |
| | | 45 | | /// <param name="metadataVersion"> |
| | | 46 | | /// The version string written to the metadata header. The default value is "v4.0.30319". |
| | | 47 | | /// </param> |
| | | 48 | | /// <param name="suppressValidation"> |
| | | 49 | | /// True to suppresses basic validation of metadata tables during serialization. |
| | | 50 | | /// The validation verifies that entries in the tables were added in order required by the ECMA specification. |
| | | 51 | | /// It does not enforce all specification requirements on metadata tables. |
| | | 52 | | /// </param> |
| | | 53 | | /// <exception cref="ArgumentNullException"><paramref name="tablesAndHeaps"/> is null.</exception> |
| | | 54 | | /// <exception cref="ArgumentException"><paramref name="metadataVersion"/> is too long (the number of bytes when |
| | 0 | 55 | | public MetadataRootBuilder(MetadataBuilder tablesAndHeaps, string? metadataVersion = null, bool suppressValidati |
| | 0 | 56 | | { |
| | 0 | 57 | | if (tablesAndHeaps is null) |
| | 0 | 58 | | { |
| | 0 | 59 | | Throw.ArgumentNull(nameof(tablesAndHeaps)); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | Debug.Assert(Encoding.UTF8.GetByteCount(DefaultMetadataVersionString) == DefaultMetadataVersionString.Length |
| | 0 | 63 | | int metadataVersionByteCount = metadataVersion != null ? Encoding.UTF8.GetByteCount(metadataVersion) : Defau |
| | | 64 | | |
| | 0 | 65 | | if (metadataVersionByteCount > MetadataSizes.MaxMetadataVersionByteCount) |
| | 0 | 66 | | { |
| | 0 | 67 | | Throw.InvalidArgument(SR.MetadataVersionTooLong, nameof(metadataVersion)); |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | _tablesAndHeaps = tablesAndHeaps; |
| | 0 | 71 | | MetadataVersion = metadataVersion ?? DefaultMetadataVersionString; |
| | 0 | 72 | | SuppressValidation = suppressValidation; |
| | 0 | 73 | | _serializedMetadata = tablesAndHeaps.GetSerializedMetadata(EmptyRowCounts, metadataVersionByteCount, isStand |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Returns sizes of various metadata structures. |
| | | 78 | | /// </summary> |
| | 0 | 79 | | public MetadataSizes Sizes => _serializedMetadata.Sizes; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Serializes metadata root content into the given <see cref="BlobBuilder"/>. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="builder">Builder to write to.</param> |
| | | 85 | | /// <param name="methodBodyStreamRva"> |
| | | 86 | | /// The relative virtual address of the start of the method body stream. |
| | | 87 | | /// Used to calculate the final value of RVA fields of MethodDef table. |
| | | 88 | | /// </param> |
| | | 89 | | /// <param name="mappedFieldDataStreamRva"> |
| | | 90 | | /// The relative virtual address of the start of the field init data stream. |
| | | 91 | | /// Used to calculate the final value of RVA fields of FieldRVA table. |
| | | 92 | | /// </param> |
| | | 93 | | /// <exception cref="ArgumentNullException"><paramref name="builder"/> is null.</exception> |
| | | 94 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="methodBodyStreamRva"/> or <paramref name="mapp |
| | | 95 | | /// <exception cref="InvalidOperationException"> |
| | | 96 | | /// A metadata table is not ordered as required by the specification and <see cref="SuppressValidation"/> is fal |
| | | 97 | | /// </exception> |
| | | 98 | | public void Serialize(BlobBuilder builder, int methodBodyStreamRva, int mappedFieldDataStreamRva) |
| | 0 | 99 | | { |
| | 0 | 100 | | if (builder is null) |
| | 0 | 101 | | { |
| | 0 | 102 | | Throw.ArgumentNull(nameof(builder)); |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | if (methodBodyStreamRva < 0) |
| | 0 | 106 | | { |
| | 0 | 107 | | Throw.ArgumentOutOfRange(nameof(methodBodyStreamRva)); |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | if (mappedFieldDataStreamRva < 0) |
| | 0 | 111 | | { |
| | 0 | 112 | | Throw.ArgumentOutOfRange(nameof(mappedFieldDataStreamRva)); |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | if (!SuppressValidation) |
| | 0 | 116 | | { |
| | 0 | 117 | | _tablesAndHeaps.ValidateOrder(); |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | // header: |
| | 0 | 121 | | MetadataBuilder.SerializeMetadataHeader(builder, MetadataVersion, _serializedMetadata.Sizes); |
| | | 122 | | |
| | | 123 | | // #~ or #- stream: |
| | 0 | 124 | | _tablesAndHeaps.SerializeMetadataTables(builder, _serializedMetadata.Sizes, _serializedMetadata.StringMap, m |
| | | 125 | | |
| | | 126 | | // #Strings, #US, #Guid and #Blob streams: |
| | 0 | 127 | | _tablesAndHeaps.WriteHeapsTo(builder, _serializedMetadata.StringHeap); |
| | 0 | 128 | | } |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |