summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/Aportis.cpp
blob: 595b38530ffebf4e49e18f63ac74ca38ffb33208 (plain)
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
#include <stdio.h>
#include <string.h>
#include "Aportis.h"

Aportis::Aportis() { /*printf("constructing:%x\n",fin);*/ }

CList<Bkmk>* Aportis::getbkmklist()
{
  if (bCompressed != 4) return NULL;
  CList<Bkmk>* t = new CList<Bkmk>;
  size_t cur = ftell(fin);
  for (int i = 0; i < nRecs2; i++)
    {
      DWORD dwPos;
      fseek(fin, 0x56 + 8*i, SEEK_SET);
      fread(&dwPos, 4, 1, fin);
      dwPos = SwapLong(dwPos);
      fseek(fin,dwPos,SEEK_SET);
      unsigned char ch;
      fread(&ch,1,1,fin);
      if (ch != 241)
      {
	char name[17];
	name[16] = '\0';
	fseek(fin,dwPos,SEEK_SET);
	fread(name,1,16,fin);
	unsigned long lcn;
	fread(&lcn,sizeof(lcn),1,fin);
	lcn = SwapLong(lcn);
#ifdef _UNICODE
	tchar tname[17];
	memset(tname, 0, sizeof(tname));
	for (int i = 0; name[i] != 0; i++)
	{
	    tname[i] = name[i];
	}
	t->push_back(Bkmk(tname,lcn));
#else
	t->push_back(Bkmk(name,lcn));
#endif
      }
    }
  fseek(fin, cur, SEEK_SET);
  return t;
}

int Aportis::openfile(const char *src)
{

  //  printf("In openfile\n");
  int ret = 0;

  if (!Cpdb::openfile(src)) return -1;

  if (head.creator != 0x64414552 //   'dAER'
      || head.type != 0x74584554) // 'tXET')
    {
      return -2;
    }

  gotorecordnumber(0);
  tDocRecord0 hdr0;
  fread(&hdr0, sizeof(hdr0), 1, fin);
  bCompressed = SwapWord(hdr0.wVersion);
  if (bCompressed!=1 && bCompressed!=2 && bCompressed != 4) {
    ret = bCompressed;
    bCompressed = 2;
  }
	
  fseek(fin,0,SEEK_END);
  dwLen = ftell(fin);
  nRecs2 = nRecs = SwapWord(head.recordList.numRecords) - 1;

  switch (bCompressed)
    {
    case 4:
      {
	dwTLen = 0;
	int i;
	for (i = 0; i < nRecs; i++)
	  {
	    unsigned int bs = GetBS(i);
	    if (bs == 0) break;
	    else dwTLen += bs;
	  }
	nRecs = i;
	BlockSize = 0;
      }
      break;
    case 1:
    case 2:
    default:
      dwTLen = SwapLong(hdr0.dwStoryLen);
      BlockSize = SwapWord(hdr0.wRecSize);
      if (BlockSize == 0)
	{
	  BlockSize = 4096;
	  printf("WARNING: Blocksize not set in source file\n");
	}
    }


  // this is the main record buffer
  // it knows how to stretch to accomodate the decompress
  currentrec = 0;
  cbptr = 0;
  outptr = 0;
  refreshbuffer();
  return ret;
}

int Aportis::getch()
{
  if (bCompressed == 1)
    {
      if ((dwRecLen == 0) && !refreshbuffer()) return EOF;
      else
	{
	  int c = getc(fin);
	  dwRecLen--;
	  currentpos++;
	  return c;
	}
    }
  if (outptr != cbptr)
    {
      currentpos++;
      return (circbuf[outptr = (outptr + 1) % 2048]);
    }
  if ((dwRecLen == 0) && !refreshbuffer()) return EOF;
  currentpos++;
  unsigned int c;

  // take a char from the input buffer
  c = getc(fin);
  dwRecLen--;
  // separate the char into zones: 0, 1...8, 9...0x7F, 0x80...0xBF, 0xC0...0xFF

  // codes 1...8 mean copy that many chars; for accented chars & binary
  if (c == 0)
    {
      circbuf[outptr = cbptr = (cbptr+1)%2048] = c;
      return c;
    }
  else if (c >= 0x09 && c <= 0x7F)
    {
      circbuf[outptr = cbptr = (cbptr+1)%2048] = c;
      return c;
    }
  else if (c >= 0x01 && c <= 0x08)
    {
      dwRecLen -= c;
      while(c--)
	{
	  circbuf[cbptr = (cbptr+1)%2048] = getc(fin);
	}
      return circbuf[outptr = (outptr+1)%2048];
    }
  else if (c >= 0x80 && c <= 0xBF)
    {
      int m,n;
      c <<= 8;
      c += getc(fin);
      dwRecLen--;
      m = (c & 0x3FFF) >> COUNT_BITS;
      n = c & ((1<<COUNT_BITS) - 1);
      n += 3;
      while (n--)
	{
	  cbptr = (cbptr+1)%2048;
	  circbuf[cbptr] = circbuf[(cbptr+2048-m)%2048];
	}
      return circbuf[outptr = (outptr+1)%2048];
    }
  else if (c >= 0xC0 && c <= 0xFF)
    {
      circbuf[cbptr = (cbptr+1)%2048] = ' ';
      circbuf[cbptr = (cbptr+1)%2048] = c^0x80;
      return circbuf[outptr = (outptr+1)%2048];
    }
}

unsigned int Aportis::GetBS(unsigned int bn)
{
  DWORD dwPos;
  WORD fs;

  fseek(fin, 0x56 + 8*bn, SEEK_SET);
  fread(&dwPos, 4, 1, fin);
  dwPos = SwapLong(dwPos);
  fseek(fin,dwPos,SEEK_SET);

//  gotorecordnumber(bn+1);
  unsigned char ch;
  fread(&ch,1,1,fin);
  if (ch == 241)
    {
      fread(&fs,sizeof(fs),1,fin);
      fs = SwapWord(fs);
    }
  else
    fs = 0;
  return fs;
}

unsigned int Aportis::locate()
{
  if (bCompressed == 4)
    {
      size_t cur = ftell(fin);
      unsigned int clen = 0;
      for (unsigned int i = 0; i < currentrec-1; i++)
	{
	  unsigned int bs = GetBS(i);
	  if (bs == 0) break;
	  clen += bs;
	}
      fseek(fin,cur,SEEK_SET);
      return clen+currentpos;
    }
  else
    return (currentrec-1)*BlockSize+currentpos;
}

void Aportis::locate(unsigned int n)
{
  unsigned int offset;
  //  currentrec = (n >> OFFBITS);
  switch (bCompressed)
    {
    case 4:
      {
	DWORD clen = 0;
	offset = n;
	unsigned int i;
	for (i = 0; i < nRecs; i++)
	  {
	    unsigned int bs = GetBS(i);
	    if (bs == 0) break;
	    clen += bs;
	    if (clen > n) break;
	    offset = n - clen;
	  }
	currentrec = i;
      }
      break;
    case 1:
    case 2:
    default:
      currentrec = n / BlockSize;
      offset = n % BlockSize;
    }

  outptr = cbptr;
  refreshbuffer();
  while (currentpos < offset && getch() != EOF);
}

bool Aportis::refreshbuffer()
{
  if (currentrec < nRecs)
    {
	dwRecLen = recordlength(currentrec+1);
	gotorecordnumber(currentrec+1);
      if (bCompressed == 4)
	{
	  unsigned char t[3];
	  fread(t,1,3,fin);
	  if (t[0] != 241)
	    {
	      printf("You shouldn't be here!\n");
	      return false;
	    }
	  dwRecLen -= 3;
	}
      /*
	int n = fread(t.buf, 1, dwRecLen, fin);
	t.len = n;
	//		if(bCompressed)
	t.Decompress();

	t.buf[t.Len()] = '\0';
      */
      currentpos = 0;
      currentrec++;
      return true;
    }
  else {
    return false;
  }
}