The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stepdump.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * stepdump.cpp - Utility to dump a STEP module
4  *
5  * $Id: stepdump.cpp 3754 2020-07-10 17:45:48Z scribe $
6  *
7  * Copyright 2000-2013 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 #ifdef _MSC_VER
24  #pragma warning( disable: 4251 )
25  #pragma warning( disable: 4996 )
26 #endif
27 
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 
32 #include <iostream>
33 #include <string>
34 
35 #ifndef __GNUC__
36 #include <io.h>
37 #else
38 #include <unistd.h>
39 #endif
40 
41 #include <filemgr.h>
42 #include <lzsscomprs.h>
43 
44 using namespace std;
45 #ifndef NO_SWORD_NAMESPACE
46 using namespace sword;
47 #endif
48 
49 typedef struct {
50  short versionRecordSize;
51  short publisherID;
52  short bookID;
53  short setID;
54  char conversionProgramVerMajor;
55  char conversionProgramVerMinor;
56  char leastCompatSTEPVerMajor;
57  char leastCompatSTEPVerMinor;
58  char encryptionType;
59  char editionID;
60  short modifiedBy;
61 } Version;
62 
63 typedef struct {
64  short viewableHeaderRecordSize;
65  long viewableBlocksCount; // this is listed as nonGlossBlocksCount in spec!
66  long glossBlocksCount;
67  char compressionType; // 0 - none; 1 - LZSS
68  char reserved1;
69  short blockEntriesSize;
70  short reserved2;
72 
73 typedef struct {
74  long offset;
75  long uncompressedSize;
76  long size;
78 
79 void readVersion(int fd, Version *versionRecord);
80 void readHeaderControlWordAreaText(int fd, char **buf);
81 void readViewableHeader(int fd, ViewableHeader *viewableHeaderRecord);
82 void readViewableBlock(int fd, ViewableBlock *vb);
83 void readViewableBlockText(int fd, ViewableBlock *vb, char **buf);
84 
86 
87 int main(int argc, char **argv) {
88 
89  compress = new LZSSCompress();
90  char *buf;
91  Version versionRecord;
92  ViewableHeader viewableHeaderRecord;
93 
94  if (argc < 2) {
95  cerr << "usage: "<< *argv << " <database to step module>\n";
96  exit (-1);
97  }
98 
99  string bookpath = argv[1];
100  string fileName;
101 
102  if ((argv[1][strlen(argv[1])-1] != '/') &&
103  (argv[1][strlen(argv[1])-1] != '\\'))
104  bookpath += "/";
105 
106  fileName = bookpath + "Book.dat";
107  int fd = FileMgr::openFileReadOnly(fileName.c_str());
108 
109  if (fd < 1) {
110  cerr << "error, couldn't open file: " << fileName << "\n";
111  exit (-2);
112  }
113 
114  readVersion(fd, &versionRecord);
116  delete [] buf;
117 
118 
119  fileName = bookpath + "Viewable.idx";
120  int fdv = FileMgr::openFileReadOnly(fileName.c_str());
121 
122  if (fdv < 1) {
123  cerr << "error, couldn't open file: " << fileName << "\n";
124  exit (-3);
125  }
126 
127  readVersion(fdv, &versionRecord);
128  readViewableHeader(fdv, &viewableHeaderRecord);
129 
130  ViewableBlock vb;
131 
132  cout << "\n\nReading special preface viewable BLOCK 0";
133 
134  readViewableBlock(fdv, &vb);
135  readViewableBlockText(fd, &vb, &buf);
136  delete [] buf;
137 
138  int nonGlossBlocksCount = viewableHeaderRecord.viewableBlocksCount
139  - viewableHeaderRecord.glossBlocksCount;
140 
141  cout << "\n\nReading " << nonGlossBlocksCount << " non-glossary viewable blocks";
142  // 1 because we already read the first block above
143  for (int i = 1; i < nonGlossBlocksCount; i++) {
144  cout << "\nNon-Glossary viewable block: " << i;
145  readViewableBlock(fdv, &vb);
146  readViewableBlockText(fd, &vb, &buf);
147  delete [] buf;
148  }
149 
150  cout << "\n\nReading " << viewableHeaderRecord.glossBlocksCount << " glossary viewable blocks";
151  for (int i = 0; i < viewableHeaderRecord.glossBlocksCount; i++) {
152  cout << "\nGlossary viewable block: " << i;
153  readViewableBlock(fdv, &vb);
154  readViewableBlockText(fd, &vb, &buf);
155  delete [] buf;
156  }
157 
158  close(fdv);
159  close(fd);
160 
161 }
162 
163 
164 
165 void readVersion(int fd, Version *versionRecord) {
166 
167  cout << "\n\nReading Version Record (" << 16/*sizeof(struct Version)*/ << " bytes)\n\n";
168 // DO NOT USE BECAUSE OF RECORD BYTE ALIGNMENT PROBLEMS
169 // read(fd, &versionRecord, sizeof(struct Version));
170 
171  cout << "Version Record Information\n";
172  read(fd, &(versionRecord->versionRecordSize), 2);
173  cout << "\tversionRecordSize: " << versionRecord->versionRecordSize << "\n";
174  read(fd, &(versionRecord->publisherID), 2);
175  cout << "\tpublisherID: " << versionRecord->publisherID << "\n";
176  read(fd, &(versionRecord->bookID), 2);
177  cout << "\tbookID: " << versionRecord->bookID << "\n";
178  read(fd, &(versionRecord->setID), 2);
179  cout << "\tsetID: " << versionRecord->setID << "\n";
180  read(fd, &(versionRecord->conversionProgramVerMajor), 1);
181  cout << "\tconversionProgramVerMajor: " << (int)versionRecord->conversionProgramVerMajor << "\n";
182  read(fd, &(versionRecord->conversionProgramVerMinor), 1);
183  cout << "\tconversionProgramVerMinor: " << (int)versionRecord->conversionProgramVerMinor << "\n";
184  read(fd, &(versionRecord->leastCompatSTEPVerMajor), 1);
185  cout << "\tleastCompatSTEPVerMajor: " << (int)versionRecord->leastCompatSTEPVerMajor << "\n";
186  read(fd, &(versionRecord->leastCompatSTEPVerMinor), 1);
187  cout << "\tleastCompatSTEPVerMinor: " << (int)versionRecord->leastCompatSTEPVerMinor << "\n";
188  read(fd, &(versionRecord->encryptionType), 1);
189  cout << "\tencryptionType: " << (int)versionRecord->encryptionType << "\n";
190  read(fd, &(versionRecord->editionID), 1);
191  cout << "\teditionID: " << (int)versionRecord->editionID << "\n";
192  read(fd, &(versionRecord->modifiedBy), 2);
193  cout << "\tmodifiedBy: " << versionRecord->modifiedBy << "\n";
194 
195  int skip = versionRecord->versionRecordSize - 16/*sizeof(struct Version*/;
196 
197  if (skip) {
198  cout << "\nSkipping " << skip << " unknown bytes.\n";
199  char *skipbuf = new char[skip];
200  read(fd, skipbuf, skip);
201  delete [] skipbuf;
202  }
203 }
204 
205 
206 void readViewableHeader(int fd, ViewableHeader *viewableHeaderRecord) {
207 
208  cout << "\n\nReading Viewable Header Record (" << 16/*sizeof(struct ViewableHeader)*/ << " bytes)\n\n";
209 
210 // DO NOT USE BECAUSE OF RECORD BYTE ALIGNMENT PROBLEMS
211 // read(fd, &viewableHeaderRecord, sizeof(struct ViewableHeader));
212 
213  cout << "Viewable Header Record Information\n";
214  read(fd, &(viewableHeaderRecord->viewableHeaderRecordSize), 2);
215  cout << "\tviewableHeaderRecordSize: " << viewableHeaderRecord->viewableHeaderRecordSize << "\n";
216  read(fd, &(viewableHeaderRecord->viewableBlocksCount), 4);
217  cout << "\tviewableBlocksCount: " << viewableHeaderRecord->viewableBlocksCount << "\n";
218  read(fd, &(viewableHeaderRecord->glossBlocksCount), 4);
219  cout << "\tglossBlocksCount: " << viewableHeaderRecord->glossBlocksCount << "\n";
220  read(fd, &(viewableHeaderRecord->compressionType), 1);
221  cout << "\tcompressionType: " << (int)viewableHeaderRecord->compressionType << "(0 - none; 1 - LZSS)\n";
222  read(fd, &(viewableHeaderRecord->reserved1), 1);
223  cout << "\treserved1: " << (int)viewableHeaderRecord->reserved1 << "\n";
224  read(fd, &(viewableHeaderRecord->blockEntriesSize), 2);
225  cout << "\tblockEntriesSize: " << viewableHeaderRecord->blockEntriesSize << "\n";
226  read(fd, &(viewableHeaderRecord->reserved2), 2);
227  cout << "\treserved2: " << viewableHeaderRecord->reserved2 << "\n";
228 
229  int skip = viewableHeaderRecord->viewableHeaderRecordSize - 16/*sizeof(struct ViewableHeader)*/;
230 
231  if (skip) {
232  cout << "\nSkipping " << skip << " unknown bytes.\n";
233  char *skipbuf = new char[skip];
234  read(fd, skipbuf, skip);
235  delete [] skipbuf;
236  }
237 }
238 
239 
240 void readViewableBlockText(int fd, ViewableBlock *vb, char **buf) {
241  unsigned long size = vb->size;
242 
243  *buf = new char [ ((vb->size > vb->uncompressedSize) ? vb->size : vb->uncompressedSize) + 1 ];
244  lseek(fd, vb->offset, SEEK_SET);
245  read(fd, *buf, vb->size);
246 
247  compress->setCompressedBuf(&size, *buf);
248  strcpy(*buf, compress->getUncompressedBuf());
249  cout << "Viewable Block Text:\n";
250  cout << *buf << "\n\n";
251 }
252 
253 
254 void readViewableBlock(int fd, ViewableBlock *vb) {
255 
256  cout << "\n\nReading Viewable Block (" << 12/*sizeof(struct ViewableHeader)*/ << " bytes)\n\n";
257 
258 // DO NOT USE BECAUSE OF RECORD BYTE ALIGNMENT PROBLEMS
259 // read(fd, &vb, sizeof(struct ViewableBlock));
260 
261  cout << "Viewable Block Information\n";
262  read(fd, &(vb->offset), 4);
263  cout << "\toffset: " << vb->offset << "\n";
264  read(fd, &(vb->uncompressedSize), 4);
265  cout << "\tuncompressedSize: " << vb->uncompressedSize << "\n";
266  read(fd, &(vb->size), 4);
267  cout << "\tsize: " << vb->size << "\n";
268 }
269 
270 
271 void readHeaderControlWordAreaText(int fd, char **buf) {
272  long headerControlWordAreaSize;
273  read(fd, &headerControlWordAreaSize, 4);
274  cout << "Reading Header Control Word Area (" << headerControlWordAreaSize << " bytes)\n\n";
275 
276  *buf = new char [headerControlWordAreaSize + 1];
277 
278  read(fd, *buf, headerControlWordAreaSize);
279  (*buf)[headerControlWordAreaSize] = 0;
280 
281  cout << "headerControlWordArea:\n" << *buf << "\n";
282 }
long uncompressedSize
Definition: step2vpl.cpp:99
short versionRecordSize
Definition: step2vpl.cpp:56
void readViewableBlockText(int fd, ViewableBlock *vb, char **buf)
Definition: step2vpl.cpp:312
static int openFileReadOnly(const char *fName)
Definition: filemgr.cpp:474
virtual void setCompressedBuf(unsigned long *len, char *buf=0)
Definition: swcomprs.cpp:101
int main(int argc, char **argv)
Definition: addcomment.cpp:32
SWCompress * compress
Definition: step2vpl.cpp:140
short modifiedBy
Definition: step2vpl.cpp:66
short blockEntriesSize
Definition: step2vpl.cpp:83
short reserved2
Definition: step2vpl.cpp:84
long viewableBlocksCount
Definition: step2vpl.cpp:79
virtual char * getUncompressedBuf(unsigned long *len=0)
Definition: swcomprs.cpp:90
short publisherID
Definition: step2vpl.cpp:57
char leastCompatSTEPVerMajor
Definition: step2vpl.cpp:62
char conversionProgramVerMinor
Definition: step2vpl.cpp:61
char compressionType
Definition: step2vpl.cpp:81
char leastCompatSTEPVerMinor
Definition: step2vpl.cpp:63
#define SEEK_SET
Definition: zconf.h:244
int size
Definition: regex.c:5043
short bookID
Definition: step2vpl.cpp:58
void readViewableBlock(int fd, ViewableBlock *vb)
Definition: step2vpl.cpp:324
void readVersion(int fd, Version *versionRecord)
Definition: step2vpl.cpp:230
long glossBlocksCount
Definition: step2vpl.cpp:80
char conversionProgramVerMajor
Definition: step2vpl.cpp:60
char editionID
Definition: step2vpl.cpp:65
char encryptionType
Definition: step2vpl.cpp:64
void readViewableHeader(int fd, ViewableHeader *viewableHeaderRecord)
Definition: step2vpl.cpp:272
short setID
Definition: step2vpl.cpp:59
short viewableHeaderRecordSize
Definition: step2vpl.cpp:78
void readHeaderControlWordAreaText(int fd, char **buf)
Definition: step2vpl.cpp:332