/src/httrack/src/htsindex.c
Line | Count | Source |
1 | | /* ------------------------------------------------------------ */ |
2 | | /* |
3 | | HTTrack Website Copier, Offline Browser for Windows and Unix |
4 | | Copyright (C) 1998 Xavier Roche and other contributors |
5 | | |
6 | | SPDX-License-Identifier: GPL-3.0-or-later |
7 | | |
8 | | This program is free software: you can redistribute it and/or modify |
9 | | it under the terms of the GNU General Public License as published by |
10 | | the Free Software Foundation, either version 3 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | | |
21 | | Ethical use: we kindly ask that you NOT use this software to harvest email |
22 | | addresses or to collect any other private information about people. Doing so |
23 | | would dishonor our work and waste the many hours we have spent on it. |
24 | | |
25 | | Please visit our Website: http://www.httrack.com |
26 | | */ |
27 | | |
28 | | /* ------------------------------------------------------------ */ |
29 | | /* File: htsindex.c */ |
30 | | /* keyword indexing system (search index) */ |
31 | | /* Author: Xavier Roche */ |
32 | | /* ------------------------------------------------------------ */ |
33 | | |
34 | | #include "htsindex.h" |
35 | | #include "htsglobal.h" |
36 | | #include "htslib.h" |
37 | | #include "htslines.h" |
38 | | #include "htsio.h" |
39 | | |
40 | | #include "htshash.h" |
41 | | #include "coucal.h" |
42 | | |
43 | | /* Keyword Indexer Parameters */ |
44 | | |
45 | | // Maximum length for a keyword |
46 | 0 | #define KEYW_LEN 50 |
47 | | /* sscanf width for the char[KEYW_LEN + 32] words below: a format width has to |
48 | | be a literal, so the static assertion next to it is what ties the two. */ |
49 | | #define KEYW_SCAN_WIDTH "81" |
50 | | // Minimum length for a keyword - MUST NOT BE NULL!!! |
51 | 0 | #define KEYW_MIN_LEN 3 |
52 | | // What characters to accept? - MUST NOT BE EMPTY AND MUST NOT CONTAIN THE SPACE |
53 | | // (32) CHARACTER!!! |
54 | 0 | #define KEYW_ACCEPT "abcdefghijklmnopqrstuvwxyz0123456789-_." |
55 | | // Letters U+00C0..U+00FF (UTF-8 C3 80..C3 BF) folded to ASCII, '*' if none |
56 | | // ('*' is neither accepted nor a space, so such a word is dropped) |
57 | | #define KEYW_LATIN1_FOLD \ |
58 | 0 | "aaaaaa*ceeeeiiii*nooooo*ouuuuy**" \ |
59 | 0 | "aaaaaa*ceeeeiiii*nooooo*ouuuuy*y" |
60 | | // These (accepted) characters will be ignored at beginning of a keyword |
61 | 0 | #define KEYW_IGNORE_BEG "-_." |
62 | | // These (accepted) characters will be stripped if at the end of a keyword |
63 | 0 | #define KEYW_STRIP_END "-_." |
64 | | // Words beginning with these (accepted) characters will be ignored |
65 | 0 | #define KEYW_NOT_BEG "0123456789" |
66 | | // Treat these characters as space characters - MUST NOT BE EMPTY!!! |
67 | 0 | #define KEYW_SPACE " ',;:!?\"\x0d\x0a\x09\x0b\x0c" |
68 | | // Common words (the,for..) detector |
69 | | // If a word represents more than KEYW_USELESS1K (%1000) of total words, then ignore it |
70 | | // 5 (0.5%) |
71 | 0 | #define KEYW_USELESS1K 5 |
72 | | // If a word is present in more than KEYW_USELESS1KPG (%1000) pages, then ignore it |
73 | | // 800 (80%) |
74 | 0 | #define KEYW_USELESS1KPG 800 |
75 | | // This number will be reduced by index hit for sorting purpose |
76 | | // leave it as it is here if you don't REALLY know what you are doing |
77 | | // Yes, I may be the only person, maybe |
78 | 0 | #define KEYW_SORT_MAXCOUNT 999999999 |
79 | | |
80 | | /* End of Keyword Indexer Parameters */ |
81 | | |
82 | | static int mystrcmp(const void *_e1, const void *_e2); |
83 | | |
84 | | /* The whole mirror's indexing state: only index_init resets it. */ |
85 | | static struct { |
86 | | hts_boolean first_page; /* index.txt and sindex.html not yet removed */ |
87 | | int lines; /* entries in tmp */ |
88 | | int words; /* every kept word, repeats included */ |
89 | | FILE *tmp; /* "word count url" lines, until index_finish */ |
90 | | } indexer = {HTS_TRUE, 0, 0, NULL}; |
91 | | |
92 | | /* |
93 | | Init index |
94 | | */ |
95 | 0 | void index_init(const char *indexpath) { |
96 | 0 | indexer.first_page = HTS_TRUE; |
97 | 0 | indexer.lines = 0; |
98 | 0 | indexer.words = 0; |
99 | 0 | if (indexer.tmp) |
100 | 0 | fclose(indexer.tmp); |
101 | 0 | indexer.tmp = tmpfile(); |
102 | 0 | } |
103 | | |
104 | | /* End of a word: drop or clean the pending keyword, store it, then clear it. */ |
105 | 0 | static void index_store_word(coucal WordIndexHash, FILE *tmpfp, char *keyword) { |
106 | 0 | int len = (int) strlen(keyword); |
107 | | |
108 | | /* Avoid these words */ |
109 | 0 | if (len > 0) { |
110 | 0 | if (strchr(KEYW_NOT_BEG, keyword[0])) { |
111 | 0 | keyword[(len = 0)] = '\0'; |
112 | 0 | } |
113 | 0 | } |
114 | | |
115 | | /* Strip ending . and so */ |
116 | 0 | { |
117 | 0 | int ok = 0; |
118 | |
|
119 | 0 | while ((len = (int) strlen(keyword)) && (!ok)) { |
120 | 0 | if (strchr(KEYW_STRIP_END, keyword[len - 1])) { /* strip it */ |
121 | 0 | keyword[len - 1] = '\0'; |
122 | 0 | } else |
123 | 0 | ok = 1; |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | /* Store it ? */ |
128 | 0 | if (len >= KEYW_MIN_LEN) { |
129 | 0 | indexer.words++; |
130 | 0 | if (coucal_inc(WordIndexHash, keyword) == 1) { /* added new */ |
131 | 0 | fprintf(tmpfp, "%s\n", keyword); |
132 | 0 | } |
133 | 0 | } |
134 | 0 | keyword[0] = '\0'; |
135 | 0 | } |
136 | | |
137 | | /* |
138 | | Indexing system |
139 | | A little bit dirty, (quick'n dirty, in fact) |
140 | | But should be okay on most cases |
141 | | Tags and javascript handled (ignored) |
142 | | */ |
143 | | /* Note: utf-8 */ |
144 | | int index_keyword(hts_span html, const char *mime, const char *filename, |
145 | 0 | const char *indexpath) { |
146 | 0 | char catbuff[CATBUFF_SIZE]; |
147 | 0 | int intag = 0, inscript = 0, incomment = 0; |
148 | 0 | char keyword[KEYW_LEN + 32]; |
149 | 0 | size_t i = 0; |
150 | |
|
151 | 0 | coucal WordIndexHash = NULL; |
152 | 0 | FILE *tmpfp = NULL; |
153 | | |
154 | | // |
155 | | |
156 | | // Check parameters |
157 | 0 | if (!html.p || !html.len) |
158 | 0 | return 0; |
159 | 0 | if (!mime) |
160 | 0 | return 0; |
161 | 0 | if (!filename) |
162 | 0 | return 0; |
163 | | |
164 | | // Init ? |
165 | 0 | if (indexer.first_page) { |
166 | 0 | if (hts_path_join(HTS_BUF(catbuff), indexpath, "index.txt")) |
167 | 0 | UNLINK(catbuff); |
168 | 0 | if (hts_path_join(HTS_BUF(catbuff), indexpath, "sindex.html")) |
169 | 0 | UNLINK(catbuff); |
170 | 0 | indexer.first_page = HTS_FALSE; |
171 | 0 | } |
172 | | // Check MIME type |
173 | 0 | if (is_html_mime_type(mime)) { |
174 | 0 | inscript = 0; |
175 | 0 | } |
176 | | // FIXME - temporary fix for image/svg+xml (svg) |
177 | | // "IN XML" (html like, in fact :) ) |
178 | 0 | else if ((hts_streq_ci(mime, "image/svg+xml")) || |
179 | 0 | (hts_streq_ci(mime, "image/svg-xml"))) { |
180 | 0 | inscript = 0; |
181 | 0 | } else if (is_javascript_mime_type(mime) || |
182 | 0 | (hts_streq_ci(mime, "text/css"))) { |
183 | 0 | inscript = 1; |
184 | 0 | } else |
185 | 0 | return 0; |
186 | | |
187 | | // Temporary file |
188 | 0 | tmpfp = tmpfile(); |
189 | 0 | if (!tmpfp) |
190 | 0 | return 0; |
191 | | |
192 | | // Create hash structure |
193 | | // Hash tables rulez da world! |
194 | 0 | WordIndexHash = coucal_new(0); |
195 | 0 | if (!WordIndexHash) { |
196 | 0 | fclose(tmpfp); |
197 | 0 | return 0; |
198 | 0 | } |
199 | | |
200 | | // Start indexing this page |
201 | 0 | keyword[0] = '\0'; |
202 | 0 | while (i < html.len) { |
203 | 0 | const hts_span rest = hts_span_skip(html, i); |
204 | | |
205 | | /* a tag ends the pending word */ |
206 | 0 | if (html.p[i] == '<' && !inscript && !incomment && !intag) |
207 | 0 | index_store_word(WordIndexHash, tmpfp, keyword); |
208 | 0 | if (hts_span_starts_with_ci(rest, hts_span_of("<script"))) { |
209 | 0 | inscript = 1; |
210 | 0 | } else if (hts_span_starts_with_ci(rest, hts_span_of("<!--"))) { |
211 | 0 | incomment = 1; |
212 | 0 | } else if (hts_span_starts_with_ci(rest, hts_span_of("</script"))) { |
213 | 0 | if (!incomment) { |
214 | 0 | inscript = 0; |
215 | 0 | intag = 1; |
216 | 0 | } |
217 | 0 | } else if (hts_span_starts_with(rest, hts_span_of("-->"))) { |
218 | 0 | incomment = 0; |
219 | 0 | } else if (html.p[i] == '<') { |
220 | 0 | if (!inscript) |
221 | 0 | intag = 1; |
222 | 0 | } else if (html.p[i] == '>') { |
223 | 0 | intag = 0; |
224 | 0 | } else { |
225 | | // Okay, parse keywords |
226 | 0 | if ((!inscript) && (!incomment) && (!intag)) { |
227 | 0 | char cchar = html.p[i]; |
228 | 0 | int len = (int) strlen(keyword); |
229 | | |
230 | | // Replace (ignore case, and so on..) |
231 | 0 | if (cchar >= 'A' && cchar <= 'Z') { |
232 | 0 | cchar = (char) (cchar + ('a' - 'A')); |
233 | 0 | } else if ((unsigned char) cchar == 0xC3 && i + 1 < html.len && |
234 | 0 | ((unsigned char) html.p[i + 1] & 0xC0) == 0x80) { |
235 | 0 | cchar = KEYW_LATIN1_FOLD[(unsigned char) html.p[i + 1] & 0x3F]; |
236 | 0 | i++; /* second byte of the UTF-8 pair */ |
237 | 0 | } |
238 | |
|
239 | 0 | if (strchr(KEYW_ACCEPT, cchar)) { |
240 | | /* Ignore some characters at beginning */ |
241 | 0 | if ((len > 0) || (!strchr(KEYW_IGNORE_BEG, cchar))) { |
242 | 0 | keyword[len++] = cchar; |
243 | 0 | keyword[len] = '\0'; |
244 | 0 | } |
245 | 0 | } else if ((strchr(KEYW_SPACE, cchar)) || (!cchar)) { |
246 | |
|
247 | 0 | index_store_word(WordIndexHash, tmpfp, keyword); |
248 | 0 | keyword[(len = 0)] = '\0'; |
249 | 0 | } else /* Invalid */ |
250 | 0 | keyword[(len = 0)] = '\0'; |
251 | |
|
252 | 0 | if (len > KEYW_LEN) { |
253 | 0 | keyword[(len = 0)] = '\0'; |
254 | 0 | } |
255 | 0 | } |
256 | 0 | } |
257 | |
|
258 | 0 | i++; |
259 | 0 | } |
260 | |
|
261 | 0 | if (!inscript && !incomment && !intag) |
262 | 0 | index_store_word(WordIndexHash, tmpfp, keyword); |
263 | | |
264 | | // Reset temp file |
265 | 0 | fseek(tmpfp, 0, SEEK_SET); |
266 | | |
267 | | // Process indexing for this page |
268 | 0 | { |
269 | 0 | if (indexer.tmp) { |
270 | 0 | while(!feof(tmpfp)) { |
271 | 0 | char line[KEYW_LEN + 32]; |
272 | |
|
273 | 0 | linput(tmpfp, HTS_BUF_N(line, KEYW_LEN + 2)); |
274 | 0 | if (strnotempty(line)) { |
275 | 0 | intptr_t e = 0; |
276 | |
|
277 | 0 | if (coucal_read(WordIndexHash, line, &e)) { |
278 | 0 | char BIGSTK savelst[HTS_URLMAXSIZE * 2]; |
279 | |
|
280 | 0 | if (strncmp(fslash(HTS_BUF(catbuff), indexpath), filename, |
281 | 0 | strlen(indexpath)) == 0) |
282 | 0 | strcpybuff(savelst, filename + strlen(indexpath)); |
283 | 0 | else |
284 | 0 | strcpybuff(savelst, filename); |
285 | | |
286 | | // Add entry for this file and word |
287 | 0 | fprintf(indexer.tmp, "%s %d %s\n", line, |
288 | 0 | (int) (KEYW_SORT_MAXCOUNT - e), savelst); |
289 | 0 | indexer.lines++; |
290 | 0 | } |
291 | 0 | } |
292 | 0 | } |
293 | 0 | } |
294 | 0 | } |
295 | | |
296 | | // Delete temp file |
297 | 0 | fclose(tmpfp); |
298 | 0 | tmpfp = NULL; |
299 | | |
300 | | // Clear hash table |
301 | 0 | coucal_delete(&WordIndexHash); |
302 | 0 | return 1; |
303 | 0 | } |
304 | | |
305 | | /* Close a word's block: its total, or "ignored" over it if it is too common. */ |
306 | | static void index_close_word(FILE *fp, hts_kindex mode, LLint total_hit, |
307 | 0 | LLint total_line, int index, long last_pos) { |
308 | 0 | if (mode == HTS_KINDEX_TEXT) |
309 | 0 | fprintf(fp, "\t=%lld\r\n", (long long) total_hit); |
310 | 0 | if ((((total_hit * 1000) / indexer.words) >= KEYW_USELESS1K) || |
311 | 0 | (((total_line * 1000) / index) >= KEYW_USELESS1KPG)) { |
312 | 0 | fseek(fp, last_pos, SEEK_SET); |
313 | 0 | if (mode == HTS_KINDEX_TEXT) |
314 | 0 | fprintf(fp, "\tignored (%d)\r\n", |
315 | 0 | (int) ((total_hit * 1000) / indexer.words)); |
316 | 0 | else |
317 | 0 | fprintf(fp, "(ignored) [%lld hits]<br>\r\n", (long long) total_hit); |
318 | 0 | } else { |
319 | 0 | if (mode == HTS_KINDEX_TEXT) |
320 | 0 | fprintf(fp, "\t(%d)\r\n", (int) ((total_hit * 1000) / indexer.words)); |
321 | 0 | } |
322 | 0 | } |
323 | | |
324 | | /* Write the HTML or text index of the sorted lines tab[0..index-1]. */ |
325 | 0 | static void index_write(FILE *fp, hts_kindex mode, char **tab, int index) { |
326 | 0 | char current_word[KEYW_LEN + 32]; |
327 | 0 | char word[KEYW_LEN + 32]; |
328 | 0 | int hit; |
329 | 0 | int i; |
330 | 0 | LLint total_hit = 0; |
331 | 0 | LLint total_line = 0; |
332 | 0 | long last_pos = 0; |
333 | 0 | char word0 = '\0'; |
334 | |
|
335 | 0 | HTS_STATIC_ASSERT(sizeof(word) == 82, keyw_scan_width_matches); |
336 | 0 | current_word[0] = '\0'; |
337 | |
|
338 | 0 | if (mode == HTS_KINDEX_HTML) { |
339 | 0 | for (i = 0; i < index; i++) { |
340 | 0 | if (word0 != tab[i][0]) { |
341 | 0 | word0 = tab[i][0]; |
342 | 0 | fprintf(fp, " <a href=\"#%c\">%c</a>\r\n", word0, word0); |
343 | 0 | } |
344 | 0 | } |
345 | 0 | word0 = '\0'; |
346 | 0 | fprintf(fp, "<br><br>\r\n"); |
347 | 0 | fprintf(fp, "<table width=\"100%%\" " |
348 | 0 | "border=\"0\">\r\n<tr>\r\n<td>word</td>\r\n<td>location\r\n"); |
349 | 0 | } |
350 | |
|
351 | 0 | for (i = 0; i < index; i++) { |
352 | 0 | char *a; |
353 | | |
354 | | /* the width follows the buffer, not the smaller keyword cap */ |
355 | 0 | if (sscanf(tab[i], "%" KEYW_SCAN_WIDTH "s %d", word, &hit) != 2) |
356 | 0 | continue; |
357 | 0 | a = strchr(tab[i], ' '); |
358 | 0 | if (a) |
359 | 0 | a = strchr(a + 1, ' '); |
360 | 0 | if (!a++) /* Yes, a++, not ++a :) */ |
361 | 0 | continue; |
362 | 0 | hit = KEYW_SORT_MAXCOUNT - hit; |
363 | 0 | if (strcmp(word, current_word)) { /* New word */ |
364 | 0 | if (total_hit) |
365 | 0 | index_close_word(fp, mode, total_hit, total_line, index, last_pos); |
366 | 0 | if (mode == HTS_KINDEX_TEXT) |
367 | 0 | fprintf(fp, "%s\r\n", word); |
368 | 0 | else { // HTML |
369 | 0 | fprintf(fp, "</td></tr>\r\n"); |
370 | 0 | if (word0 != word[0]) { |
371 | 0 | word0 = word[0]; |
372 | 0 | fprintf(fp, "<th>%c</th>\r\n", word0); |
373 | 0 | fprintf(fp, "<a name=\"%c\"></a>\r\n", word0); |
374 | 0 | } |
375 | 0 | fprintf(fp, "<tr>\r\n<td>%s</td>\r\n<td>\r\n", word); |
376 | 0 | } |
377 | 0 | fflush(fp); |
378 | 0 | last_pos = ftell(fp); |
379 | 0 | strcpybuff(current_word, word); |
380 | 0 | total_hit = total_line = 0; |
381 | 0 | } |
382 | 0 | total_hit += hit; |
383 | 0 | total_line++; |
384 | 0 | if (mode == HTS_KINDEX_TEXT) |
385 | 0 | fprintf(fp, "\t%d %s\r\n", hit, a); |
386 | 0 | else // HTML |
387 | 0 | fprintf(fp, "<a href=\"%s\">%s</a> [%d hits]<br>\r\n", a, a, hit); |
388 | 0 | } |
389 | 0 | if (total_hit) |
390 | 0 | index_close_word(fp, mode, total_hit, total_line, index, last_pos); |
391 | 0 | if (mode == HTS_KINDEX_HTML) |
392 | 0 | fprintf(fp, "</td></tr>\r\n</table>\r\n"); |
393 | | /* an "ignored" rewind can leave old bytes past the end */ |
394 | 0 | if (fflush(fp) == 0 && ftell(fp) >= 0) |
395 | 0 | if (HTS_FTRUNCATE(fp, ftell(fp)) != 0) { |
396 | 0 | } |
397 | 0 | } |
398 | | |
399 | | /* |
400 | | Sort index! |
401 | | */ |
402 | | /* Note: NOT utf-8 */ |
403 | 0 | void index_finish(const char *indexpath, hts_kindex mode) { |
404 | 0 | char catbuff[CATBUFF_SIZE]; |
405 | 0 | char **tab = NULL; |
406 | 0 | char *blk = NULL; |
407 | 0 | FILE *fp; |
408 | 0 | char *a, *b; |
409 | 0 | int index = 0; |
410 | 0 | const LLint fs = fpsize(indexer.tmp); |
411 | | /* refuse a size size_t cannot hold, or malloct() would wrap short */ |
412 | 0 | const size_t size = fs > 0 ? llint_to_size_t(fs) : (size_t) -1; |
413 | |
|
414 | 0 | if (size == (size_t) -1 || !indexer.tmp) |
415 | 0 | goto done; |
416 | 0 | tab = (char **) calloct((size_t) indexer.lines + 2, sizeof(char *)); |
417 | 0 | if (!tab) |
418 | 0 | goto done; |
419 | 0 | blk = malloct(size + 1); |
420 | 0 | if (!blk) |
421 | 0 | goto done; |
422 | 0 | fseek(indexer.tmp, 0, SEEK_SET); |
423 | 0 | if (!hts_fread_exact(blk, size, indexer.tmp)) |
424 | 0 | goto done; |
425 | | |
426 | 0 | blk[size] = '\0'; |
427 | 0 | a = blk; |
428 | 0 | while ((b = strchr(a, '\n')) && (index < indexer.lines)) { |
429 | 0 | tab[index++] = a; |
430 | 0 | *b = '\0'; |
431 | 0 | a = b + 1; |
432 | 0 | } |
433 | 0 | qsort(tab, index, sizeof(char *), mystrcmp); |
434 | |
|
435 | 0 | fclose(indexer.tmp); |
436 | 0 | indexer.tmp = NULL; |
437 | |
|
438 | 0 | fp = hts_path_join(HTS_BUF(catbuff), indexpath, |
439 | 0 | mode == HTS_KINDEX_TEXT ? "index.txt" : "sindex.html") |
440 | 0 | ? FOPEN(catbuff, "wb") |
441 | 0 | : NULL; |
442 | 0 | if (fp) { |
443 | 0 | index_write(fp, mode, tab, index); |
444 | 0 | fclose(fp); |
445 | 0 | } |
446 | |
|
447 | 0 | done: |
448 | 0 | freet(blk); |
449 | 0 | freet(tab); |
450 | 0 | if (indexer.tmp) |
451 | 0 | fclose(indexer.tmp); |
452 | 0 | indexer.tmp = NULL; |
453 | 0 | } |
454 | | |
455 | | /* Subroutines */ |
456 | | |
457 | 0 | static int mystrcmp(const void *_e1, const void *_e2) { |
458 | 0 | const char *const*const e1 = (const char *const*) _e1; |
459 | 0 | const char *const*const e2 = (const char *const*) _e2; |
460 | |
|
461 | 0 | return strcmp(*e1, *e2); |
462 | 0 | } |