コード例 #1
0
ファイル: indexer.c プロジェクト: tombouctou/msu-cmc-practice
/**
 * indexer main()
 *
 * @param argc Command line argument count
 * @param argv command line parameters
 * @return 0 if ok, 1 on failure
 * @todo file date comparison
 */
int main(int argc, char** argv)
{
	FILE* curFile;
	int curFileNumber;
	int filesIndexed = 0;
	Dictionary files, words;
	PairDictionary pairs;
	char* curWord = malloc(MAX_WORD_LENGTH + 1);
	State state = SPACE;
	int curWordIndex = 0, added = 0;
	int ch, da;
	int retn = 0;

	if ((argc == 1) || ((argc == 2) && (strcmp(argv[1], "--help") == 0)) || ((argc == 2) && (strcmp(argv[1], "-?") == 0)))
	{
		printf("Creates a database for the 'search' program.\n\n");
		printf("INDEXER [file1] [file2] [file3] ...\n");
		printf("INDEXER { [--help] | [-?] } \n\n");
		printf("\t--help, -?  Show help (default)\n");
		return EXIT_SUCCESS;
	}

	if (dictionaryInit(&files))
		return BASE_ERROR;
	if (dictionaryInit(&words))
		return BASE_ERROR;
	if (pairDictionaryInit(&pairs))
		return BASE_ERROR;
	
	if ((file_exists("files.db")) && !dictionaryLoadFromFile(&files, "files.db"))
	{
		if (!dictionaryLoadFromFile(&words, "words.db"))
		{
			if (!pairDictionaryLoadFromFile(&pairs, "pairs.db"))
			{
				//printf("Current search base: %i files, %i words, %i pairs.\n", files.count, words.count, pairs.count);
			}
			else
			{
				fprintf(stderr, "Warning: corrupt pairs table.\n");
				pairDictionaryClear(&pairs);
			}
		}
		else
		{
			fprintf(stderr, "Warning: corrupt words database.\n");
			dictionaryClear(&words);
		}
	}
	else
	{
		printf("Creating new search base.\n");
		dictionaryClear(&files);
	}

	for (curFileNumber = 1; curFileNumber < argc; curFileNumber++)
	{
		curWord[MAX_WORD_LENGTH] = 0;
		if ((curFile = fopen(argv[curFileNumber], "r")))
		{
			int curFileId;
			if ((curFileId = dictionaryGetByString(&files, argv[curFileNumber])) != DIC_NO_RECORD)
			{
				pairDictionaryRemoveByFileId(&pairs, curFileId);
			}
			else
				curFileId = dictionaryAdd(&files, argv[curFileNumber]);
			if (curFileId < 0)
			{
				fprintf(stderr, "Error: could not add to the dictionary.\n");
				retn = INDEXER_ERROR;
				goto erexit;
			}
			curWordIndex = 0;
			state = SPACE;
			while((ch = fgetc(curFile)) != EOF)
			{
				if (!isspace(ch))
				{
					if (state != SKIP)
					{
						state = WORD;
						curWord[curWordIndex++] = ch;
						if (curWordIndex == MAX_WORD_LENGTH)
						{
							state = SKIP;
							added = 0;
						}
					}
				}
				else
				{
					if ((state == WORD) || ((state == SKIP) && !added))
					{
						if (curWordIndex < MAX_WORD_LENGTH)
							curWord[curWordIndex] = 0;
						else
							curWord[MAX_WORD_LENGTH] = 0;
						da = dictionaryAdd(&words, curWord);
						if (da < 0)
						{
							fprintf(stderr, "Error: could not add to the dictionary.\n");
							retn = INDEXER_ERROR;
							goto erexit;
						}
						if (pairDictionaryAdd(&pairs, curFileId, da) < 0)
						{
							fprintf(stderr, "Error: could not add to the pair dictionary.\n");
							retn = INDEXER_ERROR;
							goto erexit;
						}
						curWordIndex = 0;
					}
					state = SPACE;
				}
			}
			if (state == WORD)
			{
				curWord[curWordIndex] = 0;
				da = dictionaryAdd(&words, curWord);
				if (da < 0)
				{
					fprintf(stderr, "Error: could not add to the dictionary.\n");
					retn = INDEXER_ERROR;
					goto erexit;
				}
				pairDictionaryAdd(&pairs, curFileId, da);
				curWordIndex = 0;
			}
			filesIndexed++;
			if (curFile) fclose(curFile);
		}
		else
		{
			printf("Warning: file '%s' does not exist.\n", argv[curFileNumber]);
		}
	}

	dictionarySaveToFile(&files, "files.db");
	dictionarySaveToFile(&words, "words.db");
	pairDictionarySaveToFile(&pairs, "pairs.db");
erexit:
	if (curWord) free(curWord);
	dictionaryFinalize(&files);
	dictionaryFinalize(&words);
	pairDictionaryFinalize(&pairs);
	printf("Current search base: %i files, %i words, %i pairs. %i files indexed.\n", files.count, words.count, pairs.count, filesIndexed);
	return retn;
}
コード例 #2
0
bool_t createRainbowTable(const char * hashName,
			  const char * prefix,
			  const char * rule,
			  const char * dictionaryFilename,
			  const char * chainLengthStr,
			  const char * entiresInHashTableStr,
			  const char * bucketBlockLengthStr) {
	bool_t ret = FALSE;
	BasicHashFunctionPtr hashFunc;
	ulong_t chainLength;
	ulong_t entiresInHashTable;
	ulong_t bucketBlockLength;
	dictionary_t dictionary;
	passwordGenerator_t passwordGenerator;
	ulong_t maxPasswordLength;
	char * enumeratorPassword = NULL;
	char * generatorPassword = NULL;
	randomPasswordEnumerator_t randomPasswordEnumerator;

	CHECK(parseHashFunName(&hashFunc, hashName));
	CHECK(verifyDEHTNotExist(prefix));
	CHECK(validateRule(rule));
	CHECK(parseIniNum(chainLengthStr, &chainLength));
	CHECK(parseIniNum(entiresInHashTableStr, &entiresInHashTable));
	CHECK(parseIniNum(bucketBlockLengthStr, &bucketBlockLength));
	CHECK(readDictionaryFromFile(&dictionary, dictionaryFilename));

	if (!passwordGeneratorInitialize(&passwordGenerator, rule, &dictionary)) {
		goto LBL_CLEANUP_DICTIONARY;
	}

	maxPasswordLength = passwordGeneratorGetMaxLength(&passwordGenerator);

	generatorPassword = (char *) malloc((maxPasswordLength + 1) * sizeof(char));
	if (NULL == generatorPassword) {
		PERROR();
		goto LBL_CLEANUP_GENERATOR;
	}

	enumeratorPassword = (char *) malloc((maxPasswordLength + 1) * sizeof(char));
	if (NULL == enumeratorPassword) {
		PERROR();
		goto LBL_CLEANUP_GENERATOR;
	}
	
	randomPasswordEnumeratorInitialize(&randomPasswordEnumerator,
					   &passwordGenerator,
					   enumeratorPassword,
					   RANDOM_PASSWORD_NUM_FACTOR * passwordGeneratorGetSize(&passwordGenerator));
	ret = RT_generate((passwordEnumerator_t *) &randomPasswordEnumerator,
			  &passwordGenerator,
			  enumeratorPassword,
			  generatorPassword,
			  hashFunc,
			  chainLength,
			  prefix,
			  entiresInHashTable,
			  bucketBlockLength);

LBL_CLEANUP_GENERATOR:
	passwordGeneratorFinalize(&passwordGenerator);
LBL_CLEANUP_DICTIONARY:
	dictionaryFinalize(&dictionary);
LBL_ERROR:
	FREE(generatorPassword);
	FREE(enumeratorPassword);
	return ret;;
}