1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/snappy/src/snappy-sinksource.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,136 @@ 1.4 +// Copyright 2011 Google Inc. All Rights Reserved. 1.5 +// 1.6 +// Redistribution and use in source and binary forms, with or without 1.7 +// modification, are permitted provided that the following conditions are 1.8 +// met: 1.9 +// 1.10 +// * Redistributions of source code must retain the above copyright 1.11 +// notice, this list of conditions and the following disclaimer. 1.12 +// * Redistributions in binary form must reproduce the above 1.13 +// copyright notice, this list of conditions and the following disclaimer 1.14 +// in the documentation and/or other materials provided with the 1.15 +// distribution. 1.16 +// * Neither the name of Google Inc. nor the names of its 1.17 +// contributors may be used to endorse or promote products derived from 1.18 +// this software without specific prior written permission. 1.19 +// 1.20 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.21 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.22 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.23 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.24 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.25 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.26 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.27 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.28 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.29 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.30 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.31 + 1.32 +#ifndef UTIL_SNAPPY_SNAPPY_SINKSOURCE_H_ 1.33 +#define UTIL_SNAPPY_SNAPPY_SINKSOURCE_H_ 1.34 + 1.35 +#include <stddef.h> 1.36 + 1.37 + 1.38 +namespace snappy { 1.39 + 1.40 +// A Sink is an interface that consumes a sequence of bytes. 1.41 +class Sink { 1.42 + public: 1.43 + Sink() { } 1.44 + virtual ~Sink(); 1.45 + 1.46 + // Append "bytes[0,n-1]" to this. 1.47 + virtual void Append(const char* bytes, size_t n) = 0; 1.48 + 1.49 + // Returns a writable buffer of the specified length for appending. 1.50 + // May return a pointer to the caller-owned scratch buffer which 1.51 + // must have at least the indicated length. The returned buffer is 1.52 + // only valid until the next operation on this Sink. 1.53 + // 1.54 + // After writing at most "length" bytes, call Append() with the 1.55 + // pointer returned from this function and the number of bytes 1.56 + // written. Many Append() implementations will avoid copying 1.57 + // bytes if this function returned an internal buffer. 1.58 + // 1.59 + // If a non-scratch buffer is returned, the caller may only pass a 1.60 + // prefix of it to Append(). That is, it is not correct to pass an 1.61 + // interior pointer of the returned array to Append(). 1.62 + // 1.63 + // The default implementation always returns the scratch buffer. 1.64 + virtual char* GetAppendBuffer(size_t length, char* scratch); 1.65 + 1.66 + private: 1.67 + // No copying 1.68 + Sink(const Sink&); 1.69 + void operator=(const Sink&); 1.70 +}; 1.71 + 1.72 +// A Source is an interface that yields a sequence of bytes 1.73 +class Source { 1.74 + public: 1.75 + Source() { } 1.76 + virtual ~Source(); 1.77 + 1.78 + // Return the number of bytes left to read from the source 1.79 + virtual size_t Available() const = 0; 1.80 + 1.81 + // Peek at the next flat region of the source. Does not reposition 1.82 + // the source. The returned region is empty iff Available()==0. 1.83 + // 1.84 + // Returns a pointer to the beginning of the region and store its 1.85 + // length in *len. 1.86 + // 1.87 + // The returned region is valid until the next call to Skip() or 1.88 + // until this object is destroyed, whichever occurs first. 1.89 + // 1.90 + // The returned region may be larger than Available() (for example 1.91 + // if this ByteSource is a view on a substring of a larger source). 1.92 + // The caller is responsible for ensuring that it only reads the 1.93 + // Available() bytes. 1.94 + virtual const char* Peek(size_t* len) = 0; 1.95 + 1.96 + // Skip the next n bytes. Invalidates any buffer returned by 1.97 + // a previous call to Peek(). 1.98 + // REQUIRES: Available() >= n 1.99 + virtual void Skip(size_t n) = 0; 1.100 + 1.101 + private: 1.102 + // No copying 1.103 + Source(const Source&); 1.104 + void operator=(const Source&); 1.105 +}; 1.106 + 1.107 +// A Source implementation that yields the contents of a flat array 1.108 +class ByteArraySource : public Source { 1.109 + public: 1.110 + ByteArraySource(const char* p, size_t n) : ptr_(p), left_(n) { } 1.111 + virtual ~ByteArraySource(); 1.112 + virtual size_t Available() const; 1.113 + virtual const char* Peek(size_t* len); 1.114 + virtual void Skip(size_t n); 1.115 + private: 1.116 + const char* ptr_; 1.117 + size_t left_; 1.118 +}; 1.119 + 1.120 +// A Sink implementation that writes to a flat array without any bound checks. 1.121 +class UncheckedByteArraySink : public Sink { 1.122 + public: 1.123 + explicit UncheckedByteArraySink(char* dest) : dest_(dest) { } 1.124 + virtual ~UncheckedByteArraySink(); 1.125 + virtual void Append(const char* data, size_t n); 1.126 + virtual char* GetAppendBuffer(size_t len, char* scratch); 1.127 + 1.128 + // Return the current output pointer so that a caller can see how 1.129 + // many bytes were produced. 1.130 + // Note: this is not a Sink method. 1.131 + char* CurrentDestination() const { return dest_; } 1.132 + private: 1.133 + char* dest_; 1.134 +}; 1.135 + 1.136 + 1.137 +} 1.138 + 1.139 +#endif // UTIL_SNAPPY_SNAPPY_SINKSOURCE_H_