123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- #region Copyright notice and license
- // Protocol Buffers - Google's data interchange format
- // Copyright 2008 Google Inc. All rights reserved.
- // https://developers.google.com/protocol-buffers/
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are
- // met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following disclaimer
- // in the documentation and/or other materials provided with the
- // distribution.
- // * Neither the name of Google Inc. nor the names of its
- // contributors may be used to endorse or promote products derived from
- // this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- #endregion
- using System;
- using System.Buffers.Binary;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Security;
- using System.Text;
- namespace Google.Protobuf
- {
- /// <summary>
- /// Primitives for encoding protobuf wire format.
- /// </summary>
- [SecuritySafeCritical]
- internal static class WritingPrimitives
- {
- // "Local" copy of Encoding.UTF8, for efficiency. (Yes, it makes a difference.)
- internal static readonly Encoding Utf8Encoding = Encoding.UTF8;
- #region Writing of values (not including tags)
- /// <summary>
- /// Writes a double field value, without a tag, to the stream.
- /// </summary>
- public static void WriteDouble(ref Span<byte> buffer, ref WriterInternalState state, double value)
- {
- WriteRawLittleEndian64(ref buffer, ref state, (ulong)BitConverter.DoubleToInt64Bits(value));
- }
- /// <summary>
- /// Writes a float field value, without a tag, to the stream.
- /// </summary>
- public static unsafe void WriteFloat(ref Span<byte> buffer, ref WriterInternalState state, float value)
- {
- const int length = sizeof(float);
- if (buffer.Length - state.position >= length)
- {
- // if there's enough space in the buffer, write the float directly into the buffer
- var floatSpan = buffer.Slice(state.position, length);
- Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(floatSpan), value);
- if (!BitConverter.IsLittleEndian)
- {
- floatSpan.Reverse();
- }
- state.position += length;
- }
- else
- {
- WriteFloatSlowPath(ref buffer, ref state, value);
- }
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static unsafe void WriteFloatSlowPath(ref Span<byte> buffer, ref WriterInternalState state, float value)
- {
- const int length = sizeof(float);
- // TODO(jtattermusch): deduplicate the code. Populating the span is the same as for the fastpath.
- Span<byte> floatSpan = stackalloc byte[length];
- Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(floatSpan), value);
- if (!BitConverter.IsLittleEndian)
- {
- floatSpan.Reverse();
- }
- WriteRawByte(ref buffer, ref state, floatSpan[0]);
- WriteRawByte(ref buffer, ref state, floatSpan[1]);
- WriteRawByte(ref buffer, ref state, floatSpan[2]);
- WriteRawByte(ref buffer, ref state, floatSpan[3]);
- }
- /// <summary>
- /// Writes a uint64 field value, without a tag, to the stream.
- /// </summary>
- public static void WriteUInt64(ref Span<byte> buffer, ref WriterInternalState state, ulong value)
- {
- WriteRawVarint64(ref buffer, ref state, value);
- }
- /// <summary>
- /// Writes an int64 field value, without a tag, to the stream.
- /// </summary>
- public static void WriteInt64(ref Span<byte> buffer, ref WriterInternalState state, long value)
- {
- WriteRawVarint64(ref buffer, ref state, (ulong)value);
- }
- /// <summary>
- /// Writes an int32 field value, without a tag, to the stream.
- /// </summary>
- public static void WriteInt32(ref Span<byte> buffer, ref WriterInternalState state, int value)
- {
- if (value >= 0)
- {
- WriteRawVarint32(ref buffer, ref state, (uint)value);
- }
- else
- {
- // Must sign-extend.
- WriteRawVarint64(ref buffer, ref state, (ulong)value);
- }
- }
- /// <summary>
- /// Writes a fixed64 field value, without a tag, to the stream.
- /// </summary>
- public static void WriteFixed64(ref Span<byte> buffer, ref WriterInternalState state, ulong value)
- {
- WriteRawLittleEndian64(ref buffer, ref state, value);
- }
- /// <summary>
- /// Writes a fixed32 field value, without a tag, to the stream.
- /// </summary>
- public static void WriteFixed32(ref Span<byte> buffer, ref WriterInternalState state, uint value)
- {
- WriteRawLittleEndian32(ref buffer, ref state, value);
- }
- /// <summary>
- /// Writes a bool field value, without a tag, to the stream.
- /// </summary>
- public static void WriteBool(ref Span<byte> buffer, ref WriterInternalState state, bool value)
- {
- WriteRawByte(ref buffer, ref state, value ? (byte)1 : (byte)0);
- }
- /// <summary>
- /// Writes a string field value, without a tag, to the stream.
- /// The data is length-prefixed.
- /// </summary>
- public static void WriteString(ref Span<byte> buffer, ref WriterInternalState state, string value)
- {
- // Optimise the case where we have enough space to write
- // the string directly to the buffer, which should be common.
- int length = Utf8Encoding.GetByteCount(value);
- WriteLength(ref buffer, ref state, length);
- if (buffer.Length - state.position >= length)
- {
- if (length == value.Length) // Must be all ASCII...
- {
- for (int i = 0; i < length; i++)
- {
- buffer[state.position + i] = (byte)value[i];
- }
- state.position += length;
- }
- else
- {
- #if NETSTANDARD1_1
- // slowpath when Encoding.GetBytes(Char*, Int32, Byte*, Int32) is not available
- byte[] bytes = Utf8Encoding.GetBytes(value);
- WriteRawBytes(ref buffer, ref state, bytes);
- #else
- ReadOnlySpan<char> source = value.AsSpan();
- int bytesUsed;
- unsafe
- {
- fixed (char* sourceChars = &MemoryMarshal.GetReference(source))
- fixed (byte* destinationBytes = &MemoryMarshal.GetReference(buffer.Slice(state.position)))
- {
- bytesUsed = Utf8Encoding.GetBytes(sourceChars, source.Length, destinationBytes, buffer.Length);
- }
- }
- state.position += bytesUsed;
- #endif
- }
- }
- else
- {
- // Opportunity for future optimization:
- // Large strings that don't fit into the current buffer segment
- // can probably be optimized by using Utf8Encoding.GetEncoder()
- // but more benchmarks would need to be added as evidence.
- byte[] bytes = Utf8Encoding.GetBytes(value);
- WriteRawBytes(ref buffer, ref state, bytes);
- }
- }
- /// <summary>
- /// Write a byte string, without a tag, to the stream.
- /// The data is length-prefixed.
- /// </summary>
- public static void WriteBytes(ref Span<byte> buffer, ref WriterInternalState state, ByteString value)
- {
- WriteLength(ref buffer, ref state, value.Length);
- WriteRawBytes(ref buffer, ref state, value.Span);
- }
- /// <summary>
- /// Writes a uint32 value, without a tag, to the stream.
- /// </summary>
- public static void WriteUInt32(ref Span<byte> buffer, ref WriterInternalState state, uint value)
- {
- WriteRawVarint32(ref buffer, ref state, value);
- }
- /// <summary>
- /// Writes an enum value, without a tag, to the stream.
- /// </summary>
- public static void WriteEnum(ref Span<byte> buffer, ref WriterInternalState state, int value)
- {
- WriteInt32(ref buffer, ref state, value);
- }
- /// <summary>
- /// Writes an sfixed32 value, without a tag, to the stream.
- /// </summary>
- public static void WriteSFixed32(ref Span<byte> buffer, ref WriterInternalState state, int value)
- {
- WriteRawLittleEndian32(ref buffer, ref state, (uint)value);
- }
- /// <summary>
- /// Writes an sfixed64 value, without a tag, to the stream.
- /// </summary>
- public static void WriteSFixed64(ref Span<byte> buffer, ref WriterInternalState state, long value)
- {
- WriteRawLittleEndian64(ref buffer, ref state, (ulong)value);
- }
- /// <summary>
- /// Writes an sint32 value, without a tag, to the stream.
- /// </summary>
- public static void WriteSInt32(ref Span<byte> buffer, ref WriterInternalState state, int value)
- {
- WriteRawVarint32(ref buffer, ref state, EncodeZigZag32(value));
- }
- /// <summary>
- /// Writes an sint64 value, without a tag, to the stream.
- /// </summary>
- public static void WriteSInt64(ref Span<byte> buffer, ref WriterInternalState state, long value)
- {
- WriteRawVarint64(ref buffer, ref state, EncodeZigZag64(value));
- }
- /// <summary>
- /// Writes a length (in bytes) for length-delimited data.
- /// </summary>
- /// <remarks>
- /// This method simply writes a rawint, but exists for clarity in calling code.
- /// </remarks>
- public static void WriteLength(ref Span<byte> buffer, ref WriterInternalState state, int length)
- {
- WriteRawVarint32(ref buffer, ref state, (uint)length);
- }
- #endregion
- #region Writing primitives
- /// <summary>
- /// Writes a 32 bit value as a varint. The fast route is taken when
- /// there's enough buffer space left to whizz through without checking
- /// for each byte; otherwise, we resort to calling WriteRawByte each time.
- /// </summary>
- public static void WriteRawVarint32(ref Span<byte> buffer, ref WriterInternalState state, uint value)
- {
- // Optimize for the common case of a single byte value
- if (value < 128 && state.position < buffer.Length)
- {
- buffer[state.position++] = (byte)value;
- return;
- }
- // Fast path when capacity is available
- while (state.position < buffer.Length)
- {
- if (value > 127)
- {
- buffer[state.position++] = (byte)((value & 0x7F) | 0x80);
- value >>= 7;
- }
- else
- {
- buffer[state.position++] = (byte)value;
- return;
- }
- }
- while (value > 127)
- {
- WriteRawByte(ref buffer, ref state, (byte)((value & 0x7F) | 0x80));
- value >>= 7;
- }
- WriteRawByte(ref buffer, ref state, (byte)value);
- }
- public static void WriteRawVarint64(ref Span<byte> buffer, ref WriterInternalState state, ulong value)
- {
- // Optimize for the common case of a single byte value
- if (value < 128 && state.position < buffer.Length)
- {
- buffer[state.position++] = (byte)value;
- return;
- }
- // Fast path when capacity is available
- while (state.position < buffer.Length)
- {
- if (value > 127)
- {
- buffer[state.position++] = (byte)((value & 0x7F) | 0x80);
- value >>= 7;
- }
- else
- {
- buffer[state.position++] = (byte)value;
- return;
- }
- }
- while (value > 127)
- {
- WriteRawByte(ref buffer, ref state, (byte)((value & 0x7F) | 0x80));
- value >>= 7;
- }
- WriteRawByte(ref buffer, ref state, (byte)value);
- }
- public static void WriteRawLittleEndian32(ref Span<byte> buffer, ref WriterInternalState state, uint value)
- {
- const int length = sizeof(uint);
- if (state.position + length > buffer.Length)
- {
- WriteRawLittleEndian32SlowPath(ref buffer, ref state, value);
- }
- else
- {
- BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(state.position), value);
- state.position += length;
- }
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static void WriteRawLittleEndian32SlowPath(ref Span<byte> buffer, ref WriterInternalState state, uint value)
- {
- WriteRawByte(ref buffer, ref state, (byte)value);
- WriteRawByte(ref buffer, ref state, (byte)(value >> 8));
- WriteRawByte(ref buffer, ref state, (byte)(value >> 16));
- WriteRawByte(ref buffer, ref state, (byte)(value >> 24));
- }
- public static void WriteRawLittleEndian64(ref Span<byte> buffer, ref WriterInternalState state, ulong value)
- {
- const int length = sizeof(ulong);
- if (state.position + length > buffer.Length)
- {
- WriteRawLittleEndian64SlowPath(ref buffer, ref state, value);
- }
- else
- {
- // TODO(jtattermusch): According to the benchmarks, writing byte-by-byte is actually faster
- // than using BinaryPrimitives.WriteUInt64LittleEndian.
- // This is strange especially because WriteUInt32LittleEndian seems to be much faster
- // in terms of throughput.
- buffer[state.position++] = ((byte)value);
- buffer[state.position++] = ((byte)(value >> 8));
- buffer[state.position++] = ((byte)(value >> 16));
- buffer[state.position++] = ((byte)(value >> 24));
- buffer[state.position++] = ((byte)(value >> 32));
- buffer[state.position++] = ((byte)(value >> 40));
- buffer[state.position++] = ((byte)(value >> 48));
- buffer[state.position++] = ((byte)(value >> 56));
- }
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- public static void WriteRawLittleEndian64SlowPath(ref Span<byte> buffer, ref WriterInternalState state, ulong value)
- {
- WriteRawByte(ref buffer, ref state, (byte)value);
- WriteRawByte(ref buffer, ref state, (byte)(value >> 8));
- WriteRawByte(ref buffer, ref state, (byte)(value >> 16));
- WriteRawByte(ref buffer, ref state, (byte)(value >> 24));
- WriteRawByte(ref buffer, ref state, (byte)(value >> 32));
- WriteRawByte(ref buffer, ref state, (byte)(value >> 40));
- WriteRawByte(ref buffer, ref state, (byte)(value >> 48));
- WriteRawByte(ref buffer, ref state, (byte)(value >> 56));
- }
- private static void WriteRawByte(ref Span<byte> buffer, ref WriterInternalState state, byte value)
- {
- if (state.position == buffer.Length)
- {
- WriteBufferHelper.RefreshBuffer(ref buffer, ref state);
- }
- buffer[state.position++] = value;
- }
- /// <summary>
- /// Writes out an array of bytes.
- /// </summary>
- public static void WriteRawBytes(ref Span<byte> buffer, ref WriterInternalState state, byte[] value)
- {
- WriteRawBytes(ref buffer, ref state, new ReadOnlySpan<byte>(value));
- }
- /// <summary>
- /// Writes out part of an array of bytes.
- /// </summary>
- public static void WriteRawBytes(ref Span<byte> buffer, ref WriterInternalState state, byte[] value, int offset, int length)
- {
- WriteRawBytes(ref buffer, ref state, new ReadOnlySpan<byte>(value, offset, length));
- }
- /// <summary>
- /// Writes out part of an array of bytes.
- /// </summary>
- public static void WriteRawBytes(ref Span<byte> buffer, ref WriterInternalState state, ReadOnlySpan<byte> value)
- {
- if (buffer.Length - state.position >= value.Length)
- {
- // We have room in the current buffer.
- value.CopyTo(buffer.Slice(state.position, value.Length));
- state.position += value.Length;
- }
- else
- {
- // When writing to a CodedOutputStream backed by a Stream, we could avoid
- // copying the data twice (first copying to the current buffer and
- // and later writing from the current buffer to the underlying Stream)
- // in some circumstances by writing the data directly to the underlying Stream.
- // Current this is not being done to avoid specialcasing the code for
- // CodedOutputStream vs IBufferWriter<byte>.
- int bytesWritten = 0;
- while (buffer.Length - state.position < value.Length - bytesWritten)
- {
- int length = buffer.Length - state.position;
- value.Slice(bytesWritten, length).CopyTo(buffer.Slice(state.position, length));
- bytesWritten += length;
- state.position += length;
- WriteBufferHelper.RefreshBuffer(ref buffer, ref state);
- }
- // copy the remaining data
- int remainderLength = value.Length - bytesWritten;
- value.Slice(bytesWritten, remainderLength).CopyTo(buffer.Slice(state.position, remainderLength));
- state.position += remainderLength;
- }
- }
- #endregion
- #region Raw tag writing
- /// <summary>
- /// Encodes and writes a tag.
- /// </summary>
- public static void WriteTag(ref Span<byte> buffer, ref WriterInternalState state, int fieldNumber, WireFormat.WireType type)
- {
- WriteRawVarint32(ref buffer, ref state, WireFormat.MakeTag(fieldNumber, type));
- }
- /// <summary>
- /// Writes an already-encoded tag.
- /// </summary>
- public static void WriteTag(ref Span<byte> buffer, ref WriterInternalState state, uint tag)
- {
- WriteRawVarint32(ref buffer, ref state, tag);
- }
- /// <summary>
- /// Writes the given single-byte tag directly to the stream.
- /// </summary>
- public static void WriteRawTag(ref Span<byte> buffer, ref WriterInternalState state, byte b1)
- {
- WriteRawByte(ref buffer, ref state, b1);
- }
- /// <summary>
- /// Writes the given two-byte tag directly to the stream.
- /// </summary>
- public static void WriteRawTag(ref Span<byte> buffer, ref WriterInternalState state, byte b1, byte b2)
- {
- WriteRawByte(ref buffer, ref state, b1);
- WriteRawByte(ref buffer, ref state, b2);
- }
- /// <summary>
- /// Writes the given three-byte tag directly to the stream.
- /// </summary>
- public static void WriteRawTag(ref Span<byte> buffer, ref WriterInternalState state, byte b1, byte b2, byte b3)
- {
- WriteRawByte(ref buffer, ref state, b1);
- WriteRawByte(ref buffer, ref state, b2);
- WriteRawByte(ref buffer, ref state, b3);
- }
- /// <summary>
- /// Writes the given four-byte tag directly to the stream.
- /// </summary>
- public static void WriteRawTag(ref Span<byte> buffer, ref WriterInternalState state, byte b1, byte b2, byte b3, byte b4)
- {
- WriteRawByte(ref buffer, ref state, b1);
- WriteRawByte(ref buffer, ref state, b2);
- WriteRawByte(ref buffer, ref state, b3);
- WriteRawByte(ref buffer, ref state, b4);
- }
- /// <summary>
- /// Writes the given five-byte tag directly to the stream.
- /// </summary>
- public static void WriteRawTag(ref Span<byte> buffer, ref WriterInternalState state, byte b1, byte b2, byte b3, byte b4, byte b5)
- {
- WriteRawByte(ref buffer, ref state, b1);
- WriteRawByte(ref buffer, ref state, b2);
- WriteRawByte(ref buffer, ref state, b3);
- WriteRawByte(ref buffer, ref state, b4);
- WriteRawByte(ref buffer, ref state, b5);
- }
- #endregion
- /// <summary>
- /// Encode a 32-bit value with ZigZag encoding.
- /// </summary>
- /// <remarks>
- /// ZigZag encodes signed integers into values that can be efficiently
- /// encoded with varint. (Otherwise, negative values must be
- /// sign-extended to 64 bits to be varint encoded, thus always taking
- /// 10 bytes on the wire.)
- /// </remarks>
- public static uint EncodeZigZag32(int n)
- {
- // Note: the right-shift must be arithmetic
- return (uint)((n << 1) ^ (n >> 31));
- }
- /// <summary>
- /// Encode a 64-bit value with ZigZag encoding.
- /// </summary>
- /// <remarks>
- /// ZigZag encodes signed integers into values that can be efficiently
- /// encoded with varint. (Otherwise, negative values must be
- /// sign-extended to 64 bits to be varint encoded, thus always taking
- /// 10 bytes on the wire.)
- /// </remarks>
- public static ulong EncodeZigZag64(long n)
- {
- return (ulong)((n << 1) ^ (n >> 63));
- }
- }
- }
|