summaryrefslogtreecommitdiff
path: root/encoder.h
blob: f68d51c8182adeddb49d1922873ce719f42347e8 (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
43
44
45
46
47
48
49
#ifndef ENCODER_H
#define ENCODER_H

#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/shared_ptr.hpp>

#include <iostream>
#include <string>

class EncoderBase {
	public:
		typedef boost::shared_ptr<EncoderBase> p;
		virtual ~EncoderBase() {}
		
		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;
};

//! Input filter to hold an encoder in a filter chain.
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;
			std::streamsize size = encoder->encode((const uint8_t*)src_data, src_read, (uint8_t*)s, n);
			// no more data, flush encoder
			if(src_read == 0 && size == 0) {
				size = encoder->flush((uint8_t*)s, n);
			}
			return size;
		};
};

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

#endif