fprint_backend_libfprint/print.rs
1// SPDX-FileCopyrightText: 2026 fprintd (pure-Rust) contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5//! Translation between libfprint's `FpPrint` and the core [`Print`].
6//!
7//! ## The D1 decision: templates are unified through `fprint-fp3`
8//!
9//! libfprint's `fp_print_serialize` and the `fprint-fp3` codec target the *same* on-disk FP3
10//! byte format (`"FP3"` + GVariant `(issbymsmsia{sv}v)`). Every template therefore crosses this
11//! boundary as an FP3 blob, with `fprint-fp3` as the single decoder: an `FpPrint` becomes a
12//! [`Print`] by `fprint_fp3::from_bytes(fp.serialize())`, and back by
13//! `FpPrint::deserialize(fprint_fp3::to_bytes(print))`. Match-on-chip handles (libfprint's
14//! `FPI_PRINT_RAW`, the FP3 field-9 opaque variant) and host-side NBIS minutiae then land in the
15//! same [`Template`](fprint_core::Template) shape the native backend produces, so the daemon
16//! stores and compares prints uniformly regardless of which backend made them.
17
18use fprint_core::{DeviceId, DriverId, Print, Result};
19
20use crate::convert;
21use crate::ffi::{FpDevice, FpPrint};
22
23/// Decode an `FpPrint` (via its FP3 serialization) into a core [`Print`].
24///
25/// The blob is libfprint's own serialization, so it is authoritative for the template payload,
26/// username/description and enroll date. Only the device-identity fields libfprint exposes as
27/// live object getters are overlaid: the live object outranks (and, in practice, equals) the
28/// decoded copy.
29pub(crate) fn fp_to_core(fp: &FpPrint) -> Result<Print> {
30 let blob = fp.serialize().map_err(convert::from_gerror)?;
31 let mut print = fprint_fp3::from_bytes(&blob).map_err(convert::from_fp3)?;
32
33 let driver = fp.driver();
34 if !driver.is_empty() {
35 print.driver = Some(DriverId::new(driver));
36 }
37 let device_id = fp.device_id();
38 if !device_id.is_empty() {
39 print.device_id = Some(DeviceId::new(device_id));
40 }
41 print.device_stored = fp.device_stored();
42
43 Ok(print)
44}
45
46/// Build a fresh enrollment template on `dev` from a core [`Print`].
47///
48/// Only the metadata a caller can meaningfully supply before enrolling is copied. The enroll
49/// date is deliberately left unset: libfprint stamps it during enrollment and the completed
50/// print carries the authoritative value in its blob, so mapping core's `y/m/d` into a
51/// `glib::Date` here would be lossy work for a value that is immediately discarded.
52pub(crate) fn core_to_fp(print: &Print, dev: &FpDevice) -> FpPrint {
53 let fp = FpPrint::new(dev);
54 if let Some(finger) = print.finger {
55 fp.set_finger(convert::finger_to_fp(finger));
56 }
57 if let Some(username) = &print.username {
58 fp.set_username(username);
59 }
60 if let Some(description) = &print.description {
61 fp.set_description(description);
62 }
63 fp
64}
65
66/// Reconstruct a stored/enrolled `FpPrint` from a core [`Print`] for use as a match candidate
67/// (the enrolled side of `verify`, or a gallery entry of `identify`).
68pub(crate) fn core_to_fp_for_match(print: &Print) -> Result<FpPrint> {
69 let blob = fprint_fp3::to_bytes(print).map_err(convert::from_fp3)?;
70 FpPrint::deserialize(&blob).map_err(convert::from_gerror)
71}