mirror of
https://github.com/asdlokj1qpi233/subconverter.git
synced 2026-11-01 17:25:02 +00:00
Fix compiler warning for base64_decode
Optimize codes.
This commit is contained in:
1 file changed
+19
-50
+19
-50
@@ -141,8 +141,8 @@ unsigned char FromHex(unsigned char x)
|
|||||||
std::string UrlEncode(const std::string& str)
|
std::string UrlEncode(const std::string& str)
|
||||||
{
|
{
|
||||||
std::string strTemp = "";
|
std::string strTemp = "";
|
||||||
size_t length = str.length();
|
string_size length = str.length();
|
||||||
for (size_t i = 0; i < length; i++)
|
for (string_size i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
if (isalnum((unsigned char)str[i]) ||
|
if (isalnum((unsigned char)str[i]) ||
|
||||||
(str[i] == '-') ||
|
(str[i] == '-') ||
|
||||||
@@ -150,8 +150,6 @@ std::string UrlEncode(const std::string& str)
|
|||||||
(str[i] == '.') ||
|
(str[i] == '.') ||
|
||||||
(str[i] == '~'))
|
(str[i] == '~'))
|
||||||
strTemp += str[i];
|
strTemp += str[i];
|
||||||
//else if (str[i] == ' ')
|
|
||||||
// strTemp += "+";
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
strTemp += '%';
|
strTemp += '%';
|
||||||
@@ -165,8 +163,8 @@ std::string UrlEncode(const std::string& str)
|
|||||||
std::string UrlDecode(const std::string& str)
|
std::string UrlDecode(const std::string& str)
|
||||||
{
|
{
|
||||||
std::string strTemp;
|
std::string strTemp;
|
||||||
size_t length = str.length();
|
string_size length = str.length();
|
||||||
for (size_t i = 0; i < length; i++)
|
for (string_size i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
if (str[i] == '+')
|
if (str[i] == '+')
|
||||||
strTemp += ' ';
|
strTemp += ' ';
|
||||||
@@ -248,16 +246,19 @@ std::string base64_decode(const std::string &encoded_string, bool accept_urlsafe
|
|||||||
string_size in_len = encoded_string.size();
|
string_size in_len = encoded_string.size();
|
||||||
string_size i = 0;
|
string_size i = 0;
|
||||||
string_size in_ = 0;
|
string_size in_ = 0;
|
||||||
unsigned char char_array_4[4], char_array_3[3];
|
unsigned char char_array_4[4], char_array_3[3], uchar = 0;
|
||||||
static unsigned char dtable[256], itable[256], table_ready = 0;
|
static unsigned char dtable[256], itable[256], table_ready = 0;
|
||||||
std::string ret;
|
std::string ret;
|
||||||
|
|
||||||
// Should not need thread_local with the flag...
|
// Should not need thread_local with the flag...
|
||||||
if (!table_ready) {
|
if (!table_ready)
|
||||||
|
{
|
||||||
// No memset needed for static/TLS
|
// No memset needed for static/TLS
|
||||||
for (string_size k = 0; k < base64_chars.length(); k++) {
|
for (string_size k = 0; k < base64_chars.length(); k++)
|
||||||
dtable[base64_chars[k]] = k; // decode (find)
|
{
|
||||||
itable[base64_chars[k]] = 1; // is_base64
|
uchar = base64_chars[k]; // make compiler happy
|
||||||
|
dtable[uchar] = k; // decode (find)
|
||||||
|
itable[uchar] = 1; // is_base64
|
||||||
}
|
}
|
||||||
// Add urlsafe table
|
// Add urlsafe table
|
||||||
dtable['-'] = dtable['+']; itable['-'] = 2;
|
dtable['-'] = dtable['+']; itable['-'] = 2;
|
||||||
@@ -265,10 +266,12 @@ std::string base64_decode(const std::string &encoded_string, bool accept_urlsafe
|
|||||||
table_ready = 1;
|
table_ready = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (in_len-- && (encoded_string[in_] != '=') &&
|
while (in_len-- && (encoded_string[in_] != '='))
|
||||||
(accept_urlsafe ? itable[encoded_string[in_]] : (itable[encoded_string[in_]] == 1)))
|
|
||||||
{
|
{
|
||||||
char_array_4[i++] = encoded_string[in_];
|
uchar = encoded_string[in_]; // make compiler happy
|
||||||
|
if (!(accept_urlsafe ? itable[uchar] : (itable[uchar] == 1))) // break away from the while condition
|
||||||
|
continue;
|
||||||
|
char_array_4[i++] = uchar;
|
||||||
in_++;
|
in_++;
|
||||||
if (i == 4)
|
if (i == 4)
|
||||||
{
|
{
|
||||||
@@ -652,7 +655,6 @@ std::string urlsafe_base64_encode(const std::string &string_to_encode)
|
|||||||
|
|
||||||
std::string getMD5(const std::string &data)
|
std::string getMD5(const std::string &data)
|
||||||
{
|
{
|
||||||
|
|
||||||
std::string result;
|
std::string result;
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
unsigned char digest[16] = {};
|
unsigned char digest[16] = {};
|
||||||
@@ -701,7 +703,6 @@ std::string fileGet(const std::string &path, bool binary, bool scope_limit)
|
|||||||
#endif // _WIN32
|
#endif // _WIN32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
infile.open(path, mode);
|
infile.open(path, mode);
|
||||||
if(infile)
|
if(infile)
|
||||||
{
|
{
|
||||||
@@ -845,62 +846,37 @@ bool is_str_utf8(const std::string &data)
|
|||||||
const char *str = data.c_str();
|
const char *str = data.c_str();
|
||||||
unsigned int nBytes = 0;
|
unsigned int nBytes = 0;
|
||||||
unsigned char chr;
|
unsigned char chr;
|
||||||
bool bAllAscii = true;
|
|
||||||
for (unsigned int i = 0; str[i] != '\0'; ++i)
|
for (unsigned int i = 0; str[i] != '\0'; ++i)
|
||||||
{
|
{
|
||||||
chr = *(str + i);
|
chr = *(str + i);
|
||||||
if (nBytes == 0 && (chr & 0x80) != 0)
|
|
||||||
{
|
|
||||||
bAllAscii = false;
|
|
||||||
}
|
|
||||||
if (nBytes == 0)
|
if (nBytes == 0)
|
||||||
{
|
{
|
||||||
if (chr >= 0x80)
|
if (chr >= 0x80)
|
||||||
{
|
{
|
||||||
if (chr >= 0xFC && chr <= 0xFD)
|
if (chr >= 0xFC && chr <= 0xFD)
|
||||||
{
|
|
||||||
nBytes = 6;
|
nBytes = 6;
|
||||||
}
|
|
||||||
else if (chr >= 0xF8)
|
else if (chr >= 0xF8)
|
||||||
{
|
|
||||||
nBytes = 5;
|
nBytes = 5;
|
||||||
}
|
|
||||||
else if (chr >= 0xF0)
|
else if (chr >= 0xF0)
|
||||||
{
|
|
||||||
nBytes = 4;
|
nBytes = 4;
|
||||||
}
|
|
||||||
else if (chr >= 0xE0)
|
else if (chr >= 0xE0)
|
||||||
{
|
|
||||||
nBytes = 3;
|
nBytes = 3;
|
||||||
}
|
|
||||||
else if (chr >= 0xC0)
|
else if (chr >= 0xC0)
|
||||||
{
|
|
||||||
nBytes = 2;
|
nBytes = 2;
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
nBytes--;
|
nBytes--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ((chr & 0xC0) != 0x80)
|
if ((chr & 0xC0) != 0x80)
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
nBytes--;
|
nBytes--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nBytes != 0)
|
if (nBytes != 0)
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
if (bAllAscii)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -949,19 +925,12 @@ std::string getFormData(const std::string &raw_data)
|
|||||||
while (std::getline(strstrm, line))
|
while (std::getline(strstrm, line))
|
||||||
{
|
{
|
||||||
if(i == 0)
|
if(i == 0)
|
||||||
{
|
boundary = line.substr(0, line.length() - 1); // Get boundary
|
||||||
// Get boundary
|
|
||||||
boundary = line.substr(0, line.length() - 1);
|
|
||||||
}
|
|
||||||
else if(line.find(boundary) == 0)
|
else if(line.find(boundary) == 0)
|
||||||
{
|
break; // The end
|
||||||
// The end
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if(line.length() == 1)
|
else if(line.length() == 1)
|
||||||
{
|
{
|
||||||
// Time to get raw data
|
// Time to get raw data
|
||||||
|
|
||||||
char c;
|
char c;
|
||||||
int bl = boundary.length();
|
int bl = boundary.length();
|
||||||
bool endfile = false;
|
bool endfile = false;
|
||||||
|
|||||||
Reference in new issue
Block a user