The Battle for Wesnoth  1.19.5+dev
validation.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Iris Morelle <shadowm2006@gmail.com>
4  Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #pragma once
18 
19 #include <vector>
20 #include <string>
21 
22 class config;
23 
24 /**
25  * Default port number for the addon server.
26  *
27  * @note This might not be the best place to declare the variable, but it's
28  * one of the few files shared by the server and the game.
29  */
30 extern const unsigned short default_campaignd_port;
31 
32 enum class ADDON_CHECK_STATUS : unsigned int
33 {
34  //
35  // General errors
36  //
37  SUCCESS = 0x0, /**< No error */
38  UNAUTHORIZED = 0x1, /**< Authentication failed */
39  DENIED = 0x2, /**< Upload denied */
40  USER_DOES_NOT_EXIST = 0x3, /**< Requested forum authentication for a user that doesn't exist on the forums */
41  UNEXPECTED_DELTA = 0xD, /**< Delta for a non-existent add-on */
42  //
43  // Structure errors
44  //
45  EMPTY_PACK = 0x100, /**< Empty pack */
46  BAD_DELTA = 0x101, /**< Bad delta pack */
47  BAD_NAME = 0x102, /**< Bad add-on name */
48  NAME_HAS_MARKUP = 0x104, /**< Markup in add-on name */
49  ILLEGAL_FILENAME = 0x10A, /**< Bad filename */
50  FILENAME_CASE_CONFLICT = 0x10B, /**< Filename case conflict */
51  INVALID_UTF8_NAME = 0x1FF, /**< Invalid UTF-8 sequence in add-on name */
52  //
53  // .pbl errors
54  //
55  NO_TITLE = 0x200, /**< No title specified */
56  NO_AUTHOR = 0x201, /**< No author specified */
57  NO_VERSION = 0x202, /**< No version specified */
58  NO_DESCRIPTION = 0x203, /**< No description specified */
59  NO_EMAIL = 0x204, /**< No email specified */
60  NO_PASSPHRASE = 0x205, /**< No passphrase specified */
61  TITLE_HAS_MARKUP = 0x206, /**< Markup in add-on title */
62  BAD_TYPE = 0x207, /**< Bad add-on type */
63  VERSION_NOT_INCREMENTED = 0x208, /**< Version number is not an increment */
64  INVALID_UTF8_ATTRIBUTE = 0x2FF, /**< Invalid UTF-8 sequence in add-on metadata */
65  BAD_FEEDBACK_TOPIC_ID = 0x209, /**< The provided topic ID for the addon's feedback forum thread is invalid */
66  FEEDBACK_TOPIC_ID_NOT_FOUND = 0x2A0, /**< The provided topic ID for the addon's feedback forum thread wasn't found in the forum database */
67  AUTH_TYPE_MISMATCH = 0x2B0, /**< The addon's forum_auth value does not match its previously set value */
68  //
69  // Server errors
70  //
71  SERVER_UNSPECIFIED = 0xF000, /**< Unspecified server error */
72  SERVER_READ_ONLY = 0xF001, /**< Server read-only mode on */
73  SERVER_ADDONS_LIST = 0xF002, /**< Corrupted server add-ons list */
74  SERVER_DELTA_NO_VERSIONS = 0xF003, /**< No versions to deltify against */
75  SERVER_FORUM_AUTH_DISABLED = 0xF004, /**< The remote add-ons server does not support forum authorization */
76 };
77 
78 std::string addon_check_status_desc(unsigned int code);
79 
81 {
82  return addon_check_status_desc(static_cast<unsigned int>(code));
83 }
84 
85 std::string translated_addon_check_status(unsigned int code);
86 
88 {
89  return translated_addon_check_status(static_cast<unsigned int>(code));
90 }
91 
92 /**
93  * Values used for add-on classification; UI-only
94  * at the moment, in the future it could be used for
95  * directory allocation too, removing the need for
96  * the ADDON_GROUP constants (TODO).
97  *
98  * @note If you change the order or content of these, you'll also need
99  * to update the @a addon_type_strings table found in validation.cpp.
100  */
102  ADDON_UNKNOWN, /**< a.k.a. anything. */
103  ADDON_CORE, /**< Total Conversion Core. */
104  ADDON_SP_CAMPAIGN, /**< Single-player campaign. */
105  ADDON_SP_SCENARIO, /**< Single-player scenario. */
106  ADDON_SP_MP_CAMPAIGN, /**< Hybrid campaign. */
107  ADDON_MP_CAMPAIGN, /**< Multiplayer campaign. */
108  ADDON_MP_SCENARIO, /**< Multiplayer scenario. */
109  ADDON_MP_MAPS, /**< Multiplayer plain (no WML) map pack. */
110  ADDON_MP_ERA, /**< Multiplayer era. */
111  ADDON_MP_FACTION, /**< Multiplayer faction. */
112  ADDON_MOD, /**< Modification of the game. */
113  ADDON_MEDIA, /**< Miscellaneous content/media (unit packs, terrain packs, music packs, etc.). */
114  ADDON_THEME, /**< GUI2 Themes */
115  ADDON_OTHER, /**< an add-on that fits in no other category */
117 };
118 
119 ADDON_TYPE get_addon_type(const std::string& str);
121 
122 /** Checks whether an add-on id/name is legal or not. */
123 bool addon_name_legal(const std::string& name);
124 /** Checks whether an add-on file name is legal or not. */
125 bool addon_filename_legal(const std::string& name);
126 
127 /**
128  * Scans an add-on archive for illegal names.
129  *
130  * @param dir The WML container for the root [dir] node where the search
131  * should begin.
132  * @param badlist If provided and not null, any illegal names encountered will
133  * be added to this list. This also makes the archive scan more
134  * thorough instead of returning on the first illegal name
135  * encountered.
136  *
137  * @returns True if no illegal names were found.
138  */
139 bool check_names_legal(const config& dir, std::vector<std::string>* badlist = nullptr);
140 /**
141  * Scans an add-on archive for case-conflicts.
142  *
143  * Case conflicts may cause trouble on case-insensitive filesystems.
144  *
145  * @param dir The WML container for the root [dir] node where the search
146  * should begin.
147  * @param badlist If provided and not null, any case conflicts encountered will
148  * be added to this list. This also makes the archive scan more
149  * thorough instead of returning on the first conflict
150  * encountered.
151  *
152  * @returns True if no conflicts were found.
153  */
154 bool check_case_insensitive_duplicates(const config& dir, std::vector<std::string>* badlist = nullptr);
155 
156 std::string encode_binary(const std::string& str);
157 std::string unencode_binary(const std::string& str);
158 bool needs_escaping(char c);
159 
160 std::string file_hash(const config& file);
161 bool comp_file_hash(const config& file_a, const config& file_b);
162 void write_hashlist(config& hashlist, const config& data);
163 bool contains_hashlist(const config& from, const config& to);
164 void make_updatepack(config& pack, const config& from, const config& to);
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
std::string_view data
Definition: picture.cpp:178
mock_char c
bool addon_name_legal(const std::string &name)
Checks whether an add-on id/name is legal or not.
Definition: validation.cpp:56
bool needs_escaping(char c)
Definition: validation.cpp:206
std::string file_hash(const config &file)
Definition: validation.cpp:258
std::string unencode_binary(const std::string &str)
Definition: validation.cpp:236
void make_updatepack(config &pack, const config &from, const config &to)
&from, &to are the top directories of their structures; addlist/removelist tag is treated as [dir]
Definition: validation.cpp:370
ADDON_TYPE get_addon_type(const std::string &str)
Definition: validation.cpp:180
bool addon_filename_legal(const std::string &name)
Checks whether an add-on file name is legal or not.
Definition: validation.cpp:66
void write_hashlist(config &hashlist, const config &data)
Definition: validation.cpp:272
bool contains_hashlist(const config &from, const config &to)
Definition: validation.cpp:288
bool check_names_legal(const config &dir, std::vector< std::string > *badlist=nullptr)
Scans an add-on archive for illegal names.
Definition: validation.cpp:166
const unsigned short default_campaignd_port
Default port number for the addon server.
Definition: validation.cpp:27
std::string addon_check_status_desc(unsigned int code)
Definition: validation.cpp:378
std::string translated_addon_check_status(unsigned int code)
Definition: validation.cpp:540
bool check_case_insensitive_duplicates(const config &dir, std::vector< std::string > *badlist=nullptr)
Scans an add-on archive for case-conflicts.
Definition: validation.cpp:175
ADDON_CHECK_STATUS
Definition: validation.hpp:33
@ SERVER_ADDONS_LIST
Corrupted server add-ons list.
@ UNAUTHORIZED
Authentication failed.
@ BAD_FEEDBACK_TOPIC_ID
The provided topic ID for the addon's feedback forum thread is invalid.
@ SERVER_DELTA_NO_VERSIONS
No versions to deltify against.
@ NO_TITLE
No title specified.
@ AUTH_TYPE_MISMATCH
The addon's forum_auth value does not match its previously set value.
@ ILLEGAL_FILENAME
Bad filename.
@ NO_PASSPHRASE
No passphrase specified.
@ UNEXPECTED_DELTA
Delta for a non-existent add-on.
@ VERSION_NOT_INCREMENTED
Version number is not an increment.
@ FILENAME_CASE_CONFLICT
Filename case conflict.
@ TITLE_HAS_MARKUP
Markup in add-on title.
@ BAD_DELTA
Bad delta pack.
@ SERVER_UNSPECIFIED
Unspecified server error.
@ DENIED
Upload denied.
@ BAD_TYPE
Bad add-on type.
@ NO_AUTHOR
No author specified.
@ EMPTY_PACK
Empty pack.
@ NO_DESCRIPTION
No description specified.
@ NO_EMAIL
No email specified.
@ FEEDBACK_TOPIC_ID_NOT_FOUND
The provided topic ID for the addon's feedback forum thread wasn't found in the forum database.
@ INVALID_UTF8_NAME
Invalid UTF-8 sequence in add-on name.
@ USER_DOES_NOT_EXIST
Requested forum authentication for a user that doesn't exist on the forums.
@ INVALID_UTF8_ATTRIBUTE
Invalid UTF-8 sequence in add-on metadata.
@ SUCCESS
No error.
@ BAD_NAME
Bad add-on name.
@ NAME_HAS_MARKUP
Markup in add-on name.
@ SERVER_FORUM_AUTH_DISABLED
The remote add-ons server does not support forum authorization.
@ SERVER_READ_ONLY
Server read-only mode on.
@ NO_VERSION
No version specified.
std::string encode_binary(const std::string &str)
Definition: validation.cpp:218
bool comp_file_hash(const config &file_a, const config &file_b)
Definition: validation.cpp:267
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:101
@ ADDON_UNKNOWN
a.k.a.
Definition: validation.hpp:102
@ ADDON_THEME
GUI2 Themes.
Definition: validation.hpp:114
@ ADDON_SP_SCENARIO
Single-player scenario.
Definition: validation.hpp:105
@ ADDON_MP_SCENARIO
Multiplayer scenario.
Definition: validation.hpp:108
@ ADDON_MP_CAMPAIGN
Multiplayer campaign.
Definition: validation.hpp:107
@ ADDON_MP_FACTION
Multiplayer faction.
Definition: validation.hpp:111
@ ADDON_TYPES_COUNT
Definition: validation.hpp:116
@ ADDON_MEDIA
Miscellaneous content/media (unit packs, terrain packs, music packs, etc.).
Definition: validation.hpp:113
@ ADDON_MP_ERA
Multiplayer era.
Definition: validation.hpp:110
@ ADDON_CORE
Total Conversion Core.
Definition: validation.hpp:103
@ ADDON_SP_CAMPAIGN
Single-player campaign.
Definition: validation.hpp:104
@ ADDON_OTHER
an add-on that fits in no other category
Definition: validation.hpp:115
@ ADDON_SP_MP_CAMPAIGN
Hybrid campaign.
Definition: validation.hpp:106
@ ADDON_MP_MAPS
Multiplayer plain (no WML) map pack.
Definition: validation.hpp:109
@ ADDON_MOD
Modification of the game.
Definition: validation.hpp:112
std::string get_addon_type_string(ADDON_TYPE type)
Definition: validation.cpp:196