提交 38483fe7 编写于 作者: V Vladislav Sovrasov

Skip UTF-8 BOM in FileStorage

上级 6c125331
......@@ -96,6 +96,15 @@ static inline bool cv_isspace(char c)
return (9 <= c && c <= 13) || c == ' ';
}
static inline char* cv_skip_BOM(char* ptr)
{
if((uchar)ptr[0] == 0xef && (uchar)ptr[1] == 0xbb && (uchar)ptr[2] == 0xbf) //UTF-8 BOM
{
return ptr + 3;
}
return ptr;
}
static char* icv_itoa( int _val, char* buffer, int /*radix*/ )
{
const int radix = 10;
......@@ -4399,10 +4408,13 @@ cvOpenFileStorage( const char* query, CvMemStorage* dststorage, int flags, const
const char* json_signature = "{";
char buf[16];
icvGets( fs, buf, sizeof(buf)-2 );
char* bufPtr = cv_skip_BOM(buf);
size_t bufOffset = bufPtr - buf;
fs->fmt
= strncmp( buf, yaml_signature, strlen(yaml_signature) ) == 0
= strncmp( bufPtr, yaml_signature, strlen(yaml_signature) ) == 0
? CV_STORAGE_FORMAT_YAML
: strncmp( buf, json_signature, strlen(json_signature) ) == 0
: strncmp( bufPtr, json_signature, strlen(json_signature) ) == 0
? CV_STORAGE_FORMAT_JSON
: CV_STORAGE_FORMAT_XML
;
......@@ -4420,6 +4432,7 @@ cvOpenFileStorage( const char* query, CvMemStorage* dststorage, int flags, const
buf_size = MAX( buf_size, (size_t)(CV_FS_MAX_LEN*2 + 1024) );
}
icvRewind(fs);
fs->strbufpos = bufOffset;
fs->str_hash = cvCreateMap( 0, sizeof(CvStringHash),
sizeof(CvStringHashNode), fs->memstorage, 256 );
......
......@@ -928,3 +928,25 @@ TEST(Core_InputOutput, filestorage_json_comment)
EXPECT_EQ(str, String("value"));
}
TEST(Core_InputOutput, filestorage_utf8_bom)
{
EXPECT_NO_THROW(
{
String content ="\xEF\xBB\xBF<?xml version=\"1.0\"?>\n<opencv_storage>\n</opencv_storage>\n";
cv::FileStorage fs(content, cv::FileStorage::READ | cv::FileStorage::MEMORY);
fs.release();
});
EXPECT_NO_THROW(
{
String content ="\xEF\xBB\xBF%YAML:1.0\n";
cv::FileStorage fs(content, cv::FileStorage::READ | cv::FileStorage::MEMORY);
fs.release();
});
EXPECT_NO_THROW(
{
String content ="\xEF\xBB\xBF{\n}\n";
cv::FileStorage fs(content, cv::FileStorage::READ | cv::FileStorage::MEMORY);
fs.release();
});
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册