00001 /// @file include/dmlite/cpp/utils/checksums.h 00002 /// @brief Utility methods for checksum handling 00003 /// @author Alejandro Álvarez Ayllón <aalvarez@cern.ch> 00004 #ifndef DMLITE_CPP_UTILS_CHECKSUMS_H 00005 #define DMLITE_CPP_UTILS_CHECKSUMS_H 00006 00007 #include <dmlite/cpp/io.h> 00008 #include <string> 00009 00010 namespace dmlite { 00011 class ExtendedStat; 00012 00013 namespace checksums { 00014 00015 00016 00017 /// To be used internally by the plug-ins that need to deal 00018 /// with the legacy-style stored checksums. 00019 /// @note AD => ADLER32 00020 /// @note CS => CRC32 00021 /// @note MD => MD5 (RFC 3230) 00022 /// @note Any other is left as is 00023 std::string fullChecksumName(const std::string& cs); 00024 00025 /// Inverse of fullChecksumName 00026 /// This should eventually disappear, once the backends can deal with 00027 /// full checksum names. 00028 std::string shortChecksumName(const std::string& cs); 00029 00030 /// Tells if the given key looks like the name of a checksum 00031 bool isChecksumFullName(const std::string& ckey); 00032 00033 /// Makes sure that the extended attributes contain the legacy checksum. 00034 /// @param xstat The stat information to be modified 00035 /// @return Zero if the xattrs were not modified 00036 int fillChecksumInXattr(ExtendedStat& xstat); 00037 00038 /// Returns the MD5 checksum of the data contained on the IO handler 00039 /// in hexadecimal format. 00040 /// @param io The IO handler to be digested. The read/write possition will be moved! 00041 /// @param offset Where to start to digest. 00042 /// @param size The number of bytes to digest. 0 means the whole file. 00043 /// @return The MD5 checkum in base 16 00044 std::string md5(IOHandler* io, off_t offset = 0, off_t size = 0); 00045 00046 /// Returns the CRC checksum of the data contained on the IO handler (as zlib crc32) 00047 /// in base 10 format. 00048 /// @param io The IO handler to be digested. The read/write possition will be moved! 00049 /// @param offset Where to start to digest. 00050 /// @param size The number of bytes to digest. 0 means the whole file. 00051 /// @return The CRC checkum in base 10 00052 std::string crc32(IOHandler* io, off_t offset = 0, off_t size = 0); 00053 00054 /// Returns the Adler32 checksum of the data contained on the IO handler 00055 /// in hexadecimal format. 00056 /// @param io The IO handler to be digested. The read/write possition will be moved! 00057 /// @param offset Where to start to digest. 00058 /// @param size The number of bytes to digest. 0 means the whole file. 00059 /// @return The Adler32 checkum in base 16 00060 std::string adler32(IOHandler* io, off_t offset = 0, off_t size = 0); 00061 00062 /// Returns the hexadecimal representation of the data 00063 /// @param data The data to dump to hexadecimal representation. 00064 /// @param nbytes The number of bytes in data 00065 std::string hexPrinter(const unsigned char* data, size_t nbytes); 00066 00067 /// Returns the decimal representation of the data, separated by spaces 00068 /// (num1 num2 num3) 00069 /// @param data The data to dump to decimal representation. 00070 /// @param nbytes The number of bytes in data 00071 /// @note It assumes data is an array of 'unsigned long' 00072 std::string decPrinter(const unsigned char* data, size_t nbytes); 00073 00074 } 00075 } 00076 00077 #endif // DMLITE_CPP_UTILS_CHECKSUMS_H