GPBCodedInputStream_PackagePrivate.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // This header is private to the ProtobolBuffers library and must NOT be
  31. // included by any sources outside this library. The contents of this file are
  32. // subject to change at any time without notice.
  33. #import "GPBCodedInputStream.h"
  34. #import <libkern/OSAtomic.h>
  35. @class GPBUnknownFieldSet;
  36. @class GPBFieldDescriptor;
  37. // GPBString is a string subclass that avoids the overhead of initializing
  38. // a full NSString until it is actually needed. Lots of protocol buffers contain
  39. // strings, and instantiating all of those strings and having them parsed to
  40. // verify correctness when the message was being read was expensive, when many
  41. // of the strings were never being used.
  42. //
  43. // Note for future-self. I tried implementing this using a NSProxy.
  44. // Turned out the performance was horrible in client apps because folks
  45. // like to use libraries like SBJSON that grab characters one at a time.
  46. // The proxy overhead was a killer.
  47. @interface GPBString : NSString
  48. @end
  49. typedef struct GPBCodedInputStreamState {
  50. const uint8_t *bytes;
  51. size_t bufferSize;
  52. size_t bufferPos;
  53. // For parsing subsections of an input stream you can put a hard limit on
  54. // how much should be read. Normally the limit is the end of the stream,
  55. // but you can adjust it to anywhere, and if you hit it you will be at the
  56. // end of the stream, until you adjust the limit.
  57. size_t currentLimit;
  58. int32_t lastTag;
  59. NSUInteger recursionDepth;
  60. } GPBCodedInputStreamState;
  61. @interface GPBCodedInputStream () {
  62. @package
  63. struct GPBCodedInputStreamState state_;
  64. NSData *buffer_;
  65. }
  66. // Group support is deprecated, so we hide this interface from users, but
  67. // support for older data.
  68. - (void)readGroup:(int32_t)fieldNumber
  69. message:(GPBMessage *)message
  70. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry;
  71. // Reads a group field value from the stream and merges it into the given
  72. // UnknownFieldSet.
  73. - (void)readUnknownGroup:(int32_t)fieldNumber
  74. message:(GPBUnknownFieldSet *)message;
  75. // Reads a map entry.
  76. - (void)readMapEntry:(id)mapDictionary
  77. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  78. field:(GPBFieldDescriptor *)field
  79. parentMessage:(GPBMessage *)parentMessage;
  80. @end
  81. CF_EXTERN_C_BEGIN
  82. // Returns a GPBString with a +1 retain count.
  83. GPBString *GPBCreateGPBStringWithUTF8(const void *bytes, NSUInteger length)
  84. __attribute__((ns_returns_retained));
  85. int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state);
  86. double GPBCodedInputStreamReadDouble(GPBCodedInputStreamState *state);
  87. float GPBCodedInputStreamReadFloat(GPBCodedInputStreamState *state);
  88. uint64_t GPBCodedInputStreamReadUInt64(GPBCodedInputStreamState *state);
  89. uint32_t GPBCodedInputStreamReadUInt32(GPBCodedInputStreamState *state);
  90. int64_t GPBCodedInputStreamReadInt64(GPBCodedInputStreamState *state);
  91. int32_t GPBCodedInputStreamReadInt32(GPBCodedInputStreamState *state);
  92. uint64_t GPBCodedInputStreamReadFixed64(GPBCodedInputStreamState *state);
  93. uint32_t GPBCodedInputStreamReadFixed32(GPBCodedInputStreamState *state);
  94. int32_t GPBCodedInputStreamReadEnum(GPBCodedInputStreamState *state);
  95. int32_t GPBCodedInputStreamReadSFixed32(GPBCodedInputStreamState *state);
  96. int64_t GPBCodedInputStreamReadSFixed64(GPBCodedInputStreamState *state);
  97. int32_t GPBCodedInputStreamReadSInt32(GPBCodedInputStreamState *state);
  98. int64_t GPBCodedInputStreamReadSInt64(GPBCodedInputStreamState *state);
  99. BOOL GPBCodedInputStreamReadBool(GPBCodedInputStreamState *state);
  100. NSString *GPBCodedInputStreamReadRetainedString(GPBCodedInputStreamState *state)
  101. __attribute((ns_returns_retained));
  102. NSData *GPBCodedInputStreamReadRetainedBytes(GPBCodedInputStreamState *state)
  103. __attribute((ns_returns_retained));
  104. NSData *GPBCodedInputStreamReadRetainedBytesNoCopy(
  105. GPBCodedInputStreamState *state) __attribute((ns_returns_retained));
  106. size_t GPBCodedInputStreamPushLimit(GPBCodedInputStreamState *state,
  107. size_t byteLimit);
  108. void GPBCodedInputStreamPopLimit(GPBCodedInputStreamState *state,
  109. size_t oldLimit);
  110. size_t GPBCodedInputStreamBytesUntilLimit(GPBCodedInputStreamState *state);
  111. BOOL GPBCodedInputStreamIsAtEnd(GPBCodedInputStreamState *state);
  112. void GPBCodedInputStreamCheckLastTagWas(GPBCodedInputStreamState *state,
  113. int32_t value);
  114. CF_EXTERN_C_END