summaryrefslogtreecommitdiff
path: root/encoder.h
blob: 1ff08794f9ceaf11696c0dfc454072ab23919562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef ENCODER_H
#define ENCODER_H

#include <boost/cstdint.hpp>
#include <boost/function.hpp>
#include <boost/functional/factory.hpp>
#include <boost/functional/value_factory.hpp>
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/shared_ptr.hpp>

#include <string>

class EncoderBase {
	public:
		typedef boost::shared_ptr<EncoderBase> p;
		virtual size_t encode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) = 0;
		virtual size_t flush(uint8_t *output, size_t output_size) = 0;
};

class EncoderFilter : public boost::iostreams::multichar_input_filter {
	private:
		EncoderBase::p encoder;
	public:
		typedef boost::shared_ptr<EncoderFilter> p;
		EncoderFilter(EncoderBase::p encoder_);
		template<typename Source>
		std::streamsize read(Source& src, char *s, std::streamsize n) {
			char src_data[0x2000];
			std::streamsize src_read = boost::iostreams::read(src, src_data, 0x2000);
			if(src_read < 0)
				src_read = 0;
			return encoder->encode((const uint8_t*)src_data, src_read, (uint8_t*)s, n);
		};
};

namespace encoder {
	void init();
	EncoderFilter::p get_encoder(const std::string& name);
};

#endif