Further compression and png work.
This commit is contained in:
parent
318b481ccc
commit
9c8faa534b
34 changed files with 1164 additions and 203 deletions
|
@ -30,6 +30,9 @@ list(APPEND core_LIB_INCLUDES
|
|||
StringUtils.cpp
|
||||
streams/BinaryStream.cpp
|
||||
streams/BitStream.cpp
|
||||
streams/InputBitStream.cpp
|
||||
streams/OutputBitStream.cpp
|
||||
streams/BufferBitStream.cpp
|
||||
http/HttpResponse.cpp
|
||||
http/HttpHeader.cpp
|
||||
http/HttpRequest.cpp
|
||||
|
|
|
@ -25,6 +25,16 @@ bool StringUtils::IsAlphabetical(char c)
|
|||
return std::isalpha(c);
|
||||
}
|
||||
|
||||
std::vector<unsigned char> StringUtils::toBytes(const std::string& input)
|
||||
{
|
||||
return {input.begin(), input.end()};
|
||||
}
|
||||
|
||||
std::string StringUtils::toString(const std::vector<unsigned char>& bytes)
|
||||
{
|
||||
return {bytes.begin(), bytes.end()};
|
||||
}
|
||||
|
||||
std::vector<std::string> StringUtils::toLines(const std::string& input)
|
||||
{
|
||||
auto result = std::vector<std::string>{};
|
||||
|
|
|
@ -30,4 +30,8 @@ public:
|
|||
static std::vector<std::string> toLines(const std::string& input);
|
||||
|
||||
static std::string stripQuotes(const std::string& input);
|
||||
|
||||
static std::vector<unsigned char> toBytes(const std::string& input);
|
||||
static std::string toString(const std::vector<unsigned char>& bytes);
|
||||
|
||||
};
|
||||
|
|
|
@ -2,37 +2,70 @@
|
|||
|
||||
#include "ByteUtils.h"
|
||||
|
||||
bool BitStream::loadNextByte()
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
BitStream::~BitStream()
|
||||
{
|
||||
if (mByteOffset + 1 == mBuffer.size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mByteOffset++;
|
||||
mCurrentByte = mBuffer[mByteOffset];
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool BitStream::getNextNBits(unsigned n, unsigned char& buffer)
|
||||
unsigned char BitStream::getCurrentByte()
|
||||
{
|
||||
int overshoot = n + mBitOffset - 7;
|
||||
if (mByteOffset < 0)
|
||||
{
|
||||
readNextByte();
|
||||
}
|
||||
return mCurrentByte;
|
||||
}
|
||||
|
||||
|
||||
int BitStream::getCurrentByteOffset() const
|
||||
{
|
||||
return mByteOffset;
|
||||
}
|
||||
|
||||
unsigned BitStream::getCurrentBitOffset() const
|
||||
{
|
||||
return mBitOffset;
|
||||
}
|
||||
|
||||
std::string BitStream::logNextNBytes(unsigned n) const
|
||||
{
|
||||
std::stringstream sstr;
|
||||
unsigned count{0};
|
||||
for(auto byte : peekNextNBytes(n))
|
||||
{
|
||||
sstr << count << " | " << ByteUtils::toString(byte) + '\n';
|
||||
count++;
|
||||
}
|
||||
return sstr.str();
|
||||
}
|
||||
|
||||
bool BitStream::readNextNBits(unsigned n, unsigned char& buffer)
|
||||
{
|
||||
if (mByteOffset < 0)
|
||||
{
|
||||
if (!readNextByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int overshoot = n + mBitOffset - 8;
|
||||
if (overshoot > 0)
|
||||
{
|
||||
unsigned char last_byte = mCurrentByte;
|
||||
if (!loadNextByte())
|
||||
if (!readNextByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto num_lower = 7 - mBitOffset;
|
||||
auto num_lower = 8 - mBitOffset;
|
||||
char lower_bits = ByteUtils::getHigherNBits(last_byte, num_lower);
|
||||
char higher_bits = ByteUtils::getLowerNBits(mCurrentByte, overshoot);
|
||||
|
||||
buffer = (higher_bits << (8 - num_lower)) | (lower_bits >> mBitOffset);
|
||||
buffer = (higher_bits << num_lower) | lower_bits;
|
||||
|
||||
mBitOffset = overshoot;
|
||||
return true;
|
||||
|
@ -44,13 +77,3 @@ bool BitStream::getNextNBits(unsigned n, unsigned char& buffer)
|
|||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void BitStream::setByte(unsigned idx, unsigned char data)
|
||||
{
|
||||
mBuffer[idx] = data;
|
||||
}
|
||||
|
||||
void BitStream::setBufferSize(std::size_t size)
|
||||
{
|
||||
mBuffer = std::vector<unsigned char>(size);
|
||||
}
|
||||
|
|
|
@ -1,27 +1,35 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
class BitStream
|
||||
{
|
||||
public:
|
||||
bool getNextNBits(unsigned n, unsigned char& buffer);
|
||||
virtual ~BitStream();
|
||||
|
||||
bool loadNextByte();
|
||||
unsigned char getCurrentByte();
|
||||
|
||||
void setByte(unsigned idx, unsigned char data);
|
||||
int getCurrentByteOffset() const;
|
||||
|
||||
void setBufferSize(std::size_t size);
|
||||
unsigned getCurrentBitOffset() const;
|
||||
|
||||
unsigned char getCurrentByte() const
|
||||
{
|
||||
return mCurrentByte;
|
||||
}
|
||||
virtual bool isFinished() const = 0;
|
||||
|
||||
private:
|
||||
unsigned mByteOffset{0};
|
||||
std::string logNextNBytes(unsigned n) const;
|
||||
|
||||
virtual std::vector<unsigned char> peekNextNBytes(unsigned n) const = 0;
|
||||
|
||||
virtual bool readNextNBits(unsigned n, unsigned char& buffer);
|
||||
|
||||
virtual std::optional<unsigned char> readNextByte() = 0;
|
||||
|
||||
virtual void writeByte(unsigned char data) = 0;
|
||||
|
||||
protected:
|
||||
int mByteOffset{0};
|
||||
unsigned mBitOffset{0};
|
||||
|
||||
char mCurrentByte{0};
|
||||
std::vector<unsigned char> mBuffer;
|
||||
unsigned char mCurrentByte{0};
|
||||
};
|
||||
|
|
49
src/core/streams/BufferBitStream.cpp
Normal file
49
src/core/streams/BufferBitStream.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include "BufferBitStream.h"
|
||||
|
||||
bool BufferBitStream::isFinished() const
|
||||
{
|
||||
return mByteOffset == mBuffer.size();
|
||||
}
|
||||
|
||||
std::vector<unsigned char> BufferBitStream::peekNextNBytes(unsigned n) const
|
||||
{
|
||||
std::vector<unsigned char> ret (n, 0);
|
||||
unsigned count = 0;
|
||||
for(unsigned idx=mByteOffset; idx<mByteOffset + n; idx++)
|
||||
{
|
||||
if (idx == mBuffer.size())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
ret[count] = mBuffer[idx];
|
||||
count ++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::optional<unsigned char> BufferBitStream::readNextByte()
|
||||
{
|
||||
if (mByteOffset + 1 == mBuffer.size())
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
else
|
||||
{
|
||||
mByteOffset++;
|
||||
mCurrentByte = mBuffer[mByteOffset];
|
||||
return mCurrentByte;
|
||||
}
|
||||
}
|
||||
|
||||
void BufferBitStream::setBuffer(const std::vector<unsigned char>& data)
|
||||
{
|
||||
mBuffer = data;
|
||||
}
|
||||
|
||||
void BufferBitStream::writeByte(unsigned char data)
|
||||
{
|
||||
mBuffer.push_back(data);
|
||||
}
|
||||
|
||||
|
27
src/core/streams/BufferBitStream.h
Normal file
27
src/core/streams/BufferBitStream.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include "BitStream.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
class BufferBitStream : public BitStream
|
||||
{
|
||||
public:
|
||||
bool isFinished() const override;
|
||||
|
||||
std::vector<unsigned char> peekNextNBytes(unsigned n) const override;
|
||||
|
||||
std::optional<unsigned char> readNextByte() override;
|
||||
|
||||
void setBuffer(const std::vector<unsigned char>& data);
|
||||
|
||||
void writeByte(unsigned char data) override;
|
||||
|
||||
const std::vector<unsigned char>& getBuffer() const
|
||||
{
|
||||
return mBuffer;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<unsigned char> mBuffer;
|
||||
};
|
35
src/core/streams/InputBitStream.cpp
Normal file
35
src/core/streams/InputBitStream.cpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include "InputBitStream.h"
|
||||
|
||||
InputBitStream::InputBitStream(std::basic_istream<unsigned char>* stream)
|
||||
: BitStream(),
|
||||
mStream(stream)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool InputBitStream::isFinished() const
|
||||
{
|
||||
return mStream->good();
|
||||
}
|
||||
|
||||
std::vector<unsigned char> InputBitStream::peekNextNBytes(unsigned n) const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<unsigned char> InputBitStream::readNextByte()
|
||||
{
|
||||
if (mStream->good())
|
||||
{
|
||||
return mStream->get();
|
||||
}
|
||||
else
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
void InputBitStream::writeByte(unsigned char data)
|
||||
{
|
||||
|
||||
}
|
21
src/core/streams/InputBitStream.h
Normal file
21
src/core/streams/InputBitStream.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
#pragma once
|
||||
|
||||
#include "BitStream.h"
|
||||
|
||||
#include <istream>
|
||||
|
||||
class InputBitStream : public BitStream
|
||||
{
|
||||
InputBitStream(std::basic_istream<unsigned char>* stream);
|
||||
|
||||
bool isFinished() const override;
|
||||
|
||||
std::vector<unsigned char> peekNextNBytes(unsigned n) const override;
|
||||
|
||||
std::optional<unsigned char> readNextByte() override;
|
||||
|
||||
void writeByte(unsigned char data) override;
|
||||
|
||||
private:
|
||||
std::basic_istream<unsigned char>* mStream{nullptr};
|
||||
};
|
28
src/core/streams/OutputBitStream.cpp
Normal file
28
src/core/streams/OutputBitStream.cpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include "OutputBitStream.h"
|
||||
|
||||
OutputBitStream::OutputBitStream(std::basic_ostream<char>* stream)
|
||||
: BitStream(),
|
||||
mStream(stream)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool OutputBitStream::isFinished() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> OutputBitStream::peekNextNBytes(unsigned n) const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<unsigned char> OutputBitStream::readNextByte()
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void OutputBitStream::writeByte(unsigned char data)
|
||||
{
|
||||
(*mStream) << data;
|
||||
}
|
22
src/core/streams/OutputBitStream.h
Normal file
22
src/core/streams/OutputBitStream.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include "BitStream.h"
|
||||
|
||||
#include <ostream>
|
||||
|
||||
class OutputBitStream : public BitStream
|
||||
{
|
||||
public:
|
||||
OutputBitStream(std::basic_ostream<char>* stream);
|
||||
|
||||
bool isFinished() const override;
|
||||
|
||||
std::vector<unsigned char> peekNextNBytes(unsigned n) const override;
|
||||
|
||||
std::optional<unsigned char> readNextByte() override;
|
||||
|
||||
void writeByte(unsigned char data) override;
|
||||
|
||||
private:
|
||||
std::basic_ostream<char>* mStream{nullptr};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue