diff options
author | Mark Meserve <mark.meserve@ni.com> | 2018-03-13 13:33:42 -0500 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2018-03-13 13:42:28 -0700 |
commit | 6768b46988c0d225da8a76a8b277141179a5b57f (patch) | |
tree | 79cbc9072d5afa95e5d29ce3c01f630e08d238cc /host/lib | |
parent | 772234b452150eeae4764a80aacfefa37eb0a9e4 (diff) | |
download | uhd-6768b46988c0d225da8a76a8b277141179a5b57f.tar.gz uhd-6768b46988c0d225da8a76a8b277141179a5b57f.tar.bz2 uhd-6768b46988c0d225da8a76a8b277141179a5b57f.zip |
x300: improve lvbitx bitstream md5 read time
- Reading line by line avoids loading the entire 25 MB bitstream into memory
since the MD5 hash usually occurs within the first 1% of the file stream
Diffstat (limited to 'host/lib')
-rw-r--r-- | host/lib/transport/nirio/nifpga_lvbitx.cpp | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/host/lib/transport/nirio/nifpga_lvbitx.cpp b/host/lib/transport/nirio/nifpga_lvbitx.cpp index 2d8b63293..ef4a02aff 100644 --- a/host/lib/transport/nirio/nifpga_lvbitx.cpp +++ b/host/lib/transport/nirio/nifpga_lvbitx.cpp @@ -17,21 +17,31 @@ namespace uhd { namespace niusrprio { std::string nifpga_lvbitx::_get_bitstream_checksum(const std::string& file_path) { - std::string checksum; + const boost::regex md5_regex( + "<BitstreamMD5>([a-fA-F0-9]{32})<\\/BitstreamMD5>", + boost::regex::icase); + std::ifstream lvbitx_stream(file_path.c_str()); - if (lvbitx_stream.is_open()) { - std::string lvbitx_contents; - lvbitx_stream.seekg(0, std::ios::end); - lvbitx_contents.reserve(static_cast<size_t>(lvbitx_stream.tellg())); - lvbitx_stream.seekg(0, std::ios::beg); - lvbitx_contents.assign((std::istreambuf_iterator<char>(lvbitx_stream)), std::istreambuf_iterator<char>()); + if (!lvbitx_stream.is_open()) { + return std::string(); + } + + std::string checksum, line; + while (std::getline(lvbitx_stream, line)) + { try { + // short-circuiting the regex search with a simple find is faster + // for cases where the tag doesn't exist boost::smatch md5_match; - if (boost::regex_search(lvbitx_contents, md5_match, boost::regex("<BitstreamMD5>([a-zA-Z0-9]{32})<\\/BitstreamMD5>", boost::regex::icase))) { + if (line.find("<BitstreamMD5>") != std::string::npos && + boost::regex_search(line, md5_match, md5_regex)) + { checksum = std::string(md5_match[1].first, md5_match[1].second); + break; } - } catch (boost::exception&) { - checksum = ""; + } + catch (boost::exception&) { + } } boost::to_upper(checksum); |