James Kuszmaul | 48dd4c8 | 2021-10-27 20:04:08 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are |
| 5 | // met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above |
| 10 | // copyright notice, this list of conditions and the following disclaimer |
| 11 | // in the documentation and/or other materials provided with the |
| 12 | // distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its |
| 14 | // contributors may be used to endorse or promote products derived from |
| 15 | // this software without specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #include <stddef.h> |
| 30 | #include <cstring> |
| 31 | |
| 32 | #include "snappy-sinksource.h" |
| 33 | |
| 34 | namespace snappy { |
| 35 | |
| 36 | Source::~Source() = default; |
| 37 | |
| 38 | Sink::~Sink() = default; |
| 39 | |
| 40 | char* Sink::GetAppendBuffer(size_t length, char* scratch) { |
| 41 | // TODO: Switch to [[maybe_unused]] when we can assume C++17. |
| 42 | (void)length; |
| 43 | |
| 44 | return scratch; |
| 45 | } |
| 46 | |
| 47 | char* Sink::GetAppendBufferVariable( |
| 48 | size_t min_size, size_t desired_size_hint, char* scratch, |
| 49 | size_t scratch_size, size_t* allocated_size) { |
| 50 | // TODO: Switch to [[maybe_unused]] when we can assume C++17. |
| 51 | (void)min_size; |
| 52 | (void)desired_size_hint; |
| 53 | |
| 54 | *allocated_size = scratch_size; |
| 55 | return scratch; |
| 56 | } |
| 57 | |
| 58 | void Sink::AppendAndTakeOwnership( |
| 59 | char* bytes, size_t n, |
| 60 | void (*deleter)(void*, const char*, size_t), |
| 61 | void *deleter_arg) { |
| 62 | Append(bytes, n); |
| 63 | (*deleter)(deleter_arg, bytes, n); |
| 64 | } |
| 65 | |
| 66 | ByteArraySource::~ByteArraySource() = default; |
| 67 | |
| 68 | size_t ByteArraySource::Available() const { return left_; } |
| 69 | |
| 70 | const char* ByteArraySource::Peek(size_t* len) { |
| 71 | *len = left_; |
| 72 | return ptr_; |
| 73 | } |
| 74 | |
| 75 | void ByteArraySource::Skip(size_t n) { |
| 76 | left_ -= n; |
| 77 | ptr_ += n; |
| 78 | } |
| 79 | |
| 80 | UncheckedByteArraySink::~UncheckedByteArraySink() { } |
| 81 | |
| 82 | void UncheckedByteArraySink::Append(const char* data, size_t n) { |
| 83 | // Do no copying if the caller filled in the result of GetAppendBuffer() |
| 84 | if (data != dest_) { |
| 85 | std::memcpy(dest_, data, n); |
| 86 | } |
| 87 | dest_ += n; |
| 88 | } |
| 89 | |
| 90 | char* UncheckedByteArraySink::GetAppendBuffer(size_t len, char* scratch) { |
| 91 | // TODO: Switch to [[maybe_unused]] when we can assume C++17. |
| 92 | (void)len; |
| 93 | (void)scratch; |
| 94 | |
| 95 | return dest_; |
| 96 | } |
| 97 | |
| 98 | void UncheckedByteArraySink::AppendAndTakeOwnership( |
| 99 | char* bytes, size_t n, |
| 100 | void (*deleter)(void*, const char*, size_t), |
| 101 | void *deleter_arg) { |
| 102 | if (bytes != dest_) { |
| 103 | std::memcpy(dest_, bytes, n); |
| 104 | (*deleter)(deleter_arg, bytes, n); |
| 105 | } |
| 106 | dest_ += n; |
| 107 | } |
| 108 | |
| 109 | char* UncheckedByteArraySink::GetAppendBufferVariable( |
| 110 | size_t min_size, size_t desired_size_hint, char* scratch, |
| 111 | size_t scratch_size, size_t* allocated_size) { |
| 112 | // TODO: Switch to [[maybe_unused]] when we can assume C++17. |
| 113 | (void)min_size; |
| 114 | (void)scratch; |
| 115 | (void)scratch_size; |
| 116 | |
| 117 | *allocated_size = desired_size_hint; |
| 118 | return dest_; |
| 119 | } |
| 120 | |
| 121 | } // namespace snappy |