Add /pks/hashquery
[onak.git] / marshal.h
1 /*
2  * marshal.h - SKS compatible marshalling routines
3  *
4  * Copyright 2011 Jonathan McDowell <noodles@earth.li>
5  */
6
7 #ifndef __MARSHAL_H__
8 #define __MARSHAL_H__
9
10 #include "keyid.h"
11 #include "keystructs.h"
12
13 /**
14  *      marshal_publickey - Output an OpenPGP key as a byte stream
15  *      @putchar_func: The function to put the next character to the stream
16  *      @ctx: A pointer to the context structure for putchar_func.
17  *      @key: The key to output.
18  *
19  *      Takes an OpenPGP key and marshals it to a byte stream - writes
20  *      a 32 bit size of the forthcoming data in network byte order and
21  *      then the flattened byte representation of the key.
22  */
23 void marshal_publickey(int (*putchar_func)(void *ctx, size_t count,
24                                 void *c),
25                                 void *ctx,
26                                 const struct openpgp_publickey *key);
27
28 /**
29  *      unmarshal_publickey - Turn a byte stream into an OpenPGP key
30  *      @getchar_func: The function to get the next character from the stream
31  *      @ctx: A pointer to the context structure for getchar_func.
32  *
33  *      Returns an OpenPGP structure which is the unmarshalled result of
34  *      the input byte stream - ie the inverse of marshal_publickey.
35  */
36 struct openpgp_publickey *unmarshal_publickey(int (*getchar_func)(void *ctx,
37                                 size_t count,
38                                 void *c),
39                                 void *ctx);
40
41 /**
42  *      marshal_skshash - Output an SKS hash as a byte stream
43  *      @putchar_func: The function to put the next character to the stream
44  *      @ctx: A pointer to the context structure for putchar_func.
45  *      @hash: The hash to output.
46  *
47  *      Takes an SKS hash and marshals it to a byte stream - writes
48  *      a 32 bit size of the forthcoming data (16 bytes) in network byte order
49  *      and then the byte representation of the hash.
50  */
51 void marshal_skshash(int (*putchar_func)(void *ctx, size_t count,
52                                 void *c),
53                                 void *ctx,
54                                 const struct skshash *hash);
55
56 /**
57  *      unmarshal_skshash - Turn a byte stream into an SKS hash structure
58  *      @getchar_func: The function to get the next character from the stream
59  *      @ctx: A pointer to the context structure for getchar_func.
60  *
61  *      Returns an SKS hash structure which is the unmarshalled result of
62  *      the input byte stream - ie the inverse of marshal_skshash.
63  */
64 struct skshash *unmarshal_skshash(int (*getchar_func)(void *ctx, size_t count,
65                                 void *c),
66                                 void *ctx);
67
68 /**
69  *      marshal_string - Output a string as a byte stream
70  *      @putchar_func: The function to put the next character to the stream
71  *      @ctx: A pointer to the context structure for putchar_func.
72  *      @string: The string to output.
73  *
74  *      Takes a string and marshals it to a byte stream - writes a 32 bit size
75  *      of the forthcoming data in network byte order and then the string.
76  */
77 void marshal_string(int (*putchar_func)(void *ctx, size_t count,
78                                 void *c),
79                                 void *ctx,
80                                 const char *string);
81
82 /**
83  *      unmarshal_string - Turn a byte stream into a string
84  *      @getchar_func: The function to get the next character from the stream
85  *      @ctx: A pointer to the context structure for getchar_func.
86  *
87  *      Returns a string which is the unmarshalled result of the input byte
88  *      stream - ie the inverse of marshal_string.
89  */
90 char *unmarshal_string(int (*getchar_func)(void *ctx, size_t count,
91                                 void *c),
92                                 void *ctx);
93
94 /**
95  *      marshal_array - Outputs an array as a byte stream
96  *      @putchar_func: The function to put the next character to the stream
97  *      @ctx: A pointer to the context structure for putchar_func.
98  *      @marshal_func: The function to use to marshal each array element.
99  *      @array: A pointer to the array to marshal
100  *      @size:: The number of elements in the array.
101  *
102  *      Takes an array and marshals it into a byte stream. Outputs a 32 bit
103  *      count of the elements in the array in network byte order and then
104  *      calls marshal_func for each element in the array to provide the
105  *      marshalled contents.
106  */
107 void marshal_array(int (*putchar_func)(void *ctx, size_t count,
108                                 void *c),
109                                 void *ctx,
110                                 void (*marshal_func)(int
111                                         (*putchar_func)(void *ctx,
112                                                 size_t count, void *c),
113                                         void *ctx, const void *item),
114                                 void **array,
115                                 int size);
116
117 /**
118  *      unmarshal_array - Turn a byte stream into an array of elements
119  *      @getchar_func: The function to get the next character from the stream
120  *      @ctx: A pointer to the context structure for getchar_func.
121  *      @unmarshal_func: The function to use to unmarshal each array element.
122  *      @size: A pointer to where to store the number of elements unmarshalled
123  *
124  *      Takes a byte stream and unmarshals it into an array of elements,
125  *      as determined by the supplied unmarshal_func function. ie the reverse
126  *      of marshal_array.
127  */
128 void **unmarshal_array(int (*getchar_func)(void *ctx, size_t count,
129                                 void *c),
130                                 void *ctx,
131                                 void *(*unmarshal_func)(int
132                                         (*getchar_func)(void *ctx,
133                                                 size_t count, void *c),
134                                         void *ctx),
135                                 int *size);
136
137 #endif /* __MARSHAL_H__ */