Commit 27e529d3 authored by Michael Davis's avatar Michael Davis
Browse files

[checksum] Removes old checksum code

parent afb6d972
......@@ -76,7 +76,6 @@ set (COMMON_LIB_SRC_FILES
dataStructures/VerifyInfo.cpp
dataStructures/WriteTestResult.cpp
dataStructures/utils.cpp
checksum/Checksum.cpp
checksum/ChecksumBlob.cpp
${CMAKE_BINARY_DIR}/eos_cta/cta_common.pb.cc
exception/AcceptConnectionInterrupted.cpp
......
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "common/checksum/Checksum.hpp"
#include "common/utils/Regex.hpp"
#include <sstream>
//------------------------------------------------------------------------------
// checksumTypeToStr
//------------------------------------------------------------------------------
const char *cta::Checksum::checksumTypeToStr(const ChecksumType enumValue)
throw() {
switch(enumValue) {
case CHECKSUMTYPE_NONE : return "NONE";
case CHECKSUMTYPE_ADLER32: return "ADLER32";
default : return "UNKNOWN";
}
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::Checksum::Checksum(): m_type(CHECKSUMTYPE_NONE) { }
cta::Checksum::Checksum(const std::string& url): m_type(CHECKSUMTYPE_NONE) {
if (url.empty() || url == "-") {
return;
}
utils::Regex re("^adler32:0[Xx]([[:xdigit:]]+)$");
auto result = re.exec(url);
if (result.size()) {
m_type = CHECKSUMTYPE_ADLER32;
std::stringstream valStr(result.at(1));
uint32_t val;
valStr >> std::hex >> val;
setNumeric(val);
}
}
//------------------------------------------------------------------------------
// operator==
//------------------------------------------------------------------------------
void cta::Checksum::validate(const Checksum &rhs) const {
if(m_type != rhs.m_type) {
throw exception::ChecksumTypeMismatch(static_cast<std::string>("Checksum type ") +
checksumTypeToStr(m_type) + " does not match " + checksumTypeToStr(rhs.m_type));
}
if(m_byteArray != rhs.m_byteArray) {
throw exception::ChecksumValueMismatch(static_cast<std::string>("Checksum value ") +
m_byteArray + " does not match " + rhs.m_byteArray);
}
}
//------------------------------------------------------------------------------
// operator==
//------------------------------------------------------------------------------
bool cta::Checksum::operator==(const Checksum &rhs) const {
return m_type == rhs.m_type && m_byteArray == rhs.m_byteArray;
}
//------------------------------------------------------------------------------
// getType
//------------------------------------------------------------------------------
cta::Checksum::ChecksumType cta::Checksum::getType() const throw() {
return m_type;
}
//------------------------------------------------------------------------------
// getByteArray
//------------------------------------------------------------------------------
const std::string &cta::Checksum::getByteArray() const throw() {
return m_byteArray;
}
//------------------------------------------------------------------------------
// str
//------------------------------------------------------------------------------
std::string cta::Checksum::str() const {
std::ostringstream oss;
switch(m_type) {
case CHECKSUMTYPE_ADLER32:
oss << "adler32:" << std::hex << std::showbase << getNumeric<uint32_t>();
break;
case CHECKSUMTYPE_NONE:
oss << "-";
break;
default:;
}
return oss.str();
}
//------------------------------------------------------------------------------
// operator<<
//------------------------------------------------------------------------------
std::ostream &cta::operator<<(std::ostream &os, const Checksum &checksum) {
os << checksum.str();
return os;
}
/**
* The CERN Tape Archive (CTA) project
* Copyright (C) 2019 CERN
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <ostream>
#include <sstream>
#include <typeinfo>
#include <common/exception/Exception.hpp>
#include <common/exception/ChecksumTypeMismatch.hpp>
#include <common/exception/ChecksumValueMismatch.hpp>
namespace cta {
/**
* A checksum
*/
class Checksum {
public:
/**
* Enumeration of the supported checksum types
*/
enum ChecksumType {
CHECKSUMTYPE_NONE, //!< No checksum specified
CHECKSUMTYPE_ADLER32, //!< Adler-32 checksum
CHECKSUMTYPE_CRC32, //!< CRC-32 checksum
CHECKSUMTYPE_CRC32C, //!< CRC-32C checksum
CHECKSUMTYPE_MD5, //!< MD5 128-bit hash
CHECKSUMTYPE_SHA1 //!< SHA-1 160-bit hash
};
/**
* Thread safe method that returns the string representation of the specified checksum type
*
* @param enumValue The integer value of the type
* @return The string representation
*/
static const char *checksumTypeToStr(const ChecksumType enumValue) throw();
/**
* Constructor
*
* Creates an empty checksum
*/
Checksum();
/**
* Constructor
*
* @param type The type of the checksum
* @param val A numeric value to store in the byte array
*/
template <typename t>
Checksum(const ChecksumType &type, t val): m_type(type) {
switch (m_type) {
case CHECKSUMTYPE_ADLER32:
if (sizeof(t) != 4) {
std::stringstream err;
err << "In Checksum::Checksum(type,value): unexpected value size="
<< sizeof(t) << " expected=4";
throw cta::exception::Exception(err.str());
}
break;
default:
throw cta::exception::Exception("In Checksum::Checksum(type,value): unsupported type for any value");
}
setNumeric(val);
}
/**
* String based constructor
*
* @param url A string describing the type of the checksum
*/
Checksum(const std::string & url);
/**
* Check two checksums are equal, throw an exception if not
*/
void validate(const Checksum &rhs) const;
/**
* Equality operator
*/
bool operator==(const Checksum &rhs) const;
/**
* Inequality operator
*/
bool operator!=(const Checksum &rhs) const { return !(*this == rhs); }
/**
* Returns the type of the checksum
*/
ChecksumType getType() const throw();
/**
* Returns the checksum as a byte array that can be used for storing in a database
*
* The bytes of the bytes array are in little-endian order
*
* @return The checksum as a byte array that can be used for storing in a database
*/
const std::string &getByteArray() const throw();
/**
* Returns a human-readable string representation of the checksum
*/
std::string str() const;
template <typename t>
t getNumeric() const {
if (m_byteArray.size() != sizeof(t)) {
std::stringstream err;
err << "In Checksum::getNumeric<"
<< typeid(t).name() << ">(): wrong size of byte array="
<< m_byteArray.size() << " expected=" << sizeof(t);
throw cta::exception::Exception(err.str());
}
return (*((t*)m_byteArray.data()));
}
template <typename t>
void setNumeric(t val) {
m_byteArray.replace(m_byteArray.begin(), m_byteArray.end(), (char *)&val, sizeof(t));
}
private:
ChecksumType m_type; //!< The type of the checksum.
std::string m_byteArray; //!< The checksum as a byte array in little-endian order
}; // class Checksum
/**
* Writes the specified Checksum object to the specified output stream
*
* @param os The output stream
* @param checksum The checksum
*/
std::ostream &operator<<(std::ostream &os, const Checksum &checksum);
} // namespace cta
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment