other-licenses/snappy/src/snappy-sinksource.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 // Copyright 2011 Google Inc. All Rights Reserved.
michael@0 2 //
michael@0 3 // Redistribution and use in source and binary forms, with or without
michael@0 4 // modification, are permitted provided that the following conditions are
michael@0 5 // met:
michael@0 6 //
michael@0 7 // * Redistributions of source code must retain the above copyright
michael@0 8 // notice, this list of conditions and the following disclaimer.
michael@0 9 // * Redistributions in binary form must reproduce the above
michael@0 10 // copyright notice, this list of conditions and the following disclaimer
michael@0 11 // in the documentation and/or other materials provided with the
michael@0 12 // distribution.
michael@0 13 // * Neither the name of Google Inc. nor the names of its
michael@0 14 // contributors may be used to endorse or promote products derived from
michael@0 15 // this software without specific prior written permission.
michael@0 16 //
michael@0 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 28
michael@0 29 #ifndef UTIL_SNAPPY_SNAPPY_SINKSOURCE_H_
michael@0 30 #define UTIL_SNAPPY_SNAPPY_SINKSOURCE_H_
michael@0 31
michael@0 32 #include <stddef.h>
michael@0 33
michael@0 34
michael@0 35 namespace snappy {
michael@0 36
michael@0 37 // A Sink is an interface that consumes a sequence of bytes.
michael@0 38 class Sink {
michael@0 39 public:
michael@0 40 Sink() { }
michael@0 41 virtual ~Sink();
michael@0 42
michael@0 43 // Append "bytes[0,n-1]" to this.
michael@0 44 virtual void Append(const char* bytes, size_t n) = 0;
michael@0 45
michael@0 46 // Returns a writable buffer of the specified length for appending.
michael@0 47 // May return a pointer to the caller-owned scratch buffer which
michael@0 48 // must have at least the indicated length. The returned buffer is
michael@0 49 // only valid until the next operation on this Sink.
michael@0 50 //
michael@0 51 // After writing at most "length" bytes, call Append() with the
michael@0 52 // pointer returned from this function and the number of bytes
michael@0 53 // written. Many Append() implementations will avoid copying
michael@0 54 // bytes if this function returned an internal buffer.
michael@0 55 //
michael@0 56 // If a non-scratch buffer is returned, the caller may only pass a
michael@0 57 // prefix of it to Append(). That is, it is not correct to pass an
michael@0 58 // interior pointer of the returned array to Append().
michael@0 59 //
michael@0 60 // The default implementation always returns the scratch buffer.
michael@0 61 virtual char* GetAppendBuffer(size_t length, char* scratch);
michael@0 62
michael@0 63 private:
michael@0 64 // No copying
michael@0 65 Sink(const Sink&);
michael@0 66 void operator=(const Sink&);
michael@0 67 };
michael@0 68
michael@0 69 // A Source is an interface that yields a sequence of bytes
michael@0 70 class Source {
michael@0 71 public:
michael@0 72 Source() { }
michael@0 73 virtual ~Source();
michael@0 74
michael@0 75 // Return the number of bytes left to read from the source
michael@0 76 virtual size_t Available() const = 0;
michael@0 77
michael@0 78 // Peek at the next flat region of the source. Does not reposition
michael@0 79 // the source. The returned region is empty iff Available()==0.
michael@0 80 //
michael@0 81 // Returns a pointer to the beginning of the region and store its
michael@0 82 // length in *len.
michael@0 83 //
michael@0 84 // The returned region is valid until the next call to Skip() or
michael@0 85 // until this object is destroyed, whichever occurs first.
michael@0 86 //
michael@0 87 // The returned region may be larger than Available() (for example
michael@0 88 // if this ByteSource is a view on a substring of a larger source).
michael@0 89 // The caller is responsible for ensuring that it only reads the
michael@0 90 // Available() bytes.
michael@0 91 virtual const char* Peek(size_t* len) = 0;
michael@0 92
michael@0 93 // Skip the next n bytes. Invalidates any buffer returned by
michael@0 94 // a previous call to Peek().
michael@0 95 // REQUIRES: Available() >= n
michael@0 96 virtual void Skip(size_t n) = 0;
michael@0 97
michael@0 98 private:
michael@0 99 // No copying
michael@0 100 Source(const Source&);
michael@0 101 void operator=(const Source&);
michael@0 102 };
michael@0 103
michael@0 104 // A Source implementation that yields the contents of a flat array
michael@0 105 class ByteArraySource : public Source {
michael@0 106 public:
michael@0 107 ByteArraySource(const char* p, size_t n) : ptr_(p), left_(n) { }
michael@0 108 virtual ~ByteArraySource();
michael@0 109 virtual size_t Available() const;
michael@0 110 virtual const char* Peek(size_t* len);
michael@0 111 virtual void Skip(size_t n);
michael@0 112 private:
michael@0 113 const char* ptr_;
michael@0 114 size_t left_;
michael@0 115 };
michael@0 116
michael@0 117 // A Sink implementation that writes to a flat array without any bound checks.
michael@0 118 class UncheckedByteArraySink : public Sink {
michael@0 119 public:
michael@0 120 explicit UncheckedByteArraySink(char* dest) : dest_(dest) { }
michael@0 121 virtual ~UncheckedByteArraySink();
michael@0 122 virtual void Append(const char* data, size_t n);
michael@0 123 virtual char* GetAppendBuffer(size_t len, char* scratch);
michael@0 124
michael@0 125 // Return the current output pointer so that a caller can see how
michael@0 126 // many bytes were produced.
michael@0 127 // Note: this is not a Sink method.
michael@0 128 char* CurrentDestination() const { return dest_; }
michael@0 129 private:
michael@0 130 char* dest_;
michael@0 131 };
michael@0 132
michael@0 133
michael@0 134 }
michael@0 135
michael@0 136 #endif // UTIL_SNAPPY_SNAPPY_SINKSOURCE_H_

mercurial