3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_TRANSFORMADAPTER_H__
29 #define TALK_BASE_TRANSFORMADAPTER_H__
31 #include "talk/base/stream.h"
34 ///////////////////////////////////////////////////////////////////////////////
36 class TransformInterface {
38 virtual ~TransformInterface() { }
40 // Transform should convert the in_len bytes of input into the out_len-sized
41 // output buffer. If flush is true, there will be no more data following
43 // After the transformation, in_len contains the number of bytes consumed, and
44 // out_len contains the number of bytes ready in output.
45 // Note: Transform should not return SR_BLOCK, as there is no asynchronous
46 // notification available.
47 virtual StreamResult Transform(const void * input, size_t * in_len,
48 void * output, size_t * out_len,
52 ///////////////////////////////////////////////////////////////////////////////
54 // TransformAdapter causes all data passed through to be transformed by the
55 // supplied TransformInterface object, which may apply compression, encryption,
58 class TransformAdapter : public StreamAdapterInterface {
60 // Note that the transformation is unidirectional, in the direction specified
61 // by the constructor. Operations in the opposite direction result in SR_EOS.
62 TransformAdapter(StreamInterface * stream,
63 TransformInterface * transform,
65 virtual ~TransformAdapter();
67 virtual StreamResult Read(void * buffer, size_t buffer_len,
68 size_t * read, int * error);
69 virtual StreamResult Write(const void * data, size_t data_len,
70 size_t * written, int * error);
73 // Apriori, we can't tell what the transformation does to the stream length.
74 virtual bool GetAvailable(size_t* size) const { return false; }
75 virtual bool ReserveSize(size_t size) { return true; }
77 // Transformations might not be restartable
78 virtual bool Rewind() { return false; }
81 enum State { ST_PROCESSING, ST_FLUSHING, ST_COMPLETE, ST_ERROR };
82 enum { BUFFER_SIZE = 1024 };
84 TransformInterface * transform_;
89 char buffer_[BUFFER_SIZE];
93 ///////////////////////////////////////////////////////////////////////////////
95 } // namespace talk_base
97 #endif // TALK_BASE_TRANSFORMADAPTER_H__