1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * mg_fast_comp_dict.cpp -- Program to generate a fast compression dictionary
|
---|
4 | * Copyright (C) 1994 Neil Sharman
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: mg_fast_comp_dict.cpp 860 2000-01-18 03:53:24Z rjmcnab $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 |
|
---|
25 | /*
|
---|
26 | $Log$
|
---|
27 | Revision 1.2 2000/01/18 03:53:23 rjmcnab
|
---|
28 | Fixed a couple of bugs and made building silent if needed.
|
---|
29 |
|
---|
30 | Revision 1.1 2000/01/14 02:26:13 sjboddie
|
---|
31 | Rodgers new C++ mg
|
---|
32 |
|
---|
33 | Revision 1.2 1999/10/17 23:43:26 cs025
|
---|
34 | Changes to eradicate Xmalloc
|
---|
35 |
|
---|
36 | Revision 1.1 1999/10/11 02:57:50 cs025
|
---|
37 | Base install of MG-PP
|
---|
38 |
|
---|
39 | Revision 1.1 1999/08/10 21:18:06 sjboddie
|
---|
40 | renamed mg-1.3d directory mg
|
---|
41 |
|
---|
42 | Revision 1.2 1998/11/25 07:55:44 rjmcnab
|
---|
43 |
|
---|
44 | Modified mg to that you can specify the stemmer you want
|
---|
45 | to use via a command line option. You specify it to
|
---|
46 | mg_passes during the build process. The number of the
|
---|
47 | stemmer that you used is stored within the inverted
|
---|
48 | dictionary header and the stemmed dictionary header so
|
---|
49 | the correct stemmer is used in later stages of building
|
---|
50 | and querying.
|
---|
51 |
|
---|
52 | Revision 1.1 1998/11/17 09:34:57 rjmcnab
|
---|
53 | *** empty log message ***
|
---|
54 |
|
---|
55 | * Revision 1.3 1994/10/20 03:56:55 tes
|
---|
56 | * I have rewritten the boolean query optimiser and abstracted out the
|
---|
57 | * components of the boolean query.
|
---|
58 | *
|
---|
59 | * Revision 1.2 1994/09/20 04:41:47 tes
|
---|
60 | * For version 1.1
|
---|
61 | *
|
---|
62 | */
|
---|
63 |
|
---|
64 | #include "sysfuncs.h"
|
---|
65 |
|
---|
66 | #include "huffman.h"
|
---|
67 | #include "messages.h"
|
---|
68 | #include "memlib.h"
|
---|
69 | #include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
|
---|
70 |
|
---|
71 | #include "local_strings.h"
|
---|
72 | #include "mg.h"
|
---|
73 | #include "text.h"
|
---|
74 | #include "invf.h"
|
---|
75 | #include "mg_files.h"
|
---|
76 | #include "locallib.h"
|
---|
77 | #include "words.h"
|
---|
78 |
|
---|
79 |
|
---|
80 |
|
---|
81 | #define ALIGN_SIZE(p,t) (((p) + (sizeof(t)-1)) & (~(sizeof(t)-1)))
|
---|
82 |
|
---|
83 | #define WORDNO(p, base) ((((char*)(p))-((char*)(base)))/sizeof(u_char*))
|
---|
84 | #define FIXUP(p) (fixup[WORDNO(p,buffer)/8] |= (1<<(WORDNO(p, buffer) & 7)))
|
---|
85 |
|
---|
86 | #define IS_FIXUP(p) ((fixup[WORDNO(p,buffer)/8] & (1<<(WORDNO(p, buffer) & 7))) != 0)
|
---|
87 |
|
---|
88 | #define FIXUP_VALS(vals) do { \
|
---|
89 | int i; \
|
---|
90 | for (i=0; i < MAX_HUFFCODE_LEN+1; i++) \
|
---|
91 | FIXUP(&vals[i]); \
|
---|
92 | } while(0)
|
---|
93 |
|
---|
94 |
|
---|
95 |
|
---|
96 | static u_long mem_for_comp_dict (char *filename);
|
---|
97 | static void load_comp_dict (char *filename);
|
---|
98 | static void save_fast_dict (char *filename);
|
---|
99 | static void unfixup_buffer (void);
|
---|
100 |
|
---|
101 |
|
---|
102 | static void *buffer;
|
---|
103 | static void *cur;
|
---|
104 | static u_char *fixup;
|
---|
105 | static u_long mem, fixup_mem;
|
---|
106 |
|
---|
107 | int main (int argc, char **argv)
|
---|
108 | {
|
---|
109 | int ch;
|
---|
110 | char *filename = "";
|
---|
111 | opterr = 0;
|
---|
112 | msg_prefix = argv[0];
|
---|
113 | while ((ch = getopt (argc, argv, "f:d:h")) != -1)
|
---|
114 | switch (ch)
|
---|
115 | {
|
---|
116 | case 'f': /* input file */
|
---|
117 | filename = optarg;
|
---|
118 | break;
|
---|
119 | case 'd':
|
---|
120 | set_basepath (optarg);
|
---|
121 | break;
|
---|
122 | case 'h':
|
---|
123 | case '?':
|
---|
124 | fprintf (stderr, "usage: %s [-f input_file] [-d data directory] [-h]\n",
|
---|
125 | argv[0]);
|
---|
126 | exit (1);
|
---|
127 | }
|
---|
128 |
|
---|
129 | mem = mem_for_comp_dict (filename);
|
---|
130 |
|
---|
131 | fixup_mem = (ALIGN_SIZE (mem, u_char *) / sizeof (u_char *) + 7) / 8;
|
---|
132 |
|
---|
133 | cur = buffer = Xmalloc (mem);
|
---|
134 | bzero (buffer, mem);
|
---|
135 | fixup = (u_char *) Xmalloc (fixup_mem);
|
---|
136 | bzero (fixup, fixup_mem);
|
---|
137 |
|
---|
138 | #ifndef SILENT
|
---|
139 | Message ("Estimated memory for fast_dict %u", mem);
|
---|
140 | Message ("Estimated memory for fixups %u", fixup_mem);
|
---|
141 | #endif
|
---|
142 |
|
---|
143 | load_comp_dict (filename);
|
---|
144 |
|
---|
145 | #ifndef SILENT
|
---|
146 | Message ("Actual memory for fast_dict %u", (char *) cur - (char *) buffer);
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | if ((u_long) cur > (u_long) buffer + mem)
|
---|
150 | FatalError (1, "The buffer was not big enough for the dictionary");
|
---|
151 |
|
---|
152 | {
|
---|
153 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
154 | compression_dict *cd = (compression_dict *) buffer;
|
---|
155 | int i, which;
|
---|
156 |
|
---|
157 | /* cfh */
|
---|
158 | for (which = 0; which <= 1; which++)
|
---|
159 | {
|
---|
160 | int j;
|
---|
161 |
|
---|
162 | HTONSI(cd->cfh[which]->hd.num_codes);
|
---|
163 | HTONSI(cd->cfh[which]->hd.mincodelen);
|
---|
164 | HTONSI(cd->cfh[which]->hd.maxcodelen);
|
---|
165 | for (j = 0; j < MAX_HUFFCODE_LEN + 1; j++)
|
---|
166 | {
|
---|
167 | HTONSI(cd->cfh[which]->hd.lencount[j]);
|
---|
168 | HTONUL(cd->cfh[which]->hd.min_code[j]);
|
---|
169 | }
|
---|
170 | HTONUL(cd->cfh[which]->uncompressed_size);
|
---|
171 | for (j = 0; j < MAX_HUFFCODE_LEN + 1; j++)
|
---|
172 | HTONUL(cd->cfh[which]->huff_words_size[j]);
|
---|
173 | }
|
---|
174 | HTONUL(cd->MemForCompDict);
|
---|
175 | /* ad */
|
---|
176 | if (cd->cdh.novel_method == MG_NOVEL_DELTA ||
|
---|
177 | cd->cdh.novel_method == MG_NOVEL_HYBRID)
|
---|
178 | for (which = 0; which <= 1; which++)
|
---|
179 | {
|
---|
180 | int j;
|
---|
181 |
|
---|
182 | HTONUL(cd->ad->afh[which].num_frags);
|
---|
183 | HTONUL(cd->ad->afh[which].mem_for_frags);
|
---|
184 | for (j = 0; j < 33; j++)
|
---|
185 | {
|
---|
186 | HTONSI(cd->ad->blk_start[which][j]);
|
---|
187 | HTONSI(cd->ad->blk_end[which][j]);
|
---|
188 | }
|
---|
189 | }
|
---|
190 | /* cdh */
|
---|
191 | HTONUL(cd->cdh.dict_type);
|
---|
192 | HTONUL(cd->cdh.novel_method);
|
---|
193 | for (i = 0; i < TEXT_PARAMS; i++)
|
---|
194 | HTONUL(cd->cdh.params[which]);
|
---|
195 | HTONUL(cd->cdh.num_words[0]);
|
---|
196 | HTONUL(cd->cdh.num_words[1]);
|
---|
197 | HTONUL(cd->cdh.num_word_chars[0]);
|
---|
198 | HTONUL(cd->cdh.num_word_chars[1]);
|
---|
199 | HTONUL(cd->cdh.lookback);
|
---|
200 | HTONSI(cd->fast_loaded);
|
---|
201 | }
|
---|
202 |
|
---|
203 | unfixup_buffer ();
|
---|
204 |
|
---|
205 | save_fast_dict (filename);
|
---|
206 |
|
---|
207 | exit (0);
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 |
|
---|
212 | static void
|
---|
213 | unfixup_buffer ()
|
---|
214 | {
|
---|
215 | u_long *p;
|
---|
216 | for (p = (u_long *) buffer; (u_long) p < (u_long) cur; p++)
|
---|
217 | {
|
---|
218 | if (IS_FIXUP (p))
|
---|
219 | *p = *p - (u_long) buffer;
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 |
|
---|
225 |
|
---|
226 | static u_long
|
---|
227 | mem_for_aux_dict (compression_dict_header * /*cdh*/, char *filename)
|
---|
228 | {
|
---|
229 | int i;
|
---|
230 | u_long mem = sizeof (auxiliary_dict);
|
---|
231 | FILE *aux;
|
---|
232 |
|
---|
233 | aux = open_file (filename, TEXT_DICT_AUX_SUFFIX, "rb",
|
---|
234 | MAGIC_AUX_DICT, MG_ABORT); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
235 |
|
---|
236 | for (i = 0; i <= 1; i++)
|
---|
237 | {
|
---|
238 | aux_frags_header afh;
|
---|
239 | fread (&afh, sizeof (afh), 1, aux);
|
---|
240 | NTOHUL(afh.num_frags); /* [RPAP - Jan 97: Endian Ordering] */
|
---|
241 | NTOHUL(afh.mem_for_frags); /* [RPAP - Jan 97: Endian Ordering] */
|
---|
242 | mem += afh.num_frags * sizeof (u_char *);
|
---|
243 | mem = ALIGN_SIZE (mem + afh.mem_for_frags, u_char *);
|
---|
244 | fseek (aux, afh.mem_for_frags, SEEK_CUR);
|
---|
245 | }
|
---|
246 | fclose (aux);
|
---|
247 |
|
---|
248 | return mem;
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 |
|
---|
253 | static u_long
|
---|
254 | mem_for_words (FILE * dict, compression_dict_header * cdh,
|
---|
255 | comp_frags_header * cfh)
|
---|
256 | {
|
---|
257 | u_long mem = 0;
|
---|
258 | long i, lookback;
|
---|
259 | int ptrs_reqd = 0;
|
---|
260 | int mem_reqd = 0;
|
---|
261 |
|
---|
262 | lookback = cdh->lookback;
|
---|
263 |
|
---|
264 | for (i = cfh->hd.mincodelen; i <= cfh->hd.maxcodelen; i++)
|
---|
265 | {
|
---|
266 | ptrs_reqd += (cfh->hd.lencount[i] + ((1 << lookback) - 1)) >> lookback;
|
---|
267 | mem_reqd += cfh->huff_words_size[i];
|
---|
268 | }
|
---|
269 |
|
---|
270 | mem += ptrs_reqd * sizeof (u_char *);
|
---|
271 | mem += (MAX_HUFFCODE_LEN + 1) * sizeof (u_char **);
|
---|
272 | mem += mem_reqd;
|
---|
273 |
|
---|
274 | for (i = 0; i < cfh->hd.num_codes; i++)
|
---|
275 | {
|
---|
276 | register int val;
|
---|
277 | for (val = getc (dict) & 0xf; val; val--)
|
---|
278 | getc (dict);
|
---|
279 | }
|
---|
280 | return ALIGN_SIZE (mem, u_char *);
|
---|
281 | }
|
---|
282 |
|
---|
283 | static u_long mem_skip_hd(FILE *dict, u_long mem)
|
---|
284 | { huff_data hd;
|
---|
285 |
|
---|
286 | mem += sizeof (hd);
|
---|
287 | Read_Huffman_Data (dict, &hd, NULL, NULL);
|
---|
288 | if (hd.clens)
|
---|
289 | delete hd.clens;
|
---|
290 | mem += hd.num_codes * sizeof (unsigned long);
|
---|
291 | mem += (MAX_HUFFCODE_LEN + 1) * sizeof (unsigned long *);
|
---|
292 | return mem;
|
---|
293 | }
|
---|
294 |
|
---|
295 | static u_long mem_read_cfh(FILE *dict, compression_dict_header *cdh, comp_frags_header *cfh,
|
---|
296 | u_long mem)
|
---|
297 | {
|
---|
298 | mem += sizeof (comp_frags_header);
|
---|
299 | Read_cfh (dict, cfh, NULL, NULL);
|
---|
300 |
|
---|
301 | /* Don't need to count the space for the clens of the huffman data */
|
---|
302 |
|
---|
303 | mem += mem_for_words (dict, cdh, cfh);
|
---|
304 | if (cfh->hd.clens)
|
---|
305 | delete cfh->hd.clens;
|
---|
306 |
|
---|
307 | return mem;
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 | static u_long
|
---|
312 | mem_for_comp_dict (char *filename)
|
---|
313 | {
|
---|
314 | u_long mem = sizeof (compression_dict);
|
---|
315 | compression_dict_header cdh;
|
---|
316 | comp_frags_header cfh;
|
---|
317 | FILE *dict;
|
---|
318 | int which;
|
---|
319 | int i; /* [RPAP - Jan 97: Endian Ordering] */
|
---|
320 |
|
---|
321 | dict = open_file (filename, TEXT_DICT_SUFFIX, "rb",
|
---|
322 | MAGIC_DICT, MG_ABORT); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
323 |
|
---|
324 | fread (&cdh, sizeof (cdh), 1, dict);
|
---|
325 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
326 | NTOHUL(cdh.dict_type);
|
---|
327 | NTOHUL(cdh.novel_method);
|
---|
328 | for (i = 0; i < TEXT_PARAMS; i++)
|
---|
329 | NTOHUL(cdh.params[i]);
|
---|
330 | NTOHUL(cdh.num_words[0]);
|
---|
331 | NTOHUL(cdh.num_words[1]);
|
---|
332 | NTOHUL(cdh.num_word_chars[0]);
|
---|
333 | NTOHUL(cdh.num_word_chars[1]);
|
---|
334 | NTOHUL(cdh.lookback);
|
---|
335 |
|
---|
336 | for (which = 0; which < 2; which++)
|
---|
337 | switch (cdh.dict_type)
|
---|
338 | {
|
---|
339 | case MG_COMPLETE_DICTIONARY:
|
---|
340 | {
|
---|
341 | mem = mem_read_cfh(dict, &cdh, &cfh, mem);
|
---|
342 | }
|
---|
343 | break;
|
---|
344 | case MG_PARTIAL_DICTIONARY:
|
---|
345 | {
|
---|
346 | // huff_data hd;
|
---|
347 | if (cdh.num_words[which])
|
---|
348 | {
|
---|
349 | mem = mem_read_cfh(dict, &cdh, &cfh, mem);
|
---|
350 | }
|
---|
351 |
|
---|
352 | mem = mem_skip_hd(dict, mem);
|
---|
353 | mem = mem_skip_hd(dict, mem);
|
---|
354 | }
|
---|
355 | break;
|
---|
356 | case MG_SEED_DICTIONARY:
|
---|
357 | {
|
---|
358 | // huff_data hd;
|
---|
359 | if (cdh.num_words[which])
|
---|
360 | {
|
---|
361 | mem = mem_read_cfh(dict, &cdh, &cfh, mem);
|
---|
362 | }
|
---|
363 | switch (cdh.novel_method)
|
---|
364 | {
|
---|
365 | case MG_NOVEL_HUFFMAN_CHARS:
|
---|
366 | mem = mem_skip_hd(dict, mem);
|
---|
367 | mem = mem_skip_hd(dict, mem);
|
---|
368 | break;
|
---|
369 | case MG_NOVEL_DELTA:
|
---|
370 | break;
|
---|
371 | case MG_NOVEL_HYBRID:
|
---|
372 | break;
|
---|
373 | }
|
---|
374 | break;
|
---|
375 | }
|
---|
376 | }
|
---|
377 | fclose (dict);
|
---|
378 |
|
---|
379 | if (cdh.novel_method == MG_NOVEL_DELTA ||
|
---|
380 | cdh.novel_method == MG_NOVEL_HYBRID)
|
---|
381 | mem += mem_for_aux_dict (&cdh, filename);
|
---|
382 |
|
---|
383 | return ALIGN_SIZE (mem, u_char *);
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 |
|
---|
388 | void *
|
---|
389 | getmem (u_long size, int align)
|
---|
390 | {
|
---|
391 | void *res;
|
---|
392 | cur = (void *) (((u_long) cur + (align - 1)) & (~(align - 1)));
|
---|
393 | res = cur;
|
---|
394 | cur = (char *) cur + size;
|
---|
395 | if ((u_long) cur > (u_long)buffer + mem)
|
---|
396 | FatalError (1, "The buffer was not big enough for the dictionary");
|
---|
397 | return res;
|
---|
398 | }
|
---|
399 |
|
---|
400 |
|
---|
401 |
|
---|
402 |
|
---|
403 |
|
---|
404 |
|
---|
405 |
|
---|
406 |
|
---|
407 |
|
---|
408 |
|
---|
409 |
|
---|
410 | static auxiliary_dict *
|
---|
411 | LoadAuxDict (compression_dict_header * cdh,
|
---|
412 | char *filename)
|
---|
413 | {
|
---|
414 | auxiliary_dict *ad;
|
---|
415 | int i;
|
---|
416 | FILE *aux;
|
---|
417 |
|
---|
418 | aux = open_file (filename, TEXT_DICT_AUX_SUFFIX, "rb",
|
---|
419 | MAGIC_AUX_DICT, MG_ABORT); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
420 |
|
---|
421 | ad = (auxiliary_dict *) getmem (sizeof (auxiliary_dict), sizeof (u_char *));
|
---|
422 |
|
---|
423 | for (i = 0; i <= 1; i++)
|
---|
424 | {
|
---|
425 | unsigned int j;
|
---|
426 | u_char *pos;
|
---|
427 |
|
---|
428 | fread (&ad->afh[i], sizeof (aux_frags_header), 1, aux);
|
---|
429 |
|
---|
430 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
431 | NTOHUL(ad->afh[i].num_frags);
|
---|
432 | NTOHUL(ad->afh[i].mem_for_frags);
|
---|
433 |
|
---|
434 | ad->word_data[i] = (u_char *) getmem (ad->afh[i].mem_for_frags, 1);
|
---|
435 | FIXUP (&ad->word_data[i]);
|
---|
436 |
|
---|
437 | ad->words[i] = (u_char **) getmem (ad->afh[i].num_frags * sizeof (u_char *),
|
---|
438 | sizeof (u_char *));
|
---|
439 | FIXUP (&ad->words[i]);
|
---|
440 |
|
---|
441 | fread (ad->word_data[i], ad->afh[i].mem_for_frags, sizeof (u_char), aux);
|
---|
442 |
|
---|
443 | pos = ad->word_data[i];
|
---|
444 | for (j = 0; j < ad->afh[i].num_frags; j++)
|
---|
445 | {
|
---|
446 | ad->words[i][j] = pos;
|
---|
447 | FIXUP (&ad->words[i][j]);
|
---|
448 | pos += *pos + 1;
|
---|
449 | }
|
---|
450 | if (cdh->novel_method == MG_NOVEL_HYBRID)
|
---|
451 | {
|
---|
452 | int num;
|
---|
453 | num = 1;
|
---|
454 | ad->blk_start[i][0] = 0;
|
---|
455 | ad->blk_end[i][0] = cdh->num_words[i] - 1;
|
---|
456 | while (num < 33)
|
---|
457 | {
|
---|
458 | ad->blk_start[i][num] = ad->blk_end[i][num - 1] + 1;
|
---|
459 | ad->blk_end[i][num] = ad->blk_start[i][num] +
|
---|
460 | (ad->blk_end[i][num - 1] - ad->blk_start[i][num - 1]) * 2;
|
---|
461 | num++;
|
---|
462 | }
|
---|
463 | }
|
---|
464 | }
|
---|
465 | fclose (aux);
|
---|
466 | return (ad);
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 |
|
---|
471 |
|
---|
472 |
|
---|
473 | static u_char ***
|
---|
474 | ReadInWords (FILE * dict, compression_dict * cd,
|
---|
475 | comp_frags_header * cfh, u_char ** escape)
|
---|
476 | {
|
---|
477 | int i, lookback;
|
---|
478 | int ptrs_reqd = 0;
|
---|
479 | int mem_reqd = 0;
|
---|
480 | int num_set[MAX_HUFFCODE_LEN + 1];
|
---|
481 | u_char *next_word[MAX_HUFFCODE_LEN + 1];
|
---|
482 | u_char **vals;
|
---|
483 | u_char ***values;
|
---|
484 | u_char word[MAXWORDLEN + 1];
|
---|
485 | u_char last_word[MAX_HUFFCODE_LEN + 1][MAXWORDLEN + 1];
|
---|
486 |
|
---|
487 | lookback = cd->cdh.lookback;
|
---|
488 |
|
---|
489 | for (i = cfh->hd.mincodelen; i <= cfh->hd.maxcodelen; i++)
|
---|
490 | {
|
---|
491 | ptrs_reqd += (cfh->hd.lencount[i] + ((1 << lookback) - 1)) >> lookback;
|
---|
492 | mem_reqd += cfh->huff_words_size[i];
|
---|
493 | }
|
---|
494 |
|
---|
495 | vals = (u_char **) getmem (ptrs_reqd * sizeof (*vals), sizeof (u_char *));
|
---|
496 |
|
---|
497 | values = (u_char ***) getmem ((MAX_HUFFCODE_LEN + 1) * sizeof (u_char **), sizeof (u_char **));
|
---|
498 |
|
---|
499 | next_word[0] = (u_char *) getmem (mem_reqd, sizeof (char));
|
---|
500 |
|
---|
501 | cd->MemForCompDict += ptrs_reqd * sizeof (*vals) +
|
---|
502 | (MAX_HUFFCODE_LEN + 1) * sizeof (u_char **) +
|
---|
503 | mem_reqd;
|
---|
504 |
|
---|
505 | values[0] = vals;
|
---|
506 | FIXUP (&values[0]);
|
---|
507 | values[0][0] = next_word[0];
|
---|
508 | FIXUP (&values[0][0]);
|
---|
509 | for (i = 1; i <= cfh->hd.maxcodelen; i++)
|
---|
510 | {
|
---|
511 | int next_start = (values[i - 1] - vals) +
|
---|
512 | ((cfh->hd.lencount[i - 1] + ((1 << lookback) - 1)) >> lookback);
|
---|
513 | values[i] = &vals[next_start];
|
---|
514 | FIXUP (&values[i]);
|
---|
515 | next_word[i] = next_word[i - 1] + cfh->huff_words_size[i - 1];
|
---|
516 | values[i][0] = next_word[i];
|
---|
517 | FIXUP (&values[i][0]);
|
---|
518 | }
|
---|
519 |
|
---|
520 | bzero ((char *) num_set, sizeof (num_set));
|
---|
521 |
|
---|
522 | for (i = 0; i < cfh->hd.num_codes; i++)
|
---|
523 | {
|
---|
524 | register int val, copy;
|
---|
525 | register int len = cfh->hd.clens[i];
|
---|
526 | val = getc (dict);
|
---|
527 | copy = (val >> 4) & 0xf;
|
---|
528 | val &= 0xf;
|
---|
529 |
|
---|
530 | fread (word + copy + 1, sizeof (u_char), val, dict);
|
---|
531 | *word = val + copy;
|
---|
532 |
|
---|
533 | if ((num_set[len] & ((1 << lookback) - 1)) == 0)
|
---|
534 | {
|
---|
535 | values[len][num_set[len] >> lookback] = next_word[len];
|
---|
536 | FIXUP (&values[len][num_set[len] >> lookback]);
|
---|
537 | memcpy (next_word[len], word, *word + 1);
|
---|
538 | if (escape && i == cfh->hd.num_codes - 1)
|
---|
539 | {
|
---|
540 | *escape = next_word[len];
|
---|
541 | FIXUP (escape);
|
---|
542 | }
|
---|
543 | next_word[len] += *word + 1;
|
---|
544 | }
|
---|
545 | else
|
---|
546 | {
|
---|
547 | copy = prefixlen (last_word[len], word);
|
---|
548 | memcpy (next_word[len] + 1, word + copy + 1, *word - copy);
|
---|
549 | *next_word[len] = (copy << 4) + (*word - copy);
|
---|
550 | if (escape && i == cfh->hd.num_codes - 1)
|
---|
551 | {
|
---|
552 | *escape = next_word[len];
|
---|
553 | FIXUP (escape);
|
---|
554 | }
|
---|
555 | next_word[len] += (*word - copy) + 1;
|
---|
556 | }
|
---|
557 | memcpy (last_word[len], word, *word + 1);
|
---|
558 | num_set[len]++;
|
---|
559 | }
|
---|
560 | if (cfh->hd.clens)
|
---|
561 | delete cfh->hd.clens;
|
---|
562 | cfh->hd.clens = NULL;
|
---|
563 | return values;
|
---|
564 | }
|
---|
565 |
|
---|
566 |
|
---|
567 | static unsigned long **
|
---|
568 | Generate_Fast_Huffman_Vals (huff_data * data, u_long * mem)
|
---|
569 | {
|
---|
570 | int i;
|
---|
571 | unsigned long *fcode[MAX_HUFFCODE_LEN + 1];
|
---|
572 | unsigned long **values;
|
---|
573 | unsigned long *vals;
|
---|
574 |
|
---|
575 | if (!data)
|
---|
576 | return (NULL);
|
---|
577 | vals = (unsigned long *) getmem (data->num_codes * sizeof (*vals), sizeof (long *));
|
---|
578 | values = (unsigned long **) getmem ((MAX_HUFFCODE_LEN + 1) * sizeof (unsigned long *),
|
---|
579 | sizeof (long *));
|
---|
580 |
|
---|
581 | bzero ((char *) values, (MAX_HUFFCODE_LEN + 1) * sizeof (unsigned long *));
|
---|
582 |
|
---|
583 | if (mem)
|
---|
584 | *mem += data->num_codes * sizeof (*vals) +
|
---|
585 | (MAX_HUFFCODE_LEN + 1) * sizeof (unsigned long *);
|
---|
586 |
|
---|
587 | fcode[0] = values[0] = &vals[0];
|
---|
588 | FIXUP (&values[0]);
|
---|
589 | for (i = 1; i <= data->maxcodelen; i++)
|
---|
590 | {
|
---|
591 | fcode[i] = values[i] = &vals[(values[i - 1] - vals) + data->lencount[i - 1]];
|
---|
592 | FIXUP (&values[i]);
|
---|
593 | }
|
---|
594 |
|
---|
595 | for (i = 0; i < data->num_codes; i++)
|
---|
596 | if (data->clens[i])
|
---|
597 | *fcode[(int) (data->clens[i])]++ = i;
|
---|
598 | return (values);
|
---|
599 | }
|
---|
600 |
|
---|
601 |
|
---|
602 | static void load_read_huffman(FILE *dict, compression_dict *cd, int which)
|
---|
603 | {
|
---|
604 | huff_data *hd;
|
---|
605 | u_long **vals;
|
---|
606 |
|
---|
607 | hd = (huff_data *) getmem (sizeof (huff_data), sizeof (char *));
|
---|
608 | cd->MemForCompDict += sizeof (huff_data);
|
---|
609 | Read_Huffman_Data (dict, hd, &cd->MemForCompDict, NULL);
|
---|
610 | vals = Generate_Fast_Huffman_Vals (hd, &cd->MemForCompDict);
|
---|
611 | cd->chars_huff[which] = hd;
|
---|
612 | FIXUP (&cd->chars_huff[which]);
|
---|
613 | cd->chars_vals[which] = vals;
|
---|
614 | FIXUP (&cd->chars_vals[which]);
|
---|
615 | if (hd->clens)
|
---|
616 | delete hd->clens;
|
---|
617 | hd->clens = NULL;
|
---|
618 | }
|
---|
619 |
|
---|
620 | static void load_read_words(FILE *dict, compression_dict *cd, int which, int fixescape)
|
---|
621 | {
|
---|
622 | cd->cfh[which] = (comp_frags_header *) getmem (sizeof (*cd->cfh[which]), sizeof (u_char *));
|
---|
623 | cd->MemForCompDict += sizeof (*cd->cfh[which]);
|
---|
624 | Read_cfh (dict, cd->cfh[which], &cd->MemForCompDict, NULL);
|
---|
625 |
|
---|
626 | cd->values[which] = ReadInWords (dict, cd, cd->cfh[which], NULL);
|
---|
627 | FIXUP (&cd->cfh[which]);
|
---|
628 | FIXUP (&cd->values[which]);
|
---|
629 | if (fixescape)
|
---|
630 | { FIXUP(&cd->escape[which]);
|
---|
631 | }
|
---|
632 | else
|
---|
633 | {
|
---|
634 | cd->escape[which] = NULL;
|
---|
635 | }
|
---|
636 | }
|
---|
637 |
|
---|
638 | static void
|
---|
639 | load_comp_dict (char *filename)
|
---|
640 | {
|
---|
641 | FILE *dict;
|
---|
642 | int which;
|
---|
643 | compression_dict *cd;
|
---|
644 |
|
---|
645 | cd = (compression_dict *) getmem (sizeof (compression_dict), sizeof (u_char *));
|
---|
646 | cd->fast_loaded = 1;
|
---|
647 |
|
---|
648 | dict = open_file (filename, TEXT_DICT_SUFFIX, "rb",
|
---|
649 | MAGIC_DICT, MG_ABORT); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
650 |
|
---|
651 | Read_cdh (dict, &cd->cdh, NULL, NULL);
|
---|
652 |
|
---|
653 | for (which = 0; which < 2; which++)
|
---|
654 | switch (cd->cdh.dict_type)
|
---|
655 | {
|
---|
656 | case MG_COMPLETE_DICTIONARY:
|
---|
657 | {
|
---|
658 | load_read_words(dict, cd, which, 0);
|
---|
659 | }
|
---|
660 | break;
|
---|
661 | case MG_PARTIAL_DICTIONARY:
|
---|
662 | {
|
---|
663 | if (cd->cdh.num_words[which])
|
---|
664 | {
|
---|
665 | load_read_words(dict, cd, which, 1);
|
---|
666 | }
|
---|
667 |
|
---|
668 | load_read_huffman(dict, cd, which);
|
---|
669 | load_read_huffman(dict, cd, which);
|
---|
670 | }
|
---|
671 | break;
|
---|
672 | case MG_SEED_DICTIONARY:
|
---|
673 | {
|
---|
674 | if (cd->cdh.num_words[which])
|
---|
675 | {
|
---|
676 | load_read_words(dict, cd, which, 1);
|
---|
677 | }
|
---|
678 | switch (cd->cdh.novel_method)
|
---|
679 | {
|
---|
680 | case MG_NOVEL_HUFFMAN_CHARS:
|
---|
681 | load_read_huffman(dict, cd, which);
|
---|
682 | load_read_huffman(dict, cd, which);
|
---|
683 | break;
|
---|
684 | case MG_NOVEL_DELTA:
|
---|
685 | break;
|
---|
686 | case MG_NOVEL_HYBRID:
|
---|
687 | break;
|
---|
688 | }
|
---|
689 | break;
|
---|
690 | }
|
---|
691 | }
|
---|
692 | fclose (dict);
|
---|
693 |
|
---|
694 |
|
---|
695 | if (cd->cdh.novel_method == MG_NOVEL_DELTA ||
|
---|
696 | cd->cdh.novel_method == MG_NOVEL_HYBRID)
|
---|
697 | {
|
---|
698 | cd->ad = LoadAuxDict (&cd->cdh, filename);
|
---|
699 | FIXUP (&cd->ad);
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 |
|
---|
704 | static void
|
---|
705 | save_fast_dict (char *filename)
|
---|
706 | {
|
---|
707 | FILE *fdict;
|
---|
708 |
|
---|
709 | fdict = create_file (filename, TEXT_DICT_FAST_SUFFIX, "wb",
|
---|
710 | MAGIC_FAST_DICT, MG_ABORT); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
711 |
|
---|
712 | {
|
---|
713 | u_long *p;
|
---|
714 | for (p = (u_long *) buffer; (u_long) p < (u_long) cur; p++)
|
---|
715 | {
|
---|
716 | if (IS_FIXUP (p))
|
---|
717 | HTONUL(*p);
|
---|
718 | }
|
---|
719 | }
|
---|
720 |
|
---|
721 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
722 | HTONUL(mem);
|
---|
723 | HTONUL(fixup_mem);
|
---|
724 |
|
---|
725 | fwrite (&mem, sizeof (mem), 1, fdict);
|
---|
726 | fwrite (&fixup_mem, sizeof (fixup_mem), 1, fdict);
|
---|
727 |
|
---|
728 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
729 | NTOHUL(mem);
|
---|
730 | NTOHUL(fixup_mem);
|
---|
731 |
|
---|
732 | fwrite (buffer, sizeof (u_char), mem, fdict);
|
---|
733 | fwrite (fixup, sizeof (u_char), fixup_mem, fdict);
|
---|
734 |
|
---|
735 | fclose (fdict);
|
---|
736 | }
|
---|