summaryrefslogtreecommitdiff
path: root/decoder.h
blob: 31986f7526c6a2e1292070efae78944848418124 (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
50
51
52
53
54
55
56
#ifndef DECODER_H
#define DECODER_H

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

#include <string>

class Source {
	public:
		virtual std::streamsize read(char* buf, std::streamsize buf_size) = 0;
};

template<class T>
class StreamSource : public Source {
	private:
		T& s;
	
	public:
		StreamSource(T& s_) : s(s_) {}
		std::streamsize read(char* buf, std::streamsize buf_size) {
			return boost::iostreams::read(s, buf, buf_size);
		}
};

class DecoderBase {
	public:
		typedef boost::shared_ptr<DecoderBase> p;
		virtual ~DecoderBase() {}
		
		virtual size_t decode(Source& input, uint8_t *output, size_t output_size) = 0;
};

//! Input filter to hold a decoder in a filter chain.
class DecoderFilter : public boost::iostreams::multichar_input_filter {
	private:
		DecoderBase::p decoder;
	
	public:
		typedef boost::shared_ptr<DecoderFilter> p;
		DecoderFilter(DecoderBase::p decoder_);
		
		template<typename Source>
		std::streamsize read(Source& src, char *s, std::streamsize n) {
			StreamSource<Source> src_f(src);
			return decoder->decode(src_f, (uint8_t*)s, n);
		}
};

namespace decoder {
	void init();
	DecoderFilter::p get_decoder(const std::string& name);
};

#endif