diff --git a/src/cpim/parser/cpim-parser.cpp b/src/cpim/parser/cpim-parser.cpp index 1235b0896..8d2a88e97 100644 --- a/src/cpim/parser/cpim-parser.cpp +++ b/src/cpim/parser/cpim-parser.cpp @@ -355,22 +355,22 @@ bool Cpim::Parser::coreHeaderIsValid(const string &headerV return false; // Check date. - const int year = stoi(headerValue.substr(0, 4)); + const int year = Utils::stoi(headerValue.substr(0, 4)); const bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; - const int month = stoi(headerValue.substr(5, 2)); + const int month = Utils::stoi(headerValue.substr(5, 2)); if (month < 1 || month > 12) return false; - const int day = stoi(headerValue.substr(8, 2)); + const int day = Utils::stoi(headerValue.substr(8, 2)); if (day < 1 || (month == 2 && isLeapYear ? day > 29 : day > daysInMonth[month - 1])) return false; // Check time. if ( - stoi(headerValue.substr(11, 2)) > 24 || - stoi(headerValue.substr(14, 2)) > 59 || - stoi(headerValue.substr(17, 2)) > 60 + Utils::stoi(headerValue.substr(11, 2)) > 24 || + Utils::stoi(headerValue.substr(14, 2)) > 59 || + Utils::stoi(headerValue.substr(17, 2)) > 60 ) return false; @@ -378,8 +378,8 @@ bool Cpim::Parser::coreHeaderIsValid(const string &headerV if (headerValue.back() != 'Z') { size_t length = headerValue.length(); if ( - stoi(headerValue.substr(length - 5, 2)) > 24 || - stoi(headerValue.substr(length - 2, 2)) > 59 + Utils::stoi(headerValue.substr(length - 5, 2)) > 24 || + Utils::stoi(headerValue.substr(length - 2, 2)) > 59 ) return false; } diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 9e32a883a..029baaa34 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -47,3 +47,13 @@ vector Utils::split (const string &str, const string &delimiter) { return out; } + +int Utils::stoi (const string &str, size_t *idx, int base) { + char *p; + int v = strtol(str.c_str(), &p, base); + + if (idx) + *idx = p - str.c_str(); + + return v; +} diff --git a/src/utils/utils.h b/src/utils/utils.h index 1fc468add..d061b9e35 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -33,6 +33,8 @@ namespace LinphonePrivate { inline std::vector split (const std::string &str, char delimiter) { return split(str, std::string(1, delimiter)); } + + int stoi (const std::string &str, size_t *idx = 0, int base = 10); } }