1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
#ifndef CSMAPPING_H_
#define CSMAPPING_H_
#include <string.h>
#include "meta.h"
#include "filereader.h"
#include "fileio.h"
// XXX: allocate this dynamically?
#define MAXCSLEN 64
namespace acng
{
typedef enum : char {
CSTYPE_INVALID=0,
CSTYPE_MD5=1,
CSTYPE_SHA1=2,
CSTYPE_SHA256=3,
CSTYPE_SHA512=4
} CSTYPES;
inline CSTYPES GuessCStype(unsigned short len)
{
switch(len)
{
case 16: return CSTYPE_MD5;
case 20: return CSTYPE_SHA1;
case 32: return CSTYPE_SHA256;
case 64: return CSTYPE_SHA512;
default: return CSTYPE_INVALID;
}
}
inline unsigned short GetCSTypeLen(CSTYPES t)
{
switch(t)
{
case CSTYPE_MD5: return 16;
case CSTYPE_SHA1: return 20;
case CSTYPE_SHA256: return 32;
case CSTYPE_SHA512: return 54;
default: return 0;
}
}
inline LPCSTR GetCsName(CSTYPES csType)
{
switch(csType)
{
case CSTYPE_MD5: return "Md5";
case CSTYPE_SHA1: return "Sha1";
case CSTYPE_SHA256: return "Sha256";
case CSTYPE_SHA512: return "Sha512";
default: return "Other";
}
}
inline LPCSTR GetCsNameReleaseFile(CSTYPES csType)
{
switch(csType)
{
case CSTYPE_MD5: return "MD5Sum";
case CSTYPE_SHA1: return "SHA1";
case CSTYPE_SHA256: return "SHA256";
case CSTYPE_SHA512: return "SHA512";
default: return "Other";
}
}
class csumBase
{
public:
virtual ~csumBase() {};
virtual void add(const char *data, size_t size) = 0;
virtual void finish(uint8_t* ret) = 0;
static std::unique_ptr<csumBase> GetChecker(CSTYPES);
};
// kind of file identity, compares by file size and checksum (MD5 or SHA*)
struct tFingerprint {
off_t size =0;
CSTYPES csType =CSTYPE_INVALID;
uint8_t csum[MAXCSLEN];
tFingerprint() =default;
tFingerprint(const tFingerprint &a) : size(a.size),
csType(a.csType)
{
memcpy(csum, a.csum, sizeof(csum));
};
bool SetCs(const mstring & hexString, CSTYPES eCstype = CSTYPE_INVALID)
{
auto l = hexString.size();
if(!l || l%2) // weird sizes...
return false;
if(eCstype == CSTYPE_INVALID)
{
eCstype = GuessCStype(hexString.size() / 2);
if(eCstype == CSTYPE_INVALID)
return false;
}
else if(l != 2*GetCSTypeLen(eCstype))
return false;
csType=eCstype;
return CsAsciiToBin(hexString.c_str(), csum, l/2);
}
void Set(uint8_t *pData, CSTYPES eCstype, off_t newsize)
{
size=newsize;
csType=eCstype;
if(pData)
memcpy(csum, pData, GetCSTypeLen(eCstype));
}
inline bool Set(cmstring & hexString, CSTYPES eCstype, off_t newsize)
{
if(!SetCs(hexString, eCstype))
return false;
size=newsize;
return true;
}
/**
* Reads first two tokens from splitter, first considered checksum, second the size.
* Keeps the splitter pointed at last token, expects splitter be set at previous position.
* @return false if data is crap or wantedType was set but does not fit what's in the first token.
*/
inline bool Set(const tSplitWalk & splitInput, CSTYPES wantedType = CSTYPE_INVALID)
{
if(!splitInput.Next())
return false;
if(!SetCs(splitInput.str(), wantedType))
return false;
if(!splitInput.Next())
return false;
size = atoofft(splitInput.str().c_str(), -1);
if(size < 0)
return false;
return true;
}
#if 0
/**
* Warning: this function only exists to work around C++ stupidity.
* The const modifier is void, it will still modify splitter state.
*/
inline bool Set(const tSplitWalk & splitInput, CSTYPES wantedType = CSTYPE_INVALID)
{ return Set(std::move(splitInput), wantedType); }
#endif
bool ScanFile(const mstring & path, const CSTYPES eCstype, bool bUnpack = false, FILE *fDump=nullptr)
{
if(! GetCSTypeLen(eCstype))
return false; // unsupported
csType=eCstype;
return filereader::GetChecksum(path, eCstype, csum, bUnpack, size, fDump);
}
void Invalidate()
{
csType=CSTYPE_INVALID;
size=-1;
}
mstring GetCsAsString() const
{
return BytesToHexString(csum, GetCSTypeLen(csType));
}
operator mstring() const
{
return GetCsAsString()+"_"+offttos(size);
}
inline bool csEquals(const tFingerprint& other) const
{
return 0==memcmp(csum, other.csum, GetCSTypeLen(csType));
}
inline bool operator==(const tFingerprint & other) const
{
if(other.csType!=csType || size!=other.size)
return false;
return csEquals(other);
}
inline bool operator!=(const tFingerprint & other) const
{
return !(other == *this);
}
bool CheckFile(cmstring & sFile) const
{
if(size != GetFileSize(sFile, -2))
return false;
tFingerprint probe;
if(!probe.ScanFile(sFile, csType, false, nullptr))
return false;
return probe == *this;
}
bool operator<(const tFingerprint & other) const
{
if(other.csType!=csType)
return csType<other.csType;
if(size!=other.size)
return size<other.size;
return memcmp(csum, other.csum, GetCSTypeLen(csType)) < 0;
}
};
struct tRemoteFileInfo
{
tFingerprint fpr;
mstring sDirectory, sFileName;
inline void SetInvalid() {
sFileName.clear();
sDirectory.clear();
fpr.csType=CSTYPE_INVALID;
fpr.size=-1;
}
inline bool IsUsable() {
return (!sFileName.empty() && fpr.csType!=CSTYPE_INVALID && fpr.size>0);
}
bool SetFromPath(cmstring &sPath, cmstring &sBaseDir)
{
if (sPath.empty())
return false;
tStrPos pos = sPath.rfind(SZPATHSEPUNIX);
if (pos == stmiss)
{
sFileName = sPath;
sDirectory = sBaseDir;
}
else
{
sFileName = sPath.substr(pos + 1);
sDirectory = sBaseDir + sPath.substr(0, pos + 1);
}
return true;
}
inline bool SetSize(LPCSTR szSize)
{
auto l = atoofft(szSize, -2);
if(l<0)
return false;
fpr.size = l;
return true;
}
};
/** For IMPORT:
* Helper class to store attributes for different purposes.
* They need to be stored somewhere, either in fingerprint or
* another struct including fingerprint, or just here :-(.
*/
struct tImpFileInfo
{
mstring sPath;
time_t mtime = 0;
bool bFileUsed = false;
inline tImpFileInfo(const mstring & s, time_t m) :
sPath(s), mtime(m) {};
tImpFileInfo() =default;
};
struct ltCacheKeyComp
{
inline bool operator()(const tImpFileInfo &a, const tImpFileInfo &b) const
{
return (a.mtime!=b.mtime) ? (a.mtime<b.mtime) :
#if defined(WINDOWS) || defined(WIN32)
(strcasecmp(a.sPath.c_str(), b.sPath.c_str()) < 0);
#else
(a.sPath<b.sPath);
#endif
}
};
typedef std::map<tImpFileInfo, tFingerprint, ltCacheKeyComp> tFprCacheMap;
}
#endif /*CSMAPPING_H_*/
|