Merge pull request #2186 from schellap/bom

Strip BOM from JSON file streams
This commit is contained in:
Senthil 2016-04-01 06:32:44 -07:00
commit be1c55f061
6 changed files with 54 additions and 5 deletions

View file

@ -362,6 +362,11 @@ bool deps_json_t::load(bool portable, const pal::string_t& deps_path, const rid_
return false;
}
if (skip_utf8_bom(&file))
{
trace::verbose(_X("UTF-8 BOM skipped while reading [%s]"), deps_path.c_str());
}
try
{
const auto json = json_value::parse(file);
@ -376,8 +381,10 @@ bool deps_json_t::load(bool portable, const pal::string_t& deps_path, const rid_
return (portable) ? load_portable(json, name, rid_fallback_graph) : load_standalone(json, name);
}
catch (...)
catch (const web::json::json_exception& je)
{
pal::string_t jes = pal::to_palstring(je.what());
trace::error(_X("A JSON parsing exception occurred in [%s]: %s"), deps_path.c_str(), jes.c_str());
return false;
}
}

View file

@ -83,6 +83,11 @@ pal::string_t fx_muxer_t::resolve_cli_version(const pal::string_t& global_json)
return retval;
}
if (skip_utf8_bom(&file))
{
trace::verbose(_X("UTF-8 BOM skipped while reading [%s]"), global_json.c_str());
}
try
{
const auto root = json_value::parse(file);
@ -106,7 +111,7 @@ pal::string_t fx_muxer_t::resolve_cli_version(const pal::string_t& global_json)
catch (const web::json::json_exception& je)
{
pal::string_t jes = pal::to_palstring(je.what());
trace::info(_X("A JSON parsing exception occurred: %s"), jes.c_str());
trace::error(_X("A JSON parsing exception occurred in [%s]: %s"), global_json.c_str(), jes.c_str());
}
trace::verbose(_X("CLI version is [%s] in global json file [%s]"), retval.c_str(), global_json.c_str());
return retval;

View file

@ -91,6 +91,11 @@ bool runtime_config_t::ensure_parsed()
return false;
}
if (skip_utf8_bom(&file))
{
trace::verbose(_X("UTF-8 BOM skipped while reading [%s]"), m_path.c_str());
}
try
{
const auto root = json_value::parse(file);
@ -104,7 +109,7 @@ bool runtime_config_t::ensure_parsed()
catch (const web::json::json_exception& je)
{
pal::string_t jes = pal::to_palstring(je.what());
trace::info(_X("A JSON parsing exception occurred: %s"), jes.c_str());
trace::error(_X("A JSON parsing exception occurred in [%s]: %s"), m_path.c_str(), jes.c_str());
return false;
}
return true;

View file

@ -259,8 +259,12 @@ void pal::readdir(const string_t& path, const string_t& pattern, std::vector<pal
string_t search_string(path);
append_path(&search_string, pattern.c_str());
WIN32_FIND_DATAW data;
auto handle = ::FindFirstFileW(search_string.c_str(), &data);
WIN32_FIND_DATAW data = { 0 };
auto handle = ::FindFirstFileExW(search_string.c_str(), FindExInfoStandard, &data, FindExSearchNameMatch, NULL, 0);
if (handle == INVALID_HANDLE_VALUE)
{
return;
}
do
{
string_t filepath(data.cFileName);

View file

@ -191,3 +191,30 @@ bool parse_known_args(
return true;
}
// Try to match 0xEF 0xBB 0xBF byte sequence (no endianness here.)
bool skip_utf8_bom(pal::ifstream_t* stream)
{
if (stream->eof() || !stream->good())
{
return false;
}
int peeked = stream->peek();
if (peeked == EOF || ((peeked & 0xFF) != 0xEF))
{
return false;
}
unsigned char bytes[3];
stream->read((char*) bytes, 3);
if ((stream->gcount() < 3) ||
(bytes[1] != 0xBB) ||
(bytes[2] != 0xBF))
{
// Reset to 0 if returning false.
stream->seekg(0, stream->beg);
return false;
}
return true;
}

View file

@ -26,4 +26,5 @@ bool parse_known_args(
const std::vector<pal::string_t>& known_opts,
std::unordered_map<pal::string_t, pal::string_t>* opts,
int* num_args);
bool skip_utf8_bom(pal::ifstream_t* stream);
#endif