1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * comp_dict.c -- Functions for loading the 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: comp_dict.c 13 1998-11-17 09:36:00Z rjmcnab $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | #include "sysfuncs.h"
|
---|
25 |
|
---|
26 | #include "huffman.h"
|
---|
27 | #include "local_strings.h"
|
---|
28 | #include "memlib.h"
|
---|
29 | #include "messages.h"
|
---|
30 |
|
---|
31 | #include "mg.h"
|
---|
32 | #include "hash.h"
|
---|
33 | #include "text.h"
|
---|
34 | #include "comp_dict.h"
|
---|
35 | #include "locallib.h"
|
---|
36 | #include "mg_files.h"
|
---|
37 |
|
---|
38 | /*
|
---|
39 | $Log$
|
---|
40 | Revision 1.1 1998/11/17 09:34:34 rjmcnab
|
---|
41 | *** empty log message ***
|
---|
42 |
|
---|
43 | * Revision 1.3 1994/10/20 03:56:41 tes
|
---|
44 | * I have rewritten the boolean query optimiser and abstracted out the
|
---|
45 | * components of the boolean query.
|
---|
46 | *
|
---|
47 | * Revision 1.2 1994/09/20 04:41:24 tes
|
---|
48 | * For version 1.1
|
---|
49 | *
|
---|
50 | */
|
---|
51 |
|
---|
52 | static char *RCSID = "$Id: comp_dict.c 13 1998-11-17 09:36:00Z rjmcnab $";
|
---|
53 |
|
---|
54 | compression_dict_header cdh;
|
---|
55 | compressed_text_header cth;
|
---|
56 | comp_frags_header cfh[2];
|
---|
57 |
|
---|
58 | dict_hash_table *ht[2];
|
---|
59 |
|
---|
60 | huff_data char_huff[2];
|
---|
61 | huff_data lens_huff[2];
|
---|
62 | u_long *char_codes[2], *lens_codes[2];
|
---|
63 | u_long Words_disk = 0;
|
---|
64 | u_long Chars_disk = 0;
|
---|
65 |
|
---|
66 |
|
---|
67 | static dict_hash_table *
|
---|
68 | ReadInWords (FILE * dict, comp_frags_header * cfh,
|
---|
69 | int esc)
|
---|
70 | {
|
---|
71 | int i;
|
---|
72 | u_char *allwords, *prev = NULL;
|
---|
73 | dict_hash_table *ht;
|
---|
74 | u_char **words;
|
---|
75 | u_long ht_size;
|
---|
76 |
|
---|
77 | ht_size = prime (cfh->hd.num_codes * HASH_RATIO);
|
---|
78 | if (!(ht = Xmalloc (sizeof (dict_hash_table) +
|
---|
79 | (ht_size - 1) * sizeof (ht->table[0]))))
|
---|
80 | {
|
---|
81 | Message ("no memory for hash_table\n");
|
---|
82 | return NULL;
|
---|
83 | }
|
---|
84 |
|
---|
85 | ht->size = ht_size;
|
---|
86 | ht->hd = &cfh->hd;
|
---|
87 |
|
---|
88 | if (!(ht->codes = Generate_Huffman_Codes (&cfh->hd, NULL)))
|
---|
89 | {
|
---|
90 | Message ("no memory for huffman codes\n");
|
---|
91 | return NULL;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (!(ht->words = Xmalloc (sizeof (u_char *) * cfh->hd.num_codes)))
|
---|
95 | {
|
---|
96 | Message ("no memory for word pointers\n");
|
---|
97 | return NULL;
|
---|
98 | }
|
---|
99 | words = ht->words;
|
---|
100 |
|
---|
101 | bzero ((char *) ht->table, ht_size * sizeof (ht->table[0]));
|
---|
102 |
|
---|
103 | if (!(allwords = Xmalloc (sizeof (u_char) * cfh->uncompressed_size)))
|
---|
104 | {
|
---|
105 | Message ("no memory for words\n");
|
---|
106 | return NULL;
|
---|
107 | }
|
---|
108 |
|
---|
109 | for (i = 0; i < cfh->hd.num_codes; i++, words++)
|
---|
110 | {
|
---|
111 | register int val, copy;
|
---|
112 | val = fgetc (dict);
|
---|
113 | copy = (val >> 4) & 0xf;
|
---|
114 | val &= 0xf;
|
---|
115 |
|
---|
116 | *words = allwords;
|
---|
117 |
|
---|
118 | memcpy (allwords + 1, prev + 1, copy);
|
---|
119 | fread (allwords + copy + 1, sizeof (u_char), val, dict);
|
---|
120 | *allwords = val + copy;
|
---|
121 |
|
---|
122 | Words_disk += val + 1;
|
---|
123 |
|
---|
124 | /* insert into the hash table */
|
---|
125 | if (i < cfh->hd.num_codes - esc)
|
---|
126 | {
|
---|
127 | register u_char **wptr;
|
---|
128 | register int tsize = ht->size;
|
---|
129 | register unsigned long hashval, step;
|
---|
130 |
|
---|
131 | HASH (hashval, step, allwords, tsize);
|
---|
132 |
|
---|
133 | wptr = ht->table[hashval];
|
---|
134 | for (; wptr;)
|
---|
135 | {
|
---|
136 | hashval += step;
|
---|
137 | if (hashval >= tsize)
|
---|
138 | hashval -= tsize;
|
---|
139 | wptr = ht->table[hashval];
|
---|
140 | }
|
---|
141 | ht->table[hashval] = words;
|
---|
142 | }
|
---|
143 | prev = allwords;
|
---|
144 | allwords += *allwords + 1;
|
---|
145 | }
|
---|
146 | return ht;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 |
|
---|
151 | int
|
---|
152 | LoadCompressionDictionary (char *dict_file_name)
|
---|
153 | {
|
---|
154 | FILE *dict;
|
---|
155 | int which;
|
---|
156 | if (!(dict = open_named_file (dict_file_name, "rb", MAGIC_DICT, MG_MESSAGE))) /* [RPAP - Feb 97: WIN32 Port] */
|
---|
157 | return COMPERROR;
|
---|
158 |
|
---|
159 | Words_disk = sizeof (u_long);
|
---|
160 |
|
---|
161 | if (Read_cdh (dict, &cdh, NULL, &Words_disk) == -1)
|
---|
162 | goto error;
|
---|
163 |
|
---|
164 | for (which = 0; which < 2; which++)
|
---|
165 | switch (cdh.dict_type)
|
---|
166 | {
|
---|
167 | case MG_COMPLETE_DICTIONARY:
|
---|
168 | {
|
---|
169 | if (Read_cfh (dict, &cfh[which], NULL, &Words_disk) == -1)
|
---|
170 | goto error;
|
---|
171 |
|
---|
172 | if (!(ht[which] = ReadInWords (dict, &cfh[which], 0)))
|
---|
173 | goto error;
|
---|
174 |
|
---|
175 | }
|
---|
176 | break;
|
---|
177 | case MG_PARTIAL_DICTIONARY:
|
---|
178 | {
|
---|
179 | if (cdh.num_words[which])
|
---|
180 | {
|
---|
181 | if (Read_cfh (dict, &cfh[which], NULL, &Words_disk) == -1)
|
---|
182 | goto error;
|
---|
183 | if (!(ht[which] = ReadInWords (dict, &cfh[which], 1)))
|
---|
184 | goto error;
|
---|
185 | }
|
---|
186 | else
|
---|
187 | ht[which] = NULL;
|
---|
188 | if (Read_Huffman_Data (dict, &char_huff[which], NULL,
|
---|
189 | &Chars_disk) == -1)
|
---|
190 | goto error;
|
---|
191 | if (!(char_codes[which] =
|
---|
192 | Generate_Huffman_Codes (&char_huff[which], NULL)))
|
---|
193 | goto error;
|
---|
194 | if (Read_Huffman_Data (dict, &lens_huff[which], NULL,
|
---|
195 | &Chars_disk) == -1)
|
---|
196 | goto error;
|
---|
197 | if (!(lens_codes[which] =
|
---|
198 | Generate_Huffman_Codes (&lens_huff[which], NULL)))
|
---|
199 | goto error;
|
---|
200 | }
|
---|
201 | break;
|
---|
202 | case MG_SEED_DICTIONARY:
|
---|
203 | {
|
---|
204 | if (cdh.num_words[which])
|
---|
205 | {
|
---|
206 | if (Read_cfh (dict, &cfh[which], NULL, &Words_disk) == -1)
|
---|
207 | goto error;
|
---|
208 | if (!(ht[which] = ReadInWords (dict, &cfh[which], 1)))
|
---|
209 | goto error;
|
---|
210 | }
|
---|
211 | else
|
---|
212 | ht[which] = NULL;
|
---|
213 | switch (cdh.novel_method)
|
---|
214 | {
|
---|
215 | case MG_NOVEL_HUFFMAN_CHARS:
|
---|
216 | if (Read_Huffman_Data (dict, &char_huff[which], NULL,
|
---|
217 | &Chars_disk) == -1)
|
---|
218 | goto error;
|
---|
219 | if (!(char_codes[which] =
|
---|
220 | Generate_Huffman_Codes (&char_huff[which], NULL)))
|
---|
221 | goto error;
|
---|
222 | if (Read_Huffman_Data (dict, &lens_huff[which], NULL,
|
---|
223 | &Chars_disk) == -1)
|
---|
224 | goto error;
|
---|
225 | if (!(lens_codes[which] =
|
---|
226 | Generate_Huffman_Codes (&lens_huff[which], NULL)))
|
---|
227 | goto error;
|
---|
228 | break;
|
---|
229 | case MG_NOVEL_BINARY:
|
---|
230 | break;
|
---|
231 | case MG_NOVEL_DELTA:
|
---|
232 | break;
|
---|
233 | case MG_NOVEL_HYBRID:
|
---|
234 | break;
|
---|
235 | case MG_NOVEL_HYBRID_MTF:
|
---|
236 | break;
|
---|
237 | default:
|
---|
238 | FatalError (1, "Bad novel method");
|
---|
239 | }
|
---|
240 | }
|
---|
241 | break;
|
---|
242 | default:
|
---|
243 | FatalError (1, "Bad dictionary kind\n");
|
---|
244 | }
|
---|
245 |
|
---|
246 | return (COMPALLOK);
|
---|
247 |
|
---|
248 |
|
---|
249 | error:
|
---|
250 | fclose (dict);
|
---|
251 | return (COMPERROR);
|
---|
252 | }
|
---|