Code to fix campaign server stored data to correct encoding

This commit is contained in:
Pauli Nieminen 2008-08-22 16:53:33 +00:00
parent dcc4b22d46
commit 447d8f74ac
4 changed files with 61 additions and 55 deletions

View File

@ -105,3 +105,58 @@ std::vector<config *> find_scripts(const config &cfg, std::string extension)
}
return python_scripts;
}
namespace {
const char escape_char = '\x01'; //!< Binary escape char.
} // end unnamed namespace 2
bool needs_escaping(char c) {
switch(c) {
case '\x00':
case escape_char:
case '\x0D': //Windows -- carriage return
case '\xFE': //Parser code -- textdomain or linenumber&filename
return true;
default:
return false;
}
}
std::string encode_binary(const std::string& str)
{
std::string res;
res.resize(str.size());
size_t n = 0;
for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
if(needs_escaping(*j)) {
res.resize(res.size()+1);
res[n++] = escape_char;
res[n++] = *j + 1;
} else {
res[n++] = *j;
}
}
return res;
}
std::string unencode_binary(const std::string& str)
{
std::string res;
res.resize(str.size());
size_t n = 0;
for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
if(*j == escape_char && j+1 != str.end()) {
++j;
res[n++] = *j - 1;
res.resize(res.size()-1);
} else {
res[n++] = *j;
}
}
return res;
}

View File

@ -67,4 +67,8 @@ bool check_names_legal(const config& dir);
/** Return a vector of detected scripts. */
std::vector<config *> find_scripts(const config &cfg, std::string extension);
std::string encode_binary(const std::string& str);
std::string unencode_binary(const std::string& str);
bool needs_escaping(char c);
#endif /* !ADDON_CHECKS_HPP_INCLUDED */

View File

@ -130,22 +130,6 @@ std::vector<std::string> installed_addons()
return res;
}
namespace {
const char escape_char = '\x01'; //!< Binary escape char.
} // end unnamed namespace 2
static bool needs_escaping(char c) {
switch(c) {
case '\x00':
case escape_char:
case '\x0D': //Windows -- carriage return
case '\xFE': //Parser code -- textdomain or linenumber&filename
return true;
default:
return false;
}
}
static inline bool IsCR(const char& c)
{
return c == '\x0D';
@ -160,43 +144,6 @@ static std::string strip_cr(std::string str, bool strip)
return str;
}
static std::string encode_binary(const std::string& str)
{
std::string res;
res.resize(str.size());
size_t n = 0;
for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
if(needs_escaping(*j)) {
res.resize(res.size()+1);
res[n++] = escape_char;
res[n++] = *j + 1;
} else {
res[n++] = *j;
}
}
return res;
}
static std::string unencode_binary(const std::string& str)
{
std::string res;
res.resize(str.size());
size_t n = 0;
for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
if(*j == escape_char && j+1 != str.end()) {
++j;
res[n++] = *j - 1;
res.resize(res.size()-1);
} else {
res[n++] = *j;
}
}
return res;
}
namespace {
void append_default_ignore_patterns(std::pair<std::vector<std::string>, std::vector<std::string> >& patterns)
{

View File

@ -292,7 +292,7 @@ namespace {
cfg_["converted_to_gzipped_data"] = "yes";
}
if (cfg_["cr_encoded"] != "yes")
if (cfg_["encoded"] != "yes")
{
// Convert all addons to gzip
config::child_list camps = campaigns().get_children("campaign");
@ -316,7 +316,7 @@ namespace {
unsigned char c = in_filter.get();
while( in_filter.good())
{
if (c == '\r')
if (needs_escaping(c))
{
out_filter.put('\x01');
out_filter.put(c+1);