~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to protobuf/files/src/google/protobuf/io/coded_stream.h

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Protocol Buffers - Google's data interchange format
2
 
// Copyright 2008 Google Inc.  All rights reserved.
3
 
// http://code.google.com/p/protobuf/
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
 
 
31
 
// Author: kenton@google.com (Kenton Varda)
32
 
//  Based on original Protocol Buffers design by
33
 
//  Sanjay Ghemawat, Jeff Dean, and others.
34
 
//
35
 
// This file contains the CodedInputStream and CodedOutputStream classes,
36
 
// which wrap a ZeroCopyInputStream or ZeroCopyOutputStream, respectively,
37
 
// and allow you to read or write individual pieces of data in various
38
 
// formats.  In particular, these implement the varint encoding for
39
 
// integers, a simple variable-length encoding in which smaller numbers
40
 
// take fewer bytes.
41
 
//
42
 
// Typically these classes will only be used internally by the protocol
43
 
// buffer library in order to encode and decode protocol buffers.  Clients
44
 
// of the library only need to know about this class if they wish to write
45
 
// custom message parsing or serialization procedures.
46
 
//
47
 
// CodedOutputStream example:
48
 
//   // Write some data to "myfile".  First we write a 4-byte "magic number"
49
 
//   // to identify the file type, then write a length-delimited string.  The
50
 
//   // string is composed of a varint giving the length followed by the raw
51
 
//   // bytes.
52
 
//   int fd = open("myfile", O_WRONLY);
53
 
//   ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
54
 
//   CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
55
 
//
56
 
//   int magic_number = 1234;
57
 
//   char text[] = "Hello world!";
58
 
//   coded_output->WriteLittleEndian32(magic_number);
59
 
//   coded_output->WriteVarint32(strlen(text));
60
 
//   coded_output->WriteRaw(text, strlen(text));
61
 
//
62
 
//   delete coded_output;
63
 
//   delete raw_output;
64
 
//   close(fd);
65
 
//
66
 
// CodedInputStream example:
67
 
//   // Read a file created by the above code.
68
 
//   int fd = open("myfile", O_RDONLY);
69
 
//   ZeroCopyInputStream* raw_input = new FileInputStream(fd);
70
 
//   CodedInputStream coded_input = new CodedInputStream(raw_input);
71
 
//
72
 
//   coded_input->ReadLittleEndian32(&magic_number);
73
 
//   if (magic_number != 1234) {
74
 
//     cerr << "File not in expected format." << endl;
75
 
//     return;
76
 
//   }
77
 
//
78
 
//   uint32 size;
79
 
//   coded_input->ReadVarint32(&size);
80
 
//
81
 
//   char* text = new char[size + 1];
82
 
//   coded_input->ReadRaw(buffer, size);
83
 
//   text[size] = '\0';
84
 
//
85
 
//   delete coded_input;
86
 
//   delete raw_input;
87
 
//   close(fd);
88
 
//
89
 
//   cout << "Text is: " << text << endl;
90
 
//   delete [] text;
91
 
//
92
 
// For those who are interested, varint encoding is defined as follows:
93
 
//
94
 
// The encoding operates on unsigned integers of up to 64 bits in length.
95
 
// Each byte of the encoded value has the format:
96
 
// * bits 0-6: Seven bits of the number being encoded.
97
 
// * bit 7: Zero if this is the last byte in the encoding (in which
98
 
//   case all remaining bits of the number are zero) or 1 if
99
 
//   more bytes follow.
100
 
// The first byte contains the least-significant 7 bits of the number, the
101
 
// second byte (if present) contains the next-least-significant 7 bits,
102
 
// and so on.  So, the binary number 1011000101011 would be encoded in two
103
 
// bytes as "10101011 00101100".
104
 
//
105
 
// In theory, varint could be used to encode integers of any length.
106
 
// However, for practicality we set a limit at 64 bits.  The maximum encoded
107
 
// length of a number is thus 10 bytes.
108
 
 
109
 
#ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
110
 
#define GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
111
 
 
112
 
#include <string>
113
 
#ifdef _MSC_VER
114
 
  #if defined(_M_IX86) && \
115
 
      !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
116
 
    #define PROTOBUF_LITTLE_ENDIAN 1
117
 
  #endif
118
 
  #if _MSC_VER >= 1300
119
 
    // If MSVC has "/RTCc" set, it will complain about truncating casts at
120
 
    // runtime.  This file contains some intentional truncating casts.
121
 
    #pragma runtime_checks("c", off)
122
 
  #endif
123
 
#else
124
 
  #include <sys/param.h>   // __BYTE_ORDER
125
 
  #if defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN && \
126
 
      !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
127
 
    #define PROTOBUF_LITTLE_ENDIAN 1
128
 
  #endif
129
 
#endif
130
 
#include <google/protobuf/stubs/common.h>
131
 
 
132
 
namespace google {
133
 
 
134
 
namespace protobuf {
135
 
 
136
 
class DescriptorPool;
137
 
class MessageFactory;
138
 
 
139
 
namespace io {
140
 
 
141
 
// Defined in this file.
142
 
class CodedInputStream;
143
 
class CodedOutputStream;
144
 
 
145
 
// Defined in other files.
146
 
class ZeroCopyInputStream;           // zero_copy_stream.h
147
 
class ZeroCopyOutputStream;          // zero_copy_stream.h
148
 
 
149
 
// Class which reads and decodes binary data which is composed of varint-
150
 
// encoded integers and fixed-width pieces.  Wraps a ZeroCopyInputStream.
151
 
// Most users will not need to deal with CodedInputStream.
152
 
//
153
 
// Most methods of CodedInputStream that return a bool return false if an
154
 
// underlying I/O error occurs or if the data is malformed.  Once such a
155
 
// failure occurs, the CodedInputStream is broken and is no longer useful.
156
 
class LIBPROTOBUF_EXPORT CodedInputStream {
157
 
 public:
158
 
  // Create a CodedInputStream that reads from the given ZeroCopyInputStream.
159
 
  explicit CodedInputStream(ZeroCopyInputStream* input);
160
 
 
161
 
  // Create a CodedInputStream that reads from the given flat array.  This is
162
 
  // faster than using an ArrayInputStream.  PushLimit(size) is implied by
163
 
  // this constructor.
164
 
  explicit CodedInputStream(const uint8* buffer, int size);
165
 
 
166
 
  // Destroy the CodedInputStream and position the underlying
167
 
  // ZeroCopyInputStream at the first unread byte.  If an error occurred while
168
 
  // reading (causing a method to return false), then the exact position of
169
 
  // the input stream may be anywhere between the last value that was read
170
 
  // successfully and the stream's byte limit.
171
 
  ~CodedInputStream();
172
 
 
173
 
 
174
 
  // Skips a number of bytes.  Returns false if an underlying read error
175
 
  // occurs.
176
 
  bool Skip(int count);
177
 
 
178
 
  // Sets *data to point directly at the unread part of the CodedInputStream's
179
 
  // underlying buffer, and *size to the size of that buffer, but does not
180
 
  // advance the stream's current position.  This will always either produce
181
 
  // a non-empty buffer or return false.  If the caller consumes any of
182
 
  // this data, it should then call Skip() to skip over the consumed bytes.
183
 
  // This may be useful for implementing external fast parsing routines for
184
 
  // types of data not covered by the CodedInputStream interface.
185
 
  bool GetDirectBufferPointer(const void** data, int* size);
186
 
 
187
 
  // Like GetDirectBufferPointer, but this method is inlined, and does not
188
 
  // attempt to Refresh() if the buffer is currently empty.
189
 
  inline void GetDirectBufferPointerInline(const void** data,
190
 
                                           int* size) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
191
 
 
192
 
  // Read raw bytes, copying them into the given buffer.
193
 
  bool ReadRaw(void* buffer, int size);
194
 
 
195
 
  // Like ReadRaw, but reads into a string.
196
 
  //
197
 
  // Implementation Note:  ReadString() grows the string gradually as it
198
 
  // reads in the data, rather than allocating the entire requested size
199
 
  // upfront.  This prevents denial-of-service attacks in which a client
200
 
  // could claim that a string is going to be MAX_INT bytes long in order to
201
 
  // crash the server because it can't allocate this much space at once.
202
 
  bool ReadString(string* buffer, int size);
203
 
  // Like the above, with inlined optimizations. This should only be used
204
 
  // by the protobuf implementation.
205
 
  inline bool InternalReadStringInline(string* buffer,
206
 
                                       int size) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
207
 
 
208
 
 
209
 
  // Read a 32-bit little-endian integer.
210
 
  bool ReadLittleEndian32(uint32* value);
211
 
  // Read a 64-bit little-endian integer.
212
 
  bool ReadLittleEndian64(uint64* value);
213
 
 
214
 
  // These methods read from an externally provided buffer. The caller is
215
 
  // responsible for ensuring that the buffer has sufficient space.
216
 
  // Read a 32-bit little-endian integer.
217
 
  static const uint8* ReadLittleEndian32FromArray(const uint8* buffer,
218
 
                                                   uint32* value);
219
 
  // Read a 64-bit little-endian integer.
220
 
  static const uint8* ReadLittleEndian64FromArray(const uint8* buffer,
221
 
                                                   uint64* value);
222
 
 
223
 
  // Read an unsigned integer with Varint encoding, truncating to 32 bits.
224
 
  // Reading a 32-bit value is equivalent to reading a 64-bit one and casting
225
 
  // it to uint32, but may be more efficient.
226
 
  bool ReadVarint32(uint32* value);
227
 
  // Read an unsigned integer with Varint encoding.
228
 
  bool ReadVarint64(uint64* value);
229
 
 
230
 
  // Read a tag.  This calls ReadVarint32() and returns the result, or returns
231
 
  // zero (which is not a valid tag) if ReadVarint32() fails.  Also, it updates
232
 
  // the last tag value, which can be checked with LastTagWas().
233
 
  // Always inline because this is only called in once place per parse loop
234
 
  // but it is called for every iteration of said loop, so it should be fast.
235
 
  // GCC doesn't want to inline this by default.
236
 
  uint32 ReadTag() GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
237
 
 
238
 
  // Usually returns true if calling ReadVarint32() now would produce the given
239
 
  // value.  Will always return false if ReadVarint32() would not return the
240
 
  // given value.  If ExpectTag() returns true, it also advances past
241
 
  // the varint.  For best performance, use a compile-time constant as the
242
 
  // parameter.
243
 
  // Always inline because this collapses to a small number of instructions
244
 
  // when given a constant parameter, but GCC doesn't want to inline by default.
245
 
  bool ExpectTag(uint32 expected) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
246
 
 
247
 
  // Like above, except this reads from the specified buffer. The caller is
248
 
  // responsible for ensuring that the buffer is large enough to read a varint
249
 
  // of the expected size. For best performance, use a compile-time constant as
250
 
  // the expected tag parameter.
251
 
  //
252
 
  // Returns a pointer beyond the expected tag if it was found, or NULL if it
253
 
  // was not.
254
 
  static const uint8* ExpectTagFromArray(
255
 
      const uint8* buffer,
256
 
      uint32 expected) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
257
 
 
258
 
  // Usually returns true if no more bytes can be read.  Always returns false
259
 
  // if more bytes can be read.  If ExpectAtEnd() returns true, a subsequent
260
 
  // call to LastTagWas() will act as if ReadTag() had been called and returned
261
 
  // zero, and ConsumedEntireMessage() will return true.
262
 
  bool ExpectAtEnd();
263
 
 
264
 
  // If the last call to ReadTag() returned the given value, returns true.
265
 
  // Otherwise, returns false;
266
 
  //
267
 
  // This is needed because parsers for some types of embedded messages
268
 
  // (with field type TYPE_GROUP) don't actually know that they've reached the
269
 
  // end of a message until they see an ENDGROUP tag, which was actually part
270
 
  // of the enclosing message.  The enclosing message would like to check that
271
 
  // tag to make sure it had the right number, so it calls LastTagWas() on
272
 
  // return from the embedded parser to check.
273
 
  bool LastTagWas(uint32 expected);
274
 
 
275
 
  // When parsing message (but NOT a group), this method must be called
276
 
  // immediately after MergeFromCodedStream() returns (if it returns true)
277
 
  // to further verify that the message ended in a legitimate way.  For
278
 
  // example, this verifies that parsing did not end on an end-group tag.
279
 
  // It also checks for some cases where, due to optimizations,
280
 
  // MergeFromCodedStream() can incorrectly return true.
281
 
  bool ConsumedEntireMessage();
282
 
 
283
 
  // Limits ----------------------------------------------------------
284
 
  // Limits are used when parsing length-delimited embedded messages.
285
 
  // After the message's length is read, PushLimit() is used to prevent
286
 
  // the CodedInputStream from reading beyond that length.  Once the
287
 
  // embedded message has been parsed, PopLimit() is called to undo the
288
 
  // limit.
289
 
 
290
 
  // Opaque type used with PushLimit() and PopLimit().  Do not modify
291
 
  // values of this type yourself.  The only reason that this isn't a
292
 
  // struct with private internals is for efficiency.
293
 
  typedef int Limit;
294
 
 
295
 
  // Places a limit on the number of bytes that the stream may read,
296
 
  // starting from the current position.  Once the stream hits this limit,
297
 
  // it will act like the end of the input has been reached until PopLimit()
298
 
  // is called.
299
 
  //
300
 
  // As the names imply, the stream conceptually has a stack of limits.  The
301
 
  // shortest limit on the stack is always enforced, even if it is not the
302
 
  // top limit.
303
 
  //
304
 
  // The value returned by PushLimit() is opaque to the caller, and must
305
 
  // be passed unchanged to the corresponding call to PopLimit().
306
 
  Limit PushLimit(int byte_limit);
307
 
 
308
 
  // Pops the last limit pushed by PushLimit().  The input must be the value
309
 
  // returned by that call to PushLimit().
310
 
  void PopLimit(Limit limit);
311
 
 
312
 
  // Returns the number of bytes left until the nearest limit on the
313
 
  // stack is hit, or -1 if no limits are in place.
314
 
  int BytesUntilLimit();
315
 
 
316
 
  // Total Bytes Limit -----------------------------------------------
317
 
  // To prevent malicious users from sending excessively large messages
318
 
  // and causing integer overflows or memory exhaustion, CodedInputStream
319
 
  // imposes a hard limit on the total number of bytes it will read.
320
 
 
321
 
  // Sets the maximum number of bytes that this CodedInputStream will read
322
 
  // before refusing to continue.  To prevent integer overflows in the
323
 
  // protocol buffers implementation, as well as to prevent servers from
324
 
  // allocating enormous amounts of memory to hold parsed messages, the
325
 
  // maximum message length should be limited to the shortest length that
326
 
  // will not harm usability.  The theoretical shortest message that could
327
 
  // cause integer overflows is 512MB.  The default limit is 64MB.  Apps
328
 
  // should set shorter limits if possible.  If warning_threshold is not -1,
329
 
  // a warning will be printed to stderr after warning_threshold bytes are
330
 
  // read.  An error will always be printed to stderr if the limit is
331
 
  // reached.
332
 
  //
333
 
  // This is unrelated to PushLimit()/PopLimit().
334
 
  //
335
 
  // Hint:  If you are reading this because your program is printing a
336
 
  //   warning about dangerously large protocol messages, you may be
337
 
  //   confused about what to do next.  The best option is to change your
338
 
  //   design such that excessively large messages are not necessary.
339
 
  //   For example, try to design file formats to consist of many small
340
 
  //   messages rather than a single large one.  If this is infeasible,
341
 
  //   you will need to increase the limit.  Chances are, though, that
342
 
  //   your code never constructs a CodedInputStream on which the limit
343
 
  //   can be set.  You probably parse messages by calling things like
344
 
  //   Message::ParseFromString().  In this case, you will need to change
345
 
  //   your code to instead construct some sort of ZeroCopyInputStream
346
 
  //   (e.g. an ArrayInputStream), construct a CodedInputStream around
347
 
  //   that, then call Message::ParseFromCodedStream() instead.  Then
348
 
  //   you can adjust the limit.  Yes, it's more work, but you're doing
349
 
  //   something unusual.
350
 
  void SetTotalBytesLimit(int total_bytes_limit, int warning_threshold);
351
 
 
352
 
  // Recursion Limit -------------------------------------------------
353
 
  // To prevent corrupt or malicious messages from causing stack overflows,
354
 
  // we must keep track of the depth of recursion when parsing embedded
355
 
  // messages and groups.  CodedInputStream keeps track of this because it
356
 
  // is the only object that is passed down the stack during parsing.
357
 
 
358
 
  // Sets the maximum recursion depth.  The default is 64.
359
 
  void SetRecursionLimit(int limit);
360
 
 
361
 
  // Increments the current recursion depth.  Returns true if the depth is
362
 
  // under the limit, false if it has gone over.
363
 
  bool IncrementRecursionDepth();
364
 
 
365
 
  // Decrements the recursion depth.
366
 
  void DecrementRecursionDepth();
367
 
 
368
 
  // Extension Registry ----------------------------------------------
369
 
  // ADVANCED USAGE:  99.9% of people can ignore this section.
370
 
  //
371
 
  // By default, when parsing extensions, the parser looks for extension
372
 
  // definitions in the pool which owns the outer message's Descriptor.
373
 
  // However, you may call SetExtensionRegistry() to provide an alternative
374
 
  // pool instead.  This makes it possible, for example, to parse a message
375
 
  // using a generated class, but represent some extensions using
376
 
  // DynamicMessage.
377
 
 
378
 
  // Set the pool used to look up extensions.  Most users do not need to call
379
 
  // this as the correct pool will be chosen automatically.
380
 
  //
381
 
  // WARNING:  It is very easy to misuse this.  Carefully read the requirements
382
 
  //   below.  Do not use this unless you are sure you need it.  Almost no one
383
 
  //   does.
384
 
  //
385
 
  // Let's say you are parsing a message into message object m, and you want
386
 
  // to take advantage of SetExtensionRegistry().  You must follow these
387
 
  // requirements:
388
 
  //
389
 
  // The given DescriptorPool must contain m->GetDescriptor().  It is not
390
 
  // sufficient for it to simply contain a descriptor that has the same name
391
 
  // and content -- it must be the *exact object*.  In other words:
392
 
  //   assert(pool->FindMessageTypeByName(m->GetDescriptor()->full_name()) ==
393
 
  //          m->GetDescriptor());
394
 
  // There are two ways to satisfy this requirement:
395
 
  // 1) Use m->GetDescriptor()->pool() as the pool.  This is generally useless
396
 
  //    because this is the pool that would be used anyway if you didn't call
397
 
  //    SetExtensionRegistry() at all.
398
 
  // 2) Use a DescriptorPool which has m->GetDescriptor()->pool() as an
399
 
  //    "underlay".  Read the documentation for DescriptorPool for more
400
 
  //    information about underlays.
401
 
  //
402
 
  // You must also provide a MessageFactory.  This factory will be used to
403
 
  // construct Message objects representing extensions.  The factory's
404
 
  // GetPrototype() MUST return non-NULL for any Descriptor which can be found
405
 
  // through the provided pool.
406
 
  //
407
 
  // If the provided factory might return instances of protocol-compiler-
408
 
  // generated (i.e. compiled-in) types, or if the outer message object m is
409
 
  // a generated type, then the given factory MUST have this property:  If
410
 
  // GetPrototype() is given a Descriptor which resides in
411
 
  // DescriptorPool::generated_pool(), the factory MUST return the same
412
 
  // prototype which MessageFactory::generated_factory() would return.  That
413
 
  // is, given a descriptor for a generated type, the factory must return an
414
 
  // instance of the generated class (NOT DynamicMessage).  However, when
415
 
  // given a descriptor for a type that is NOT in generated_pool, the factory
416
 
  // is free to return any implementation.
417
 
  //
418
 
  // The reason for this requirement is that generated sub-objects may be
419
 
  // accessed via the standard (non-reflection) extension accessor methods,
420
 
  // and these methods will down-cast the object to the generated class type.
421
 
  // If the object is not actually of that type, the results would be undefined.
422
 
  // On the other hand, if an extension is not compiled in, then there is no
423
 
  // way the code could end up accessing it via the standard accessors -- the
424
 
  // only way to access the extension is via reflection.  When using reflection,
425
 
  // DynamicMessage and generated messages are indistinguishable, so it's fine
426
 
  // if these objects are represented using DynamicMessage.
427
 
  //
428
 
  // Using DynamicMessageFactory on which you have called
429
 
  // SetDelegateToGeneratedFactory(true) should be sufficient to satisfy the
430
 
  // above requirement.
431
 
  //
432
 
  // If either pool or factory is NULL, both must be NULL.
433
 
  //
434
 
  // Note that this feature is ignored when parsing "lite" messages as they do
435
 
  // not have descriptors.
436
 
  void SetExtensionRegistry(DescriptorPool* pool, MessageFactory* factory);
437
 
 
438
 
  // Get the DescriptorPool set via SetExtensionRegistry(), or NULL if no pool
439
 
  // has been provided.
440
 
  const DescriptorPool* GetExtensionPool();
441
 
 
442
 
  // Get the MessageFactory set via SetExtensionRegistry(), or NULL if no
443
 
  // factory has been provided.
444
 
  MessageFactory* GetExtensionFactory();
445
 
 
446
 
 private:
447
 
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream);
448
 
 
449
 
  ZeroCopyInputStream* input_;
450
 
  const uint8* buffer_;
451
 
  const uint8* buffer_end_;     // pointer to the end of the buffer.
452
 
  int total_bytes_read_;  // total bytes read from input_, including
453
 
                          // the current buffer
454
 
 
455
 
  // If total_bytes_read_ surpasses INT_MAX, we record the extra bytes here
456
 
  // so that we can BackUp() on destruction.
457
 
  int overflow_bytes_;
458
 
 
459
 
  // LastTagWas() stuff.
460
 
  uint32 last_tag_;         // result of last ReadTag().
461
 
 
462
 
  // This is set true by ReadTag{Fallback/Slow}() if it is called when exactly
463
 
  // at EOF, or by ExpectAtEnd() when it returns true.  This happens when we
464
 
  // reach the end of a message and attempt to read another tag.
465
 
  bool legitimate_message_end_;
466
 
 
467
 
  // See EnableAliasing().
468
 
  bool aliasing_enabled_;
469
 
 
470
 
  // Limits
471
 
  Limit current_limit_;   // if position = -1, no limit is applied
472
 
 
473
 
  // For simplicity, if the current buffer crosses a limit (either a normal
474
 
  // limit created by PushLimit() or the total bytes limit), buffer_size_
475
 
  // only tracks the number of bytes before that limit.  This field
476
 
  // contains the number of bytes after it.  Note that this implies that if
477
 
  // buffer_size_ == 0 and buffer_size_after_limit_ > 0, we know we've
478
 
  // hit a limit.  However, if both are zero, it doesn't necessarily mean
479
 
  // we aren't at a limit -- the buffer may have ended exactly at the limit.
480
 
  int buffer_size_after_limit_;
481
 
 
482
 
  // Maximum number of bytes to read, period.  This is unrelated to
483
 
  // current_limit_.  Set using SetTotalBytesLimit().
484
 
  int total_bytes_limit_;
485
 
  int total_bytes_warning_threshold_;
486
 
 
487
 
  // Current recursion depth, controlled by IncrementRecursionDepth() and
488
 
  // DecrementRecursionDepth().
489
 
  int recursion_depth_;
490
 
  // Recursion depth limit, set by SetRecursionLimit().
491
 
  int recursion_limit_;
492
 
 
493
 
  // See SetExtensionRegistry().
494
 
  const DescriptorPool* extension_pool_;
495
 
  MessageFactory* extension_factory_;
496
 
 
497
 
  // Private member functions.
498
 
 
499
 
  // Advance the buffer by a given number of bytes.
500
 
  void Advance(int amount);
501
 
 
502
 
  // Back up input_ to the current buffer position.
503
 
  void BackUpInputToCurrentPosition();
504
 
 
505
 
  // Recomputes the value of buffer_size_after_limit_.  Must be called after
506
 
  // current_limit_ or total_bytes_limit_ changes.
507
 
  void RecomputeBufferLimits();
508
 
 
509
 
  // Writes an error message saying that we hit total_bytes_limit_.
510
 
  void PrintTotalBytesLimitError();
511
 
 
512
 
  // Called when the buffer runs out to request more data.  Implies an
513
 
  // Advance(BufferSize()).
514
 
  bool Refresh();
515
 
 
516
 
  // When parsing varints, we optimize for the common case of small values, and
517
 
  // then optimize for the case when the varint fits within the current buffer
518
 
  // piece. The Fallback method is used when we can't use the one-byte
519
 
  // optimization. The Slow method is yet another fallback when the buffer is
520
 
  // not large enough. Making the slow path out-of-line speeds up the common
521
 
  // case by 10-15%. The slow path is fairly uncommon: it only triggers when a
522
 
  // message crosses multiple buffers.
523
 
  bool ReadVarint32Fallback(uint32* value);
524
 
  bool ReadVarint64Fallback(uint64* value);
525
 
  bool ReadVarint32Slow(uint32* value);
526
 
  bool ReadVarint64Slow(uint64* value);
527
 
  bool ReadLittleEndian32Fallback(uint32* value);
528
 
  bool ReadLittleEndian64Fallback(uint64* value);
529
 
  // Fallback/slow methods for reading tags. These do not update last_tag_,
530
 
  // but will set legitimate_message_end_ if we are at the end of the input
531
 
  // stream.
532
 
  uint32 ReadTagFallback();
533
 
  uint32 ReadTagSlow();
534
 
  bool ReadStringFallback(string* buffer, int size);
535
 
 
536
 
  // Return the size of the buffer.
537
 
  int BufferSize() const;
538
 
 
539
 
  static const int kDefaultTotalBytesLimit = 64 << 20;  // 64MB
540
 
 
541
 
  static const int kDefaultTotalBytesWarningThreshold = 32 << 20;  // 32MB
542
 
  static const int kDefaultRecursionLimit = 64;
543
 
};
544
 
 
545
 
// Class which encodes and writes binary data which is composed of varint-
546
 
// encoded integers and fixed-width pieces.  Wraps a ZeroCopyOutputStream.
547
 
// Most users will not need to deal with CodedOutputStream.
548
 
//
549
 
// Most methods of CodedOutputStream which return a bool return false if an
550
 
// underlying I/O error occurs.  Once such a failure occurs, the
551
 
// CodedOutputStream is broken and is no longer useful. The Write* methods do
552
 
// not return the stream status, but will invalidate the stream if an error
553
 
// occurs. The client can probe HadError() to determine the status.
554
 
//
555
 
// Note that every method of CodedOutputStream which writes some data has
556
 
// a corresponding static "ToArray" version. These versions write directly
557
 
// to the provided buffer, returning a pointer past the last written byte.
558
 
// They require that the buffer has sufficient capacity for the encoded data.
559
 
// This allows an optimization where we check if an output stream has enough
560
 
// space for an entire message before we start writing and, if there is, we
561
 
// call only the ToArray methods to avoid doing bound checks for each
562
 
// individual value.
563
 
// i.e., in the example above:
564
 
//
565
 
//   CodedOutputStream coded_output = new CodedOutputStream(raw_output);
566
 
//   int magic_number = 1234;
567
 
//   char text[] = "Hello world!";
568
 
//
569
 
//   int coded_size = sizeof(magic_number) +
570
 
//                    CodedOutputStream::Varint32Size(strlen(text)) +
571
 
//                    strlen(text);
572
 
//
573
 
//   uint8* buffer =
574
 
//       coded_output->GetDirectBufferForNBytesAndAdvance(coded_size);
575
 
//   if (buffer != NULL) {
576
 
//     // The output stream has enough space in the buffer: write directly to
577
 
//     // the array.
578
 
//     buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number,
579
 
//                                                            buffer);
580
 
//     buffer = CodedOutputStream::WriteVarint32ToArray(strlen(text), buffer);
581
 
//     buffer = CodedOutputStream::WriteRawToArray(text, strlen(text), buffer);
582
 
//   } else {
583
 
//     // Make bound-checked writes, which will ask the underlying stream for
584
 
//     // more space as needed.
585
 
//     coded_output->WriteLittleEndian32(magic_number);
586
 
//     coded_output->WriteVarint32(strlen(text));
587
 
//     coded_output->WriteRaw(text, strlen(text));
588
 
//   }
589
 
//
590
 
//   delete coded_output;
591
 
class LIBPROTOBUF_EXPORT CodedOutputStream {
592
 
 public:
593
 
  // Create an CodedOutputStream that writes to the given ZeroCopyOutputStream.
594
 
  explicit CodedOutputStream(ZeroCopyOutputStream* output);
595
 
 
596
 
  // Destroy the CodedOutputStream and position the underlying
597
 
  // ZeroCopyOutputStream immediately after the last byte written.
598
 
  ~CodedOutputStream();
599
 
 
600
 
  // Skips a number of bytes, leaving the bytes unmodified in the underlying
601
 
  // buffer.  Returns false if an underlying write error occurs.  This is
602
 
  // mainly useful with GetDirectBufferPointer().
603
 
  bool Skip(int count);
604
 
 
605
 
  // Sets *data to point directly at the unwritten part of the
606
 
  // CodedOutputStream's underlying buffer, and *size to the size of that
607
 
  // buffer, but does not advance the stream's current position.  This will
608
 
  // always either produce a non-empty buffer or return false.  If the caller
609
 
  // writes any data to this buffer, it should then call Skip() to skip over
610
 
  // the consumed bytes.  This may be useful for implementing external fast
611
 
  // serialization routines for types of data not covered by the
612
 
  // CodedOutputStream interface.
613
 
  bool GetDirectBufferPointer(void** data, int* size);
614
 
 
615
 
  // If there are at least "size" bytes available in the current buffer,
616
 
  // returns a pointer directly into the buffer and advances over these bytes.
617
 
  // The caller may then write directly into this buffer (e.g. using the
618
 
  // *ToArray static methods) rather than go through CodedOutputStream.  If
619
 
  // there are not enough bytes available, returns NULL.  The return pointer is
620
 
  // invalidated as soon as any other non-const method of CodedOutputStream
621
 
  // is called.
622
 
  inline uint8* GetDirectBufferForNBytesAndAdvance(int size);
623
 
 
624
 
  // Write raw bytes, copying them from the given buffer.
625
 
  void WriteRaw(const void* buffer, int size);
626
 
  // Like WriteRaw()  but writing directly to the target array.
627
 
  // This is _not_ inlined, as the compiler often optimizes memcpy into inline
628
 
  // copy loops. Since this gets called by every field with string or bytes
629
 
  // type, inlining may lead to a significant amount of code bloat, with only a
630
 
  // minor performance gain.
631
 
  static uint8* WriteRawToArray(const void* buffer, int size, uint8* target);
632
 
 
633
 
  // Equivalent to WriteRaw(str.data(), str.size()).
634
 
  void WriteString(const string& str);
635
 
  // Like WriteString()  but writing directly to the target array.
636
 
  static uint8* WriteStringToArray(const string& str, uint8* target);
637
 
 
638
 
 
639
 
  // Write a 32-bit little-endian integer.
640
 
  void WriteLittleEndian32(uint32 value);
641
 
  // Like WriteLittleEndian32()  but writing directly to the target array.
642
 
  static uint8* WriteLittleEndian32ToArray(uint32 value, uint8* target);
643
 
  // Write a 64-bit little-endian integer.
644
 
  void WriteLittleEndian64(uint64 value);
645
 
  // Like WriteLittleEndian64()  but writing directly to the target array.
646
 
  static uint8* WriteLittleEndian64ToArray(uint64 value, uint8* target);
647
 
 
648
 
  // Write an unsigned integer with Varint encoding.  Writing a 32-bit value
649
 
  // is equivalent to casting it to uint64 and writing it as a 64-bit value,
650
 
  // but may be more efficient.
651
 
  void WriteVarint32(uint32 value);
652
 
  // Like WriteVarint32()  but writing directly to the target array.
653
 
  static uint8* WriteVarint32ToArray(uint32 value, uint8* target);
654
 
  // Write an unsigned integer with Varint encoding.
655
 
  void WriteVarint64(uint64 value);
656
 
  // Like WriteVarint64()  but writing directly to the target array.
657
 
  static uint8* WriteVarint64ToArray(uint64 value, uint8* target);
658
 
 
659
 
  // Equivalent to WriteVarint32() except when the value is negative,
660
 
  // in which case it must be sign-extended to a full 10 bytes.
661
 
  void WriteVarint32SignExtended(int32 value);
662
 
  // Like WriteVarint32SignExtended()  but writing directly to the target array.
663
 
  static uint8* WriteVarint32SignExtendedToArray(int32 value, uint8* target);
664
 
 
665
 
  // This is identical to WriteVarint32(), but optimized for writing tags.
666
 
  // In particular, if the input is a compile-time constant, this method
667
 
  // compiles down to a couple instructions.
668
 
  // Always inline because otherwise the aformentioned optimization can't work,
669
 
  // but GCC by default doesn't want to inline this.
670
 
  void WriteTag(uint32 value);
671
 
  // Like WriteTag()  but writing directly to the target array.
672
 
  static uint8* WriteTagToArray(
673
 
      uint32 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
674
 
 
675
 
  // Returns the number of bytes needed to encode the given value as a varint.
676
 
  static int VarintSize32(uint32 value);
677
 
  // Returns the number of bytes needed to encode the given value as a varint.
678
 
  static int VarintSize64(uint64 value);
679
 
 
680
 
  // If negative, 10 bytes.  Otheriwse, same as VarintSize32().
681
 
  static int VarintSize32SignExtended(int32 value);
682
 
 
683
 
  // Returns the total number of bytes written since this object was created.
684
 
  inline int ByteCount() const;
685
 
 
686
 
  // Returns true if there was an underlying I/O error since this object was
687
 
  // created.
688
 
  bool HadError() const { return had_error_; }
689
 
 
690
 
 private:
691
 
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream);
692
 
 
693
 
  ZeroCopyOutputStream* output_;
694
 
  uint8* buffer_;
695
 
  int buffer_size_;
696
 
  int total_bytes_;  // Sum of sizes of all buffers seen so far.
697
 
  bool had_error_;   // Whether an error occurred during output.
698
 
 
699
 
  // Advance the buffer by a given number of bytes.
700
 
  void Advance(int amount);
701
 
 
702
 
  // Called when the buffer runs out to request more data.  Implies an
703
 
  // Advance(buffer_size_).
704
 
  bool Refresh();
705
 
 
706
 
  static uint8* WriteVarint32FallbackToArray(uint32 value, uint8* target);
707
 
 
708
 
  // Always-inlined versions of WriteVarint* functions so that code can be
709
 
  // reused, while still controlling size. For instance, WriteVarint32ToArray()
710
 
  // should not directly call this: since it is inlined itself, doing so
711
 
  // would greatly increase the size of generated code. Instead, it should call
712
 
  // WriteVarint32FallbackToArray.  Meanwhile, WriteVarint32() is already
713
 
  // out-of-line, so it should just invoke this directly to avoid any extra
714
 
  // function call overhead.
715
 
  static uint8* WriteVarint32FallbackToArrayInline(
716
 
      uint32 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
717
 
  static uint8* WriteVarint64ToArrayInline(
718
 
      uint64 value, uint8* target) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
719
 
 
720
 
  static int VarintSize32Fallback(uint32 value);
721
 
};
722
 
 
723
 
// inline methods ====================================================
724
 
// The vast majority of varints are only one byte.  These inline
725
 
// methods optimize for that case.
726
 
 
727
 
inline bool CodedInputStream::ReadVarint32(uint32* value) {
728
 
  if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) {
729
 
    *value = *buffer_;
730
 
    Advance(1);
731
 
    return true;
732
 
  } else {
733
 
    return ReadVarint32Fallback(value);
734
 
  }
735
 
}
736
 
 
737
 
inline bool CodedInputStream::ReadVarint64(uint64* value) {
738
 
  if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) {
739
 
    *value = *buffer_;
740
 
    Advance(1);
741
 
    return true;
742
 
  } else {
743
 
    return ReadVarint64Fallback(value);
744
 
  }
745
 
}
746
 
 
747
 
// static
748
 
inline const uint8* CodedInputStream::ReadLittleEndian32FromArray(
749
 
    const uint8* buffer,
750
 
    uint32* value) {
751
 
#if defined(PROTOBUF_LITTLE_ENDIAN)
752
 
  memcpy(value, buffer, sizeof(*value));
753
 
  return buffer + sizeof(*value);
754
 
#else
755
 
  *value = (static_cast<uint32>(buffer[0])      ) |
756
 
           (static_cast<uint32>(buffer[1]) <<  8) |
757
 
           (static_cast<uint32>(buffer[2]) << 16) |
758
 
           (static_cast<uint32>(buffer[3]) << 24);
759
 
  return buffer + sizeof(*value);
760
 
#endif
761
 
}
762
 
// static
763
 
inline const uint8* CodedInputStream::ReadLittleEndian64FromArray(
764
 
    const uint8* buffer,
765
 
    uint64* value) {
766
 
#if defined(PROTOBUF_LITTLE_ENDIAN)
767
 
  memcpy(value, buffer, sizeof(*value));
768
 
  return buffer + sizeof(*value);
769
 
#else
770
 
  uint32 part0 = (static_cast<uint32>(buffer[0])      ) |
771
 
                 (static_cast<uint32>(buffer[1]) <<  8) |
772
 
                 (static_cast<uint32>(buffer[2]) << 16) |
773
 
                 (static_cast<uint32>(buffer[3]) << 24);
774
 
  uint32 part1 = (static_cast<uint32>(buffer[4])      ) |
775
 
                 (static_cast<uint32>(buffer[5]) <<  8) |
776
 
                 (static_cast<uint32>(buffer[6]) << 16) |
777
 
                 (static_cast<uint32>(buffer[7]) << 24);
778
 
  *value = static_cast<uint64>(part0) |
779
 
          (static_cast<uint64>(part1) << 32);
780
 
  return buffer + sizeof(*value);
781
 
#endif
782
 
}
783
 
 
784
 
inline bool CodedInputStream::ReadLittleEndian32(uint32* value) {
785
 
#if defined(PROTOBUF_LITTLE_ENDIAN)
786
 
  if (GOOGLE_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
787
 
    memcpy(value, buffer_, sizeof(*value));
788
 
    Advance(sizeof(*value));
789
 
    return true;
790
 
  } else {
791
 
    return ReadLittleEndian32Fallback(value);
792
 
  }
793
 
#else
794
 
  return ReadLittleEndian32Fallback(value);
795
 
#endif
796
 
}
797
 
 
798
 
inline bool CodedInputStream::ReadLittleEndian64(uint64* value) {
799
 
#if defined(PROTOBUF_LITTLE_ENDIAN)
800
 
  if (GOOGLE_PREDICT_TRUE(BufferSize() >= static_cast<int>(sizeof(*value)))) {
801
 
    memcpy(value, buffer_, sizeof(*value));
802
 
    Advance(sizeof(*value));
803
 
    return true;
804
 
  } else {
805
 
    return ReadLittleEndian64Fallback(value);
806
 
  }
807
 
#else
808
 
  return ReadLittleEndian64Fallback(value);
809
 
#endif
810
 
}
811
 
 
812
 
inline uint32 CodedInputStream::ReadTag() {
813
 
  if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && buffer_[0] < 0x80) {
814
 
    last_tag_ = buffer_[0];
815
 
    Advance(1);
816
 
    return last_tag_;
817
 
  } else {
818
 
    last_tag_ = ReadTagFallback();
819
 
    return last_tag_;
820
 
  }
821
 
}
822
 
 
823
 
inline bool CodedInputStream::LastTagWas(uint32 expected) {
824
 
  return last_tag_ == expected;
825
 
}
826
 
 
827
 
inline bool CodedInputStream::ConsumedEntireMessage() {
828
 
  return legitimate_message_end_;
829
 
}
830
 
 
831
 
inline bool CodedInputStream::ExpectTag(uint32 expected) {
832
 
  if (expected < (1 << 7)) {
833
 
    if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_) && buffer_[0] == expected) {
834
 
      Advance(1);
835
 
      return true;
836
 
    } else {
837
 
      return false;
838
 
    }
839
 
  } else if (expected < (1 << 14)) {
840
 
    if (GOOGLE_PREDICT_TRUE(BufferSize() >= 2) &&
841
 
        buffer_[0] == static_cast<uint8>(expected | 0x80) &&
842
 
        buffer_[1] == static_cast<uint8>(expected >> 7)) {
843
 
      Advance(2);
844
 
      return true;
845
 
    } else {
846
 
      return false;
847
 
    }
848
 
  } else {
849
 
    // Don't bother optimizing for larger values.
850
 
    return false;
851
 
  }
852
 
}
853
 
 
854
 
inline const uint8* CodedInputStream::ExpectTagFromArray(
855
 
    const uint8* buffer, uint32 expected) {
856
 
  if (expected < (1 << 7)) {
857
 
    if (buffer[0] == expected) {
858
 
      return buffer + 1;
859
 
    }
860
 
  } else if (expected < (1 << 14)) {
861
 
    if (buffer[0] == static_cast<uint8>(expected | 0x80) &&
862
 
        buffer[1] == static_cast<uint8>(expected >> 7)) {
863
 
      return buffer + 2;
864
 
    }
865
 
  }
866
 
  return NULL;
867
 
}
868
 
 
869
 
inline void CodedInputStream::GetDirectBufferPointerInline(const void** data,
870
 
                                                           int* size) {
871
 
  *data = buffer_;
872
 
  *size = buffer_end_ - buffer_;
873
 
}
874
 
 
875
 
inline bool CodedInputStream::ExpectAtEnd() {
876
 
  // If we are at a limit we know no more bytes can be read.  Otherwise, it's
877
 
  // hard to say without calling Refresh(), and we'd rather not do that.
878
 
 
879
 
  if (buffer_ == buffer_end_ && buffer_size_after_limit_ != 0) {
880
 
    last_tag_ = 0;                   // Pretend we called ReadTag()...
881
 
    legitimate_message_end_ = true;  // ... and it hit EOF.
882
 
    return true;
883
 
  } else {
884
 
    return false;
885
 
  }
886
 
}
887
 
 
888
 
inline uint8* CodedOutputStream::GetDirectBufferForNBytesAndAdvance(int size) {
889
 
  if (buffer_size_ < size) {
890
 
    return NULL;
891
 
  } else {
892
 
    uint8* result = buffer_;
893
 
    Advance(size);
894
 
    return result;
895
 
  }
896
 
}
897
 
 
898
 
inline uint8* CodedOutputStream::WriteVarint32ToArray(uint32 value,
899
 
                                                        uint8* target) {
900
 
  if (value < 0x80) {
901
 
    *target = value;
902
 
    return target + 1;
903
 
  } else {
904
 
    return WriteVarint32FallbackToArray(value, target);
905
 
  }
906
 
}
907
 
 
908
 
inline void CodedOutputStream::WriteVarint32SignExtended(int32 value) {
909
 
  if (value < 0) {
910
 
    WriteVarint64(static_cast<uint64>(value));
911
 
  } else {
912
 
    WriteVarint32(static_cast<uint32>(value));
913
 
  }
914
 
}
915
 
 
916
 
inline uint8* CodedOutputStream::WriteVarint32SignExtendedToArray(
917
 
    int32 value, uint8* target) {
918
 
  if (value < 0) {
919
 
    return WriteVarint64ToArray(static_cast<uint64>(value), target);
920
 
  } else {
921
 
    return WriteVarint32ToArray(static_cast<uint32>(value), target);
922
 
  }
923
 
}
924
 
 
925
 
inline uint8* CodedOutputStream::WriteLittleEndian32ToArray(uint32 value,
926
 
                                                            uint8* target) {
927
 
#if defined(PROTOBUF_LITTLE_ENDIAN)
928
 
  memcpy(target, &value, sizeof(value));
929
 
#else
930
 
  target[0] = static_cast<uint8>(value);
931
 
  target[1] = static_cast<uint8>(value >>  8);
932
 
  target[2] = static_cast<uint8>(value >> 16);
933
 
  target[3] = static_cast<uint8>(value >> 24);
934
 
#endif
935
 
  return target + sizeof(value);
936
 
}
937
 
 
938
 
inline uint8* CodedOutputStream::WriteLittleEndian64ToArray(uint64 value,
939
 
                                                            uint8* target) {
940
 
#if defined(PROTOBUF_LITTLE_ENDIAN)
941
 
  memcpy(target, &value, sizeof(value));
942
 
#else
943
 
  uint32 part0 = static_cast<uint32>(value);
944
 
  uint32 part1 = static_cast<uint32>(value >> 32);
945
 
 
946
 
  target[0] = static_cast<uint8>(part0);
947
 
  target[1] = static_cast<uint8>(part0 >>  8);
948
 
  target[2] = static_cast<uint8>(part0 >> 16);
949
 
  target[3] = static_cast<uint8>(part0 >> 24);
950
 
  target[4] = static_cast<uint8>(part1);
951
 
  target[5] = static_cast<uint8>(part1 >>  8);
952
 
  target[6] = static_cast<uint8>(part1 >> 16);
953
 
  target[7] = static_cast<uint8>(part1 >> 24);
954
 
#endif
955
 
  return target + sizeof(value);
956
 
}
957
 
 
958
 
inline void CodedOutputStream::WriteTag(uint32 value) {
959
 
  WriteVarint32(value);
960
 
}
961
 
 
962
 
inline uint8* CodedOutputStream::WriteTagToArray(
963
 
    uint32 value, uint8* target) {
964
 
  if (value < (1 << 7)) {
965
 
    target[0] = value;
966
 
    return target + 1;
967
 
  } else if (value < (1 << 14)) {
968
 
    target[0] = static_cast<uint8>(value | 0x80);
969
 
    target[1] = static_cast<uint8>(value >> 7);
970
 
    return target + 2;
971
 
  } else {
972
 
    return WriteVarint32FallbackToArray(value, target);
973
 
  }
974
 
}
975
 
 
976
 
inline int CodedOutputStream::VarintSize32(uint32 value) {
977
 
  if (value < (1 << 7)) {
978
 
    return 1;
979
 
  } else  {
980
 
    return VarintSize32Fallback(value);
981
 
  }
982
 
}
983
 
 
984
 
inline int CodedOutputStream::VarintSize32SignExtended(int32 value) {
985
 
  if (value < 0) {
986
 
    return 10;     // TODO(kenton):  Make this a symbolic constant.
987
 
  } else {
988
 
    return VarintSize32(static_cast<uint32>(value));
989
 
  }
990
 
}
991
 
 
992
 
inline void CodedOutputStream::WriteString(const string& str) {
993
 
  WriteRaw(str.data(), str.size());
994
 
}
995
 
 
996
 
inline uint8* CodedOutputStream::WriteStringToArray(
997
 
    const string& str, uint8* target) {
998
 
  return WriteRawToArray(str.data(), str.size(), target);
999
 
}
1000
 
 
1001
 
inline int CodedOutputStream::ByteCount() const {
1002
 
  return total_bytes_ - buffer_size_;
1003
 
}
1004
 
 
1005
 
inline void CodedInputStream::Advance(int amount) {
1006
 
  buffer_ += amount;
1007
 
}
1008
 
 
1009
 
inline void CodedOutputStream::Advance(int amount) {
1010
 
  buffer_ += amount;
1011
 
  buffer_size_ -= amount;
1012
 
}
1013
 
 
1014
 
inline void CodedInputStream::SetRecursionLimit(int limit) {
1015
 
  recursion_limit_ = limit;
1016
 
}
1017
 
 
1018
 
inline bool CodedInputStream::IncrementRecursionDepth() {
1019
 
  ++recursion_depth_;
1020
 
  return recursion_depth_ <= recursion_limit_;
1021
 
}
1022
 
 
1023
 
inline void CodedInputStream::DecrementRecursionDepth() {
1024
 
  if (recursion_depth_ > 0) --recursion_depth_;
1025
 
}
1026
 
 
1027
 
inline void CodedInputStream::SetExtensionRegistry(DescriptorPool* pool,
1028
 
                                                   MessageFactory* factory) {
1029
 
  extension_pool_ = pool;
1030
 
  extension_factory_ = factory;
1031
 
}
1032
 
 
1033
 
inline const DescriptorPool* CodedInputStream::GetExtensionPool() {
1034
 
  return extension_pool_;
1035
 
}
1036
 
 
1037
 
inline MessageFactory* CodedInputStream::GetExtensionFactory() {
1038
 
  return extension_factory_;
1039
 
}
1040
 
 
1041
 
inline int CodedInputStream::BufferSize() const {
1042
 
  return buffer_end_ - buffer_;
1043
 
}
1044
 
 
1045
 
inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input)
1046
 
  : input_(input),
1047
 
    buffer_(NULL),
1048
 
    buffer_end_(NULL),
1049
 
    total_bytes_read_(0),
1050
 
    overflow_bytes_(0),
1051
 
    last_tag_(0),
1052
 
    legitimate_message_end_(false),
1053
 
    aliasing_enabled_(false),
1054
 
    current_limit_(kint32max),
1055
 
    buffer_size_after_limit_(0),
1056
 
    total_bytes_limit_(kDefaultTotalBytesLimit),
1057
 
    total_bytes_warning_threshold_(kDefaultTotalBytesWarningThreshold),
1058
 
    recursion_depth_(0),
1059
 
    recursion_limit_(kDefaultRecursionLimit),
1060
 
    extension_pool_(NULL),
1061
 
    extension_factory_(NULL) {
1062
 
  // Eagerly Refresh() so buffer space is immediately available.
1063
 
  Refresh();
1064
 
}
1065
 
 
1066
 
inline CodedInputStream::CodedInputStream(const uint8* buffer, int size)
1067
 
  : input_(NULL),
1068
 
    buffer_(buffer),
1069
 
    buffer_end_(buffer + size),
1070
 
    total_bytes_read_(size),
1071
 
    overflow_bytes_(0),
1072
 
    last_tag_(0),
1073
 
    legitimate_message_end_(false),
1074
 
    aliasing_enabled_(false),
1075
 
    current_limit_(size),
1076
 
    buffer_size_after_limit_(0),
1077
 
    total_bytes_limit_(kDefaultTotalBytesLimit),
1078
 
    total_bytes_warning_threshold_(kDefaultTotalBytesWarningThreshold),
1079
 
    recursion_depth_(0),
1080
 
    recursion_limit_(kDefaultRecursionLimit),
1081
 
    extension_pool_(NULL),
1082
 
    extension_factory_(NULL) {
1083
 
  // Note that setting current_limit_ == size is important to prevent some
1084
 
  // code paths from trying to access input_ and segfaulting.
1085
 
}
1086
 
 
1087
 
inline CodedInputStream::~CodedInputStream() {
1088
 
  if (input_ != NULL) {
1089
 
    BackUpInputToCurrentPosition();
1090
 
  }
1091
 
}
1092
 
 
1093
 
}  // namespace io
1094
 
}  // namespace protobuf
1095
 
 
1096
 
}  // namespace google
1097
 
 
1098
 
#if defined(_MSC_VER) && _MSC_VER >= 1300
1099
 
  #pragma runtime_checks("c", restore)
1100
 
#endif  // _MSC_VER
1101
 
 
1102
 
#endif  // GOOGLE_PROTOBUF_IO_CODED_STREAM_H__