1 //
2 // Copyright 2013-2014 Ettus Research LLC
3 // Copyright 2018 Ettus Research, a National Instruments Company
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 
8 #include <uhd/transport/nirio/nifpga_lvbitx.h>
9 #include <boost/algorithm/string.hpp>
10 #include <cstdlib>
11 #include <fstream>
12 #include <regex>
13 #include <streambuf>
14 #include <string>
15 
16 namespace uhd { namespace niusrprio {
17 
_get_bitstream_checksum(const std::string & file_path)18 std::string nifpga_lvbitx::_get_bitstream_checksum(const std::string& file_path)
19 {
20     const std::regex md5_regex(
21         "<BitstreamMD5>([a-fA-F0-9]{32})<\\/BitstreamMD5>", std::regex::icase);
22 
23     std::ifstream lvbitx_stream(file_path.c_str());
24     if (!lvbitx_stream.is_open()) {
25         return std::string();
26     }
27 
28     std::string checksum, line;
29     while (std::getline(lvbitx_stream, line)) {
30         try {
31             // short-circuiting the regex search with a simple find is faster
32             // for cases where the tag doesn't exist
33             std::smatch md5_match;
34             if (line.find("<BitstreamMD5>") != std::string::npos
35                 && std::regex_search(line, md5_match, md5_regex)) {
36                 checksum = std::string(md5_match[1].first, md5_match[1].second);
37                 break;
38             }
39         } catch (boost::exception&) {
40         }
41     }
42     boost::to_upper(checksum);
43     return checksum;
44 }
45 
46 }} // namespace uhd::niusrprio
47