2 * marshal.c - SKS compatible marshalling routines
4 * Copyright 2011 Jonathan McDowell <noodles@earth.li>
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.
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
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.
20 #include <arpa/inet.h>
25 #include "charfuncs.h"
27 #include "keystructs.h"
31 void marshal_publickey(int (*putchar_func)(void *ctx, size_t count,
34 const struct openpgp_publickey *key)
37 struct openpgp_packet_list *packets = NULL, *list_end = NULL;
38 struct buffer_ctx buf;
40 buf.buffer = calloc(1, 1024);
44 flatten_publickey((struct openpgp_publickey *) key, &packets,
46 write_openpgp_stream(buffer_putchar, &buf, packets);
48 len = htonl(buf.offset);
50 putchar_func(ctx, sizeof(len), &len);
51 putchar_func(ctx, buf.offset, buf.buffer);
53 free_packet_list(packets);
56 void marshal_skshash(int (*putchar_func)(void *ctx, size_t count,
59 const struct skshash *hash)
63 len = htonl(sizeof(hash->hash));
65 putchar_func(ctx, sizeof(len), &len);
66 putchar_func(ctx, sizeof(hash->hash), (void *) hash->hash);
69 struct skshash *unmarshal_skshash(int (*getchar_func)(void *ctx, size_t count,
76 if (getchar_func(ctx, sizeof(len), &len)) {
80 if (len > sizeof(struct skshash)) {
83 hash = calloc(sizeof(struct skshash), 1);
84 if (getchar_func(ctx, len, hash->hash)) {
92 void marshal_string(int (*putchar_func)(void *ctx, size_t count,
102 putchar_func(ctx, sizeof(nlen), &nlen);
103 putchar_func(ctx, len, &string);
106 char *unmarshal_string(int (*getchar_func)(void *ctx, size_t count,
113 if (getchar_func(ctx, sizeof(len), &len)) {
117 string = malloc(len + 1);
118 if (getchar_func(ctx, len, string)) {
127 void marshal_array(int (*putchar_func)(void *ctx, size_t count,
130 void (*marshal_func)(int
131 (*putchar_func)(void *ctx,
132 size_t count, void *c),
133 void *ctx, const void *item),
142 putchar_func(ctx, sizeof(len), &len);
144 for (i = 0; i < size; i++) {
145 marshal_func(putchar_func, ctx, array[i]);
149 void **unmarshal_array(int (*getchar_func)(void *ctx, size_t count,
152 void *(*unmarshal_func)(int
153 (*getchar_func)(void *ctx,
154 size_t count, void *c),
162 if (getchar_func(ctx, sizeof(len), &len)) {
166 array = malloc(*size * sizeof(void *));
167 for (i = 0; i < *size; i++) {
168 array[i] = unmarshal_func(getchar_func, ctx);