This source file includes following definitions.
- create
- m_isBytesAdded
- initialize
- setStreamParameter
- addBytes
- finish
- reset
- softReset
- create
- initialize
- addBytes
- finish
- reset
#include "config.h"
#include "modules/websockets/WebSocketDeflater.h"
#include "platform/Logging.h"
#include "wtf/FastMalloc.h"
#include "wtf/HashMap.h"
#include "wtf/StdLibExtras.h"
#include "wtf/StringExtras.h"
#include "wtf/text/StringHash.h"
#include "wtf/text/WTFString.h"
#include <zlib.h>
namespace WebCore {
static const int defaultMemLevel = 1;
static const size_t bufferIncrementUnit = 4096;
PassOwnPtr<WebSocketDeflater> WebSocketDeflater::create(int windowBits, ContextTakeOverMode contextTakeOverMode)
{
return adoptPtr(new WebSocketDeflater(windowBits, contextTakeOverMode));
}
WebSocketDeflater::WebSocketDeflater(int windowBits, ContextTakeOverMode contextTakeOverMode)
: m_windowBits(windowBits)
, m_contextTakeOverMode(contextTakeOverMode)
, m_isBytesAdded(false)
{
ASSERT(m_windowBits >= 8);
ASSERT(m_windowBits <= 15);
m_stream = adoptPtr(new z_stream);
memset(m_stream.get(), 0, sizeof(z_stream));
}
bool WebSocketDeflater::initialize()
{
return deflateInit2(m_stream.get(), Z_DEFAULT_COMPRESSION, Z_DEFLATED, -m_windowBits, defaultMemLevel, Z_DEFAULT_STRATEGY) == Z_OK;
}
WebSocketDeflater::~WebSocketDeflater()
{
int result = deflateEnd(m_stream.get());
if (result != Z_OK)
WTF_LOG(Network, "WebSocketDeflater %p Destructor deflateEnd() failed: %d is returned", this, result);
}
static void setStreamParameter(z_stream* stream, const char* inputData, size_t inputLength, char* outputData, size_t outputLength)
{
stream->next_in = reinterpret_cast<Bytef*>(const_cast<char*>(inputData));
stream->avail_in = inputLength;
stream->next_out = reinterpret_cast<Bytef*>(outputData);
stream->avail_out = outputLength;
}
bool WebSocketDeflater::addBytes(const char* data, size_t length)
{
if (!length)
return false;
size_t maxLength = deflateBound(m_stream.get(), length);
do {
size_t writePosition = m_buffer.size();
m_buffer.grow(writePosition + maxLength);
setStreamParameter(m_stream.get(), data, length, m_buffer.data() + writePosition, maxLength);
int result = deflate(m_stream.get(), Z_NO_FLUSH);
if (result != Z_OK)
return false;
m_buffer.shrink(writePosition + maxLength - m_stream->avail_out);
maxLength *= 2;
} while (m_stream->avail_in > 0);
m_isBytesAdded = true;
return true;
}
bool WebSocketDeflater::finish()
{
if (!m_isBytesAdded) {
ASSERT(!m_buffer.size());
m_buffer.append("\x00", 1);
return true;
}
while (true) {
size_t writePosition = m_buffer.size();
m_buffer.grow(writePosition + bufferIncrementUnit);
size_t availableCapacity = m_buffer.size() - writePosition;
setStreamParameter(m_stream.get(), 0, 0, m_buffer.data() + writePosition, availableCapacity);
int result = deflate(m_stream.get(), Z_SYNC_FLUSH);
m_buffer.shrink(writePosition + availableCapacity - m_stream->avail_out);
if (result == Z_OK)
break;
if (result != Z_BUF_ERROR)
return false;
}
if (m_buffer.size() <= 4)
return false;
m_buffer.resize(m_buffer.size() - 4);
m_isBytesAdded = false;
return true;
}
void WebSocketDeflater::reset()
{
m_buffer.clear();
m_isBytesAdded = false;
if (m_contextTakeOverMode == DoNotTakeOverContext)
deflateReset(m_stream.get());
}
void WebSocketDeflater::softReset()
{
m_buffer.clear();
}
PassOwnPtr<WebSocketInflater> WebSocketInflater::create(int windowBits)
{
return adoptPtr(new WebSocketInflater(windowBits));
}
WebSocketInflater::WebSocketInflater(int windowBits)
: m_windowBits(windowBits)
{
m_stream = adoptPtr(new z_stream);
memset(m_stream.get(), 0, sizeof(z_stream));
}
bool WebSocketInflater::initialize()
{
return inflateInit2(m_stream.get(), -m_windowBits) == Z_OK;
}
WebSocketInflater::~WebSocketInflater()
{
int result = inflateEnd(m_stream.get());
if (result != Z_OK)
WTF_LOG(Network, "WebSocketInflater %p Destructor inflateEnd() failed: %d is returned", this, result);
}
bool WebSocketInflater::addBytes(const char* data, size_t length)
{
if (!length)
return false;
size_t consumedSoFar = 0;
while (consumedSoFar < length) {
size_t writePosition = m_buffer.size();
m_buffer.grow(writePosition + bufferIncrementUnit);
size_t availableCapacity = m_buffer.size() - writePosition;
size_t remainingLength = length - consumedSoFar;
setStreamParameter(m_stream.get(), data + consumedSoFar, remainingLength, m_buffer.data() + writePosition, availableCapacity);
int result = inflate(m_stream.get(), Z_NO_FLUSH);
consumedSoFar += remainingLength - m_stream->avail_in;
m_buffer.shrink(writePosition + availableCapacity - m_stream->avail_out);
if (result == Z_BUF_ERROR)
continue;
if (result == Z_STREAM_END) {
if (inflateReset(m_stream.get()) != Z_OK)
return false;
continue;
}
if (result != Z_OK)
return false;
ASSERT(remainingLength > m_stream->avail_in);
}
ASSERT(consumedSoFar == length);
return true;
}
bool WebSocketInflater::finish()
{
static const char strippedFields[] = "\0\0\xff\xff";
static const size_t strippedLength = 4;
size_t consumedSoFar = 0;
while (consumedSoFar < strippedLength) {
size_t writePosition = m_buffer.size();
m_buffer.grow(writePosition + bufferIncrementUnit);
size_t availableCapacity = m_buffer.size() - writePosition;
size_t remainingLength = strippedLength - consumedSoFar;
setStreamParameter(m_stream.get(), strippedFields + consumedSoFar, remainingLength, m_buffer.data() + writePosition, availableCapacity);
int result = inflate(m_stream.get(), Z_FINISH);
consumedSoFar += remainingLength - m_stream->avail_in;
m_buffer.shrink(writePosition + availableCapacity - m_stream->avail_out);
if (result == Z_BUF_ERROR)
continue;
if (result != Z_OK && result != Z_STREAM_END)
return false;
ASSERT(remainingLength > m_stream->avail_in);
}
ASSERT(consumedSoFar == strippedLength);
return true;
}
void WebSocketInflater::reset()
{
m_buffer.clear();
}
}