Exemplo n.º 1
0
        public void ProperStreamRead()
        {
            var firstRead    = true;
            var sourceArray  = new byte[] { 1, 2, 3, 4 };
            int sourceOffset = 0;

            var stream = new TestStream(readFunc: (buf, offset, count) =>
            {
                if (firstRead)
                {
                    count     = count / 2;
                    firstRead = false;
                }
                Array.Copy(sourceArray, sourceOffset, buf, offset, count);
                sourceOffset += count;
                return(count);
            });

            var builder = PooledBlobBuilder.GetInstance(sourceArray.Length);

            Assert.Equal(sourceArray.Length, builder.TryWriteBytes(stream, sourceArray.Length));
            Assert.Equal(sourceArray, builder.ToArray());

            builder.Free();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Encodes a string using UTF8 encoding to a blob and adds it to the Blob heap, if it's not there already.
        /// </summary>
        /// <param name="value">Constant value.</param>
        /// <param name="allowUnpairedSurrogates">
        /// True to encode unpaired surrogates as specified, otherwise replace them with U+FFFD character.
        /// </param>
        /// <returns>Handle to the added or existing blob.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
        public BlobHandle GetOrAddBlobUTF8(string value, bool allowUnpairedSurrogates = true)
        {
            var builder = PooledBlobBuilder.GetInstance();

            builder.WriteUTF8(value, allowUnpairedSurrogates);
            var handle = GetOrAddBlob(builder);

            builder.Free();
            return(handle);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Encodes a string using UTF16 encoding to a blob and adds it to the Blob heap, if it's not there already.
        /// </summary>
        /// <param name="value">String.</param>
        /// <returns>Handle to the added or existing blob.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
        public BlobHandle GetOrAddBlobUTF16(string value)
        {
            var builder = PooledBlobBuilder.GetInstance();

            builder.WriteUTF16(value);
            var handle = GetOrAddBlob(builder);

            builder.Free();
            return(handle);
        }
Exemplo n.º 4
0
        public void PrematureEndOfStream()
        {
            var sourceArray = new byte[] { 1, 2, 3, 4 };
            var stream      = new MemoryStream(sourceArray);

            var destArray = new byte[6];
            var builder   = PooledBlobBuilder.GetInstance(destArray.Length);

            // Try to write more bytes than exist in the stream
            Assert.Equal(4, builder.TryWriteBytes(stream, 6));

            Assert.Equal(sourceArray, builder.ToArray());

            builder.Free();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Encodes a constant value to a blob and adds it to the Blob heap, if it's not there already.
        /// Uses UTF16 to encode string constants.
        /// </summary>
        /// <param name="value">Constant value.</param>
        /// <returns>Handle to the added or existing blob.</returns>
        public unsafe BlobHandle GetOrAddConstantBlob(object?value)
        {
            if (value is string str)
            {
                return(GetOrAddBlobUTF16(str));
            }

            var builder = PooledBlobBuilder.GetInstance();

            builder.WriteConstant(value);
            var result = GetOrAddBlob(builder);

            builder.Free();
            return(result);
        }
Exemplo n.º 6
0
        public void Pooled()
        {
            var builder1 = PooledBlobBuilder.GetInstance();
            var builder2 = PooledBlobBuilder.GetInstance();
            var builder3 = new BlobBuilder();

            builder1.WriteByte(1);
            builder2.WriteByte(2);
            builder3.WriteByte(3);

            // mix pooled with non-pooled
            builder1.LinkPrefix(builder3);

            builder1.Free();
            builder2.Free();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Encodes a debug document name and adds it to the Blob heap, if it's not there already.
        /// </summary>
        /// <param name="value">Document name.</param>
        /// <returns>
        /// Handle to the added or existing document name blob
        /// (see https://github.com/dotnet/corefx/blob/master/src/System.Reflection.Metadata/specs/PortablePdb-Metadata.md#DocumentNameBlob).
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
        public BlobHandle GetOrAddDocumentName(string value)
        {
            if (value == null)
            {
                Throw.ArgumentNull(nameof(value));
            }

            char separator = ChooseSeparator(value);

            var resultBuilder = PooledBlobBuilder.GetInstance();

            resultBuilder.WriteByte((byte)separator);

            var partBuilder = PooledBlobBuilder.GetInstance();

            int i = 0;

            while (true)
            {
                int next = value.IndexOf(separator, i);

                partBuilder.WriteUTF8(value, i, (next >= 0 ? next : value.Length) - i, allowUnpairedSurrogates: true, prependSize: false);
                resultBuilder.WriteCompressedInteger(GetOrAddBlob(partBuilder).GetHeapOffset());

                if (next == -1)
                {
                    break;
                }

                if (next == value.Length - 1)
                {
                    // trailing separator:
                    resultBuilder.WriteByte(0);
                    break;
                }

                partBuilder.Clear();
                i = next + 1;
            }

            partBuilder.Free();

            var resultHandle = GetOrAddBlob(resultBuilder);

            resultBuilder.Free();
            return(resultHandle);
        }
Exemplo n.º 8
0
        public BlobHandle GetOrAddConstantBlob(object value)
        {
            string str = value as string;

            if (str != null)
            {
                return(GetOrAddBlob(str));
            }

            var builder = PooledBlobBuilder.GetInstance();

            builder.WriteConstant(value);
            var result = GetOrAddBlob(builder);

            builder.Free();
            return(result);
        }
Exemplo n.º 9
0
 /// <summary>
 /// Write one entry in the "Debug Directory (Image Only)"
 /// See https://msdn.microsoft.com/en-us/windows/hardware/gg463119.aspx
 /// section 5.1.1 (pages 71-72).
 /// </summary>
 private static void WriteDebugTableEntry(
     PooledBlobBuilder writer,
     byte[] stamp,
     uint version, // major and minor version, combined
     uint debugType,
     uint sizeOfData,
     uint addressOfRawData,
     uint pointerToRawData
     )
 {
     writer.WriteUInt32(0); // characteristics
     Debug.Assert(stamp.Length == 4);
     writer.WriteBytes(stamp);
     writer.WriteUInt32(version);
     writer.WriteUInt32(debugType);
     writer.WriteUInt32(sizeOfData);
     writer.WriteUInt32(addressOfRawData);
     writer.WriteUInt32(pointerToRawData);
 }