Improved RNG seeding on Windows Updated AES implementation Removed SHA1 implementation, replaced by QCryptographicHash Replaced ARC4 implementation by the one from KeePass 1.11 Some cleanup git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@216 b624d157-de02-0410-bad0-e51aec6abb33master
parent
455e68ff60
commit
636f3b8af6
@ -0,0 +1,404 @@ |
||||
/*
|
||||
** Copyright (c) 1999, 2000, 2001, 2002, 2003 |
||||
** Adel I. Mirzazhanov. All rights reserved |
||||
** |
||||
** Redistribution and use in source and binary forms, with or without |
||||
** modification, are permitted provided that the following conditions |
||||
** are met: |
||||
**
|
||||
** 1.Redistributions of source code must retain the above copyright notice, |
||||
** this list of conditions and the following disclaimer.
|
||||
** 2.Redistributions in binary form must reproduce the above copyright |
||||
** notice, this list of conditions and the following disclaimer in the |
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3.The name of the author may not be used to endorse or promote products |
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
||||
** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||||
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||||
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
||||
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||||
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
||||
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
#include <stdlib.h> |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include "random.h" |
||||
|
||||
#include "randpass.h" |
||||
#include "convert.h" |
||||
|
||||
/*
|
||||
** GLOBALS |
||||
*/ |
||||
|
||||
/* small letters */ |
||||
char let[26] = |
||||
{ |
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
||||
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', |
||||
'u', 'v', 'w', 'x', 'w', 'z' |
||||
}; |
||||
/* capital letters */ |
||||
char clet[26] = |
||||
{ |
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
||||
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
||||
'U', 'V', 'W', 'X', 'W', 'Z' |
||||
}; |
||||
|
||||
/*
|
||||
** FUNCTIONS |
||||
*/ |
||||
|
||||
/*
|
||||
** decapitalize() - This routine replaces all capital letters |
||||
** to small letters in the word: |
||||
** INPUT: |
||||
** char * - word. |
||||
** OUTPUT: |
||||
** none. |
||||
** NOTES: |
||||
** none. |
||||
*/ |
||||
void |
||||
decapitalize (char *word) |
||||
{ |
||||
int i = 0; /* counter */ |
||||
int j = 0; /* counter */ |
||||
int str_len = strlen(word); |
||||
for(j = 0; j < str_len; j++) |
||||
for(i=0; i < 26; i++) |
||||
if(word[j] == clet[i]) |
||||
word[j] = let[i]; |
||||
} |
||||
|
||||
#ifndef APGBFM |
||||
/*
|
||||
** capitalize() - This routine designed to modify sullable like this: |
||||
** adel ----> Adel |
||||
** dot ----> Dot |
||||
** etc. |
||||
** INPUT: |
||||
** char * - syllable. |
||||
** OUTPUT: |
||||
** none. |
||||
** NOTES: |
||||
** none. |
||||
*/ |
||||
void |
||||
capitalize (char *syllable) |
||||
{ |
||||
char tmp = 0x00; |
||||
int i = 0; |
||||
if ( randint(2) == TRUE) |
||||
{ |
||||
(void)memcpy((void *)&tmp, (void *)syllable, sizeof(tmp)); |
||||
for(i=0; i < 26; i++) |
||||
if ( let[i] == tmp ) |
||||
if (is_restricted_symbol(clet[i]) != TRUE) |
||||
(void)memcpy ((void *)syllable, (void *)&clet[i], 1); |
||||
} |
||||
} |
||||
|
||||
/*
|
||||
** numerize() - This routine designed to modify single-letter |
||||
** syllable like this: |
||||
** a ----> 1 or 2 or 3 etc. |
||||
** u ----> 1 or 2 or 3 etc. |
||||
** etc. |
||||
** INPUT: |
||||
** char * - single-letter syllable |
||||
** OUTPUT: |
||||
** none. |
||||
** NOTES: |
||||
** none. |
||||
*/ |
||||
void |
||||
numerize (char *syllable) |
||||
{ |
||||
char *tmp; |
||||
if ( (tmp = (char *)calloc(1, 4)) == NULL) |
||||
perror("calloc"); |
||||
if ( strlen (syllable) == 1 ) |
||||
{ |
||||
(void) gen_rand_symbol(tmp, S_NB); |
||||
(void)memcpy ((void *)syllable, (void *)tmp, 1); |
||||
} |
||||
free ((void *)tmp); |
||||
} |
||||
/*
|
||||
** specialize() - This routine designed to modify single-letter syllable |
||||
** like this: |
||||
** a ----> # or $ or % etc. |
||||
** u ----> # or $ or % etc. |
||||
** etc. |
||||
** INPUT: |
||||
** char * - single-letter syllable. |
||||
** OUTPUT: |
||||
** none. |
||||
** NOTES: |
||||
** none. |
||||
*/ |
||||
void |
||||
specialize (char *syllable) |
||||
{ |
||||
char *tmp; |
||||
if ( (tmp = (char *)calloc(1, 4)) == NULL) |
||||
perror("calloc"); |
||||
if ( strlen (syllable) == 1 ) |
||||
{ |
||||
(void) gen_rand_symbol(tmp, S_SS); |
||||
(void)memcpy ((void *)syllable, (void *)tmp, 1); |
||||
} |
||||
free ((void *)tmp); |
||||
} |
||||
|
||||
/*
|
||||
** symb2name - convert symbol to it's name |
||||
** INPUT: |
||||
** char * - one symbol syllable |
||||
** OUTPUT: |
||||
** none. |
||||
** NOTES: |
||||
** none. |
||||
*/ |
||||
void |
||||
symb2name(char * syllable, char * h_syllable) |
||||
{ |
||||
struct ssymb_names |
||||
{ |
||||
char symbol; |
||||
char *name; |
||||
}; |
||||
static struct ssymb_names ssn[42] = |
||||
{ |
||||
{'1',"ONE"}, |
||||
{'2',"TWO"}, |
||||
{'3',"THREE"}, |
||||
{'4',"FOUR"}, |
||||
{'5',"FIVE"}, |
||||
{'6',"SIX"}, |
||||
{'7',"SEVEN"}, |
||||
{'8',"EIGHT"}, |
||||
{'9',"NINE"}, |
||||
{'0',"ZERO"}, |
||||
{33, "EXCLAMATION_POINT"}, |
||||
{34, "QUOTATION_MARK"}, |
||||
{35, "CROSSHATCH"}, |
||||
{36, "DOLLAR_SIGN"}, |
||||
{37, "PERCENT_SIGN"}, |
||||
{38, "AMPERSAND"}, |
||||
{39, "APOSTROPHE"}, |
||||
{40, "LEFT_PARENTHESIS"}, |
||||
{41, "RIGHT_PARENTHESIS"}, |
||||
{42, "ASTERISK"}, |
||||
{43, "PLUS_SIGN"}, |
||||
{44, "COMMA"}, |
||||
{45, "HYPHEN"}, |
||||
{46, "PERIOD"}, |
||||
{47, "SLASH"}, |
||||
{58, "COLON"}, |
||||
{59, "SEMICOLON"}, |
||||
{60, "LESS_THAN"}, |
||||
{61, "EQUAL_SIGN"}, |
||||
{62, "GREATER_THAN"}, |
||||
{63, "QUESTION_MARK"}, |
||||
{64, "AT_SIGN"}, |
||||
{91, "LEFT_BRACKET"}, |
||||
{92, "BACKSLASH"}, |
||||
{93, "RIGHT_BRACKET"}, |
||||
{94, "CIRCUMFLEX"}, |
||||
{95, "UNDERSCORE"}, |
||||
{96, "GRAVE"}, |
||||
{123, "LEFT_BRACE"}, |
||||
{124, "VERTICAL_BAR"}, |
||||
{125, "RIGHT_BRACE"}, |
||||
{126, "TILDE"} |
||||
}; |
||||
int i = 0; |
||||
int flag = FALSE; |
||||
|
||||
if (strlen(syllable) == 1) |
||||
{ |
||||
for (i = 0; i < 42; i++) |
||||
{ |
||||
if(*syllable == ssn[i].symbol) |
||||
{ |
||||
(void)memcpy((void*)h_syllable, (void*)ssn[i].name, strlen(ssn[i].name)); |
||||
flag = TRUE; |
||||
} |
||||
} |
||||
if (flag != TRUE) |
||||
(void)memcpy((void*)h_syllable, (void*)syllable, strlen(syllable)); |
||||
} |
||||
} |
||||
|
||||
/*
|
||||
** spell_word - spell the word |
||||
** INPUT: |
||||
** char * - pointer to the word |
||||
** char * - pointer to the spelled word |
||||
** OUTPUT: |
||||
** char * - pointer to the spelled word |
||||
** NULL - something is wrong |
||||
** NOTES: |
||||
** You should free() memory pointed by spelled_word after each use of spell_word |
||||
*/ |
||||
char * |
||||
spell_word(char * word, char * spelled_word) |
||||
{ |
||||
struct char_spell |
||||
{ |
||||
char symbol; |
||||
char *name; |
||||
}; |
||||
static struct char_spell cs[94] = |
||||
{ |
||||
{'1',"ONE" }, |
||||
{'2',"TWO" }, |
||||
{'3',"THREE" }, |
||||
{'4',"FOUR" }, |
||||
{'5',"FIVE" }, |
||||
{'6',"SIX" }, |
||||
{'7',"SEVEN" }, |
||||
{'8',"EIGHT" }, |
||||
{'9',"NINE" }, |
||||
{'0',"ZERO" }, |
||||
{'A', "Alfa" }, |
||||
{'B', "Bravo" }, |
||||
{'C', "Charlie" }, |
||||
{'D', "Delta" }, |
||||
{'E', "Echo" }, |
||||
{'F', "Foxtrot" }, |
||||
{'G', "Golf" }, |
||||
{'H', "Hotel" }, |
||||
{'I', "India" }, |
||||
{'J', "Juliett" }, |
||||
{'K', "Kilo" }, |
||||
{'L', "Lima" }, |
||||
{'M', "Mike" }, |
||||
{'N', "November" }, |
||||
{'O', "Oscar" }, |
||||
{'P', "Papa" }, |
||||
{'Q', "Quebec" }, |
||||
{'R', "Romeo" }, |
||||
{'S', "Sierra" }, |
||||
{'T', "Tango" }, |
||||
{'U', "Uniform" }, |
||||
{'V', "Victor" }, |
||||
{'W', "Whiskey" }, |
||||
{'X', "X_ray" }, |
||||
{'Y', "Yankee" }, |
||||
{'Z', "Zulu" }, |
||||
{'a', "alfa" }, |
||||
{'b', "bravo" }, |
||||
{'c', "charlie" }, |
||||
{'d', "delta" }, |
||||
{'e', "echo" }, |
||||
{'f', "foxtrot" }, |
||||
{'g', "golf" }, |
||||
{'h', "hotel" }, |
||||
{'i', "india" }, |
||||
{'j', "juliett" }, |
||||
{'k', "kilo" }, |
||||
{'l', "lima" }, |
||||
{'m', "mike" }, |
||||
{'n', "november" }, |
||||
{'o', "oscar" }, |
||||
{'p', "papa" }, |
||||
{'q', "quebec" }, |
||||
{'r', "romeo" }, |
||||
{'s', "sierra" }, |
||||
{'t', "tango" }, |
||||
{'u', "uniform" }, |
||||
{'v', "victor" }, |
||||
{'w', "whiskey" }, |
||||
{'x', "x_ray" }, |
||||
{'y', "yankee" }, |
||||
{'z', "zulu" }, |
||||
{33, "EXCLAMATION_POINT"}, |
||||
{34, "QUOTATION_MARK" }, |
||||
{35, "CROSSHATCH" }, |
||||
{36, "DOLLAR_SIGN" }, |
||||
{37, "PERCENT_SIGN" }, |
||||
{38, "AMPERSAND" }, |
||||
{39, "APOSTROPHE" }, |
||||
{40, "LEFT_PARENTHESIS" }, |
||||
{41, "RIGHT_PARENTHESIS"}, |
||||
{42, "ASTERISK" }, |
||||
{43, "PLUS_SIGN" }, |
||||
{44, "COMMA" }, |
||||
{45, "HYPHEN" }, |
||||
{46, "PERIOD" }, |
||||
{47, "SLASH" }, |
||||
{58, "COLON" }, |
||||
{59, "SEMICOLON" }, |
||||
{60, "LESS_THAN" }, |
||||
{61, "EQUAL_SIGN" }, |
||||
{62, "GREATER_THAN" }, |
||||
{63, "QUESTION_MARK" }, |
||||
{64, "AT_SIGN" }, |
||||
{91, "LEFT_BRACKET" }, |
||||
{92, "BACKSLASH" }, |
||||
{93, "RIGHT_BRACKET" }, |
||||
{94, "CIRCUMFLEX" }, |
||||
{95, "UNDERSCORE" }, |
||||
{96, "GRAVE" }, |
||||
{123, "LEFT_BRACE" }, |
||||
{124, "VERTICAL_BAR" }, |
||||
{125, "RIGHT_BRACE" }, |
||||
{126, "TILDE" } |
||||
}; |
||||
int s_length = 0; |
||||
int i = 0; |
||||
int j = 0; |
||||
int word_len = strlen(word); |
||||
char * tmp_ptr; |
||||
char hyphen = '-'; |
||||
char zero = 0x00; |
||||
|
||||
/* Count the length of the spelled word */ |
||||
for (i=0; i <= word_len; i++) |
||||
for (j=0; j < 94; j++) |
||||
if (word[i] == cs[j].symbol) |
||||
{ |
||||
s_length = s_length + strlen(cs[j].name) + 1; |
||||
continue; |
||||
} |
||||
|
||||
/* Allocate memory for spelled word */ |
||||
if ( (spelled_word = (char *)calloc(1, (size_t)s_length)) == NULL) |
||||
return(NULL); |
||||
|
||||
/* Construct spelled word */ |
||||
tmp_ptr = spelled_word; |
||||
|
||||
for (i=0; i < word_len; i++) |
||||
for (j=0; j < 94; j++) |
||||
if (word[i] == cs[j].symbol) |
||||
{ |
||||
(void) memcpy((void *)tmp_ptr, (void *)cs[j].name, strlen(cs[j].name)); |
||||
tmp_ptr = tmp_ptr + strlen(cs[j].name); |
||||
/* Place the hyphen after each symbol */ |
||||
(void) memcpy((void *)(tmp_ptr), (void *)&hyphen, 1); |
||||
tmp_ptr = tmp_ptr + 1; |
||||
continue; |
||||
} |
||||
|
||||
/* Remove hyphen at the end of the word */ |
||||
tmp_ptr = tmp_ptr - 1; |
||||
(void) memcpy((void *)(tmp_ptr), (void *)&zero, 1); |
||||
|
||||
return (spelled_word); |
||||
} |
||||
|
||||
#endif /* APGBFM */ |
@ -0,0 +1,43 @@ |
||||
/*
|
||||
** Copyright (c) 1999, 2000, 2001, 2002, 2003 |
||||
** Adel I. Mirzazhanov. All rights reserved |
||||
** |
||||
** Redistribution and use in source and binary forms, with or without |
||||
** modification, are permitted provided that the following conditions |
||||
** are met: |
||||
**
|
||||
** 1.Redistributions of source code must retain the above copyright notice, |
||||
** this list of conditions and the following disclaimer.
|
||||
** 2.Redistributions in binary form must reproduce the above copyright |
||||
** notice, this list of conditions and the following disclaimer in the |
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3.The name of the author may not be used to endorse or promote products |
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
||||
** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||||
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||||
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
||||
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||||
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
||||
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
#ifndef APG_CONVERT_H |
||||
#define APG_CONVERT_H 1 |
||||
|
||||
void decapitalize (char *word); |
||||
|
||||
#ifndef APGBFM |
||||
void capitalize (char *syllable); |
||||
void numerize (char *syllable); |
||||
void specialize (char *syllable); |
||||
void symb2name(char * syllable, char * h_syllable); |
||||
char* spell_word(char * word, char * spelled_word); |
||||
#endif /* APGBFM */ |
||||
|
||||
#endif /* APG_CONVERT_H */ |
@ -0,0 +1,49 @@ |
||||
/*
|
||||
** Copyright (c) 1999, 2000, 2001, 2002, 2003 |
||||
** Adel I. Mirzazhanov. All rights reserved |
||||
** |
||||
** Redistribution and use in source and binary forms, with or without |
||||
** modification, are permitted provided that the following conditions |
||||
** are met: |
||||
**
|
||||
** 1.Redistributions of source code must retain the above copyright notice, |
||||
** this list of conditions and the following disclaimer.
|
||||
** 2.Redistributions in binary form must reproduce the above copyright |
||||
** notice, this list of conditions and the following disclaimer in the |
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3.The name of the author may not be used to endorse or promote products |
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
||||
** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||||
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||||
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
||||
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||||
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
||||
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
#ifndef APG_OWN_TYPES_H |
||||
#define APG_OWN_TYPES_H 1 |
||||
|
||||
typedef unsigned int UINT; |
||||
typedef unsigned short USHORT; |
||||
typedef short int SHORT; |
||||
typedef int boolean; |
||||
typedef unsigned long int UINT32; |
||||
|
||||
#ifndef TRUE |
||||
#define TRUE 1 |
||||
#endif |
||||
|
||||
#ifndef FALSE |
||||
#define FALSE 0 |
||||
#endif |
||||
|
||||
#define APG_MAX_PASSWORD_LENGTH 255 |
||||
|
||||
#endif /* APG_OWN_TYPES_H */ |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,92 @@ |
||||
/*
|
||||
** This module uses code from the NIST implementation of FIPS-181, |
||||
** but the algorythm is CHANGED and I think that I CAN |
||||
** copyright it. See copiright notes below. |
||||
*/ |
||||
|
||||
/*
|
||||
** Copyright (c) 1999, 2000, 2001, 2002, 2003 |
||||
** Adel I. Mirzazhanov. All rights reserved |
||||
** |
||||
** Redistribution and use in source and binary forms, with or without |
||||
** modification, are permitted provided that the following conditions |
||||
** are met: |
||||
**
|
||||
** 1.Redistributions of source code must retain the above copyright notice, |
||||
** this list of conditions and the following disclaimer.
|
||||
** 2.Redistributions in binary form must reproduce the above copyright |
||||
** notice, this list of conditions and the following disclaimer in the |
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3.The name of the author may not be used to endorse or promote products |
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
||||
** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||||
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||||
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
||||
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||||
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
||||
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
|
||||
#ifndef APG_PRONPASS_H |
||||
#define APG_PRONPASS_H 1 |
||||
|
||||
#ifndef APG_OWN_TYPES_H |
||||
#include "owntypes.h" |
||||
#endif /* APG_OWN_TYPES_H */ |
||||
|
||||
//#ifndef APG_RND_H
|
||||
//#include "rnd.h"
|
||||
//#endif /* APG_RND_H */
|
||||
|
||||
#define RULE_SIZE (sizeof(rules)/sizeof(struct unit)) |
||||
#define ALLOWED(flag) (digram[units_in_syllable[current_unit -1]][unit] & (flag)) |
||||
|
||||
#define MAX_UNACCEPTABLE 20 |
||||
#define MAX_RETRIES (4 * (int) pwlen + RULE_SIZE) |
||||
|
||||
#define NOT_BEGIN_SYLLABLE 010 |
||||
#define NO_FINAL_SPLIT 04 |
||||
#define VOWEL 02 |
||||
#define ALTERNATE_VOWEL 01 |
||||
#define NO_SPECIAL_RULE 0 |
||||
|
||||
#define BEGIN 0200 |
||||
#define NOT_BEGIN 0100 |
||||
#define BREAK 040 |
||||
#define PREFIX 020 |
||||
#define ILLEGAL_PAIR 010 |
||||
#define SUFFIX 04 |
||||
#define END 02 |
||||
#define NOT_END 01 |
||||
#define ANY_COMBINATION 0 |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
extern int gen_pron_pass (char *word, char *hyphenated_word, USHORT minlen, |
||||
USHORT maxlen, unsigned int pass_mode); |
||||
|
||||
USHORT random_unit (USHORT type); |
||||
USHORT get_random (USHORT minlen, USHORT maxlen); |
||||
boolean have_initial_y (USHORT *units, USHORT unit_size); |
||||
boolean illegal_placement (USHORT *units, USHORT pwlen); |
||||
boolean improper_word (USHORT *units, USHORT word_size); |
||||
boolean have_final_split (USHORT *units, USHORT unit_size); |
||||
int gen_word (char *word, char *hyphenated_word, USHORT pwlen, |
||||
unsigned int pass_mode); |
||||
char *gen_syllable(char *syllable, USHORT pwlen, USHORT *units_in_syllable, |
||||
USHORT *syllable_length); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* APG_PRONPASS_H */ |
@ -0,0 +1,162 @@ |
||||
/*
|
||||
** Copyright (c) 1999, 2000, 2001, 2002, 2003 |
||||
** Adel I. Mirzazhanov. All rights reserved |
||||
** |
||||
** Redistribution and use in source and binary forms, with or without |
||||
** modification, are permitted provided that the following conditions |
||||
** are met: |
||||
**
|
||||
** 1.Redistributions of source code must retain the above copyright notice, |
||||
** this list of conditions and the following disclaimer.
|
||||
** 2.Redistributions in binary form must reproduce the above copyright |
||||
** notice, this list of conditions and the following disclaimer in the |
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3.The name of the author may not be used to endorse or promote products |
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
||||
** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||||
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||||
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
||||
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||||
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
||||
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
/*
|
||||
** randpass.c - Random password generation module of PWGEN program |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <time.h> |
||||
#include <unistd.h> |
||||
|
||||
#include "random.h" |
||||
|
||||
#include "randpass.h" |
||||
#include "owntypes.h" |
||||
#include "smbl.h" |
||||
|
||||
/*
|
||||
** gen_rand_pass - generates random password of specified type |
||||
** INPUT: |
||||
** char * - password string. |
||||
** int - minimum password length. |
||||
** int - maximum password length. |
||||
** unsigned int - password generation mode. |
||||
** OUTPUT: |
||||
** int - password length or -1 on error. |
||||
** NOTES: |
||||
** none. |
||||
*/ |
||||
int |
||||
gen_rand_pass (char *password_string, int minl, int maxl, unsigned int pass_mode) |
||||
{ |
||||
int i = 0; |
||||
int j = 0; |
||||
int length = 0; |
||||
char *str_pointer; |
||||
int random_weight[94]; |
||||
int max_weight = 0; |
||||
int max_weight_element_number = 0; |
||||
|
||||
if (minl > APG_MAX_PASSWORD_LENGTH || maxl > APG_MAX_PASSWORD_LENGTH || |
||||
minl < 1 || maxl < 1 || minl > maxl) |
||||
return (-1); |
||||
for (i = 0; i <= 93; i++) random_weight[i] = 0;
|
||||
length = minl + randint(maxl-minl+1); |
||||
str_pointer = password_string; |
||||
|
||||
for (i = 0; i < length; i++) |
||||
{ |
||||
/* Asign random weight in weight array if mode is present*/ |
||||
for (j = 0; j <= 93 ; j++) |
||||
if ( ( (pass_mode & smbl[j].type) > 0) && |
||||
!( (S_RS & smbl[j].type) > 0)) |
||||
random_weight[j] = 1 + randint(20000); |
||||
j = 0; |
||||
/* Find an element with maximum weight */ |
||||
for (j = 0; j <= 93; j++) |
||||
if (random_weight[j] > max_weight) |
||||
{ |
||||
max_weight = random_weight[j]; |
||||
max_weight_element_number = j; |
||||
} |
||||
/* Get password symbol */ |
||||
*str_pointer = smbl[max_weight_element_number].ch; |
||||
str_pointer++; |
||||
max_weight = 0; |
||||
max_weight_element_number = 0; |
||||
for (j = 0; j <= 93; j++) random_weight[j] = 0; |
||||
} |
||||
*str_pointer = 0; |
||||
return (length); |
||||
} |
||||
|
||||
/*
|
||||
** gen_rand_symbol - generates random password of specified type |
||||
** INPUT: |
||||
** char * - symbol. |
||||
** unsigned int - symbol type. |
||||
** OUTPUT: |
||||
** int - password length or -1 on error. |
||||
** NOTES: |
||||
** none. |
||||
*/ |
||||
int |
||||
gen_rand_symbol (char *symbol, unsigned int mode) |
||||
{ |
||||
int j = 0; |
||||
char *str_pointer; |
||||
int random_weight[94]; |
||||
int max_weight = 0; |
||||
int max_weight_element_number = 0; |
||||
|
||||
for (j = 0; j <= 93; j++) random_weight[j] = 0;
|
||||
str_pointer = symbol; |
||||
j = 0; |
||||
/* Asign random weight in weight array if mode is present*/ |
||||
for (j = 0; j <= 93 ; j++) |
||||
if ( ( (mode & smbl[j].type) > 0) && |
||||
!( (S_RS & smbl[j].type) > 0)) |
||||
random_weight[j] = 1 + randint(20000); |
||||
j = 0; |
||||
/* Find an element with maximum weight */ |
||||
for (j = 0; j <= 93; j++) |
||||
if (random_weight[j] > max_weight) |
||||
{ |
||||
max_weight = random_weight[j]; |
||||
max_weight_element_number = j; |
||||
} |
||||
/* Get password symbol */ |
||||
*str_pointer = smbl[max_weight_element_number].ch; |
||||
max_weight = 0; |
||||
max_weight_element_number = 0; |
||||
return (0); |
||||
} |
||||
|
||||
/*
|
||||
** is_restricted_symbol - detcts if symbol is restricted rigt now |
||||
** INPUT: |
||||
** char - symbol. |
||||
** OUTPUT: |
||||
** int - 0 - not restricted |
||||
** 1 - restricted |
||||
** NOTES: |
||||
** none. |
||||
*/ |
||||
int |
||||
is_restricted_symbol (char symbol) |
||||
{ |
||||
int j = 0; |
||||
for (j = 0; j <= 93 ; j++) |
||||
if (symbol == smbl[j].ch) |
||||
if ((S_RS & smbl[j].type) > 0) |
||||
return(1); |
||||
return(0); |
||||
} |
@ -0,0 +1,70 @@ |
||||
/*
|
||||
** Copyright (c) 1999, 2000, 2001, 2002, 2003 |
||||
** Adel I. Mirzazhanov. All rights reserved |
||||
** |
||||
** Redistribution and use in source and binary forms, with or without |
||||
** modification, are permitted provided that the following conditions |
||||
** are met: |
||||
**
|
||||
** 1.Redistributions of source code must retain the above copyright notice, |
||||
** this list of conditions and the following disclaimer.
|
||||
** 2.Redistributions in binary form must reproduce the above copyright |
||||
** notice, this list of conditions and the following disclaimer in the |
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3.The name of the author may not be used to endorse or promote products |
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
||||
** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||||
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||||
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
||||
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||||
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
||||
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
/*
|
||||
** randpass.h |
||||
*/ |
||||
#ifndef APG_RANDPASS_H |
||||
#define APG_RANDPASS_H 1 |
||||
|
||||
/*#ifndef APG_RND_H
|
||||
#include "rnd.h" |
||||
#endif*/ |
||||
|
||||
#ifndef APG_OWN_TYPES_H |
||||
#include "owntypes.h" |
||||
#endif |
||||
|
||||
#define S_NB 0x01 /* Numeric */ |
||||
#define S_SS 0x02 /* Special */ |
||||
#define S_CL 0x04 /* Capital */ |
||||
#define S_SL 0x08 /* Small */ |
||||
#define S_RS 0x10 /* Restricted Symbol*/ |
||||
|
||||
struct sym |
||||
{ |
||||
char ch; |
||||
USHORT type; |
||||
}; |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/* char gen_symbol(unsigned short int symbol_class); */ |
||||
extern int gen_rand_pass(char* password_string, int minl, |
||||
int maxl, unsigned int pass_mode); |
||||
extern int gen_rand_symbol (char *symbol, unsigned int mode); |
||||
extern int is_restricted_symbol (char symbol); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* RANDPASS_H */ |
@ -0,0 +1,51 @@ |
||||
/*
|
||||
** Copyright (c) 1999, 2000, 2001, 2002, 2003 |
||||
** Adel I. Mirzazhanov. All rights reserved |
||||
** |
||||
** Redistribution and use in source and binary forms, with or without |
||||
** modification, are permitted provided that the following conditions |
||||
** are met: |
||||
**
|
||||
** 1.Redistributions of source code must retain the above copyright notice, |
||||
** this list of conditions and the following disclaimer.
|
||||
** 2.Redistributions in binary form must reproduce the above copyright |
||||
** notice, this list of conditions and the following disclaimer in the |
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3.The name of the author may not be used to endorse or promote products |
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
||||
** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||||
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||||
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
||||
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||||
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
||||
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
#ifndef APG_SMBL_H |
||||
#define APG_SMBL_H 1 |
||||
struct sym smbl[94] =
|
||||
{ |
||||
{'a', S_SL}, {'b', S_SL}, {'c', S_SL}, {'d', S_SL}, {'e', S_SL}, {'f', S_SL}, |
||||
{'g', S_SL}, {'h', S_SL}, {'i', S_SL}, {'j', S_SL}, {'k', S_SL}, {'l', S_SL}, |
||||
{'m', S_SL}, {'n', S_SL}, {'o', S_SL}, {'p', S_SL}, {'q', S_SL}, {'r', S_SL}, |
||||
{'s', S_SL}, {'t', S_SL}, {'u', S_SL}, {'v', S_SL}, {'w', S_SL}, {'x', S_SL}, |
||||
{'y', S_SL}, {'z', S_SL}, {'A', S_CL}, {'B', S_CL}, {'C', S_CL}, {'D', S_CL}, |
||||
{'E', S_CL}, {'F', S_CL}, {'G', S_CL}, {'H', S_CL}, {'I', S_CL}, {'J', S_CL}, |
||||
{'K', S_CL}, {'L', S_CL}, {'M', S_CL}, {'N', S_CL}, {'O', S_CL}, {'P', S_CL}, |
||||
{'Q', S_CL}, {'R', S_CL}, {'S', S_CL}, {'T', S_CL}, {'U', S_CL}, {'V', S_CL}, |
||||
{'W', S_CL}, {'X', S_CL}, {'Y', S_CL}, {'Z', S_CL}, {'1', S_NB}, {'2', S_NB}, |
||||
{'3', S_NB}, {'4', S_NB}, {'5', S_NB}, {'6', S_NB}, {'7', S_NB}, {'8', S_NB}, |
||||
{'9', S_NB}, {'0', S_NB}, {33 , S_SS}, {34 , S_SS}, {35 , S_SS}, {36 , S_SS}, |
||||
{37 , S_SS}, {38 , S_SS}, {39 , S_SS}, {40 , S_SS}, {41 , S_SS}, {42 , S_SS}, |
||||
{43 , S_SS}, {44 , S_SS}, {45 , S_SS}, {46 , S_SS}, {47 , S_SS}, {58 , S_SS}, |
||||
{59 , S_SS}, {60 , S_SS}, {61 , S_SS}, {62 , S_SS}, {63 , S_SS}, {64 , S_SS}, |
||||
{91 , S_SS}, {92 , S_SS}, {93 , S_SS}, {94 , S_SS}, {95 , S_SS}, {96 , S_SS}, |
||||
{123, S_SS}, {124, S_SS}, {125, S_SS}, {126, S_SS} |
||||
}; |
||||
|
||||
#endif /* APG_SMBL_H */ |
@ -1,84 +1,58 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2005-2006 by Tarek Saidi * |
||||
* tarek.saidi@arcor.de * |
||||
* * |
||||
* 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; version 2 of the License. * |
||||
/*
|
||||
Copyright (C) 2003-2008 Dominik Reichl <dominik.reichl@t-online.de> |
||||
|
||||
* * |
||||
* 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, write to the * |
||||
* Free Software Foundation, Inc., * |
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#include <QByteArray> |
||||
#include "arcfour.h" |
||||
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 2 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. |
||||
|
||||
static inline void swap_byte(unsigned char *a, unsigned char *b) |
||||
{ |
||||
unsigned char swapByte; |
||||
You should have received a copy of the GNU General Public License |
||||
along with this program; if not, write to the Free Software |
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
||||
*/ |
||||
|
||||
swapByte = *a; |
||||
*a = *b;
|
||||
*b = swapByte; |
||||
}
|
||||
#include "arcfour.h" |
||||
|
||||
void CArcFour::setKey(byte* key_data_ptr, int key_data_len){ |
||||
RawKey=QByteArray((const char*)key_data_ptr,key_data_len); |
||||
void CArcFour::setKey(quint8* key, int length){ |
||||
RawKey = key; |
||||
RawKeyLength = length; |
||||
} |
||||
|
||||
void CArcFour::prepareKey(){ |
||||
unsigned char index1; |
||||
unsigned char index2; |
||||
unsigned char* state; |
||||
short counter;
|
||||
void CArcFour::encrypt(const quint8* src, quint8* dst, int length){ |
||||
quint8 S[256]; |
||||
quint32 w; |
||||
|
||||
state = &key.state[0];
|
||||
for(counter = 0; counter < 256; counter++)
|
||||
state[counter] = counter;
|
||||
key.x = 0;
|
||||
key.y = 0;
|
||||
index1 = 0;
|
||||
index2 = 0;
|
||||
for(counter = 0; counter < 256; counter++)
|
||||
{
|
||||
index2 = (RawKey.at(index1) + state[counter] + index2) % 256;
|
||||
swap_byte(&state[counter], &state[index2]);
|
||||
index1 = (index1 + 1) % RawKey.size();
|
||||
}
|
||||
} |
||||
for(w = 0; w < 256; ++w) |
||||
S[w] = static_cast<quint8>(w); // Fill linearly
|
||||
|
||||
void CArcFour::encrypt(const byte* src, byte* dst,int length){ |
||||
prepareKey(); |
||||
unsigned char x; |
||||
unsigned char y; |
||||
unsigned char* state; |
||||
unsigned char xorIndex; |
||||
short counter;
|
||||
const quint8 btBufDep = static_cast<quint8>((length & 0xFF) << 1); |
||||
|
||||
x = key.x;
|
||||
y = key.y;
|
||||
quint8 i = 0, j = 0, t; |
||||
quint32 k = 0; |
||||
for(w = 0; w < 256; ++w) // Key setup
|
||||
{ |
||||
j += S[w] + RawKey[k] + btBufDep; |
||||
|
||||
state = &key.state[0];
|
||||
for(counter = 0; counter < length; counter ++)
|
||||
{
|
||||
x = (x + 1) % 256;
|
||||
y = (state[x] + y) % 256;
|
||||
swap_byte(&state[x], &state[y]);
|
||||
t = S[i]; S[i] = S[j]; S[j] = t; // Swap entries
|
||||
|
||||
xorIndex = (state[x] + state[y]) % 256;
|
||||
++k; |
||||
if(k == RawKeyLength) k = 0; |
||||
} |
||||
|
||||
dst[counter]=src[counter]^state[xorIndex];
|
||||
}
|
||||
key.x = x;
|
||||
key.y = y; |
||||
} |
||||
i = 0; j = 0; |
||||
for(w = 0; w < length; ++w) // Encrypt PT
|
||||
{ |
||||
++i; |
||||
j += S[i]; |
||||
|
||||
t = S[i]; S[i] = S[j]; S[j] = t; // Swap entries
|
||||
|
||||
t = S[i] + S[j]; // Generate random byte
|
||||
dst[w] = src[w] ^ S[t]; // XOR with PT
|
||||
} |
||||
} |
||||
|
@ -1,45 +1,33 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2005-2006 by Tarek Saidi * |
||||
* tarek.saidi@arcor.de * |
||||
* * |
||||
* 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; version 2 of the License. * |
||||
/*
|
||||
Copyright (C) 2003-2008 Dominik Reichl <dominik.reichl@t-online.de> |
||||
|
||||
* * |
||||
* 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, write to the * |
||||
* Free Software Foundation, Inc., * |
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||||
***************************************************************************/ |
||||
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 2 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, write to the Free Software |
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
||||
*/ |
||||
|
||||
#ifndef _ARCFOUR_H_ |
||||
#define _ARCFOUR_H_ |
||||
|
||||
#ifndef byte |
||||
#define byte quint8 |
||||
#endif |
||||
|
||||
class CArcFour{ |
||||
public: |
||||
void encrypt(const byte* src, byte* dst,int length); |
||||
inline void decrypt(const byte* src, byte* dst,int length){encrypt(src,dst,length);} //just for readability
|
||||
void setKey(byte* key, int length); |
||||
QByteArray RawKey; |
||||
private: |
||||
void prepareKey(); |
||||
public: |
||||
void encrypt(const quint8* src, quint8* dst, int length); |
||||
inline void decrypt(const quint8* src, quint8* dst, int length){encrypt(src,dst,length);} //just for readability
|
||||
void setKey(quint8* key, int length); |
||||
|
||||
typedef struct rc4_key{
|
||||
byte state[256];
|
||||
byte x;
|
||||
byte y;}rc4_key; |
||||
rc4_key key; |
||||
private: |
||||
quint8* RawKey; |
||||
int RawKeyLength; |
||||
}; |
||||
|
||||
|
||||
#endif |
||||
|
@ -1,258 +0,0 @@ |
||||
/***************************************************************************
|
||||
* Implementation of the SHA-1 algorithm * |
||||
* by Dominik Reichl <dominik.reichl@t-online.de> (public domain) * |
||||
* * |
||||
* Copyright (C) 2007 by Tarek Saidi * |
||||
* tarek.saidi@arcor.de * |
||||
* * |
||||
* 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; version 2 of the License. * |
||||
* * |
||||
* 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, write to the * |
||||
* Free Software Foundation, Inc., * |
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#include "global.h" |
||||
#include "sha1.h" |
||||
|
||||
#define SHA1_MAX_FILE_BUFFER 8000 |
||||
|
||||
// Rotate x bits to the left
|
||||
#ifndef ROL32 |
||||
#define ROL32(_val32, _nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits)))) |
||||
#endif |
||||
|
||||
#ifdef KEEPASS_LITTLE_ENDIAN |
||||
#define SHABLK0(i) (m_block->l[i] = \ |
||||
(ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF)) |
||||
#else |
||||
#define SHABLK0(i) (m_block->l[i]) |
||||
#endif |
||||
|
||||
#define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \ |
||||
^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1)) |
||||
|
||||
// SHA-1 rounds
|
||||
#define _R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); } |
||||
#define _R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); } |
||||
#define _R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); } |
||||
#define _R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); } |
||||
#define _R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); } |
||||
|
||||
CSHA1::CSHA1() |
||||
{ |
||||
m_block = (SHA1_WORKSPACE_BLOCK *)m_workspace; |
||||
|
||||
Reset(); |
||||
} |
||||
|
||||
CSHA1::~CSHA1() |
||||
{ |
||||
Reset(); |
||||
} |
||||
|
||||
void CSHA1::Reset() |
||||
{ |
||||
// SHA1 initialization constants
|
||||
m_state[0] = 0x67452301; |
||||
m_state[1] = 0xEFCDAB89; |
||||
m_state[2] = 0x98BADCFE; |
||||
m_state[3] = 0x10325476; |
||||
m_state[4] = 0xC3D2E1F0; |
||||
|
||||
m_count[0] = 0; |
||||
m_count[1] = 0; |
||||
} |
||||
void CSHA1::Update(unsigned char* data, int len){ |
||||
|
||||
quint_32 i, j; |
||||
|
||||
j = (m_count[0] >> 3) & 63; |
||||
|
||||
if((m_count[0] += len << 3) < (len << 3)) m_count[1]++; |
||||
|
||||
m_count[1] += (len >> 29); |
||||
|
||||
if((j + len) > 63) |
||||
{ |
||||
i = 64 - j; |
||||
memcpy(&m_buffer[j], data, i); |
||||
Transform(m_state, m_buffer); |
||||
|
||||
for( ; i + 63 < len; i += 64) Transform(m_state, &data[i]); |
||||
|
||||
j = 0; |
||||
} |
||||
else i = 0; |
||||
|
||||
memcpy(&m_buffer[j], &data[i], len - i); |
||||
|
||||
|
||||
} |
||||
|
||||
|
||||
void CSHA1::Transform(quint_32 *state, quint_8 *buffer) |
||||
{ |
||||
// Copy state[] to working vars
|
||||
quint_32 a = state[0], b = state[1], c = state[2], d = state[3], e = state[4]; |
||||
|
||||
memcpy(m_block, buffer, 64); |
||||
|
||||
// 4 rounds of 20 operations each. Loop unrolled.
|
||||
_R0(a,b,c,d,e, 0); _R0(e,a,b,c,d, 1); _R0(d,e,a,b,c, 2); _R0(c,d,e,a,b, 3); |
||||
_R0(b,c,d,e,a, 4); _R0(a,b,c,d,e, 5); _R0(e,a,b,c,d, 6); _R0(d,e,a,b,c, 7); |
||||
_R0(c,d,e,a,b, 8); _R0(b,c,d,e,a, 9); _R0(a,b,c,d,e,10); _R0(e,a,b,c,d,11); |
||||
_R0(d,e,a,b,c,12); _R0(c,d,e,a,b,13); _R0(b,c,d,e,a,14); _R0(a,b,c,d,e,15); |
||||
_R1(e,a,b,c,d,16); _R1(d,e,a,b,c,17); _R1(c,d,e,a,b,18); _R1(b,c,d,e,a,19); |
||||
_R2(a,b,c,d,e,20); _R2(e,a,b,c,d,21); _R2(d,e,a,b,c,22); _R2(c,d,e,a,b,23); |
||||
_R2(b,c,d,e,a,24); _R2(a,b,c,d,e,25); _R2(e,a,b,c,d,26); _R2(d,e,a,b,c,27); |
||||
_R2(c,d,e,a,b,28); _R2(b,c,d,e,a,29); _R2(a,b,c,d,e,30); _R2(e,a,b,c,d,31); |
||||
_R2(d,e,a,b,c,32); _R2(c,d,e,a,b,33); _R2(b,c,d,e,a,34); _R2(a,b,c,d,e,35); |
||||
_R2(e,a,b,c,d,36); _R2(d,e,a,b,c,37); _R2(c,d,e,a,b,38); _R2(b,c,d,e,a,39); |
||||
_R3(a,b,c,d,e,40); _R3(e,a,b,c,d,41); _R3(d,e,a,b,c,42); _R3(c,d,e,a,b,43); |
||||
_R3(b,c,d,e,a,44); _R3(a,b,c,d,e,45); _R3(e,a,b,c,d,46); _R3(d,e,a,b,c,47); |
||||
_R3(c,d,e,a,b,48); _R3(b,c,d,e,a,49); _R3(a,b,c,d,e,50); _R3(e,a,b,c,d,51); |
||||
_R3(d,e,a,b,c,52); _R3(c,d,e,a,b,53); _R3(b,c,d,e,a,54); _R3(a,b,c,d,e,55); |
||||
_R3(e,a,b,c,d,56); _R3(d,e,a,b,c,57); _R3(c,d,e,a,b,58); _R3(b,c,d,e,a,59); |
||||
_R4(a,b,c,d,e,60); _R4(e,a,b,c,d,61); _R4(d,e,a,b,c,62); _R4(c,d,e,a,b,63); |
||||
_R4(b,c,d,e,a,64); _R4(a,b,c,d,e,65); _R4(e,a,b,c,d,66); _R4(d,e,a,b,c,67); |
||||
_R4(c,d,e,a,b,68); _R4(b,c,d,e,a,69); _R4(a,b,c,d,e,70); _R4(e,a,b,c,d,71); |
||||
_R4(d,e,a,b,c,72); _R4(c,d,e,a,b,73); _R4(b,c,d,e,a,74); _R4(a,b,c,d,e,75); |
||||
_R4(e,a,b,c,d,76); _R4(d,e,a,b,c,77); _R4(c,d,e,a,b,78); _R4(b,c,d,e,a,79); |
||||
|
||||
// Add the working vars back into state
|
||||
state[0] += a; |
||||
state[1] += b; |
||||
state[2] += c; |
||||
state[3] += d; |
||||
state[4] += e; |
||||
|
||||
// Wipe variables
|
||||
#ifdef SHA1_WIPE_VARIABLES |
||||
a = b = c = d = e = 0; |
||||
#endif |
||||
} |
||||
|
||||
|
||||
// Hash in file contents
|
||||
bool CSHA1::HashFile(char *szFileName) |
||||
{ |
||||
unsigned long ulFileSize, ulRest, ulBlocks; |
||||
unsigned long i; |
||||
quint_8 uData[SHA1_MAX_FILE_BUFFER]; |
||||
FILE *fIn; |
||||
|
||||
if(szFileName == NULL) return false; |
||||
|
||||
fIn = fopen(szFileName, "rb"); |
||||
if(fIn == NULL) return false; |
||||
|
||||
fseek(fIn, 0, SEEK_END); |
||||
ulFileSize = (unsigned long)ftell(fIn); |
||||
fseek(fIn, 0, SEEK_SET); |
||||
|
||||
if(ulFileSize != 0) |
||||
{ |
||||
ulBlocks = ulFileSize / SHA1_MAX_FILE_BUFFER; |
||||
ulRest = ulFileSize % SHA1_MAX_FILE_BUFFER; |
||||
} |
||||
else |
||||
{ |
||||
ulBlocks = 0; |
||||
ulRest = 0; |
||||
} |
||||
|
||||
for(i = 0; i < ulBlocks; i++) |
||||
{ |
||||
fread(uData, 1, SHA1_MAX_FILE_BUFFER, fIn); |
||||
Update((quint_8 *)uData, SHA1_MAX_FILE_BUFFER); |
||||
} |
||||
|
||||
if(ulRest != 0) |
||||
{ |
||||
fread(uData, 1, ulRest, fIn); |
||||
Update((quint_8 *)uData, ulRest); |
||||
} |
||||
|
||||
fclose(fIn); fIn = NULL; |
||||
return true; |
||||
} |
||||
|
||||
void CSHA1::Final() |
||||
{ |
||||
quint_32 i; |
||||
quint_8 finalcount[8]; |
||||
|
||||
for(i = 0; i < 8; i++) |
||||
finalcount[i] = (quint_8)((m_count[((i >= 4) ? 0 : 1)] |
||||
>> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
|
||||
|
||||
Update((quint_8 *)"\200", 1); |
||||
|
||||
while ((m_count[0] & 504) != 448) |
||||
Update((quint_8 *)"\0", 1); |
||||
|
||||
Update(finalcount, 8); // Cause a SHA1Transform()
|
||||
|
||||
for(i = 0; i < 20; i++) |
||||
{ |
||||
m_digest[i] = (quint_8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255); |
||||
} |
||||
|
||||
// Wipe variables for security reasons
|
||||
#ifdef SHA1_WIPE_VARIABLES |
||||
i = 0; |
||||
memset(m_buffer, 0, 64); |
||||
memset(m_state, 0, 20); |
||||
memset(m_count, 0, 8); |
||||
memset(finalcount, 0, 8); |
||||
Transform(m_state, m_buffer); |
||||
#endif |
||||
} |
||||
|
||||
// Get the final hash as a pre-formatted string
|
||||
void CSHA1::ReportHash(char *szReport, unsigned char uReportType) |
||||
{ |
||||
unsigned char i; |
||||
char szTemp[16]; |
||||
|
||||
if(szReport == NULL) return; |
||||
|
||||
if(uReportType == REPORT_HEX) |
||||
{ |
||||
sprintf(szTemp, "%02X", m_digest[0]); |
||||
strcat(szReport, szTemp); |
||||
|
||||
for(i = 1; i < 20; i++) |
||||
{ |
||||
sprintf(szTemp, " %02X", m_digest[i]); |
||||
strcat(szReport, szTemp); |
||||
} |
||||
} |
||||
else if(uReportType == REPORT_DIGIT) |
||||
{ |
||||
sprintf(szTemp, "%u", m_digest[0]); |
||||
strcat(szReport, szTemp); |
||||
|
||||
for(i = 1; i < 20; i++) |
||||
{ |
||||
sprintf(szTemp, " %u", m_digest[i]); |
||||
strcat(szReport, szTemp); |
||||
} |
||||
} |
||||
else strcpy(szReport, "Error: Unknown report type!"); |
||||
} |
||||
|
||||
// Get the raw message digest
|
||||
void CSHA1::GetHash(quint_8 *puDest) |
||||
{ |
||||
memcpy(puDest, m_digest, 20); |
||||
} |
@ -1,119 +0,0 @@ |
||||
/***************************************************************************
|
||||
* Implementation of the SHA-1 algorithm * |
||||
* by Dominik Reichl <dominik.reichl@t-online.de> (no copyright) * |
||||
* * |
||||
* Copyright (C) 2007 by Tarek Saidi * |
||||
* tarek.saidi@arcor.de * |
||||
* * |
||||
* 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; version 2 of the License. * |
||||
* * |
||||
* 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, write to the * |
||||
* Free Software Foundation, Inc., * |
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||||
***************************************************************************/ |
||||
|
||||
|
||||
/*
|
||||
Version 1.5 - 2005-01-01 |
||||
- 64-bit compiler compatibility added |
||||
- Made variable wiping optional (define SHA1_WIPE_VARIABLES) |
||||
- Removed unnecessary variable initializations |
||||
- ROL32 improvement for the Microsoft compiler (using _rotl) |
||||
|
||||
======== Test Vectors (from FIPS PUB 180-1) ======== |
||||
|
||||
SHA1("abc") = |
||||
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D |
||||
|
||||
SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = |
||||
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 |
||||
|
||||
SHA1(A million repetitions of "a") = |
||||
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F |
||||
*/ |
||||
|
||||
#ifndef ___SHA1_HDR___ |
||||
#define ___SHA1_HDR___ |
||||
|
||||
#include <stdio.h> // Needed for file access |
||||
#include <memory.h> // Needed for memset and memcpy |
||||
#include <string.h> // Needed for strcat and strcpy |
||||
|
||||
|
||||
// If you're compiling big endian, just comment out the following line
|
||||
|
||||
|
||||
// #define or #undef this, if you want the CSHA1 class to wipe all
|
||||
// temporary variables after processing
|
||||
#define SHA1_WIPE_VARIABLES |
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Define 8- and 32-bit variables
|
||||
|
||||
#ifndef quint_32 |
||||
#define quint_8 unsigned char |
||||
#if (ULONG_MAX == 0xFFFFFFFF) |
||||
#define quint_32 unsigned long |
||||
#else |
||||
#define quint_32 unsigned int |
||||
#endif |
||||
#endif |
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Declare SHA1 workspace
|
||||
|
||||
typedef union |
||||
{ |
||||
quint_8 c[64]; |
||||
quint_32 l[16]; |
||||
} SHA1_WORKSPACE_BLOCK; |
||||
|
||||
class CSHA1 |
||||
{ |
||||
public: |
||||
// Two different formats for ReportHash(...)
|
||||
enum |
||||
{ |
||||
REPORT_HEX = 0, |
||||
REPORT_DIGIT = 1 |
||||
}; |
||||
|
||||
// Constructor and Destructor
|
||||
CSHA1(); |
||||
~CSHA1(); |
||||
|
||||
quint_32 m_state[5]; |
||||
quint_32 m_count[2]; |
||||
quint_8 m_buffer[64]; |
||||
quint_8 m_digest[20]; |
||||
|
||||
void Reset(); |
||||
|
||||
// Update the hash value
|
||||
void Update(unsigned char* data, int len); |
||||
bool HashFile(char *szFileName); |
||||
|
||||
// Finalize hash and report
|
||||
void Final(); |
||||
void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX); |
||||
void GetHash(quint_8 *puDest); |
||||
|
||||
private: |
||||
// Private SHA-1 transformation
|
||||
void Transform(quint_32 *state, quint_8 *buffer); |
||||
|
||||
// Member variables
|
||||
quint_8 m_workspace[64]; |
||||
SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above
|
||||
}; |
||||
|
||||
#endif |
@ -1,488 +0,0 @@ |
||||
// IniFile.cpp: Implementation of the CIniFile class.
|
||||
// Written by: Adam Clauss
|
||||
// Email: cabadam@houston.rr.com
|
||||
// You may use this class/code as you wish in your programs. Feel free to distribute it, and
|
||||
// email suggested changes to me.
|
||||
//
|
||||
// Rewritten by: Shane Hill
|
||||
// Date: 21/08/2001
|
||||
// Email: Shane.Hill@dsto.defence.gov.au
|
||||
// Reason: Remove dependancy on MFC. Code should compile on any
|
||||
// platform.
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
// C++ Includes
|
||||
#include <iostream> |
||||
#include <fstream> |
||||
#include <strstream> |
||||
|
||||
// C Includes
|
||||
#include <stdio.h> |
||||
#include <stdarg.h> |
||||
#include <ctype.h> |
||||
|
||||
// Local Includes
|
||||
#include "IniReader.h" |
||||
#include <qglobal.h> |
||||
|
||||
#if defined(Q_WS_WIN) |
||||
#define iniEOL endl |
||||
#else |
||||
#define iniEOL '\r' << endl |
||||
#endif |
||||
|
||||
CIniFile::CIniFile( string const iniPath) |
||||
{ |
||||
Path( iniPath); |
||||
caseInsensitive = true; |
||||
} |
||||
|
||||
bool CIniFile::ReadFile() |
||||
{ |
||||
// Normally you would use ifstream, but the SGI CC compiler has
|
||||
// a few bugs with ifstream. So ... fstream used.
|
||||
fstream f; |
||||
string line; |
||||
string keyname, valuename, value; |
||||
string::size_type pLeft, pRight; |
||||
|
||||
f.open( path.c_str(), ios::in); |
||||
if ( f.fail()) |
||||
return false; |
||||
|
||||
while( getline( f, line)) { |
||||
// To be compatible with Win32, check for existence of '\r'.
|
||||
// Win32 files have the '\r' and Unix files don't at the end of a line.
|
||||
// Note that the '\r' will be written to INI files from
|
||||
// Unix so that the created INI file can be read under Win32
|
||||
// without change.
|
||||
if( !line.length())continue; |
||||
if ( line[line.length() - 1] == '\r') |
||||
line = line.substr( 0, line.length() - 1); |
||||
|
||||
if ( line.length()) { |
||||
// Check that the user hasn't openned a binary file by checking the first
|
||||
// character of each line!
|
||||
if ( !isprint( line[0])) { |
||||
printf( "Failing on char %d\n", line[0]); |
||||
f.close(); |
||||
return false; |
||||
} |
||||
if (( pLeft = line.find_first_of(";#[=")) != string::npos) { |
||||
switch ( line[pLeft]) { |
||||
case '[': |
||||
if ((pRight = line.find_last_of("]")) != string::npos && |
||||
pRight > pLeft) { |
||||
keyname = line.substr( pLeft + 1, pRight - pLeft - 1); |
||||
AddKeyName( keyname); |
||||
} |
||||
break; |
||||
|
||||
case '=': |
||||
valuename = line.substr( 0, pLeft); |
||||
value = line.substr( pLeft + 1); |
||||
SetValue( keyname, valuename, value); |
||||
break; |
||||
|
||||
case ';': |
||||
case '#': |
||||
if ( !names.size()) |
||||
HeaderComment( line.substr( pLeft + 1)); |
||||
else |
||||
KeyComment( keyname, line.substr( pLeft + 1)); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
f.close(); |
||||
if ( names.size()) |
||||
return true; |
||||
return false; |
||||
} |
||||
|
||||
bool CIniFile::WriteFile() |
||||
{ |
||||
unsigned commentID, keyID, valueID; |
||||
// Normally you would use ofstream, but the SGI CC compiler has
|
||||
// a few bugs with ofstream. So ... fstream used.
|
||||
fstream f; |
||||
|
||||
f.open( path.c_str(), ios::out); |
||||
if ( f.fail()) |
||||
return false; |
||||
|
||||
// Write header comments.
|
||||
for ( commentID = 0; commentID < comments.size(); ++commentID) |
||||
f << ';' << comments[commentID] << iniEOL; |
||||
if ( comments.size()) |
||||
f << iniEOL; |
||||
|
||||
// Write keys and values.
|
||||
for ( keyID = 0; keyID < keys.size(); ++keyID) { |
||||
f << '[' << names[keyID] << ']' << iniEOL; |
||||
// Comments.
|
||||
for ( commentID = 0; commentID < keys[keyID].comments.size(); ++commentID) |
||||
f << ';' << keys[keyID].comments[commentID] << iniEOL; |
||||
// Values.
|
||||
for ( valueID = 0; valueID < keys[keyID].names.size(); ++valueID) |
||||
f << keys[keyID].names[valueID] << '=' << keys[keyID].values[valueID] << iniEOL; |
||||
f << iniEOL; |
||||
} |
||||
f.close(); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
long CIniFile::FindKey( string const keyname) const |
||||
{ |
||||
for ( unsigned keyID = 0; keyID < names.size(); ++keyID) |
||||
if ( CheckCase( names[keyID]) == CheckCase( keyname)) |
||||
return long(keyID); |
||||
return noID; |
||||
} |
||||
|
||||
long CIniFile::FindValue( unsigned const keyID, string const valuename) const |
||||
{ |
||||
if ( !keys.size() || keyID >= keys.size()) |
||||
return noID; |
||||
|
||||
for ( unsigned valueID = 0; valueID < keys[keyID].names.size(); ++valueID) |
||||
if ( CheckCase( keys[keyID].names[valueID]) == CheckCase( valuename)) |
||||
return long(valueID); |
||||
return noID; |
||||
} |
||||
|
||||
unsigned CIniFile::AddKeyName( string const keyname) |
||||
{ |
||||
names.resize( names.size() + 1, keyname); |
||||
keys.resize( keys.size() + 1); |
||||
return names.size() - 1; |
||||
} |
||||
|
||||
string CIniFile::KeyName( unsigned const keyID) const |
||||
{ |
||||
if ( keyID < names.size()) |
||||
return names[keyID]; |
||||
else |
||||
return ""; |
||||
} |
||||
|
||||
unsigned CIniFile::NumValues( unsigned const keyID) |
||||
{ |
||||
if ( keyID < keys.size()) |
||||
return keys[keyID].names.size(); |
||||
return 0; |
||||
} |
||||
|
||||
unsigned CIniFile::NumValues( string const keyname) |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) |
||||
return 0; |
||||
return keys[keyID].names.size(); |
||||
} |
||||
|
||||
string CIniFile::ValueName( unsigned const keyID, unsigned const valueID) const |
||||
{ |
||||
if ( keyID < keys.size() && valueID < keys[keyID].names.size()) |
||||
return keys[keyID].names[valueID]; |
||||
return ""; |
||||
} |
||||
|
||||
string CIniFile::ValueName( string const keyname, unsigned const valueID) const |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) |
||||
return ""; |
||||
return ValueName( keyID, valueID); |
||||
} |
||||
|
||||
bool CIniFile::SetValue( unsigned const keyID, unsigned const valueID, string const value) |
||||
{ |
||||
if ( keyID < keys.size() && valueID < keys[keyID].names.size()) |
||||
keys[keyID].values[valueID] = value; |
||||
|
||||
return false; |
||||
} |
||||
|
||||
bool CIniFile::SetValue( string const keyname, string const valuename, string const value, bool const create) |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) { |
||||
if ( create) |
||||
keyID = long( AddKeyName( keyname)); |
||||
else |
||||
return false; |
||||
} |
||||
|
||||
long valueID = FindValue( unsigned(keyID), valuename); |
||||
if ( valueID == noID) { |
||||
if ( !create) |
||||
return false; |
||||
keys[keyID].names.resize( keys[keyID].names.size() + 1, valuename); |
||||
keys[keyID].values.resize( keys[keyID].values.size() + 1, value); |
||||
} else |
||||
keys[keyID].values[valueID] = value; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
bool CIniFile::SetValueI( string const keyname, string const valuename, int const value, bool const create) |
||||
{ |
||||
char svalue[MAX_VALUEDATA]; |
||||
|
||||
sprintf( svalue, "%d", value); |
||||
return SetValue( keyname, valuename, svalue); |
||||
} |
||||
|
||||
bool CIniFile::SetValueF( string const keyname, string const valuename, double const value, bool const create) |
||||
{ |
||||
char svalue[MAX_VALUEDATA]; |
||||
|
||||
sprintf( svalue, "%f", value); |
||||
return SetValue( keyname, valuename, svalue); |
||||
} |
||||
|
||||
bool CIniFile::SetValueV( string const keyname, string const valuename, char *format, ...) |
||||
{ |
||||
va_list args; |
||||
char value[MAX_VALUEDATA]; |
||||
|
||||
va_start( args, format); |
||||
vsprintf( value, format, args); |
||||
va_end( args); |
||||
return SetValue( keyname, valuename, value); |
||||
} |
||||
|
||||
string CIniFile::GetValue( unsigned const keyID, unsigned const valueID, string const defValue) const |
||||
{ |
||||
if ( keyID < keys.size() && valueID < keys[keyID].names.size()) |
||||
return keys[keyID].values[valueID]; |
||||
return defValue; |
||||
} |
||||
|
||||
string CIniFile::GetValue( string const keyname, string const valuename, string const defValue) const |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) |
||||
return defValue; |
||||
|
||||
long valueID = FindValue( unsigned(keyID), valuename); |
||||
if ( valueID == noID) |
||||
return defValue; |
||||
|
||||
return keys[keyID].values[valueID]; |
||||
} |
||||
|
||||
int CIniFile::GetValueI(string const keyname, string const valuename, int const defValue) const |
||||
{ |
||||
char svalue[MAX_VALUEDATA]; |
||||
|
||||
sprintf( svalue, "%d", defValue); |
||||
return atoi( GetValue( keyname, valuename, svalue).c_str());
|
||||
} |
||||
|
||||
double CIniFile::GetValueF(string const keyname, string const valuename, double const defValue) const |
||||
{ |
||||
char svalue[MAX_VALUEDATA]; |
||||
|
||||
sprintf( svalue, "%f", defValue); |
||||
return atof( GetValue( keyname, valuename, svalue).c_str());
|
||||
} |
||||
|
||||
// 16 variables may be a bit of over kill, but hey, it's only code.
|
||||
unsigned CIniFile::GetValueV( string const keyname, string const valuename, char *format, |
||||
void *v1, void *v2, void *v3, void *v4, |
||||
void *v5, void *v6, void *v7, void *v8, |
||||
void *v9, void *v10, void *v11, void *v12, |
||||
void *v13, void *v14, void *v15, void *v16) |
||||
{ |
||||
string value; |
||||
// va_list args;
|
||||
unsigned nVals; |
||||
|
||||
|
||||
value = GetValue( keyname, valuename); |
||||
if ( !value.length()) |
||||
return false; |
||||
// Why is there not vsscanf() function. Linux man pages say that there is
|
||||
// but no compiler I've seen has it defined. Bummer!
|
||||
//
|
||||
// va_start( args, format);
|
||||
// nVals = vsscanf( value.c_str(), format, args);
|
||||
// va_end( args);
|
||||
|
||||
nVals = sscanf( value.c_str(), format, |
||||
v1, v2, v3, v4, v5, v6, v7, v8, |
||||
v9, v10, v11, v12, v13, v14, v15, v16); |
||||
|
||||
return nVals; |
||||
} |
||||
|
||||
bool CIniFile::DeleteValue( string const keyname, string const valuename) |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) |
||||
return false; |
||||
|
||||
long valueID = FindValue( unsigned(keyID), valuename); |
||||
if ( valueID == noID) |
||||
return false; |
||||
|
||||
// This looks strange, but is neccessary.
|
||||
vector<string>::iterator npos = keys[keyID].names.begin() + valueID; |
||||
vector<string>::iterator vpos = keys[keyID].values.begin() + valueID; |
||||
keys[keyID].names.erase( npos, npos + 1); |
||||
keys[keyID].values.erase( vpos, vpos + 1); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
bool CIniFile::DeleteKey( string const keyname) |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) |
||||
return false; |
||||
|
||||
// Now hopefully this destroys the vector lists within keys.
|
||||
// Looking at <vector> source, this should be the case using the destructor.
|
||||
// If not, I may have to do it explicitly. Memory leak check should tell.
|
||||
// memleak_test.cpp shows that the following not required.
|
||||
//keys[keyID].names.clear();
|
||||
//keys[keyID].values.clear();
|
||||
|
||||
vector<string>::iterator npos = names.begin() + keyID; |
||||
vector<key>::iterator kpos = keys.begin() + keyID; |
||||
names.erase( npos, npos + 1); |
||||
keys.erase( kpos, kpos + 1); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
void CIniFile::Erase() |
||||
{ |
||||
// This loop not needed. The vector<> destructor seems to do
|
||||
// all the work itself. memleak_test.cpp shows this.
|
||||
//for ( unsigned i = 0; i < keys.size(); ++i) {
|
||||
// keys[i].names.clear();
|
||||
// keys[i].values.clear();
|
||||
//}
|
||||
names.clear(); |
||||
keys.clear(); |
||||
comments.clear(); |
||||
} |
||||
|
||||
void CIniFile::HeaderComment( string const comment) |
||||
{ |
||||
comments.resize( comments.size() + 1, comment); |
||||
} |
||||
|
||||
string CIniFile::HeaderComment( unsigned const commentID) const |
||||
{ |
||||
if ( commentID < comments.size()) |
||||
return comments[commentID]; |
||||
return ""; |
||||
} |
||||
|
||||
bool CIniFile::DeleteHeaderComment( unsigned commentID) |
||||
{ |
||||
if ( commentID < comments.size()) { |
||||
vector<string>::iterator cpos = comments.begin() + commentID; |
||||
comments.erase( cpos, cpos + 1); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
unsigned CIniFile::NumKeyComments( unsigned const keyID) const |
||||
{ |
||||
if ( keyID < keys.size()) |
||||
return keys[keyID].comments.size(); |
||||
return 0; |
||||
} |
||||
|
||||
unsigned CIniFile::NumKeyComments( string const keyname) const |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) |
||||
return 0; |
||||
return keys[keyID].comments.size(); |
||||
} |
||||
|
||||
bool CIniFile::KeyComment( unsigned const keyID, string const comment) |
||||
{ |
||||
if ( keyID < keys.size()) { |
||||
keys[keyID].comments.resize( keys[keyID].comments.size() + 1, comment); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
bool CIniFile::KeyComment( string const keyname, string const comment) |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) |
||||
return false; |
||||
return KeyComment( unsigned(keyID), comment); |
||||
} |
||||
|
||||
string CIniFile::KeyComment( unsigned const keyID, unsigned const commentID) const |
||||
{ |
||||
if ( keyID < keys.size() && commentID < keys[keyID].comments.size()) |
||||
return keys[keyID].comments[commentID]; |
||||
return ""; |
||||
} |
||||
|
||||
string CIniFile::KeyComment( string const keyname, unsigned const commentID) const |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) |
||||
return ""; |
||||
return KeyComment( unsigned(keyID), commentID); |
||||
} |
||||
|
||||
bool CIniFile::DeleteKeyComment( unsigned const keyID, unsigned const commentID) |
||||
{ |
||||
if ( keyID < keys.size() && commentID < keys[keyID].comments.size()) { |
||||
vector<string>::iterator cpos = keys[keyID].comments.begin() + commentID; |
||||
keys[keyID].comments.erase( cpos, cpos + 1); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
bool CIniFile::DeleteKeyComment( string const keyname, unsigned const commentID) |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) |
||||
return false; |
||||
return DeleteKeyComment( unsigned(keyID), commentID); |
||||
} |
||||
|
||||
bool CIniFile::DeleteKeyComments( unsigned const keyID) |
||||
{ |
||||
if ( keyID < keys.size()) { |
||||
keys[keyID].comments.clear(); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
bool CIniFile::DeleteKeyComments( string const keyname) |
||||
{ |
||||
long keyID = FindKey( keyname); |
||||
if ( keyID == noID) |
||||
return false; |
||||
return DeleteKeyComments( unsigned(keyID)); |
||||
} |
||||
|
||||
string CIniFile::CheckCase( string s) const |
||||
{ |
||||
if ( caseInsensitive) |
||||
for ( string::size_type i = 0; i < s.length(); ++i) |
||||
s[i] = tolower(s[i]); |
||||
return s; |
||||
} |
@ -1,182 +0,0 @@ |
||||
// IniFile.cpp: Implementation of the CIniFile class.
|
||||
// Written by: Adam Clauss
|
||||
// Email: cabadam@tamu.edu
|
||||
// You may use this class/code as you wish in your programs. Feel free to distribute it, and
|
||||
// email suggested changes to me.
|
||||
//
|
||||
// Rewritten by: Shane Hill
|
||||
// Date: 21/08/2001
|
||||
// Email: Shane.Hill@dsto.defence.gov.au
|
||||
// Reason: Remove dependancy on MFC. Code should compile on any
|
||||
// platform. Tested on Windows/Linux/Irix
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef CIniFile_H |
||||
#define CIniFile_H |
||||
|
||||
// C++ Includes
|
||||
#include <string> |
||||
#include <vector> |
||||
|
||||
// C Includes
|
||||
#include <stdlib.h> |
||||
|
||||
using namespace std; |
||||
|
||||
#define MAX_KEYNAME 128 |
||||
#define MAX_VALUENAME 128 |
||||
#define MAX_VALUEDATA 2048 |
||||
|
||||
class CIniFile
|
||||
{ |
||||
private: |
||||
bool caseInsensitive; |
||||
string path; |
||||
struct key { |
||||
vector<string> names; |
||||
vector<string> values;
|
||||
vector<string> comments; |
||||
}; |
||||
vector<key> keys;
|
||||
vector<string> names;
|
||||
vector<string> comments; |
||||
string CheckCase( string s) const; |
||||
|
||||
public: |
||||
enum errors{ noID = -1}; |
||||
CIniFile( string const iniPath = ""); |
||||
virtual ~CIniFile() {} |
||||
|
||||
// Sets whether or not keynames and valuenames should be case sensitive.
|
||||
// The default is case insensitive.
|
||||
void CaseSensitive() {caseInsensitive = false;} |
||||
void CaseInsensitive() {caseInsensitive = true;} |
||||
|
||||
// Sets path of ini file to read and write from.
|
||||
void Path(string const newPath) {path = newPath;} |
||||
string Path() const {return path;} |
||||
void SetPath(string const newPath) {Path( newPath);} |
||||
|
||||
// Reads ini file specified using path.
|
||||
// Returns true if successful, false otherwise.
|
||||
bool ReadFile(); |
||||
|
||||
// Writes data stored in class to ini file.
|
||||
bool WriteFile();
|
||||
|
||||
// Deletes all stored ini data.
|
||||
void Erase(); |
||||
void Clear() {Erase();} |
||||
void Reset() {Erase();} |
||||
|
||||
// Returns index of specified key, or noID if not found.
|
||||
long FindKey( string const keyname) const; |
||||
|
||||
// Returns index of specified value, in the specified key, or noID if not found.
|
||||
long FindValue( unsigned const keyID, string const valuename) const; |
||||
|
||||
// Returns number of keys currently in the ini.
|
||||
unsigned NumKeys() const {return names.size();} |
||||
unsigned GetNumKeys() const {return NumKeys();} |
||||
|
||||
// Add a key name.
|
||||
unsigned AddKeyName( string const keyname); |
||||
|
||||
// Returns key names by index.
|
||||
string KeyName( unsigned const keyID) const; |
||||
string GetKeyName( unsigned const keyID) const {return KeyName(keyID);} |
||||
|
||||
// Returns number of values stored for specified key.
|
||||
unsigned NumValues( unsigned const keyID); |
||||
unsigned GetNumValues( unsigned const keyID) {return NumValues( keyID);} |
||||
unsigned NumValues( string const keyname); |
||||
unsigned GetNumValues( string const keyname) {return NumValues( keyname);} |
||||
|
||||
// Returns value name by index for a given keyname or keyID.
|
||||
string ValueName( unsigned const keyID, unsigned const valueID) const; |
||||
string GetValueName( unsigned const keyID, unsigned const valueID) const { |
||||
return ValueName( keyID, valueID); |
||||
} |
||||
string ValueName( string const keyname, unsigned const valueID) const; |
||||
string GetValueName( string const keyname, unsigned const valueID) const { |
||||
return ValueName( keyname, valueID); |
||||
} |
||||
|
||||
// Gets value of [keyname] valuename =.
|
||||
// Overloaded to return string, int, and double.
|
||||
// Returns defValue if key/value not found.
|
||||
string GetValue( unsigned const keyID, unsigned const valueID, string const defValue = "") const; |
||||
string GetValue(string const keyname, string const valuename, string const defValue = "") const;
|
||||
int GetValueI(string const keyname, string const valuename, int const defValue = 0) const; |
||||
bool GetValueB(string const keyname, string const valuename, bool const defValue = false) const { |
||||
return bool( GetValueI( keyname, valuename, int( defValue))); |
||||
} |
||||
double GetValueF(string const keyname, string const valuename, double const defValue = 0.0) const; |
||||
// This is a variable length formatted GetValue routine. All these voids
|
||||
// are required because there is no vsscanf() like there is a vsprintf().
|
||||
// Only a maximum of 8 variable can be read.
|
||||
unsigned GetValueV( string const keyname, string const valuename, char *format, |
||||
void *v1 = 0, void *v2 = 0, void *v3 = 0, void *v4 = 0, |
||||
void *v5 = 0, void *v6 = 0, void *v7 = 0, void *v8 = 0, |
||||
void *v9 = 0, void *v10 = 0, void *v11 = 0, void *v12 = 0, |
||||
void *v13 = 0, void *v14 = 0, void *v15 = 0, void *v16 = 0); |
||||
|
||||
// Sets value of [keyname] valuename =.
|
||||
// Specify the optional paramter as false (0) if you do not want it to create
|
||||
// the key if it doesn't exist. Returns true if data entered, false otherwise.
|
||||
// Overloaded to accept string, int, and double.
|
||||
bool SetValue( unsigned const keyID, unsigned const valueID, string const value); |
||||
bool SetValue( string const keyname, string const valuename, string const value, bool const create = true); |
||||
bool SetValueI( string const keyname, string const valuename, int const value, bool const create = true); |
||||
bool SetValueB( string const keyname, string const valuename, bool const value, bool const create = true) { |
||||
return SetValueI( keyname, valuename, int(value), create); |
||||
} |
||||
bool SetValueF( string const keyname, string const valuename, double const value, bool const create = true); |
||||
bool SetValueV( string const keyname, string const valuename, char *format, ...); |
||||
|
||||
// Deletes specified value.
|
||||
// Returns true if value existed and deleted, false otherwise.
|
||||
bool DeleteValue( string const keyname, string const valuename); |
||||
|
||||
// Deletes specified key and all values contained within.
|
||||
// Returns true if key existed and deleted, false otherwise.
|
||||
bool DeleteKey(string keyname); |
||||
|
||||
// Header comment functions.
|
||||
// Header comments are those comments before the first key.
|
||||
//
|
||||
// Number of header comments.
|
||||
unsigned NumHeaderComments() {return comments.size();} |
||||
// Add a header comment.
|
||||
void HeaderComment( string const comment); |
||||
// Return a header comment.
|
||||
string HeaderComment( unsigned const commentID) const; |
||||
// Delete a header comment.
|
||||
bool DeleteHeaderComment( unsigned commentID); |
||||
// Delete all header comments.
|
||||
void DeleteHeaderComments() {comments.clear();} |
||||
|
||||
// Key comment functions.
|
||||
// Key comments are those comments within a key. Any comments
|
||||
// defined within value names will be added to this list. Therefore,
|
||||
// these comments will be moved to the top of the key definition when
|
||||
// the CIniFile::WriteFile() is called.
|
||||
//
|
||||
// Number of key comments.
|
||||
unsigned NumKeyComments( unsigned const keyID) const; |
||||
unsigned NumKeyComments( string const keyname) const; |
||||
// Add a key comment.
|
||||
bool KeyComment( unsigned const keyID, string const comment); |
||||
bool KeyComment( string const keyname, string const comment); |
||||
// Return a key comment.
|
||||
string KeyComment( unsigned const keyID, unsigned const commentID) const; |
||||
string KeyComment( string const keyname, unsigned const commentID) const; |
||||
// Delete a key comment.
|
||||
bool DeleteKeyComment( unsigned const keyID, unsigned const commentID); |
||||
bool DeleteKeyComment( string const keyname, unsigned const commentID); |
||||
// Delete all comments for a key.
|
||||
bool DeleteKeyComments( unsigned const keyID); |
||||
bool DeleteKeyComments( string const keyname); |
||||
}; |
||||
|
||||
#endif |
Reference in new issue