1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! [Hash](generic::Hash), [HashParseError]
use crate::*;
use std::convert::*;
use std::fmt::{self, Debug, Display, Formatter};
use std::io::{self, Read};
use std::marker::PhantomData;
use std::str::FromStr;
/// A [SHA-1] or [SHA-256] reference to a git [Commit], [Tree], or Blob
///
/// [SHA-1]: https://en.wikipedia.org/wiki/SHA-1
/// [SHA-256]: https://en.wikipedia.org/wiki/SHA-2
pub struct Hash<T> {
bytes: [u8; 32],
len: u8,
_pd: PhantomData<T>,
}
impl<T> Hash<T> {
/// Construct a [Hash](generic::Hash) from a hexidecimal string. The entire hash must be specified: 40 characters ([SHA-1]) or 64 ([SHA-256])
///
/// # Examples
/// ```rust
/// use clgit::unknown::Hash; // aka clgit::generic::Hash<()>
///
/// for good in [
/// // Legal SHA-1 hashes (20 bytes / 40 characters)
/// "74da26a93c3eac22884a62bd8d70aab3434c9174",
/// "89dd60cc88e4f89e0af91e2739c42a31c3a106bb",
/// "eb6c43cb699caa2ccbc4e28f9ab75a2a17e4ee7c",
///
/// // Uppercase is legal too
/// "74DA26A93C3EAC22884A62BD8D70AAB3434C9174",
/// "89DD60CC88E4F89E0AF91E2739C42A31C3A106BB",
/// "EB6C43CB699CAA2CCBC4E28F9AB75A2A17E4EE7C",
///
/// // SHA-256 hashes (40 bytes / 64 characters)
/// "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
/// ].iter().cloned() {
/// Hash::from_str(good).unwrap_or_else(|e| panic!("Failed to parse {}: {}", good, e));
/// }
///
/// for bad in [
/// "eb6c43cb699caa2ccbc4e28f9ab75a2a17e4ee7c0", // too long
/// "eb6c43cb699caa2ccbc4e28f9ab75a2a17e4ee7", // too short
/// "eb6c43cb699caa2ccbc4e28f9ab75a2a17e4ee7!", // invalid character
/// "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde", // too short
/// "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0", // too long
/// ].iter().cloned() {
/// assert!(Hash::from_str(bad).is_err(), "Didn't expect to parse {}", bad);
/// }
/// ```
///
/// [SHA-1]: https://en.wikipedia.org/wiki/SHA-1
/// [SHA-256]: https://en.wikipedia.org/wiki/SHA-2
pub fn from_str(s: &str) -> Result<Self, HashParseError> {
let mut bytes = [0u8; 32];
let mut ascii = s.as_bytes();
match ascii.len() {
40 | 64 => {},
_ => return Err(HashParseError::LengthMismatch),
}
let len = ascii.len() / 2;
let mut dst = &mut bytes[..len];
while !dst.is_empty() {
let h = ascii_byte_to_hex(ascii[0])?;
let l = ascii_byte_to_hex(ascii[1])?;
dst[0] = (h << 4) | l;
ascii = &ascii[2..];
dst = &mut dst[1..];
}
Ok(Self { bytes, len: len as u8, _pd: PhantomData })
}
/// Construct a [Hash](generic::Hash) from a slice of bytes. The entire hash must be specified: 20 bytes ([SHA-1]) or 32 ([SHA-256])
///
/// # Examples
/// ```rust
/// # use clgit::unknown::Hash;
/// Hash::from_bytes(&[0u8; 20][..]).expect("20 bytes OK");
/// Hash::from_bytes(&[0u8; 32][..]).expect("32 bytes OK");
///
/// Hash::from_bytes(&[0u8; 19][..]).expect_err("19 bytes invalid");
/// Hash::from_bytes(&[0u8; 21][..]).expect_err("21 bytes invalid");
/// Hash::from_bytes(&[0u8; 31][..]).expect_err("31 bytes invalid");
/// Hash::from_bytes(&[0u8; 33][..]).expect_err("33 bytes invalid");
/// ```
///
/// [SHA-1]: https://en.wikipedia.org/wiki/SHA-1
/// [SHA-256]: https://en.wikipedia.org/wiki/SHA-2
pub fn from_bytes(src: &[u8]) -> Result<Self, HashParseError> {
let mut bytes = [0u8; 32];
let len = src.len();
match len {
20 | 32 => bytes[..len].copy_from_slice(src),
_ => return Err(HashParseError::LengthMismatch),
}
Ok(Self { bytes, len: len as u8, _pd: PhantomData })
}
/// [Read] 20 bytes from `r` and treat it as a [SHA-1] [Hash](generic::Hash)
///
/// # Example
/// ```rust
/// # use clgit::unknown::Hash;
/// let mut io = std::io::Cursor::new(vec![0; 128]);
/// Hash::read_sha1(&mut io).unwrap();
/// ```
///
/// [SHA-1]: https://en.wikipedia.org/wiki/SHA-1
pub fn read_sha1(r: &mut impl Read) -> io::Result<Self> {
let mut bytes = [0u8; 32];
r.read_exact(&mut bytes[..20])?;
Ok(Self { bytes, len: 20, _pd: PhantomData })
}
/// [Read] 32 bytes from `r` and treat it as a [SHA-256] [Hash](generic::Hash)
///
/// # Example
/// ```rust
/// # use clgit::unknown::Hash;
/// let mut io = std::io::Cursor::new(vec![0; 128]);
/// Hash::read_sha256(&mut io).unwrap();
/// ```
///
/// [SHA-256]: https://en.wikipedia.org/wiki/SHA-2
pub fn read_sha256(r: &mut impl Read) -> io::Result<Self> {
let mut bytes = [0u8; 32];
r.read_exact(&mut bytes[..])?;
Ok(Self { bytes, len: 32, _pd: PhantomData })
}
/// Get the number of bytes in this hash (20 or 32)
///
/// # Example
/// ```rust
/// # use clgit::unknown::Hash;
/// # let hash = Hash::default();
/// assert!(hash.len() == 20 || hash.len() == 32);
/// ```
pub fn len(&self) -> usize { usize::from(self.len) }
/// Get the bytes in this hash (length of 20 or 32)
///
/// # Example
/// ```rust
/// # use clgit::unknown::Hash;
/// # let hash = Hash::default();
/// let bytes : &[u8] = hash.bytes();
/// assert!(bytes.len() == 20 || bytes.len() == 32);
/// ```
pub fn bytes(&self) -> &[u8] { &self.bytes[..self.len()] }
/// Get the first byte of this hash
///
/// # Example
/// ```rust
/// # use clgit::unknown::Hash;
/// # let hash = Hash::default();
/// println!("byte: {:02x}", hash.first_byte());
/// ```
pub fn first_byte(&self) -> u8 { self.bytes[0] }
/// Discard type information for this hash
pub fn typeless(&self) -> Hash<()> {
Hash {
bytes: self.bytes.clone(),
len: self.len,
_pd: PhantomData,
}
}
}
impl Hash<()> {
/// Acquire type information for this hash
pub fn cast<T>(&self) -> Hash<T> {
Hash {
bytes: self.bytes.clone(),
len: self.len,
_pd: PhantomData,
}
}
}
impl<T> Clone for Hash<T> {
fn clone(&self) -> Self {
Self {
bytes: self.bytes.clone(),
len: self.len,
_pd: PhantomData,
}
}
}
impl<T> Display for Hash<T> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
for b in &self.bytes[..self.len as usize] {
write!(fmt, "{:02x}", b)?;
}
Ok(())
}
}
impl<T> Debug for Hash<T> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "Hash(\"{}\")", self)
}
}
impl<T> Default for Hash<T> {
fn default() -> Self {
Self {
bytes: [0u8; 32],
len: 20, // sha1
_pd: PhantomData,
}
}
}
impl<T> PartialEq<Self> for Hash<T> { fn eq(&self, other: &Self) -> bool { self.bytes() == other.bytes() }}
impl<T> Eq for Hash<T> {}
impl<T> PartialOrd<Self> for Hash<T> { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { self.bytes().partial_cmp(other.bytes()) } }
impl<T> Ord for Hash<T> { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.bytes().cmp(other.bytes()) } }
impl<T> std::hash::Hash for Hash<T> { fn hash<H: std::hash::Hasher>(&self, state: &mut H) { self.bytes().hash(state) } }
impl<T> FromStr for Hash<T> { fn from_str(s: &str) -> Result<Self, HashParseError> { Self::from_str(s) } type Err = HashParseError; }
impl PartialEq<Hash<()>> for Hash<Blob > { fn eq(&self, other: &Hash<()>) -> bool { self.bytes() == other.bytes() } }
impl PartialEq<Hash<()>> for Hash<Commit> { fn eq(&self, other: &Hash<()>) -> bool { self.bytes() == other.bytes() } }
impl PartialEq<Hash<()>> for Hash<Tree > { fn eq(&self, other: &Hash<()>) -> bool { self.bytes() == other.bytes() } }
impl PartialEq<Hash<Blob >> for Hash<()> { fn eq(&self, other: &Hash<Blob >) -> bool { self.bytes() == other.bytes() } }
impl PartialEq<Hash<Commit>> for Hash<()> { fn eq(&self, other: &Hash<Commit>) -> bool { self.bytes() == other.bytes() } }
impl PartialEq<Hash<Tree >> for Hash<()> { fn eq(&self, other: &Hash<Tree >) -> bool { self.bytes() == other.bytes() } }
/// Describes how a [Hash](generic::Hash) failed to [parse](str::parse).
/// Convertable to [std::io::Error], [Box]<dyn [std::error::Error]>.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum HashParseError {
/// [Hash](generic::Hash) wasn't an expected length (20/32 bytes, or 40/64 characters)
LengthMismatch,
/// [Hash](generic::Hash) contained an invalid character (expected [hexadecimal](https://simple.wikipedia.org/wiki/Hexadecimal) characters only)
BadCharacter(char),
}
impl std::error::Error for HashParseError {}
impl Display for HashParseError {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
match self {
HashParseError::LengthMismatch => write!(fmt, "Hash length mismatch"),
HashParseError::BadCharacter(c) => write!(fmt, "Invalid character {:?} in hash", c),
}
}
}
impl From<HashParseError> for io::Error {
fn from(hpe: HashParseError) -> Self {
io::Error::new(io::ErrorKind::InvalidData, hpe)
}
}
pub(crate) struct HashTempStr {
ascii: [u8; 64],
len: usize,
}
impl HashTempStr {
pub fn new<T>(hash: &Hash<T>) -> Self {
let mut ascii = [0u8; 64];
let len = usize::from(hash.len) * 2;
let mut dst = &mut ascii[..];
let hex = b"0123456789abcdef";
for b in &hash.bytes[..usize::from(hash.len)] {
dst[0] = hex[usize::from(b >> 4)];
dst[1] = hex[usize::from(b & 0xF)];
dst = &mut dst[2..];
}
Self {
ascii,
len
}
}
pub fn bytes(&self) -> &[u8] { &self.ascii[..self.len] }
pub fn as_str(&self) -> &str { std::str::from_utf8(self.bytes()).unwrap() }
}
fn ascii_byte_to_hex(b: u8) -> Result<u8, HashParseError> {
match b {
b'0' ..= b'9' => Ok(b - b'0'),
b'a' ..= b'f' => Ok(b - b'a' + 10),
b'A' ..= b'F' => Ok(b - b'A' + 10),
_ => Err(HashParseError::BadCharacter(b as char)),
}
}