Initialize the gmime for upstream
[platform/upstream/gmime.git] / gmime / gmime-signature.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2012 Jeffrey Stedfast
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public License
7  *  as published by the Free Software Foundation; either version 2.1
8  *  of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free
17  *  Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
18  *  02110-1301, USA.
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27
28 #include "gmime-signature.h"
29
30
31 /**
32  * SECTION: gmime-signature
33  * @title: GMimeSignature
34  * @short_description: Digital signatures
35  * @see_also:
36  *
37  * A #GMimeSignature is an object containing useful information about a
38  * digital signature as used in signing and encrypting data.
39  **/
40
41
42 static void g_mime_signature_class_init (GMimeSignatureClass *klass);
43 static void g_mime_signature_init (GMimeSignature *sig, GMimeSignatureClass *klass);
44 static void g_mime_signature_finalize (GObject *object);
45
46 static GObjectClass *parent_class = NULL;
47
48
49 GType
50 g_mime_signature_get_type (void)
51 {
52         static GType type = 0;
53         
54         if (!type) {
55                 static const GTypeInfo info = {
56                         sizeof (GMimeSignatureClass),
57                         NULL, /* base_class_init */
58                         NULL, /* base_class_finalize */
59                         (GClassInitFunc) g_mime_signature_class_init,
60                         NULL, /* class_finalize */
61                         NULL, /* class_data */
62                         sizeof (GMimeSignature),
63                         0,    /* n_preallocs */
64                         (GInstanceInitFunc) g_mime_signature_init,
65                 };
66                 
67                 type = g_type_register_static (G_TYPE_OBJECT, "GMimeSignature", &info, 0);
68         }
69         
70         return type;
71 }
72
73 static void
74 g_mime_signature_class_init (GMimeSignatureClass *klass)
75 {
76         GObjectClass *object_class = G_OBJECT_CLASS (klass);
77         
78         parent_class = g_type_class_ref (G_TYPE_OBJECT);
79         
80         object_class->finalize = g_mime_signature_finalize;
81 }
82
83 static void
84 g_mime_signature_init (GMimeSignature *sig, GMimeSignatureClass *klass)
85 {
86         sig->status = GMIME_SIGNATURE_STATUS_GOOD;
87         sig->errors = GMIME_SIGNATURE_ERROR_NONE;
88         sig->cert = g_mime_certificate_new ();
89         sig->created = (time_t) -1;
90         sig->expires = (time_t) -1;
91 }
92
93 static void
94 g_mime_signature_finalize (GObject *object)
95 {
96         GMimeSignature *sig = (GMimeSignature *) object;
97         
98         if (sig->cert)
99                 g_object_unref (sig->cert);
100         
101         G_OBJECT_CLASS (parent_class)->finalize (object);
102 }
103
104
105 /**
106  * g_mime_signature_new:
107  *
108  * Creates a new #GMimeSignature object.
109  * 
110  * Returns: a new #GMimeSignature object.
111  **/
112 GMimeSignature *
113 g_mime_signature_new (void)
114 {
115         return g_object_newv (GMIME_TYPE_SIGNATURE, 0, NULL);
116 }
117
118
119 /**
120  * g_mime_signature_set_status:
121  * @sig: a #GMimeSignature
122  * @status: a #GMimeSignatureStatus
123  *
124  * Set the status on the signature.
125  **/
126 void
127 g_mime_signature_set_status (GMimeSignature *sig, GMimeSignatureStatus status)
128 {
129         g_return_if_fail (GMIME_IS_SIGNATURE (sig));
130         
131         sig->status = status;
132 }
133
134
135 /**
136  * g_mime_signature_get_status:
137  * @sig: a #GMimeSignature
138  *
139  * Get the signature status.
140  *
141  * Returns: the signature status.
142  **/
143 GMimeSignatureStatus
144 g_mime_signature_get_status (GMimeSignature *sig)
145 {
146         g_return_val_if_fail (GMIME_IS_SIGNATURE (sig), GMIME_SIGNATURE_STATUS_BAD);
147         
148         return sig->status;
149 }
150
151
152 /**
153  * g_mime_signature_set_errors:
154  * @sig: a #GMimeSignature
155  * @errors: a #GMimeSignatureError
156  *
157  * Set the errors on the signature.
158  **/
159 void
160 g_mime_signature_set_errors (GMimeSignature *sig, GMimeSignatureError errors)
161 {
162         g_return_if_fail (GMIME_IS_SIGNATURE (sig));
163         
164         sig->errors = errors;
165 }
166
167
168 /**
169  * g_mime_signature_get_errors:
170  * @sig: a #GMimeSignature
171  *
172  * Get the signature errors. If the #GMimeSignatureStatus returned from
173  * g_mime_signature_get_status() is not #GMIME_SIGNATURE_STATUS_GOOD, then the
174  * errors may provide a clue as to why.
175  *
176  * Returns: a bitfield of errors.
177  **/
178 GMimeSignatureError
179 g_mime_signature_get_errors (GMimeSignature *sig)
180 {
181         g_return_val_if_fail (GMIME_IS_SIGNATURE (sig), GMIME_SIGNATURE_ERROR_NONE);
182         
183         return sig->errors;
184 }
185
186
187 /**
188  * g_mime_signature_set_certificate:
189  * @sig: a #GMimeSignature
190  * @cert: a #GMimeCertificate
191  *
192  * Set the signature's certificate.
193  **/
194 void
195 g_mime_signature_set_certificate (GMimeSignature *sig, GMimeCertificate *cert)
196 {
197         g_return_if_fail (GMIME_IS_SIGNATURE (sig));
198         g_return_if_fail (GMIME_IS_CERTIFICATE (cert));
199         
200         if (sig->cert == cert)
201                 return;
202         
203         if (sig->cert != NULL)
204                 g_object_unref (sig->cert);
205         
206         if (cert != NULL)
207                 g_object_ref (cert);
208         
209         sig->cert = cert;
210 }
211
212
213 /**
214  * g_mime_signature_get_certificate:
215  * @sig: a #GMimeSignature
216  *
217  * Get the signature's certificate.
218  *
219  * Returns: the signature's certificate.
220  **/
221 GMimeCertificate *
222 g_mime_signature_get_certificate (GMimeSignature *sig)
223 {
224         g_return_val_if_fail (GMIME_IS_SIGNATURE (sig), NULL);
225         
226         return sig->cert;
227 }
228
229
230 /**
231  * g_mime_signature_set_created:
232  * @sig: a #GMimeSignature
233  * @created: creation date
234  *
235  * Set the creation date of the signature.
236  **/
237 void
238 g_mime_signature_set_created (GMimeSignature *sig, time_t created)
239 {
240         g_return_if_fail (GMIME_IS_SIGNATURE (sig));
241         
242         sig->created = created;
243 }
244
245
246 /**
247  * g_mime_signature_get_created:
248  * @sig: a #GMimeSignature
249  *
250  * Get the creation date of the signature.
251  *
252  * Returns: the creation date of the signature or %-1 if unknown.
253  **/
254 time_t
255 g_mime_signature_get_created (GMimeSignature *sig)
256 {
257         g_return_val_if_fail (GMIME_IS_SIGNATURE (sig), (time_t) -1);
258         
259         return sig->created;
260 }
261
262
263 /**
264  * g_mime_signature_set_expires:
265  * @sig: a #GMimeSignature
266  * @expires: expiration date
267  *
268  * Set the expiration date of the signature.
269  **/
270 void
271 g_mime_signature_set_expires (GMimeSignature *sig, time_t expires)
272 {
273         g_return_if_fail (GMIME_IS_SIGNATURE (sig));
274         
275         sig->expires = expires;
276 }
277
278
279 /**
280  * g_mime_signature_get_expires:
281  * @sig: a #GMimeSignature
282  *
283  * Get the expiration date of the signature.
284  *
285  * Returns: the expiration date of the signature or %-1 if unknown.
286  **/
287 time_t
288 g_mime_signature_get_expires (GMimeSignature *sig)
289 {
290         g_return_val_if_fail (GMIME_IS_SIGNATURE (sig), (time_t) -1);
291         
292         return sig->expires;
293 }
294
295
296 static void g_mime_signature_list_class_init (GMimeSignatureListClass *klass);
297 static void g_mime_signature_list_init (GMimeSignatureList *list, GMimeSignatureListClass *klass);
298 static void g_mime_signature_list_finalize (GObject *object);
299
300
301 static GObjectClass *list_parent_class = NULL;
302
303
304 GType
305 g_mime_signature_list_get_type (void)
306 {
307         static GType type = 0;
308         
309         if (!type) {
310                 static const GTypeInfo info = {
311                         sizeof (GMimeSignatureListClass),
312                         NULL, /* base_class_init */
313                         NULL, /* base_class_finalize */
314                         (GClassInitFunc) g_mime_signature_list_class_init,
315                         NULL, /* class_finalize */
316                         NULL, /* class_data */
317                         sizeof (GMimeSignatureList),
318                         0,    /* n_preallocs */
319                         (GInstanceInitFunc) g_mime_signature_list_init,
320                 };
321                 
322                 type = g_type_register_static (G_TYPE_OBJECT, "GMimeSignatureList", &info, 0);
323         }
324         
325         return type;
326 }
327
328
329 static void
330 g_mime_signature_list_class_init (GMimeSignatureListClass *klass)
331 {
332         GObjectClass *object_class = G_OBJECT_CLASS (klass);
333         
334         list_parent_class = g_type_class_ref (G_TYPE_OBJECT);
335         
336         object_class->finalize = g_mime_signature_list_finalize;
337 }
338
339 static void
340 g_mime_signature_list_init (GMimeSignatureList *list, GMimeSignatureListClass *klass)
341 {
342         list->array = g_ptr_array_new ();
343 }
344
345 static void
346 g_mime_signature_list_finalize (GObject *object)
347 {
348         GMimeSignatureList *list = (GMimeSignatureList *) object;
349         GMimeSignature *sig;
350         guint i;
351         
352         for (i = 0; i < list->array->len; i++) {
353                 sig = (GMimeSignature *) list->array->pdata[i];
354                 g_object_unref (sig);
355         }
356         
357         g_ptr_array_free (list->array, TRUE);
358         
359         G_OBJECT_CLASS (list_parent_class)->finalize (object);
360 }
361
362
363 /**
364  * g_mime_signature_list_new:
365  *
366  * Creates a new #GMimeSignatureList.
367  *
368  * Returns: a new #GMimeSignatureList.
369  **/
370 GMimeSignatureList *
371 g_mime_signature_list_new (void)
372 {
373         return g_object_newv (GMIME_TYPE_SIGNATURE_LIST, 0, NULL);
374 }
375
376
377 /**
378  * g_mime_signature_list_length:
379  * @list: a #GMimeSignatureList
380  *
381  * Gets the length of the list.
382  *
383  * Returns: the number of #GMimeSignature objects in the list.
384  **/
385 int
386 g_mime_signature_list_length (GMimeSignatureList *list)
387 {
388         g_return_val_if_fail (GMIME_IS_SIGNATURE_LIST (list), -1);
389         
390         return list->array->len;
391 }
392
393
394 /**
395  * g_mime_signature_list_clear:
396  * @list: a #GMimeSignatureList
397  *
398  * Clears the list of addresses.
399  **/
400 void
401 g_mime_signature_list_clear (GMimeSignatureList *list)
402 {
403         GMimeSignature *sig;
404         guint i;
405         
406         g_return_if_fail (GMIME_IS_SIGNATURE_LIST (list));
407         
408         for (i = 0; i < list->array->len; i++) {
409                 sig = (GMimeSignature *) list->array->pdata[i];
410                 g_object_unref (sig);
411         }
412         
413         g_ptr_array_set_size (list->array, 0);
414 }
415
416
417 /**
418  * g_mime_signature_list_add:
419  * @list: a #GMimeSignatureList
420  * @sig: a #GMimeSignature
421  *
422  * Adds a #GMimeSignature to the #GMimeSignatureList.
423  *
424  * Returns: the index of the added #GMimeSignature.
425  **/
426 int
427 g_mime_signature_list_add (GMimeSignatureList *list, GMimeSignature *sig)
428 {
429         int index;
430         
431         g_return_val_if_fail (GMIME_IS_SIGNATURE_LIST (list), -1);
432         g_return_val_if_fail (GMIME_IS_SIGNATURE (sig), -1);
433         
434         index = list->array->len;
435         g_ptr_array_add (list->array, sig);
436         g_object_ref (sig);
437         
438         return index;
439 }
440
441
442 /**
443  * g_mime_signature_list_insert:
444  * @list: a #GMimeSignatureList
445  * @index: index to insert at
446  * @sig: a #GMimeSignature
447  *
448  * Inserts a #GMimeSignature into the #GMimeSignatureList at the specified
449  * index.
450  **/
451 void
452 g_mime_signature_list_insert (GMimeSignatureList *list, int index, GMimeSignature *sig)
453 {
454         char *dest, *src;
455         size_t n;
456         
457         g_return_if_fail (GMIME_IS_SIGNATURE_LIST (list));
458         g_return_if_fail (GMIME_IS_SIGNATURE (sig));
459         g_return_if_fail (index >= 0);
460         
461         if ((guint) index < list->array->len) {
462                 g_ptr_array_set_size (list->array, list->array->len + 1);
463                 
464                 dest = ((char *) list->array->pdata) + (sizeof (void *) * (index + 1));
465                 src = ((char *) list->array->pdata) + (sizeof (void *) * index);
466                 n = list->array->len - index - 1;
467                 
468                 g_memmove (dest, src, (sizeof (void *) * n));
469                 list->array->pdata[index] = sig;
470         } else {
471                 /* the easy case */
472                 g_ptr_array_add (list->array, sig);
473         }
474         
475         g_object_ref (sig);
476 }
477
478
479 /**
480  * g_mime_signature_list_remove:
481  * @list: a #GMimeSignatureList
482  * @sig: a #GMimeSignature
483  *
484  * Removes a #GMimeSignature from the #GMimeSignatureList.
485  *
486  * Returns: %TRUE if the specified #GMimeSignature was removed or %FALSE
487  * otherwise.
488  **/
489 gboolean
490 g_mime_signature_list_remove (GMimeSignatureList *list, GMimeSignature *sig)
491 {
492         int index;
493         
494         g_return_val_if_fail (GMIME_IS_SIGNATURE_LIST (list), FALSE);
495         g_return_val_if_fail (GMIME_IS_SIGNATURE (sig), FALSE);
496         
497         if ((index = g_mime_signature_list_index_of (list, sig)) == -1)
498                 return FALSE;
499         
500         g_mime_signature_list_remove_at (list, index);
501         
502         return TRUE;
503 }
504
505
506 /**
507  * g_mime_signature_list_remove_at:
508  * @list: a #GMimeSignatureList
509  * @index: index to remove
510  *
511  * Removes a #GMimeSignature from the #GMimeSignatureList at the specified
512  * index.
513  *
514  * Returns: %TRUE if an #GMimeSignature was removed or %FALSE otherwise.
515  **/
516 gboolean
517 g_mime_signature_list_remove_at (GMimeSignatureList *list, int index)
518 {
519         GMimeSignature *sig;
520         
521         g_return_val_if_fail (GMIME_IS_SIGNATURE_LIST (list), FALSE);
522         g_return_val_if_fail (index >= 0, FALSE);
523         
524         if ((guint) index >= list->array->len)
525                 return FALSE;
526         
527         sig = list->array->pdata[index];
528         g_ptr_array_remove_index (list->array, index);
529         g_object_unref (sig);
530         
531         return TRUE;
532 }
533
534
535 /**
536  * g_mime_signature_list_contains:
537  * @list: a #GMimeSignatureList
538  * @sig: a #GMimeSignature
539  *
540  * Checks whether or not the specified #GMimeSignature is contained within
541  * the #GMimeSignatureList.
542  *
543  * Returns: %TRUE if the specified #GMimeSignature is contained within the
544  * specified #GMimeSignatureList or %FALSE otherwise.
545  **/
546 gboolean
547 g_mime_signature_list_contains (GMimeSignatureList *list, GMimeSignature *sig)
548 {
549         return g_mime_signature_list_index_of (list, sig) != -1;
550 }
551
552
553 /**
554  * g_mime_signature_list_index_of:
555  * @list: a #GMimeSignatureList
556  * @sig: a #GMimeSignature
557  *
558  * Gets the index of the specified #GMimeSignature inside the
559  * #GMimeSignatureList.
560  *
561  * Returns: the index of the requested #GMimeSignature within the
562  * #GMimeSignatureList or %-1 if it is not contained within the
563  * #GMimeSignatureList.
564  **/
565 int
566 g_mime_signature_list_index_of (GMimeSignatureList *list, GMimeSignature *sig)
567 {
568         guint i;
569         
570         g_return_val_if_fail (GMIME_IS_SIGNATURE_LIST (list), -1);
571         g_return_val_if_fail (GMIME_IS_SIGNATURE (sig), -1);
572         
573         for (i = 0; i < list->array->len; i++) {
574                 if (list->array->pdata[i] == sig)
575                         return i;
576         }
577         
578         return -1;
579 }
580
581
582 /**
583  * g_mime_signature_list_get_signature:
584  * @list: a #GMimeSignatureList
585  * @index: index of #GMimeSignature to get
586  *
587  * Gets the #GMimeSignature at the specified index.
588  *
589  * Returns: the #GMimeSignature at the specified index or %NULL if
590  * the index is out of range.
591  **/
592 GMimeSignature *
593 g_mime_signature_list_get_signature (GMimeSignatureList *list, int index)
594 {
595         g_return_val_if_fail (GMIME_IS_SIGNATURE_LIST (list), NULL);
596         g_return_val_if_fail (index >= 0, NULL);
597         
598         if ((guint) index >= list->array->len)
599                 return NULL;
600         
601         return list->array->pdata[index];
602 }
603
604
605 /**
606  * g_mime_signature_list_set_signature:
607  * @list: a #GMimeSignatureList
608  * @index: index of #GMimeSignature to set
609  * @sig: a #GMimeSignature
610  *
611  * Sets the #GMimeSignature at the specified index to @sig.
612  **/
613 void
614 g_mime_signature_list_set_signature (GMimeSignatureList *list, int index, GMimeSignature *sig)
615 {
616         GMimeSignature *old;
617         
618         g_return_if_fail (GMIME_IS_SIGNATURE_LIST (list));
619         g_return_if_fail (GMIME_IS_SIGNATURE (sig));
620         g_return_if_fail (index >= 0);
621         
622         if ((guint) index > list->array->len)
623                 return;
624         
625         if ((guint) index == list->array->len) {
626                 g_mime_signature_list_add (list, sig);
627                 return;
628         }
629         
630         if ((old = list->array->pdata[index]) == sig)
631                 return;
632         
633         list->array->pdata[index] = sig;
634         g_object_unref (old);
635         g_object_ref (sig);
636 }