Commit 4c91f19f authored by Chen Bill's avatar Chen Bill Committed by GitHub

fix EncodeUTF8, DecodeUTF8 (#2540)

* check sizeof(wchar_t)

* EncodeUTF8: use unsigned operand

* DecodeUTF8: use unsigned operand

* EncodeUTF8: check array size

* DecodeUTF8: check array size

* replace sprintf with snprintf

* zero initialize

* remove outdated macro

* remove outdated macro in clzma
parent 0a7a4745
......@@ -49,67 +49,93 @@ public:
return l;
}
// UTF-16/UTF-32 to UTF-8
static int EncodeUTF8(const wchar_t * wsrc, char * str) {
template<size_t N>
static int EncodeUTF8(const wchar_t* wsrc, char(&str)[N]) {
char* pstr = str;
while(*wsrc != 0) {
if(*wsrc < 0x80) {
*str = (char)*wsrc;
++str;
} else if(*wsrc < 0x800) {
str[0] = ((*wsrc >> 6) & 0x1f) | 0xc0;
str[1] = ((*wsrc) & 0x3f) | 0x80;
str += 2;
} else if(*wsrc < 0x10000 && (*wsrc < 0xd800 || *wsrc > 0xdfff)) {
str[0] = ((*wsrc >> 12) & 0xf) | 0xe0;
str[1] = ((*wsrc >> 6) & 0x3f) | 0x80;
str[2] = ((*wsrc) & 0x3f) | 0x80;
str += 3;
} else {
#ifdef _WIN32
unsigned unicode = 0;
unicode |= (*wsrc++ & 0x3ff) << 10;
unicode |= *wsrc & 0x3ff;
unicode += 0x10000;
str[0] = ((unicode >> 18) & 0x7) | 0xf0;
str[1] = ((unicode >> 12) & 0x3f) | 0x80;
str[2] = ((unicode >> 6) & 0x3f) | 0x80;
str[3] = ((unicode) & 0x3f) | 0x80;
#else
str[0] = ((*wsrc >> 18) & 0x7) | 0xf0;
str[1] = ((*wsrc >> 12) & 0x3f) | 0x80;
str[2] = ((*wsrc >> 6) & 0x3f) | 0x80;
str[3] = ((*wsrc) & 0x3f) | 0x80;
#endif // _WIN32
str += 4;
while (*wsrc != 0) {
unsigned cur = *wsrc;
int codepoint_size = 0;
if (cur < 0x80U)
codepoint_size = 1;
else if (cur < 0x800U)
codepoint_size = 2;
else if (cur < 0x10000U && (cur < 0xd800U || cur > 0xdfffU))
codepoint_size = 3;
else
codepoint_size = 4;
if (pstr - str + codepoint_size > N - 1)
break;
switch (codepoint_size) {
case 1:
*pstr = (char)cur;
break;
case 2:
pstr[0] = ((cur >> 6) & 0x1f) | 0xc0;
pstr[1] = (cur & 0x3f) | 0x80;
break;
case 3:
pstr[0] = ((cur >> 12) & 0xf) | 0xe0;
pstr[1] = ((cur >> 6) & 0x3f) | 0x80;
pstr[2] = (cur & 0x3f) | 0x80;
break;
case 4:
if (sizeof(wchar_t) == 2) {
cur = 0;
cur |= ((unsigned)*wsrc & 0x3ff) << 10;
++wsrc;
cur |= (unsigned)*wsrc & 0x3ff;
cur += 0x10000;
}
pstr[0] = ((cur >> 18) & 0x7) | 0xf0;
pstr[1] = ((cur >> 12) & 0x3f) | 0x80;
pstr[2] = ((cur >> 6) & 0x3f) | 0x80;
pstr[3] = (cur & 0x3f) | 0x80;
break;
default:
break;
}
pstr += codepoint_size;
wsrc++;
}
*str = 0;
return str - pstr;
*pstr = 0;
return pstr - str;
}
// UTF-8 to UTF-16/UTF-32
static int DecodeUTF8(const char * src, wchar_t * wstr) {
template<size_t N>
static int DecodeUTF8(const char* src, wchar_t(&wstr)[N]) {
const char* p = src;
wchar_t* wp = wstr;
while(*p != 0) {
if((*p & 0x80) == 0) {
const unsigned cur = (unsigned)*p & 0xff;
int codepoint_size = 0;
if ((cur & 0xf8) == 0xf0) {
if (sizeof(wchar_t) == 2)
codepoint_size = 2;
else
codepoint_size = 1;
}
else
codepoint_size = 1;
if (wp - wstr + codepoint_size > N - 1)
break;
if((cur & 0x80) == 0) {
*wp = *p;
p++;
} else if((*p & 0xe0) == 0xc0) {
} else if((cur & 0xe0) == 0xc0) {
*wp = (((unsigned)p[0] & 0x1f) << 6) | ((unsigned)p[1] & 0x3f);
p += 2;
} else if((*p & 0xf0) == 0xe0) {
} else if((cur & 0xf0) == 0xe0) {
*wp = (((unsigned)p[0] & 0xf) << 12) | (((unsigned)p[1] & 0x3f) << 6) | ((unsigned)p[2] & 0x3f);
p += 3;
} else if((*p & 0xf8) == 0xf0) {
#ifdef _WIN32
unsigned unicode = (((unsigned)p[0] & 0x7) << 18) | (((unsigned)p[1] & 0x3f) << 12) | (((unsigned)p[2] & 0x3f) << 6) | ((unsigned)p[3] & 0x3f);
unicode -= 0x10000;
*wp++ = (unicode >> 10) | 0xd800;
*wp = (unicode & 0x3ff) | 0xdc00;
#else
*wp = (((unsigned)p[0] & 0x7) << 18) | (((unsigned)p[1] & 0x3f) << 12) | (((unsigned)p[2] & 0x3f) << 6) | ((unsigned)p[3] & 0x3f);
#endif // _WIN32
} else if((cur & 0xf8) == 0xf0) {
if (sizeof(wchar_t) == 2) {
unsigned unicode = (((unsigned)p[0] & 0x7) << 18) | (((unsigned)p[1] & 0x3f) << 12) | (((unsigned)p[2] & 0x3f) << 6) | ((unsigned)p[3] & 0x3f);
unicode -= 0x10000;
*wp++ = (unicode >> 10) | 0xd800;
*wp = (unicode & 0x3ff) | 0xdc00;
} else {
*wp = (((unsigned)p[0] & 0x7) << 18) | (((unsigned)p[1] & 0x3f) << 12) | (((unsigned)p[2] & 0x3f) << 6) | ((unsigned)p[3] & 0x3f);
}
p += 4;
} else
p++;
......
......@@ -400,14 +400,14 @@ uint32 DataManager::CardReader(uint32 code, card_data* pData) {
}
byte* DataManager::ScriptReaderEx(const char* script_name, int* slen) {
// default script name: ./script/c%d.lua
char first[256];
char second[256];
char first[256]{};
char second[256]{};
if(mainGame->gameConf.prefer_expansion_script) {
sprintf(first, "expansions/%s", script_name + 2);
sprintf(second, "%s", script_name + 2);
snprintf(first, sizeof first, "expansions/%s", script_name + 2);
snprintf(second, sizeof second, "%s", script_name + 2);
} else {
sprintf(first, "%s", script_name + 2);
sprintf(second, "expansions/%s", script_name + 2);
snprintf(first, sizeof first, "%s", script_name + 2);
snprintf(second, sizeof second, "expansions/%s", script_name + 2);
}
if(ScriptReader(first, slen))
return scriptBuffer;
......@@ -416,7 +416,7 @@ byte* DataManager::ScriptReaderEx(const char* script_name, int* slen) {
}
byte* DataManager::ScriptReader(const char* script_name, int* slen) {
#ifdef _WIN32
wchar_t fname[256];
wchar_t fname[256]{};
BufferIO::DecodeUTF8(script_name, fname);
IReadFile* reader = FileSystem->createAndOpenFile(fname);
#else
......
......@@ -1713,7 +1713,7 @@ void Game::AddDebugMsg(const char* msg) {
}
if (enable_log & 0x2) {
char msgbuf[1040];
sprintf(msgbuf, "[Script Error]: %s", msg);
snprintf(msgbuf, sizeof msgbuf, "[Script Error]: %s", msg);
ErrorLog(msgbuf);
}
}
......
......@@ -233,10 +233,10 @@ irr::video::ITexture* ImageManager::GetTexture(int code, bool fit) {
auto tit = tMap[fit ? 1 : 0].find(code);
if(tit == tMap[fit ? 1 : 0].end()) {
char file[256];
sprintf(file, "expansions/pics/%d.jpg", code);
snprintf(file, sizeof file, "expansions/pics/%d.jpg", code);
irr::video::ITexture* img = GetTextureFromFile(file, width, height);
if(img == NULL) {
sprintf(file, "pics/%d.jpg", code);
snprintf(file, sizeof file, "pics/%d.jpg", code);
img = GetTextureFromFile(file, width, height);
}
if(img == NULL && !mainGame->gameConf.use_image_scale) {
......@@ -260,10 +260,10 @@ irr::video::ITexture* ImageManager::GetBigPicture(int code, float zoom) {
}
irr::video::ITexture* texture;
char file[256];
sprintf(file, "expansions/pics/%d.jpg", code);
snprintf(file, sizeof file, "expansions/pics/%d.jpg", code);
irr::video::IImage* srcimg = driver->createImageFromFile(file);
if(srcimg == NULL) {
sprintf(file, "pics/%d.jpg", code);
snprintf(file, sizeof file, "pics/%d.jpg", code);
srcimg = driver->createImageFromFile(file);
}
if(srcimg == NULL) {
......@@ -289,18 +289,18 @@ int ImageManager::LoadThumbThread() {
imageManager.tThumbLoadingCodes.pop();
imageManager.tThumbLoadingMutex.unlock();
char file[256];
sprintf(file, "expansions/pics/thumbnail/%d.jpg", code);
snprintf(file, sizeof file, "expansions/pics/thumbnail/%d.jpg", code);
irr::video::IImage* img = imageManager.driver->createImageFromFile(file);
if(img == NULL) {
sprintf(file, "pics/thumbnail/%d.jpg", code);
snprintf(file, sizeof file, "pics/thumbnail/%d.jpg", code);
img = imageManager.driver->createImageFromFile(file);
}
if(img == NULL && mainGame->gameConf.use_image_scale) {
sprintf(file, "expansions/pics/%d.jpg", code);
snprintf(file, sizeof file, "expansions/pics/%d.jpg", code);
img = imageManager.driver->createImageFromFile(file);
}
if(img == NULL && mainGame->gameConf.use_image_scale) {
sprintf(file, "pics/%d.jpg", code);
snprintf(file, sizeof file, "pics/%d.jpg", code);
img = imageManager.driver->createImageFromFile(file);
}
if(img != NULL) {
......@@ -345,7 +345,7 @@ irr::video::ITexture* ImageManager::GetTextureThumb(int code) {
if(lit != tThumbLoading.end()) {
if(lit->second != NULL) {
char file[256];
sprintf(file, "pics/thumbnail/%d.jpg", code);
snprintf(file, sizeof file, "pics/thumbnail/%d.jpg", code);
irr::video::ITexture* texture = driver->addTexture(file, lit->second); // textures must be added in the main thread due to OpenGL
lit->second->drop();
tThumb[code] = texture;
......@@ -378,18 +378,18 @@ irr::video::ITexture* ImageManager::GetTextureField(int code) {
auto tit = tFields.find(code);
if(tit == tFields.end()) {
char file[256];
sprintf(file, "expansions/pics/field/%d.png", code);
snprintf(file, sizeof file, "expansions/pics/field/%d.png", code);
irr::video::ITexture* img = GetTextureFromFile(file, 512 * mainGame->xScale, 512 * mainGame->yScale);
if(img == NULL) {
sprintf(file, "expansions/pics/field/%d.jpg", code);
snprintf(file, sizeof file, "expansions/pics/field/%d.jpg", code);
img = GetTextureFromFile(file, 512 * mainGame->xScale, 512 * mainGame->yScale);
}
if(img == NULL) {
sprintf(file, "pics/field/%d.png", code);
snprintf(file, sizeof file, "pics/field/%d.png", code);
img = GetTextureFromFile(file, 512 * mainGame->xScale, 512 * mainGame->yScale);
}
if(img == NULL) {
sprintf(file, "pics/field/%d.jpg", code);
snprintf(file, sizeof file, "pics/field/%d.jpg", code);
img = GetTextureFromFile(file, 512 * mainGame->xScale, 512 * mainGame->yScale);
if(img == NULL) {
tFields[code] = NULL;
......
......@@ -31,12 +31,8 @@
#ifndef __IRR_USTRING_H_INCLUDED__
#define __IRR_USTRING_H_INCLUDED__
#if (__cplusplus > 199711L) || (_MSC_VER >= 1600) || defined(__GXX_EXPERIMENTAL_CXX0X__)
# define USTRING_CPP0X
# if defined(__GXX_EXPERIMENTAL_CXX0X__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)))
# define USTRING_CPP0X_NEWLITERALS
# endif
#endif
#define USTRING_CPP0X
#define USTRING_CPP0X_NEWLITERALS
#include <stdio.h>
#include <string.h>
......
......@@ -74,15 +74,9 @@ typedef unsigned long UInt64;
#else
#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef __int64 Int64;
typedef unsigned __int64 UInt64;
#define UINT64_CONST(n) n
#else
typedef long long int Int64;
typedef unsigned long long int UInt64;
#define UINT64_CONST(n) n ## ULL
#endif
#endif
......
......@@ -388,9 +388,9 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
int flag = 0;
flag += (mainGame->chkBotHand->isChecked() ? 0x1 : 0);
char arg2[8];
sprintf(arg2, "%d", flag);
snprintf(arg2, sizeof arg2, "%d", flag);
char arg3[8];
sprintf(arg3, "%d", mainGame->gameConf.serverport);
snprintf(arg3, sizeof arg3, "%d", mainGame->gameConf.serverport);
execl("./bot", "bot", arg1, arg2, arg3, NULL);
exit(0);
} else {
......
......@@ -176,7 +176,7 @@ public:
bool success = true;
TraversalDir(dir, [dir, &success](const char *name, bool isdir) {
char full_path[256];
sprintf(full_path, "%s/%s", dir, name);
snprintf(full_path, sizeof full_path, "%s/%s", dir, name);
if (isdir)
{
if(!DeleteDir(full_path))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment