libfprint/fpi-print.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * FPrint Print handling - Private APIs | ||
| 3 | * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org> | ||
| 4 | * Copyright (C) 2019 Benjamin Berg <bberg@redhat.com> | ||
| 5 | * | ||
| 6 | * This library is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This library is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with this library; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #define FP_COMPONENT "print" | ||
| 22 | #include "fpi-log.h" | ||
| 23 | |||
| 24 | #include "fp-print-private.h" | ||
| 25 | #include "fpi-device.h" | ||
| 26 | #include "fpi-compat.h" | ||
| 27 | |||
| 28 | /** | ||
| 29 | * SECTION: fpi-print | ||
| 30 | * @title: Internal FpPrint | ||
| 31 | * @short_description: Internal fingerprint handling routines | ||
| 32 | * | ||
| 33 | * Interaction with prints and their storage. See also the public | ||
| 34 | * #FpPrint routines. | ||
| 35 | */ | ||
| 36 | |||
| 37 | /** | ||
| 38 | * fpi_print_add_print: | ||
| 39 | * @print: A #FpPrint | ||
| 40 | * @add: Print to append to @print | ||
| 41 | * | ||
| 42 | * Appends the single #FPI_PRINT_NBIS print from @add to the collection of | ||
| 43 | * prints in @print. Both print objects need to be of type #FPI_PRINT_NBIS | ||
| 44 | * for this to work. | ||
| 45 | */ | ||
| 46 | void | ||
| 47 | 25 | fpi_print_add_print (FpPrint *print, FpPrint *add) | |
| 48 | { | ||
| 49 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 25 times.
|
25 | g_return_if_fail (print->type == FPI_PRINT_NBIS); |
| 50 |
1/2✓ Branch 4 → 5 taken 25 times.
✗ Branch 4 → 6 not taken.
|
25 | g_return_if_fail (add->type == FPI_PRINT_NBIS); |
| 51 | |||
| 52 |
1/2✓ Branch 5 → 7 taken 25 times.
✗ Branch 5 → 9 not taken.
|
25 | g_assert (add->prints->len == 1); |
| 53 | 25 | g_ptr_array_add (print->prints, g_memdup2 (add->prints->pdata[0], sizeof (struct xyt_struct))); | |
| 54 | } | ||
| 55 | |||
| 56 | /** | ||
| 57 | * fpi_print_set_type: | ||
| 58 | * @print: A #FpPrint | ||
| 59 | * @type: The newly type of the print data | ||
| 60 | * | ||
| 61 | * This function can only be called exactly once. Drivers should | ||
| 62 | * call it after creating a new print, or to initialize the template | ||
| 63 | * print passed during enrollment. | ||
| 64 | */ | ||
| 65 | void | ||
| 66 | 6263 | fpi_print_set_type (FpPrint *print, | |
| 67 | FpiPrintType type) | ||
| 68 | { | ||
| 69 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 6263 times.
|
6263 | g_return_if_fail (FP_IS_PRINT (print)); |
| 70 | /* We only allow setting this once! */ | ||
| 71 |
1/2✓ Branch 5 → 6 taken 6263 times.
✗ Branch 5 → 7 not taken.
|
6263 | g_return_if_fail (print->type == FPI_PRINT_UNDEFINED); |
| 72 | |||
| 73 | 6263 | print->type = type; | |
| 74 |
2/2✓ Branch 6 → 8 taken 54 times.
✓ Branch 6 → 12 taken 6209 times.
|
6263 | if (print->type == FPI_PRINT_NBIS) |
| 75 | { | ||
| 76 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 54 times.
|
54 | g_assert_null (print->prints); |
| 77 | 54 | print->prints = g_ptr_array_new_with_free_func (g_free); | |
| 78 | } | ||
| 79 | 6263 | g_object_notify (G_OBJECT (print), "fpi-type"); | |
| 80 | } | ||
| 81 | |||
| 82 | /** | ||
| 83 | * fpi_print_get_type: | ||
| 84 | * @print: A #FpPrint | ||
| 85 | * | ||
| 86 | * Retrieves the type of the print data. | ||
| 87 | * | ||
| 88 | * Returns: The #FpiPrintType of the print. | ||
| 89 | */ | ||
| 90 | FpiPrintType | ||
| 91 | 45 | fpi_print_get_type (FpPrint *print) | |
| 92 | { | ||
| 93 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 45 times.
|
45 | g_return_val_if_fail (FP_IS_PRINT (print), FPI_PRINT_UNDEFINED); |
| 94 | |||
| 95 | 45 | return print->type; | |
| 96 | } | ||
| 97 | |||
| 98 | /** | ||
| 99 | * fpi_print_set_device_stored: | ||
| 100 | * @print: A #FpPrint | ||
| 101 | * @device_stored: Whether the print is stored on the device or not | ||
| 102 | * | ||
| 103 | * Drivers must set this to %TRUE for any print that is really a handle | ||
| 104 | * for data that is stored on the device itself. | ||
| 105 | */ | ||
| 106 | void | ||
| 107 | 117 | fpi_print_set_device_stored (FpPrint *print, | |
| 108 | gboolean device_stored) | ||
| 109 | { | ||
| 110 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 117 times.
|
117 | g_return_if_fail (FP_IS_PRINT (print)); |
| 111 | |||
| 112 | 117 | print->device_stored = device_stored; | |
| 113 | 117 | g_object_notify (G_OBJECT (print), "device-stored"); | |
| 114 | } | ||
| 115 | |||
| 116 | /* XXX: This is the old version, but wouldn't it be smarter to instead | ||
| 117 | * use the highest quality mintutiae? Possibly just using bz_prune from | ||
| 118 | * upstream? */ | ||
| 119 | static void | ||
| 120 | 33 | minutiae_to_xyt (struct fp_minutiae *minutiae, | |
| 121 | int bwidth, | ||
| 122 | int bheight, | ||
| 123 | struct xyt_struct *xyt) | ||
| 124 | { | ||
| 125 | 33 | int i; | |
| 126 | 33 | struct fp_minutia *minutia; | |
| 127 | 33 | struct minutiae_struct c[MAX_FILE_MINUTIAE]; | |
| 128 | |||
| 129 | /* struct xyt_struct uses arrays of MAX_BOZORTH_MINUTIAE (200) */ | ||
| 130 | 33 | int nmin = min (minutiae->num, MAX_BOZORTH_MINUTIAE); | |
| 131 | |||
| 132 |
2/2✓ Branch 10 → 3 taken 2982 times.
✓ Branch 10 → 11 taken 33 times.
|
3015 | for (i = 0; i < nmin; i++) |
| 133 | { | ||
| 134 | 2982 | minutia = minutiae->list[i]; | |
| 135 | |||
| 136 | 2982 | lfs2nist_minutia_XYT (&c[i].col[0], &c[i].col[1], &c[i].col[2], | |
| 137 | minutia, bwidth, bheight); | ||
| 138 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 2982 times.
|
2982 | c[i].col[3] = sround (minutia->reliability * 100.0); |
| 139 | |||
| 140 |
2/2✓ Branch 7 → 8 taken 1530 times.
✓ Branch 7 → 9 taken 1452 times.
|
2982 | if (c[i].col[2] > 180) |
| 141 | 1530 | c[i].col[2] -= 360; | |
| 142 | } | ||
| 143 | |||
| 144 | 33 | qsort ((void *) &c, (size_t) nmin, sizeof (struct minutiae_struct), | |
| 145 | sort_x_y); | ||
| 146 | |||
| 147 |
2/2✓ Branch 14 → 13 taken 2982 times.
✓ Branch 14 → 15 taken 33 times.
|
3048 | for (i = 0; i < nmin; i++) |
| 148 | { | ||
| 149 | 2982 | xyt->xcol[i] = c[i].col[0]; | |
| 150 | 2982 | xyt->ycol[i] = c[i].col[1]; | |
| 151 | 2982 | xyt->thetacol[i] = c[i].col[2]; | |
| 152 | } | ||
| 153 | 33 | xyt->nrows = nmin; | |
| 154 | 33 | } | |
| 155 | |||
| 156 | /** | ||
| 157 | * fpi_print_add_from_image: | ||
| 158 | * @print: A #FpPrint | ||
| 159 | * @image: A #FpImage | ||
| 160 | * @error: Return location for error | ||
| 161 | * | ||
| 162 | * Extracts the minutiae from the given image and adds it to @print of | ||
| 163 | * type #FPI_PRINT_NBIS. | ||
| 164 | * | ||
| 165 | * The @image will be kept so that API users can get retrieve it e.g. | ||
| 166 | * for debugging purposes. | ||
| 167 | * | ||
| 168 | * Returns: %TRUE on success | ||
| 169 | */ | ||
| 170 | gboolean | ||
| 171 | 33 | fpi_print_add_from_image (FpPrint *print, | |
| 172 | FpImage *image, | ||
| 173 | GError **error) | ||
| 174 | { | ||
| 175 | 33 | GPtrArray *minutiae; | |
| 176 | 33 | struct fp_minutiae _minutiae; | |
| 177 | 33 | struct xyt_struct *xyt; | |
| 178 | |||
| 179 |
2/4✓ Branch 2 → 3 taken 33 times.
✗ Branch 2 → 4 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 33 times.
|
33 | if (print->type != FPI_PRINT_NBIS || !image) |
| 180 | { | ||
| 181 | ✗ | g_set_error (error, | |
| 182 | G_IO_ERROR, | ||
| 183 | G_IO_ERROR_INVALID_DATA, | ||
| 184 | "Cannot add print data from image!"); | ||
| 185 | ✗ | return FALSE; | |
| 186 | } | ||
| 187 | |||
| 188 | 33 | minutiae = fp_image_get_minutiae (image); | |
| 189 |
2/4✓ Branch 8 → 9 taken 33 times.
✗ Branch 8 → 10 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 13 taken 33 times.
|
33 | if (!minutiae || minutiae->len == 0) |
| 190 | { | ||
| 191 | ✗ | g_set_error (error, | |
| 192 | G_IO_ERROR, | ||
| 193 | G_IO_ERROR_INVALID_DATA, | ||
| 194 | "No minutiae found in image or not yet detected!"); | ||
| 195 | ✗ | return FALSE; | |
| 196 | } | ||
| 197 | |||
| 198 | 33 | _minutiae.num = minutiae->len; | |
| 199 | 33 | _minutiae.list = (struct fp_minutia **) minutiae->pdata; | |
| 200 | 33 | _minutiae.alloc = minutiae->len; | |
| 201 | |||
| 202 | 33 | xyt = g_new0 (struct xyt_struct, 1); | |
| 203 | 33 | minutiae_to_xyt (&_minutiae, image->width, image->height, xyt); | |
| 204 | 33 | g_ptr_array_add (print->prints, xyt); | |
| 205 | |||
| 206 |
1/2✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 33 times.
|
33 | g_clear_object (&print->image); |
| 207 | 33 | print->image = g_object_ref (image); | |
| 208 | 33 | g_object_notify (G_OBJECT (print), "image"); | |
| 209 | |||
| 210 | 33 | return TRUE; | |
| 211 | } | ||
| 212 | |||
| 213 | /** | ||
| 214 | * fpi_print_bz3_match: | ||
| 215 | * @print_template: A #FpPrint containing one or more prints | ||
| 216 | * @print: A newly scanned #FpPrint to test | ||
| 217 | * @bz3_threshold: The BZ3 match threshold | ||
| 218 | * @error: Return location for error | ||
| 219 | * | ||
| 220 | * Match the newly scanned @print (containing exactly one print) against the | ||
| 221 | * prints contained in @template which will have been stored during enrollment. | ||
| 222 | * | ||
| 223 | * Both @template and @print need to be of type #FPI_PRINT_NBIS for this to | ||
| 224 | * work. | ||
| 225 | * | ||
| 226 | * Returns: Whether the prints match, @error will be set if #FPI_MATCH_ERROR is returned | ||
| 227 | */ | ||
| 228 | FpiMatchResult | ||
| 229 | 9 | fpi_print_bz3_match (FpPrint *print_template, | |
| 230 | FpPrint *print, | ||
| 231 | gint bz3_threshold, | ||
| 232 | GError **error) | ||
| 233 | { | ||
| 234 | 9 | struct xyt_struct *pstruct; | |
| 235 | 9 | gint probe_len; | |
| 236 | 9 | gint i; | |
| 237 | |||
| 238 | /* XXX: Use a different error type? */ | ||
| 239 |
2/4✓ Branch 2 → 3 taken 9 times.
✗ Branch 2 → 4 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 9 times.
|
9 | if (print_template->type != FPI_PRINT_NBIS || print->type != FPI_PRINT_NBIS) |
| 240 | { | ||
| 241 | ✗ | *error = fpi_device_error_new_msg (FP_DEVICE_ERROR_NOT_SUPPORTED, | |
| 242 | "It is only possible to match NBIS type print data"); | ||
| 243 | ✗ | return FPI_MATCH_ERROR; | |
| 244 | } | ||
| 245 | |||
| 246 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 9 times.
|
9 | if (print->prints->len != 1) |
| 247 | { | ||
| 248 | ✗ | *error = fpi_device_error_new_msg (FP_DEVICE_ERROR_GENERAL, | |
| 249 | "New print contains more than one print!"); | ||
| 250 | ✗ | return FPI_MATCH_ERROR; | |
| 251 | } | ||
| 252 | |||
| 253 | 9 | pstruct = g_ptr_array_index (print->prints, 0); | |
| 254 | 9 | probe_len = bozorth_probe_init (pstruct); | |
| 255 | |||
| 256 |
2/2✓ Branch 15 → 11 taken 26 times.
✓ Branch 15 → 16 taken 3 times.
|
38 | for (i = 0; i < print_template->prints->len; i++) |
| 257 | { | ||
| 258 | 26 | struct xyt_struct *gstruct; | |
| 259 | 26 | gint score; | |
| 260 | 26 | gstruct = g_ptr_array_index (print_template->prints, i); | |
| 261 | 26 | score = bozorth_to_gallery (probe_len, pstruct, gstruct); | |
| 262 | 26 | fp_dbg ("score %d/%d", score, bz3_threshold); | |
| 263 | |||
| 264 |
2/2✓ Branch 13 → 14 taken 20 times.
✓ Branch 13 → 16 taken 6 times.
|
26 | if (score >= bz3_threshold) |
| 265 | return FPI_MATCH_SUCCESS; | ||
| 266 | } | ||
| 267 | |||
| 268 | return FPI_MATCH_FAIL; | ||
| 269 | } | ||
| 270 | |||
| 271 | /** | ||
| 272 | * fpi_print_generate_user_id: | ||
| 273 | * @print: #FpPrint to generate the ID for | ||
| 274 | * | ||
| 275 | * Generates a string identifier for the represented print. This identifier | ||
| 276 | * encodes some metadata about the print. It also includes a random string | ||
| 277 | * and may be assumed to be unique. | ||
| 278 | * | ||
| 279 | * This is useful if devices are able to store a string identifier, but more | ||
| 280 | * storing more metadata may be desirable. In effect, this means the driver | ||
| 281 | * can provide somewhat more meaningful data to fp_device_list_prints(). | ||
| 282 | * | ||
| 283 | * The generated ID may be truncated after 23 characters. However, more space | ||
| 284 | * is required to include the username, and it is recommended to store at | ||
| 285 | * at least 31 bytes. | ||
| 286 | * | ||
| 287 | * The generated format may change in the future. It is versioned though and | ||
| 288 | * decoding should remain functional. | ||
| 289 | * | ||
| 290 | * Returns: A unique string of 23 + strlen(username) characters | ||
| 291 | */ | ||
| 292 | gchar * | ||
| 293 | 17 | fpi_print_generate_user_id (FpPrint *print) | |
| 294 | { | ||
| 295 | 17 | const gchar *username = NULL; | |
| 296 | 17 | gchar *user_id = NULL; | |
| 297 | 17 | const GDate *date; | |
| 298 | 17 | gint y = 0, m = 0, d = 0; | |
| 299 | 17 | gint32 rand_id = 0; | |
| 300 | |||
| 301 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 17 times.
|
17 | g_assert (print); |
| 302 | 17 | date = fp_print_get_enroll_date (print); | |
| 303 |
1/4✗ Branch 5 → 6 not taken.
✓ Branch 5 → 12 taken 17 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 12 not taken.
|
17 | if (date && g_date_valid (date)) |
| 304 | { | ||
| 305 | ✗ | y = g_date_get_year (date); | |
| 306 | ✗ | m = g_date_get_month (date); | |
| 307 | ✗ | d = g_date_get_day (date); | |
| 308 | } | ||
| 309 | |||
| 310 | 17 | username = fp_print_get_username (print); | |
| 311 |
1/2✓ Branch 13 → 14 taken 17 times.
✗ Branch 13 → 15 not taken.
|
17 | if (!username) |
| 312 | 17 | username = "nobody"; | |
| 313 | |||
| 314 |
1/2✗ Branch 16 → 17 not taken.
✓ Branch 16 → 19 taken 17 times.
|
17 | if (fpi_device_emulation_mode_enabled (NULL)) |
| 315 | rand_id = 0; | ||
| 316 | else | ||
| 317 | ✗ | rand_id = g_random_int (); | |
| 318 | |||
| 319 | 17 | user_id = g_strdup_printf ("FP1-%04d%02d%02d-%X-%08X-%s", | |
| 320 | y, m, d, | ||
| 321 | 17 | fp_print_get_finger (print), | |
| 322 | rand_id, | ||
| 323 | username); | ||
| 324 | |||
| 325 | 17 | return user_id; | |
| 326 | |||
| 327 | } | ||
| 328 | |||
| 329 | /** | ||
| 330 | * fpi_print_fill_from_user_id: | ||
| 331 | * @print: #FpPrint to fill metadata into | ||
| 332 | * @user_id: An ID that was likely encoded using fpi_print_generate_user_id() | ||
| 333 | * | ||
| 334 | * This is the reverse operation of fpi_print_generate_user_id(), allowing | ||
| 335 | * the driver to encode some print metadata in a string. | ||
| 336 | * | ||
| 337 | * Returns: Whether a valid ID was found | ||
| 338 | */ | ||
| 339 | gboolean | ||
| 340 | 89 | fpi_print_fill_from_user_id (FpPrint *print, const char *user_id) | |
| 341 | { | ||
| 342 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 89 times.
|
89 | g_return_val_if_fail (user_id, FALSE); |
| 343 | |||
| 344 | /* The format has 24 bytes at the start and some dashes in the right places */ | ||
| 345 |
5/6✓ Branch 4 → 5 taken 85 times.
✓ Branch 4 → 28 taken 4 times.
✓ Branch 5 → 6 taken 70 times.
✓ Branch 5 → 28 taken 15 times.
✓ Branch 6 → 7 taken 70 times.
✗ Branch 6 → 28 not taken.
|
89 | if (g_str_has_prefix (user_id, "FP1-") && strlen (user_id) >= 24 && |
| 346 |
3/6✓ Branch 7 → 8 taken 70 times.
✗ Branch 7 → 28 not taken.
✓ Branch 8 → 9 taken 70 times.
✗ Branch 8 → 28 not taken.
✓ Branch 9 → 10 taken 70 times.
✗ Branch 9 → 28 not taken.
|
70 | user_id[12] == '-' && user_id[14] == '-' && user_id[23] == '-') |
| 347 | { | ||
| 348 |
1/2✗ Branch 10 → 11 not taken.
✓ Branch 10 → 13 taken 70 times.
|
140 | g_autofree gchar *copy = g_strdup (user_id); |
| 349 | 70 | g_autoptr(GDate) date = NULL; | |
| 350 | 70 | gint32 date_ymd; | |
| 351 | 70 | gint32 finger; | |
| 352 | 70 | gchar *username; | |
| 353 | /* Try to parse information from the string. */ | ||
| 354 | |||
| 355 | 70 | copy[12] = '\0'; | |
| 356 | 70 | date_ymd = g_ascii_strtod (copy + 4, NULL); | |
| 357 |
2/2✓ Branch 15 → 16 taken 9 times.
✓ Branch 15 → 17 taken 61 times.
|
70 | if (date_ymd > 0) |
| 358 | 9 | date = g_date_new_dmy (date_ymd % 100, | |
| 359 | 9 | (date_ymd / 100) % 100, | |
| 360 | 9 | date_ymd / 10000); | |
| 361 | else | ||
| 362 | 61 | date = g_date_new (); | |
| 363 | |||
| 364 | 70 | fp_print_set_enroll_date (print, date); | |
| 365 | |||
| 366 | 70 | copy[14] = '\0'; | |
| 367 | 70 | finger = g_ascii_strtoll (copy + 13, NULL, 16); | |
| 368 | 70 | fp_print_set_finger (print, finger); | |
| 369 | |||
| 370 | /* We ignore the next chunk, it is just random data. | ||
| 371 | * Then comes the username; nobody is the default if the metadata | ||
| 372 | * is unknown */ | ||
| 373 | 70 | username = copy + 24; | |
| 374 |
3/4✓ Branch 21 → 22 taken 70 times.
✗ Branch 21 → 25 not taken.
✓ Branch 23 → 24 taken 15 times.
✓ Branch 23 → 25 taken 55 times.
|
70 | if (strlen (username) > 0 && g_strcmp0 (username, "nobody") != 0) |
| 375 | 15 | fp_print_set_username (print, username); | |
| 376 | |||
| 377 |
1/2✓ Branch 25 → 26 taken 70 times.
✗ Branch 25 → 27 not taken.
|
70 | return TRUE; |
| 378 | } | ||
| 379 | |||
| 380 | return FALSE; | ||
| 381 | } | ||
| 382 |