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/Package.h"
#include "support/checksum.h"
#include "support/lassert.h"
#include <boost/crc.hpp>
#include <algorithm>
#include <fstream>
@ -41,14 +41,6 @@ namespace lyx {
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
// This should be OK because it is only assigned during init()
static FileName cache_dir;
@ -62,7 +54,8 @@ public:
: timestamp(t), checksum(c)
{
ostringstream os;
os << setw(10) << setfill('0') << do_crc(orig_from.absFileName())
os << setw(10) << setfill('0')
<< support::checksum(orig_from.absFileName())
<< '-' << to_format;
cache_name = FileName(addName(cache_dir.absFileName(), os.str()));
LYXERR(Debug::FILES, "Add file cache item " << orig_from

View File

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

View File

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

View File

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

View File

@ -38,6 +38,8 @@ liblyxsupport_a_SOURCES = \
bind.h \
Cache.h \
Changer.h \
checksum.cpp \
checksum.h \
ConsoleApplication.cpp \
ConsoleApplication.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