Debian Codegraph @

src/EWS/addressbook/ews-oab-decoder.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 1999-2011 Novell, Inc. (www.novell.com)
3 * SPDX-FileContributor: Chenthill Palanisamy <[email protected]>
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 #include "evolution-ews-config.h"
8
9 #include <math.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <glib/gstdio.h>
13
14 #include <libebook/libebook.h>
15 #include <libedata-book/libedata-book.h>
16
17 #include "ews-oab-decoder.h"
18 #include "ews-oab-props.h"
19
20 #define d(x)
21
22 #define EOD_ERROR \
23 (ews_oab_decoder_error_quark ())
24
25 struct _EwsOabDecoderPrivate {
26 gchar *cache_dir;
27 GInputStream *fis;
28
29 guint32 total_records;
30 GSList *hdr_props;
31 GSList *oab_props;
32
33 GHashTable *prop_index_dict;
34 };
35
36 G_DEFINE_TYPE_WITH_PRIVATE (EwsOabDecoder, ews_oab_decoder, G_TYPE_OBJECT)
37
38 /* The of properties which will be accumulated and later set in EContact */
39 typedef struct {
40 EContactAddress *addr;
41 } EwsDeferredSet;
42
43 static void
44 ews_populate_string_sha1 (EContact *contact,
45 EContactField field,
46 gpointer value,
47 gpointer user_data)
48 {
49 gchar *sum;
50
51 sum = g_compute_checksum_for_string (G_CHECKSUM_SHA1, (const gchar *) value, -1);
52 e_contact_set (contact, field, sum);
53 g_free (sum);
54 }
55
56 static void
57 ews_populate_simple_string (EContact *contact,
58 EContactField field,
59 gpointer value,
60 gpointer user_data)
61 {
62 const gchar *str = (const gchar *) value;
63 e_contact_set (contact, field, str);
64 }
65
66 static void
67 ews_deffered_populate_physical_address (EwsDeferredSet *dset,
68 guint32 prop_id,
69 gpointer value)
70 {
71 gchar *val = g_strdup ((gchar *) value);
72
73 if (!dset->addr)
74 dset->addr = e_contact_address_new ();
75
76 switch (prop_id) {
77 case EWS_PT_STREET_ADDRESS:
78 dset->addr->street = val;
79 break;
80 case EWS_PT_LOCALITY:
81 dset->addr->locality = val;
82 break;
83 case EWS_PT_STATE_OR_PROVINCE:
84 dset->addr->region = val;
85 break;
86 case EWS_PT_POSTAL_CODE:
87 dset->addr->code = val;
88 break;
89 case EWS_PT_COUNTRY:
90 dset->addr->country = val;
91 break;
92 default:
93 g_free (val);
94 break;
95 }
96 }
97
98 static void
99 ews_populate_phone_numbers (EContact *contact,
100 EContactField field,
101 gpointer value,
102 gpointer user_data)
103 {
104 GSList *values = (GSList *) value;
105 gint i, len;
106
107 /* Just the two phone numbers from the list. it would be either business or home phone number. I don't
108 * see a cleaner way than this at the moment */
109 len = g_slist_length (values);
110 for (i = 0; i < 2 && i < len; i++) {
111 const gchar *val = g_slist_nth_data (values, i);
112 e_contact_set (contact, field + i, val);
113 }
114 }
115
116 static void
117 ews_populate_string_list (EContact *contact,
118 EContactField field,
119 gpointer value,
120 gpointer user_data)
121 {
122 GSList *sl_values = (GSList *) value, *sl;
123 GList *l_values = NULL;
124
125 for (sl = sl_values; sl != NULL; sl = g_slist_next (sl)) {
126 const gchar *val = (gchar *) sl_values->data;
127 l_values = g_list_prepend (l_values, g_strdup (val));
128 }
129
130 l_values = g_list_reverse (l_values);
131 e_contact_set (contact, field, l_values);
132
133 g_list_free (l_values);
134 }
135
136 static void
137 ews_populate_cert_data (EContact *contact,
138 GBytes *bytes)
139 {
140 EContactCert cert;
141
142 if (!bytes || !g_bytes_get_size (bytes))
143 return;
144
145 cert.data = (gpointer) g_bytes_get_data (bytes, &cert.length);
146 cert.length = g_bytes_get_size (bytes);
147
148 e_contact_set (contact, E_CONTACT_X509_CERT, &cert);
149 }
150
151 static void
152 ews_populate_cert (EContact *contact,
153 EContactField field,
154 gpointer value,
155 gpointer user_data)
156 {
157 GSList *link;
158
159 for (link = value; link; link = g_slist_next (link)) {
160 GBytes *bytes = link->data;
161
162 ews_populate_cert_data (contact, bytes);
163 }
164 }
165
166 static void
167 ews_populate_user_cert (EContact *contact,
168 EContactField field,
169 gpointer value,
170 gpointer user_data)
171 {
172 GBytes *bytes = value;
173
174 ews_populate_cert_data (contact, bytes);
175 }
176
177 static void
178 ews_populate_user_x509_cert (EContact *contact,
179 EContactField field,
180 gpointer value,
181 gpointer user_data)
182 {
183 GSList *link;
184
185 for (link = value; link; link = g_slist_next (link)) {
186 GBytes *bytes = link->data;
187
188 ews_populate_cert_data (contact, bytes);
189 }
190 }
191
192 static void
193 ews_populate_photo (EContact *contact,
194 EContactField field,
195 gpointer value,
196 gpointer user_data)
197 {
198 EwsOabDecoder *eod = EWS_OAB_DECODER (user_data);
199 const gchar *at;
200 GBytes *bytes = value;
201 gchar *email;
202 gchar *filename = NULL, *pic_name = NULL, *name;
203 gboolean success = TRUE;
204 GError *local_error = NULL;
205
206 if (!bytes)
207 return;
208
209 email = e_contact_get (contact, E_CONTACT_EMAIL_1);
210 if (!email || !strchr (email, '@')) {
211 g_free (email);
212 return;
213 }
214
215 /* Rename the binary file to name.jpg */
216 at = strchr (email, '@');
217 name = g_strndup (email, at - email);
218
219 pic_name = g_strconcat (name, ".jpg", NULL);
220 filename = g_build_filename (eod->priv->cache_dir, pic_name, NULL);
221
222 success = g_file_set_contents (filename, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), &local_error);
223
224 if (success) {
225 EContactPhoto photo;
226
227 memset (&photo, 0, sizeof (EContactPhoto));
228 photo.type = E_CONTACT_PHOTO_TYPE_URI;
229 photo.data.uri = filename;
230
231 e_contact_set (contact, field, &photo);
232 } else {
233 g_warning ("%s: Failed to store '%s': %s", G_STRFUNC, filename, local_error ? local_error->message : "Unknown error");
234 }
235
236 g_clear_error (&local_error);
237
238 g_free (email);
239 g_free (name);
240 g_free (pic_name);
241 g_free (filename);
242 }
243
244 /* Make sure that all the temp files are renamed while the fields are getting set in EContact */
245 static const struct prop_field_mapping {
246 guint32 prop_id;
247 EContactField field;
248 void (*populate_function) (EContact *contact, EContactField field, gpointer value, gpointer user_data);
249 void (*defered_populate_function) (EwsDeferredSet *dset, guint32 prop_id, gpointer value);
250 } prop_map[] = {
251 {EWS_PT_EMAIL_ADDRESS, E_CONTACT_UID, ews_populate_string_sha1},
252 {EWS_PT_SMTP_ADDRESS, E_CONTACT_EMAIL_1, ews_populate_simple_string},
253 {EWS_PT_DISPLAY_NAME, E_CONTACT_FULL_NAME, ews_populate_simple_string},
254 {EWS_PT_ACCOUNT, E_CONTACT_NICKNAME, ews_populate_simple_string},
255 {EWS_PT_SURNAME, E_CONTACT_FAMILY_NAME, ews_populate_simple_string},
256 {EWS_PT_GIVEN_NAME, E_CONTACT_GIVEN_NAME, ews_populate_simple_string},
257 {EWS_PT_BUS_TEL_NUMBER, E_CONTACT_PHONE_BUSINESS, ews_populate_simple_string},
258 {EWS_PT_STREET_ADDRESS, E_CONTACT_ADDRESS_WORK, NULL, ews_deffered_populate_physical_address},
259 {EWS_PT_LOCALITY, E_CONTACT_ADDRESS_WORK, NULL, ews_deffered_populate_physical_address},
260 {EWS_PT_STATE_OR_PROVINCE, E_CONTACT_ADDRESS_WORK, NULL, ews_deffered_populate_physical_address},
261 {EWS_PT_POSTAL_CODE, E_CONTACT_ADDRESS_WORK, NULL, ews_deffered_populate_physical_address},
262 {EWS_PT_COUNTRY, E_CONTACT_ADDRESS_WORK, NULL, ews_deffered_populate_physical_address},
263 {EWS_PT_TITLE, E_CONTACT_TITLE, ews_populate_simple_string},
264 {EWS_PT_COMPANY_NAME, E_CONTACT_ORG, ews_populate_simple_string},
265 {EWS_PT_ASSISTANT, E_CONTACT_ASSISTANT, ews_populate_simple_string},
266 {EWS_PT_DEPARTMENT_NAME, E_CONTACT_ORG_UNIT, ews_populate_simple_string},
267 {EWS_PT_HOME_TEL_NUMBER, E_CONTACT_PHONE_HOME, ews_populate_simple_string},
268 {EWS_PT_BUS_TEL_NUMBERS, E_CONTACT_PHONE_BUSINESS, ews_populate_phone_numbers},
269 {EWS_PT_HOME_TEL_NUMBERS, E_CONTACT_PHONE_HOME, ews_populate_phone_numbers},
270 {EWS_PT_PRIMARY_FAX_NUMBER, E_CONTACT_PHONE_BUSINESS_FAX, ews_populate_simple_string},
271 {EWS_PT_MOB_TEL_NUMBER, E_CONTACT_PHONE_MOBILE, ews_populate_simple_string},
272 {EWS_PT_ASSISTANT_TEL_NUMBER, E_CONTACT_PHONE_ASSISTANT, ews_populate_simple_string},
273 {EWS_PT_PAGER_NUMBER, E_CONTACT_PHONE_PAGER, ews_populate_simple_string},
274 {EWS_PT_COMMENT, E_CONTACT_NOTE, ews_populate_simple_string},
275 {EWS_PT_DL_MEMBERS, E_CONTACT_EMAIL, ews_populate_string_list},
276 {EWS_PT_THUMBNAIL_PHOTO, E_CONTACT_PHOTO, ews_populate_photo},
277 {EWS_PT_OFFICE_LOCATION, E_CONTACT_OFFICE, ews_populate_simple_string},
278 {EWS_PT_X509_CERT, E_CONTACT_X509_CERT, ews_populate_cert},
279 {EWS_PT_USER_CERTIFICATE, E_CONTACT_X509_CERT, ews_populate_user_cert},
280 {EWS_PT_USER_X509_CERTIFICATE, E_CONTACT_X509_CERT, ews_populate_user_x509_cert},
281 {EWS_PT_SEND_RICH_INFO, E_CONTACT_WANTS_HTML, ews_populate_simple_string},
282 };
283
284 static void
285 ews_oab_decoder_finalize (GObject *object)
286 {
287 EwsOabDecoder *eod = EWS_OAB_DECODER (object);
288
289 g_clear_pointer (&eod->priv->cache_dir, g_free);
290 g_clear_object (&eod->priv->fis);
291 g_clear_pointer (&eod->priv->prop_index_dict, g_hash_table_destroy);
292 g_clear_pointer (&eod->priv->oab_props, g_slist_free);
293 g_clear_pointer (&eod->priv->hdr_props, g_slist_free);
294
295 G_OBJECT_CLASS (ews_oab_decoder_parent_class)->finalize (object);
296 }
297
298 static void
299 ews_oab_decoder_class_init (EwsOabDecoderClass *klass)
300 {
301 GObjectClass *object_class = G_OBJECT_CLASS (klass);
302
303 object_class->finalize = ews_oab_decoder_finalize;
304 }
305
306 static void
307 ews_oab_decoder_init (EwsOabDecoder *self)
308 {
309 gint i;
310
311 self->priv = ews_oab_decoder_get_instance_private (self);
312
313 self->priv->prop_index_dict = g_hash_table_new (g_direct_hash, g_direct_equal);
314 for (i = 1; i <= G_N_ELEMENTS (prop_map); i++)
315 g_hash_table_insert (self->priv->prop_index_dict, GINT_TO_POINTER (prop_map[i - 1].prop_id), GINT_TO_POINTER (i));
316 }
317
318 EwsOabDecoder *
319 ews_oab_decoder_new (const gchar *oab_filename,
320 const gchar *cache_dir,
321 GError **error)
322 {
323 EwsOabDecoder *eod;
324 GError *err = NULL;
325 GFile *gf = NULL;
326
327 eod = g_object_new (EWS_TYPE_OAB_DECODER, NULL);
328
329 gf = g_file_new_for_path (oab_filename);
330 eod->priv->fis = (GInputStream *) g_file_read (gf, NULL, &err);
331 if (err)
332 goto exit;
333
334 eod->priv->cache_dir = g_strdup (cache_dir);
335
336 exit:
337 g_clear_object (&gf);
338
339 if (err) {
340 g_propagate_error (error, err);
341 g_object_unref (eod);
342 return NULL;
343 }
344
345 return eod;
346 }
347
348 static GQuark
349 ews_oab_decoder_error_quark (void)
350 {
351 static GQuark quark = 0;
352
353 if (G_UNLIKELY (quark == 0)) {
354 const gchar *string = "ews-oab-decoder";
355 quark = g_quark_from_static_string (string);
356 }
357
358 return quark;
359 }
360
361 /* endian-neutral reading of little-endian data */
362 #define __egi32(a,n) ( ((((guchar *) a)[n+3]) << 24) | \
363 ((((guchar *) a)[n + 2]) << 16) | \
364 ((((guchar *) a)[n + 1]) << 8) | \
365 ((((guchar *) a)[n + 0])))
366 #define EndGetI64(a) ((((guint64) __egi32(a,4)) << 32) | \
367 ((guint) __egi32 (a,0)))
368 #define EndGetI32(a) __egi32(a,0)
369 #define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
370
371 static guint32
372 ews_oab_read_uint32 (GInputStream *is,
373 GCancellable *cancellable,
374 GError **error)
375 {
376 gchar *str = g_malloc0 (4);
377 guint32 ret = 0;
378
379 g_input_stream_read (is, str, 4, cancellable, error);
380 if (!*error)
381 ret = EndGetI32 (str);
382
383 g_free (str);
384 return ret;
385 }
386
387 static guint16
388 ews_oab_read_uint16 (GInputStream *is,
389 GCancellable *cancellable,
390 GError **error)
391 {
392 guchar str[2];
393 guint16 ret = 0;
394
395 g_input_stream_read (is, str, 2, cancellable, error);
396 if (!*error)
397 ret = EndGetI16 (str);
398
399 return ret;
400 }
401
402 static gint
403 get_pos (const gchar *str,
404 gsize len,
405 gchar stop)
406 {
407 gsize i = 0;
408
409 while (i < len && str[i] != stop)
410 i++;
411 return i;
412 }
413
414 /* Read upto the stop char include the same */
415 static gchar *
416 ews_oab_read_upto (GInputStream *is,
417 gchar stop,
418 GCancellable *cancellable,
419 GError **error)
420 {
421 gsize size = 50;
422 GString *str;
423
424 str = g_string_sized_new (size);
425 while (1) {
426 gsize len;
427 gsize bytes_read;
428 gchar *c = g_malloc0 (size);
429
430 if (!g_input_stream_read_all (is, c, size, &bytes_read,
431 cancellable, error)) {
432 g_free (c);
433 break;
434 }
435
436 if (bytes_read != size)
437 size = bytes_read;
438
439 len = get_pos (c, size, stop);
440 if (len)
441 g_string_append_len (str, c, len);
442
443 g_free (c);
444
445 if (len == 0 || len < size) {
446 goffset seek = (goffset) len + 1 - (goffset) size;
447
448 /* seek back */
449 g_seekable_seek ((GSeekable *) is, seek, G_SEEK_CUR, cancellable, error);
450 break;
451 }
452
453 size *= 2;
454 }
455
456 return g_string_free (str, FALSE);
457 }
458
459 typedef struct {
460 guint32 version;
461 guint32 serial;
462 guint32 total_recs;
463 } EwsOabHdr;
464
465 static EwsOabHdr *
466 ews_read_oab_header (EwsOabDecoder *eod, GInputStream *stream,
467 GCancellable *cancellable,
468 GError **error)
469 {
470 EwsOabHdr *o_hdr;
471
472 o_hdr = g_new0 (EwsOabHdr, 1);
473
474 o_hdr->version = ews_oab_read_uint32 (stream, cancellable, error);
475 if (*error)
476 goto exit;
477
478 if (o_hdr->version != 0x00000020) {
479 g_set_error_literal (error, EOD_ERROR, 1, "wrong version header");
480 goto exit;
481 }
482
483 o_hdr->serial = ews_oab_read_uint32 (stream, cancellable, error);
484 if (*error)
485 goto exit;
486 o_hdr->total_recs = ews_oab_read_uint32 (stream, cancellable, error);
487
488 exit:
489 if (*error) {
490 g_free (o_hdr);
491 return NULL;
492 }
493
494 return o_hdr;
495 }
496
497 static gboolean
498 ews_decode_hdr_props (EwsOabDecoder *eod,
499 GInputStream *stream,
500 gboolean oab_hdrs,
501 GCancellable *cancellable,
502 GError **error)
503 {
504 guint32 num_props, i;
505 GSList **props;
506
507 /* number of properties */
508 num_props = ews_oab_read_uint32 (stream, cancellable, error);
509
510 if (*error)
511 return FALSE;
512
513 if (oab_hdrs)
514 props = &eod->priv->oab_props;
515 else
516 props = &eod->priv->hdr_props;
517
518 if (*props) {
519 g_slist_free (*props);
520 *props = NULL;
521 }
522
523 for (i = 0; i < num_props; i++) {
524 guint32 prop_id;
525
526 prop_id = ews_oab_read_uint32 (stream, cancellable, error);
527
528 *props = g_slist_prepend (*props, GUINT_TO_POINTER (prop_id));
529
530 if (*error)
531 return FALSE;
532
533 /* eat the flags */
534 ews_oab_read_uint32 (stream, cancellable, error);
535
536 if (*error)
537 return FALSE;
538
539 /* skip store anr_index and primary key prop list as we will not be using it for online search,
540 * store if required later */
541 }
542
543 *props = g_slist_reverse (*props);
544
545 return TRUE;
546 }
547
548 static gboolean
549 ews_decode_metadata (EwsOabDecoder *eod, GInputStream *stream,
550 GCancellable *cancellable,
551 GError **error)
552 {
553 gboolean ret = TRUE;
554
555 /* eat the size */
556 ews_oab_read_uint32 (stream, cancellable, error);
557
558 if (*error)
559 return FALSE;
560
561 ret = ews_decode_hdr_props (eod, stream, FALSE, cancellable, error);
562 if (!ret)
563 return FALSE;
564
565 ret = ews_decode_hdr_props (eod, stream, TRUE, cancellable, error);
566
567 return ret;
568 }
569
570 static gboolean
571 ews_is_bit_set (const gchar *str,
572 guint32 pos)
573 {
574 guint32 index, bit_pos;
575
576 index = pos / 8;
577 bit_pos = pos & (8 - 1);
578
579 if ((str[index] << bit_pos) & 0x80)
580 return TRUE;
581 else
582 return FALSE;
583 }
584
585 static guint32
586 ews_decode_uint32 (EwsOabDecoder *eod, GInputStream *stream,
587 GCancellable *cancellable,
588 GError **error)
589 {
590 guint8 first;
591 guint32 ret = 0, num;
592
593 g_input_stream_read (stream, &first, 1, cancellable, error);
594 if (*error)
595 return ret;
596
597 if (first & 0x80)
598 num = first & 0x0F;
599 else
600 return (guint32) first;
601
602 if (num == 1) {
603 g_input_stream_read (stream, &first, 1, cancellable, error);
604 return (guint32) first;
605 }
606
607 if (num == 2)
608 ret = ews_oab_read_uint16 (stream, cancellable, error);
609 if (num == 3) {
610 gchar *tmp, *str = g_malloc0 (num + 1);
611
612 g_input_stream_read (stream, str, num, cancellable, error);
613 /* not sure if its the right way to do, test it */
614 tmp = g_strconcat ("0", str, NULL);
615
616 sscanf (tmp, "%"G_GUINT32_FORMAT, &ret);
617 ret = GUINT32_SWAP_LE_BE (ret);
618
619 g_free (str);
620 g_free (tmp);
621 } else if (num == 4)
622 ret = ews_oab_read_uint32 (stream, cancellable, error);
623
624 return ret;
625 }
626
627 static GBytes *
628 ews_decode_binary (EwsOabDecoder *eod, GInputStream *stream,
629 GCancellable *cancellable,
630 GError **error)
631 {
632 guint32 len;
633 gchar *binary;
634 GBytes *val = NULL;
635
636 len = ews_decode_uint32 (eod, stream, cancellable, error);
637 if (*error)
638 return NULL;
639 binary = g_malloc (len);
640 g_input_stream_read (stream, binary, len, cancellable, error);
641 if (*error) {
642 g_free (binary);
643 goto exit;
644 }
645
646 val = g_bytes_new_take (binary, len);
647 binary = NULL;
648
649 exit:
650 return val;
651 }
652
653 static gpointer
654 ews_decode_oab_prop (EwsOabDecoder *eod, GInputStream *stream,
655 guint32 prop_id,
656 GCancellable *cancellable,
657 GError **error)
658 {
659 guint32 prop_type;
660 gpointer ret_val = NULL;
661
662 prop_type = prop_id & 0x0000FFFF;
663
664 switch (prop_type) {
665 case EWS_PTYP_INTEGER32:
666 {
667 guint32 val;
668
669 val = ews_decode_uint32 (eod, stream, cancellable, error);
670 ret_val = GUINT_TO_POINTER (val);
671
672 d (g_print ("prop id %X prop type: int32 value %d \n", prop_id, val);)
673
674 break;
675 }
676 case EWS_PTYP_BOOLEAN:
677 {
678 guchar val;
679
680 g_input_stream_read (stream, &val, 1, cancellable, error);
681 ret_val = GUINT_TO_POINTER ((guint) val);
682 d (g_print ("prop id %X prop type: bool value %d \n", prop_id, val);)
683
684 break;
685 }
686 case EWS_PTYP_STRING8:
687 case EWS_PTYP_STRING:
688 {
689 gchar *val;
690
691 val = ews_oab_read_upto (stream, '\0', cancellable, error);
692 ret_val = (gpointer) val;
693
694 d (g_print ("prop id %X prop type: string value %s \n", prop_id, val);)
695 break;
696 }
697 case EWS_PTYP_BINARY:
698 {
699 ret_val = ews_decode_binary (eod, stream, cancellable, error);
700 d (g_print ("prop id %X prop type: binary size %zd \n", prop_id, g_bytes_get_size ((GBytes *)ret_val)));
701 break;
702 }
703 case EWS_PTYP_MULTIPLEINTEGER32:
704 case EWS_PTYP_MULTIPLESTRING8:
705 case EWS_PTYP_MULTIPLESTRING:
706 case EWS_PTYP_MULTIPLEBINARY:
707 {
708 guint32 num, i;
709 GSList *list = NULL;
710
711 num = ews_decode_uint32 (eod, stream, cancellable, error);
712 if (*error)
713 break;
714 d (g_print ("prop id %X prop type: multi-num %d \n", prop_id, num);)
715
716 for (i = 0; i < num; i++) {
717 if (prop_type == EWS_PTYP_MULTIPLEINTEGER32) {
718 gpointer val;
719 guint32 v = 0;
720
721 v = ews_decode_uint32 (eod, stream, cancellable, error);
722 val = GUINT_TO_POINTER (v);
723 list = g_slist_prepend (list, val);
724
725 d (g_print ("prop id %X prop type: multi-int32 %d \n", prop_id, v);)
726 if (*error) {
727 g_slist_free (list);
728 return NULL;
729 }
730 } else if (prop_type == EWS_PTYP_MULTIPLEBINARY) {
731 GBytes *val;
732
733 val = ews_decode_binary (eod, stream, cancellable, error);
734 if (!val) {
735 g_slist_foreach (list, (GFunc) g_bytes_unref, NULL);
736 g_slist_free (list);
737 return NULL;
738 }
739
740 d (g_print ("prop id %X prop type: multi-bin size %zd\n", prop_id, g_bytes_get_size (val)));
741
742 list = g_slist_prepend (list, val);
743 } else {
744 gchar *val;
745
746 val = ews_oab_read_upto (stream, '\0', cancellable, error);
747 if (!val) {
748 g_slist_foreach (list, (GFunc) g_free, NULL);
749 g_slist_free (list);
750 return NULL;
751 }
752
753 d (g_print ("prop id %X prop type: multi-str '%s'\n", prop_id, val));
754 list = g_slist_prepend (list, val);
755 }
756
757 }
758 ret_val = list;
759
760 break;
761 }
762 default:
763 g_error ("%s: Cannot decode property 0x%x", G_STRFUNC, prop_id);
764 break;
765 }
766
767 return ret_val;
768 }
769
770 static void
771 ews_destroy_oab_prop (guint32 prop_id, gpointer val)
772 {
773 guint32 prop_type;
774
775 prop_type = prop_id & 0x0000FFFF;
776
777 switch (prop_type) {
778 case EWS_PTYP_INTEGER32:
779 case EWS_PTYP_BOOLEAN:
780 break;
781 case EWS_PTYP_BINARY:
782 g_bytes_unref (val);
783 break;
784 case EWS_PTYP_STRING8:
785 case EWS_PTYP_STRING:
786 g_free ((gchar *) val);
787 break;
788 case EWS_PTYP_MULTIPLEBINARY:
789 g_slist_free_full ((GSList *) val, (GDestroyNotify) g_bytes_unref);
790 break;
791 case EWS_PTYP_MULTIPLESTRING8:
792 case EWS_PTYP_MULTIPLESTRING:
793 g_slist_free_full ((GSList *) val, g_free);
794 break;
795 case EWS_PTYP_MULTIPLEINTEGER32:
796 g_slist_free ((GSList *) val);
797 break;
798 default:
799 g_warn_if_reached ();
800 break;
801 }
802 }
803
804 static const gchar *
805 ews_decode_addressbook_get_display_type (guint32 value)
806 {
807 const gchar *display_type = NULL;
808
809 switch (value) {
810 case EWS_DT_MAILUSER:
811 /*
812 * DT_MAILUSER means that the display type could be, besides itself, one of
813 * these extended types: DT_ROOM, DT_EQUIPMENT, DT_SEC_DISTLIST. Considering
814 * this, we can just ignore this case here and treat it properly in the
815 * ews_decode_addressbook_get_display_type_extended() function.
816 */
817 break;
818 case EWS_DT_DISTLIST:
819 display_type = "DT_DISTLIST";
820 break;
821 case EWS_DT_FORUM:
822 display_type = "DT_FORUM";
823 break;
824 case EWS_DT_AGENT:
825 display_type = "DT_AGENT";
826 break;
827 case EWS_DT_ORGANIZATION:
828 display_type = "DT_ORGANIZATION";
829 break;
830 case EWS_DT_PRIVATE_DISTLIST:
831 display_type = "DT_PRIVATE_DISTLIST";
832 break;
833 case EWS_DT_REMOTE_MAILUSER:
834 display_type = "DT_PRIVATE_MAILUSER";
835 break;
836 }
837
838 return display_type;
839 }
840
841 static const gchar *
842 ews_decode_addressbook_get_display_type_extended (guint32 value)
843 {
844 const gchar *display_type = "DT_MAILUSER";
845
846 switch (value) {
847 case EWS_DT_ROOM:
848 display_type = "DT_ROOM";
849 break;
850 case EWS_DT_EQUIPMENT:
851 display_type = "DT_EQUIPMENT";
852 break;
853 case EWS_DT_SEC_DISTLIST:
854 display_type = "DT_SEC_DISTLIST";
855 break;
856 }
857
858 return display_type;
859 }
860
861 static void
862 ews_decode_addressbook_write_display_type (EContact **contact,
863 guint32 value,
864 gboolean extended)
865 {
866 EVCardAttribute *attr;
867 const gchar *display_type;
868
869 if (extended)
870 display_type = ews_decode_addressbook_get_display_type_extended (value);
871 else
872 display_type = ews_decode_addressbook_get_display_type (value);
873
874 if (display_type != NULL) {
875 attr = e_vcard_attribute_new (NULL, "X-EWS-KIND");
876 e_vcard_add_attribute_with_value (E_VCARD (*contact), attr, display_type);
877 }
878 }
879
880 /**
881 * ews_decode_addressbook_record
882 * @eod:
883 * @contact: Pass a valid EContact for decoding the address-book record. NULL in case of header record.
884 * @props:
885 * @cancellable:
886 * @error:
887 *
888 * Decodes the address-book records starting from presence bit array.
889 *
890 *
891 * Returns:
892 **/
893 static gboolean
894 ews_decode_addressbook_record (EwsOabDecoder *eod,
895 GInputStream *stream,
896 EContact *contact,
897 GSList *props,
898 GCancellable *cancellable,
899 GError **error)
900 {
901 EwsDeferredSet *dset = NULL;
902 guint bit_array_size, i, len;
903 gchar *bit_str;
904 gboolean ret = TRUE;
905
906 len = g_slist_length (props);
907 bit_array_size = (guint) ceil (len / 8.0);
908 bit_str = g_malloc0 (bit_array_size);
909 g_input_stream_read (stream, bit_str, bit_array_size, cancellable, error);
910 if (*error) {
911 ret = FALSE;
912 goto exit;
913 }
914
915 if (contact)
916 dset = g_new0 (EwsDeferredSet, 1);
917
918 for (i = 0; i < len; i++) {
919 gpointer val, index;
920 guint32 prop_id;
921
922 if (!ews_is_bit_set (bit_str, i))
923 continue;
924
925 val = g_slist_nth_data (props, i);
926 prop_id = GPOINTER_TO_UINT (val);
927
928 /* these are not encoded in the OAB, according to
929 http://msdn.microsoft.com/en-us/library/gg671985%28v=EXCHG.80%29.aspx
930 */
931 if ((prop_id & 0xFFFF) == EWS_PTYP_OBJECT)
932 continue;
933
934 val = ews_decode_oab_prop (eod, stream, prop_id, cancellable, error);
935
936 if (prop_id == EWS_PT_DISPLAY_TYPE)
937 ews_decode_addressbook_write_display_type (&contact, GPOINTER_TO_UINT (val), FALSE);
938
939 if (prop_id == EWS_PT_DISPLAY_TYPE_EX)
940 ews_decode_addressbook_write_display_type (&contact, GPOINTER_TO_UINT (val), TRUE);
941
942 /* Check the contact map and store the data in EContact */
943 index = g_hash_table_lookup (eod->priv->prop_index_dict, GINT_TO_POINTER (prop_id));
944 if (contact && index) {
945 gint idx = GPOINTER_TO_INT (index);
946
947 if (prop_map[idx - 1].populate_function)
948 prop_map[idx - 1].populate_function (contact, prop_map[idx - 1].field, val, (gpointer) eod);
949 else
950 prop_map[idx - 1].defered_populate_function (dset, prop_id, val);
951 }
952 ews_destroy_oab_prop (prop_id, val);
953 if (*error)
954 goto exit;
955 }
956
957 exit:
958 if (bit_str)
959 g_free (bit_str);
960
961 if (!contact)
962 return ret;
963
964 if (dset && dset->addr) {
965 e_contact_set (contact, E_CONTACT_ADDRESS_WORK, dset->addr);
966 e_contact_address_free (dset->addr);
967 }
968 g_free (dset);
969
970 /* set the smtp address as contact's uid */
971 if (!e_contact_get_const(contact, E_CONTACT_UID)) {
972 const gchar *uid = e_contact_get_const (contact, E_CONTACT_EMAIL_1);
973 if (uid && *uid)
974 e_contact_set (contact, E_CONTACT_UID, uid);
975 else
976 ret = FALSE;
977 }
978
979 return ret;
980 }
981
982 /* Decodes the hdr and address-book records and stores the address-book records inside the db */
983 static gboolean
984 ews_decode_and_store_oab_records (EwsOabDecoder *eod,
985 EwsOabContactFilterCb filter_cb,
986 EwsOabContactAddedCb cb,
987 gpointer user_data,
988 GCancellable *cancellable,
989 GError **error)
990 {
991 gboolean ret = FALSE;
992 guint32 i;
993 int buf_len = 200;
994 guchar *record_buf = g_malloc (buf_len);
995 GChecksum *sum = g_checksum_new (G_CHECKSUM_SHA1);
996
997 if (!record_buf || !sum)
998 goto exit;
999
1000 /* eat the size */
1001 ews_oab_read_uint32 (
1002 eod->priv->fis,
1003 cancellable, error);
1004
1005 ews_decode_addressbook_record (eod, eod->priv->fis, NULL,
1006 eod->priv->hdr_props, cancellable, error);
1007
1008 if (*error)
1009 goto exit;
1010
1011
1012 for (i = 0; i < eod->priv->total_records; i++) {
1013 EContact *contact;
1014 goffset offset;
1015 guint32 rec_size;
1016 GInputStream *memstream;
1017 const gchar *sum_str;
1018
1019 contact = e_contact_new ();
1020
1021 /* eat the size */
1022 rec_size = ews_oab_read_uint32 (eod->priv->fis, cancellable, error);
1023 if (rec_size < 4)
1024 goto exit;
1025
1026 rec_size -= 4;
1027
1028 if (rec_size > buf_len) {
1029 g_free (record_buf);
1030 record_buf = g_malloc(rec_size);
1031 buf_len = rec_size;
1032 if (!record_buf)
1033 goto exit;
1034 }
1035 /* fetch the offset */
1036 offset = g_seekable_tell ((GSeekable *) eod->priv->fis);
1037 if (g_input_stream_read (eod->priv->fis, record_buf, rec_size, cancellable, error) != rec_size)
1038 goto exit;
1039
1040 g_checksum_reset (sum);
1041 g_checksum_update (sum, record_buf, rec_size);
1042 sum_str = g_checksum_get_string (sum);
1043
1044 memstream = g_memory_input_stream_new_from_data (record_buf, rec_size, NULL);
1045
1046 if ((!filter_cb || filter_cb (offset, sum_str, user_data, error)) &&
1047 ews_decode_addressbook_record (eod, memstream,
1048 contact, eod->priv->oab_props,
1049 cancellable, error))
1050 cb (contact, offset, sum_str,
1051 ((gfloat) (i + 1) / eod->priv->total_records) * 100,
1052 user_data, cancellable, error);
1053
1054 g_object_unref (memstream);
1055 g_object_unref (contact);
1056
1057 if (*error)
1058 goto exit;
1059 }
1060
1061 ret = TRUE;
1062 exit:
1063 g_checksum_free (sum);
1064 g_free (record_buf);
1065 return ret;
1066 }
1067
1068 gchar *
1069 ews_oab_decoder_get_oab_prop_string (EwsOabDecoder *eod,
1070 GError **error)
1071 {
1072 GString *str;
1073 GSList *l;
1074
1075 if (!eod->priv->oab_props) {
1076 g_set_error_literal (
1077 error, EOD_ERROR, 1,
1078 "Oab props not found");
1079 return NULL;
1080 }
1081
1082 str = g_string_new (NULL);
1083
1084 /* Ideally i would liked to store int as int instead of converting to
1085 * string, but sqlite db doesn't yet support storing keys as blob. */
1086 for (l = eod->priv->oab_props; l != NULL; l = g_slist_next (l)) {
1087 guint32 prop_id = GPOINTER_TO_UINT (l->data);
1088 g_string_append_printf (str, "%"G_GUINT32_FORMAT, prop_id);
1089 g_string_append_c (str, ';');
1090 }
1091 if (str->len)
1092 g_string_erase (str, str->len - 1, 1);
1093
1094 d (g_print ("Oab prop string: %s \n", str->str);)
1095
1096 return g_string_free (str, FALSE);
1097 }
1098
1099 gboolean
1100 ews_oab_decoder_set_oab_prop_string (EwsOabDecoder *eod,
1101 const gchar *prop_str,
1102 GError **error)
1103 {
1104 gchar **vals;
1105 guint32 len, i;
1106
1107 vals = g_strsplit (prop_str, ";", -1);
1108 len = g_strv_length (vals);
1109
1110 /* hmm is there a better way to check ? */
1111 if (len < 2) {
1112 g_set_error_literal (
1113 error, EOD_ERROR, 1,
1114 "Does not contain oab properties");
1115 return FALSE;
1116 }
1117
1118 g_clear_pointer (&eod->priv->oab_props, g_slist_free);
1119
1120 for (i = 0; i < len; i++) {
1121 guint32 prop_id;
1122
1123 sscanf (vals[i],"%"G_GUINT32_FORMAT,&prop_id);
1124 eod->priv->oab_props = g_slist_prepend (
1125 eod->priv->oab_props, GUINT_TO_POINTER (prop_id));
1126 d (printf ("%X\n", prop_id);)
1127 }
1128
1129 eod->priv->oab_props = g_slist_reverse (eod->priv->oab_props);
1130
1131 g_strfreev (vals);
1132
1133 return TRUE;
1134 }
1135
1136 /**
1137 * ews_oab_decoder_decode
1138 * @eod:
1139 * @cancellable:
1140 * @error:
1141 *
1142 * Decodes the oab full details verions 4 file and stores
1143 * the properties in the sqlite db.
1144 * Returns: TRUE if successfully decoded and indexed in db
1145 **/
1146 gboolean
1147 ews_oab_decoder_decode (EwsOabDecoder *eod,
1148 EwsOabContactFilterCb filter_cb,
1149 EwsOabContactAddedCb cb,
1150 gpointer user_data,
1151 GCancellable *cancellable,
1152 GError **error)
1153 {
1154 GError *err = NULL;
1155 EwsOabHdr *o_hdr;
1156 gboolean ret = TRUE;
1157
1158 o_hdr = ews_read_oab_header (eod, eod->priv->fis, cancellable, &err);
1159 if (!o_hdr) {
1160 ret = FALSE;
1161 goto exit;
1162 }
1163
1164 eod->priv->total_records = o_hdr->total_recs;
1165 d (g_print ("Total records is %d \n", eod->priv->total_records));
1166
1167 ret = ews_decode_metadata (eod, eod->priv->fis, cancellable, &err);
1168 if (!ret)
1169 goto exit;
1170
1171 ret = ews_decode_and_store_oab_records (
1172 eod, filter_cb, cb, user_data, cancellable, &err);
1173 exit:
1174 if (o_hdr)
1175 g_free (o_hdr);
1176
1177 if (err)
1178 g_propagate_error (error, err);
1179
1180 return ret;
1181 }
1182
1183 EContact *
1184 ews_oab_decoder_get_contact_from_offset (EwsOabDecoder *eod,
1185 goffset offset,
1186 GSList *oab_props,
1187 GCancellable *cancellable,
1188 GError **error)
1189 {
1190 EContact *contact = NULL;
1191
1192 if (!g_seekable_seek ((GSeekable *) eod->priv->fis, offset, G_SEEK_SET, cancellable, error))
1193 return NULL;
1194
1195 contact = e_contact_new ();
1196 if (!ews_decode_addressbook_record (eod, eod->priv->fis,
1197 contact, oab_props, cancellable,
1198 error)) {
1199 g_object_unref (contact);
1200 contact = NULL;
1201 }
1202
1203 return contact;
1204 }
1205