| | | 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.IO; |
| | | 7 | | using System.Reflection.Internal; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | using System.Text; |
| | | 10 | | |
| | | 11 | | namespace System.Reflection.Metadata |
| | | 12 | | { |
| | | 13 | | // TODO: argument checking |
| | | 14 | | public struct BlobWriter |
| | | 15 | | { |
| | | 16 | | // writable slice: |
| | | 17 | | private readonly byte[] _buffer; |
| | | 18 | | private readonly int _start; |
| | | 19 | | private readonly int _end; // exclusive |
| | | 20 | | |
| | | 21 | | // position in buffer relative to the beginning of the array: |
| | | 22 | | private int _position; |
| | | 23 | | |
| | | 24 | | public BlobWriter(int size) |
| | 0 | 25 | | : this(new byte[size]) |
| | 0 | 26 | | { |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public BlobWriter(byte[] buffer) |
| | 0 | 30 | | : this(buffer, 0, buffer.Length) |
| | 0 | 31 | | { |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public BlobWriter(Blob blob) |
| | 0 | 35 | | : this(blob.Buffer, blob.Start, blob.Length) |
| | 0 | 36 | | { |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | public BlobWriter(byte[] buffer, int start, int count) |
| | 0 | 40 | | { |
| | 0 | 41 | | Debug.Assert(buffer != null); |
| | 0 | 42 | | Debug.Assert(count >= 0); |
| | 0 | 43 | | Debug.Assert(count <= buffer.Length - start); |
| | | 44 | | |
| | 0 | 45 | | _buffer = buffer; |
| | 0 | 46 | | _start = start; |
| | 0 | 47 | | _position = start; |
| | 0 | 48 | | _end = start + count; |
| | 0 | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | internal bool IsDefault => _buffer == null; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Compares the current content of this writer with another one. |
| | | 55 | | /// </summary> |
| | | 56 | | public bool ContentEquals(BlobWriter other) |
| | 0 | 57 | | { |
| | 0 | 58 | | return Length == other.Length && _buffer.AsSpan(_start, Length).SequenceEqual(other._buffer.AsSpan(other._st |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | public int Offset |
| | | 62 | | { |
| | | 63 | | get |
| | 0 | 64 | | { |
| | 0 | 65 | | return _position - _start; |
| | 0 | 66 | | } |
| | | 67 | | set |
| | 0 | 68 | | { |
| | 0 | 69 | | if (value < 0 || _start > _end - value) |
| | 0 | 70 | | { |
| | 0 | 71 | | Throw.ValueArgumentOutOfRange(); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | _position = _start + value; |
| | 0 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | public int Length => _end - _start; |
| | 0 | 79 | | public int RemainingBytes => _end - _position; |
| | 0 | 80 | | public Blob Blob => new Blob(_buffer, _start, Length); |
| | | 81 | | |
| | | 82 | | public byte[] ToArray() |
| | 0 | 83 | | { |
| | 0 | 84 | | return ToArray(0, Offset); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name |
| | | 88 | | public byte[] ToArray(int start, int byteCount) |
| | 0 | 89 | | { |
| | 0 | 90 | | BlobUtilities.ValidateRange(Length, start, byteCount, nameof(byteCount)); |
| | | 91 | | |
| | 0 | 92 | | return _buffer.AsSpan(_start + start, byteCount).ToArray(); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | public ImmutableArray<byte> ToImmutableArray() |
| | 0 | 96 | | { |
| | 0 | 97 | | return ToImmutableArray(0, Offset); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name |
| | | 101 | | public ImmutableArray<byte> ToImmutableArray(int start, int byteCount) |
| | 0 | 102 | | { |
| | 0 | 103 | | BlobUtilities.ValidateRange(Length, start, byteCount, nameof(byteCount)); |
| | | 104 | | |
| | 0 | 105 | | return ImmutableArray.Create(_buffer.AsSpan(_start + start, byteCount)); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | private int Advance(int value) |
| | 0 | 109 | | { |
| | 0 | 110 | | Debug.Assert(value >= 0); |
| | | 111 | | |
| | 0 | 112 | | int position = _position; |
| | 0 | 113 | | if (position > _end - value) |
| | 0 | 114 | | { |
| | 0 | 115 | | Throw.OutOfBounds(); |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | _position = position + value; |
| | 0 | 119 | | return position; |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="byteCount"/> is negative.</exception> |
| | | 123 | | public void WriteBytes(byte value, int byteCount) |
| | 0 | 124 | | { |
| | 0 | 125 | | if (byteCount < 0) |
| | 0 | 126 | | { |
| | 0 | 127 | | Throw.ArgumentOutOfRange(nameof(byteCount)); |
| | | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | int start = Advance(byteCount); |
| | 0 | 131 | | _buffer.AsSpan(start, byteCount).Fill(value); |
| | 0 | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> |
| | | 135 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="byteCount"/> is negative.</exception> |
| | | 136 | | public unsafe void WriteBytes(byte* buffer, int byteCount) |
| | 0 | 137 | | { |
| | 0 | 138 | | if (buffer is null) |
| | 0 | 139 | | { |
| | 0 | 140 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | if (byteCount < 0) |
| | 0 | 144 | | { |
| | 0 | 145 | | Throw.ArgumentOutOfRange(nameof(byteCount)); |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | WriteBytes(new ReadOnlySpan<byte>(buffer, byteCount)); |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | internal void WriteBytes(ReadOnlySpan<byte> buffer) |
| | 0 | 152 | | { |
| | 0 | 153 | | int start = Advance(buffer.Length); |
| | 0 | 154 | | buffer.CopyTo(_buffer.AsSpan(start)); |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| | | 158 | | public void WriteBytes(BlobBuilder source) |
| | 0 | 159 | | { |
| | 0 | 160 | | if (source is null) |
| | 0 | 161 | | { |
| | 0 | 162 | | Throw.ArgumentNull(nameof(source)); |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | source.WriteContentTo(ref this); |
| | 0 | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| | | 169 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="byteCount"/> is negative.</exception> |
| | | 170 | | public int WriteBytes(Stream source, int byteCount) |
| | 0 | 171 | | { |
| | 0 | 172 | | if (source is null) |
| | 0 | 173 | | { |
| | 0 | 174 | | Throw.ArgumentNull(nameof(source)); |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | if (byteCount < 0) |
| | 0 | 178 | | { |
| | 0 | 179 | | Throw.ArgumentOutOfRange(nameof(byteCount)); |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | int start = Advance(byteCount); |
| | 0 | 183 | | int bytesRead = source.TryReadAll(_buffer, start, byteCount); |
| | 0 | 184 | | _position = start + bytesRead; |
| | 0 | 185 | | return bytesRead; |
| | 0 | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> |
| | | 189 | | public void WriteBytes(ImmutableArray<byte> buffer) |
| | 0 | 190 | | { |
| | 0 | 191 | | if (buffer.IsDefault) |
| | 0 | 192 | | { |
| | 0 | 193 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | WriteBytes(buffer.AsSpan()); |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> |
| | | 200 | | /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name |
| | | 201 | | public void WriteBytes(ImmutableArray<byte> buffer, int start, int byteCount) |
| | 0 | 202 | | { |
| | 0 | 203 | | if (buffer.IsDefault) |
| | 0 | 204 | | { |
| | 0 | 205 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | BlobUtilities.ValidateRange(buffer.Length, start, byteCount, nameof(byteCount)); |
| | | 209 | | |
| | 0 | 210 | | WriteBytes(buffer.AsSpan(start, byteCount)); |
| | 0 | 211 | | } |
| | | 212 | | |
| | | 213 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> |
| | | 214 | | public void WriteBytes(byte[] buffer) |
| | 0 | 215 | | { |
| | 0 | 216 | | if (buffer is null) |
| | 0 | 217 | | { |
| | 0 | 218 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 219 | | } |
| | | 220 | | |
| | 0 | 221 | | WriteBytes(buffer.AsSpan()); |
| | 0 | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> |
| | | 225 | | /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name |
| | | 226 | | public void WriteBytes(byte[] buffer, int start, int byteCount) |
| | 0 | 227 | | { |
| | 0 | 228 | | if (buffer is null) |
| | 0 | 229 | | { |
| | 0 | 230 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | BlobUtilities.ValidateRange(buffer.Length, start, byteCount, nameof(byteCount)); |
| | | 234 | | |
| | 0 | 235 | | WriteBytes(buffer.AsSpan(start, byteCount)); |
| | 0 | 236 | | } |
| | | 237 | | |
| | | 238 | | public void PadTo(int offset) |
| | 0 | 239 | | { |
| | 0 | 240 | | WriteBytes(0, offset - Offset); |
| | 0 | 241 | | } |
| | | 242 | | |
| | | 243 | | public void Align(int alignment) |
| | 0 | 244 | | { |
| | 0 | 245 | | int offset = Offset; |
| | 0 | 246 | | WriteBytes(0, BitArithmetic.Align(offset, alignment) - offset); |
| | 0 | 247 | | } |
| | | 248 | | |
| | | 249 | | public void WriteBoolean(bool value) |
| | 0 | 250 | | { |
| | 0 | 251 | | WriteByte((byte)(value ? 1 : 0)); |
| | 0 | 252 | | } |
| | | 253 | | |
| | | 254 | | public void WriteByte(byte value) |
| | 0 | 255 | | { |
| | 0 | 256 | | int start = Advance(sizeof(byte)); |
| | 0 | 257 | | _buffer[start] = value; |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | public void WriteSByte(sbyte value) |
| | 0 | 261 | | { |
| | 0 | 262 | | WriteByte(unchecked((byte)value)); |
| | 0 | 263 | | } |
| | | 264 | | |
| | | 265 | | public void WriteDouble(double value) |
| | 0 | 266 | | { |
| | 0 | 267 | | int start = Advance(sizeof(double)); |
| | 0 | 268 | | _buffer.WriteDouble(start, value); |
| | 0 | 269 | | } |
| | | 270 | | |
| | | 271 | | public void WriteSingle(float value) |
| | 0 | 272 | | { |
| | 0 | 273 | | int start = Advance(sizeof(float)); |
| | 0 | 274 | | _buffer.WriteSingle(start, value); |
| | 0 | 275 | | } |
| | | 276 | | |
| | | 277 | | public void WriteInt16(short value) |
| | 0 | 278 | | { |
| | 0 | 279 | | WriteUInt16(unchecked((ushort)value)); |
| | 0 | 280 | | } |
| | | 281 | | |
| | | 282 | | public void WriteUInt16(ushort value) |
| | 0 | 283 | | { |
| | 0 | 284 | | int start = Advance(sizeof(ushort)); |
| | 0 | 285 | | _buffer.WriteUInt16(start, value); |
| | 0 | 286 | | } |
| | | 287 | | |
| | | 288 | | public void WriteInt16BE(short value) |
| | 0 | 289 | | { |
| | 0 | 290 | | WriteUInt16BE(unchecked((ushort)value)); |
| | 0 | 291 | | } |
| | | 292 | | |
| | | 293 | | public void WriteUInt16BE(ushort value) |
| | 0 | 294 | | { |
| | 0 | 295 | | int start = Advance(sizeof(ushort)); |
| | 0 | 296 | | _buffer.WriteUInt16BE(start, value); |
| | 0 | 297 | | } |
| | | 298 | | |
| | | 299 | | public void WriteInt32BE(int value) |
| | 0 | 300 | | { |
| | 0 | 301 | | WriteUInt32BE(unchecked((uint)value)); |
| | 0 | 302 | | } |
| | | 303 | | |
| | | 304 | | public void WriteUInt32BE(uint value) |
| | 0 | 305 | | { |
| | 0 | 306 | | int start = Advance(sizeof(uint)); |
| | 0 | 307 | | _buffer.WriteUInt32BE(start, value); |
| | 0 | 308 | | } |
| | | 309 | | |
| | | 310 | | public void WriteInt32(int value) |
| | 0 | 311 | | { |
| | 0 | 312 | | WriteUInt32(unchecked((uint)value)); |
| | 0 | 313 | | } |
| | | 314 | | |
| | | 315 | | public void WriteUInt32(uint value) |
| | 0 | 316 | | { |
| | 0 | 317 | | int start = Advance(sizeof(uint)); |
| | 0 | 318 | | _buffer.WriteUInt32(start, value); |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | public void WriteInt64(long value) |
| | 0 | 322 | | { |
| | 0 | 323 | | WriteUInt64(unchecked((ulong)value)); |
| | 0 | 324 | | } |
| | | 325 | | |
| | | 326 | | public void WriteUInt64(ulong value) |
| | 0 | 327 | | { |
| | 0 | 328 | | int start = Advance(sizeof(ulong)); |
| | 0 | 329 | | _buffer.WriteUInt64(start, value); |
| | 0 | 330 | | } |
| | | 331 | | |
| | | 332 | | public void WriteDecimal(decimal value) |
| | 0 | 333 | | { |
| | 0 | 334 | | int start = Advance(BlobUtilities.SizeOfSerializedDecimal); |
| | 0 | 335 | | _buffer.WriteDecimal(start, value); |
| | 0 | 336 | | } |
| | | 337 | | |
| | | 338 | | public void WriteGuid(Guid value) |
| | 0 | 339 | | { |
| | 0 | 340 | | int start = Advance(BlobUtilities.SizeOfGuid); |
| | 0 | 341 | | _buffer.WriteGuid(start, value); |
| | 0 | 342 | | } |
| | | 343 | | |
| | | 344 | | public void WriteDateTime(DateTime value) |
| | 0 | 345 | | { |
| | 0 | 346 | | WriteInt64(value.Ticks); |
| | 0 | 347 | | } |
| | | 348 | | |
| | | 349 | | /// <summary> |
| | | 350 | | /// Writes a reference to a heap (heap offset) or a table (row number). |
| | | 351 | | /// </summary> |
| | | 352 | | /// <param name="reference">Heap offset or table row number.</param> |
| | | 353 | | /// <param name="isSmall">True to encode the reference as 16-bit integer, false to encode as 32-bit integer.</pa |
| | | 354 | | public void WriteReference(int reference, bool isSmall) |
| | 0 | 355 | | { |
| | | 356 | | // This code is a very hot path, hence we don't check if the reference actually fits 2B. |
| | | 357 | | |
| | 0 | 358 | | if (isSmall) |
| | 0 | 359 | | { |
| | 0 | 360 | | Debug.Assert(unchecked((ushort)reference) == reference); |
| | 0 | 361 | | WriteUInt16((ushort)reference); |
| | 0 | 362 | | } |
| | | 363 | | else |
| | 0 | 364 | | { |
| | 0 | 365 | | WriteInt32(reference); |
| | 0 | 366 | | } |
| | 0 | 367 | | } |
| | | 368 | | |
| | | 369 | | /// <summary> |
| | | 370 | | /// Writes UTF-16 (little-endian) encoded string at the current position. |
| | | 371 | | /// </summary> |
| | | 372 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | | 373 | | public void WriteUTF16(char[] value) |
| | 0 | 374 | | { |
| | 0 | 375 | | if (value is null) |
| | 0 | 376 | | { |
| | 0 | 377 | | Throw.ArgumentNull(nameof(value)); |
| | | 378 | | } |
| | | 379 | | |
| | 0 | 380 | | WriteUTF16(value.AsSpan()); |
| | 0 | 381 | | } |
| | | 382 | | |
| | | 383 | | /// <summary> |
| | | 384 | | /// Writes UTF-16 (little-endian) encoded string at the current position. |
| | | 385 | | /// </summary> |
| | | 386 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | | 387 | | public void WriteUTF16(string value) |
| | 0 | 388 | | { |
| | 0 | 389 | | if (value is null) |
| | 0 | 390 | | { |
| | 0 | 391 | | Throw.ArgumentNull(nameof(value)); |
| | | 392 | | } |
| | | 393 | | |
| | 0 | 394 | | WriteUTF16(value.AsSpan()); |
| | 0 | 395 | | } |
| | | 396 | | |
| | | 397 | | private void WriteUTF16(ReadOnlySpan<char> value) |
| | 0 | 398 | | { |
| | 0 | 399 | | if (BitConverter.IsLittleEndian) |
| | 0 | 400 | | { |
| | 0 | 401 | | WriteBytes(MemoryMarshal.AsBytes(value)); |
| | 0 | 402 | | } |
| | | 403 | | else |
| | 0 | 404 | | { |
| | 0 | 405 | | foreach (char c in value) |
| | 0 | 406 | | { |
| | 0 | 407 | | WriteUInt16(c); |
| | 0 | 408 | | } |
| | 0 | 409 | | } |
| | 0 | 410 | | } |
| | | 411 | | |
| | | 412 | | /// <summary> |
| | | 413 | | /// Writes string in SerString format (see ECMA-335-II 23.3 Custom attributes). |
| | | 414 | | /// </summary> |
| | | 415 | | /// <remarks> |
| | | 416 | | /// The string is UTF-8 encoded and prefixed by the its size in bytes. |
| | | 417 | | /// Null string is represented as a single byte 0xFF. |
| | | 418 | | /// </remarks> |
| | | 419 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 420 | | public void WriteSerializedString(string? str) |
| | 0 | 421 | | { |
| | 0 | 422 | | if (str == null) |
| | 0 | 423 | | { |
| | 0 | 424 | | WriteByte(0xff); |
| | 0 | 425 | | return; |
| | | 426 | | } |
| | | 427 | | |
| | 0 | 428 | | WriteUTF8(str, 0, str.Length, allowUnpairedSurrogates: true, prependSize: true); |
| | 0 | 429 | | } |
| | | 430 | | |
| | | 431 | | /// <summary> |
| | | 432 | | /// Writes string in User String (#US) heap format (see ECMA-335-II 24.2.4 #US and #Blob heaps): |
| | | 433 | | /// </summary> |
| | | 434 | | /// <remarks> |
| | | 435 | | /// The string is UTF-16 encoded and prefixed by the its size in bytes. |
| | | 436 | | /// |
| | | 437 | | /// This final byte holds the value 1 if and only if any UTF-16 character within the string has any bit set in i |
| | | 438 | | /// or its low byte is any of the following: 0x01-0x08, 0x0E-0x1F, 0x27, 0x2D, 0x7F. Otherwise, it holds 0. |
| | | 439 | | /// The 1 signifies Unicode characters that require handling beyond that normally provided for 8-bit encoding se |
| | | 440 | | /// </remarks> |
| | | 441 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 442 | | public void WriteUserString(string value) |
| | 0 | 443 | | { |
| | 0 | 444 | | if (value is null) |
| | 0 | 445 | | { |
| | 0 | 446 | | Throw.ArgumentNull(nameof(value)); |
| | | 447 | | } |
| | | 448 | | |
| | 0 | 449 | | WriteCompressedInteger(BlobUtilities.GetUserStringByteLength(value.Length)); |
| | 0 | 450 | | WriteUTF16(value); |
| | 0 | 451 | | WriteByte(BlobUtilities.GetUserStringTrailingByte(value)); |
| | 0 | 452 | | } |
| | | 453 | | |
| | | 454 | | /// <summary> |
| | | 455 | | /// Writes UTF-8 encoded string at the current position. |
| | | 456 | | /// </summary> |
| | | 457 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | | 458 | | public void WriteUTF8(string value, bool allowUnpairedSurrogates) |
| | 0 | 459 | | { |
| | 0 | 460 | | if (value is null) |
| | 0 | 461 | | { |
| | 0 | 462 | | Throw.ArgumentNull(nameof(value)); |
| | | 463 | | } |
| | | 464 | | |
| | 0 | 465 | | WriteUTF8(value, 0, value.Length, allowUnpairedSurrogates, prependSize: false); |
| | 0 | 466 | | } |
| | | 467 | | |
| | | 468 | | private unsafe void WriteUTF8(string str, int start, int length, bool allowUnpairedSurrogates, bool prependSize) |
| | 0 | 469 | | { |
| | 0 | 470 | | fixed (char* strPtr = str) |
| | 0 | 471 | | { |
| | 0 | 472 | | char* charPtr = strPtr + start; |
| | 0 | 473 | | int byteCount = Encoding.UTF8.GetByteCount(charPtr, length); |
| | | 474 | | |
| | 0 | 475 | | if (prependSize) |
| | 0 | 476 | | { |
| | 0 | 477 | | WriteCompressedInteger(byteCount); |
| | 0 | 478 | | } |
| | | 479 | | |
| | 0 | 480 | | int startOffset = Advance(byteCount); |
| | 0 | 481 | | _buffer.WriteUTF8(startOffset, charPtr, length, byteCount, allowUnpairedSurrogates); |
| | 0 | 482 | | } |
| | 0 | 483 | | } |
| | | 484 | | |
| | | 485 | | /// <summary> |
| | | 486 | | /// Implements compressed signed integer encoding as defined by ECMA-335-II chapter 23.2: Blobs and signatures. |
| | | 487 | | /// </summary> |
| | | 488 | | /// <remarks> |
| | | 489 | | /// If the value lies between -64 (0xFFFFFFC0) and 63 (0x3F), inclusive, encode as a one-byte integer: |
| | | 490 | | /// bit 7 clear, value bits 5 through 0 held in bits 6 through 1, sign bit (value bit 31) in bit 0. |
| | | 491 | | /// |
| | | 492 | | /// If the value lies between -8192 (0xFFFFE000) and 8191 (0x1FFF), inclusive, encode as a two-byte integer: |
| | | 493 | | /// 15 set, bit 14 clear, value bits 12 through 0 held in bits 13 through 1, sign bit(value bit 31) in bit 0. |
| | | 494 | | /// |
| | | 495 | | /// If the value lies between -268435456 (0xF000000) and 268435455 (0x0FFFFFFF), inclusive, encode as a four-byt |
| | | 496 | | /// 31 set, 30 set, bit 29 clear, value bits 27 through 0 held in bits 28 through 1, sign bit(value bit 31) in b |
| | | 497 | | /// </remarks> |
| | | 498 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> can't be represented as a compressed |
| | | 499 | | public void WriteCompressedSignedInteger(int value) |
| | 0 | 500 | | { |
| | 0 | 501 | | BlobWriterImpl.WriteCompressedSignedInteger(ref this, value); |
| | 0 | 502 | | } |
| | | 503 | | |
| | | 504 | | /// <summary> |
| | | 505 | | /// Implements compressed unsigned integer encoding as defined by ECMA-335-II chapter 23.2: Blobs and signatures |
| | | 506 | | /// </summary> |
| | | 507 | | /// <remarks> |
| | | 508 | | /// If the value lies between 0 (0x00) and 127 (0x7F), inclusive, |
| | | 509 | | /// encode as a one-byte integer (bit 7 is clear, value held in bits 6 through 0). |
| | | 510 | | /// |
| | | 511 | | /// If the value lies between 28 (0x80) and 214 - 1 (0x3FFF), inclusive, |
| | | 512 | | /// encode as a 2-byte integer with bit 15 set, bit 14 clear(value held in bits 13 through 0). |
| | | 513 | | /// |
| | | 514 | | /// Otherwise, encode as a 4-byte integer, with bit 31 set, bit 30 set, bit 29 clear (value held in bits 28 thro |
| | | 515 | | /// </remarks> |
| | | 516 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> can't be represented as a compressed |
| | | 517 | | public void WriteCompressedInteger(int value) |
| | 0 | 518 | | { |
| | 0 | 519 | | BlobWriterImpl.WriteCompressedInteger(ref this, unchecked((uint)value)); |
| | 0 | 520 | | } |
| | | 521 | | |
| | | 522 | | /// <summary> |
| | | 523 | | /// Writes a constant value (see ECMA-335 Partition II section 22.9) at the current position. |
| | | 524 | | /// </summary> |
| | | 525 | | /// <exception cref="ArgumentException"><paramref name="value"/> is not of a constant type.</exception> |
| | | 526 | | public void WriteConstant(object? value) |
| | 0 | 527 | | { |
| | 0 | 528 | | BlobWriterImpl.WriteConstant(ref this, value); |
| | 0 | 529 | | } |
| | | 530 | | |
| | | 531 | | public void Clear() |
| | 0 | 532 | | { |
| | 0 | 533 | | _position = _start; |
| | 0 | 534 | | } |
| | | 535 | | } |
| | | 536 | | } |
| | | 537 | | |