|
1
|
1 #pragma once
|
|
|
2 #include "filesystem.h" // FB2K_STREAM_READER_OVERLOAD, FB2K_STREAM_WRITER_OVERLOAD
|
|
|
3
|
|
|
4 struct hasher_md5_state {
|
|
|
5 char m_data[128];
|
|
|
6 };
|
|
|
7
|
|
|
8 struct hasher_md5_result {
|
|
|
9 char m_data[16];
|
|
|
10
|
|
|
11 t_uint64 xorHalve() const;
|
|
|
12 GUID asGUID() const;
|
|
|
13 pfc::string8 asString() const;
|
|
|
14 pfc::string8 toString() const { return asString(); }
|
|
|
15 GUID toGUID() const;
|
|
|
16
|
|
|
17 static hasher_md5_result null() {hasher_md5_result h = {}; return h;}
|
|
|
18 static int compare(hasher_md5_result const & h1, hasher_md5_result const & h2) { return memcmp(&h1, &h2, sizeof(hasher_md5_result)); }
|
|
|
19 };
|
|
|
20
|
|
|
21 FB2K_STREAM_READER_OVERLOAD(hasher_md5_result) {
|
|
|
22 stream.read_raw(&value, sizeof(value)); return stream;
|
|
|
23 }
|
|
|
24 FB2K_STREAM_WRITER_OVERLOAD(hasher_md5_result) {
|
|
|
25 stream.write_raw(&value, sizeof(value)); return stream;
|
|
|
26 }
|
|
|
27
|
|
|
28 inline bool operator==(const hasher_md5_result & p_item1,const hasher_md5_result & p_item2) {return memcmp(&p_item1,&p_item2,sizeof(hasher_md5_result)) == 0;}
|
|
|
29 inline bool operator!=(const hasher_md5_result & p_item1,const hasher_md5_result & p_item2) {return memcmp(&p_item1,&p_item2,sizeof(hasher_md5_result)) != 0;}
|
|
|
30 inline bool operator>(const hasher_md5_result & p_item1, const hasher_md5_result & p_item2) { return memcmp(&p_item1, &p_item2, sizeof(hasher_md5_result)) > 0; }
|
|
|
31 inline bool operator<(const hasher_md5_result & p_item1, const hasher_md5_result & p_item2) { return memcmp(&p_item1, &p_item2, sizeof(hasher_md5_result)) < 0; }
|
|
|
32
|
|
|
33 namespace pfc {
|
|
|
34 template<> class traits_t<hasher_md5_state> : public traits_rawobject {};
|
|
|
35 template<> class traits_t<hasher_md5_result> : public traits_rawobject {};
|
|
|
36
|
|
|
37 template<> inline int compare_t(const hasher_md5_result & p_item1, const hasher_md5_result & p_item2) {
|
|
|
38 return memcmp(&p_item1, &p_item2, sizeof(hasher_md5_result));
|
|
|
39 }
|
|
|
40
|
|
|
41 }
|
|
|
42
|
|
|
43 class NOVTABLE hasher_md5 : public service_base
|
|
|
44 {
|
|
|
45 public:
|
|
|
46
|
|
|
47 virtual void initialize(hasher_md5_state & p_state) = 0;
|
|
|
48 virtual void process(hasher_md5_state & p_state,const void * p_buffer,t_size p_bytes) = 0;
|
|
|
49 virtual hasher_md5_result get_result(const hasher_md5_state & p_state) = 0;
|
|
|
50
|
|
|
51
|
|
|
52 static GUID guid_from_result(const hasher_md5_result & param);
|
|
|
53
|
|
|
54 hasher_md5_result process_single(const void * p_buffer,t_size p_bytes);
|
|
|
55 hasher_md5_result process_single_string(const char * str) {return process_single(str, strlen(str));}
|
|
|
56 GUID process_single_guid(const void * p_buffer,t_size p_bytes);
|
|
|
57 GUID get_result_guid(const hasher_md5_state & p_state) {return guid_from_result(get_result(p_state));}
|
|
|
58
|
|
|
59
|
|
|
60 //! Helper
|
|
|
61 void process_string(hasher_md5_state & p_state,const char * p_string,t_size p_length = ~0) {return process(p_state,p_string,pfc::strlen_max(p_string,p_length));}
|
|
|
62 hasher_md5_state initialize() { hasher_md5_state ret; initialize(ret); return ret; }
|
|
|
63
|
|
|
64 FB2K_MAKE_SERVICE_COREAPI(hasher_md5);
|
|
|
65 };
|
|
|
66
|
|
|
67
|
|
|
68 class stream_writer_hasher_md5 : public stream_writer {
|
|
|
69 public:
|
|
|
70 stream_writer_hasher_md5() {
|
|
|
71 m_hasher->initialize(m_state);
|
|
|
72 }
|
|
|
73 void write(const void * p_buffer,t_size p_bytes,abort_callback & p_abort) {
|
|
|
74 p_abort.check();
|
|
|
75 m_hasher->process(m_state,p_buffer,p_bytes);
|
|
|
76 }
|
|
|
77 hasher_md5_result result() const {
|
|
|
78 return m_hasher->get_result(m_state);
|
|
|
79 }
|
|
|
80 GUID resultGuid() const {
|
|
|
81 return hasher_md5::guid_from_result(result());
|
|
|
82 }
|
|
|
83 private:
|
|
|
84 hasher_md5_state m_state;
|
|
|
85 const hasher_md5::ptr m_hasher = hasher_md5::get();
|
|
|
86 };
|
|
|
87
|
|
|
88 template<bool isBigEndian = false>
|
|
|
89 class stream_formatter_hasher_md5 : public stream_writer_formatter<isBigEndian> {
|
|
|
90 public:
|
|
|
91 stream_formatter_hasher_md5() : stream_writer_formatter<isBigEndian>(_m_stream,fb2k::noAbort) {}
|
|
|
92
|
|
|
93 hasher_md5_result result() const {
|
|
|
94 return _m_stream.result();
|
|
|
95 }
|
|
|
96 GUID resultGuid() const {
|
|
|
97 return hasher_md5::guid_from_result(result());
|
|
|
98 }
|
|
|
99 private:
|
|
|
100 stream_writer_hasher_md5 _m_stream;
|
|
|
101 };
|