]> the.earth.li Git - onak.git/blob - photoid.c
983d6a84716101ffe476100a04630f3b5f14734c
[onak.git] / photoid.c
1 /*
2  * photoid.c - Routines for OpenPGP id photos.
3  *
4  * Copyright 2004 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdlib.h>
21
22 #include "keystructs.h"
23 #include "onak.h"
24 #include "photoid.h"
25
26 /**
27  *      getphoto - returns an OpenPGP packet containing a photo id.
28  *      @key: The key to return the photo id from.
29  *      @index: The index of the photo to return.
30  *      @photo: The photo data.
31  *      @length: The length of the photo data.
32  *
33  *      This function returns the photo data contained in a supplied key.
34  *      index specifies which photo id should be returned. If there's no such
35  *      photo id NULL is returned. The returned data pointer refers to the key
36  *      data supplied rather than a copy of it.
37  */
38 onak_status_t getphoto(struct openpgp_publickey *key, int index,
39                 unsigned char **photo, size_t *length)
40 {
41         struct openpgp_signedpacket_list *curuid = NULL;
42         int                               i = 0;
43         int                               j = 0;
44
45         if (key == NULL || photo == NULL || length == NULL)
46                 return ONAK_E_INVALID_PARAM;
47
48         *photo = NULL;
49         
50         curuid = key->uids;
51         i = 0;
52         while (*photo == NULL && curuid != NULL && i <= index) {
53                 if (curuid->packet->tag == 17) {
54                         if (i == index) {
55                                 j = 0;
56                                 *length = curuid->packet->data[j++];
57                                 if (*length < 192) {
58                                         /* length is correct */
59                                 } else if (*length < 255) {
60                                         *length -= 192;
61                                         *length <<= 8;
62                                         *length += curuid->packet->data[j++];
63                                         *length +=  192;
64                                 } else {
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                                         *length <<= 8;
71                                         *length += curuid->packet->data[j++];
72                                 }
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 ? ONAK_E_NOT_FOUND : ONAK_E_OK;
84 }