addon/validation.cpp

Go to the documentation of this file.
00001 /* $Id: validation.cpp 53253 2012-02-25 19:45:38Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
00004                  2008 - 2012 by Ignacio R. Morelle <shadowm2006@gmail.com>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
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", /* "mod", "gui", */ "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'; /**< Binary escape char. */
00079 } // end unnamed namespace 2
00080 
00081 bool needs_escaping(char c) {
00082     switch(c) {
00083         case '\x00':
00084         case escape_char:
00085         case '\x0D': //Windows -- carriage return
00086         case '\xFE': //Parser code -- textdomain or linenumber&filename
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 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:39 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs