summaryrefslogtreecommitdiff
path: root/src/Hash.cpp
blob: 442369e91b2ce52da8653bae4f5f54769ffb8138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (C) 2020-2022 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include "Hash.hpp"

#include "Fd.hpp"
#include "Logging.hpp"
#include "fmtmacros.hpp"

#include <core/wincompat.hpp>
#include <util/file.hpp>

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

const std::string_view HASH_DELIMITER("\000cCaChE\000", 8);

Hash::Hash()
{
  blake3_hasher_init(&m_hasher);
}

void
Hash::enable_debug(std::string_view section_name,
                   FILE* debug_binary,
                   FILE* debug_text)
{
  m_debug_binary = debug_binary;
  m_debug_text = debug_text;

  add_debug_text("=== ");
  add_debug_text(section_name);
  add_debug_text(" ===\n");
}

Digest
Hash::digest() const
{
  // Note that blake3_hasher_finalize doesn't modify the hasher itself, thus it
  // is possible to finalize again after more data has been added.
  Digest digest;
  blake3_hasher_finalize(&m_hasher, digest.bytes(), digest.size());
  return digest;
}

Hash&
Hash::hash_delimiter(std::string_view type)
{
  hash_buffer(HASH_DELIMITER);
  hash_buffer(type);
  hash_buffer(std::string_view("\x00", 1));
  add_debug_text("### ");
  add_debug_text(type);
  add_debug_text("\n");
  return *this;
}

Hash&
Hash::hash(const void* data, size_t size, HashType hash_type)
{
  std::string_view buffer(static_cast<const char*>(data), size);
  hash_buffer(buffer);

  switch (hash_type) {
  case HashType::binary:
    add_debug_text(
      Util::format_base16(static_cast<const uint8_t*>(data), size));
    break;

  case HashType::text:
    add_debug_text(buffer);
    break;
  }

  add_debug_text("\n");
  return *this;
}

Hash&
Hash::hash(std::string_view data)
{
  hash(data.data(), data.length());
  return *this;
}

Hash&
Hash::hash(int64_t x)
{
  hash_buffer(std::string_view(reinterpret_cast<const char*>(&x), sizeof(x)));
  add_debug_text(FMT("{}\n", x));
  return *this;
}

nonstd::expected<void, std::string>
Hash::hash_fd(int fd)
{
  return util::read_fd(
    fd, [this](const void* data, size_t size) { hash(data, size); });
}

nonstd::expected<void, std::string>
Hash::hash_file(const std::string& path)
{
  Fd fd(open(path.c_str(), O_RDONLY | O_BINARY));
  if (!fd) {
    LOG("Failed to open {}: {}", path, strerror(errno));
    return nonstd::make_unexpected(strerror(errno));
  }

  return hash_fd(*fd);
}

void
Hash::hash_buffer(std::string_view buffer)
{
  blake3_hasher_update(&m_hasher, buffer.data(), buffer.size());
  if (!buffer.empty() && m_debug_binary) {
    (void)fwrite(buffer.data(), 1, buffer.size(), m_debug_binary);
  }
}

void
Hash::add_debug_text(std::string_view text)
{
  if (!text.empty() && m_debug_text) {
    (void)fwrite(text.data(), 1, text.length(), m_debug_text);
  }
}