Refactor checksum calculation

This commit is contained in:
Yuriy Skalko 2020-11-21 15:40:31 +02:00
parent 0b1e0b8610
commit cbad214cdd
7 changed files with 95 additions and 44 deletions

View File

@ -25,8 +25,8 @@
#include "support/lyxtime.h" #include "support/lyxtime.h"
#include "support/Package.h" #include "support/Package.h"
#include "support/checksum.h"
#include "support/lassert.h" #include "support/lassert.h"
#include <boost/crc.hpp>
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
@ -41,14 +41,6 @@ namespace lyx {
namespace { namespace {
unsigned long do_crc(string const & s)
{
boost::crc_32_type crc;
crc = for_each(s.begin(), s.end(), crc);
return crc.checksum();
}
// FIXME THREAD // FIXME THREAD
// This should be OK because it is only assigned during init() // This should be OK because it is only assigned during init()
static FileName cache_dir; static FileName cache_dir;
@ -62,7 +54,8 @@ public:
: timestamp(t), checksum(c) : timestamp(t), checksum(c)
{ {
ostringstream os; ostringstream os;
os << setw(10) << setfill('0') << do_crc(orig_from.absFileName()) os << setw(10) << setfill('0')
<< support::checksum(orig_from.absFileName())
<< '-' << to_format; << '-' << to_format;
cache_name = FileName(addName(cache_dir.absFileName(), os.str())); cache_name = FileName(addName(cache_dir.absFileName(), os.str()));
LYXERR(Debug::FILES, "Add file cache item " << orig_from LYXERR(Debug::FILES, "Add file cache item " << orig_from

View File

@ -59,6 +59,7 @@
#include "insets/InsetText.h" #include "insets/InsetText.h"
#include "support/checksum.h"
#include "support/convert.h" #include "support/convert.h"
#include "support/debug.h" #include "support/debug.h"
#include "support/ExceptionMessage.h" #include "support/ExceptionMessage.h"
@ -146,8 +147,6 @@
#include <QMacPasteboardMime> #include <QMacPasteboardMime>
#endif // Q_OS_MAC #endif // Q_OS_MAC
#include <boost/crc.hpp>
#include <exception> #include <exception>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
@ -1708,9 +1707,7 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
&& !is_open)) { && !is_open)) {
// We want the ui session to be saved per document and not per // We want the ui session to be saved per document and not per
// window number. The filename crc is a good enough identifier. // window number. The filename crc is a good enough identifier.
boost::crc_32_type crc; createView(support::checksum(fname));
crc = for_each(fname.begin(), fname.end(), crc);
createView(crc.checksum());
current_view_->openDocument(fname); current_view_->openDocument(fname);
if (!current_view_->documentBufferView()) if (!current_view_->documentBufferView())
current_view_->close(); current_view_->close();
@ -2670,10 +2667,8 @@ void GuiApplication::restoreGuiSession()
FileName const & file_name = last.file_name; FileName const & file_name = last.file_name;
if (!current_view_ || (!lyxrc.open_buffers_in_tabs if (!current_view_ || (!lyxrc.open_buffers_in_tabs
&& current_view_->documentBufferView() != 0)) { && current_view_->documentBufferView() != 0)) {
boost::crc_32_type crc;
string const & fname = file_name.absFileName(); string const & fname = file_name.absFileName();
crc = for_each(fname.begin(), fname.end(), crc); createView(support::checksum(fname));
createView(crc.checksum());
} }
current_view_->loadDocument(file_name, false); current_view_->loadDocument(file_name, false);

View File

@ -35,6 +35,8 @@
#include "frontends/alert.h" #include "frontends/alert.h"
#include "support/checksum.h"
#include <QApplication> #include <QApplication>
#include <QBuffer> #include <QBuffer>
#include <QClipboard> #include <QClipboard>
@ -47,8 +49,6 @@
#include <QTextDocument> #include <QTextDocument>
#include <QTimer> #include <QTimer>
#include <boost/crc.hpp>
#include <memory> #include <memory>
#include <map> #include <map>
#include <iostream> #include <iostream>
@ -431,11 +431,8 @@ void GuiClipboard::put(string const & lyx, docstring const & html, docstring con
data->setData(lyxMimeType(), qlyx); data->setData(lyxMimeType(), qlyx);
// If the OS has not the concept of clipboard ownership, // If the OS has not the concept of clipboard ownership,
// we recognize internal data through its checksum. // we recognize internal data through its checksum.
if (!hasInternal()) { if (!hasInternal())
boost::crc_32_type crc32; checksum = support::checksum(lyx);
crc32.process_bytes(lyx.c_str(), lyx.size());
checksum = crc32.checksum();
}
} }
// Don't test for text.empty() since we want to be able to clear the // Don't test for text.empty() since we want to be able to clear the
// clipboard. // clipboard.
@ -528,9 +525,7 @@ bool GuiClipboard::isInternal() const
// ourself by comparing its checksum with the stored one. // ourself by comparing its checksum with the stored one.
QByteArray const ar = cache_.data(lyxMimeType()); QByteArray const ar = cache_.data(lyxMimeType());
string const data(ar.data(), ar.count()); string const data(ar.data(), ar.count());
boost::crc_32_type crc32; return checksum == static_cast<std::uint32_t>(support::checksum(data));
crc32.process_bytes(data.c_str(), data.size());
return checksum == crc32.checksum();
} }

View File

@ -35,7 +35,7 @@
#include <QThread> #include <QThread>
#endif #endif
#include <boost/crc.hpp> #include "support/checksum.h"
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
@ -537,20 +537,14 @@ bool FileName::link(FileName const & name) const
unsigned long checksum_ifstream_fallback(char const * file) unsigned long checksum_ifstream_fallback(char const * file)
{ {
unsigned long result = 0;
//LYXERR(Debug::FILES, "lyx::sum() using istreambuf_iterator (fast)"); //LYXERR(Debug::FILES, "lyx::sum() using istreambuf_iterator (fast)");
ifstream ifs(file, ios_base::in | ios_base::binary); ifstream ifs(file, ios_base::in | ios_base::binary);
if (!ifs) if (!ifs)
return result; return 0;
return support::checksum(ifs);
istreambuf_iterator<char> beg(ifs);
istreambuf_iterator<char> end;
boost::crc_32_type crc;
crc = for_each(beg, end, crc);
result = crc.checksum();
return result;
} }
unsigned long FileName::checksum() const unsigned long FileName::checksum() const
{ {
if (!exists()) { if (!exists()) {
@ -583,11 +577,9 @@ unsigned long FileName::checksum() const
qint64 size = fi.size(); qint64 size = fi.size();
uchar * ubeg = qf.map(0, size); uchar * ubeg = qf.map(0, size);
uchar * uend = ubeg + size; uchar * uend = ubeg + size;
boost::crc_32_type ucrc; result = support::checksum(ubeg, uend);
ucrc.process_block(ubeg, uend);
qf.unmap(ubeg); qf.unmap(ubeg);
qf.close(); qf.close();
result = ucrc.checksum();
#else // QT_VERSION #else // QT_VERSION
@ -619,9 +611,7 @@ unsigned long FileName::checksum() const
char * beg = static_cast<char*>(mm); char * beg = static_cast<char*>(mm);
char * end = beg + info.st_size; char * end = beg + info.st_size;
boost::crc_32_type crc; result = support::checksum(beg, end);
crc.process_block(beg, end);
result = crc.checksum();
munmap(mm, info.st_size); munmap(mm, info.st_size);
close(fd); close(fd);

View File

@ -38,6 +38,8 @@ liblyxsupport_a_SOURCES = \
bind.h \ bind.h \
Cache.h \ Cache.h \
Changer.h \ Changer.h \
checksum.cpp \
checksum.h \
ConsoleApplication.cpp \ ConsoleApplication.cpp \
ConsoleApplication.h \ ConsoleApplication.h \
ConsoleApplicationPrivate.h \ ConsoleApplicationPrivate.h \

46
src/support/checksum.cpp Normal file
View File

@ -0,0 +1,46 @@
// -*- C++ -*-
/**
* \file checksum.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Yuriy Skalko
*
* Full author contact details are available in file CREDITS.
*/
#include "support/checksum.h"
#include "boost/crc.hpp"
#include <iterator>
namespace lyx {
namespace support {
unsigned long checksum(std::string const & s)
{
boost::crc_32_type crc;
crc.process_bytes(s.c_str(), s.size());
return crc.checksum();
}
unsigned long checksum(std::ifstream & ifs)
{
std::istreambuf_iterator<char> beg(ifs);
std::istreambuf_iterator<char> end;
boost::crc_32_type crc;
crc = for_each(beg, end, crc);
return crc.checksum();
}
unsigned long checksum(char const * beg, char const * end)
{
boost::crc_32_type crc;
crc.process_block(beg, end);
return crc.checksum();
}
} // namespace support
} // namespace lyx

30
src/support/checksum.h Normal file
View File

@ -0,0 +1,30 @@
// -*- C++ -*-
/**
* \file checksum.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Yuriy Skalko
*
* Full author contact details are available in file CREDITS.
*/
#ifndef LYX_CHECKSUM_H
#define LYX_CHECKSUM_H
#include <fstream>
#include <string>
namespace lyx {
namespace support {
unsigned long checksum(std::string const & s);
unsigned long checksum(std::ifstream & ifs);
unsigned long checksum(char const * beg, char const * end);
} // namespace support
} // namespace lyx
#endif // LYX_CHECKSUM_H