Rename runtime/base/zend_* to zend-
[hiphop-php.git] / hphp / runtime / base / md5.h
blob3cd7ee273286e0e0a124134f021349123ad36e9b
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 | Copyright (c) 1998-2010 Zend Technologies Ltd. (http://www.zend.com) |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 2.00 of the Zend license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.zend.com/license/2_00.txt. |
12 | If you did not receive a copy of the Zend license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@zend.com so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
18 #ifndef incl_HPHP_BASE_MD5_H_
19 #define incl_HPHP_BASE_MD5_H_
21 #include "hphp/runtime/base/zend-string.h"
22 #include "hphp/util/util.h"
24 namespace HPHP {
26 * Most of php's infrastructure for md5sums treats them as strings. They
27 * are 128-bits, though, and fit economically in a fixed-size struct.
30 struct MD5 {
31 uint64_t q[2];
32 MD5() {
33 q[0] = q[1] = 0;
36 explicit MD5(const char* str) {
37 // We expect our input to be null-terminated output from PHP::md5().
38 assert(strlen(str) == 32);
39 const int kQWordAsciiLen = 16;
40 char buf[kQWordAsciiLen + 1];
41 buf[kQWordAsciiLen] = 0;
42 memcpy(buf, str, kQWordAsciiLen);
43 assert(strlen(buf) == 16);
44 q[0] = strtoull(buf, nullptr, 16);
46 memcpy(buf, str + kQWordAsciiLen, 16);
47 assert(strlen(buf) == 16);
48 q[1] = strtoull(buf, nullptr, 16);
51 std::string toString() const {
52 int len = 16;
53 char md5nbo[16];
54 nbo((void *)md5nbo);
55 return std::string(string_bin2hex(md5nbo, len));
58 // blob is assumed to be in network byte order.
59 explicit MD5(const void* blob) {
60 q[0] = ntohq(((const uint64_t*)blob)[0]);
61 q[1] = ntohq(((const uint64_t*)blob)[1]);
64 // Copy out in network byte order.
65 void nbo(void* blob) const {
66 ((uint64_t*)blob)[0] = htonq(q[0]);
67 ((uint64_t*)blob)[1] = htonq(q[1]);
70 bool isValid() const {
72 * We arbitrarily choose kDefaultMD5 to be an "impossible" md5 value.
73 * If someone manages to construct a PHP compilation unit that
74 * collides, well, we owe them a pizza dinner or something.
76 return q[0] || q[1];
79 bool operator==(const MD5& r) const {
80 return q[0] == r.q[0] && q[1] == r.q[1];
82 bool operator<(const MD5& r) const {
83 return q[0] < r.q[0] || (q[0] == r.q[0] && q[1] < r.q[1]);
85 bool operator>(const MD5& r) const {
86 return q[0] > r.q[0] || (q[0] == r.q[0] && q[1] > r.q[1]);
88 bool operator!=(const MD5& r) const {
89 return !operator==(r);
92 uint64_t hash() const {
93 // hash_int64_pair does way more work than necessary; all the bits here
94 // are fantastically good.
95 return q[0];
99 } // HPHP
100 #endif