xref: /aosp_15_r20/external/protobuf/csharp/src/Google.Protobuf/InvalidProtocolBufferException.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.IO;
35*1b3f573fSAndroid Build Coastguard Worker 
36*1b3f573fSAndroid Build Coastguard Worker namespace Google.Protobuf
37*1b3f573fSAndroid Build Coastguard Worker {
38*1b3f573fSAndroid Build Coastguard Worker     /// <summary>
39*1b3f573fSAndroid Build Coastguard Worker     /// Thrown when a protocol message being parsed is invalid in some way,
40*1b3f573fSAndroid Build Coastguard Worker     /// e.g. it contains a malformed varint or a negative byte length.
41*1b3f573fSAndroid Build Coastguard Worker     /// </summary>
42*1b3f573fSAndroid Build Coastguard Worker     public sealed class InvalidProtocolBufferException : IOException
43*1b3f573fSAndroid Build Coastguard Worker     {
InvalidProtocolBufferException(string message)44*1b3f573fSAndroid Build Coastguard Worker         internal InvalidProtocolBufferException(string message)
45*1b3f573fSAndroid Build Coastguard Worker             : base(message)
46*1b3f573fSAndroid Build Coastguard Worker         {
47*1b3f573fSAndroid Build Coastguard Worker         }
48*1b3f573fSAndroid Build Coastguard Worker 
InvalidProtocolBufferException(string message, Exception innerException)49*1b3f573fSAndroid Build Coastguard Worker         internal InvalidProtocolBufferException(string message, Exception innerException)
50*1b3f573fSAndroid Build Coastguard Worker             : base(message, innerException)
51*1b3f573fSAndroid Build Coastguard Worker         {
52*1b3f573fSAndroid Build Coastguard Worker         }
53*1b3f573fSAndroid Build Coastguard Worker 
MoreDataAvailable()54*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException MoreDataAvailable()
55*1b3f573fSAndroid Build Coastguard Worker         {
56*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
57*1b3f573fSAndroid Build Coastguard Worker                 "Completed reading a message while more data was available in the stream.");
58*1b3f573fSAndroid Build Coastguard Worker         }
59*1b3f573fSAndroid Build Coastguard Worker 
TruncatedMessage()60*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException TruncatedMessage()
61*1b3f573fSAndroid Build Coastguard Worker         {
62*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
63*1b3f573fSAndroid Build Coastguard Worker                 "While parsing a protocol message, the input ended unexpectedly " +
64*1b3f573fSAndroid Build Coastguard Worker                 "in the middle of a field.  This could mean either that the " +
65*1b3f573fSAndroid Build Coastguard Worker                 "input has been truncated or that an embedded message " +
66*1b3f573fSAndroid Build Coastguard Worker                 "misreported its own length.");
67*1b3f573fSAndroid Build Coastguard Worker         }
68*1b3f573fSAndroid Build Coastguard Worker 
NegativeSize()69*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException NegativeSize()
70*1b3f573fSAndroid Build Coastguard Worker         {
71*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
72*1b3f573fSAndroid Build Coastguard Worker                 "CodedInputStream encountered an embedded string or message " +
73*1b3f573fSAndroid Build Coastguard Worker                 "which claimed to have negative size.");
74*1b3f573fSAndroid Build Coastguard Worker         }
75*1b3f573fSAndroid Build Coastguard Worker 
MalformedVarint()76*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException MalformedVarint()
77*1b3f573fSAndroid Build Coastguard Worker         {
78*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
79*1b3f573fSAndroid Build Coastguard Worker                 "CodedInputStream encountered a malformed varint.");
80*1b3f573fSAndroid Build Coastguard Worker         }
81*1b3f573fSAndroid Build Coastguard Worker 
82*1b3f573fSAndroid Build Coastguard Worker         /// <summary>
83*1b3f573fSAndroid Build Coastguard Worker         /// Creates an exception for an error condition of an invalid tag being encountered.
84*1b3f573fSAndroid Build Coastguard Worker         /// </summary>
InvalidTag()85*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException InvalidTag()
86*1b3f573fSAndroid Build Coastguard Worker         {
87*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
88*1b3f573fSAndroid Build Coastguard Worker                 "Protocol message contained an invalid tag (zero).");
89*1b3f573fSAndroid Build Coastguard Worker         }
90*1b3f573fSAndroid Build Coastguard Worker 
InvalidWireType()91*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException InvalidWireType()
92*1b3f573fSAndroid Build Coastguard Worker         {
93*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
94*1b3f573fSAndroid Build Coastguard Worker                 "Protocol message contained a tag with an invalid wire type.");
95*1b3f573fSAndroid Build Coastguard Worker         }
96*1b3f573fSAndroid Build Coastguard Worker 
InvalidBase64(Exception innerException)97*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException InvalidBase64(Exception innerException)
98*1b3f573fSAndroid Build Coastguard Worker         {
99*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException("Invalid base64 data", innerException);
100*1b3f573fSAndroid Build Coastguard Worker         }
101*1b3f573fSAndroid Build Coastguard Worker 
InvalidEndTag()102*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException InvalidEndTag()
103*1b3f573fSAndroid Build Coastguard Worker         {
104*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
105*1b3f573fSAndroid Build Coastguard Worker                 "Protocol message end-group tag did not match expected tag.");
106*1b3f573fSAndroid Build Coastguard Worker         }
107*1b3f573fSAndroid Build Coastguard Worker 
RecursionLimitExceeded()108*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException RecursionLimitExceeded()
109*1b3f573fSAndroid Build Coastguard Worker         {
110*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
111*1b3f573fSAndroid Build Coastguard Worker                 "Protocol message had too many levels of nesting.  May be malicious.  " +
112*1b3f573fSAndroid Build Coastguard Worker                 "Use CodedInputStream.SetRecursionLimit() to increase the depth limit.");
113*1b3f573fSAndroid Build Coastguard Worker         }
114*1b3f573fSAndroid Build Coastguard Worker 
JsonRecursionLimitExceeded()115*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException JsonRecursionLimitExceeded()
116*1b3f573fSAndroid Build Coastguard Worker         {
117*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
118*1b3f573fSAndroid Build Coastguard Worker                 "Protocol message had too many levels of nesting.  May be malicious.  " +
119*1b3f573fSAndroid Build Coastguard Worker                 "Use JsonParser.Settings to increase the depth limit.");
120*1b3f573fSAndroid Build Coastguard Worker         }
121*1b3f573fSAndroid Build Coastguard Worker 
SizeLimitExceeded()122*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException SizeLimitExceeded()
123*1b3f573fSAndroid Build Coastguard Worker         {
124*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
125*1b3f573fSAndroid Build Coastguard Worker                 "Protocol message was too large.  May be malicious.  " +
126*1b3f573fSAndroid Build Coastguard Worker                 "Use CodedInputStream.SetSizeLimit() to increase the size limit.");
127*1b3f573fSAndroid Build Coastguard Worker         }
128*1b3f573fSAndroid Build Coastguard Worker 
InvalidMessageStreamTag()129*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException InvalidMessageStreamTag()
130*1b3f573fSAndroid Build Coastguard Worker         {
131*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException(
132*1b3f573fSAndroid Build Coastguard Worker                 "Stream of protocol messages had invalid tag. Expected tag is length-delimited field 1.");
133*1b3f573fSAndroid Build Coastguard Worker         }
134*1b3f573fSAndroid Build Coastguard Worker 
MissingFields()135*1b3f573fSAndroid Build Coastguard Worker         internal static InvalidProtocolBufferException MissingFields()
136*1b3f573fSAndroid Build Coastguard Worker         {
137*1b3f573fSAndroid Build Coastguard Worker             return new InvalidProtocolBufferException("Message was missing required fields");
138*1b3f573fSAndroid Build Coastguard Worker         }
139*1b3f573fSAndroid Build Coastguard Worker }
140*1b3f573fSAndroid Build Coastguard Worker }