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