InvalidProtocolBufferException.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System.IO;
  17. namespace Google.ProtocolBuffers {
  18. /// <summary>
  19. /// Thrown when a protocol message being parsed is invalid in some way,
  20. /// e.g. it contains a malformed varint or a negative byte length.
  21. ///
  22. /// TODO(jonskeet): Make the methods throw directly? Rename them?
  23. /// </summary>
  24. public class InvalidProtocolBufferException : IOException {
  25. private InvalidProtocolBufferException(string message)
  26. : base(message) {
  27. }
  28. /// TODO(jonskeet): Make this internal again and use InternalVisibleTo?
  29. public static InvalidProtocolBufferException TruncatedMessage() {
  30. return new InvalidProtocolBufferException(
  31. "While parsing a protocol message, the input ended unexpectedly " +
  32. "in the middle of a field. This could mean either than the " +
  33. "input has been truncated or that an embedded message " +
  34. "misreported its own length.");
  35. }
  36. /// TODO(jonskeet): Make this internal again and use InternalVisibleTo?
  37. internal static InvalidProtocolBufferException NegativeSize() {
  38. return new InvalidProtocolBufferException(
  39. "CodedInputStream encountered an embedded string or message " +
  40. "which claimed to have negative size.");
  41. }
  42. public static InvalidProtocolBufferException MalformedVarint() {
  43. return new InvalidProtocolBufferException(
  44. "CodedInputStream encountered a malformed varint.");
  45. }
  46. internal static InvalidProtocolBufferException InvalidTag() {
  47. return new InvalidProtocolBufferException(
  48. "Protocol message contained an invalid tag (zero).");
  49. }
  50. internal static InvalidProtocolBufferException InvalidEndTag() {
  51. return new InvalidProtocolBufferException(
  52. "Protocol message end-group tag did not match expected tag.");
  53. }
  54. internal static InvalidProtocolBufferException InvalidWireType() {
  55. return new InvalidProtocolBufferException(
  56. "Protocol message tag had invalid wire type.");
  57. }
  58. internal static InvalidProtocolBufferException RecursionLimitExceeded() {
  59. return new InvalidProtocolBufferException(
  60. "Protocol message had too many levels of nesting. May be malicious. " +
  61. "Use CodedInputStream.setRecursionLimit() to increase the depth limit.");
  62. }
  63. internal static InvalidProtocolBufferException SizeLimitExceeded() {
  64. return new InvalidProtocolBufferException(
  65. "Protocol message was too large. May be malicious. " +
  66. "Use CodedInputStream.setSizeLimit() to increase the size limit.");
  67. }
  68. }
  69. }