Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "global.hpp"
00018 #include "addon/validation.hpp"
00019 #include "config.hpp"
00020 #include "foreach.hpp"
00021
00022 const unsigned short default_campaignd_port = 15002;
00023
00024 namespace {
00025 const std::string addon_type_strings[] = {
00026 "unknown", "campaign", "scenario", "campaign_mp", "scenario_mp",
00027 "map_pack", "era", "faction", "media", "other",
00028 ""
00029 };
00030 }
00031
00032 bool addon_name_legal(const std::string& name)
00033 {
00034 if(name.empty() || name == "." ||
00035 name.find_first_of("/:\\~") != std::string::npos ||
00036 name.find("..") != std::string::npos) {
00037 return false;
00038 } else {
00039 return true;
00040 }
00041 }
00042
00043 bool check_names_legal(const config& dir)
00044 {
00045 foreach (const config &path, dir.child_range("file")) {
00046 if (!addon_name_legal(path["name"])) return false;
00047 }
00048 foreach (const config &path, dir.child_range("dir")) {
00049 if (!addon_name_legal(path["name"])) return false;
00050 if (!check_names_legal(path)) return false;
00051 }
00052 return true;
00053 }
00054
00055 ADDON_TYPE get_addon_type(const std::string& str)
00056 {
00057 if (str.empty())
00058 return ADDON_UNKNOWN;
00059
00060 unsigned addon_type_num = 0;
00061
00062 while(++addon_type_num != ADDON_TYPES_COUNT) {
00063 if(str == addon_type_strings[addon_type_num]) {
00064 return ADDON_TYPE(addon_type_num);
00065 }
00066 }
00067
00068 return ADDON_UNKNOWN;
00069 }
00070
00071 std::string get_addon_type_string(ADDON_TYPE type)
00072 {
00073 assert(type != ADDON_TYPES_COUNT);
00074 return addon_type_strings[type];
00075 }
00076
00077 namespace {
00078 const char escape_char = '\x01';
00079 }
00080
00081 bool needs_escaping(char c) {
00082 switch(c) {
00083 case '\x00':
00084 case escape_char:
00085 case '\x0D':
00086 case '\xFE':
00087 return true;
00088 default:
00089 return false;
00090 }
00091 }
00092
00093 std::string encode_binary(const std::string& str)
00094 {
00095 std::string res;
00096 res.resize(str.size());
00097 size_t n = 0;
00098 for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
00099 if(needs_escaping(*j)) {
00100 res.resize(res.size()+1);
00101 res[n++] = escape_char;
00102 res[n++] = *j + 1;
00103 } else {
00104 res[n++] = *j;
00105 }
00106 }
00107
00108 return res;
00109 }
00110
00111 std::string unencode_binary(const std::string& str)
00112 {
00113 std::string res;
00114 res.resize(str.size());
00115
00116 size_t n = 0;
00117 for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
00118 if(*j == escape_char && j+1 != str.end()) {
00119 ++j;
00120 res[n++] = *j - 1;
00121 res.resize(res.size()-1);
00122 } else {
00123 res[n++] = *j;
00124 }
00125 }
00126
00127 return res;
00128 }
00129
00130