|
| 1 | +// Copyright 2019-2026 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// \file HashStreamBuf.h |
| 13 | +/// \author Felix Weiglhofer |
| 14 | +/// \brief Stream buffer that supports on the fly hashing with an optional backing file buffer, |
| 15 | +// if we want to access the hash and file contents at the same time. |
| 16 | + |
| 17 | +#include "Framework/SHA1.h" |
| 18 | + |
| 19 | +#include <cstdint> |
| 20 | +#include <streambuf> |
| 21 | +#include <ostream> |
| 22 | + |
| 23 | +class HashStreamBuf : public std::streambuf |
| 24 | +{ |
| 25 | + public: |
| 26 | + HashStreamBuf() = default; |
| 27 | + |
| 28 | + HashStreamBuf(bool doHash, |
| 29 | + std::streambuf* backing = nullptr) |
| 30 | + : mBacking(backing), |
| 31 | + mDoHash(doHash) |
| 32 | + { |
| 33 | + o2::framework::internal::SHA1Init(&mSHA1); |
| 34 | + } |
| 35 | + |
| 36 | + // Returns the hash without modifying the current hashing state. |
| 37 | + std::string hash() const |
| 38 | + { |
| 39 | + auto copy = mSHA1; |
| 40 | + unsigned char digest[20]; |
| 41 | + o2::framework::internal::SHA1Final(digest, ©); |
| 42 | + |
| 43 | + static constexpr char hex[] = "0123456789ABCDEF"; |
| 44 | + std::string result; |
| 45 | + result.reserve(40); |
| 46 | + |
| 47 | + for (unsigned char byte : digest) { |
| 48 | + result += hex[byte >> 4]; |
| 49 | + result += hex[byte & 0x0f]; |
| 50 | + } |
| 51 | + |
| 52 | + return result; |
| 53 | + } |
| 54 | + |
| 55 | + protected: |
| 56 | + std::streamsize xsputn(const char* data, |
| 57 | + std::streamsize size) override |
| 58 | + { |
| 59 | + if (size <= 0) { |
| 60 | + size = 0; |
| 61 | + } |
| 62 | + |
| 63 | + if (mBacking) { |
| 64 | + // Only hash bytes that were successfully written |
| 65 | + // to the backing stream. |
| 66 | + size = mBacking->sputn(data, size); |
| 67 | + } |
| 68 | + |
| 69 | + updateHash(data, size_t(size)); |
| 70 | + return size; |
| 71 | + } |
| 72 | + |
| 73 | + int_type overflow(int_type ch) override |
| 74 | + { |
| 75 | + if (traits_type::eq_int_type(ch, traits_type::eof())) |
| 76 | + return traits_type::not_eof(ch); |
| 77 | + |
| 78 | + const char c = traits_type::to_char_type(ch); |
| 79 | + |
| 80 | + if (mBacking) { |
| 81 | + const auto result = mBacking->sputc(c); |
| 82 | + |
| 83 | + if (traits_type::eq_int_type(result, |
| 84 | + traits_type::eof())) { |
| 85 | + return traits_type::eof(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + updateHash(&c, 1); |
| 90 | + |
| 91 | + return ch; |
| 92 | + } |
| 93 | + |
| 94 | + int sync() override |
| 95 | + { |
| 96 | + int s = 0; |
| 97 | + if (mBacking) { |
| 98 | + s = mBacking->pubsync(); |
| 99 | + } |
| 100 | + return s; |
| 101 | + } |
| 102 | + |
| 103 | + private: |
| 104 | + std::streambuf* mBacking = nullptr; |
| 105 | + o2::framework::internal::SHA1_CTX mSHA1; |
| 106 | + bool mDoHash = false; |
| 107 | + |
| 108 | + void updateHash(const char* data, size_t size) |
| 109 | + { |
| 110 | + if (mDoHash) { |
| 111 | + o2::framework::internal::SHA1Update(&mSHA1, reinterpret_cast<const unsigned char*>(data), size); |
| 112 | + } |
| 113 | + } |
| 114 | +}; |
0 commit comments