Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 * | 6 * |
| 7 * The following code is based on the description in RFC 1321. | 7 * The following code is based on the description in RFC 1321. |
| 8 * http://www.ietf.org/rfc/rfc1321.txt | 8 * http://www.ietf.org/rfc/rfc1321.txt |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 bufferIndex = 0; | 53 bufferIndex = 0; |
| 54 } else { | 54 } else { |
| 55 inputIndex = 0; | 55 inputIndex = 0; |
| 56 } | 56 } |
| 57 | 57 |
| 58 memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIn dex); | 58 memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIn dex); |
| 59 | 59 |
| 60 this->byteCount += inputLength; | 60 this->byteCount += inputLength; |
| 61 } | 61 } |
| 62 | 62 |
| 63 void SkMD5::finish(Digest& digest) { | 63 void SkMD5::finish(SkHashDigest& digest) { |
| 64 // Get the number of bits before padding. | 64 // Get the number of bits before padding. |
| 65 uint8_t bits[8]; | 65 uint8_t bits[8]; |
| 66 encode(bits, this->byteCount << 3); | 66 encode(bits, this->byteCount << 3); |
| 67 | 67 |
| 68 // Pad out to 56 mod 64. | 68 // Pad out to 56 mod 64. |
| 69 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); | 69 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); |
| 70 unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex); | 70 unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex); |
| 71 static uint8_t PADDING[64] = { | 71 static uint8_t PADDING[64] = { |
| 72 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 72 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 76 }; | 76 }; |
| 77 this->update(PADDING, paddingLength); | 77 this->update(PADDING, paddingLength); |
| 78 | 78 |
| 79 // Append length (length before padding, will cause final update). | 79 // Append length (length before padding, will cause final update). |
| 80 this->update(bits, 8); | 80 this->update(bits, 8); |
| 81 | 81 |
| 82 // Write out digest. | 82 // Write out digest. |
| 83 encode(digest.data, this->state); | 83 // TODO(epoger): eliminate the extra 16-byte copy, by writing directly into digest.data??? |
|
epoger
2013/04/15 18:21:38
Any thoughts on this? I intend to get to work on
| |
| 84 // NO, we can't do that, because digest.data is immutable (wrapped in SkData ). | |
| 85 // Or: | |
| 86 // 1. malloc 16 bytes here (wrapped in an SkData object), | |
| 87 // 2. write the digest into the SkData-wrapped 16 bytes, | |
| 88 // 3. bump the ref counter on that SkData-wrapped 16 bytes, | |
| 89 // 4. replace digest's fSkDataPtr with the new one | |
| 90 size_t digestSize = 16; | |
| 91 uint8_t tempDigestData[digestSize]; | |
| 92 encode(tempDigestData, this->state); | |
| 93 digest.set(tempDigestData, digestSize); | |
| 84 | 94 |
| 85 #if defined(SK_MD5_CLEAR_DATA) | 95 #if defined(SK_MD5_CLEAR_DATA) |
| 86 // Clear state. | 96 // Clear state. |
| 87 memset(this, 0, sizeof(*this)); | 97 memset(this, 0, sizeof(*this)); |
| 88 #endif | 98 #endif |
| 89 } | 99 } |
| 90 | 100 |
| 91 struct F { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { | 101 struct F { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { |
| 92 //return (x & y) | ((~x) & z); | 102 //return (x & y) | ((~x) & z); |
| 93 return ((y ^ z) & x) ^ z; //equivelent but faster | 103 return ((y ^ z) & x) ^ z; //equivalent but faster |
| 94 }}; | 104 }}; |
| 95 | 105 |
| 96 struct G { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { | 106 struct G { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { |
| 97 return (x & z) | (y & (~z)); | 107 return (x & z) | (y & (~z)); |
| 98 //return ((x ^ y) & z) ^ y; //equivelent but slower | 108 //return ((x ^ y) & z) ^ y; //equivalent but slower |
| 99 }}; | 109 }}; |
| 100 | 110 |
| 101 struct H { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { | 111 struct H { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { |
| 102 return x ^ y ^ z; | 112 return x ^ y ^ z; |
| 103 }}; | 113 }}; |
| 104 | 114 |
| 105 struct I { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { | 115 struct I { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { |
| 106 return y ^ (x | (~z)); | 116 return y ^ (x | (~z)); |
| 107 }}; | 117 }}; |
| 108 | 118 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 #endif | 253 #endif |
| 244 for (size_t i = 0, j = 0; j < 64; i++, j += 4) { | 254 for (size_t i = 0, j = 0; j < 64; i++, j += 4) { |
| 245 storage[i] = ((uint32_t)input[j ]) | | 255 storage[i] = ((uint32_t)input[j ]) | |
| 246 (((uint32_t)input[j+1]) << 8) | | 256 (((uint32_t)input[j+1]) << 8) | |
| 247 (((uint32_t)input[j+2]) << 16) | | 257 (((uint32_t)input[j+2]) << 16) | |
| 248 (((uint32_t)input[j+3]) << 24); | 258 (((uint32_t)input[j+3]) << 24); |
| 249 } | 259 } |
| 250 return storage; | 260 return storage; |
| 251 #endif | 261 #endif |
| 252 } | 262 } |
| OLD | NEW |