LCOV - code coverage report
Current view: top level - src/util - readwritefile.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 13 27 48.1 %
Date: 2023-10-05 15:40:34 Functions: 3 3 100.0 %
Branches: 4 30 13.3 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2015-2022 The Bitcoin Core developers
       2                 :            : // Copyright (c) 2017 The Zcash developers
       3                 :            : // Distributed under the MIT software license, see the accompanying
       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :            : 
       6                 :            : #include <util/readwritefile.h>
       7                 :            : 
       8                 :            : #include <util/fs.h>
       9                 :            : 
      10                 :            : #include <algorithm>
      11                 :            : #include <cstdio>
      12                 :            : #include <limits>
      13                 :            : #include <string>
      14                 :            : #include <utility>
      15                 :            : 
      16                 :         12 : std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize)
      17                 :            : {
      18                 :         12 :     FILE *f = fsbridge::fopen(filename, "rb");
      19         [ -  + ]:         12 :     if (f == nullptr)
      20                 :         12 :         return std::make_pair(false,"");
      21                 :          0 :     std::string retval;
      22                 :            :     char buffer[128];
      23                 :          0 :     do {
      24   [ #  #  #  # ]:          0 :         const size_t n = fread(buffer, 1, std::min(sizeof(buffer), maxsize - retval.size()), f);
      25                 :            :         // Check for reading errors so we don't return any data if we couldn't
      26                 :            :         // read the entire file (or up to maxsize)
      27         [ #  # ]:          0 :         if (ferror(f)) {
      28         [ #  # ]:          0 :             fclose(f);
      29   [ #  #  #  # ]:          0 :             return std::make_pair(false,"");
      30                 :            :         }
      31         [ #  # ]:          0 :         retval.append(buffer, buffer+n);
      32   [ #  #  #  # ]:          0 :     } while (!feof(f) && retval.size() < maxsize);
      33         [ #  # ]:          0 :     fclose(f);
      34         [ #  # ]:          0 :     return std::make_pair(true,retval);
      35                 :         12 : }
      36                 :            : 
      37                 :       3432 : bool WriteBinaryFile(const fs::path &filename, const std::string &data)
      38                 :            : {
      39                 :       3432 :     FILE *f = fsbridge::fopen(filename, "wb");
      40         [ +  - ]:       3432 :     if (f == nullptr)
      41                 :          0 :         return false;
      42         [ -  + ]:       3432 :     if (fwrite(data.data(), 1, data.size(), f) != data.size()) {
      43                 :          0 :         fclose(f);
      44                 :          0 :         return false;
      45                 :            :     }
      46         [ +  - ]:       3432 :     if (fclose(f) != 0) {
      47                 :          0 :         return false;
      48                 :            :     }
      49                 :       3432 :     return true;
      50                 :       3432 : }

Generated by: LCOV version 1.14