photodna 1.5.1

Safe, high-level Rust bindings for the Microsoft PhotoDNA Edge Hash Generator
Documentation
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! PhotoDNA hash types and operations.
//!
//! This module provides the [`Hash`] type, a fixed-size container for
//! PhotoDNA perceptual hashes with zero-copy semantics.

use std::fmt;

/// Size of PhotoDNA Edge V2 hash in bytes (binary format).
///
/// This is the standard hash size for all PhotoDNA Edge V2 hashes.
pub const HASH_SIZE: usize = photodna_sys::PHOTODNA_HASH_SIZE_EDGE_V2;

/// Maximum possible hash buffer size.
///
/// Use this when you need to support any hash format, including Base64.
pub const HASH_SIZE_MAX: usize = photodna_sys::PHOTODNA_HASH_SIZE_MAX;

/// A PhotoDNA perceptual hash.
///
/// This type wraps a fixed-size byte array containing the raw hash bytes.
/// It is designed for high-performance use cases:
///
/// - **Zero-copy**: The hash is stored inline on the stack (no heap allocation).
/// - **Copy-safe**: Implements `Copy` for trivial duplication.
/// - **Hashable**: Can be used as a key in hash maps and sets.
///
/// # Size
///
/// The hash is 924 bytes (Edge V2 binary format). For Base64-encoded hashes,
/// use the raw byte methods and encode/decode as needed.
///
/// # Examples
///
/// ```rust
/// use photodna::Hash;
///
/// // Create an empty hash (all zeros)
/// let hash = Hash::default();
/// assert!(hash.is_empty());
///
/// // Access raw bytes
/// let bytes: &[u8] = hash.as_bytes();
/// assert_eq!(bytes.len(), photodna::HASH_SIZE);
///
/// // Format as hex string
/// let hex = hash.to_hex();
/// ```
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Hash {
    /// The raw hash bytes.
    bytes: [u8; HASH_SIZE],
    /// The actual length of valid hash data (may be less than HASH_SIZE).
    len: usize,
}

impl Hash {
    /// Creates a new hash from raw bytes.
    ///
    /// # Arguments
    ///
    /// * `bytes` - The raw hash bytes. Must be exactly [`HASH_SIZE`] bytes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use photodna::{Hash, HASH_SIZE};
    ///
    /// let data = [0u8; HASH_SIZE];
    /// let hash = Hash::new(data);
    /// ```
    #[inline]
    pub const fn new(bytes: [u8; HASH_SIZE]) -> Self {
        Self {
            bytes,
            len: HASH_SIZE,
        }
    }

    /// Creates a hash from a slice, copying the bytes.
    ///
    /// # Arguments
    ///
    /// * `slice` - A byte slice containing hash data. Must not exceed [`HASH_SIZE`] bytes.
    ///
    /// # Returns
    ///
    /// Returns `Some(Hash)` if the slice length is valid, `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use photodna::Hash;
    ///
    /// let data = [0xAB; 100];
    /// let hash = Hash::from_slice(&data).unwrap();
    /// assert_eq!(hash.len(), 100);
    /// ```
    pub fn from_slice(slice: &[u8]) -> Option<Self> {
        if slice.len() > HASH_SIZE {
            return None;
        }

        let mut bytes = [0u8; HASH_SIZE];
        bytes[..slice.len()].copy_from_slice(slice);

        Some(Self {
            bytes,
            len: slice.len(),
        })
    }

    /// Returns the hash bytes as a slice.
    ///
    /// The returned slice contains only the valid hash bytes (up to `len()`).
    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes[..self.len]
    }

    /// Returns the full hash buffer as a fixed-size array reference.
    ///
    /// This includes any padding bytes if the hash is shorter than [`HASH_SIZE`].
    #[inline]
    pub const fn as_array(&self) -> &[u8; HASH_SIZE] {
        &self.bytes
    }

    /// Returns the length of valid hash bytes.
    #[inline]
    pub const fn len(&self) -> usize {
        self.len
    }

    /// Returns `true` if all hash bytes are zero.
    ///
    /// An empty hash typically indicates that no hash was computed.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.bytes[..self.len].iter().all(|&b| b == 0)
    }

    /// Formats the hash as a lowercase hexadecimal string.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use photodna::Hash;
    ///
    /// let data = [0xAB; 4];
    /// let hash = Hash::from_slice(&data).unwrap();
    /// assert_eq!(&hash.to_hex()[..8], "abababab");
    /// ```
    pub fn to_hex(&self) -> String {
        let mut hex = String::with_capacity(self.len * 2);
        for byte in &self.bytes[..self.len] {
            use std::fmt::Write;
            let _ = write!(hex, "{:02x}", byte);
        }
        hex
    }

    /// Formats the hash as an uppercase hexadecimal string.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use photodna::Hash;
    ///
    /// let data = [0xAB; 4];
    /// let hash = Hash::from_slice(&data).unwrap();
    /// assert_eq!(&hash.to_hex_upper()[..8], "ABABABAB");
    /// ```
    pub fn to_hex_upper(&self) -> String {
        let mut hex = String::with_capacity(self.len * 2);
        for byte in &self.bytes[..self.len] {
            use std::fmt::Write;
            let _ = write!(hex, "{:02X}", byte);
        }
        hex
    }

    /// Parses a hash from a hexadecimal string.
    ///
    /// # Arguments
    ///
    /// * `hex` - A hexadecimal string (case-insensitive).
    ///
    /// # Returns
    ///
    /// Returns `Some(Hash)` if parsing succeeds, `None` if the string
    /// contains invalid characters or has an invalid length.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use photodna::Hash;
    ///
    /// let hash = Hash::from_hex("abcdef01").unwrap();
    /// assert_eq!(hash.len(), 4);
    /// assert_eq!(hash.as_bytes(), &[0xAB, 0xCD, 0xEF, 0x01]);
    /// ```
    pub fn from_hex(hex: &str) -> Option<Self> {
        // Hex string must have even length
        if hex.len() % 2 != 0 {
            return None;
        }

        let byte_len = hex.len() / 2;
        if byte_len > HASH_SIZE {
            return None;
        }

        let mut bytes = [0u8; HASH_SIZE];

        for (i, chunk) in hex.as_bytes().chunks(2).enumerate() {
            let high = hex_digit_value(chunk[0])?;
            let low = hex_digit_value(chunk[1])?;
            bytes[i] = (high << 4) | low;
        }

        Some(Self {
            bytes,
            len: byte_len,
        })
    }

    /// Returns a mutable slice to the entire hash buffer.
    ///
    /// This is useful for passing to FFI functions that write directly
    /// to the buffer.
    ///
    /// # Safety
    ///
    /// After modifying the buffer via this method, you may need to update
    /// the logical length using [`set_len`](Self::set_len) if the actual
    /// data size has changed.
    #[inline]
    pub fn as_mut_bytes(&mut self) -> &mut [u8; HASH_SIZE] {
        &mut self.bytes
    }

    /// Sets the length of valid hash data.
    ///
    /// # Panics
    ///
    /// Panics if `len > HASH_SIZE`.
    #[inline]
    pub fn set_len(&mut self, len: usize) {
        assert!(len <= HASH_SIZE, "length exceeds maximum hash size");
        self.len = len;
    }

    /// Creates a new hash with uninitialized content.
    ///
    /// This is useful for performance-critical code where the hash
    /// will be immediately overwritten by FFI.
    ///
    /// # Safety
    ///
    /// The caller must ensure the hash is fully initialized before
    /// reading from it.
    #[inline]
    pub const fn zeroed() -> Self {
        Self {
            bytes: [0u8; HASH_SIZE],
            len: 0,
        }
    }
}

impl Default for Hash {
    fn default() -> Self {
        Self {
            bytes: [0u8; HASH_SIZE],
            len: HASH_SIZE,
        }
    }
}

impl fmt::Debug for Hash {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Show first 16 bytes as hex for readability
        let preview_len = 16.min(self.len);
        let preview: String = self.bytes[..preview_len]
            .iter()
            .map(|b| format!("{:02x}", b))
            .collect();

        if self.len > preview_len {
            write!(f, "Hash({}..., {} bytes)", preview, self.len)
        } else {
            write!(f, "Hash({})", preview)
        }
    }
}

impl fmt::Display for Hash {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_hex())
    }
}

impl AsRef<[u8]> for Hash {
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl From<[u8; HASH_SIZE]> for Hash {
    fn from(bytes: [u8; HASH_SIZE]) -> Self {
        Self::new(bytes)
    }
}

impl TryFrom<&[u8]> for Hash {
    type Error = ();

    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        Self::from_slice(slice).ok_or(())
    }
}

/// Converts a hex character to its numeric value.
#[inline]
fn hex_digit_value(c: u8) -> Option<u8> {
    match c {
        b'0'..=b'9' => Some(c - b'0'),
        b'a'..=b'f' => Some(c - b'a' + 10),
        b'A'..=b'F' => Some(c - b'A' + 10),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_hash_size_constant() {
        assert_eq!(HASH_SIZE, 924);
    }

    #[test]
    fn test_hash_new() {
        let data = [0xAB; HASH_SIZE];
        let hash = Hash::new(data);
        assert_eq!(hash.len(), HASH_SIZE);
        assert!(!hash.is_empty());
    }

    #[test]
    fn test_hash_default() {
        let hash = Hash::default();
        assert!(hash.is_empty());
        assert_eq!(hash.len(), HASH_SIZE);
    }

    #[test]
    fn test_hash_from_slice() {
        let data = [0xAB; 100];
        let hash = Hash::from_slice(&data).unwrap();
        assert_eq!(hash.len(), 100);
        assert_eq!(&hash.as_bytes()[..100], &data);
    }

    #[test]
    fn test_hash_from_slice_too_large() {
        let data = [0xAB; HASH_SIZE + 1];
        assert!(Hash::from_slice(&data).is_none());
    }

    #[test]
    fn test_hash_to_hex() {
        let data = [0xAB, 0xCD, 0xEF, 0x01];
        let hash = Hash::from_slice(&data).unwrap();
        assert_eq!(hash.to_hex(), "abcdef01");
        assert_eq!(hash.to_hex_upper(), "ABCDEF01");
    }

    #[test]
    fn test_hash_from_hex() {
        let hash = Hash::from_hex("abcdef01").unwrap();
        assert_eq!(hash.len(), 4);
        assert_eq!(hash.as_bytes(), &[0xAB, 0xCD, 0xEF, 0x01]);
    }

    #[test]
    fn test_hash_from_hex_invalid() {
        assert!(Hash::from_hex("abc").is_none()); // Odd length
        assert!(Hash::from_hex("ghij").is_none()); // Invalid chars
    }

    #[test]
    fn test_hash_copy() {
        let hash1 = Hash::from_slice(&[1, 2, 3, 4]).unwrap();
        let hash2 = hash1; // Copy
        assert_eq!(hash1, hash2);
    }

    #[test]
    fn test_hash_debug() {
        let hash = Hash::from_slice(&[0xAB; 20]).unwrap();
        let debug = format!("{:?}", hash);
        assert!(debug.contains("Hash("));
        assert!(debug.contains("20 bytes"));
    }

    #[test]
    fn test_hash_display() {
        let hash = Hash::from_slice(&[0xAB, 0xCD]).unwrap();
        assert_eq!(format!("{}", hash), "abcd");
    }

    #[test]
    fn test_hash_equality() {
        let hash1 = Hash::from_slice(&[1, 2, 3, 4]).unwrap();
        let hash2 = Hash::from_slice(&[1, 2, 3, 4]).unwrap();
        let hash3 = Hash::from_slice(&[1, 2, 3, 5]).unwrap();

        assert_eq!(hash1, hash2);
        assert_ne!(hash1, hash3);
    }

    #[test]
    fn test_hash_in_hashset() {
        use std::collections::HashSet;

        let mut set = HashSet::new();
        set.insert(Hash::from_slice(&[1, 2, 3]).unwrap());
        set.insert(Hash::from_slice(&[4, 5, 6]).unwrap());

        assert!(set.contains(&Hash::from_slice(&[1, 2, 3]).unwrap()));
        assert!(!set.contains(&Hash::from_slice(&[7, 8, 9]).unwrap()));
    }
}