]> git.sommitrealweird.co.uk Git - onak.git/blob - photoid.c
febc3f5441847be211fb02e40c405201c8789447
[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 <inttypes.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "keyid.h"
26 #include "keyindex.h"
27 #include "keystructs.h"
28 #include "log.h"
29 #include "photoid.h"
30
31 /**
32  *      getphoto - returns an OpenPGP packet containing a photo id.
33  *      @key: The key to return the photo id from.
34  *      @index: The index of the photo to return.
35  *      @photo: The photo data.
36  *      @length: The length of the photo data.
37  *
38  *      This function returns the photo data contained in a supplied key.
39  *      index specifies which photo id should be returned. If there's no such
40  *      photo id NULL is returned. The returned data pointer refers to the key
41  *      data supplied rather than a copy of it.
42  */
43 int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo,
44                 size_t *length)
45 {
46         struct openpgp_signedpacket_list *curuid = NULL;
47         int                               i = 0;
48         int                               j = 0;
49
50         log_assert(key != NULL);
51         log_assert(photo != NULL);
52         log_assert(length != NULL);
53
54         *photo = NULL;
55         
56         curuid = key->uids;
57         i = 0;
58         while (*photo == NULL && curuid != NULL && i <= index) {
59                 if (curuid->packet->tag == 17) {
60                         if (i == index) {
61                                 j = 0;
62                                 *length = curuid->packet->data[j++];
63                                 if (*length < 192) {
64                                         /* length is correct */
65                                 } else if (*length < 255) {
66                                         *length -= 192;
67                                         *length <<= 8;
68                                         *length += curuid->packet->data[j++];
69                                         *length +=  192;
70                                 } else {
71                                         *length = curuid->packet->data[j++];
72                                         *length <<= 8;
73                                         *length += curuid->packet->data[j++];
74                                         *length <<= 8;
75                                         *length += curuid->packet->data[j++];
76                                         *length <<= 8;
77                                         *length += curuid->packet->data[j++];
78                                 }
79                                 logthing(LOGTHING_DEBUG, "Got photo, size %d",
80                                                 *length);
81                                 j++;
82                                 *length -= 17;
83                                 *photo = &(curuid->packet->data[j+16]);
84                         } else {
85                                 i++;
86                         }
87                 }
88                 curuid = curuid->next;
89         }
90
91         return (*photo != NULL);
92 }