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, see <https://www.gnu.org/licenses/>.
19 #include <arpa/inet.h>
24 #include "charfuncs.h"
26 #include "keystructs.h"
30 void marshal_publickey(size_t (*putchar_func)(void *ctx, size_t count,
33 const struct openpgp_publickey *key)
36 struct openpgp_packet_list *packets = NULL, *list_end = NULL;
37 struct buffer_ctx buf;
39 buf.buffer = calloc(1, 1024);
43 flatten_publickey((struct openpgp_publickey *) key, &packets,
45 write_openpgp_stream(buffer_putchar, &buf, packets);
47 len = htonl(buf.offset);
49 putchar_func(ctx, sizeof(len), &len);
50 putchar_func(ctx, buf.offset, buf.buffer);
52 free_packet_list(packets);
55 void marshal_skshash(size_t (*putchar_func)(void *ctx, size_t count,
58 const struct skshash *hash)
62 len = htonl(sizeof(hash->hash));
64 putchar_func(ctx, sizeof(len), &len);
65 putchar_func(ctx, sizeof(hash->hash), (void *) hash->hash);
68 struct skshash *unmarshal_skshash(size_t (*getchar_func)(void *ctx, size_t count,
75 if (getchar_func(ctx, sizeof(len), &len) != sizeof(len)) {
79 if (len > sizeof(struct skshash)) {
82 hash = calloc(sizeof(struct skshash), 1);
83 if (getchar_func(ctx, len, hash->hash) != len) {
91 void marshal_string(size_t (*putchar_func)(void *ctx, size_t count,
101 putchar_func(ctx, sizeof(nlen), &nlen);
102 putchar_func(ctx, len, &string);
105 char *unmarshal_string(size_t (*getchar_func)(void *ctx, size_t count,
112 if (getchar_func(ctx, sizeof(len), &len) != sizeof(len)) {
116 string = malloc(len + 1);
117 if (getchar_func(ctx, len, string) != len) {
126 void marshal_array(size_t (*putchar_func)(void *ctx, size_t count,
129 void (*marshal_func)(size_t
130 (*putchar_func)(void *ctx,
131 size_t count, void *c),
132 void *ctx, const void *item),
141 putchar_func(ctx, sizeof(len), &len);
143 for (i = 0; i < size; i++) {
144 marshal_func(putchar_func, ctx, array[i]);
148 void **unmarshal_array(size_t (*getchar_func)(void *ctx, size_t count,
151 void *(*unmarshal_func)(size_t
152 (*getchar_func)(void *ctx,
153 size_t count, void *c),
161 if (getchar_func(ctx, sizeof(len), &len) != sizeof(len)) {
165 array = malloc(*size * sizeof(void *));
166 for (i = 0; i < *size; i++) {
167 array[i] = unmarshal_func(getchar_func, ctx);