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 | |
31 | #include <google/protobuf/stubs/bytestream.h> |
32 | |
33 | #include <string.h> |
34 | #include <algorithm> |
35 | |
36 | #include <google/protobuf/stubs/logging.h> |
37 | |
38 | namespace google { |
39 | namespace protobuf { |
40 | namespace strings { |
41 | |
42 | void ByteSource::CopyTo(ByteSink* sink, size_t n) { |
43 | while (n > 0) { |
44 | StringPiece fragment = Peek(); |
45 | if (fragment.empty()) { |
46 | GOOGLE_LOG(DFATAL) << "ByteSource::CopyTo() overran input." ; |
47 | break; |
48 | } |
49 | std::size_t fragment_size = std::min<std::size_t>(n, fragment.size()); |
50 | sink->Append(bytes: fragment.data(), n: fragment_size); |
51 | Skip(n: fragment_size); |
52 | n -= fragment_size; |
53 | } |
54 | } |
55 | |
56 | void ByteSink::Flush() {} |
57 | |
58 | void UncheckedArrayByteSink::Append(const char* data, size_t n) { |
59 | if (data != dest_) { |
60 | // Catch cases where the pointer returned by GetAppendBuffer() was modified. |
61 | GOOGLE_DCHECK(!(dest_ <= data && data < (dest_ + n))) |
62 | << "Append() data[] overlaps with dest_[]" ; |
63 | memcpy(dest: dest_, src: data, n: n); |
64 | } |
65 | dest_ += n; |
66 | } |
67 | |
68 | CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, size_t capacity) |
69 | : outbuf_(outbuf), capacity_(capacity), size_(0), overflowed_(false) { |
70 | } |
71 | |
72 | void CheckedArrayByteSink::Append(const char* bytes, size_t n) { |
73 | size_t available = capacity_ - size_; |
74 | if (n > available) { |
75 | n = available; |
76 | overflowed_ = true; |
77 | } |
78 | if (n > 0 && bytes != (outbuf_ + size_)) { |
79 | // Catch cases where the pointer returned by GetAppendBuffer() was modified. |
80 | GOOGLE_DCHECK(!(outbuf_ <= bytes && bytes < (outbuf_ + capacity_))) |
81 | << "Append() bytes[] overlaps with outbuf_[]" ; |
82 | memcpy(dest: outbuf_ + size_, src: bytes, n: n); |
83 | } |
84 | size_ += n; |
85 | } |
86 | |
87 | GrowingArrayByteSink::GrowingArrayByteSink(size_t estimated_size) |
88 | : capacity_(estimated_size), |
89 | buf_(new char[estimated_size]), |
90 | size_(0) { |
91 | } |
92 | |
93 | GrowingArrayByteSink::~GrowingArrayByteSink() { |
94 | delete[] buf_; // Just in case the user didn't call GetBuffer. |
95 | } |
96 | |
97 | void GrowingArrayByteSink::Append(const char* bytes, size_t n) { |
98 | size_t available = capacity_ - size_; |
99 | if (bytes != (buf_ + size_)) { |
100 | // Catch cases where the pointer returned by GetAppendBuffer() was modified. |
101 | // We need to test for this before calling Expand() which may reallocate. |
102 | GOOGLE_DCHECK(!(buf_ <= bytes && bytes < (buf_ + capacity_))) |
103 | << "Append() bytes[] overlaps with buf_[]" ; |
104 | } |
105 | if (n > available) { |
106 | Expand(amount: n - available); |
107 | } |
108 | if (n > 0 && bytes != (buf_ + size_)) { |
109 | memcpy(dest: buf_ + size_, src: bytes, n: n); |
110 | } |
111 | size_ += n; |
112 | } |
113 | |
114 | char* GrowingArrayByteSink::GetBuffer(size_t* nbytes) { |
115 | ShrinkToFit(); |
116 | char* b = buf_; |
117 | *nbytes = size_; |
118 | buf_ = nullptr; |
119 | size_ = capacity_ = 0; |
120 | return b; |
121 | } |
122 | |
123 | void GrowingArrayByteSink::Expand(size_t amount) { // Expand by at least 50%. |
124 | size_t new_capacity = std::max(capacity_ + amount, (3 * capacity_) / 2); |
125 | char* bigger = new char[new_capacity]; |
126 | memcpy(dest: bigger, src: buf_, n: size_); |
127 | delete[] buf_; |
128 | buf_ = bigger; |
129 | capacity_ = new_capacity; |
130 | } |
131 | |
132 | void GrowingArrayByteSink::ShrinkToFit() { |
133 | // Shrink only if the buffer is large and size_ is less than 3/4 |
134 | // of capacity_. |
135 | if (capacity_ > 256 && size_ < (3 * capacity_) / 4) { |
136 | char* just_enough = new char[size_]; |
137 | memcpy(dest: just_enough, src: buf_, n: size_); |
138 | delete[] buf_; |
139 | buf_ = just_enough; |
140 | capacity_ = size_; |
141 | } |
142 | } |
143 | |
144 | void StringByteSink::Append(const char* data, size_t n) { |
145 | dest_->append(s: data, n: n); |
146 | } |
147 | |
148 | size_t ArrayByteSource::Available() const { |
149 | return input_.size(); |
150 | } |
151 | |
152 | StringPiece ArrayByteSource::Peek() { |
153 | return input_; |
154 | } |
155 | |
156 | void ArrayByteSource::Skip(size_t n) { |
157 | GOOGLE_DCHECK_LE(n, input_.size()); |
158 | input_.remove_prefix(n); |
159 | } |
160 | |
161 | LimitByteSource::LimitByteSource(ByteSource *source, size_t limit) |
162 | : source_(source), |
163 | limit_(limit) { |
164 | } |
165 | |
166 | size_t LimitByteSource::Available() const { |
167 | size_t available = source_->Available(); |
168 | if (available > limit_) { |
169 | available = limit_; |
170 | } |
171 | |
172 | return available; |
173 | } |
174 | |
175 | StringPiece LimitByteSource::Peek() { |
176 | StringPiece piece = source_->Peek(); |
177 | return StringPiece(piece.data(), std::min(piece.size(), limit_)); |
178 | } |
179 | |
180 | void LimitByteSource::Skip(size_t n) { |
181 | GOOGLE_DCHECK_LE(n, limit_); |
182 | source_->Skip(n); |
183 | limit_ -= n; |
184 | } |
185 | |
186 | void LimitByteSource::CopyTo(ByteSink *sink, size_t n) { |
187 | GOOGLE_DCHECK_LE(n, limit_); |
188 | source_->CopyTo(sink, n); |
189 | limit_ -= n; |
190 | } |
191 | |
192 | } // namespace strings |
193 | } // namespace protobuf |
194 | } // namespace google |
195 | |