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