mirror of https://github.com/OpenTTD/OpenTTD
(svn r23590) -Codechange: make the string validation settings better expandable
parent
6ae8cac432
commit
fefe22b4aa
|
@ -1099,7 +1099,7 @@ struct QueryStringWindow : public QueryStringBaseWindow
|
||||||
QueryStringBaseWindow(max_bytes, max_chars)
|
QueryStringBaseWindow(max_bytes, max_chars)
|
||||||
{
|
{
|
||||||
GetString(this->edit_str_buf, str, &this->edit_str_buf[max_bytes - 1]);
|
GetString(this->edit_str_buf, str, &this->edit_str_buf[max_bytes - 1]);
|
||||||
str_validate(this->edit_str_buf, &this->edit_str_buf[max_bytes - 1], false, true);
|
str_validate(this->edit_str_buf, &this->edit_str_buf[max_bytes - 1], SVS_NONE);
|
||||||
|
|
||||||
/* Make sure the name isn't too long for the text buffer in the number of
|
/* Make sure the name isn't too long for the text buffer in the number of
|
||||||
* characters (not bytes). max_chars also counts the '\0' characters. */
|
* characters (not bytes). max_chars also counts the '\0' characters. */
|
||||||
|
|
|
@ -283,9 +283,9 @@ uint64 Packet::Recv_uint64()
|
||||||
* Reads a string till it finds a '\0' in the stream.
|
* Reads a string till it finds a '\0' in the stream.
|
||||||
* @param buffer The buffer to put the data into.
|
* @param buffer The buffer to put the data into.
|
||||||
* @param size The size of the buffer.
|
* @param size The size of the buffer.
|
||||||
* @param allow_newlines Whether the string validation should remove newlines.
|
* @param settings The string validation settings.
|
||||||
*/
|
*/
|
||||||
void Packet::Recv_string(char *buffer, size_t size, bool allow_newlines)
|
void Packet::Recv_string(char *buffer, size_t size, StringValidationSettings settings)
|
||||||
{
|
{
|
||||||
PacketSize pos;
|
PacketSize pos;
|
||||||
char *bufp = buffer;
|
char *bufp = buffer;
|
||||||
|
@ -306,7 +306,7 @@ void Packet::Recv_string(char *buffer, size_t size, bool allow_newlines)
|
||||||
}
|
}
|
||||||
this->pos = pos;
|
this->pos = pos;
|
||||||
|
|
||||||
str_validate(bufp, last, allow_newlines);
|
str_validate(bufp, last, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* ENABLE_NETWORK */
|
#endif /* ENABLE_NETWORK */
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
|
#include "../../string_type.h"
|
||||||
|
|
||||||
#ifdef ENABLE_NETWORK
|
#ifdef ENABLE_NETWORK
|
||||||
|
|
||||||
|
@ -83,7 +84,7 @@ public:
|
||||||
uint16 Recv_uint16();
|
uint16 Recv_uint16();
|
||||||
uint32 Recv_uint32();
|
uint32 Recv_uint32();
|
||||||
uint64 Recv_uint64();
|
uint64 Recv_uint64();
|
||||||
void Recv_string(char *buffer, size_t size, bool allow_newlines = false);
|
void Recv_string(char *buffer, size_t size, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* ENABLE_NETWORK */
|
#endif /* ENABLE_NETWORK */
|
||||||
|
|
|
@ -56,7 +56,7 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet *p)
|
||||||
p->Recv_string(ci->name, lengthof(ci->name));
|
p->Recv_string(ci->name, lengthof(ci->name));
|
||||||
p->Recv_string(ci->version, lengthof(ci->name));
|
p->Recv_string(ci->version, lengthof(ci->name));
|
||||||
p->Recv_string(ci->url, lengthof(ci->url));
|
p->Recv_string(ci->url, lengthof(ci->url));
|
||||||
p->Recv_string(ci->description, lengthof(ci->description), true);
|
p->Recv_string(ci->description, lengthof(ci->description), SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
|
||||||
|
|
||||||
ci->unique_id = p->Recv_uint32();
|
ci->unique_id = p->Recv_uint32();
|
||||||
for (uint j = 0; j < sizeof(ci->md5sum); j++) {
|
for (uint j = 0; j < sizeof(ci->md5sum); j++) {
|
||||||
|
|
|
@ -599,7 +599,7 @@ private:
|
||||||
char *p = this->text + (strncmp("\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
|
char *p = this->text + (strncmp("\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
|
||||||
|
|
||||||
/* Make sure the string is a valid UTF-8 sequence. */
|
/* Make sure the string is a valid UTF-8 sequence. */
|
||||||
str_validate(p, this->text + filesize, true);
|
str_validate(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
|
||||||
|
|
||||||
/* Split the string on newlines. */
|
/* Split the string on newlines. */
|
||||||
*this->lines.Append() = p;
|
*this->lines.Append() = p;
|
||||||
|
|
|
@ -184,10 +184,9 @@ char *CDECL str_fmt(const char *str, ...)
|
||||||
* replaces them with a question mark '?' (if not ignored)
|
* replaces them with a question mark '?' (if not ignored)
|
||||||
* @param str the string to validate
|
* @param str the string to validate
|
||||||
* @param last the last valid character of str
|
* @param last the last valid character of str
|
||||||
* @param allow_newlines whether newlines should be allowed or ignored
|
* @param settings the settings for the string validation.
|
||||||
* @param ignore whether to ignore or replace with a question mark
|
|
||||||
*/
|
*/
|
||||||
void str_validate(char *str, const char *last, bool allow_newlines, bool ignore)
|
void str_validate(char *str, const char *last, StringValidationSettings settings)
|
||||||
{
|
{
|
||||||
/* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
|
/* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
|
||||||
|
|
||||||
|
@ -215,16 +214,16 @@ void str_validate(char *str, const char *last, bool allow_newlines, bool ignore)
|
||||||
do {
|
do {
|
||||||
*dst++ = *str++;
|
*dst++ = *str++;
|
||||||
} while (--len != 0);
|
} while (--len != 0);
|
||||||
} else if (allow_newlines && c == '\n') {
|
} else if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\n') {
|
||||||
*dst++ = *str++;
|
*dst++ = *str++;
|
||||||
} else {
|
} else {
|
||||||
if (allow_newlines && c == '\r' && str[1] == '\n') {
|
if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\r' && str[1] == '\n') {
|
||||||
str += len;
|
str += len;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/* Replace the undesirable character with a question mark */
|
/* Replace the undesirable character with a question mark */
|
||||||
str += len;
|
str += len;
|
||||||
if (!ignore) *dst++ = '?';
|
if ((settings & SVS_REPLACE_WITH_QUESTION_MARK) != 0) *dst++ = '?';
|
||||||
|
|
||||||
/* In case of these two special cases assume that they really
|
/* In case of these two special cases assume that they really
|
||||||
* mean SETX/SETXY and also "eat" the paramater. If this was
|
* mean SETX/SETXY and also "eat" the paramater. If this was
|
||||||
|
|
|
@ -39,7 +39,7 @@ int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FO
|
||||||
|
|
||||||
char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
|
char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
|
||||||
|
|
||||||
void str_validate(char *str, const char *last, bool allow_newlines = false, bool ignore = false);
|
void str_validate(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
|
||||||
void str_strip_colours(char *str);
|
void str_strip_colours(char *str);
|
||||||
bool strtolower(char *str);
|
bool strtolower(char *str);
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
#ifndef STRING_TYPE_H
|
#ifndef STRING_TYPE_H
|
||||||
#define STRING_TYPE_H
|
#define STRING_TYPE_H
|
||||||
|
|
||||||
|
#include "core/enum_type.hpp"
|
||||||
|
|
||||||
/** A non-breaking space. */
|
/** A non-breaking space. */
|
||||||
#define NBSP "\xC2\xA0"
|
#define NBSP "\xC2\xA0"
|
||||||
|
|
||||||
|
@ -42,4 +44,12 @@ static const WChar CHAR_TD_LRO = 0x202D; ///< Force the following characters to
|
||||||
static const WChar CHAR_TD_RLO = 0x202E; ///< Force the following characters to be treated as right-to-left characters.
|
static const WChar CHAR_TD_RLO = 0x202E; ///< Force the following characters to be treated as right-to-left characters.
|
||||||
static const WChar CHAR_TD_PDF = 0x202C; ///< Restore the text-direction state to before the last LRE, RLE, LRO or RLO.
|
static const WChar CHAR_TD_PDF = 0x202C; ///< Restore the text-direction state to before the last LRE, RLE, LRO or RLO.
|
||||||
|
|
||||||
|
/** Settings for the string validation. */
|
||||||
|
enum StringValidationSettings {
|
||||||
|
SVS_NONE = 0, ///< Allow nothing and replace nothing.
|
||||||
|
SVS_REPLACE_WITH_QUESTION_MARK = 1 << 0, ///< Replace the unknown/bad bits with question marks.
|
||||||
|
SVS_ALLOW_NEWLINE = 1 << 1, ///< Allow newlines.
|
||||||
|
};
|
||||||
|
DECLARE_ENUM_AS_BIT_SET(StringValidationSettings);
|
||||||
|
|
||||||
#endif /* STRING_TYPE_H */
|
#endif /* STRING_TYPE_H */
|
||||||
|
|
Loading…
Reference in New Issue