X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fhelperfuncs.cpp;h=94a5240c9d4474888c81b91e010ee98c5a2c8262;hb=f5b2265c2e6868431abbaa301aa1d64e8d49d8b0;hp=8ced78ae8ac720d880f14d34cdba42da5144ddc7;hpb=124c17e14134a4999afc1a5e981ab7c75b3694b9;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index 8ced78ae8..94a5240c9 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -156,10 +156,11 @@ void InspIRCd::ProcessColors(file_cache& input) special_chars("\\c", "\x03"), // Color special_chars("\\i", "\x1D"), // Italic special_chars("\\m", "\x11"), // Monospace + special_chars("\\r", "\x16"), // Reverse special_chars("\\s", "\x1E"), // Strikethrough special_chars("\\u", "\x1F"), // Underline special_chars("\\x", "\x0F"), // Reset - special_chars("\\", "") + special_chars("", "") }; for(file_cache::iterator it = input.begin(), it_end = input.end(); it != it_end; it++) @@ -348,24 +349,43 @@ void InspIRCd::CheckRoot() #endif } -/** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array - * lookups and pointer maths. +/** A lookup table of values for multiplier characters used by + * InspIRCd::Duration(). In this lookup table, the indexes for + * the ascii values 'm' and 'M' have the value '60', the indexes + * for the ascii values 'D' and 'd' have a value of '86400', etc. */ -unsigned long InspIRCd::Duration(const std::string &str) +static const unsigned int duration_multi[] = { - unsigned char multiplier = 0; - long total = 0; - long times = 1; - long subtotal = 0; + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 86400, 0, 0, 0, 3600, 0, 0, 0, 0, 60, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 604800, 0, 31557600, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 86400, 0, 0, 0, 3600, 0, 0, 0, 0, 60, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 604800, 0, 31557600, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; + +bool InspIRCd::Duration(const std::string& str, unsigned long& duration) +{ + unsigned long total = 0; + unsigned long subtotal = 0; /* Iterate each item in the string, looking for number or multiplier */ - for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i) + for (std::string::const_iterator i = str.begin(); i != str.end(); ++i) { /* Found a number, queue it onto the current number */ if ((*i >= '0') && (*i <= '9')) { - subtotal = subtotal + ((*i - '0') * times); - times = times * 10; + subtotal = (subtotal * 10) + (*i - '0'); } else { @@ -373,22 +393,40 @@ unsigned long InspIRCd::Duration(const std::string &str) * it multiplies the built up number by, multiply the total * and reset the built up number. */ - if (subtotal) - total += subtotal * duration_multi[multiplier]; + unsigned int multiplier = duration_multi[static_cast(*i)]; + if (multiplier == 0) + return false; + + total += subtotal * multiplier; /* Next subtotal please */ subtotal = 0; - multiplier = *i; - times = 1; } } - if (multiplier) + /* Any trailing values built up are treated as raw seconds */ + duration = total + subtotal; + return true; +} + +unsigned long InspIRCd::Duration(const std::string& str) +{ + unsigned long out = 0; + InspIRCd::Duration(str, out); + return out; +} + +bool InspIRCd::IsValidDuration(const std::string& duration) +{ + for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i) { - total += subtotal * duration_multi[multiplier]; - subtotal = 0; + unsigned char c = *i; + if (((c >= '0') && (c <= '9'))) + continue; + + if (!duration_multi[c]) + return false; } - /* Any trailing values built up are treated as raw seconds */ - return total + subtotal; + return true; } std::string InspIRCd::Format(va_list& vaList, const char* formatString) @@ -477,8 +515,11 @@ unsigned long InspIRCd::GenRandomInt(unsigned long max) // This is overridden by a higher-quality algorithm when SSL support is loaded void InspIRCd::DefaultGenRandom(char* output, size_t max) { - for(unsigned int i=0; i < max; i++) -#ifdef _WIN32 +#if defined HAS_ARC4RANDOM_BUF + arc4random_buf(output, max); +#else + for (unsigned int i = 0; i < max; ++i) +# ifdef _WIN32 { unsigned int uTemp; if(rand_s(&uTemp) != 0) @@ -486,7 +527,8 @@ void InspIRCd::DefaultGenRandom(char* output, size_t max) else output[i] = uTemp; } -#else +# else output[i] = random(); +# endif #endif }