2 * marshal.c - SKS compatible marshalling routines
4 * Copyright 2011 Jonathan McDowell <noodles@earth.li>
12 #include "charfuncs.h"
14 #include "keystructs.h"
18 void marshal_publickey(int (*putchar_func)(void *ctx, size_t count,
21 const struct openpgp_publickey *key)
24 struct openpgp_packet_list *packets = NULL, *list_end = NULL;
25 struct buffer_ctx buf;
27 buf.buffer = calloc(1, 1024);
31 flatten_publickey((struct openpgp_publickey *) key, &packets,
33 write_openpgp_stream(buffer_putchar, &buf, packets);
35 len = htonl(buf.offset);
37 putchar_func(ctx, sizeof(len), &len);
38 putchar_func(ctx, buf.offset, buf.buffer);
40 free_packet_list(packets);
43 void marshal_skshash(int (*putchar_func)(void *ctx, size_t count,
46 const struct skshash *hash)
50 len = htonl(sizeof(hash->hash));
52 putchar_func(ctx, sizeof(len), &len);
53 putchar_func(ctx, sizeof(hash->hash), (void *) hash->hash);
56 struct skshash *unmarshal_skshash(int (*getchar_func)(void *ctx, size_t count,
63 if (getchar_func(ctx, sizeof(len), &len)) {
67 if (len > sizeof(struct skshash)) {
70 hash = calloc(sizeof(struct skshash), 1);
71 if (getchar_func(ctx, len, hash->hash)) {
79 void marshal_string(int (*putchar_func)(void *ctx, size_t count,
89 putchar_func(ctx, sizeof(nlen), &nlen);
90 putchar_func(ctx, len, &string);
93 char *unmarshal_string(int (*getchar_func)(void *ctx, size_t count,
100 if (getchar_func(ctx, sizeof(len), &len)) {
104 string = malloc(len + 1);
105 if (getchar_func(ctx, len, string)) {
114 void marshal_array(int (*putchar_func)(void *ctx, size_t count,
117 void (*marshal_func)(int
118 (*putchar_func)(void *ctx,
119 size_t count, void *c),
120 void *ctx, const void *item),
129 putchar_func(ctx, sizeof(len), &len);
131 for (i = 0; i < size; i++) {
132 marshal_func(putchar_func, ctx, array[i]);
136 void **unmarshal_array(int (*getchar_func)(void *ctx, size_t count,
139 void *(*unmarshal_func)(int
140 (*getchar_func)(void *ctx,
141 size_t count, void *c),
149 if (getchar_func(ctx, sizeof(len), &len)) {
153 array = malloc(*size * sizeof(void *));
154 for (i = 0; i < *size; i++) {
155 array[i] = unmarshal_func(getchar_func, ctx);