本文整理汇总了C#中BlobBuilder.WriteUTF8方法的典型用法代码示例。如果您正苦于以下问题:C# BlobBuilder.WriteUTF8方法的具体用法?C# BlobBuilder.WriteUTF8怎么用?C# BlobBuilder.WriteUTF8使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BlobBuilder
的用法示例。
在下文中一共展示了BlobBuilder.WriteUTF8方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteDebugTable
private readonly static byte[] zeroStamp = new byte[4]; // four bytes of zero
/// <summary>
/// Write the entire "Debug Directory (Image Only)" along with data that it points to.
/// </summary>
internal void WriteDebugTable(BlobBuilder builder, PESectionLocation textSectionLocation, ContentId nativePdbContentId, ContentId portablePdbContentId)
{
Debug.Assert(builder.Count == 0);
int tableSize = ImageDebugDirectoryBaseSize;
Debug.Assert(tableSize != 0);
Debug.Assert(nativePdbContentId.IsDefault || portablePdbContentId.IsDefault);
Debug.Assert(!EmitPdb || (nativePdbContentId.IsDefault ^ portablePdbContentId.IsDefault));
int dataSize = ComputeSizeOfDebugDirectoryData();
if (EmitPdb)
{
const int IMAGE_DEBUG_TYPE_CODEVIEW = 2; // from PE spec
uint dataOffset = (uint)(ComputeOffsetToDebugTable() + tableSize);
WriteDebugTableEntry(builder,
stamp: nativePdbContentId.Stamp ?? portablePdbContentId.Stamp,
version: portablePdbContentId.IsDefault ? (uint)0 : ('P' << 24 | 'M' << 16 | 0x01 << 8 | 0x00),
debugType: IMAGE_DEBUG_TYPE_CODEVIEW,
sizeOfData: (uint)dataSize,
addressOfRawData: (uint)textSectionLocation.RelativeVirtualAddress + dataOffset, // RVA of the data
pointerToRawData: (uint)textSectionLocation.PointerToRawData + dataOffset); // position of the data in the PE stream
}
if (IsDeterministic)
{
const int IMAGE_DEBUG_TYPE_NO_TIMESTAMP = 16; // from PE spec
WriteDebugTableEntry(builder,
stamp: zeroStamp,
version: 0,
debugType: IMAGE_DEBUG_TYPE_NO_TIMESTAMP,
sizeOfData: 0,
addressOfRawData: 0,
pointerToRawData: 0);
}
// We should now have written all and precisely the data we said we'd write for the table entries.
Debug.Assert(builder.Count == tableSize);
// ====================
// The following is additional data beyond the debug directory at the offset `dataOffset`
// pointed to by the ImageDebugTypeCodeView entry.
if (EmitPdb)
{
builder.WriteByte((byte)'R');
builder.WriteByte((byte)'S');
builder.WriteByte((byte)'D');
builder.WriteByte((byte)'S');
// PDB id:
builder.WriteBytes(nativePdbContentId.Guid ?? portablePdbContentId.Guid);
// age
builder.WriteUInt32(1); // TODO: allow specify for native PDBs
// UTF-8 encoded zero-terminated path to PDB
int pathStart = builder.Count;
builder.WriteUTF8(PdbPathOpt, allowUnpairedSurrogates: true);
builder.WriteByte(0);
// padding:
builder.WriteBytes(0, Math.Max(0, MinPdbPath - (builder.Count - pathStart)));
}
// We should now have written all and precisely the data we said we'd write for the table and its data.
Debug.Assert(builder.Count == tableSize + dataSize);
}
示例2: WriteUTF8_Substring
public void WriteUTF8_Substring()
{
var writer = new BlobBuilder(4);
writer.WriteUTF8("abc", 0, 0, allowUnpairedSurrogates: true, prependSize: false);
AssertEx.Equal(new byte[0], writer.ToArray());
writer.Clear();
writer.WriteUTF8("abc", 0, 1, allowUnpairedSurrogates: true, prependSize: false);
AssertEx.Equal(new[] { (byte)'a' }, writer.ToArray());
writer.Clear();
writer.WriteUTF8("abc", 0, 2, allowUnpairedSurrogates: true, prependSize: false);
AssertEx.Equal(new[] { (byte)'a', (byte)'b' }, writer.ToArray());
writer.Clear();
writer.WriteUTF8("abc", 0, 3, allowUnpairedSurrogates: true, prependSize: false);
AssertEx.Equal(new[] { (byte)'a', (byte)'b', (byte)'c' }, writer.ToArray());
writer.Clear();
writer.WriteUTF8("abc", 1, 0, allowUnpairedSurrogates: true, prependSize: false);
AssertEx.Equal(new byte[0], writer.ToArray());
writer.Clear();
writer.WriteUTF8("abc", 1, 1, allowUnpairedSurrogates: true, prependSize: false);
AssertEx.Equal(new[] { (byte)'b' }, writer.ToArray());
writer.Clear();
writer.WriteUTF8("abc", 1, 2, allowUnpairedSurrogates: true, prependSize: false);
AssertEx.Equal(new[] { (byte)'b', (byte)'c' }, writer.ToArray());
writer.Clear();
writer.WriteUTF8("abc", 2, 1, allowUnpairedSurrogates: true, prependSize: false);
AssertEx.Equal(new[] { (byte)'c' }, writer.ToArray());
writer.Clear();
}
示例3: WriteUTF8_ReplaceUnpairedSurrogates
public void WriteUTF8_ReplaceUnpairedSurrogates()
{
var writer = new BlobBuilder(4);
writer.WriteUTF8("a", allowUnpairedSurrogates: false);
writer.WriteUTF8("", allowUnpairedSurrogates: false);
writer.WriteUTF8("bc", allowUnpairedSurrogates: false);
writer.WriteUTF8("d", allowUnpairedSurrogates: false);
writer.WriteUTF8("", allowUnpairedSurrogates: false);
writer.WriteUTF8(Encoding.UTF8.GetString(new byte[]
{
0x00,
0xC2, 0x80,
0xE1, 0x88, 0xB4
}), allowUnpairedSurrogates: false);
writer.WriteUTF8("\0\ud800", allowUnpairedSurrogates: false); // hi surrogate
writer.WriteUTF8("\0\udc00", allowUnpairedSurrogates: false); // lo surrogate
writer.WriteUTF8("\0\ud800\udc00", allowUnpairedSurrogates: false); // pair
writer.WriteUTF8("\0\udc00\ud800", allowUnpairedSurrogates: false); // lo + hi
AssertEx.Equal(new byte[]
{
(byte)'a',
(byte)'b', (byte)'c',
(byte)'d',
0x00, 0xC2, 0x80, 0xE1, 0x88, 0xB4,
0x00, 0xEF, 0xBF, 0xBD,
0x00, 0xEF, 0xBF, 0xBD,
0x00, 0xF0, 0x90, 0x80, 0x80,
0x00, 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD
}, writer.ToArray());
}
示例4: Write_Errors
public unsafe void Write_Errors()
{
var builder = new BlobBuilder(16);
Assert.Throws<ArgumentNullException>(() => builder.WriteUTF16((char[])null));
Assert.Throws<ArgumentNullException>(() => builder.WriteUTF16((string)null));
Assert.Throws<ArgumentNullException>(() => builder.WriteUTF8(null, allowUnpairedSurrogates: true));
Assert.Throws<ArgumentNullException>(() => builder.WriteUTF8(null, allowUnpairedSurrogates: true));
Assert.Throws<ArgumentNullException>(() => builder.TryWriteBytes((Stream)null, 0));
Assert.Throws<ArgumentNullException>(() => builder.WriteBytes(null));
Assert.Throws<ArgumentNullException>(() => builder.WriteBytes(null, 0, 0));
Assert.Throws<ArgumentNullException>(() => builder.WriteBytes((byte*)null, 0));
Assert.Throws<ArgumentNullException>(() => builder.WriteBytes(default(ImmutableArray<byte>)));
Assert.Throws<ArgumentNullException>(() => builder.WriteBytes(default(ImmutableArray<byte>), 0, 0));
var bw = default(BlobWriter);
Assert.Throws<ArgumentNullException>(() => builder.WriteContentTo(ref bw));
Assert.Throws<ArgumentNullException>(() => builder.WriteContentTo((Stream)null));
Assert.Throws<ArgumentNullException>(() => builder.WriteContentTo((BlobBuilder)null));
Assert.Throws<ArgumentOutOfRangeException>(() => builder.TryWriteBytes(new MemoryStream(), -1));
Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(0, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(new byte[] { }, 1, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(new byte[] { }, 0, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(new byte[] { }, 0, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(ImmutableArray<byte>.Empty, 1, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(ImmutableArray<byte>.Empty, 0, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => builder.WriteBytes(ImmutableArray<byte>.Empty, 1, -1));
}
示例5: Link
public void Link()
{
var builder1 = new BlobBuilder(16);
builder1.WriteByte(1);
var builder2 = new BlobBuilder(16);
builder2.WriteByte(2);
var builder3 = new BlobBuilder(16);
builder3.WriteByte(3);
var builder4 = new BlobBuilder(16);
builder4.WriteByte(4);
var builder5 = new BlobBuilder(16);
builder5.WriteByte(5);
builder2.LinkPrefix(builder1);
AssertEx.Equal(new byte[] { 1, 2 }, builder2.ToArray());
Assert.Throws<InvalidOperationException>(() => builder1.ToArray());
Assert.Throws<InvalidOperationException>(() => builder2.LinkPrefix(builder1));
Assert.Throws<InvalidOperationException>(() => builder1.WriteByte(0xff));
Assert.Throws<InvalidOperationException>(() => builder1.WriteBytes(1, 10));
Assert.Throws<InvalidOperationException>(() => builder1.WriteBytes(new byte[] { 1 }));
Assert.Throws<InvalidOperationException>(() => builder1.ReserveBytes(1));
Assert.Throws<InvalidOperationException>(() => builder1.GetBlobs());
Assert.Throws<InvalidOperationException>(() => builder1.ContentEquals(builder1));
Assert.Throws<InvalidOperationException>(() => builder1.WriteUTF16("str"));
Assert.Throws<InvalidOperationException>(() => builder1.WriteUTF8("str", allowUnpairedSurrogates: false));
builder2.LinkSuffix(builder3);
AssertEx.Equal(new byte[] { 1, 2, 3 }, builder2.ToArray());
Assert.Throws<InvalidOperationException>(() => builder3.LinkPrefix(builder5));
builder2.LinkPrefix(builder4);
AssertEx.Equal(new byte[] { 4, 1, 2, 3 }, builder2.ToArray());
Assert.Throws<InvalidOperationException>(() => builder4.LinkPrefix(builder5));
builder2.LinkSuffix(builder5);
AssertEx.Equal(new byte[] { 4, 1, 2, 3, 5 }, builder2.ToArray());
}
示例6: SerializeMetadataHeader
internal static void SerializeMetadataHeader(BlobBuilder builder, string metadataVersion, MetadataSizes sizes)
{
int startOffset = builder.Count;
// signature
builder.WriteUInt32(0x424A5342);
// major version
builder.WriteUInt16(1);
// minor version
builder.WriteUInt16(1);
// reserved
builder.WriteUInt32(0);
// Spec (section 24.2.1 Metadata Root):
// Length ... Number of bytes allocated to hold version string (including null terminator), call this x.
// Call the length of the string (including the terminator) m (we require m <= 255);
// the length x is m rounded up to a multiple of four.
builder.WriteInt32(sizes.MetadataVersionPaddedLength);
int metadataVersionStart = builder.Count;
builder.WriteUTF8(metadataVersion);
builder.WriteByte(0);
int metadataVersionEnd = builder.Count;
for (int i = 0; i < sizes.MetadataVersionPaddedLength - (metadataVersionEnd - metadataVersionStart); i++)
{
builder.WriteByte(0);
}
// reserved
builder.WriteUInt16(0);
// number of streams
builder.WriteUInt16((ushort)(5 + (sizes.IsEncDelta ? 1 : 0) + (sizes.IsStandaloneDebugMetadata ? 1 : 0)));
// stream headers
int offsetFromStartOfMetadata = sizes.MetadataHeaderSize;
// emit the #Pdb stream first so that only a single page has to be read in order to find out PDB ID
if (sizes.IsStandaloneDebugMetadata)
{
SerializeStreamHeader(ref offsetFromStartOfMetadata, sizes.StandalonePdbStreamSize, "#Pdb", builder);
}
// Spec: Some compilers store metadata in a #- stream, which holds an uncompressed, or non-optimized, representation of metadata tables;
// this includes extra metadata -Ptr tables. Such PE files do not form part of ECMA-335 standard.
//
// Note: EnC delta is stored as uncompressed metadata stream.
SerializeStreamHeader(ref offsetFromStartOfMetadata, sizes.MetadataTableStreamSize, (sizes.IsCompressed ? "#~" : "#-"), builder);
SerializeStreamHeader(ref offsetFromStartOfMetadata, sizes.GetAlignedHeapSize(HeapIndex.String), "#Strings", builder);
SerializeStreamHeader(ref offsetFromStartOfMetadata, sizes.GetAlignedHeapSize(HeapIndex.UserString), "#US", builder);
SerializeStreamHeader(ref offsetFromStartOfMetadata, sizes.GetAlignedHeapSize(HeapIndex.Guid), "#GUID", builder);
SerializeStreamHeader(ref offsetFromStartOfMetadata, sizes.GetAlignedHeapSize(HeapIndex.Blob), "#Blob", builder);
if (sizes.IsEncDelta)
{
SerializeStreamHeader(ref offsetFromStartOfMetadata, 0, "#JTD", builder);
}
int endOffset = builder.Count;
Debug.Assert(endOffset - startOffset == sizes.MetadataHeaderSize);
}