summaryrefslogtreecommitdiff
path: root/encoder.h
blob: 7021cdccc20598f329f4b5f3bcdce980d907d216 (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
#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 <string>

class EncoderBase {
	public:
		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& encoder;
	public:
		EncoderFilter(EncoderBase& encoder_) : encoder(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();
	EncoderBase *get_encoder(const std::string& name);
};

#endif