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
#include "talk/base/transformadapter.h"
32
#include "talk/base/common.h"
36
///////////////////////////////////////////////////////////////////////////////
38
TransformAdapter::TransformAdapter(StreamInterface * stream,
39
TransformInterface * transform,
41
: StreamAdapterInterface(stream), transform_(transform),
42
direction_read_(direction_read), state_(ST_PROCESSING), len_(0) {
45
TransformAdapter::~TransformAdapter() {
46
TransformAdapter::Close();
51
TransformAdapter::Read(void * buffer, size_t buffer_len,
52
size_t * read, int * error) {
56
while (state_ != ST_ERROR) {
57
if (state_ == ST_COMPLETE)
61
if ((state_ == ST_PROCESSING) && (len_ < sizeof(buffer_))) {
63
StreamResult result = StreamAdapterInterface::Read(
65
sizeof(buffer_) - len_,
68
if (result == SR_BLOCK) {
70
} else if (result == SR_ERROR) {
73
} else if (result == SR_EOS) {
80
// Process buffered data
82
size_t out_len = buffer_len;
83
StreamResult result = transform_->Transform(buffer_, &in_len,
85
(state_ == ST_FLUSHING));
86
ASSERT(result != SR_BLOCK);
87
if (result == SR_EOS) {
88
// Note: Don't signal SR_EOS this iteration, unless out_len is zero
90
} else if (result == SR_ERROR) {
92
error_ = -1; // TODO: propagate error
94
} else if ((out_len == 0) && (state_ == ST_FLUSHING)) {
95
// If there is no output AND no more input, then something is wrong
97
error_ = -1; // TODO: better error code?
103
memmove(buffer_, buffer_ + in_len, len_);
119
TransformAdapter::Write(const void * data, size_t data_len,
120
size_t * written, int * error) {
124
size_t bytes_written = 0;
125
while (state_ != ST_ERROR) {
126
if (state_ == ST_COMPLETE)
129
if (len_ < sizeof(buffer_)) {
130
// Process buffered data
131
size_t in_len = data_len;
132
size_t out_len = sizeof(buffer_) - len_;
133
StreamResult result = transform_->Transform(data, &in_len,
134
buffer_ + len_, &out_len,
135
(state_ == ST_FLUSHING));
137
ASSERT(result != SR_BLOCK);
138
if (result == SR_EOS) {
139
// Note: Don't signal SR_EOS this iteration, unless no data written
140
state_ = ST_COMPLETE;
141
} else if (result == SR_ERROR) {
142
ASSERT(false); // When this happens, think about what should be done
144
error_ = -1; // TODO: propagate error
149
bytes_written = in_len;
155
StreamResult result = StreamAdapterInterface::Write(buffer_ + pos,
159
if (result == SR_BLOCK) {
160
ASSERT(false); // TODO: we should handle this
162
} else if (result == SR_ERROR) {
165
} else if (result == SR_EOS) {
166
state_ = ST_COMPLETE;
175
memmove(buffer_, buffer_ + pos, len_);
177
if (bytes_written == 0)
181
*written = bytes_written;
191
TransformAdapter::Close() {
192
if (!direction_read_ && (state_ == ST_PROCESSING)) {
193
state_ = ST_FLUSHING;
195
Write(0, 0, NULL, NULL);
196
} while (state_ == ST_FLUSHING);
198
state_ = ST_COMPLETE;
199
StreamAdapterInterface::Close();
202
} // namespace talk_base