• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 // https://developers.google.com/protocol-buffers/
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 //     * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #endregion
32 
33 using System;
34 
35 namespace Google.Protobuf
36 {
37     // This part of CodedOutputStream provides all the static entry points that are used
38     // by generated code and internally to compute the size of messages prior to being
39     // written to an instance of CodedOutputStream.
40     public sealed partial class CodedOutputStream
41     {
42         private const int LittleEndian64Size = 8;
43         private const int LittleEndian32Size = 4;
44 
45         internal const int DoubleSize = LittleEndian64Size;
46         internal const int FloatSize = LittleEndian32Size;
47         internal const int BoolSize = 1;
48 
49         /// <summary>
50         /// Computes the number of bytes that would be needed to encode a
51         /// double field, including the tag.
52         /// </summary>
ComputeDoubleSize(double value)53         public static int ComputeDoubleSize(double value)
54         {
55             return DoubleSize;
56         }
57 
58         /// <summary>
59         /// Computes the number of bytes that would be needed to encode a
60         /// float field, including the tag.
61         /// </summary>
ComputeFloatSize(float value)62         public static int ComputeFloatSize(float value)
63         {
64             return FloatSize;
65         }
66 
67         /// <summary>
68         /// Computes the number of bytes that would be needed to encode a
69         /// uint64 field, including the tag.
70         /// </summary>
ComputeUInt64Size(ulong value)71         public static int ComputeUInt64Size(ulong value)
72         {
73             return ComputeRawVarint64Size(value);
74         }
75 
76         /// <summary>
77         /// Computes the number of bytes that would be needed to encode an
78         /// int64 field, including the tag.
79         /// </summary>
ComputeInt64Size(long value)80         public static int ComputeInt64Size(long value)
81         {
82             return ComputeRawVarint64Size((ulong) value);
83         }
84 
85         /// <summary>
86         /// Computes the number of bytes that would be needed to encode an
87         /// int32 field, including the tag.
88         /// </summary>
ComputeInt32Size(int value)89         public static int ComputeInt32Size(int value)
90         {
91             if (value >= 0)
92             {
93                 return ComputeRawVarint32Size((uint) value);
94             }
95             else
96             {
97                 // Must sign-extend.
98                 return 10;
99             }
100         }
101 
102         /// <summary>
103         /// Computes the number of bytes that would be needed to encode a
104         /// fixed64 field, including the tag.
105         /// </summary>
ComputeFixed64Size(ulong value)106         public static int ComputeFixed64Size(ulong value)
107         {
108             return LittleEndian64Size;
109         }
110 
111         /// <summary>
112         /// Computes the number of bytes that would be needed to encode a
113         /// fixed32 field, including the tag.
114         /// </summary>
ComputeFixed32Size(uint value)115         public static int ComputeFixed32Size(uint value)
116         {
117             return LittleEndian32Size;
118         }
119 
120         /// <summary>
121         /// Computes the number of bytes that would be needed to encode a
122         /// bool field, including the tag.
123         /// </summary>
ComputeBoolSize(bool value)124         public static int ComputeBoolSize(bool value)
125         {
126             return BoolSize;
127         }
128 
129         /// <summary>
130         /// Computes the number of bytes that would be needed to encode a
131         /// string field, including the tag.
132         /// </summary>
ComputeStringSize(String value)133         public static int ComputeStringSize(String value)
134         {
135             int byteArraySize = WritingPrimitives.Utf8Encoding.GetByteCount(value);
136             return ComputeLengthSize(byteArraySize) + byteArraySize;
137         }
138 
139         /// <summary>
140         /// Computes the number of bytes that would be needed to encode a
141         /// group field, including the tag.
142         /// </summary>
ComputeGroupSize(IMessage value)143         public static int ComputeGroupSize(IMessage value)
144         {
145             return value.CalculateSize();
146         }
147 
148         /// <summary>
149         /// Computes the number of bytes that would be needed to encode an
150         /// embedded message field, including the tag.
151         /// </summary>
ComputeMessageSize(IMessage value)152         public static int ComputeMessageSize(IMessage value)
153         {
154             int size = value.CalculateSize();
155             return ComputeLengthSize(size) + size;
156         }
157 
158         /// <summary>
159         /// Computes the number of bytes that would be needed to encode a
160         /// bytes field, including the tag.
161         /// </summary>
ComputeBytesSize(ByteString value)162         public static int ComputeBytesSize(ByteString value)
163         {
164             return ComputeLengthSize(value.Length) + value.Length;
165         }
166 
167         /// <summary>
168         /// Computes the number of bytes that would be needed to encode a
169         /// uint32 field, including the tag.
170         /// </summary>
ComputeUInt32Size(uint value)171         public static int ComputeUInt32Size(uint value)
172         {
173             return ComputeRawVarint32Size(value);
174         }
175 
176         /// <summary>
177         /// Computes the number of bytes that would be needed to encode a
178         /// enum field, including the tag. The caller is responsible for
179         /// converting the enum value to its numeric value.
180         /// </summary>
ComputeEnumSize(int value)181         public static int ComputeEnumSize(int value)
182         {
183             // Currently just a pass-through, but it's nice to separate it logically.
184             return ComputeInt32Size(value);
185         }
186 
187         /// <summary>
188         /// Computes the number of bytes that would be needed to encode an
189         /// sfixed32 field, including the tag.
190         /// </summary>
ComputeSFixed32Size(int value)191         public static int ComputeSFixed32Size(int value)
192         {
193             return LittleEndian32Size;
194         }
195 
196         /// <summary>
197         /// Computes the number of bytes that would be needed to encode an
198         /// sfixed64 field, including the tag.
199         /// </summary>
ComputeSFixed64Size(long value)200         public static int ComputeSFixed64Size(long value)
201         {
202             return LittleEndian64Size;
203         }
204 
205         /// <summary>
206         /// Computes the number of bytes that would be needed to encode an
207         /// sint32 field, including the tag.
208         /// </summary>
ComputeSInt32Size(int value)209         public static int ComputeSInt32Size(int value)
210         {
211             return ComputeRawVarint32Size(WritingPrimitives.EncodeZigZag32(value));
212         }
213 
214         /// <summary>
215         /// Computes the number of bytes that would be needed to encode an
216         /// sint64 field, including the tag.
217         /// </summary>
ComputeSInt64Size(long value)218         public static int ComputeSInt64Size(long value)
219         {
220             return ComputeRawVarint64Size(WritingPrimitives.EncodeZigZag64(value));
221         }
222 
223         /// <summary>
224         /// Computes the number of bytes that would be needed to encode a length,
225         /// as written by <see cref="WriteLength"/>.
226         /// </summary>
ComputeLengthSize(int length)227         public static int ComputeLengthSize(int length)
228         {
229             return ComputeRawVarint32Size((uint) length);
230         }
231 
232         /// <summary>
233         /// Computes the number of bytes that would be needed to encode a varint.
234         /// </summary>
ComputeRawVarint32Size(uint value)235         public static int ComputeRawVarint32Size(uint value)
236         {
237             if ((value & (0xffffffff << 7)) == 0)
238             {
239                 return 1;
240             }
241             if ((value & (0xffffffff << 14)) == 0)
242             {
243                 return 2;
244             }
245             if ((value & (0xffffffff << 21)) == 0)
246             {
247                 return 3;
248             }
249             if ((value & (0xffffffff << 28)) == 0)
250             {
251                 return 4;
252             }
253             return 5;
254         }
255 
256         /// <summary>
257         /// Computes the number of bytes that would be needed to encode a varint.
258         /// </summary>
ComputeRawVarint64Size(ulong value)259         public static int ComputeRawVarint64Size(ulong value)
260         {
261             if ((value & (0xffffffffffffffffL << 7)) == 0)
262             {
263                 return 1;
264             }
265             if ((value & (0xffffffffffffffffL << 14)) == 0)
266             {
267                 return 2;
268             }
269             if ((value & (0xffffffffffffffffL << 21)) == 0)
270             {
271                 return 3;
272             }
273             if ((value & (0xffffffffffffffffL << 28)) == 0)
274             {
275                 return 4;
276             }
277             if ((value & (0xffffffffffffffffL << 35)) == 0)
278             {
279                 return 5;
280             }
281             if ((value & (0xffffffffffffffffL << 42)) == 0)
282             {
283                 return 6;
284             }
285             if ((value & (0xffffffffffffffffL << 49)) == 0)
286             {
287                 return 7;
288             }
289             if ((value & (0xffffffffffffffffL << 56)) == 0)
290             {
291                 return 8;
292             }
293             if ((value & (0xffffffffffffffffL << 63)) == 0)
294             {
295                 return 9;
296             }
297             return 10;
298         }
299 
300         /// <summary>
301         /// Computes the number of bytes that would be needed to encode a tag.
302         /// </summary>
ComputeTagSize(int fieldNumber)303         public static int ComputeTagSize(int fieldNumber)
304         {
305             return ComputeRawVarint32Size(WireFormat.MakeTag(fieldNumber, 0));
306         }
307     }
308 }