File: stepdump.cpp

package info (click to toggle)
sword 1.7.3+dfsg-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,584 kB
  • ctags: 5,515
  • sloc: cpp: 41,736; sh: 11,924; ansic: 6,447; objc: 1,362; makefile: 1,239; cs: 897; java: 821; perl: 784; pascal: 681; tcl: 350; xml: 180; python: 79
file content (337 lines) | stat: -rw-r--r-- 10,773 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/******************************************************************************
 *
 *  stepdump.cpp -	Utility to dump a STEP module
 *
 * $Id: stepdump.cpp 2833 2013-06-29 06:40:28Z chrislit $
 *
 * Copyright 2000-2013 CrossWire Bible Society (http://www.crosswire.org)
 *	CrossWire Bible Society
 *	P. O. Box 2528
 *	Tempe, AZ  85280-2528
 *
 * 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 version 2.
 *
 * 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.
 *
 */

#ifdef _MSC_VER
	#pragma warning( disable: 4251 )
	#pragma warning( disable: 4996 )
#endif

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

#include <iostream>
#include <string>

#ifndef __GNUC__
#include <io.h>
#else
#include <unistd.h>
#endif

#include <filemgr.h>
#include <lzsscomprs.h>

using namespace std;
#ifndef NO_SWORD_NAMESPACE
using namespace sword;
#endif

typedef struct {
	short versionRecordSize;
	short publisherID;
	short bookID;
	short setID;
	char conversionProgramVerMajor;
	char conversionProgramVerMinor;
	char leastCompatSTEPVerMajor;
	char leastCompatSTEPVerMinor;
	char encryptionType;
	char editionID;
	short modifiedBy;
} Version;

typedef struct {
	short viewableHeaderRecordSize;
	long viewableBlocksCount; // this is listed as nonGlossBlocksCount in spec!
	long glossBlocksCount;
	char compressionType; // 0 - none; 1 - LZSS
	char reserved1;
	short blockEntriesSize;
	short reserved2;
} ViewableHeader;

typedef struct {
	long offset;
	long uncompressedSize;
	long size;
} ViewableBlock;
		
void readVersion(int fd, Version *versionRecord);
void readHeaderControlWordAreaText(int fd, char **buf);
void readViewableHeader(int fd, ViewableHeader *viewableHeaderRecord);
void readViewableBlock(int fd, ViewableBlock *vb);
void readViewableBlockText(int fd, ViewableBlock *vb, char **buf);

SWCompress *compress = 0;

int main(int argc, char **argv) {

	compress = new LZSSCompress();
	char *buf;
	Version versionRecord;
	ViewableHeader viewableHeaderRecord;

	if (argc < 2) {
		cerr << "usage: "<< *argv << " <database to step module>\n";
		exit (-1);
	}

	string bookpath = argv[1];
	string fileName;

	if ((argv[1][strlen(argv[1])-1] != '/') &&
		(argv[1][strlen(argv[1])-1] != '\\'))
		bookpath += "/";

	fileName = bookpath + "Book.dat";
	int fd = FileMgr::openFileReadOnly(fileName.c_str());

	if (fd < 1) {
		cerr << "error, couldn't open file: " << fileName << "\n";
		exit (-2);
	}

	readVersion(fd, &versionRecord);
	readHeaderControlWordAreaText(fd, &buf);
	delete [] buf;


	fileName = bookpath + "Viewable.idx";
	int fdv = FileMgr::openFileReadOnly(fileName.c_str());

	if (fdv < 1) {
		cerr << "error, couldn't open file: " << fileName << "\n";
		exit (-3);
	}

	readVersion(fdv, &versionRecord);
	readViewableHeader(fdv, &viewableHeaderRecord);

	ViewableBlock vb;

	cout << "\n\nReading special preface viewable BLOCK 0";

	readViewableBlock(fdv, &vb);
	readViewableBlockText(fd, &vb, &buf);
	delete [] buf;
	
	int nonGlossBlocksCount = viewableHeaderRecord.viewableBlocksCount
					    - viewableHeaderRecord.glossBlocksCount;

	cout << "\n\nReading " << nonGlossBlocksCount << " non-glossary viewable blocks";
	// 1 because we already read the first block above
	for (int i = 1; i < nonGlossBlocksCount; i++) {
		cout << "\nNon-Glossary viewable block: " << i;
		readViewableBlock(fdv, &vb);
		readViewableBlockText(fd, &vb, &buf);
		delete [] buf;
	}

	cout << "\n\nReading " << viewableHeaderRecord.glossBlocksCount << " glossary viewable blocks";
	for (int i = 0; i < viewableHeaderRecord.glossBlocksCount; i++) {
		cout << "\nGlossary viewable block: " << i;
		readViewableBlock(fdv, &vb);
		readViewableBlockText(fd, &vb, &buf);
		delete [] buf;
	}

	close(fdv);
	close(fd);

}



void readVersion(int fd, Version *versionRecord) {
	int r;

	cout << "\n\nReading Version Record (" << 16/*sizeof(struct Version)*/ << " bytes)\n\n";
// DO NOT USE BECAUSE OF RECORD BYTE ALIGNMENT PROBLEMS
//	read(fd, &versionRecord, sizeof(struct Version));

	cout << "Version Record Information\n";
	r = read(fd, &(versionRecord->versionRecordSize), 2);
	if (r != 2)
		perror("ERROR: read failed in readVersion");
	cout << "\tversionRecordSize: " << versionRecord->versionRecordSize << "\n";
	r = read(fd, &(versionRecord->publisherID), 2);
	if (r != 2)
		perror("ERROR: read failed in readVersion");
	cout << "\tpublisherID: " << versionRecord->publisherID << "\n";
	r = read(fd, &(versionRecord->bookID), 2);
	if (r != 2)
		perror("ERROR: read failed in readVersion");
	cout << "\tbookID: " << versionRecord->bookID << "\n";
	r = read(fd, &(versionRecord->setID), 2);
	if (r != 2)
		perror("ERROR: read failed in readVersion");
	cout << "\tsetID: " << versionRecord->setID << "\n";
	r = read(fd, &(versionRecord->conversionProgramVerMajor), 1);
	if (r != 1)
		perror("ERROR: read failed in readVersion");
	cout << "\tconversionProgramVerMajor: " << (int)versionRecord->conversionProgramVerMajor << "\n";
	r = read(fd, &(versionRecord->conversionProgramVerMinor), 1);
	if (r != 1)
		perror("ERROR: read failed in readVersion");
	cout << "\tconversionProgramVerMinor: " << (int)versionRecord->conversionProgramVerMinor << "\n";
	r = read(fd, &(versionRecord->leastCompatSTEPVerMajor), 1);
	if (r != 1)
		perror("ERROR: read failed in readVersion");
	cout << "\tleastCompatSTEPVerMajor: " << (int)versionRecord->leastCompatSTEPVerMajor << "\n";
	r = read(fd, &(versionRecord->leastCompatSTEPVerMinor), 1);
	if (r != 1)
		perror("ERROR: read failed in readVersion");
	cout << "\tleastCompatSTEPVerMinor: " << (int)versionRecord->leastCompatSTEPVerMinor << "\n";
	r = read(fd, &(versionRecord->encryptionType), 1);
	if (r != 1)
		perror("ERROR: read failed in readVersion");
	cout << "\tencryptionType: " << (int)versionRecord->encryptionType << "\n";
	r = read(fd, &(versionRecord->editionID), 1);
	if (r != 1)
		perror("ERROR: read failed in readVersion");
	cout << "\teditionID: " << (int)versionRecord->editionID << "\n";
	r = read(fd, &(versionRecord->modifiedBy), 2);
	if (r != 2)
		perror("ERROR: read failed in readVersion");
	cout << "\tmodifiedBy: " << versionRecord->modifiedBy << "\n";

	int skip = versionRecord->versionRecordSize - 16/*sizeof(struct Version*/;

	if (skip) {
		cout << "\nSkipping " << skip << " unknown bytes.\n";
		char *skipbuf = new char[skip];
		r = read(fd, skipbuf, skip);
		if (r != skip)
			perror("ERROR: read failed in readVersion");
		delete [] skipbuf;
	}
}


void readViewableHeader(int fd, ViewableHeader *viewableHeaderRecord) {
	int r;
	cout << "\n\nReading Viewable Header Record (" << 16/*sizeof(struct ViewableHeader)*/ << " bytes)\n\n";

// DO NOT USE BECAUSE OF RECORD BYTE ALIGNMENT PROBLEMS
//	read(fd, &viewableHeaderRecord, sizeof(struct ViewableHeader));

	cout << "Viewable Header Record Information\n";
	r = read(fd, &(viewableHeaderRecord->viewableHeaderRecordSize), 2);
	if (r != 2)
		perror("ERROR: read failed in readViewableHeader");
	cout << "\tviewableHeaderRecordSize: " << viewableHeaderRecord->viewableHeaderRecordSize << "\n";
	r = read(fd, &(viewableHeaderRecord->viewableBlocksCount), 4);
	if (r != 4)
		perror("ERROR: read failed in readViewableHeader");
	cout << "\tviewableBlocksCount: " << viewableHeaderRecord->viewableBlocksCount << "\n";
	r = read(fd, &(viewableHeaderRecord->glossBlocksCount), 4);
	if (r != 4)
		perror("ERROR: read failed in readViewableHeader");
	cout << "\tglossBlocksCount: " << viewableHeaderRecord->glossBlocksCount << "\n";
	r = read(fd, &(viewableHeaderRecord->compressionType), 1);
	if (r != 1)
		perror("ERROR: read failed in readViewableHeader");
	cout << "\tcompressionType: " << (int)viewableHeaderRecord->compressionType << "(0 - none; 1 - LZSS)\n";
	r = read(fd, &(viewableHeaderRecord->reserved1), 1);
	if (r != 1)
		perror("ERROR: read failed in readViewableHeader");
	cout << "\treserved1: " << (int)viewableHeaderRecord->reserved1 << "\n";
	r = read(fd, &(viewableHeaderRecord->blockEntriesSize), 2);
	if (r != 2)
		perror("ERROR: read failed in readViewableHeader");
	cout << "\tblockEntriesSize: " << viewableHeaderRecord->blockEntriesSize << "\n";
	r = read(fd, &(viewableHeaderRecord->reserved2), 2);
	if (r != 2)
		perror("ERROR: read failed in readViewableHeader");
	cout << "\treserved2: " << viewableHeaderRecord->reserved2 << "\n";

	int skip = viewableHeaderRecord->viewableHeaderRecordSize - 16/*sizeof(struct ViewableHeader)*/;

	if (skip) {
		cout << "\nSkipping " << skip << " unknown bytes.\n";
		char *skipbuf = new char[skip];
		r = read(fd, skipbuf, skip);
		if (r != skip)
			perror("ERROR: read failed in readViewableHeader");
		delete [] skipbuf;
	}
}


void readViewableBlockText(int fd, ViewableBlock *vb, char **buf) {
	int r;
	unsigned long size = vb->size;

	*buf = new char [ ((vb->size > vb->uncompressedSize) ? vb->size : vb->uncompressedSize) + 1 ];
	lseek(fd, vb->offset, SEEK_SET);
	r = read(fd, *buf, vb->size);
	if (r != vb->size)
		perror("ERROR: read failed in readViewableBlocktext");

	compress->zBuf(&size, *buf);
	strcpy(*buf, compress->Buf());
	cout << "Viewable Block Text:\n";
	cout << *buf << "\n\n";
}


void readViewableBlock(int fd, ViewableBlock *vb) {
	int r;
	cout << "\n\nReading Viewable Block (" << 12/*sizeof(struct ViewableHeader)*/ << " bytes)\n\n";

// DO NOT USE BECAUSE OF RECORD BYTE ALIGNMENT PROBLEMS
//	read(fd, &vb, sizeof(struct ViewableBlock));

	cout << "Viewable Block Information\n";
	r = read(fd, &(vb->offset), 4);
	if (r != 4)
		perror("ERROR: read failed in readViewableBlock");
	cout << "\toffset: " << vb->offset << "\n";
	r = read(fd, &(vb->uncompressedSize), 4);
	if (r != 4)
		perror("ERROR: read failed in readViewableBlock");
	cout << "\tuncompressedSize: " << vb->uncompressedSize << "\n";
	r = read(fd, &(vb->size), 4);
	if (r != 4)
		perror("ERROR: read failed in readViewableBlock");
	cout << "\tsize: " << vb->size << "\n";
}


void readHeaderControlWordAreaText(int fd, char **buf) {
	int r;
	long headerControlWordAreaSize;
	r = read(fd, &headerControlWordAreaSize, 4);
	if (r != 4)
		perror("ERROR: read failed in readHeaderControlWordAreaText");
	cout << "Reading Header Control Word Area (" << headerControlWordAreaSize << " bytes)\n\n";

	*buf = new char [headerControlWordAreaSize + 1];

	r = read(fd, *buf, headerControlWordAreaSize);
	if (r != headerControlWordAreaSize)
		perror("ERROR: read failed in readHeaderControlWordAreaText");
	(*buf)[headerControlWordAreaSize] = 0;

	cout << "headerControlWordArea:\n" << *buf << "\n";
}