1ffe3c632Sopenharmony_ci#region Copyright notice and license
2ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format
3ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc.  All rights reserved.
4ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/
5ffe3c632Sopenharmony_ci//
6ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
7ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are
8ffe3c632Sopenharmony_ci// met:
9ffe3c632Sopenharmony_ci//
10ffe3c632Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
11ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
12ffe3c632Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
13ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
14ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the
15ffe3c632Sopenharmony_ci// distribution.
16ffe3c632Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
17ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from
18ffe3c632Sopenharmony_ci// this software without specific prior written permission.
19ffe3c632Sopenharmony_ci//
20ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31ffe3c632Sopenharmony_ci#endregion
32ffe3c632Sopenharmony_ci
33ffe3c632Sopenharmony_ciusing System;
34ffe3c632Sopenharmony_ciusing System.Runtime.CompilerServices;
35ffe3c632Sopenharmony_ciusing System.Runtime.InteropServices.ComTypes;
36ffe3c632Sopenharmony_ciusing System.Security;
37ffe3c632Sopenharmony_ci
38ffe3c632Sopenharmony_cinamespace Google.Protobuf
39ffe3c632Sopenharmony_ci{
40ffe3c632Sopenharmony_ci    /// <summary>
41ffe3c632Sopenharmony_ci    /// Writing messages / groups.
42ffe3c632Sopenharmony_ci    /// </summary>
43ffe3c632Sopenharmony_ci    [SecuritySafeCritical]
44ffe3c632Sopenharmony_ci    internal static class WritingPrimitivesMessages
45ffe3c632Sopenharmony_ci    {
46ffe3c632Sopenharmony_ci        /// <summary>
47ffe3c632Sopenharmony_ci        /// Writes a message, without a tag.
48ffe3c632Sopenharmony_ci        /// The data is length-prefixed.
49ffe3c632Sopenharmony_ci        /// </summary>
50ffe3c632Sopenharmony_ci        [MethodImpl(MethodImplOptions.AggressiveInlining)]
51ffe3c632Sopenharmony_ci        public static void WriteMessage(ref WriteContext ctx, IMessage value)
52ffe3c632Sopenharmony_ci        {
53ffe3c632Sopenharmony_ci            WritingPrimitives.WriteLength(ref ctx.buffer, ref ctx.state, value.CalculateSize());
54ffe3c632Sopenharmony_ci            WriteRawMessage(ref ctx, value);
55ffe3c632Sopenharmony_ci        }
56ffe3c632Sopenharmony_ci
57ffe3c632Sopenharmony_ci        /// <summary>
58ffe3c632Sopenharmony_ci        /// Writes a group, without a tag.
59ffe3c632Sopenharmony_ci        /// </summary>
60ffe3c632Sopenharmony_ci        [MethodImpl(MethodImplOptions.AggressiveInlining)]
61ffe3c632Sopenharmony_ci        public static void WriteGroup(ref WriteContext ctx, IMessage value)
62ffe3c632Sopenharmony_ci        {
63ffe3c632Sopenharmony_ci            WriteRawMessage(ref ctx, value);
64ffe3c632Sopenharmony_ci        }
65ffe3c632Sopenharmony_ci
66ffe3c632Sopenharmony_ci        /// <summary>
67ffe3c632Sopenharmony_ci        /// Writes a message, without a tag.
68ffe3c632Sopenharmony_ci        /// Message will be written without a length prefix.
69ffe3c632Sopenharmony_ci        /// </summary>
70ffe3c632Sopenharmony_ci        [MethodImpl(MethodImplOptions.AggressiveInlining)]
71ffe3c632Sopenharmony_ci        public static void WriteRawMessage(ref WriteContext ctx, IMessage message)
72ffe3c632Sopenharmony_ci        {
73ffe3c632Sopenharmony_ci            if (message is IBufferMessage bufferMessage)
74ffe3c632Sopenharmony_ci            {
75ffe3c632Sopenharmony_ci                bufferMessage.InternalWriteTo(ref ctx);
76ffe3c632Sopenharmony_ci            }
77ffe3c632Sopenharmony_ci            else
78ffe3c632Sopenharmony_ci            {
79ffe3c632Sopenharmony_ci                // If we reached here, it means we've ran into a nested message with older generated code
80ffe3c632Sopenharmony_ci                // which doesn't provide the InternalWriteTo method that takes a WriteContext.
81ffe3c632Sopenharmony_ci                // With a slight performance overhead, we can still serialize this message just fine,
82ffe3c632Sopenharmony_ci                // but we need to find the original CodedOutputStream instance that initiated this
83ffe3c632Sopenharmony_ci                // serialization process and make sure its internal state is up to date.
84ffe3c632Sopenharmony_ci                // Note that this performance overhead is not very high (basically copying contents of a struct)
85ffe3c632Sopenharmony_ci                // and it will only be incurred in case the application mixes older and newer generated code.
86ffe3c632Sopenharmony_ci                // Regenerating the code from .proto files will remove this overhead because it will
87ffe3c632Sopenharmony_ci                // generate the InternalWriteTo method we need.
88ffe3c632Sopenharmony_ci
89ffe3c632Sopenharmony_ci                if (ctx.state.CodedOutputStream == null)
90ffe3c632Sopenharmony_ci                {
91ffe3c632Sopenharmony_ci                    // This can only happen when the serialization started without providing a CodedOutputStream instance
92ffe3c632Sopenharmony_ci                    // (e.g. WriteContext was created directly from a IBufferWriter).
93ffe3c632Sopenharmony_ci                    // That also means that one of the new parsing APIs was used at the top level
94ffe3c632Sopenharmony_ci                    // and in such case it is reasonable to require that all the nested message provide
95ffe3c632Sopenharmony_ci                    // up-to-date generated code with WriteContext support (and fail otherwise).
96ffe3c632Sopenharmony_ci                    throw new InvalidProtocolBufferException($"Message {message.GetType().Name} doesn't provide the generated method that enables WriteContext-based serialization. You might need to regenerate the generated protobuf code.");
97ffe3c632Sopenharmony_ci                }
98ffe3c632Sopenharmony_ci
99ffe3c632Sopenharmony_ci                ctx.CopyStateTo(ctx.state.CodedOutputStream);
100ffe3c632Sopenharmony_ci                try
101ffe3c632Sopenharmony_ci                {
102ffe3c632Sopenharmony_ci                    // fallback parse using the CodedOutputStream that started current serialization tree
103ffe3c632Sopenharmony_ci                    message.WriteTo(ctx.state.CodedOutputStream);
104ffe3c632Sopenharmony_ci                }
105ffe3c632Sopenharmony_ci                finally
106ffe3c632Sopenharmony_ci                {
107ffe3c632Sopenharmony_ci                    ctx.LoadStateFrom(ctx.state.CodedOutputStream);
108ffe3c632Sopenharmony_ci                }
109ffe3c632Sopenharmony_ci            }
110ffe3c632Sopenharmony_ci        }
111ffe3c632Sopenharmony_ci    }
112ffe3c632Sopenharmony_ci}