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