5 * Created by Victor Grishchenko on 3/6/09.
6 * Copyright 2009 Delft University of Technology. All rights reserved.
9 #ifndef P2TP_SHA1_HASH_TREE_H
10 #define P2TP_SHA1_HASH_TREE_H
22 Sha1Hash() { memset(bits,0,20); }
23 Sha1Hash(const Sha1Hash& left, const Sha1Hash& right);
24 Sha1Hash(const char* str, size_t length=-1);
25 Sha1Hash(const uint8_t* data, size_t length);
26 Sha1Hash(bool hex, const char* hash);
28 std::string hex() const;
29 bool operator == (const Sha1Hash& b) const
30 { return 0==memcmp(bits,b.bits,SIZE); }
31 bool operator != (const Sha1Hash& b) const { return !(*this==b); }
32 const char* operator * () const { return (char*) bits; }
34 const static Sha1Hash ZERO;
35 const static size_t SIZE;
41 /** Merkle hash tree: root */
44 /** Merkle hash tree: peak hashes */
45 Sha1Hash peak_hashes_[64];
48 /** File descriptor to put hashes to */
51 /** Whether to re-hash files. */
53 /** Base size, as derived from the hashes. */
56 /** Part of the tree currently checked. */
64 void RecoverProgress();
65 Sha1Hash DeriveRoot();
66 bool OfferPeakHash (bin64_t pos, const Sha1Hash& hash);
70 HashTree (const char* file_name, const Sha1Hash& root=Sha1Hash::ZERO,
71 const char* hash_filename=NULL);
73 /** Offer a hash; returns true if it verified; false otherwise.
74 Once it cannot be verified (no sibling or parent), the hash
75 is remembered, while returning false. */
76 bool OfferHash (bin64_t pos, const Sha1Hash& hash);
77 /** Offer data; the behavior is the same as with a hash:
78 accept or remember or drop. Returns true => ACK is sent. */
79 bool OfferData (bin64_t bin, const char* data, size_t length);
80 /** Not implemented yet. */
81 int AppendData (char* data, int length) ;
83 int file_descriptor () const { return fd_; }
84 int peak_count () const { return peak_count_; }
85 bin64_t peak (int i) const { return peaks_[i]; }
86 const Sha1Hash& peak_hash (int i) const { return peak_hashes_[i]; }
87 bin64_t peak_for (bin64_t pos) const;
88 const Sha1Hash& hash (bin64_t pos) const {return hashes_[pos];}
89 const Sha1Hash& root_hash () const { return root_hash_; }
90 uint64_t size () const { return size_; }
91 uint64_t size_kilo () const { return sizek_; }
92 uint64_t complete () const { return complete_; }
93 uint64_t complete_kilo () const { return completek_; }
94 uint64_t seq_complete () ;
96 { return size_ && seq_complete()==size_; }
97 bins& ack_out () { return ack_out_; }