cscvs to tla changeset 133
[onak.git] / photoid.c
1 /*
2  * photoid.c - Routines for OpenPGP id photos.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2004 Project Purple
7  *
8  * $Id: photoid.c,v 1.2 2004/05/27 21:58:18 noodles Exp $
9  */
10
11 #include <assert.h>
12 #include <inttypes.h>
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #include "keyid.h"
18 #include "keyindex.h"
19 #include "keystructs.h"
20 #include "log.h"
21 #include "photoid.h"
22
23 /**
24  *      getphoto - returns an OpenPGP packet containing a photo id.
25  *      @key: The key to return the photo id from.
26  *      @index: The index of the photo to return.
27  *      @photo: The photo data.
28  *      @length: The length of the photo data.
29  *
30  *      This function returns the photo data contained in a supplied key.
31  *      index specifies which photo id should be returned. If there's no such
32  *      photo id NULL is returned. The returned data pointer refers to the key
33  *      data supplied rather than a copy of it.
34  */
35 int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo,
36                 size_t *length)
37 {
38         struct openpgp_signedpacket_list *curuid = NULL;
39         int                               i = 0;
40         int                               j = 0;
41
42         assert(key != NULL);
43         assert(photo != NULL);
44         assert(length != NULL);
45
46         *photo = NULL;
47         
48         curuid = key->uids;
49         i = 0;
50         while (*photo == NULL && curuid != NULL && i <= index) {
51                 if (curuid->packet->tag == 17) {
52                         if (i == index) {
53                                 j = 0;
54                                 *length = curuid->packet->data[j++];
55                                 if (*length < 192) {
56                                         /* length is correct */
57                                 } else if (*length < 255) {
58                                         *length -= 192;
59                                         *length <<= 8;
60                                         *length += curuid->packet->data[j++];
61                                         *length +=  192;
62                                 } else {
63                                         *length = curuid->packet->data[j++];
64                                         *length <<= 8;
65                                         *length += curuid->packet->data[j++];
66                                         *length <<= 8;
67                                         *length += curuid->packet->data[j++];
68                                         *length <<= 8;
69                                         *length += curuid->packet->data[j++];
70                                 }
71                                 logthing(LOGTHING_DEBUG, "Got photo, size %d",
72                                                 *length);
73                                 j++;
74                                 *length -= 17;
75                                 *photo = &(curuid->packet->data[j+16]);
76                         } else {
77                                 i++;
78                         }
79                 }
80                 curuid = curuid->next;
81         }
82
83         return (*photo != NULL);
84 }