OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // http://code.google.com/p/protobuf/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
(...skipping 15 matching lines...) Expand all Loading... |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 // Author: jasonh@google.com (Jason Hsueh) | 31 // Author: jasonh@google.com (Jason Hsueh) |
32 // | 32 // |
33 // Implements methods of coded_stream.h that need to be inlined for performance | 33 // Implements methods of coded_stream.h that need to be inlined for performance |
34 // reasons, but should not be defined in a public header. | 34 // reasons, but should not be defined in a public header. |
35 | 35 |
36 #ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__ | 36 #ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__ |
37 #define GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__ | 37 #define GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__ |
38 | 38 |
39 #include <google/protobuf/stubs/common.h> | |
40 #include <google/protobuf/io/coded_stream.h> | 39 #include <google/protobuf/io/coded_stream.h> |
41 #include <google/protobuf/io/zero_copy_stream_impl_lite.h> | |
42 #include <string> | 40 #include <string> |
43 #include <google/protobuf/stubs/stl_util.h> | 41 #include <google/protobuf/stubs/stl_util.h> |
44 | 42 |
45 namespace google { | 43 namespace google { |
46 namespace protobuf { | 44 namespace protobuf { |
47 namespace io { | 45 namespace io { |
48 | 46 |
49 inline bool CodedInputStream::InternalReadStringInline(string* buffer, | 47 inline bool CodedInputStream::InternalReadStringInline(string* buffer, |
50 int size) { | 48 int size) { |
51 if (size < 0) return false; // security: size is often user-supplied | 49 if (size < 0) return false; // security: size is often user-supplied |
52 | 50 |
53 if (BufferSize() >= size) { | 51 if (BufferSize() >= size) { |
54 STLStringResizeUninitialized(buffer, size); | 52 STLStringResizeUninitialized(buffer, size); |
55 std::pair<char*, bool> z = as_string_data(buffer); | 53 // When buffer is empty, string_as_array(buffer) will return NULL but memcpy |
56 if (z.second) { | 54 // requires non-NULL pointers even when size is 0. Hench this check. |
57 // Oddly enough, memcpy() requires its first two args to be non-NULL even | 55 if (size > 0) { |
58 // if we copy 0 bytes. So, we have ensured that z.first is non-NULL here. | 56 memcpy(string_as_array(buffer), buffer_, size); |
59 GOOGLE_DCHECK(z.first != NULL); | |
60 memcpy(z.first, buffer_, size); | |
61 Advance(size); | 57 Advance(size); |
62 } | 58 } |
63 return true; | 59 return true; |
64 } | 60 } |
65 | 61 |
66 return ReadStringFallback(buffer, size); | 62 return ReadStringFallback(buffer, size); |
67 } | 63 } |
68 | 64 |
69 inline bool CodedInputStream::InternalReadRawInline(void* buffer, int size) { | |
70 int current_buffer_size; | |
71 while ((current_buffer_size = BufferSize()) < size) { | |
72 // Reading past end of buffer. Copy what we have, then refresh. | |
73 memcpy(buffer, buffer_, current_buffer_size); | |
74 buffer = reinterpret_cast<uint8*>(buffer) + current_buffer_size; | |
75 size -= current_buffer_size; | |
76 Advance(current_buffer_size); | |
77 if (!Refresh()) return false; | |
78 } | |
79 | |
80 memcpy(buffer, buffer_, size); | |
81 Advance(size); | |
82 | |
83 return true; | |
84 } | |
85 | |
86 } // namespace io | 65 } // namespace io |
87 } // namespace protobuf | 66 } // namespace protobuf |
88 } // namespace google | 67 } // namespace google |
89 #endif // GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__ | 68 #endif // GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__ |
OLD | NEW |