2 * Copyright (C) 2012-2013 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 int stricmp (const char *s1, const char *s2) {
30 return stricmplen(s1, s2, -1);
33 int stricmplen(const char *s1, const char *s2, int len) {
35 return (s2 == NULL ? 0 : -(*s2));
40 while ((c1 = tolower(*s1)) == (c2 = tolower(*s2))) {
46 if(len != -1 && i == len) break;
51 /* copied from IRCU 2.10.12 match.c */
53 * Compare if a given string (name) matches the given
54 * mask (which can contain wild cards: '*' - match any
55 * number of chars, '?' - match any single character.
60 * Originally by Douglas A Lewis (dalewis@acsu.buffalo.edu)
61 * Rewritten by Timothy Vogelsang (netski), net@astrolink.org
63 int match(const char *mask, const char *name)
65 const char *m = mask, *n = name;
66 const char *m_tmp = mask, *n_tmp = name;
69 for (;;) switch (*m) {
83 /* allow escaping to force capitalization */
88 for (star_p = 0; ; m++) {
99 else if (*m == '\\') {
103 for (n_tmp = n; *n && *n != *m; n++) ;
106 for (n_tmp = n; *n && tolower(*n) != tolower(*m); n++) ;
109 /* and fall through */
113 if (tolower(*m) != tolower(*n))
122 static unsigned char random_numbers[256];
123 static int random_numbers_available = 0;
125 int get_random(int min, int max) {
126 int requested_bytes = 0;
127 unsigned int rnd_width = max-min;
128 if((rnd_width % (0xFF000000)))
130 else if((rnd_width % (0xFF0000)))
132 else if((rnd_width % (0xFF00)))
137 //get_random_generate:
139 if(random_numbers_available < requested_bytes) {
140 // generate new random numbers
142 HMODULE hLib=LoadLibrary("ADVAPI32.DLL");
144 BOOLEAN (APIENTRY *pfn)(void*, ULONG) = (BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036");
146 ULONG ulCbBuff = sizeof(random_numbers) - random_numbers_available;
147 if(pfn(random_numbers + random_numbers_available,ulCbBuff)) {
148 random_numbers_available += ulCbBuff;
154 int randomData = open("/dev/random", O_RDONLY);
155 int randLen = sizeof(random_numbers) - random_numbers_available;
156 size_t randomDataLen = random_numbers_available;
157 while (randomDataLen < randLen) {
158 ssize_t result = read(randomData, ((char*)random_numbers) + randomDataLen, randLen - randomDataLen);
161 randomDataLen += result;
163 random_numbers_available = randomDataLen;
167 if(random_numbers_available < requested_bytes) {
168 //system random number generators failed!
178 rnd |= random_numbers[--random_numbers_available];
179 } while(++i < requested_bytes);
181 // TODO: find better solution for this
182 //if((rnd % rnd_width) > rnd_width)
183 // goto get_random_generate;
185 return min + (rnd % rnd_width);