Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:55:47

0001 // Copyright 2011 Google Inc. All Rights Reserved.
0002 //
0003 // Redistribution and use in source and binary forms, with or without
0004 // modification, are permitted provided that the following conditions are
0005 // met:
0006 //
0007 //     * Redistributions of source code must retain the above copyright
0008 // notice, this list of conditions and the following disclaimer.
0009 //     * Redistributions in binary form must reproduce the above
0010 // copyright notice, this list of conditions and the following disclaimer
0011 // in the documentation and/or other materials provided with the
0012 // distribution.
0013 //     * Neither the name of Google Inc. nor the names of its
0014 // contributors may be used to endorse or promote products derived from
0015 // this software without specific prior written permission.
0016 //
0017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0018 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0019 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0020 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0021 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0022 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0023 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0024 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0025 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0026 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0027 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0028 
0029 #ifndef THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_
0030 #define THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_
0031 
0032 #include <stddef.h>
0033 
0034 namespace snappy {
0035 
0036 // A Sink is an interface that consumes a sequence of bytes.
0037 class Sink {
0038  public:
0039   Sink() { }
0040   virtual ~Sink();
0041 
0042   // Append "bytes[0,n-1]" to this.
0043   virtual void Append(const char* bytes, size_t n) = 0;
0044 
0045   // Returns a writable buffer of the specified length for appending.
0046   // May return a pointer to the caller-owned scratch buffer which
0047   // must have at least the indicated length.  The returned buffer is
0048   // only valid until the next operation on this Sink.
0049   //
0050   // After writing at most "length" bytes, call Append() with the
0051   // pointer returned from this function and the number of bytes
0052   // written.  Many Append() implementations will avoid copying
0053   // bytes if this function returned an internal buffer.
0054   //
0055   // If a non-scratch buffer is returned, the caller may only pass a
0056   // prefix of it to Append().  That is, it is not correct to pass an
0057   // interior pointer of the returned array to Append().
0058   //
0059   // The default implementation always returns the scratch buffer.
0060   virtual char* GetAppendBuffer(size_t length, char* scratch);
0061 
0062   // For higher performance, Sink implementations can provide custom
0063   // AppendAndTakeOwnership() and GetAppendBufferVariable() methods.
0064   // These methods can reduce the number of copies done during
0065   // compression/decompression.
0066 
0067   // Append "bytes[0,n-1] to the sink. Takes ownership of "bytes"
0068   // and calls the deleter function as (*deleter)(deleter_arg, bytes, n)
0069   // to free the buffer. deleter function must be non NULL.
0070   //
0071   // The default implementation just calls Append and frees "bytes".
0072   // Other implementations may avoid a copy while appending the buffer.
0073   virtual void AppendAndTakeOwnership(
0074       char* bytes, size_t n, void (*deleter)(void*, const char*, size_t),
0075       void *deleter_arg);
0076 
0077   // Returns a writable buffer for appending and writes the buffer's capacity to
0078   // *allocated_size. Guarantees *allocated_size >= min_size.
0079   // May return a pointer to the caller-owned scratch buffer which must have
0080   // scratch_size >= min_size.
0081   //
0082   // The returned buffer is only valid until the next operation
0083   // on this ByteSink.
0084   //
0085   // After writing at most *allocated_size bytes, call Append() with the
0086   // pointer returned from this function and the number of bytes written.
0087   // Many Append() implementations will avoid copying bytes if this function
0088   // returned an internal buffer.
0089   //
0090   // If the sink implementation allocates or reallocates an internal buffer,
0091   // it should use the desired_size_hint if appropriate. If a caller cannot
0092   // provide a reasonable guess at the desired capacity, it should set
0093   // desired_size_hint = 0.
0094   //
0095   // If a non-scratch buffer is returned, the caller may only pass
0096   // a prefix to it to Append(). That is, it is not correct to pass an
0097   // interior pointer to Append().
0098   //
0099   // The default implementation always returns the scratch buffer.
0100   virtual char* GetAppendBufferVariable(
0101       size_t min_size, size_t desired_size_hint, char* scratch,
0102       size_t scratch_size, size_t* allocated_size);
0103 
0104  private:
0105   // No copying
0106   Sink(const Sink&);
0107   void operator=(const Sink&);
0108 };
0109 
0110 // A Source is an interface that yields a sequence of bytes
0111 class Source {
0112  public:
0113   Source() { }
0114   virtual ~Source();
0115 
0116   // Return the number of bytes left to read from the source
0117   virtual size_t Available() const = 0;
0118 
0119   // Peek at the next flat region of the source.  Does not reposition
0120   // the source.  The returned region is empty iff Available()==0.
0121   //
0122   // Returns a pointer to the beginning of the region and store its
0123   // length in *len.
0124   //
0125   // The returned region is valid until the next call to Skip() or
0126   // until this object is destroyed, whichever occurs first.
0127   //
0128   // The returned region may be larger than Available() (for example
0129   // if this ByteSource is a view on a substring of a larger source).
0130   // The caller is responsible for ensuring that it only reads the
0131   // Available() bytes.
0132   virtual const char* Peek(size_t* len) = 0;
0133 
0134   // Skip the next n bytes.  Invalidates any buffer returned by
0135   // a previous call to Peek().
0136   // REQUIRES: Available() >= n
0137   virtual void Skip(size_t n) = 0;
0138 
0139  private:
0140   // No copying
0141   Source(const Source&);
0142   void operator=(const Source&);
0143 };
0144 
0145 // A Source implementation that yields the contents of a flat array
0146 class ByteArraySource : public Source {
0147  public:
0148   ByteArraySource(const char* p, size_t n) : ptr_(p), left_(n) { }
0149   ~ByteArraySource() override;
0150   size_t Available() const override;
0151   const char* Peek(size_t* len) override;
0152   void Skip(size_t n) override;
0153  private:
0154   const char* ptr_;
0155   size_t left_;
0156 };
0157 
0158 // A Sink implementation that writes to a flat array without any bound checks.
0159 class UncheckedByteArraySink : public Sink {
0160  public:
0161   explicit UncheckedByteArraySink(char* dest) : dest_(dest) { }
0162   ~UncheckedByteArraySink() override;
0163   void Append(const char* data, size_t n) override;
0164   char* GetAppendBuffer(size_t len, char* scratch) override;
0165   char* GetAppendBufferVariable(
0166       size_t min_size, size_t desired_size_hint, char* scratch,
0167       size_t scratch_size, size_t* allocated_size) override;
0168   void AppendAndTakeOwnership(
0169       char* bytes, size_t n, void (*deleter)(void*, const char*, size_t),
0170       void *deleter_arg) override;
0171 
0172   // Return the current output pointer so that a caller can see how
0173   // many bytes were produced.
0174   // Note: this is not a Sink method.
0175   char* CurrentDestination() const { return dest_; }
0176  private:
0177   char* dest_;
0178 };
0179 
0180 }  // namespace snappy
0181 
0182 #endif  // THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_