Merge pull request #6946 from hanjukim/skip-utf8-bom

Skip UTF8 BOM if it exists and bug fix for unterminated string
This commit is contained in:
minggo 2014-06-19 22:02:38 +08:00
commit e374bd67c7
2 changed files with 25 additions and 5 deletions

View File

@ -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());
}

View File

@ -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,11 +539,16 @@ 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)
if (nullptr == buffer || 0 == readsize)
{
std::string msg = "Get data from file(";
msg.append(filename).append(") failed!");
@ -550,7 +556,7 @@ static Data getData(const std::string& filename, bool forString)
}
else
{
ret.fastSet(buffer, size);
ret.fastSet(buffer, readsize);
}
return ret;