Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / transformadapter.h
1 /*
2  * libjingle
3  * Copyright 2004--2005, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without 
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
15  *
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.
26  */
27
28 #ifndef TALK_BASE_TRANSFORMADAPTER_H__
29 #define TALK_BASE_TRANSFORMADAPTER_H__
30
31 #include "talk/base/stream.h"
32
33 namespace talk_base {
34 ///////////////////////////////////////////////////////////////////////////////
35
36 class TransformInterface {
37 public:
38   virtual ~TransformInterface() { }
39
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
42   // input.
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,
49                                  bool flush) = 0;
50 };
51
52 ///////////////////////////////////////////////////////////////////////////////
53
54 // TransformAdapter causes all data passed through to be transformed by the
55 // supplied TransformInterface object, which may apply compression, encryption,
56 // etc.
57
58 class TransformAdapter : public StreamAdapterInterface {
59 public:
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,
64                    bool direction_read);
65   virtual ~TransformAdapter();
66   
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);
71   virtual void Close();
72
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; }
76
77   // Transformations might not be restartable
78   virtual bool Rewind() { return false; }
79
80 private:
81   enum State { ST_PROCESSING, ST_FLUSHING, ST_COMPLETE, ST_ERROR };
82   enum { BUFFER_SIZE = 1024 };
83
84   TransformInterface * transform_;
85   bool direction_read_;
86   State state_;
87   int error_;
88
89   char buffer_[BUFFER_SIZE];
90   size_t len_;
91 };
92
93 ///////////////////////////////////////////////////////////////////////////////
94
95 } // namespace talk_base
96
97 #endif // TALK_BASE_TRANSFORMADAPTER_H__