forked from zbackup/zbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk_id.cc
54 lines (39 loc) · 1.28 KB
/
chunk_id.cc
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
// Copyright (c) 2012-2014 Konstantin Isakov <[email protected]> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include "chunk_id.hh"
#include <string.h>
#include "endian.hh"
#include "check.hh"
string ChunkId::toBlob() const
{
string out( BlobSize, 0 );
toBlob( &out[ 0 ] );
return out;
}
void ChunkId::toBlob( void * outPtr ) const
{
char * out = ( char * ) outPtr;
RollingHash::Digest v = toLittleEndian( rollingHash );
memcpy( out, cryptoHash, sizeof( cryptoHash ) );
memcpy( out + sizeof( cryptoHash ), &v, sizeof( v ) );
}
void ChunkId::setFromBlob( void const * data )
{
char const * blob = ( char const * ) data;
RollingHash::Digest v;
memcpy( cryptoHash, blob, sizeof( cryptoHash ) );
memcpy( &v, blob + sizeof( cryptoHash ), sizeof( v ) );
rollingHash = fromLittleEndian( v );
}
bool operator <( const ChunkId &lhs, const ChunkId &rhs )
{
int r = memcmp( &lhs.cryptoHash, &rhs.cryptoHash, sizeof( lhs.cryptoHash ) );
if ( r != 0 )
return r < 0;
return memcmp( &lhs.rollingHash, &rhs.rollingHash, sizeof( lhs.rollingHash ) ) < 0;
}
ChunkId::ChunkId( string const & blob )
{
CHECK( blob.size() == BlobSize, "incorrect blob size: %zu", blob.size() );
setFromBlob( blob.data() );
}