/* ------------------------------------------------------------ */
/*
HTTrack Website Copier, Offline Browser for Windows and Unix
Copyright (C) 1998-2017 Xavier Roche and other contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Important notes:
- We hereby ask people using this source NOT to use it in purpose of grabbing
emails addresses, or collecting any other private information on persons.
This would disgrace our work, and spoil the many hours we spent on it.
Please visit our Website: http://www.httrack.com
*/
/* ------------------------------------------------------------ */
/* File: htsindex.c */
/* keyword indexing system (search index) */
/* Author: Xavier Roche */
/* ------------------------------------------------------------ */
/* Internal engine bytecode */
#define HTS_INTERNAL_BYTECODE
#include "htsindex.h"
#include "htsglobal.h"
#include "htslib.h"
#if HTS_MAKE_KEYWORD_INDEX
#include "htshash.h"
#include "coucal.h"
/* Keyword Indexer Parameters */
// Maximum length for a keyword
#define KEYW_LEN 50
// Minimum length for a keyword - MUST NOT BE NULL!!!
#define KEYW_MIN_LEN 3
// What characters to accept? - MUST NOT BE EMPTY AND MUST NOT CONTAIN THE SPACE (32) CHARACTER!!!
#define KEYW_ACCEPT "abcdefghijklmnopqrstuvwxyz0123456789-_."
// Convert A to a, and so on.. to avoid case problems in indexing
// This can be a generic table, containing characters that are in fact not accepted by KEYW_ACCEPT
// MUST HAVE SAME SIZES!!
#define KEYW_TRANSCODE_FROM (\
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"àâä" \
"ÀÂÄ" \
"éèêë" \
"ÈÈÊË" \
"ìîï" \
"ÌÎÏ" \
"òôö" \
"ÒÔÖ" \
"ùûü" \
"ÙÛÜ" \
"ÿ" \
)
#define KEYW_TRANSCODE_TO ( \
"abcdefghijklmnopqrstuvwxyz" \
"aaa" \
"aaa" \
"eeee" \
"eeee" \
"iii" \
"iii" \
"ooo" \
"ooo" \
"uuu" \
"uuu" \
"y" \
)
// These (accepted) characters will be ignored at beginning of a keyword
#define KEYW_IGNORE_BEG "-_."
// These (accepted) characters will be stripped if at the end of a keyword
#define KEYW_STRIP_END "-_."
// Words beginning with these (accepted) characters will be ignored
#define KEYW_NOT_BEG "0123456789"
// Treat these characters as space characters - MUST NOT BE EMPTY!!!
#define KEYW_SPACE " ',;:!?\"\x0d\x0a\x09\x0b\x0c"
// Common words (the,for..) detector
// If a word represents more than KEYW_USELESS1K (%1000) of total words, then ignore it
// 5 (0.5%)
#define KEYW_USELESS1K 5
// If a word is present in more than KEYW_USELESS1KPG (%1000) pages, then ignore it
// 800 (80%)
#define KEYW_USELESS1KPG 800
// This number will be reduced by index hit for sorting purpose
// leave it as it is here if you don't REALLY know what you are doing
// Yes, I may be the only person, maybe
#define KEYW_SORT_MAXCOUNT 999999999
/* End of Keyword Indexer Parameters */
int strcpos(const char *adr, char c);
int mystrcmp(const void *_e1, const void *_e2);
// Global variables
int hts_index_init = 1;
int hts_primindex_size = 0;
FILE *fp_tmpproject = NULL;
int hts_primindex_words = 0;
#endif
/*
Init index
*/
void index_init(const char *indexpath) {
#if HTS_MAKE_KEYWORD_INDEX
/* remove(concat(indexpath,"index.txt")); */
hts_index_init = 1;
hts_primindex_size = 0;
hts_primindex_words = 0;
fp_tmpproject = tmpfile();
#endif
}
/*
Indexing system
A little bit dirty, (quick'n dirty, in fact)
But should be okay on most cases
Tags and javascript handled (ignored)
*/
/* Note: utf-8 */
int index_keyword(const char *html_data, LLint size, const char *mime,
const char *filename, const char *indexpath) {
#if HTS_MAKE_KEYWORD_INDEX
char catbuff[CATBUFF_SIZE];
int intag = 0, inscript = 0, incomment = 0;
char keyword[KEYW_LEN + 32];
int i = 0;
//
//int WordIndexSize = 1024;
coucal WordIndexHash = NULL;
FILE *tmpfp = NULL;
//
// Check parameters
if (!html_data)
return 0;
if (!size)
return 0;
if (!mime)
return 0;
if (!filename)
return 0;
// Init ?
if (hts_index_init) {
UNLINK(concat(catbuff, sizeof(catbuff), indexpath, "index.txt"));
UNLINK(concat(catbuff, sizeof(catbuff), indexpath, "sindex.html"));
hts_index_init = 0;
}
// Check MIME type
if (is_html_mime_type(mime)) {
inscript = 0;
}
// FIXME - temporary fix for image/svg+xml (svg)
// "IN XML" (html like, in fact :) )
else if ((strfield2(mime, "image/svg+xml"))
|| (strfield2(mime, "image/svg-xml"))) {
inscript = 0;
} else if ((strfield2(mime, "application/x-javascript"))
|| (strfield2(mime, "text/css"))
) {
inscript = 1;
//} else if (strfield2(mime, "text/vnd.wap.wml")) { // humm won't work in many cases
// inscript=0;
} else
return 0;
// Temporary file
tmpfp = tmpfile();
if (!tmpfp)
return 0;
// Create hash structure
// Hash tables rulez da world!
WordIndexHash = coucal_new(0);
if (!WordIndexHash)
return 0;
// Start indexing this page
keyword[0] = '\0';
while(i < size) {
if (strfield(html_data + i, "