The Battle for Wesnoth  1.19.8+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  ICON_TOO_LARGE = 0x2C0, /**< The add-on's icon is too large (presumably a DataURI) */
69  //
70  // Server errors
71  //
72  SERVER_UNSPECIFIED = 0xF000, /**< Unspecified server error */
73  SERVER_READ_ONLY = 0xF001, /**< Server read-only mode on */
74  SERVER_ADDONS_LIST = 0xF002, /**< Corrupted server add-ons list */
75  SERVER_DELTA_NO_VERSIONS = 0xF003, /**< No versions to deltify against */
76  SERVER_FORUM_AUTH_DISABLED = 0xF004, /**< The remote add-ons server does not support forum authorization */
77 };
78 
79 std::string addon_check_status_desc(unsigned int code);
80 
82 {
83  return addon_check_status_desc(static_cast<unsigned int>(code));
84 }
85 
86 std::string translated_addon_check_status(unsigned int code);
87 
89 {
90  return translated_addon_check_status(static_cast<unsigned int>(code));
91 }
92 
93 /**
94  * Values used for add-on classification; UI-only
95  * at the moment, in the future it could be used for
96  * directory allocation too, removing the need for
97  * the ADDON_GROUP constants (TODO).
98  *
99  * @note If you change the order or content of these, you'll also need
100  * to update the @a addon_type_strings table found in validation.cpp.
101  */
103  ADDON_UNKNOWN, /**< a.k.a. anything. */
104  ADDON_CORE, /**< Total Conversion Core. */
105  ADDON_SP_CAMPAIGN, /**< Single-player campaign. */
106  ADDON_SP_SCENARIO, /**< Single-player scenario. */
107  ADDON_SP_MP_CAMPAIGN, /**< Hybrid campaign. */
108  ADDON_MP_CAMPAIGN, /**< Multiplayer campaign. */
109  ADDON_MP_SCENARIO, /**< Multiplayer scenario. */
110  ADDON_MP_MAPS, /**< Multiplayer plain (no WML) map pack. */
111  ADDON_MP_ERA, /**< Multiplayer era. */
112  ADDON_MP_FACTION, /**< Multiplayer faction. */
113  ADDON_MOD, /**< Modification of the game. */
114  ADDON_MEDIA, /**< Miscellaneous content/media (unit packs, terrain packs, music packs, etc.). */
115  ADDON_THEME, /**< GUI2 Themes */
116  ADDON_OTHER, /**< an add-on that fits in no other category */
118 };
119 
120 ADDON_TYPE get_addon_type(const std::string& str);
122 
123 /** Checks whether an add-on id/name is legal or not. */
124 bool addon_name_legal(const std::string& name);
125 /** Checks whether an add-on file name is legal or not. */
126 bool addon_filename_legal(const std::string& name);
127 /** Checks whether an add-on icon is too large. */
128 bool addon_icon_too_large(const std::string& icon);
129 constexpr std::size_t max_icon_size = 500'000;
130 
131 /**
132  * Scans an add-on archive for illegal names.
133  *
134  * @param dir The WML container for the root [dir] node where the search
135  * should begin.
136  * @param badlist If provided and not null, any illegal names encountered will
137  * be added to this list. This also makes the archive scan more
138  * thorough instead of returning on the first illegal name
139  * encountered.
140  *
141  * @returns True if no illegal names were found.
142  */
143 bool check_names_legal(const config& dir, std::vector<std::string>* badlist = nullptr);
144 /**
145  * Scans an add-on archive for case-conflicts.
146  *
147  * Case conflicts may cause trouble on case-insensitive filesystems.
148  *
149  * @param dir The WML container for the root [dir] node where the search
150  * should begin.
151  * @param badlist If provided and not null, any case conflicts encountered will
152  * be added to this list. This also makes the archive scan more
153  * thorough instead of returning on the first conflict
154  * encountered.
155  *
156  * @returns True if no conflicts were found.
157  */
158 bool check_case_insensitive_duplicates(const config& dir, std::vector<std::string>* badlist = nullptr);
159 
160 std::string encode_binary(const std::string& str);
161 std::string unencode_binary(const std::string& str);
162 bool needs_escaping(char c);
163 
164 std::string file_hash(const config& file);
165 bool comp_file_hash(const config& file_a, const config& file_b);
166 void write_hashlist(config& hashlist, const config& data);
167 bool contains_hashlist(const config& from, const config& to);
168 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:158
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 addon_icon_too_large(const std::string &icon)
Checks whether an add-on icon is too large.
Definition: validation.cpp:74
bool needs_escaping(char c)
Definition: validation.cpp:210
std::string file_hash(const config &file)
Definition: validation.cpp:262
std::string unencode_binary(const std::string &str)
Definition: validation.cpp:240
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:374
ADDON_TYPE get_addon_type(const std::string &str)
Definition: validation.cpp:184
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:276
bool contains_hashlist(const config &from, const config &to)
Definition: validation.cpp:292
bool check_names_legal(const config &dir, std::vector< std::string > *badlist=nullptr)
Scans an add-on archive for illegal names.
Definition: validation.cpp:170
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:382
constexpr std::size_t max_icon_size
Definition: validation.hpp:129
std::string translated_addon_check_status(unsigned int code)
Definition: validation.cpp:548
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:179
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.
@ ICON_TOO_LARGE
The add-on's icon is too large (presumably a DataURI)
@ 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:222
bool comp_file_hash(const config &file_a, const config &file_b)
Definition: validation.cpp:271
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:102
@ ADDON_UNKNOWN
a.k.a.
Definition: validation.hpp:103
@ ADDON_THEME
GUI2 Themes.
Definition: validation.hpp:115
@ ADDON_SP_SCENARIO
Single-player scenario.
Definition: validation.hpp:106
@ ADDON_MP_SCENARIO
Multiplayer scenario.
Definition: validation.hpp:109
@ ADDON_MP_CAMPAIGN
Multiplayer campaign.
Definition: validation.hpp:108
@ ADDON_MP_FACTION
Multiplayer faction.
Definition: validation.hpp:112
@ ADDON_TYPES_COUNT
Definition: validation.hpp:117
@ ADDON_MEDIA
Miscellaneous content/media (unit packs, terrain packs, music packs, etc.).
Definition: validation.hpp:114
@ ADDON_MP_ERA
Multiplayer era.
Definition: validation.hpp:111
@ ADDON_CORE
Total Conversion Core.
Definition: validation.hpp:104
@ ADDON_SP_CAMPAIGN
Single-player campaign.
Definition: validation.hpp:105
@ ADDON_OTHER
an add-on that fits in no other category
Definition: validation.hpp:116
@ ADDON_SP_MP_CAMPAIGN
Hybrid campaign.
Definition: validation.hpp:107
@ ADDON_MP_MAPS
Multiplayer plain (no WML) map pack.
Definition: validation.hpp:110
@ ADDON_MOD
Modification of the game.
Definition: validation.hpp:113
std::string get_addon_type_string(ADDON_TYPE type)
Definition: validation.cpp:200