fp-print.c 24.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * FPrint Print handling
 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
 * Copyright (C) 2019 Benjamin Berg <bberg@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21
#define FP_COMPONENT "print"
22

23
#include "fp-print-private.h"
24
#include "fpi-compat.h"
25
#include "fpi-log.h"
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

/**
 * SECTION: fp-print
 * @title: FpPrint
 * @short_description: Fingerprint handling
 *
 * Interaction with prints and their storage.
 */

/**
 * SECTION: fpi-print
 * @title: Internal FpPrint
 * @short_description: Internal fingerprint handling routines
 *
 * Interaction with prints and their storage. See also the public
 * #FpPrint routines.
 */

G_DEFINE_TYPE (FpPrint, fp_print, G_TYPE_INITIALLY_UNOWNED)

enum {
  PROP_0,
  PROP_DRIVER,
  PROP_DEVICE_ID,
  PROP_DEVICE_STORED,
  PROP_IMAGE,

  /* The following is metadata that is stored by default for each print.
Bastien Nocera's avatar
Bastien Nocera committed
54
   * Drivers may make use of these during enrollment (e.g. to additionally store
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
   * the metadata on the device). */
  PROP_FINGER,
  PROP_USERNAME,
  PROP_DESCRIPTION,
  PROP_ENROLL_DATE,

  /* Private property*/
  PROP_FPI_TYPE,
  PROP_FPI_DATA,
  N_PROPS
};

static GParamSpec *properties[N_PROPS];

static void
fp_print_finalize (GObject *object)
{
  FpPrint *self = (FpPrint *) object;

  g_clear_object (&self->image);
  g_clear_pointer (&self->device_id, g_free);
  g_clear_pointer (&self->driver, g_free);
  g_clear_pointer (&self->username, g_free);
  g_clear_pointer (&self->description, g_free);
  g_clear_pointer (&self->enroll_date, g_date_free);
  g_clear_pointer (&self->data, g_variant_unref);
81
  g_clear_pointer (&self->prints, g_ptr_array_unref);
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

  G_OBJECT_CLASS (fp_print_parent_class)->finalize (object);
}

static void
fp_print_get_property (GObject    *object,
                       guint       prop_id,
                       GValue     *value,
                       GParamSpec *pspec)
{
  FpPrint *self = FP_PRINT (object);

  switch (prop_id)
    {
    case PROP_DRIVER:
      g_value_set_string (value, self->driver);
      break;

    case PROP_DEVICE_ID:
      g_value_set_string (value, self->device_id);
      break;

    case PROP_DEVICE_STORED:
      g_value_set_boolean (value, self->device_stored);
      break;

    case PROP_IMAGE:
      g_value_set_object (value, self->image);
      break;

    case PROP_FINGER:
      g_value_set_enum (value, self->finger);
      break;

    case PROP_USERNAME:
      g_value_set_string (value, self->username);
      break;

    case PROP_DESCRIPTION:
      g_value_set_string (value, self->description);
      break;

    case PROP_ENROLL_DATE:
      g_value_set_boxed (value, self->enroll_date);
      break;

    case PROP_FPI_TYPE:
      g_value_set_enum (value, self->type);
      break;

    case PROP_FPI_DATA:
      g_value_set_variant (value, self->data);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
fp_print_set_property (GObject      *object,
                       guint         prop_id,
                       const GValue *value,
                       GParamSpec   *pspec)
{
  FpPrint *self = FP_PRINT (object);

  switch (prop_id)
    {
    case PROP_FPI_TYPE:
      fpi_print_set_type (self, g_value_get_enum (value));
      break;

    case PROP_DRIVER:
      self->driver = g_value_dup_string (value);
      break;

    case PROP_DEVICE_ID:
      self->device_id = g_value_dup_string (value);
      break;

    case PROP_DEVICE_STORED:
      self->device_stored = g_value_get_boolean (value);
      break;

    case PROP_FINGER:
      self->finger = g_value_get_enum (value);
      break;

    case PROP_USERNAME:
      g_clear_pointer (&self->username, g_free);
      self->username = g_value_dup_string (value);
      break;

    case PROP_DESCRIPTION:
      g_clear_pointer (&self->description, g_free);
      self->description = g_value_dup_string (value);
      break;

    case PROP_ENROLL_DATE:
      g_clear_pointer (&self->enroll_date, g_date_free);
      self->enroll_date = g_value_dup_boxed (value);
      break;

    case PROP_FPI_DATA:
187
      g_clear_pointer (&self->data, g_variant_unref);
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
      self->data = g_value_dup_variant (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
fp_print_constructed (GObject *obj)
{
  FpPrint *self = FP_PRINT (obj);

  g_assert (self->driver != NULL);
  g_assert (self->device_id != NULL);
}

static void
fp_print_class_init (FpPrintClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = fp_print_constructed;
  object_class->finalize = fp_print_finalize;
  object_class->get_property = fp_print_get_property;
  object_class->set_property = fp_print_set_property;

  properties[PROP_DRIVER] =
    g_param_spec_string ("driver",
                         "Driver",
                         "The name of the driver that created the print",
                         NULL,
                         G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

  properties[PROP_DEVICE_ID] =
    g_param_spec_string ("device-id",
                         "Device ID",
                         "Unique ID allowing to check if a device is compatible with the print",
                         NULL,
                         G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

  properties[PROP_DEVICE_STORED] =
    g_param_spec_boolean ("device-stored",
                          "Device Stored",
                          "Whether the print is a handle for data that is stored on the device",
                          FALSE,
                          G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

  properties[PROP_IMAGE] =
    g_param_spec_object ("image",
                         "Image",
                         "The image that was used for the print, only valid for newly enrolled prints on image based devices",
                         FP_TYPE_IMAGE,
                         G_PARAM_STATIC_STRINGS | G_PARAM_READABLE);

  properties[PROP_FINGER] =
    g_param_spec_enum ("finger",
                       "Finger",
                       "The enrolled finger",
                       FP_TYPE_FINGER,
                       FP_FINGER_UNKNOWN,
                       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);

  properties[PROP_USERNAME] =
    g_param_spec_string ("username",
                         "Username",
                         "The username that the enrolled print belongs to",
                         NULL,
                         G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);

  properties[PROP_DESCRIPTION] =
    g_param_spec_string ("description",
                         "Description",
                         "A user defined description for the print",
                         NULL,
                         G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);

  properties[PROP_ENROLL_DATE] =
    g_param_spec_boxed ("enroll-date",
                        "Enroll Date",
                        "The date of enrollment",
                        G_TYPE_DATE,
                        G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);

272
273
274
275
276
277
278
  /**
   * FpPrint::fpi-type: (skip)
   *
   * This property is only for internal purposes.
   *
   * Stability: private
   */
279
  properties[PROP_FPI_TYPE] =
280
    g_param_spec_enum ("fpi-type",
281
282
                       "Type",
                       "Private: The type of the print data",
283
284
                       FPI_TYPE_PRINT_TYPE,
                       FPI_PRINT_RAW,
285
286
                       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

287
288
289
290
291
292
293
  /**
   * FpPrint::fpi-data: (skip)
   *
   * This property is only for internal purposes.
   *
   * Stability: private
   */
294
  properties[PROP_FPI_DATA] =
295
    g_param_spec_variant ("fpi-data",
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
                          "Raw Data",
                          "The raw data for internal use only",
                          G_VARIANT_TYPE_ANY,
                          NULL,
                          G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);

  g_object_class_install_properties (object_class, N_PROPS, properties);
}

static void
fp_print_init (FpPrint *self)
{
}

/**
 * fp_print_new:
 * @device: A #FpDevice
 *
 * Create a new #FpPrint. This is only useful to prepare an enrollment
 * of a new print using fp_device_enroll(). For this you should first
 * create a new print, fill in the relevant metadata, and then start
 * enrollment.
 *
 * Returns: (transfer floating): A newyl created #FpPrint
 */
FpPrint *
fp_print_new (FpDevice *device)
{
  g_return_val_if_fail (device, NULL);

  return g_object_new (FP_TYPE_PRINT,
                       "driver", fp_device_get_driver (device),
                       "device-id", fp_device_get_device_id (device),
                       NULL);
}

/**
 * fp_print_get_driver:
 * @print: A #FpPrint
 *
 * Returns the driver that the print was created for.
 *
 * Returns: (transfer none): The driver
 */
const gchar *
fp_print_get_driver (FpPrint *print)
{
  g_return_val_if_fail (FP_IS_PRINT (print), NULL);

  return print->driver;
}

/**
 * fp_print_get_device_id:
 * @print: A #FpPrint
 *
 * Returns the device ID that the print was created for.
 *
 * Returns: (transfer none): The device ID
 */
const gchar *
fp_print_get_device_id (FpPrint *print)
{
  g_return_val_if_fail (FP_IS_PRINT (print), NULL);

  return print->device_id;
}

/**
 * fp_print_get_device_stored:
 * @print: A #FpPrint
 *
 * Whether the print is actually stored on the device and this is
 * just a handle to use that references the device stored data.
 *
 * Returns: Whether the print is stored on the device
 */
gboolean
fp_print_get_device_stored (FpPrint *print)
{
  g_return_val_if_fail (FP_IS_PRINT (print), FALSE);

  return print->device_stored;
}

/**
 * fp_print_get_image:
 * @print: A #FpPrint
 *
 * Returns the image that the print was created from, or %NULL
 *
 * Returns: (transfer none) (nullable): The #FpImage
 */
FpImage *
fp_print_get_image (FpPrint *print)
{
  g_return_val_if_fail (FP_IS_PRINT (print), NULL);

  return print->image;
}

/**
 * fp_print_get_finger:
 * @print: A #FpPrint
 *
 * Returns the finger that the print was created for.
 *
 * Returns: The #FpFinger
 */
FpFinger
fp_print_get_finger (FpPrint *print)
{
  g_return_val_if_fail (FP_IS_PRINT (print), FP_FINGER_UNKNOWN);

  return print->finger;
}

/**
 * fp_print_get_username:
 * @print: A #FpPrint
 *
 * Returns the user defined username for the print.
 *
 * Returns: (transfer none) (nullable): The username
 */
const gchar *
fp_print_get_username (FpPrint *print)
{
  g_return_val_if_fail (FP_IS_PRINT (print), NULL);

  return print->username;
}

/**
 * fp_print_get_description:
 * @print: A #FpPrint
 *
 * Returns the user defined description for the print.
 *
 * Returns: (transfer none) (nullable): The description
 */
const gchar *
fp_print_get_description (FpPrint *print)
{
  g_return_val_if_fail (FP_IS_PRINT (print), NULL);

  return print->description;
}

/**
 * fp_print_get_enroll_date:
 * @print: A #FpPrint
 *
 * Returns the user defined enroll date for the print.
 *
 * Returns: (transfer none) (nullable): The #GDate
 */
const GDate *
fp_print_get_enroll_date (FpPrint *print)
{
  g_return_val_if_fail (FP_IS_PRINT (print), NULL);

  return print->enroll_date;
}

/**
 * fp_print_set_finger:
 * @print: A #FpPrint
 * @finger: The #FpFinger
 *
 * Set the finger that the print is for.
 */
void
fp_print_set_finger (FpPrint *print,
                     FpFinger finger)
{
  g_return_if_fail (FP_IS_PRINT (print));

  print->finger = finger;
  g_object_notify_by_pspec (G_OBJECT (print), properties[PROP_FINGER]);
}

/**
 * fp_print_set_username:
 * @print: A #FpPrint
 * @username: (transfer none): The new username
 *
 * Set the username for the print.
 */
void
fp_print_set_username (FpPrint     *print,
                       const gchar *username)
{
  g_return_if_fail (FP_IS_PRINT (print));

  g_clear_pointer (&print->username, g_free);
  print->username = g_strdup (username);
  g_object_notify_by_pspec (G_OBJECT (print), properties[PROP_USERNAME]);
}

/**
 * fp_print_set_description:
 * @print: A #FpPrint
 * @description: (transfer none): The new description
 *
 * Set the description for the print.
 */
void
fp_print_set_description (FpPrint     *print,
                          const gchar *description)
{
  g_return_if_fail (FP_IS_PRINT (print));

  g_clear_pointer (&print->description, g_free);
  print->description = g_strdup (description);
  g_object_notify_by_pspec (G_OBJECT (print), properties[PROP_DESCRIPTION]);
}

/**
 * fp_print_set_enroll_date:
 * @print: A #FpPrint
 * @enroll_date: (transfer none): The new enroll date
 *
 * Set the enroll date for the print.
 */
void
fp_print_set_enroll_date (FpPrint     *print,
                          const GDate *enroll_date)
{
  g_return_if_fail (FP_IS_PRINT (print));

  g_clear_pointer (&print->enroll_date, g_date_free);
  if (enroll_date)
Marco Trevisan's avatar
Marco Trevisan committed
529
530
    print->enroll_date = g_date_copy (enroll_date);

531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
  g_object_notify_by_pspec (G_OBJECT (print), properties[PROP_ENROLL_DATE]);
}

/**
 * fp_print_compatible:
 * @self: A #FpPrint
 * @device: A #FpDevice
 *
 * Tests whether the prints is compatible with the given device.
 *
 * Returns: %TRUE if the print is compatible with the device
 */
gboolean
fp_print_compatible (FpPrint *self, FpDevice *device)
{
  g_return_val_if_fail (FP_IS_PRINT (self), FALSE);
  g_return_val_if_fail (FP_IS_DEVICE (device), FALSE);

  if (g_strcmp0 (self->driver, fp_device_get_driver (device)))
    return FALSE;

  if (g_strcmp0 (self->device_id, fp_device_get_device_id (device)))
    return FALSE;

  return TRUE;
}

/**
 * fp_print_equal:
 * @self: First #FpPrint
 * @other: Second #FpPrint
 *
 * Tests whether the prints can be considered equal. This only compares the
 * actual information about the print, not the metadata.
 *
 * Returns: %TRUE if the prints are equal
 */
gboolean
fp_print_equal (FpPrint *self, FpPrint *other)
{
  g_return_val_if_fail (FP_IS_PRINT (self), FALSE);
  g_return_val_if_fail (FP_IS_PRINT (other), FALSE);
573
574
  g_return_val_if_fail (self->type != FPI_PRINT_UNDEFINED, FALSE);
  g_return_val_if_fail (other->type != FPI_PRINT_UNDEFINED, FALSE);
575
576
577
578
579
580
581
582
583
584

  if (self->type != other->type)
    return FALSE;

  if (g_strcmp0 (self->driver, other->driver))
    return FALSE;

  if (g_strcmp0 (self->device_id, other->device_id))
    return FALSE;

585
  if (self->type == FPI_PRINT_RAW)
586
587
588
    {
      return g_variant_equal (self->data, other->data);
    }
589
  else if (self->type == FPI_PRINT_NBIS)
590
    {
591
      guint i;
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612

      if (self->prints->len != other->prints->len)
        return FALSE;

      for (i = 0; i < self->prints->len; i++)
        {
          struct xyt_struct *a = g_ptr_array_index (self->prints, i);
          struct xyt_struct *b = g_ptr_array_index (other->prints, i);

          if (memcmp (a, b, sizeof (struct xyt_struct)) != 0)
            return FALSE;
        }

      return TRUE;
    }
  else
    {
      g_assert_not_reached ();
    }
}

613
#define FPI_PRINT_VARIANT_TYPE G_VARIANT_TYPE ("(issbymsmsia{sv}v)")
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635

G_STATIC_ASSERT (sizeof (((struct xyt_struct *) NULL)->xcol[0]) == 4);

/**
 * fp_print_serialize:
 * @print: A #FpPrint
 * @data: (array length=length) (transfer full) (out): Return location for data pointer
 * @length: (transfer full) (out): Length of @data
 * @error: Return location for error
 *
 * Serialize a print definition for permanent storage. Note that this is
 * lossy in the sense that e.g. the image data is discarded.
 *
 * Returns: (type void): %TRUE on success
 */
gboolean
fp_print_serialize (FpPrint *print,
                    guchar **data,
                    gsize   *length,
                    GError **error)
{
  g_autoptr(GVariant) result = NULL;
636
  GVariantBuilder builder = G_VARIANT_BUILDER_INIT (FPI_PRINT_VARIANT_TYPE);
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
  gsize len;

  g_assert (data);
  g_assert (length);

  g_variant_builder_add (&builder, "i", print->type);
  g_variant_builder_add (&builder, "s", print->driver);
  g_variant_builder_add (&builder, "s", print->device_id);
  g_variant_builder_add (&builder, "b", print->device_stored);

  /* Metadata */
  g_variant_builder_add (&builder, "y", print->finger);
  g_variant_builder_add (&builder, "ms", print->username);
  g_variant_builder_add (&builder, "ms", print->description);
  if (print->enroll_date && g_date_valid (print->enroll_date))
    g_variant_builder_add (&builder, "i", g_date_get_julian (print->enroll_date));
  else
    g_variant_builder_add (&builder, "i", G_MININT32);

  /* Unused a{sv} for expansion */
  g_variant_builder_open (&builder, G_VARIANT_TYPE_VARDICT);
  g_variant_builder_close (&builder);

  /* Insert NBIS print data for type NBIS, otherwise the GVariant directly */
661
  if (print->type == FPI_PRINT_NBIS)
662
663
    {
      GVariantBuilder nested = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE ("(a(aiaiai))"));
664
      guint i;
665
666
667
668
669
670
671
672
673
674

      g_variant_builder_open (&nested, G_VARIANT_TYPE ("a(aiaiai)"));
      for (i = 0; i < print->prints->len; i++)
        {
          struct xyt_struct *xyt = g_ptr_array_index (print->prints, i);

          g_variant_builder_open (&nested, G_VARIANT_TYPE ("(aiaiai)"));

          g_variant_builder_add_value (&nested,
                                       g_variant_new_fixed_array (G_VARIANT_TYPE_INT32,
675
                                                                  xyt->xcol,
676
                                                                  xyt->nrows,
677
                                                                  sizeof (xyt->xcol[0])));
678
679
          g_variant_builder_add_value (&nested,
                                       g_variant_new_fixed_array (G_VARIANT_TYPE_INT32,
680
                                                                  xyt->ycol,
681
                                                                  xyt->nrows,
682
                                                                  sizeof (xyt->ycol[0])));
683
684
          g_variant_builder_add_value (&nested,
                                       g_variant_new_fixed_array (G_VARIANT_TYPE_INT32,
685
                                                                  xyt->thetacol,
686
                                                                  xyt->nrows,
687
                                                                  sizeof (xyt->thetacol[0])));
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
          g_variant_builder_close (&nested);
        }

      g_variant_builder_close (&nested);
      g_variant_builder_add (&builder, "v", g_variant_builder_end (&nested));
    }
  else
    {
      g_variant_builder_add (&builder, "v", g_variant_new_variant (print->data));
    }

  result = g_variant_builder_end (&builder);

  if (G_BYTE_ORDER == G_BIG_ENDIAN)
    {
      GVariant *tmp;
      tmp = g_variant_byteswap (result);
      g_variant_unref (result);
      result = tmp;
    }

  len = g_variant_get_size (result);
  /* Add 3 bytes of header */
  len += 3;

  *data = g_malloc (len);
  *length = len;

  (*data)[0] = (guchar) 'F';
  (*data)[1] = (guchar) 'P';
  (*data)[2] = (guchar) '3';

  g_variant_get_data (result);
  g_variant_store (result, (*data) + 3);

  return TRUE;
}

/**
 * fp_print_deserialize:
 * @data: (array length=length): The binary data
 * @length: Length of the data
 * @error: Return location for error
 *
 * Deserialize a print definition from permanent storage.
 *
 * Returns: (transfer full): A newly created #FpPrint on success
 */
FpPrint *
fp_print_deserialize (const guchar *data,
                      gsize         length,
                      GError      **error)
{
  g_autoptr(FpPrint) result = NULL;
  g_autoptr(GVariant) raw_value = NULL;
  g_autoptr(GVariant) value = NULL;
744
  g_autoptr(GVariant) print_data = NULL;
745
  g_autoptr(GDate) date = NULL;
746
747
748
749
750
751
  guchar *aligned_data = NULL;
  guint8 finger_int8;
  FpFinger finger;
  g_autofree gchar *username = NULL;
  g_autofree gchar *description = NULL;
  gint julian_date;
752
  FpiPrintType type;
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
  const gchar *driver;
  const gchar *device_id;
  gboolean device_stored;

  g_assert (data);
  g_assert (length > 3);

  if (memcmp (data, "FP3", 3) != 0)
    goto invalid_format;

  /* NOTE:
   * We make sure that we have no variant left over from the parsing at the end
   * of this function (meaning we don't need to keep the data around.
   */

  /* To support GLIB < 2.60 we need to make sure that the memory is aligned correctly.
   * We also need to copy the backing store for the raw data that we may keep for
   * longer. */
  aligned_data = g_malloc (length - 3);
  memcpy (aligned_data, data + 3, length - 3);
773
  raw_value = g_variant_new_from_data (FPI_PRINT_VARIANT_TYPE,
774
                                       aligned_data, length - 3,
775
                                       FALSE, g_free, aligned_data);
776
777
778
779
780
781
782
783
784
785

  if (!raw_value)
    goto invalid_format;

  if (G_BYTE_ORDER == G_BIG_ENDIAN)
    value = g_variant_byteswap (raw_value);
  else
    value = g_variant_get_normal_form (raw_value);

  g_variant_get (value,
786
                 "(i&s&sbymsmsi@a{sv}v)",
787
788
789
790
791
792
793
794
795
796
797
798
799
800
                 &type,
                 &driver,
                 &device_id,
                 &device_stored,
                 &finger_int8,
                 &username,
                 &description,
                 &julian_date,
                 NULL,
                 &print_data);

  finger = finger_int8;

  /* Assume data is valid at this point if the values are somewhat sane. */
801
  if (type == FPI_PRINT_NBIS)
802
803
    {
      g_autoptr(GVariant) prints = g_variant_get_child_value (print_data, 0);
804
      guint i;
805
806
807
808
809
810

      result = g_object_new (FP_TYPE_PRINT,
                             "driver", driver,
                             "device-id", device_id,
                             "device-stored", device_stored,
                             NULL);
811
      fpi_print_set_type (result, FPI_PRINT_NBIS);
812
813
      for (i = 0; i < g_variant_n_children (prints); i++)
        {
814
          g_autofree struct xyt_struct *xyt = NULL;
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
          const gint32 *xcol, *ycol, *thetacol;
          gsize xlen, ylen, thetalen;
          g_autoptr(GVariant) xyt_data = NULL;
          GVariant *child;

          xyt_data = g_variant_get_child_value (prints, i);

          child = g_variant_get_child_value (xyt_data, 0);
          xcol = g_variant_get_fixed_array (child, &xlen, sizeof (gint32));
          g_variant_unref (child);

          child = g_variant_get_child_value (xyt_data, 1);
          ycol = g_variant_get_fixed_array (child, &ylen, sizeof (gint32));
          g_variant_unref (child);

          child = g_variant_get_child_value (xyt_data, 2);
          thetacol = g_variant_get_fixed_array (child, &thetalen, sizeof (gint32));
          g_variant_unref (child);

          if (xlen != ylen || xlen != thetalen)
            goto invalid_format;

          if (xlen > G_N_ELEMENTS (xyt->xcol))
            goto invalid_format;

840
          xyt = g_new0 (struct xyt_struct, 1);
841
842
843
844
845
          xyt->nrows = xlen;
          memcpy (xyt->xcol, xcol, sizeof (xcol[0]) * xlen);
          memcpy (xyt->ycol, ycol, sizeof (xcol[0]) * xlen);
          memcpy (xyt->thetacol, thetacol, sizeof (xcol[0]) * xlen);

846
          g_ptr_array_add (result->prints, g_steal_pointer (&xyt));
847
848
        }
    }
849
  else if (type == FPI_PRINT_RAW)
850
851
852
853
    {
      g_autoptr(GVariant) fp_data = g_variant_get_child_value (print_data, 0);

      result = g_object_new (FP_TYPE_PRINT,
854
                             "fpi-type", type,
855
856
857
                             "driver", driver,
                             "device-id", device_id,
                             "device-stored", device_stored,
858
                             "fpi-data", fp_data,
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
                             NULL);
    }
  else
    {
      g_warning ("Invalid print type: 0x%X", type);
      goto invalid_format;
    }

  date = g_date_new_julian (julian_date);
  g_object_set (result,
                "finger", finger,
                "username", username,
                "description", description,
                "enroll_date", date,
                NULL);

  return g_steal_pointer (&result);

invalid_format:
878
879
  g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
               "Data could not be parsed");
880
  return NULL;
881
}