1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::hash::Hasher;
12 use std::marker::PhantomData;
14 use blake2b::Blake2bHasher;
15 use rustc_serialize::leb128;
17 fn write_unsigned_leb128_to_buf(buf: &mut [u8; 16], value: u64) -> usize {
18 leb128::write_unsigned_leb128_to(value as u128, |i, v| buf[i] = v)
21 fn write_signed_leb128_to_buf(buf: &mut [u8; 16], value: i64) -> usize {
22 leb128::write_signed_leb128_to(value as i128, |i, v| buf[i] = v)
25 /// When hashing something that ends up affecting properties like symbol names. We
26 /// want these symbol names to be calculated independent of other factors like
27 /// what architecture you're compiling *from*.
29 /// The hashing just uses the standard `Hash` trait, but the implementations of
30 /// `Hash` for the `usize` and `isize` types are *not* architecture independent
31 /// (e.g. they has 4 or 8 bytes). As a result we want to avoid `usize` and
32 /// `isize` completely when hashing.
34 /// To do that, we encode all integers to be hashed with some
35 /// arch-independent encoding.
37 /// At the moment, we pass i8/u8 straight through and encode
38 /// all other integers using leb128.
40 /// This hasher currently always uses the stable Blake2b algorithm
41 /// and allows for variable output lengths through its type
44 pub struct StableHasher<W> {
47 width: PhantomData<W>,
50 pub trait StableHasherResult: Sized {
51 fn finish(hasher: StableHasher<Self>) -> Self;
54 impl<W: StableHasherResult> StableHasher<W> {
55 pub fn new() -> Self {
57 state: Blake2bHasher::new(mem::size_of::<W>(), &[]),
63 pub fn finish(self) -> W {
68 impl StableHasherResult for [u8; 20] {
69 fn finish(mut hasher: StableHasher<Self>) -> Self {
70 let mut result: [u8; 20] = [0; 20];
71 result.copy_from_slice(hasher.state.finalize());
76 impl StableHasherResult for u64 {
77 fn finish(mut hasher: StableHasher<Self>) -> Self {
78 hasher.state.finalize();
83 impl<W> StableHasher<W> {
85 pub fn finalize(&mut self) -> &[u8] {
90 pub fn bytes_hashed(&self) -> u64 {
95 fn write_uleb128(&mut self, value: u64) {
96 let mut buf = [0; 16];
97 let len = write_unsigned_leb128_to_buf(&mut buf, value);
98 self.state.write(&buf[..len]);
99 self.bytes_hashed += len as u64;
103 fn write_ileb128(&mut self, value: i64) {
104 let mut buf = [0; 16];
105 let len = write_signed_leb128_to_buf(&mut buf, value);
106 self.state.write(&buf[..len]);
107 self.bytes_hashed += len as u64;
111 // For the non-u8 integer cases we leb128 encode them first. Because small
112 // integers dominate, this significantly and cheaply reduces the number of
113 // bytes hashed, which is good because blake2b is expensive.
114 impl<W> Hasher for StableHasher<W> {
115 fn finish(&self) -> u64 {
116 panic!("use StableHasher::finish instead");
120 fn write(&mut self, bytes: &[u8]) {
121 self.state.write(bytes);
122 self.bytes_hashed += bytes.len() as u64;
126 fn write_u8(&mut self, i: u8) {
127 self.state.write_u8(i);
128 self.bytes_hashed += 1;
132 fn write_u16(&mut self, i: u16) {
133 self.write_uleb128(i as u64);
137 fn write_u32(&mut self, i: u32) {
138 self.write_uleb128(i as u64);
142 fn write_u64(&mut self, i: u64) {
143 self.write_uleb128(i);
147 fn write_usize(&mut self, i: usize) {
148 self.write_uleb128(i as u64);
152 fn write_i8(&mut self, i: i8) {
153 self.state.write_i8(i);
154 self.bytes_hashed += 1;
158 fn write_i16(&mut self, i: i16) {
159 self.write_ileb128(i as i64);
163 fn write_i32(&mut self, i: i32) {
164 self.write_ileb128(i as i64);
168 fn write_i64(&mut self, i: i64) {
169 self.write_ileb128(i);
173 fn write_isize(&mut self, i: isize) {
174 self.write_ileb128(i as i64);