summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/Aportis.cpp
Unidiff
Diffstat (limited to 'noncore/apps/opie-reader/Aportis.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/apps/opie-reader/Aportis.cpp109
1 files changed, 95 insertions, 14 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,11 +1,12 @@
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
7Aportis::Aportis() : peanutfile(false) { /*printf("constructing:%x\n",fin);*/ } 8Aportis::Aportis() : peanutfile(false) { /*printf("constructing:%x\n",fin);*/ }
8 9
9void Aportis::dePeanut(int& ch) 10void Aportis::dePeanut(int& ch)
10{ 11{
11 if (peanutfile && ch != EOF) 12 if (peanutfile && ch != EOF)
@@ -25,20 +26,20 @@ CList<Bkmk>* Aportis::getbkmklist()
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 }
@@ -91,40 +92,59 @@ CList<Bkmk>* Aportis::getbkmklist()
91} 92}
92 93
93int Aportis::OpenFile(const char *src) 94int 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 }
110 else if (memcmp(&head.creator, "MOBI", 4) == 0 && memcmp(&head.type, "BOOK", 4) == 0)
111 {
112 html = true;
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 }
108 else 124 else
109 { 125 {
110 return -2; 126 return -2;
111 } 127 }
112 }
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 {
@@ -138,12 +158,13 @@ int Aportis::OpenFile(const char *src)
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) {
164 qDebug("ERROR:Unrecognised compression type in Aportis:%u", bCompressed);
144 ret = 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:
@@ -161,31 +182,64 @@ int Aportis::OpenFile(const char *src)
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
189int Aportis::getch() 243int Aportis::getch()
190{ 244{
191 if (bCompressed == 1) 245 if (bCompressed == 1)
@@ -369,6 +423,33 @@ bool Aportis::refreshbuffer()
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
432QImage* 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
451extern "C"
452{
453 CExpander* newcodec() { return new Aportis; }
454}
455#endif