From 28b95bd54a76b27b96e9440bf94a7896934f1bb6 Mon Sep 17 00:00:00 2001 From: Hanju Kim Date: Sat, 31 May 2014 00:06:56 +0200 Subject: [PATCH 1/2] * Skip UTF8 BOM if it exists * Fix when string isn't terminated if fread() reads less size than paramter at FileUtils getData --- .../cocostudio/CCDataReaderHelper.cpp | 18 ++++++++++++++++-- cocos/platform/CCFileUtils.cpp | 8 +++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp b/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp index cea37b4e63..f3a37a41d6 100644 --- a/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp +++ b/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp @@ -1185,8 +1185,22 @@ ContourData *DataReaderHelper::decodeContour(tinyxml2::XMLElement *contourXML, D void DataReaderHelper::addDataFromJsonCache(const std::string& fileContent, DataInfo *dataInfo) { rapidjson::Document json; - - json.Parse<0>(fileContent.c_str()); + rapidjson::StringStream stream(fileContent.c_str()); + + if (fileContent.size() >= 3) { + // Skip BOM if exists + const unsigned char* c = (const unsigned char *)fileContent.c_str(); + unsigned bom = c[0] | (c[1] << 8) | (c[2] << 16); + + if (bom == 0xBFBBEF) // UTF8 BOM + { + stream.Take(); + stream.Take(); + stream.Take(); + } + } + + json.ParseStream<0>(stream); if (json.HasParseError()) { CCLOG("GetParseError %s\n",json.GetParseError()); } diff --git a/cocos/platform/CCFileUtils.cpp b/cocos/platform/CCFileUtils.cpp index af4a22ab26..f929bf821d 100644 --- a/cocos/platform/CCFileUtils.cpp +++ b/cocos/platform/CCFileUtils.cpp @@ -512,6 +512,7 @@ static Data getData(const std::string& filename, bool forString) Data ret; unsigned char* buffer = nullptr; ssize_t size = 0; + size_t readsize; const char* mode = nullptr; if (forString) mode = "rt"; @@ -538,8 +539,13 @@ static Data getData(const std::string& filename, bool forString) buffer = (unsigned char*)malloc(sizeof(unsigned char) * size); } - size = fread(buffer, sizeof(unsigned char), size, fp); + readsize = fread(buffer, sizeof(unsigned char), size, fp); fclose(fp); + + if (forString && readsize < size) + { + buffer[readsize] = '\0'; + } } while (0); if (nullptr == buffer || 0 == size) From 05b992c6289c880c62dabe323a51841973e93de4 Mon Sep 17 00:00:00 2001 From: Hanju Kim Date: Sat, 31 May 2014 00:13:08 +0200 Subject: [PATCH 2/2] Bug fix --- cocos/platform/CCFileUtils.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos/platform/CCFileUtils.cpp b/cocos/platform/CCFileUtils.cpp index f929bf821d..c655dbba03 100644 --- a/cocos/platform/CCFileUtils.cpp +++ b/cocos/platform/CCFileUtils.cpp @@ -548,7 +548,7 @@ static Data getData(const std::string& filename, bool forString) } } while (0); - if (nullptr == buffer || 0 == size) + if (nullptr == buffer || 0 == readsize) { std::string msg = "Get data from file("; msg.append(filename).append(") failed!"); @@ -556,7 +556,7 @@ static Data getData(const std::string& filename, bool forString) } else { - ret.fastSet(buffer, size); + ret.fastSet(buffer, readsize); } return ret;