Diffstat (limited to 'noncore/apps/opie-reader/Aportis.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-reader/Aportis.cpp | 125 |
1 files changed, 103 insertions, 22 deletions
diff --git a/noncore/apps/opie-reader/Aportis.cpp b/noncore/apps/opie-reader/Aportis.cpp index 06f44a4..7528e78 100644 --- a/noncore/apps/opie-reader/Aportis.cpp +++ b/noncore/apps/opie-reader/Aportis.cpp | |||
@@ -1,13 +1,14 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <string.h> | 2 | #include <string.h> |
3 | #include "Aportis.h" | 3 | #include "Aportis.h" |
4 | #include "my_list.h" | 4 | #include "my_list.h" |
5 | #include "Bkmks.h" | 5 | #include "Bkmks.h" |
6 | #include "static.h" | ||
6 | 7 | ||
7 | Aportis::Aportis() : peanutfile(false) { /*printf("constructing:%x\n",fin);*/ } | 8 | Aportis::Aportis() : peanutfile(false) { /*printf("constructing:%x\n",fin);*/ } |
8 | 9 | ||
9 | void Aportis::dePeanut(int& ch) | 10 | void Aportis::dePeanut(int& ch) |
10 | { | 11 | { |
11 | if (peanutfile && ch != EOF) | 12 | if (peanutfile && ch != EOF) |
12 | { | 13 | { |
13 | unsigned char c = ch; | 14 | unsigned char c = ch; |
@@ -23,24 +24,24 @@ CList<Bkmk>* Aportis::getbkmklist() | |||
23 | { | 24 | { |
24 | if (nRecs2 > nRecs) | 25 | if (nRecs2 > nRecs) |
25 | { | 26 | { |
26 | CList<Bkmk>* t = new CList<Bkmk>; | 27 | CList<Bkmk>* t = new CList<Bkmk>; |
27 | for (int i = nRecs; i < nRecs2; i++) | 28 | for (int i = nRecs; i < nRecs2; i++) |
28 | { | 29 | { |
29 | char name[17]; | 30 | char name[17]; |
30 | name[16] = '\0'; | 31 | name[16] = '\0'; |
31 | // odebug << "Record:" << i << ", Length:" << recordlength(i) << "" << oendl; | 32 | // qDebug("Record:%d, Length:%u",i,recordlength(i)); |
32 | gotorecordnumber(i); | 33 | gotorecordnumber(i); |
33 | fread(name,1,16,fin); | 34 | fread(name,1,16,fin); |
34 | unsigned long lcn; | 35 | unsigned long lcn; |
35 | fread(&lcn,sizeof(lcn),1,fin); | 36 | fread(&lcn,sizeof(lcn),1,fin); |
36 | lcn ^= 0xa5a5a5a5; | 37 | lcn ^= 0xa5a5a5a5; |
37 | lcn = SwapLong(lcn); | 38 | lcn = SwapLong(lcn); |
38 | // odebug << "Bookmark:" << name << ":" << lcn << "" << oendl; | 39 | // qDebug("Bookmark:%s:%u", name,lcn); |
39 | tchar tname[17]; | 40 | tchar tname[17]; |
40 | memset(tname, 0, sizeof(tname)); | 41 | memset(tname, 0, sizeof(tname)); |
41 | for (int i = 0; name[i] != 0; i++) | 42 | for (int i = 0; name[i] != 0; i++) |
42 | { | 43 | { |
43 | tname[i] = name[i] ^ 0xa5; | 44 | tname[i] = name[i] ^ 0xa5; |
44 | } | 45 | } |
45 | t->push_back(Bkmk(tname, NULL, lcn)); | 46 | t->push_back(Bkmk(tname, NULL, lcn)); |
46 | } | 47 | } |
@@ -89,44 +90,63 @@ CList<Bkmk>* Aportis::getbkmklist() | |||
89 | fseek(fin, cur, SEEK_SET); | 90 | fseek(fin, cur, SEEK_SET); |
90 | return t; | 91 | return t; |
91 | } | 92 | } |
92 | 93 | ||
93 | int Aportis::OpenFile(const char *src) | 94 | int Aportis::OpenFile(const char *src) |
94 | { | 95 | { |
95 | // printf("In openfile\n"); | 96 | // printf("In openfile\n"); |
96 | int ret = 0; | 97 | int ret = 0; |
98 | html = false; | ||
99 | mobiimagerec = 0; | ||
100 | if (!Cpdb::openpdbfile(src)) return -1; | ||
97 | 101 | ||
98 | if (!Cpdb::openfile(src)) return -1; | 102 | if (head.creator == 0x64414552 // 'dAER' |
99 | 103 | || head.type == 0x74584554) // 'tXET') | |
100 | if (head.creator != 0x64414552 // 'dAER' | ||
101 | || head.type != 0x74584554) // 'tXET') | ||
102 | { | 104 | { |
103 | 105 | } | |
104 | if (memcmp(&head.creator, "PPrs", 4) == 0 && memcmp(&head.type, "PNRd", 4) == 0) | 106 | else if (memcmp(&head.creator, "PPrs", 4) == 0 && memcmp(&head.type, "PNRd", 4) == 0) |
105 | { | 107 | { |
106 | peanutfile = true; | 108 | peanutfile = true; |
107 | } | 109 | } |
108 | else | 110 | else if (memcmp(&head.creator, "MOBI", 4) == 0 && memcmp(&head.type, "BOOK", 4) == 0) |
109 | { | 111 | { |
110 | return -2; | 112 | html = true; |
111 | } | 113 | unsigned char vsn; |
114 | fseek(fin, recordpos(0)+39, SEEK_SET); | ||
115 | fread(&vsn, 1, sizeof(vsn), fin); | ||
116 | qDebug("Mobi version:%x", vsn); | ||
117 | if (vsn > 2) | ||
118 | { | ||
119 | fseek(fin, recordpos(0)+110, SEEK_SET); | ||
120 | fread(&mobiimagerec, 1, sizeof(mobiimagerec), fin); | ||
121 | mobiimagerec = ntohs(mobiimagerec)-1; | ||
122 | } | ||
123 | } | ||
124 | else | ||
125 | { | ||
126 | return -2; | ||
112 | } | 127 | } |
113 | 128 | ||
114 | nRecs2 = nRecs = SwapWord(head.recordList.numRecords) - 1; | 129 | nRecs2 = nRecs = SwapWord(head.recordList.numRecords) - 1; |
115 | fseek(fin,0,SEEK_END); | 130 | |
116 | dwLen = ftell(fin); | 131 | struct stat _stat; |
132 | stat(src,&_stat); | ||
133 | dwLen = _stat.st_size; | ||
134 | |||
135 | //fseek(fin,0,SEEK_END); | ||
136 | //dwLen = ftell(fin); | ||
117 | 137 | ||
118 | if (peanutfile) | 138 | if (peanutfile) |
119 | { | 139 | { |
120 | 140 | ||
121 | PeanutHeader hdr0; | 141 | PeanutHeader hdr0; |
122 | gotorecordnumber(0); | 142 | gotorecordnumber(0); |
123 | fread(&hdr0, sizeof(hdr0), 1, fin); | 143 | fread(&hdr0, sizeof(hdr0), 1, fin); |
124 | // odebug << "Version:" << ntohs(hdr0.Version) << "" << oendl; | 144 | // qDebug("Version:%x", ntohs(hdr0.Version)); |
125 | if (hdr0.Version && 0x0200) | 145 | if (hdr0.Version && 0x0200) |
126 | { | 146 | { |
127 | bCompressed = 2; | 147 | bCompressed = 2; |
128 | } | 148 | } |
129 | else | 149 | else |
130 | { | 150 | { |
131 | bCompressed = 1; | 151 | bCompressed = 1; |
132 | } | 152 | } |
@@ -136,17 +156,18 @@ int Aportis::OpenFile(const char *src) | |||
136 | } | 156 | } |
137 | else | 157 | else |
138 | { | 158 | { |
139 | gotorecordnumber(0); | 159 | gotorecordnumber(0); |
140 | tDocRecord0 hdr0; | 160 | tDocRecord0 hdr0; |
141 | fread(&hdr0, sizeof(hdr0), 1, fin); | 161 | fread(&hdr0, sizeof(hdr0), 1, fin); |
142 | bCompressed = SwapWord(hdr0.wVersion); | 162 | bCompressed = SwapWord(hdr0.wVersion); |
143 | if (bCompressed!=1 && bCompressed!=2 && bCompressed != 4) { | 163 | if (bCompressed!=1 && bCompressed!=2 && bCompressed != 4) { |
144 | ret = bCompressed; | 164 | qDebug("ERROR:Unrecognised compression type in Aportis:%u", bCompressed); |
165 | ret = bCompressed; | ||
145 | bCompressed = 2; | 166 | bCompressed = 2; |
146 | } | 167 | } |
147 | switch (bCompressed) | 168 | switch (bCompressed) |
148 | { | 169 | { |
149 | case 4: | 170 | case 4: |
150 | { | 171 | { |
151 | dwTLen = 0; | 172 | dwTLen = 0; |
152 | int i; | 173 | int i; |
@@ -159,35 +180,68 @@ int Aportis::OpenFile(const char *src) | |||
159 | nRecs = i; | 180 | nRecs = i; |
160 | BlockSize = 0; | 181 | BlockSize = 0; |
161 | } | 182 | } |
162 | break; | 183 | break; |
163 | case 1: | 184 | case 1: |
164 | case 2: | 185 | case 2: |
165 | default: | 186 | default: |
166 | nRecs = SwapWord(hdr0.wNumRecs); | 187 | nRecs = SwapWord(hdr0.wNumRecs); |
188 | if (mobiimagerec == 0 || mobiimagerec > nRecs2) mobiimagerec = nRecs; | ||
167 | dwTLen = SwapLong(hdr0.dwStoryLen); | 189 | dwTLen = SwapLong(hdr0.dwStoryLen); |
168 | BlockSize = SwapWord(hdr0.wRecSize); | 190 | BlockSize = SwapWord(hdr0.wRecSize); |
169 | if (BlockSize == 0) | 191 | if (BlockSize == 0) |
170 | { | 192 | { |
171 | BlockSize = 4096; | 193 | BlockSize = 4096; |
172 | printf("WARNING: Blocksize not set in source file\n"); | 194 | printf("WARNING: Blocksize not set in source file\n"); |
173 | } | 195 | } |
174 | } | 196 | } |
175 | } | 197 | } |
176 | 198 | ||
177 | 199 | qDebug("Mobi image rec:%u", mobiimagerec); | |
178 | 200 | ||
179 | // this is the main record buffer | 201 | // this is the main record buffer |
180 | // it knows how to stretch to accomodate the decompress | 202 | // it knows how to stretch to accomodate the decompress |
181 | currentrec = 0; | 203 | currentrec = 0; |
182 | cbptr = 0; | 204 | cbptr = 0; |
183 | outptr = 0; | 205 | outptr = 0; |
184 | refreshbuffer(); | 206 | refreshbuffer(); |
185 | // odebug << "Number of records:[" << nRecs << "," << nRecs2 << "]" << oendl; | 207 | if (!html) |
208 | { | ||
209 | int c; | ||
210 | char htmltag[] = "<HTML>"; | ||
211 | char *p = htmltag; | ||
212 | while (1) | ||
213 | { | ||
214 | c = getch(); | ||
215 | char ch = *p++; | ||
216 | if (ch == 0) | ||
217 | { | ||
218 | html = true; | ||
219 | break; | ||
220 | } | ||
221 | if (c != ch) | ||
222 | { | ||
223 | html = false; | ||
224 | break; | ||
225 | } | ||
226 | } | ||
227 | currentrec = 0; | ||
228 | cbptr = 0; | ||
229 | outptr = 0; | ||
230 | refreshbuffer(); | ||
231 | } | ||
232 | /* | ||
233 | for (int i = 0; i < nRecs2; i++) | ||
234 | { | ||
235 | qDebug("Record:%u - %u bytes at position %x", i, recordlength(i), recordpos(i)); | ||
236 | } | ||
237 | */ | ||
238 | |||
239 | qDebug("Number of records:[%u,%u]", nRecs, nRecs2); | ||
186 | return ret; | 240 | return ret; |
187 | } | 241 | } |
188 | 242 | ||
189 | int Aportis::getch() | 243 | int Aportis::getch() |
190 | { | 244 | { |
191 | if (bCompressed == 1) | 245 | if (bCompressed == 1) |
192 | { | 246 | { |
193 | if ((dwRecLen == 0) && !refreshbuffer()) return EOF; | 247 | if ((dwRecLen == 0) && !refreshbuffer()) return EOF; |
@@ -367,8 +421,35 @@ bool Aportis::refreshbuffer() | |||
367 | currentpos = 0; | 421 | currentpos = 0; |
368 | currentrec++; | 422 | currentrec++; |
369 | return true; | 423 | return true; |
370 | } | 424 | } |
371 | else { | 425 | else { |
372 | return false; | 426 | return false; |
373 | } | 427 | } |
374 | } | 428 | } |
429 | |||
430 | #include <qimage.h> | ||
431 | |||
432 | QImage* Aportis::getPicture(unsigned long tgt) | ||
433 | { | ||
434 | unsigned short tgtrec = tgt+mobiimagerec; | ||
435 | if (tgtrec > nRecs2) return NULL; | ||
436 | size_t cur = ftell(fin); | ||
437 | unsigned short reclen = recordlength(tgtrec); | ||
438 | gotorecordnumber(tgtrec); | ||
439 | UInt8* imgbuffer = new UInt8[reclen]; | ||
440 | fread(imgbuffer, 1, reclen, fin); | ||
441 | QByteArray arr; | ||
442 | arr.assign((const char*)imgbuffer, reclen); | ||
443 | |||
444 | QImage* qimage = new QImage(arr); | ||
445 | fseek(fin, cur, SEEK_SET); | ||
446 | |||
447 | return qimage; | ||
448 | } | ||
449 | |||
450 | #ifndef __STATIC | ||
451 | extern "C" | ||
452 | { | ||
453 | CExpander* newcodec() { return new Aportis; } | ||
454 | } | ||
455 | #endif | ||