Changeset - r18732:ad27548fd630
[Not reviewed]
master
0 8 0
rubidium - 13 years ago 2011-12-18 18:37:54
rubidium@openttd.org
(svn r23590) -Codechange: make the string validation settings better expandable
8 files changed with 24 insertions and 14 deletions:
0 comments (0 inline, 0 general)
src/misc_gui.cpp
Show inline comments
 
@@ -1099,7 +1099,7 @@ struct QueryStringWindow : public QueryS
 
			QueryStringBaseWindow(max_bytes, max_chars)
 
	{
 
		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
 
		 * characters (not bytes). max_chars also counts the '\0' characters. */
src/network/core/packet.cpp
Show inline comments
 
@@ -283,9 +283,9 @@ uint64 Packet::Recv_uint64()
 
 * Reads a string till it finds a '\0' in the stream.
 
 * @param buffer The buffer to put the data into.
 
 * @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;
 
	char *bufp = buffer;
 
@@ -306,7 +306,7 @@ void Packet::Recv_string(char *buffer, s
 
	}
 
	this->pos = pos;
 

	
 
	str_validate(bufp, last, allow_newlines);
 
	str_validate(bufp, last, settings);
 
}
 

	
 
#endif /* ENABLE_NETWORK */
src/network/core/packet.h
Show inline comments
 
@@ -16,6 +16,7 @@
 

	
 
#include "config.h"
 
#include "core.h"
 
#include "../../string_type.h"
 

	
 
#ifdef ENABLE_NETWORK
 

	
 
@@ -83,7 +84,7 @@ public:
 
	uint16 Recv_uint16();
 
	uint32 Recv_uint32();
 
	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 */
src/network/network_content.cpp
Show inline comments
 
@@ -56,7 +56,7 @@ bool ClientNetworkContentSocketHandler::
 
	p->Recv_string(ci->name, lengthof(ci->name));
 
	p->Recv_string(ci->version, lengthof(ci->name));
 
	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();
 
	for (uint j = 0; j < sizeof(ci->md5sum); j++) {
src/newgrf_gui.cpp
Show inline comments
 
@@ -599,7 +599,7 @@ private:
 
		char *p = this->text + (strncmp("\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
 

	
 
		/* 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. */
 
		*this->lines.Append() = p;
src/string.cpp
Show inline comments
 
@@ -184,10 +184,9 @@ char *CDECL str_fmt(const char *str, ...
 
 * replaces them with a question mark '?' (if not ignored)
 
 * @param str the string to validate
 
 * @param last the last valid character of str
 
 * @param allow_newlines whether newlines should be allowed or ignored
 
 * @param ignore whether to ignore or replace with a question mark
 
 * @param settings the settings for the string validation.
 
 */
 
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. */
 

	
 
@@ -215,16 +214,16 @@ void str_validate(char *str, const char 
 
			do {
 
				*dst++ = *str++;
 
			} while (--len != 0);
 
		} else if (allow_newlines && c == '\n') {
 
		} else if ((settings & SVS_ALLOW_NEWLINE) != 0  && c == '\n') {
 
			*dst++ = *str++;
 
		} else {
 
			if (allow_newlines && c == '\r' && str[1] == '\n') {
 
			if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\r' && str[1] == '\n') {
 
				str += len;
 
				continue;
 
			}
 
			/* Replace the undesirable character with a question mark */
 
			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
 
			 * mean SETX/SETXY and also "eat" the paramater. If this was
src/string_func.h
Show inline comments
 
@@ -39,7 +39,7 @@ int CDECL seprintf(char *str, const char
 

	
 
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);
 
bool strtolower(char *str);
 

	
src/string_type.h
Show inline comments
 
@@ -12,6 +12,8 @@
 
#ifndef STRING_TYPE_H
 
#define STRING_TYPE_H
 

	
 
#include "core/enum_type.hpp"
 

	
 
/** A non-breaking space. */
 
#define NBSP "\xC2\xA0"
 

	
 
@@ -42,4 +44,12 @@ static const WChar CHAR_TD_LRO = 0x202D;
 
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.
 

	
 
/** 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 */
0 comments (0 inline, 0 general)