1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 /*
24  * This code is based on original Sfinx source code
25  * Copyright (c) 1994-1997 Janus B. Wisniewski and L.K. Avalon
26  */
27 
28 #ifndef CGE2_FILEIO_H
29 #define CGE2_FILEIO_H
30 
31 #include "common/file.h"
32 
33 namespace CGE2 {
34 
35 class CGE2Engine;
36 
37 #define kBtSize       2048
38 #define kBtKeySize    13
39 #define kBtLevel      2
40 #define kBtInnerCount ((kBtSize - 4 /*sizeof(Header) */) / (kBtKeySize + 2 /*sizeof(Inner) */))
41 #define kBtLeafCount  ((kBtSize - 4 /*sizeof(Header) */) / (kBtKeySize + 4 + 4 /*sizeof(BtKeypack) */))
42 #define kBtValNone    0xFFFF
43 #define kBtValRoot    0
44 #define kCatName      "VOL.CAT"
45 #define kDatName      "VOL.DAT"
46 #define kCryptSeed    0xA5
47 
48 enum ID {
49 	kIdNear, kIdMTake, kIdFTake, kIdPhase, kIdSeq,
50 	kIdName, kIdType, kIdFront, kIdEast,
51 	kIdPortable, kIdTransparent,
52 	kIdNone = -1
53 };
54 
55 struct BtKeypack {
56 	char _key[kBtKeySize];
57 	uint32 _pos;
58 	uint32 _size;
59 };
60 
61 struct Inner {
62 	uint8  _key[kBtKeySize];
63 	uint16 _down;
64 };
65 
66 struct Header {
67 	uint16 _count;
68 	uint16 _down;
69 };
70 
71 struct BtPage {
72 	Header _header;
73 	union {
74 		// dummy filler to make proper size of union
75 		uint8 _data[kBtSize - 4]; /* 4 is the size of struct Header */
76 		// inner version of data: key + word-sized page link
77 		Inner _inner[kBtInnerCount];
78 		// leaf version of data: key + all user data
79 		BtKeypack _leaf[kBtLeafCount];
80 	};
81 
82 	void readBTree(Common::ReadStream &s);
83 };
84 
85 class ResourceManager {
86 private:
87 	struct {
88 		BtPage *_page;
89 		uint16 _pageNo;
90 		int _index;
91 	} _buff[kBtLevel];
92 
93 	BtPage *getPage(int level, uint16 pageId);
94 	uint16 catRead(byte *buf, uint16 length);
95 	Common::File *_catFile;
96 	Common::File *_datFile;
97 	void xCrypt(byte *buf, uint16 length);
98 public:
99 	ResourceManager();
100 	~ResourceManager();
101 	uint16 read(byte *buf, uint16 length);
102 	bool seek(int32 offs, int whence = SEEK_SET);
103 
104 	BtKeypack *find(const char *key);
105 	bool exist(const char *name);
106 };
107 
108 class EncryptedStream {
109 private:
110 	CGE2Engine *_vm;
111 	Common::SeekableReadStream *_readStream;
112 	int _lineCount;
113 	bool _error;
114 public:
115 	EncryptedStream(CGE2Engine *vm, const char *name);
116 	~EncryptedStream();
117 	bool err();
118 	bool eos();
119 	bool seek(int32 offset);
120 	int32 pos();
121 	int32 size();
122 	uint32 read(byte *dataPtr, uint32 dataSize);
123 	int16 readSint16LE();
124 	uint32 readUint32LE();
125 	Common::String readLine();
getLineCount()126 	int getLineCount() { return _lineCount; }
127 
128 	static const char *kIdTab[];
129 };
130 
131 } // End of namespace CGE2
132 
133 #endif // CGE2_FILEIO_H
134