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