RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
ChecksumFile.cpp
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2018 Roman Lebedev
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#include "common/ChecksumFile.h"
22#include "common/Common.h"
24#include "io/FileReader.h"
25#include <cassert>
26#include <string>
27#include <vector>
28
29namespace rawspeed {
30
31namespace {
32
33// The length of the sha256 digest (256-bit, 64 hexadecimal chars).
34constexpr auto Sha256CheckSumLength = 64;
35// The separator after the digest and before filename.
36// Should be either " " or " b".
37constexpr auto CheckSumSeparatorWidth = 2;
38
40 const std::string& RootDir) {
42
43 // We are just assuming that the checksum file is correct and valid.
44 // It is up to user to validate it first (via actually running `sha256sum
45 // -c`).
46
47 static constexpr auto Offset = Sha256CheckSumLength + CheckSumSeparatorWidth;
48
49 if (Line.size() <= Offset)
50 ThrowRSE("Malformed checksum line: \"%s\"", Line.c_str());
51
52 Entry.RelFileName = Line.substr(Offset);
53 assert(!Entry.RelFileName.empty());
54 assert(Entry.RelFileName.back() != '\n');
55
56 Entry.FullFileName = RootDir + "/" + Entry.RelFileName;
57
58 return Entry;
59}
60
61} // namespace
62
63std::vector<ChecksumFileEntry>
64ParseChecksumFileContent(const std::string& ChecksumFileContent,
65 const std::string& RootDir) {
66 std::vector<ChecksumFileEntry> Listing;
67
68 const std::vector<std::string> Lines = splitString(ChecksumFileContent, '\n');
69
70 Listing.reserve(Lines.size());
71
72 for (const auto& Line : Lines) {
73 assert(!Line.empty());
74 Listing.emplace_back(ParseChecksumFileLine(Line, RootDir));
75 }
76
77 return Listing;
78}
79
80std::vector<ChecksumFileEntry>
81ReadChecksumFile(const std::string& RootDir,
82 const std::string& ChecksumFileBasename) {
83 const std::string ChecksumFileName = RootDir + "/" + ChecksumFileBasename;
84 FileReader FR(ChecksumFileName.c_str());
85
86 const auto [storage, buf] = FR.readFile();
87 const std::string ChecksumFileContent(
88 reinterpret_cast<const char*>(buf.begin()), buf.getSize());
89
90 return ParseChecksumFileContent(ChecksumFileContent, RootDir);
91}
92
93} // namespace rawspeed
#define ThrowRSE(...)
assert(dim.area() >=area)
std::pair< std::unique_ptr< std::vector< uint8_t, DefaultInitAllocatorAdaptor< uint8_t, AlignedAllocator< uint8_t, 16 > > > >, Buffer > readFile() const
ChecksumFileEntry ParseChecksumFileLine(const std::string &Line, const std::string &RootDir)
std::vector< ChecksumFileEntry > ReadChecksumFile(const std::string &RootDir, const std::string &ChecksumFileBasename)
std::vector< std::string > splitString(const std::string &input, char c=' ')
Definition Common.h:178
void RAWSPEED_UNLIKELY_FUNCTION RAWSPEED_NOINLINE static char buf[bufSize]
std::vector< ChecksumFileEntry > ParseChecksumFileContent(const std::string &ChecksumFileContent, const std::string &RootDir)
std::string RelFileName
std::string FullFileName