summaryrefslogtreecommitdiffstats
path: root/fbreader/src/formats/chm/CHMFile.h
blob: d98bd840fd4d1ef36da992614e9e0d8194dbf808 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#ifndef __CHMFILE_H__
#define __CHMFILE_H__

#include <string>
#include <map>
#include <vector>

#include <shared_ptr.h>
#include <ZLInputStream.h>

class ZLFile;

class LZXDecompressor;

class CHMFileInfo {

public:
	struct FileNames {
		std::string TOC;
		std::string Index;
		std::string Start;
		std::string Home;

		bool empty() const;
	};

public:
	CHMFileInfo(const ZLFile &file);
	bool init(ZLInputStream &stream);
	// We assume that base exists and is already open
	shared_ptr<ZLInputStream> entryStream(shared_ptr<ZLInputStream> base, const std::string &name) const;
	// We assume that base exists and is already open
	FileNames sectionNames(shared_ptr<ZLInputStream> base) const;
	const std::string filePath() const;

private:
	bool moveToEntry(ZLInputStream &stream, const std::string &entryName);

private:
	unsigned long long mySection0Offset;

	struct RecordInfo {
		RecordInfo(int section, int offset, int length) : Section(section), Offset(offset), Length(length) {}
		std::size_t Section;
		std::size_t Offset;
		std::size_t Length;
	};

	typedef std::map<std::string,RecordInfo> RecordMap;
	RecordMap myRecords;
	std::vector<std::string> mySectionNames;

	struct SectionInfo {
		std::size_t WindowSizeIndex;
		std::size_t ResetInterval;
		std::size_t Offset;
		std::size_t Length;
		std::size_t CompressedSize;
		std::size_t UncompressedSize;
		std::vector<std::size_t> ResetTable;
	};
	std::vector<SectionInfo> mySectionInfos;

	const std::string myFilePath;

private:
	CHMFileInfo(const CHMFileInfo&);
	const CHMFileInfo &operator = (const CHMFileInfo&);

friend class CHMInputStream;
};

class CHMInputStream : public ZLInputStream {

public:
	CHMInputStream(shared_ptr<ZLInputStream> base, const CHMFileInfo::SectionInfo &sectionInfo, std::size_t offset, std::size_t size);
	~CHMInputStream();

	bool open();
	std::size_t read(char *buffer, std::size_t maxSize);
	void close();

	void seek(int offset, bool absoluteOffset);
	std::size_t offset() const;
	std::size_t sizeOfOpened();

private:
	std::size_t do_read(char *buffer, std::size_t maxSize);

private:
	shared_ptr<ZLInputStream> myBase;
	const CHMFileInfo::SectionInfo mySectionInfo;
	std::size_t myBaseStartIndex;
	std::size_t myBaseIndex;
	std::size_t myBytesToSkip;
	const std::size_t mySize;

	std::size_t myOffset;
	bool myDoSkip;

	shared_ptr<LZXDecompressor> myDecompressor;
	std::string myInData;
	
	unsigned char *myOutData;
	std::size_t myOutDataOffset;
	std::size_t myOutDataLength;
};

#endif /* __CHMFILE_H__ */