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