The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
validation.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2008 by David White <[email protected]>
3  2008 - 2015 by Ignacio R. Morelle <[email protected]>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "global.hpp"
17 #include "addon/validation.hpp"
18 #include "config.hpp"
19 
20 const unsigned short default_campaignd_port = 15008;
21 
22 namespace {
23  const std::string addon_type_strings[] = {
24  "unknown", "core", "campaign", "scenario", "campaign_sp_mp", "campaign_mp",
25  "scenario_mp", "map_pack", "era", "faction", "mod_mp", /*"gui", */ "media",
26  "other", ""
27  };
28 
29  struct addon_name_char_illegal
30  {
31  /**
32  * Returns whether the given add-on name char is not whitelisted.
33  */
34  inline bool operator()(char c)
35  {
36  switch(c)
37  {
38  case '-': // hyphen-minus
39  case '_': // low line
40  return false;
41  default:
42  return !isalnum(c);
43  }
44  }
45  };
46 }
47 
49 {
50  if(name.empty() || name == "." ||
51  std::find_if(name.begin(), name.end(), addon_name_char_illegal()) != name.end() ||
52  name.find("..") != std::string::npos) {
53  return false;
54  } else {
55  return true;
56  }
57 }
58 
60 {
61  if(name.empty() || name == "." ||
62  name.find_first_of("/:\\~ \r\n\v\t") != std::string::npos ||
63  name.find("..") != std::string::npos) {
64  return false;
65  } else {
66  return true;
67  }
68 }
69 
70 bool check_names_legal(const config& dir)
71 {
72  for (const config &path : dir.child_range("file")) {
73  if (!addon_filename_legal(path["name"])) return false;
74  }
75  for (const config &path : dir.child_range("dir")) {
76  if (!addon_filename_legal(path["name"])) return false;
77  if (!check_names_legal(path)) return false;
78  }
79  return true;
80 }
81 
83 {
84  if (str.empty())
85  return ADDON_UNKNOWN;
86 
87  unsigned addon_type_num = 0;
88 
89  while(++addon_type_num != ADDON_TYPES_COUNT) {
90  if(str == addon_type_strings[addon_type_num]) {
91  return ADDON_TYPE(addon_type_num);
92  }
93  }
94 
95  return ADDON_UNKNOWN;
96 }
97 
99 {
100  assert(type != ADDON_TYPES_COUNT);
101  return addon_type_strings[type];
102 }
103 
104 namespace {
105  const char escape_char = '\x01'; /**< Binary escape char. */
106 } // end unnamed namespace 2
107 
108 bool needs_escaping(char c) {
109  switch(c) {
110  case '\x00':
111  case escape_char:
112  case '\x0D': //Windows -- carriage return
113  case '\xFE': //Parser code -- textdomain or linenumber&filename
114  return true;
115  default:
116  return false;
117  }
118 }
119 
121 {
123  res.resize(str.size());
124  size_t n = 0;
125  for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
126  if(needs_escaping(*j)) {
127  res.resize(res.size()+1);
128  res[n++] = escape_char;
129  res[n++] = *j + 1;
130  } else {
131  res[n++] = *j;
132  }
133  }
134 
135  return res;
136 }
137 
139 {
141  res.resize(str.size());
142 
143  size_t n = 0;
144  for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
145  if(*j == escape_char && j+1 != str.end()) {
146  ++j;
147  res[n++] = *j - 1;
148  res.resize(res.size()-1);
149  } else {
150  res[n++] = *j;
151  }
152  }
153 
154  return res;
155 }
156 
157 
child_itors child_range(const std::string &key)
Definition: config.cpp:613
ADDON_TYPE
Values used for add-on classification; UI-only at the moment, in the future it could be used for dire...
Definition: validation.hpp:41
const GLfloat * c
Definition: glew.h:12741
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
std::string encode_binary(const std::string &str)
Definition: validation.cpp:120
std::string unencode_binary(const std::string &str)
Definition: validation.cpp:138
bool needs_escaping(char c)
Definition: validation.cpp:108
Definitions for the interface to Wesnoth Markup Language (WML).
GLsizei const char ** path
Definition: glew.h:4654
ADDON_TYPE get_addon_type(const std::string &str)
Definition: validation.cpp:82
bool addon_name_legal(const std::string &name)
Checks whether an add-on id/name is legal or not.
Definition: validation.cpp:48
GLuint res
Definition: glew.h:9258
GLuint const GLchar * name
Definition: glew.h:1782
GLclampd n
Definition: glew.h:5903
const unsigned short default_campaignd_port
Default port number for the addon server.
Definition: validation.cpp:20
std::string get_addon_type_string(ADDON_TYPE type)
Definition: validation.cpp:98
bool check_names_legal(const config &dir)
Probes an add-on archive for illegal names.
Definition: validation.cpp:70
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool addon_filename_legal(const std::string &name)
Checks whether an add-on file name is legal or not.
Definition: validation.cpp:59