]> the.earth.li Git - onak.git/blob - marshal.c
Remove --with-systemd option to dh
[onak.git] / marshal.c
1 /*
2  * marshal.c - SKS compatible marshalling routines
3  *
4  * Copyright 2011 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, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <arpa/inet.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "charfuncs.h"
25 #include "keyid.h"
26 #include "keystructs.h"
27 #include "mem.h"
28 #include "parsekey.h"
29
30 void marshal_publickey(int (*putchar_func)(void *ctx, size_t count,
31                                 void *c),
32                                 void *ctx,
33                                 const struct openpgp_publickey *key)
34 {
35         uint32_t len;
36         struct openpgp_packet_list *packets = NULL, *list_end = NULL;
37         struct buffer_ctx buf;
38
39         buf.buffer = calloc(1, 1024);
40         buf.size = 1024;
41         buf.offset = 0;
42
43         flatten_publickey((struct openpgp_publickey *) key, &packets,
44                         &list_end);
45         write_openpgp_stream(buffer_putchar, &buf, packets);
46
47         len = htonl(buf.offset);
48
49         putchar_func(ctx, sizeof(len), &len);
50         putchar_func(ctx, buf.offset, buf.buffer);
51
52         free_packet_list(packets);
53 }
54
55 void marshal_skshash(int (*putchar_func)(void *ctx, size_t count,
56                                 void *c),
57                                 void *ctx,
58                                 const struct skshash *hash)
59 {
60         uint32_t len;
61
62         len = htonl(sizeof(hash->hash));
63
64         putchar_func(ctx, sizeof(len), &len);
65         putchar_func(ctx, sizeof(hash->hash), (void *) hash->hash);
66 }
67
68 struct skshash *unmarshal_skshash(int (*getchar_func)(void *ctx, size_t count,
69                                 void *c),
70                                 void *ctx)
71 {
72         uint32_t len;
73         struct skshash *hash;
74
75         if (getchar_func(ctx, sizeof(len), &len)) {
76                 return NULL;
77         }
78         len = ntohl(len);
79         if (len > sizeof(struct skshash)) {
80                 return NULL;
81         }
82         hash = calloc(sizeof(struct skshash), 1);
83         if (getchar_func(ctx, len, hash->hash)) {
84                 free(hash);
85                 return NULL;
86         }
87
88         return hash;
89 }
90
91 void marshal_string(int (*putchar_func)(void *ctx, size_t count,
92                                 void *c),
93                                 void *ctx,
94                                 const char *string)
95 {
96         uint32_t len, nlen;
97
98         len = strlen(string);
99         nlen = htonl(len);
100
101         putchar_func(ctx, sizeof(nlen), &nlen);
102         putchar_func(ctx, len, &string);
103 }
104
105 char *unmarshal_string(int (*getchar_func)(void *ctx, size_t count,
106                                 void *c),
107                                 void *ctx)
108 {
109         uint32_t len;
110         char *string;
111
112         if (getchar_func(ctx, sizeof(len), &len)) {
113                 return NULL;
114         }
115         len = ntohl(len);
116         string = malloc(len + 1);
117         if (getchar_func(ctx, len, string)) {
118                 free(string);
119                 return NULL;
120         }
121
122         string[len] = 0;
123         return string;
124 }
125
126 void marshal_array(int (*putchar_func)(void *ctx, size_t count,
127                                 void *c),
128                                 void *ctx,
129                                 void (*marshal_func)(int
130                                         (*putchar_func)(void *ctx,
131                                                 size_t count, void *c),
132                                         void *ctx, const void *item),
133                                 void **array,
134                                 int size)
135 {
136         uint32_t len;
137         int i;
138
139         len = htonl(size);
140
141         putchar_func(ctx, sizeof(len), &len);
142
143         for (i = 0; i < size; i++) {
144                 marshal_func(putchar_func, ctx, array[i]);
145         }
146 }
147
148 void **unmarshal_array(int (*getchar_func)(void *ctx, size_t count,
149                                 void *c),
150                                 void *ctx,
151                                 void *(*unmarshal_func)(int
152                                         (*getchar_func)(void *ctx,
153                                                 size_t count, void *c),
154                                         void *ctx),
155                                 int *size)
156 {
157         uint32_t len;
158         void **array;
159         int i;
160
161         if (getchar_func(ctx, sizeof(len), &len)) {
162                 return NULL;
163         }
164         *size = ntohl(len);
165         array = malloc(*size * sizeof(void *));
166         for (i = 0; i < *size; i++) {
167                 array[i] = unmarshal_func(getchar_func, ctx);
168         }
169
170         return array;
171 }