xref: /freebsd/contrib/libevent/test/regress.gen.c (revision b50261e21f39a6c7249a49e7b60aa878c98512a8)
1*b50261e2SCy Schubert 
2c43e99fdSEd Maste /*
3*b50261e2SCy Schubert  * Automatically generated from ../test/regress.rpc
4c43e99fdSEd Maste  * by event_rpcgen.py/0.1.  DO NOT EDIT THIS FILE.
5c43e99fdSEd Maste  */
6c43e99fdSEd Maste 
7c43e99fdSEd Maste #include <stdlib.h>
8c43e99fdSEd Maste #include <string.h>
9c43e99fdSEd Maste #include <assert.h>
10c43e99fdSEd Maste #include <event2/event-config.h>
11c43e99fdSEd Maste #include <event2/event.h>
12c43e99fdSEd Maste #include <event2/buffer.h>
13c43e99fdSEd Maste #include <event2/tag.h>
14c43e99fdSEd Maste 
15*b50261e2SCy Schubert #if defined(EVENT__HAVE___func__)
16*b50261e2SCy Schubert # ifndef __func__
17*b50261e2SCy Schubert #  define __func__ __func__
18*b50261e2SCy Schubert # endif
19*b50261e2SCy Schubert #elif defined(EVENT__HAVE___FUNCTION__)
20*b50261e2SCy Schubert # define __func__ __FUNCTION__
21*b50261e2SCy Schubert #else
22*b50261e2SCy Schubert # define __func__ __FILE__
23c43e99fdSEd Maste #endif
24c43e99fdSEd Maste 
25c43e99fdSEd Maste 
26c43e99fdSEd Maste #include "regress.gen.h"
27c43e99fdSEd Maste 
28c43e99fdSEd Maste void event_warn(const char *fmt, ...);
29c43e99fdSEd Maste void event_warnx(const char *fmt, ...);
30c43e99fdSEd Maste 
31c43e99fdSEd Maste /*
32c43e99fdSEd Maste  * Implementation of msg
33c43e99fdSEd Maste  */
34c43e99fdSEd Maste 
35c43e99fdSEd Maste static struct msg_access_ msg_base__ = {
36c43e99fdSEd Maste   msg_from_name_assign,
37c43e99fdSEd Maste   msg_from_name_get,
38c43e99fdSEd Maste   msg_to_name_assign,
39c43e99fdSEd Maste   msg_to_name_get,
40c43e99fdSEd Maste   msg_attack_assign,
41c43e99fdSEd Maste   msg_attack_get,
42c43e99fdSEd Maste   msg_run_assign,
43c43e99fdSEd Maste   msg_run_get,
44c43e99fdSEd Maste   msg_run_add,
45c43e99fdSEd Maste };
46c43e99fdSEd Maste 
47c43e99fdSEd Maste struct msg *
msg_new(void)48c43e99fdSEd Maste msg_new(void)
49c43e99fdSEd Maste {
50c43e99fdSEd Maste   return msg_new_with_arg(NULL);
51c43e99fdSEd Maste }
52c43e99fdSEd Maste 
53c43e99fdSEd Maste struct msg *
msg_new_with_arg(void * unused)54c43e99fdSEd Maste msg_new_with_arg(void *unused)
55c43e99fdSEd Maste {
56c43e99fdSEd Maste   struct msg *tmp;
57c43e99fdSEd Maste   if ((tmp = malloc(sizeof(struct msg))) == NULL) {
58c43e99fdSEd Maste     event_warn("%s: malloc", __func__);
59c43e99fdSEd Maste     return (NULL);
60c43e99fdSEd Maste   }
61c43e99fdSEd Maste   tmp->base = &msg_base__;
62c43e99fdSEd Maste 
63c43e99fdSEd Maste   tmp->from_name_data = NULL;
64c43e99fdSEd Maste   tmp->from_name_set = 0;
65c43e99fdSEd Maste 
66c43e99fdSEd Maste   tmp->to_name_data = NULL;
67c43e99fdSEd Maste   tmp->to_name_set = 0;
68c43e99fdSEd Maste 
69c43e99fdSEd Maste   tmp->attack_data = NULL;
70c43e99fdSEd Maste   tmp->attack_set = 0;
71c43e99fdSEd Maste 
72c43e99fdSEd Maste   tmp->run_data = NULL;
73c43e99fdSEd Maste   tmp->run_length = 0;
74c43e99fdSEd Maste   tmp->run_num_allocated = 0;
75c43e99fdSEd Maste   tmp->run_set = 0;
76c43e99fdSEd Maste 
77c43e99fdSEd Maste   return (tmp);
78c43e99fdSEd Maste }
79c43e99fdSEd Maste 
80c43e99fdSEd Maste 
81c43e99fdSEd Maste 
82c43e99fdSEd Maste 
83c43e99fdSEd Maste static int
msg_run_expand_to_hold_more(struct msg * msg)84c43e99fdSEd Maste msg_run_expand_to_hold_more(struct msg *msg)
85c43e99fdSEd Maste {
86c43e99fdSEd Maste   int tobe_allocated = msg->run_num_allocated;
87c43e99fdSEd Maste   struct run** new_data = NULL;
88c43e99fdSEd Maste   tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
89c43e99fdSEd Maste   new_data = (struct run**) realloc(msg->run_data,
90c43e99fdSEd Maste       tobe_allocated * sizeof(struct run*));
91c43e99fdSEd Maste   if (new_data == NULL)
92c43e99fdSEd Maste     return -1;
93c43e99fdSEd Maste   msg->run_data = new_data;
94c43e99fdSEd Maste   msg->run_num_allocated = tobe_allocated;
95*b50261e2SCy Schubert   return 0;
96*b50261e2SCy Schubert }
97c43e99fdSEd Maste 
98c43e99fdSEd Maste struct run*
msg_run_add(struct msg * msg)99c43e99fdSEd Maste msg_run_add(struct msg *msg)
100c43e99fdSEd Maste {
101c43e99fdSEd Maste   if (++msg->run_length >= msg->run_num_allocated) {
102c43e99fdSEd Maste     if (msg_run_expand_to_hold_more(msg)<0)
103c43e99fdSEd Maste       goto error;
104c43e99fdSEd Maste   }
105c43e99fdSEd Maste   msg->run_data[msg->run_length - 1] = run_new();
106c43e99fdSEd Maste   if (msg->run_data[msg->run_length - 1] == NULL)
107c43e99fdSEd Maste     goto error;
108c43e99fdSEd Maste   msg->run_set = 1;
109c43e99fdSEd Maste   return (msg->run_data[msg->run_length - 1]);
110c43e99fdSEd Maste error:
111c43e99fdSEd Maste   --msg->run_length;
112c43e99fdSEd Maste   return (NULL);
113c43e99fdSEd Maste }
114c43e99fdSEd Maste 
115c43e99fdSEd Maste int
msg_from_name_assign(struct msg * msg,const char * value)116c43e99fdSEd Maste msg_from_name_assign(struct msg *msg,
117c43e99fdSEd Maste     const char * value)
118c43e99fdSEd Maste {
119c43e99fdSEd Maste   if (msg->from_name_data != NULL)
120c43e99fdSEd Maste     free(msg->from_name_data);
121c43e99fdSEd Maste   if ((msg->from_name_data = strdup(value)) == NULL)
122c43e99fdSEd Maste     return (-1);
123c43e99fdSEd Maste   msg->from_name_set = 1;
124c43e99fdSEd Maste   return (0);
125c43e99fdSEd Maste }
126c43e99fdSEd Maste 
127c43e99fdSEd Maste int
msg_to_name_assign(struct msg * msg,const char * value)128c43e99fdSEd Maste msg_to_name_assign(struct msg *msg,
129c43e99fdSEd Maste     const char * value)
130c43e99fdSEd Maste {
131c43e99fdSEd Maste   if (msg->to_name_data != NULL)
132c43e99fdSEd Maste     free(msg->to_name_data);
133c43e99fdSEd Maste   if ((msg->to_name_data = strdup(value)) == NULL)
134c43e99fdSEd Maste     return (-1);
135c43e99fdSEd Maste   msg->to_name_set = 1;
136c43e99fdSEd Maste   return (0);
137c43e99fdSEd Maste }
138c43e99fdSEd Maste 
139c43e99fdSEd Maste int
msg_attack_assign(struct msg * msg,const struct kill * value)140c43e99fdSEd Maste msg_attack_assign(struct msg *msg,
141c43e99fdSEd Maste     const struct kill* value)
142c43e99fdSEd Maste {
143c43e99fdSEd Maste    struct evbuffer *tmp = NULL;
144c43e99fdSEd Maste    if (msg->attack_set) {
145c43e99fdSEd Maste      kill_clear(msg->attack_data);
146c43e99fdSEd Maste      msg->attack_set = 0;
147c43e99fdSEd Maste    } else {
148c43e99fdSEd Maste      msg->attack_data = kill_new();
149c43e99fdSEd Maste      if (msg->attack_data == NULL) {
150c43e99fdSEd Maste        event_warn("%s: kill_new()", __func__);
151c43e99fdSEd Maste        goto error;
152c43e99fdSEd Maste      }
153c43e99fdSEd Maste    }
154c43e99fdSEd Maste    if ((tmp = evbuffer_new()) == NULL) {
155c43e99fdSEd Maste      event_warn("%s: evbuffer_new()", __func__);
156c43e99fdSEd Maste      goto error;
157c43e99fdSEd Maste    }
158c43e99fdSEd Maste    kill_marshal(tmp, value);
159c43e99fdSEd Maste    if (kill_unmarshal(msg->attack_data, tmp) == -1) {
160c43e99fdSEd Maste      event_warnx("%s: kill_unmarshal", __func__);
161c43e99fdSEd Maste      goto error;
162c43e99fdSEd Maste    }
163c43e99fdSEd Maste    msg->attack_set = 1;
164c43e99fdSEd Maste    evbuffer_free(tmp);
165c43e99fdSEd Maste    return (0);
166c43e99fdSEd Maste  error:
167c43e99fdSEd Maste    if (tmp != NULL)
168c43e99fdSEd Maste      evbuffer_free(tmp);
169c43e99fdSEd Maste    if (msg->attack_data != NULL) {
170c43e99fdSEd Maste      kill_free(msg->attack_data);
171c43e99fdSEd Maste      msg->attack_data = NULL;
172c43e99fdSEd Maste    }
173c43e99fdSEd Maste    return (-1);
174c43e99fdSEd Maste }
175c43e99fdSEd Maste 
176c43e99fdSEd Maste int
msg_run_assign(struct msg * msg,int off,const struct run * value)177c43e99fdSEd Maste msg_run_assign(struct msg *msg, int off,
178c43e99fdSEd Maste   const struct run* value)
179c43e99fdSEd Maste {
180c43e99fdSEd Maste   if (!msg->run_set || off < 0 || off >= msg->run_length)
181c43e99fdSEd Maste     return (-1);
182c43e99fdSEd Maste 
183c43e99fdSEd Maste   {
184c43e99fdSEd Maste     int had_error = 0;
185c43e99fdSEd Maste     struct evbuffer *tmp = NULL;
186c43e99fdSEd Maste     run_clear(msg->run_data[off]);
187c43e99fdSEd Maste     if ((tmp = evbuffer_new()) == NULL) {
188c43e99fdSEd Maste       event_warn("%s: evbuffer_new()", __func__);
189c43e99fdSEd Maste       had_error = 1;
190c43e99fdSEd Maste       goto done;
191c43e99fdSEd Maste     }
192c43e99fdSEd Maste     run_marshal(tmp, value);
193c43e99fdSEd Maste     if (run_unmarshal(msg->run_data[off], tmp) == -1) {
194c43e99fdSEd Maste       event_warnx("%s: run_unmarshal", __func__);
195c43e99fdSEd Maste       had_error = 1;
196c43e99fdSEd Maste       goto done;
197c43e99fdSEd Maste     }
198*b50261e2SCy Schubert     done:
199*b50261e2SCy Schubert     if (tmp != NULL)
200c43e99fdSEd Maste       evbuffer_free(tmp);
201c43e99fdSEd Maste     if (had_error) {
202c43e99fdSEd Maste       run_clear(msg->run_data[off]);
203c43e99fdSEd Maste       return (-1);
204c43e99fdSEd Maste     }
205c43e99fdSEd Maste   }
206c43e99fdSEd Maste   return (0);
207c43e99fdSEd Maste }
208c43e99fdSEd Maste 
209c43e99fdSEd Maste int
msg_from_name_get(struct msg * msg,char ** value)210c43e99fdSEd Maste msg_from_name_get(struct msg *msg, char * *value)
211c43e99fdSEd Maste {
212c43e99fdSEd Maste   if (msg->from_name_set != 1)
213c43e99fdSEd Maste     return (-1);
214c43e99fdSEd Maste   *value = msg->from_name_data;
215c43e99fdSEd Maste   return (0);
216c43e99fdSEd Maste }
217c43e99fdSEd Maste 
218c43e99fdSEd Maste int
msg_to_name_get(struct msg * msg,char ** value)219c43e99fdSEd Maste msg_to_name_get(struct msg *msg, char * *value)
220c43e99fdSEd Maste {
221c43e99fdSEd Maste   if (msg->to_name_set != 1)
222c43e99fdSEd Maste     return (-1);
223c43e99fdSEd Maste   *value = msg->to_name_data;
224c43e99fdSEd Maste   return (0);
225c43e99fdSEd Maste }
226c43e99fdSEd Maste 
227c43e99fdSEd Maste int
msg_attack_get(struct msg * msg,struct kill ** value)228c43e99fdSEd Maste msg_attack_get(struct msg *msg, struct kill* *value)
229c43e99fdSEd Maste {
230c43e99fdSEd Maste   if (msg->attack_set != 1) {
231c43e99fdSEd Maste     msg->attack_data = kill_new();
232c43e99fdSEd Maste     if (msg->attack_data == NULL)
233c43e99fdSEd Maste       return (-1);
234c43e99fdSEd Maste     msg->attack_set = 1;
235c43e99fdSEd Maste   }
236c43e99fdSEd Maste   *value = msg->attack_data;
237c43e99fdSEd Maste   return (0);
238c43e99fdSEd Maste }
239c43e99fdSEd Maste 
240c43e99fdSEd Maste int
msg_run_get(struct msg * msg,int offset,struct run ** value)241c43e99fdSEd Maste msg_run_get(struct msg *msg, int offset,
242c43e99fdSEd Maste     struct run* *value)
243c43e99fdSEd Maste {
244c43e99fdSEd Maste   if (!msg->run_set || offset < 0 || offset >= msg->run_length)
245c43e99fdSEd Maste     return (-1);
246c43e99fdSEd Maste   *value = msg->run_data[offset];
247c43e99fdSEd Maste   return (0);
248c43e99fdSEd Maste }
249c43e99fdSEd Maste 
250c43e99fdSEd Maste void
msg_clear(struct msg * tmp)251c43e99fdSEd Maste msg_clear(struct msg *tmp)
252c43e99fdSEd Maste {
253c43e99fdSEd Maste   if (tmp->from_name_set == 1) {
254c43e99fdSEd Maste     free(tmp->from_name_data);
255c43e99fdSEd Maste     tmp->from_name_data = NULL;
256c43e99fdSEd Maste     tmp->from_name_set = 0;
257c43e99fdSEd Maste   }
258c43e99fdSEd Maste   if (tmp->to_name_set == 1) {
259c43e99fdSEd Maste     free(tmp->to_name_data);
260c43e99fdSEd Maste     tmp->to_name_data = NULL;
261c43e99fdSEd Maste     tmp->to_name_set = 0;
262c43e99fdSEd Maste   }
263c43e99fdSEd Maste   if (tmp->attack_set == 1) {
264c43e99fdSEd Maste     kill_free(tmp->attack_data);
265c43e99fdSEd Maste     tmp->attack_data = NULL;
266c43e99fdSEd Maste     tmp->attack_set = 0;
267c43e99fdSEd Maste   }
268c43e99fdSEd Maste   if (tmp->run_set == 1) {
269c43e99fdSEd Maste     int i;
270c43e99fdSEd Maste     for (i = 0; i < tmp->run_length; ++i) {
271c43e99fdSEd Maste       run_free(tmp->run_data[i]);
272c43e99fdSEd Maste     }
273c43e99fdSEd Maste     free(tmp->run_data);
274c43e99fdSEd Maste     tmp->run_data = NULL;
275c43e99fdSEd Maste     tmp->run_set = 0;
276c43e99fdSEd Maste     tmp->run_length = 0;
277c43e99fdSEd Maste     tmp->run_num_allocated = 0;
278c43e99fdSEd Maste   }
279c43e99fdSEd Maste }
280c43e99fdSEd Maste 
281c43e99fdSEd Maste void
msg_free(struct msg * tmp)282c43e99fdSEd Maste msg_free(struct msg *tmp)
283c43e99fdSEd Maste {
284c43e99fdSEd Maste   if (tmp->from_name_data != NULL)
285c43e99fdSEd Maste       free (tmp->from_name_data);
286c43e99fdSEd Maste   if (tmp->to_name_data != NULL)
287c43e99fdSEd Maste       free (tmp->to_name_data);
288c43e99fdSEd Maste   if (tmp->attack_data != NULL)
289c43e99fdSEd Maste       kill_free(tmp->attack_data);
290c43e99fdSEd Maste   if (tmp->run_set == 1) {
291c43e99fdSEd Maste     int i;
292c43e99fdSEd Maste     for (i = 0; i < tmp->run_length; ++i) {
293c43e99fdSEd Maste       run_free(tmp->run_data[i]);
294c43e99fdSEd Maste     }
295c43e99fdSEd Maste     free(tmp->run_data);
296c43e99fdSEd Maste     tmp->run_data = NULL;
297c43e99fdSEd Maste     tmp->run_set = 0;
298c43e99fdSEd Maste     tmp->run_length = 0;
299c43e99fdSEd Maste     tmp->run_num_allocated = 0;
300c43e99fdSEd Maste   }
301c43e99fdSEd Maste   free(tmp->run_data);
302c43e99fdSEd Maste   free(tmp);
303c43e99fdSEd Maste }
304c43e99fdSEd Maste 
305c43e99fdSEd Maste void
msg_marshal(struct evbuffer * evbuf,const struct msg * tmp)306c43e99fdSEd Maste msg_marshal(struct evbuffer *evbuf, const struct msg *tmp) {
307c43e99fdSEd Maste   evtag_marshal_string(evbuf, MSG_FROM_NAME, tmp->from_name_data);
308c43e99fdSEd Maste   evtag_marshal_string(evbuf, MSG_TO_NAME, tmp->to_name_data);
309c43e99fdSEd Maste   if (tmp->attack_set) {
310c43e99fdSEd Maste     evtag_marshal_kill(evbuf, MSG_ATTACK, tmp->attack_data);
311c43e99fdSEd Maste   }
312c43e99fdSEd Maste   if (tmp->run_set) {
313c43e99fdSEd Maste     {
314c43e99fdSEd Maste       int i;
315c43e99fdSEd Maste       for (i = 0; i < tmp->run_length; ++i) {
316c43e99fdSEd Maste     evtag_marshal_run(evbuf, MSG_RUN, tmp->run_data[i]);
317c43e99fdSEd Maste       }
318c43e99fdSEd Maste     }
319c43e99fdSEd Maste   }
320c43e99fdSEd Maste }
321c43e99fdSEd Maste 
322c43e99fdSEd Maste int
msg_unmarshal(struct msg * tmp,struct evbuffer * evbuf)323c43e99fdSEd Maste msg_unmarshal(struct msg *tmp, struct evbuffer *evbuf)
324c43e99fdSEd Maste {
325c43e99fdSEd Maste   ev_uint32_t tag;
326c43e99fdSEd Maste   while (evbuffer_get_length(evbuf) > 0) {
327c43e99fdSEd Maste     if (evtag_peek(evbuf, &tag) == -1)
328c43e99fdSEd Maste       return (-1);
329c43e99fdSEd Maste     switch (tag) {
330c43e99fdSEd Maste 
331c43e99fdSEd Maste       case MSG_FROM_NAME:
332c43e99fdSEd Maste         if (tmp->from_name_set)
333c43e99fdSEd Maste           return (-1);
334c43e99fdSEd Maste         if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) {
335c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal from_name", __func__);
336c43e99fdSEd Maste           return (-1);
337c43e99fdSEd Maste         }
338c43e99fdSEd Maste         tmp->from_name_set = 1;
339c43e99fdSEd Maste         break;
340c43e99fdSEd Maste       case MSG_TO_NAME:
341c43e99fdSEd Maste         if (tmp->to_name_set)
342c43e99fdSEd Maste           return (-1);
343c43e99fdSEd Maste         if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) {
344c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal to_name", __func__);
345c43e99fdSEd Maste           return (-1);
346c43e99fdSEd Maste         }
347c43e99fdSEd Maste         tmp->to_name_set = 1;
348c43e99fdSEd Maste         break;
349c43e99fdSEd Maste       case MSG_ATTACK:
350c43e99fdSEd Maste         if (tmp->attack_set)
351c43e99fdSEd Maste           return (-1);
352c43e99fdSEd Maste         tmp->attack_data = kill_new();
353c43e99fdSEd Maste         if (tmp->attack_data == NULL)
354c43e99fdSEd Maste           return (-1);
355*b50261e2SCy Schubert         if (evtag_unmarshal_kill(evbuf, MSG_ATTACK,
356*b50261e2SCy Schubert             tmp->attack_data) == -1) {
357c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal attack", __func__);
358c43e99fdSEd Maste           return (-1);
359c43e99fdSEd Maste         }
360c43e99fdSEd Maste         tmp->attack_set = 1;
361c43e99fdSEd Maste         break;
362c43e99fdSEd Maste       case MSG_RUN:
363c43e99fdSEd Maste         if (tmp->run_length >= tmp->run_num_allocated &&
364c43e99fdSEd Maste             msg_run_expand_to_hold_more(tmp) < 0) {
365c43e99fdSEd Maste           puts("HEY NOW");
366c43e99fdSEd Maste           return (-1);
367c43e99fdSEd Maste         }
368c43e99fdSEd Maste         tmp->run_data[tmp->run_length] = run_new();
369c43e99fdSEd Maste         if (tmp->run_data[tmp->run_length] == NULL)
370c43e99fdSEd Maste           return (-1);
371*b50261e2SCy Schubert         if (evtag_unmarshal_run(evbuf, MSG_RUN,
372*b50261e2SCy Schubert             tmp->run_data[tmp->run_length]) == -1) {
373c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal run", __func__);
374c43e99fdSEd Maste           return (-1);
375c43e99fdSEd Maste         }
376c43e99fdSEd Maste         ++tmp->run_length;
377c43e99fdSEd Maste         tmp->run_set = 1;
378c43e99fdSEd Maste         break;
379c43e99fdSEd Maste       default:
380c43e99fdSEd Maste         return -1;
381c43e99fdSEd Maste     }
382c43e99fdSEd Maste   }
383c43e99fdSEd Maste 
384c43e99fdSEd Maste   if (msg_complete(tmp) == -1)
385c43e99fdSEd Maste     return (-1);
386c43e99fdSEd Maste   return (0);
387c43e99fdSEd Maste }
388c43e99fdSEd Maste 
389c43e99fdSEd Maste int
msg_complete(struct msg * msg)390c43e99fdSEd Maste msg_complete(struct msg *msg)
391c43e99fdSEd Maste {
392c43e99fdSEd Maste   if (!msg->from_name_set)
393c43e99fdSEd Maste     return (-1);
394c43e99fdSEd Maste   if (!msg->to_name_set)
395c43e99fdSEd Maste     return (-1);
396c43e99fdSEd Maste   if (msg->attack_set && kill_complete(msg->attack_data) == -1)
397c43e99fdSEd Maste     return (-1);
398c43e99fdSEd Maste   {
399c43e99fdSEd Maste     int i;
400c43e99fdSEd Maste     for (i = 0; i < msg->run_length; ++i) {
401c43e99fdSEd Maste       if (msg->run_set && run_complete(msg->run_data[i]) == -1)
402c43e99fdSEd Maste         return (-1);
403c43e99fdSEd Maste     }
404c43e99fdSEd Maste   }
405c43e99fdSEd Maste   return (0);
406c43e99fdSEd Maste }
407c43e99fdSEd Maste 
408c43e99fdSEd Maste int
evtag_unmarshal_msg(struct evbuffer * evbuf,ev_uint32_t need_tag,struct msg * msg)409*b50261e2SCy Schubert evtag_unmarshal_msg(struct evbuffer *evbuf, ev_uint32_t need_tag,
410*b50261e2SCy Schubert   struct msg *msg)
411c43e99fdSEd Maste {
412c43e99fdSEd Maste   ev_uint32_t tag;
413c43e99fdSEd Maste   int res = -1;
414c43e99fdSEd Maste 
415c43e99fdSEd Maste   struct evbuffer *tmp = evbuffer_new();
416c43e99fdSEd Maste 
417c43e99fdSEd Maste   if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
418c43e99fdSEd Maste     goto error;
419c43e99fdSEd Maste 
420c43e99fdSEd Maste   if (msg_unmarshal(msg, tmp) == -1)
421c43e99fdSEd Maste     goto error;
422c43e99fdSEd Maste 
423c43e99fdSEd Maste   res = 0;
424c43e99fdSEd Maste 
425c43e99fdSEd Maste  error:
426c43e99fdSEd Maste   evbuffer_free(tmp);
427c43e99fdSEd Maste   return (res);
428c43e99fdSEd Maste }
429c43e99fdSEd Maste 
430c43e99fdSEd Maste void
evtag_marshal_msg(struct evbuffer * evbuf,ev_uint32_t tag,const struct msg * msg)431*b50261e2SCy Schubert evtag_marshal_msg(struct evbuffer *evbuf, ev_uint32_t tag,
432*b50261e2SCy Schubert     const struct msg *msg)
433c43e99fdSEd Maste {
434c43e99fdSEd Maste   struct evbuffer *buf_ = evbuffer_new();
435c43e99fdSEd Maste   assert(buf_ != NULL);
436c43e99fdSEd Maste   msg_marshal(buf_, msg);
437c43e99fdSEd Maste   evtag_marshal_buffer(evbuf, tag, buf_);
438c43e99fdSEd Maste   evbuffer_free(buf_);
439c43e99fdSEd Maste }
440c43e99fdSEd Maste 
441c43e99fdSEd Maste /*
442c43e99fdSEd Maste  * Implementation of kill
443c43e99fdSEd Maste  */
444c43e99fdSEd Maste 
445c43e99fdSEd Maste static struct kill_access_ kill_base__ = {
446c43e99fdSEd Maste   kill_weapon_assign,
447c43e99fdSEd Maste   kill_weapon_get,
448c43e99fdSEd Maste   kill_action_assign,
449c43e99fdSEd Maste   kill_action_get,
450c43e99fdSEd Maste   kill_how_often_assign,
451c43e99fdSEd Maste   kill_how_often_get,
452c43e99fdSEd Maste   kill_how_often_add,
453c43e99fdSEd Maste };
454c43e99fdSEd Maste 
455c43e99fdSEd Maste struct kill *
kill_new(void)456c43e99fdSEd Maste kill_new(void)
457c43e99fdSEd Maste {
458c43e99fdSEd Maste   return kill_new_with_arg(NULL);
459c43e99fdSEd Maste }
460c43e99fdSEd Maste 
461c43e99fdSEd Maste struct kill *
kill_new_with_arg(void * unused)462c43e99fdSEd Maste kill_new_with_arg(void *unused)
463c43e99fdSEd Maste {
464c43e99fdSEd Maste   struct kill *tmp;
465c43e99fdSEd Maste   if ((tmp = malloc(sizeof(struct kill))) == NULL) {
466c43e99fdSEd Maste     event_warn("%s: malloc", __func__);
467c43e99fdSEd Maste     return (NULL);
468c43e99fdSEd Maste   }
469c43e99fdSEd Maste   tmp->base = &kill_base__;
470c43e99fdSEd Maste 
471c43e99fdSEd Maste   tmp->weapon_data = NULL;
472c43e99fdSEd Maste   tmp->weapon_set = 0;
473c43e99fdSEd Maste 
474c43e99fdSEd Maste   tmp->action_data = NULL;
475c43e99fdSEd Maste   tmp->action_set = 0;
476c43e99fdSEd Maste 
477c43e99fdSEd Maste   tmp->how_often_data = NULL;
478c43e99fdSEd Maste   tmp->how_often_length = 0;
479c43e99fdSEd Maste   tmp->how_often_num_allocated = 0;
480c43e99fdSEd Maste   tmp->how_often_set = 0;
481c43e99fdSEd Maste 
482c43e99fdSEd Maste   return (tmp);
483c43e99fdSEd Maste }
484c43e99fdSEd Maste 
485c43e99fdSEd Maste 
486c43e99fdSEd Maste 
487c43e99fdSEd Maste static int
kill_how_often_expand_to_hold_more(struct kill * msg)488c43e99fdSEd Maste kill_how_often_expand_to_hold_more(struct kill *msg)
489c43e99fdSEd Maste {
490c43e99fdSEd Maste   int tobe_allocated = msg->how_often_num_allocated;
491c43e99fdSEd Maste   ev_uint32_t* new_data = NULL;
492c43e99fdSEd Maste   tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
493c43e99fdSEd Maste   new_data = (ev_uint32_t*) realloc(msg->how_often_data,
494c43e99fdSEd Maste       tobe_allocated * sizeof(ev_uint32_t));
495c43e99fdSEd Maste   if (new_data == NULL)
496c43e99fdSEd Maste     return -1;
497c43e99fdSEd Maste   msg->how_often_data = new_data;
498c43e99fdSEd Maste   msg->how_often_num_allocated = tobe_allocated;
499*b50261e2SCy Schubert   return 0;
500*b50261e2SCy Schubert }
501c43e99fdSEd Maste 
502c43e99fdSEd Maste ev_uint32_t *
kill_how_often_add(struct kill * msg,const ev_uint32_t value)503c43e99fdSEd Maste kill_how_often_add(struct kill *msg, const ev_uint32_t value)
504c43e99fdSEd Maste {
505c43e99fdSEd Maste   if (++msg->how_often_length >= msg->how_often_num_allocated) {
506c43e99fdSEd Maste     if (kill_how_often_expand_to_hold_more(msg)<0)
507c43e99fdSEd Maste       goto error;
508c43e99fdSEd Maste   }
509c43e99fdSEd Maste   msg->how_often_data[msg->how_often_length - 1] = value;
510c43e99fdSEd Maste   msg->how_often_set = 1;
511c43e99fdSEd Maste   return &(msg->how_often_data[msg->how_often_length - 1]);
512c43e99fdSEd Maste error:
513c43e99fdSEd Maste   --msg->how_often_length;
514c43e99fdSEd Maste   return (NULL);
515c43e99fdSEd Maste }
516c43e99fdSEd Maste 
517c43e99fdSEd Maste int
kill_weapon_assign(struct kill * msg,const char * value)518c43e99fdSEd Maste kill_weapon_assign(struct kill *msg,
519c43e99fdSEd Maste     const char * value)
520c43e99fdSEd Maste {
521c43e99fdSEd Maste   if (msg->weapon_data != NULL)
522c43e99fdSEd Maste     free(msg->weapon_data);
523c43e99fdSEd Maste   if ((msg->weapon_data = strdup(value)) == NULL)
524c43e99fdSEd Maste     return (-1);
525c43e99fdSEd Maste   msg->weapon_set = 1;
526c43e99fdSEd Maste   return (0);
527c43e99fdSEd Maste }
528c43e99fdSEd Maste 
529c43e99fdSEd Maste int
kill_action_assign(struct kill * msg,const char * value)530c43e99fdSEd Maste kill_action_assign(struct kill *msg,
531c43e99fdSEd Maste     const char * value)
532c43e99fdSEd Maste {
533c43e99fdSEd Maste   if (msg->action_data != NULL)
534c43e99fdSEd Maste     free(msg->action_data);
535c43e99fdSEd Maste   if ((msg->action_data = strdup(value)) == NULL)
536c43e99fdSEd Maste     return (-1);
537c43e99fdSEd Maste   msg->action_set = 1;
538c43e99fdSEd Maste   return (0);
539c43e99fdSEd Maste }
540c43e99fdSEd Maste 
541c43e99fdSEd Maste int
kill_how_often_assign(struct kill * msg,int off,const ev_uint32_t value)542c43e99fdSEd Maste kill_how_often_assign(struct kill *msg, int off,
543c43e99fdSEd Maste   const ev_uint32_t value)
544c43e99fdSEd Maste {
545c43e99fdSEd Maste   if (!msg->how_often_set || off < 0 || off >= msg->how_often_length)
546c43e99fdSEd Maste     return (-1);
547c43e99fdSEd Maste 
548c43e99fdSEd Maste   {
549c43e99fdSEd Maste     msg->how_often_data[off] = value;
550c43e99fdSEd Maste   }
551c43e99fdSEd Maste   return (0);
552c43e99fdSEd Maste }
553c43e99fdSEd Maste 
554c43e99fdSEd Maste int
kill_weapon_get(struct kill * msg,char ** value)555c43e99fdSEd Maste kill_weapon_get(struct kill *msg, char * *value)
556c43e99fdSEd Maste {
557c43e99fdSEd Maste   if (msg->weapon_set != 1)
558c43e99fdSEd Maste     return (-1);
559c43e99fdSEd Maste   *value = msg->weapon_data;
560c43e99fdSEd Maste   return (0);
561c43e99fdSEd Maste }
562c43e99fdSEd Maste 
563c43e99fdSEd Maste int
kill_action_get(struct kill * msg,char ** value)564c43e99fdSEd Maste kill_action_get(struct kill *msg, char * *value)
565c43e99fdSEd Maste {
566c43e99fdSEd Maste   if (msg->action_set != 1)
567c43e99fdSEd Maste     return (-1);
568c43e99fdSEd Maste   *value = msg->action_data;
569c43e99fdSEd Maste   return (0);
570c43e99fdSEd Maste }
571c43e99fdSEd Maste 
572c43e99fdSEd Maste int
kill_how_often_get(struct kill * msg,int offset,ev_uint32_t * value)573c43e99fdSEd Maste kill_how_often_get(struct kill *msg, int offset,
574c43e99fdSEd Maste     ev_uint32_t *value)
575c43e99fdSEd Maste {
576c43e99fdSEd Maste   if (!msg->how_often_set || offset < 0 || offset >= msg->how_often_length)
577c43e99fdSEd Maste     return (-1);
578c43e99fdSEd Maste   *value = msg->how_often_data[offset];
579c43e99fdSEd Maste   return (0);
580c43e99fdSEd Maste }
581c43e99fdSEd Maste 
582c43e99fdSEd Maste void
kill_clear(struct kill * tmp)583c43e99fdSEd Maste kill_clear(struct kill *tmp)
584c43e99fdSEd Maste {
585c43e99fdSEd Maste   if (tmp->weapon_set == 1) {
586c43e99fdSEd Maste     free(tmp->weapon_data);
587c43e99fdSEd Maste     tmp->weapon_data = NULL;
588c43e99fdSEd Maste     tmp->weapon_set = 0;
589c43e99fdSEd Maste   }
590c43e99fdSEd Maste   if (tmp->action_set == 1) {
591c43e99fdSEd Maste     free(tmp->action_data);
592c43e99fdSEd Maste     tmp->action_data = NULL;
593c43e99fdSEd Maste     tmp->action_set = 0;
594c43e99fdSEd Maste   }
595c43e99fdSEd Maste   if (tmp->how_often_set == 1) {
596c43e99fdSEd Maste     free(tmp->how_often_data);
597c43e99fdSEd Maste     tmp->how_often_data = NULL;
598c43e99fdSEd Maste     tmp->how_often_set = 0;
599c43e99fdSEd Maste     tmp->how_often_length = 0;
600c43e99fdSEd Maste     tmp->how_often_num_allocated = 0;
601c43e99fdSEd Maste   }
602c43e99fdSEd Maste }
603c43e99fdSEd Maste 
604c43e99fdSEd Maste void
kill_free(struct kill * tmp)605c43e99fdSEd Maste kill_free(struct kill *tmp)
606c43e99fdSEd Maste {
607c43e99fdSEd Maste   if (tmp->weapon_data != NULL)
608c43e99fdSEd Maste       free (tmp->weapon_data);
609c43e99fdSEd Maste   if (tmp->action_data != NULL)
610c43e99fdSEd Maste       free (tmp->action_data);
611c43e99fdSEd Maste   if (tmp->how_often_set == 1) {
612c43e99fdSEd Maste     free(tmp->how_often_data);
613c43e99fdSEd Maste     tmp->how_often_data = NULL;
614c43e99fdSEd Maste     tmp->how_often_set = 0;
615c43e99fdSEd Maste     tmp->how_often_length = 0;
616c43e99fdSEd Maste     tmp->how_often_num_allocated = 0;
617c43e99fdSEd Maste   }
618c43e99fdSEd Maste   free(tmp->how_often_data);
619c43e99fdSEd Maste   free(tmp);
620c43e99fdSEd Maste }
621c43e99fdSEd Maste 
622c43e99fdSEd Maste void
kill_marshal(struct evbuffer * evbuf,const struct kill * tmp)623c43e99fdSEd Maste kill_marshal(struct evbuffer *evbuf, const struct kill *tmp) {
624c43e99fdSEd Maste   evtag_marshal_string(evbuf, KILL_WEAPON, tmp->weapon_data);
625c43e99fdSEd Maste   evtag_marshal_string(evbuf, KILL_ACTION, tmp->action_data);
626c43e99fdSEd Maste   if (tmp->how_often_set) {
627c43e99fdSEd Maste     {
628c43e99fdSEd Maste       int i;
629c43e99fdSEd Maste       for (i = 0; i < tmp->how_often_length; ++i) {
630c43e99fdSEd Maste     evtag_marshal_int(evbuf, KILL_HOW_OFTEN, tmp->how_often_data[i]);
631c43e99fdSEd Maste       }
632c43e99fdSEd Maste     }
633c43e99fdSEd Maste   }
634c43e99fdSEd Maste }
635c43e99fdSEd Maste 
636c43e99fdSEd Maste int
kill_unmarshal(struct kill * tmp,struct evbuffer * evbuf)637c43e99fdSEd Maste kill_unmarshal(struct kill *tmp, struct evbuffer *evbuf)
638c43e99fdSEd Maste {
639c43e99fdSEd Maste   ev_uint32_t tag;
640c43e99fdSEd Maste   while (evbuffer_get_length(evbuf) > 0) {
641c43e99fdSEd Maste     if (evtag_peek(evbuf, &tag) == -1)
642c43e99fdSEd Maste       return (-1);
643c43e99fdSEd Maste     switch (tag) {
644c43e99fdSEd Maste 
645c43e99fdSEd Maste       case KILL_WEAPON:
646c43e99fdSEd Maste         if (tmp->weapon_set)
647c43e99fdSEd Maste           return (-1);
648c43e99fdSEd Maste         if (evtag_unmarshal_string(evbuf, KILL_WEAPON, &tmp->weapon_data) == -1) {
649c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal weapon", __func__);
650c43e99fdSEd Maste           return (-1);
651c43e99fdSEd Maste         }
652c43e99fdSEd Maste         tmp->weapon_set = 1;
653c43e99fdSEd Maste         break;
654c43e99fdSEd Maste       case KILL_ACTION:
655c43e99fdSEd Maste         if (tmp->action_set)
656c43e99fdSEd Maste           return (-1);
657c43e99fdSEd Maste         if (evtag_unmarshal_string(evbuf, KILL_ACTION, &tmp->action_data) == -1) {
658c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal action", __func__);
659c43e99fdSEd Maste           return (-1);
660c43e99fdSEd Maste         }
661c43e99fdSEd Maste         tmp->action_set = 1;
662c43e99fdSEd Maste         break;
663c43e99fdSEd Maste       case KILL_HOW_OFTEN:
664c43e99fdSEd Maste         if (tmp->how_often_length >= tmp->how_often_num_allocated &&
665c43e99fdSEd Maste             kill_how_often_expand_to_hold_more(tmp) < 0) {
666c43e99fdSEd Maste           puts("HEY NOW");
667c43e99fdSEd Maste           return (-1);
668c43e99fdSEd Maste         }
669c43e99fdSEd Maste         if (evtag_unmarshal_int(evbuf, KILL_HOW_OFTEN, &tmp->how_often_data[tmp->how_often_length]) == -1) {
670c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal how_often", __func__);
671c43e99fdSEd Maste           return (-1);
672c43e99fdSEd Maste         }
673c43e99fdSEd Maste         ++tmp->how_often_length;
674c43e99fdSEd Maste         tmp->how_often_set = 1;
675c43e99fdSEd Maste         break;
676c43e99fdSEd Maste       default:
677c43e99fdSEd Maste         return -1;
678c43e99fdSEd Maste     }
679c43e99fdSEd Maste   }
680c43e99fdSEd Maste 
681c43e99fdSEd Maste   if (kill_complete(tmp) == -1)
682c43e99fdSEd Maste     return (-1);
683c43e99fdSEd Maste   return (0);
684c43e99fdSEd Maste }
685c43e99fdSEd Maste 
686c43e99fdSEd Maste int
kill_complete(struct kill * msg)687c43e99fdSEd Maste kill_complete(struct kill *msg)
688c43e99fdSEd Maste {
689c43e99fdSEd Maste   if (!msg->weapon_set)
690c43e99fdSEd Maste     return (-1);
691c43e99fdSEd Maste   if (!msg->action_set)
692c43e99fdSEd Maste     return (-1);
693c43e99fdSEd Maste   return (0);
694c43e99fdSEd Maste }
695c43e99fdSEd Maste 
696c43e99fdSEd Maste int
evtag_unmarshal_kill(struct evbuffer * evbuf,ev_uint32_t need_tag,struct kill * msg)697*b50261e2SCy Schubert evtag_unmarshal_kill(struct evbuffer *evbuf, ev_uint32_t need_tag,
698*b50261e2SCy Schubert   struct kill *msg)
699c43e99fdSEd Maste {
700c43e99fdSEd Maste   ev_uint32_t tag;
701c43e99fdSEd Maste   int res = -1;
702c43e99fdSEd Maste 
703c43e99fdSEd Maste   struct evbuffer *tmp = evbuffer_new();
704c43e99fdSEd Maste 
705c43e99fdSEd Maste   if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
706c43e99fdSEd Maste     goto error;
707c43e99fdSEd Maste 
708c43e99fdSEd Maste   if (kill_unmarshal(msg, tmp) == -1)
709c43e99fdSEd Maste     goto error;
710c43e99fdSEd Maste 
711c43e99fdSEd Maste   res = 0;
712c43e99fdSEd Maste 
713c43e99fdSEd Maste  error:
714c43e99fdSEd Maste   evbuffer_free(tmp);
715c43e99fdSEd Maste   return (res);
716c43e99fdSEd Maste }
717c43e99fdSEd Maste 
718c43e99fdSEd Maste void
evtag_marshal_kill(struct evbuffer * evbuf,ev_uint32_t tag,const struct kill * msg)719*b50261e2SCy Schubert evtag_marshal_kill(struct evbuffer *evbuf, ev_uint32_t tag,
720*b50261e2SCy Schubert     const struct kill *msg)
721c43e99fdSEd Maste {
722c43e99fdSEd Maste   struct evbuffer *buf_ = evbuffer_new();
723c43e99fdSEd Maste   assert(buf_ != NULL);
724c43e99fdSEd Maste   kill_marshal(buf_, msg);
725c43e99fdSEd Maste   evtag_marshal_buffer(evbuf, tag, buf_);
726c43e99fdSEd Maste   evbuffer_free(buf_);
727c43e99fdSEd Maste }
728c43e99fdSEd Maste 
729c43e99fdSEd Maste /*
730c43e99fdSEd Maste  * Implementation of run
731c43e99fdSEd Maste  */
732c43e99fdSEd Maste 
733c43e99fdSEd Maste static struct run_access_ run_base__ = {
734c43e99fdSEd Maste   run_how_assign,
735c43e99fdSEd Maste   run_how_get,
736c43e99fdSEd Maste   run_some_bytes_assign,
737c43e99fdSEd Maste   run_some_bytes_get,
738c43e99fdSEd Maste   run_fixed_bytes_assign,
739c43e99fdSEd Maste   run_fixed_bytes_get,
740c43e99fdSEd Maste   run_notes_assign,
741c43e99fdSEd Maste   run_notes_get,
742c43e99fdSEd Maste   run_notes_add,
743c43e99fdSEd Maste   run_large_number_assign,
744c43e99fdSEd Maste   run_large_number_get,
745c43e99fdSEd Maste   run_other_numbers_assign,
746c43e99fdSEd Maste   run_other_numbers_get,
747c43e99fdSEd Maste   run_other_numbers_add,
748c43e99fdSEd Maste };
749c43e99fdSEd Maste 
750c43e99fdSEd Maste struct run *
run_new(void)751c43e99fdSEd Maste run_new(void)
752c43e99fdSEd Maste {
753c43e99fdSEd Maste   return run_new_with_arg(NULL);
754c43e99fdSEd Maste }
755c43e99fdSEd Maste 
756c43e99fdSEd Maste struct run *
run_new_with_arg(void * unused)757c43e99fdSEd Maste run_new_with_arg(void *unused)
758c43e99fdSEd Maste {
759c43e99fdSEd Maste   struct run *tmp;
760c43e99fdSEd Maste   if ((tmp = malloc(sizeof(struct run))) == NULL) {
761c43e99fdSEd Maste     event_warn("%s: malloc", __func__);
762c43e99fdSEd Maste     return (NULL);
763c43e99fdSEd Maste   }
764c43e99fdSEd Maste   tmp->base = &run_base__;
765c43e99fdSEd Maste 
766c43e99fdSEd Maste   tmp->how_data = NULL;
767c43e99fdSEd Maste   tmp->how_set = 0;
768c43e99fdSEd Maste 
769c43e99fdSEd Maste   tmp->some_bytes_data = NULL;
770c43e99fdSEd Maste   tmp->some_bytes_length = 0;
771c43e99fdSEd Maste   tmp->some_bytes_set = 0;
772c43e99fdSEd Maste 
773c43e99fdSEd Maste   memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
774c43e99fdSEd Maste   tmp->fixed_bytes_set = 0;
775c43e99fdSEd Maste 
776c43e99fdSEd Maste   tmp->notes_data = NULL;
777c43e99fdSEd Maste   tmp->notes_length = 0;
778c43e99fdSEd Maste   tmp->notes_num_allocated = 0;
779c43e99fdSEd Maste   tmp->notes_set = 0;
780c43e99fdSEd Maste 
781c43e99fdSEd Maste   tmp->large_number_data = 0;
782c43e99fdSEd Maste   tmp->large_number_set = 0;
783c43e99fdSEd Maste 
784c43e99fdSEd Maste   tmp->other_numbers_data = NULL;
785c43e99fdSEd Maste   tmp->other_numbers_length = 0;
786c43e99fdSEd Maste   tmp->other_numbers_num_allocated = 0;
787c43e99fdSEd Maste   tmp->other_numbers_set = 0;
788c43e99fdSEd Maste 
789c43e99fdSEd Maste   return (tmp);
790c43e99fdSEd Maste }
791c43e99fdSEd Maste 
792c43e99fdSEd Maste 
793c43e99fdSEd Maste 
794c43e99fdSEd Maste 
795c43e99fdSEd Maste static int
run_notes_expand_to_hold_more(struct run * msg)796c43e99fdSEd Maste run_notes_expand_to_hold_more(struct run *msg)
797c43e99fdSEd Maste {
798c43e99fdSEd Maste   int tobe_allocated = msg->notes_num_allocated;
799c43e99fdSEd Maste   char ** new_data = NULL;
800c43e99fdSEd Maste   tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
801c43e99fdSEd Maste   new_data = (char **) realloc(msg->notes_data,
802c43e99fdSEd Maste       tobe_allocated * sizeof(char *));
803c43e99fdSEd Maste   if (new_data == NULL)
804c43e99fdSEd Maste     return -1;
805c43e99fdSEd Maste   msg->notes_data = new_data;
806c43e99fdSEd Maste   msg->notes_num_allocated = tobe_allocated;
807*b50261e2SCy Schubert   return 0;
808*b50261e2SCy Schubert }
809c43e99fdSEd Maste 
810c43e99fdSEd Maste char * *
run_notes_add(struct run * msg,const char * value)811c43e99fdSEd Maste run_notes_add(struct run *msg, const char * value)
812c43e99fdSEd Maste {
813c43e99fdSEd Maste   if (++msg->notes_length >= msg->notes_num_allocated) {
814c43e99fdSEd Maste     if (run_notes_expand_to_hold_more(msg)<0)
815c43e99fdSEd Maste       goto error;
816c43e99fdSEd Maste   }
817c43e99fdSEd Maste   if (value != NULL) {
818c43e99fdSEd Maste     msg->notes_data[msg->notes_length - 1] = strdup(value);
819c43e99fdSEd Maste     if (msg->notes_data[msg->notes_length - 1] == NULL) {
820c43e99fdSEd Maste       goto error;
821c43e99fdSEd Maste     }
822c43e99fdSEd Maste   } else {
823c43e99fdSEd Maste     msg->notes_data[msg->notes_length - 1] = NULL;
824c43e99fdSEd Maste   }
825c43e99fdSEd Maste   msg->notes_set = 1;
826c43e99fdSEd Maste   return &(msg->notes_data[msg->notes_length - 1]);
827c43e99fdSEd Maste error:
828c43e99fdSEd Maste   --msg->notes_length;
829c43e99fdSEd Maste   return (NULL);
830c43e99fdSEd Maste }
831c43e99fdSEd Maste 
832c43e99fdSEd Maste 
833c43e99fdSEd Maste static int
run_other_numbers_expand_to_hold_more(struct run * msg)834c43e99fdSEd Maste run_other_numbers_expand_to_hold_more(struct run *msg)
835c43e99fdSEd Maste {
836c43e99fdSEd Maste   int tobe_allocated = msg->other_numbers_num_allocated;
837c43e99fdSEd Maste   ev_uint32_t* new_data = NULL;
838c43e99fdSEd Maste   tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
839c43e99fdSEd Maste   new_data = (ev_uint32_t*) realloc(msg->other_numbers_data,
840c43e99fdSEd Maste       tobe_allocated * sizeof(ev_uint32_t));
841c43e99fdSEd Maste   if (new_data == NULL)
842c43e99fdSEd Maste     return -1;
843c43e99fdSEd Maste   msg->other_numbers_data = new_data;
844c43e99fdSEd Maste   msg->other_numbers_num_allocated = tobe_allocated;
845*b50261e2SCy Schubert   return 0;
846*b50261e2SCy Schubert }
847c43e99fdSEd Maste 
848c43e99fdSEd Maste ev_uint32_t *
run_other_numbers_add(struct run * msg,const ev_uint32_t value)849c43e99fdSEd Maste run_other_numbers_add(struct run *msg, const ev_uint32_t value)
850c43e99fdSEd Maste {
851c43e99fdSEd Maste   if (++msg->other_numbers_length >= msg->other_numbers_num_allocated) {
852c43e99fdSEd Maste     if (run_other_numbers_expand_to_hold_more(msg)<0)
853c43e99fdSEd Maste       goto error;
854c43e99fdSEd Maste   }
855c43e99fdSEd Maste   msg->other_numbers_data[msg->other_numbers_length - 1] = value;
856c43e99fdSEd Maste   msg->other_numbers_set = 1;
857c43e99fdSEd Maste   return &(msg->other_numbers_data[msg->other_numbers_length - 1]);
858c43e99fdSEd Maste error:
859c43e99fdSEd Maste   --msg->other_numbers_length;
860c43e99fdSEd Maste   return (NULL);
861c43e99fdSEd Maste }
862c43e99fdSEd Maste 
863c43e99fdSEd Maste int
run_how_assign(struct run * msg,const char * value)864c43e99fdSEd Maste run_how_assign(struct run *msg,
865c43e99fdSEd Maste     const char * value)
866c43e99fdSEd Maste {
867c43e99fdSEd Maste   if (msg->how_data != NULL)
868c43e99fdSEd Maste     free(msg->how_data);
869c43e99fdSEd Maste   if ((msg->how_data = strdup(value)) == NULL)
870c43e99fdSEd Maste     return (-1);
871c43e99fdSEd Maste   msg->how_set = 1;
872c43e99fdSEd Maste   return (0);
873c43e99fdSEd Maste }
874c43e99fdSEd Maste 
875c43e99fdSEd Maste int
run_some_bytes_assign(struct run * msg,const ev_uint8_t * value,ev_uint32_t len)876c43e99fdSEd Maste run_some_bytes_assign(struct run *msg, const ev_uint8_t * value, ev_uint32_t len)
877c43e99fdSEd Maste {
878c43e99fdSEd Maste   if (msg->some_bytes_data != NULL)
879c43e99fdSEd Maste     free (msg->some_bytes_data);
880c43e99fdSEd Maste   msg->some_bytes_data = malloc(len);
881c43e99fdSEd Maste   if (msg->some_bytes_data == NULL)
882c43e99fdSEd Maste     return (-1);
883c43e99fdSEd Maste   msg->some_bytes_set = 1;
884c43e99fdSEd Maste   msg->some_bytes_length = len;
885c43e99fdSEd Maste   memcpy(msg->some_bytes_data, value, len);
886c43e99fdSEd Maste   return (0);
887c43e99fdSEd Maste }
888c43e99fdSEd Maste 
889c43e99fdSEd Maste int
run_fixed_bytes_assign(struct run * msg,const ev_uint8_t * value)890c43e99fdSEd Maste run_fixed_bytes_assign(struct run *msg, const ev_uint8_t *value)
891c43e99fdSEd Maste {
892c43e99fdSEd Maste   msg->fixed_bytes_set = 1;
893c43e99fdSEd Maste   memcpy(msg->fixed_bytes_data, value, 24);
894c43e99fdSEd Maste   return (0);
895c43e99fdSEd Maste }
896c43e99fdSEd Maste 
897c43e99fdSEd Maste int
run_notes_assign(struct run * msg,int off,const char * value)898c43e99fdSEd Maste run_notes_assign(struct run *msg, int off,
899c43e99fdSEd Maste   const char * value)
900c43e99fdSEd Maste {
901c43e99fdSEd Maste   if (!msg->notes_set || off < 0 || off >= msg->notes_length)
902c43e99fdSEd Maste     return (-1);
903c43e99fdSEd Maste 
904c43e99fdSEd Maste   {
905c43e99fdSEd Maste     if (msg->notes_data[off] != NULL)
906c43e99fdSEd Maste       free(msg->notes_data[off]);
907c43e99fdSEd Maste     msg->notes_data[off] = strdup(value);
908c43e99fdSEd Maste     if (msg->notes_data[off] == NULL) {
909c43e99fdSEd Maste       event_warnx("%s: strdup", __func__);
910c43e99fdSEd Maste       return (-1);
911c43e99fdSEd Maste     }
912c43e99fdSEd Maste   }
913c43e99fdSEd Maste   return (0);
914c43e99fdSEd Maste }
915c43e99fdSEd Maste 
916c43e99fdSEd Maste int
run_large_number_assign(struct run * msg,const ev_uint64_t value)917c43e99fdSEd Maste run_large_number_assign(struct run *msg, const ev_uint64_t value)
918c43e99fdSEd Maste {
919c43e99fdSEd Maste   msg->large_number_set = 1;
920c43e99fdSEd Maste   msg->large_number_data = value;
921c43e99fdSEd Maste   return (0);
922c43e99fdSEd Maste }
923c43e99fdSEd Maste 
924c43e99fdSEd Maste int
run_other_numbers_assign(struct run * msg,int off,const ev_uint32_t value)925c43e99fdSEd Maste run_other_numbers_assign(struct run *msg, int off,
926c43e99fdSEd Maste   const ev_uint32_t value)
927c43e99fdSEd Maste {
928c43e99fdSEd Maste   if (!msg->other_numbers_set || off < 0 || off >= msg->other_numbers_length)
929c43e99fdSEd Maste     return (-1);
930c43e99fdSEd Maste 
931c43e99fdSEd Maste   {
932c43e99fdSEd Maste     msg->other_numbers_data[off] = value;
933c43e99fdSEd Maste   }
934c43e99fdSEd Maste   return (0);
935c43e99fdSEd Maste }
936c43e99fdSEd Maste 
937c43e99fdSEd Maste int
run_how_get(struct run * msg,char ** value)938c43e99fdSEd Maste run_how_get(struct run *msg, char * *value)
939c43e99fdSEd Maste {
940c43e99fdSEd Maste   if (msg->how_set != 1)
941c43e99fdSEd Maste     return (-1);
942c43e99fdSEd Maste   *value = msg->how_data;
943c43e99fdSEd Maste   return (0);
944c43e99fdSEd Maste }
945c43e99fdSEd Maste 
946c43e99fdSEd Maste int
run_some_bytes_get(struct run * msg,ev_uint8_t ** value,ev_uint32_t * plen)947c43e99fdSEd Maste run_some_bytes_get(struct run *msg, ev_uint8_t * *value, ev_uint32_t *plen)
948c43e99fdSEd Maste {
949c43e99fdSEd Maste   if (msg->some_bytes_set != 1)
950c43e99fdSEd Maste     return (-1);
951c43e99fdSEd Maste   *value = msg->some_bytes_data;
952c43e99fdSEd Maste   *plen = msg->some_bytes_length;
953c43e99fdSEd Maste   return (0);
954c43e99fdSEd Maste }
955c43e99fdSEd Maste 
956c43e99fdSEd Maste int
run_fixed_bytes_get(struct run * msg,ev_uint8_t ** value)957c43e99fdSEd Maste run_fixed_bytes_get(struct run *msg, ev_uint8_t **value)
958c43e99fdSEd Maste {
959c43e99fdSEd Maste   if (msg->fixed_bytes_set != 1)
960c43e99fdSEd Maste     return (-1);
961c43e99fdSEd Maste   *value = msg->fixed_bytes_data;
962c43e99fdSEd Maste   return (0);
963c43e99fdSEd Maste }
964c43e99fdSEd Maste 
965c43e99fdSEd Maste int
run_notes_get(struct run * msg,int offset,char ** value)966c43e99fdSEd Maste run_notes_get(struct run *msg, int offset,
967c43e99fdSEd Maste     char * *value)
968c43e99fdSEd Maste {
969c43e99fdSEd Maste   if (!msg->notes_set || offset < 0 || offset >= msg->notes_length)
970c43e99fdSEd Maste     return (-1);
971c43e99fdSEd Maste   *value = msg->notes_data[offset];
972c43e99fdSEd Maste   return (0);
973c43e99fdSEd Maste }
974c43e99fdSEd Maste 
975c43e99fdSEd Maste int
run_large_number_get(struct run * msg,ev_uint64_t * value)976c43e99fdSEd Maste run_large_number_get(struct run *msg, ev_uint64_t *value)
977c43e99fdSEd Maste {
978c43e99fdSEd Maste   if (msg->large_number_set != 1)
979c43e99fdSEd Maste     return (-1);
980c43e99fdSEd Maste   *value = msg->large_number_data;
981c43e99fdSEd Maste   return (0);
982c43e99fdSEd Maste }
983c43e99fdSEd Maste 
984c43e99fdSEd Maste int
run_other_numbers_get(struct run * msg,int offset,ev_uint32_t * value)985c43e99fdSEd Maste run_other_numbers_get(struct run *msg, int offset,
986c43e99fdSEd Maste     ev_uint32_t *value)
987c43e99fdSEd Maste {
988c43e99fdSEd Maste   if (!msg->other_numbers_set || offset < 0 || offset >= msg->other_numbers_length)
989c43e99fdSEd Maste     return (-1);
990c43e99fdSEd Maste   *value = msg->other_numbers_data[offset];
991c43e99fdSEd Maste   return (0);
992c43e99fdSEd Maste }
993c43e99fdSEd Maste 
994c43e99fdSEd Maste void
run_clear(struct run * tmp)995c43e99fdSEd Maste run_clear(struct run *tmp)
996c43e99fdSEd Maste {
997c43e99fdSEd Maste   if (tmp->how_set == 1) {
998c43e99fdSEd Maste     free(tmp->how_data);
999c43e99fdSEd Maste     tmp->how_data = NULL;
1000c43e99fdSEd Maste     tmp->how_set = 0;
1001c43e99fdSEd Maste   }
1002c43e99fdSEd Maste   if (tmp->some_bytes_set == 1) {
1003c43e99fdSEd Maste     free (tmp->some_bytes_data);
1004c43e99fdSEd Maste     tmp->some_bytes_data = NULL;
1005c43e99fdSEd Maste     tmp->some_bytes_length = 0;
1006c43e99fdSEd Maste     tmp->some_bytes_set = 0;
1007c43e99fdSEd Maste   }
1008c43e99fdSEd Maste   tmp->fixed_bytes_set = 0;
1009c43e99fdSEd Maste   memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
1010c43e99fdSEd Maste   if (tmp->notes_set == 1) {
1011c43e99fdSEd Maste     int i;
1012c43e99fdSEd Maste     for (i = 0; i < tmp->notes_length; ++i) {
1013c43e99fdSEd Maste       if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]);
1014c43e99fdSEd Maste     }
1015c43e99fdSEd Maste     free(tmp->notes_data);
1016c43e99fdSEd Maste     tmp->notes_data = NULL;
1017c43e99fdSEd Maste     tmp->notes_set = 0;
1018c43e99fdSEd Maste     tmp->notes_length = 0;
1019c43e99fdSEd Maste     tmp->notes_num_allocated = 0;
1020c43e99fdSEd Maste   }
1021c43e99fdSEd Maste   tmp->large_number_set = 0;
1022c43e99fdSEd Maste   if (tmp->other_numbers_set == 1) {
1023c43e99fdSEd Maste     free(tmp->other_numbers_data);
1024c43e99fdSEd Maste     tmp->other_numbers_data = NULL;
1025c43e99fdSEd Maste     tmp->other_numbers_set = 0;
1026c43e99fdSEd Maste     tmp->other_numbers_length = 0;
1027c43e99fdSEd Maste     tmp->other_numbers_num_allocated = 0;
1028c43e99fdSEd Maste   }
1029c43e99fdSEd Maste }
1030c43e99fdSEd Maste 
1031c43e99fdSEd Maste void
run_free(struct run * tmp)1032c43e99fdSEd Maste run_free(struct run *tmp)
1033c43e99fdSEd Maste {
1034c43e99fdSEd Maste   if (tmp->how_data != NULL)
1035c43e99fdSEd Maste       free (tmp->how_data);
1036c43e99fdSEd Maste   if (tmp->some_bytes_data != NULL)
1037c43e99fdSEd Maste       free(tmp->some_bytes_data);
1038c43e99fdSEd Maste   if (tmp->notes_set == 1) {
1039c43e99fdSEd Maste     int i;
1040c43e99fdSEd Maste     for (i = 0; i < tmp->notes_length; ++i) {
1041c43e99fdSEd Maste       if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]);
1042c43e99fdSEd Maste     }
1043c43e99fdSEd Maste     free(tmp->notes_data);
1044c43e99fdSEd Maste     tmp->notes_data = NULL;
1045c43e99fdSEd Maste     tmp->notes_set = 0;
1046c43e99fdSEd Maste     tmp->notes_length = 0;
1047c43e99fdSEd Maste     tmp->notes_num_allocated = 0;
1048c43e99fdSEd Maste   }
1049c43e99fdSEd Maste   free(tmp->notes_data);
1050c43e99fdSEd Maste   if (tmp->other_numbers_set == 1) {
1051c43e99fdSEd Maste     free(tmp->other_numbers_data);
1052c43e99fdSEd Maste     tmp->other_numbers_data = NULL;
1053c43e99fdSEd Maste     tmp->other_numbers_set = 0;
1054c43e99fdSEd Maste     tmp->other_numbers_length = 0;
1055c43e99fdSEd Maste     tmp->other_numbers_num_allocated = 0;
1056c43e99fdSEd Maste   }
1057c43e99fdSEd Maste   free(tmp->other_numbers_data);
1058c43e99fdSEd Maste   free(tmp);
1059c43e99fdSEd Maste }
1060c43e99fdSEd Maste 
1061c43e99fdSEd Maste void
run_marshal(struct evbuffer * evbuf,const struct run * tmp)1062c43e99fdSEd Maste run_marshal(struct evbuffer *evbuf, const struct run *tmp) {
1063c43e99fdSEd Maste   evtag_marshal_string(evbuf, RUN_HOW, tmp->how_data);
1064c43e99fdSEd Maste   if (tmp->some_bytes_set) {
1065c43e99fdSEd Maste     evtag_marshal(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length);
1066c43e99fdSEd Maste   }
1067c43e99fdSEd Maste   evtag_marshal(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24));
1068c43e99fdSEd Maste   if (tmp->notes_set) {
1069c43e99fdSEd Maste     {
1070c43e99fdSEd Maste       int i;
1071c43e99fdSEd Maste       for (i = 0; i < tmp->notes_length; ++i) {
1072c43e99fdSEd Maste     evtag_marshal_string(evbuf, RUN_NOTES, tmp->notes_data[i]);
1073c43e99fdSEd Maste       }
1074c43e99fdSEd Maste     }
1075c43e99fdSEd Maste   }
1076c43e99fdSEd Maste   if (tmp->large_number_set) {
1077c43e99fdSEd Maste     evtag_marshal_int64(evbuf, RUN_LARGE_NUMBER, tmp->large_number_data);
1078c43e99fdSEd Maste   }
1079c43e99fdSEd Maste   if (tmp->other_numbers_set) {
1080c43e99fdSEd Maste     {
1081c43e99fdSEd Maste       int i;
1082c43e99fdSEd Maste       for (i = 0; i < tmp->other_numbers_length; ++i) {
1083c43e99fdSEd Maste     evtag_marshal_int(evbuf, RUN_OTHER_NUMBERS, tmp->other_numbers_data[i]);
1084c43e99fdSEd Maste       }
1085c43e99fdSEd Maste     }
1086c43e99fdSEd Maste   }
1087c43e99fdSEd Maste }
1088c43e99fdSEd Maste 
1089c43e99fdSEd Maste int
run_unmarshal(struct run * tmp,struct evbuffer * evbuf)1090c43e99fdSEd Maste run_unmarshal(struct run *tmp, struct evbuffer *evbuf)
1091c43e99fdSEd Maste {
1092c43e99fdSEd Maste   ev_uint32_t tag;
1093c43e99fdSEd Maste   while (evbuffer_get_length(evbuf) > 0) {
1094c43e99fdSEd Maste     if (evtag_peek(evbuf, &tag) == -1)
1095c43e99fdSEd Maste       return (-1);
1096c43e99fdSEd Maste     switch (tag) {
1097c43e99fdSEd Maste 
1098c43e99fdSEd Maste       case RUN_HOW:
1099c43e99fdSEd Maste         if (tmp->how_set)
1100c43e99fdSEd Maste           return (-1);
1101c43e99fdSEd Maste         if (evtag_unmarshal_string(evbuf, RUN_HOW, &tmp->how_data) == -1) {
1102c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal how", __func__);
1103c43e99fdSEd Maste           return (-1);
1104c43e99fdSEd Maste         }
1105c43e99fdSEd Maste         tmp->how_set = 1;
1106c43e99fdSEd Maste         break;
1107c43e99fdSEd Maste       case RUN_SOME_BYTES:
1108c43e99fdSEd Maste         if (tmp->some_bytes_set)
1109c43e99fdSEd Maste           return (-1);
1110c43e99fdSEd Maste         if (evtag_payload_length(evbuf, &tmp->some_bytes_length) == -1)
1111c43e99fdSEd Maste           return (-1);
1112c43e99fdSEd Maste         if (tmp->some_bytes_length > evbuffer_get_length(evbuf))
1113c43e99fdSEd Maste           return (-1);
1114c43e99fdSEd Maste         if ((tmp->some_bytes_data = malloc(tmp->some_bytes_length)) == NULL)
1115c43e99fdSEd Maste           return (-1);
1116c43e99fdSEd Maste         if (evtag_unmarshal_fixed(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length) == -1) {
1117c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal some_bytes", __func__);
1118c43e99fdSEd Maste           return (-1);
1119c43e99fdSEd Maste         }
1120c43e99fdSEd Maste         tmp->some_bytes_set = 1;
1121c43e99fdSEd Maste         break;
1122c43e99fdSEd Maste       case RUN_FIXED_BYTES:
1123c43e99fdSEd Maste         if (tmp->fixed_bytes_set)
1124c43e99fdSEd Maste           return (-1);
1125c43e99fdSEd Maste         if (evtag_unmarshal_fixed(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24)) == -1) {
1126c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal fixed_bytes", __func__);
1127c43e99fdSEd Maste           return (-1);
1128c43e99fdSEd Maste         }
1129c43e99fdSEd Maste         tmp->fixed_bytes_set = 1;
1130c43e99fdSEd Maste         break;
1131c43e99fdSEd Maste       case RUN_NOTES:
1132c43e99fdSEd Maste         if (tmp->notes_length >= tmp->notes_num_allocated &&
1133c43e99fdSEd Maste             run_notes_expand_to_hold_more(tmp) < 0) {
1134c43e99fdSEd Maste           puts("HEY NOW");
1135c43e99fdSEd Maste           return (-1);
1136c43e99fdSEd Maste         }
1137c43e99fdSEd Maste         if (evtag_unmarshal_string(evbuf, RUN_NOTES, &tmp->notes_data[tmp->notes_length]) == -1) {
1138c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal notes", __func__);
1139c43e99fdSEd Maste           return (-1);
1140c43e99fdSEd Maste         }
1141c43e99fdSEd Maste         ++tmp->notes_length;
1142c43e99fdSEd Maste         tmp->notes_set = 1;
1143c43e99fdSEd Maste         break;
1144c43e99fdSEd Maste       case RUN_LARGE_NUMBER:
1145c43e99fdSEd Maste         if (tmp->large_number_set)
1146c43e99fdSEd Maste           return (-1);
1147c43e99fdSEd Maste         if (evtag_unmarshal_int64(evbuf, RUN_LARGE_NUMBER, &tmp->large_number_data) == -1) {
1148c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal large_number", __func__);
1149c43e99fdSEd Maste           return (-1);
1150c43e99fdSEd Maste         }
1151c43e99fdSEd Maste         tmp->large_number_set = 1;
1152c43e99fdSEd Maste         break;
1153c43e99fdSEd Maste       case RUN_OTHER_NUMBERS:
1154c43e99fdSEd Maste         if (tmp->other_numbers_length >= tmp->other_numbers_num_allocated &&
1155c43e99fdSEd Maste             run_other_numbers_expand_to_hold_more(tmp) < 0) {
1156c43e99fdSEd Maste           puts("HEY NOW");
1157c43e99fdSEd Maste           return (-1);
1158c43e99fdSEd Maste         }
1159c43e99fdSEd Maste         if (evtag_unmarshal_int(evbuf, RUN_OTHER_NUMBERS, &tmp->other_numbers_data[tmp->other_numbers_length]) == -1) {
1160c43e99fdSEd Maste           event_warnx("%s: failed to unmarshal other_numbers", __func__);
1161c43e99fdSEd Maste           return (-1);
1162c43e99fdSEd Maste         }
1163c43e99fdSEd Maste         ++tmp->other_numbers_length;
1164c43e99fdSEd Maste         tmp->other_numbers_set = 1;
1165c43e99fdSEd Maste         break;
1166c43e99fdSEd Maste       default:
1167c43e99fdSEd Maste         return -1;
1168c43e99fdSEd Maste     }
1169c43e99fdSEd Maste   }
1170c43e99fdSEd Maste 
1171c43e99fdSEd Maste   if (run_complete(tmp) == -1)
1172c43e99fdSEd Maste     return (-1);
1173c43e99fdSEd Maste   return (0);
1174c43e99fdSEd Maste }
1175c43e99fdSEd Maste 
1176c43e99fdSEd Maste int
run_complete(struct run * msg)1177c43e99fdSEd Maste run_complete(struct run *msg)
1178c43e99fdSEd Maste {
1179c43e99fdSEd Maste   if (!msg->how_set)
1180c43e99fdSEd Maste     return (-1);
1181c43e99fdSEd Maste   if (!msg->fixed_bytes_set)
1182c43e99fdSEd Maste     return (-1);
1183c43e99fdSEd Maste   return (0);
1184c43e99fdSEd Maste }
1185c43e99fdSEd Maste 
1186c43e99fdSEd Maste int
evtag_unmarshal_run(struct evbuffer * evbuf,ev_uint32_t need_tag,struct run * msg)1187*b50261e2SCy Schubert evtag_unmarshal_run(struct evbuffer *evbuf, ev_uint32_t need_tag,
1188*b50261e2SCy Schubert   struct run *msg)
1189c43e99fdSEd Maste {
1190c43e99fdSEd Maste   ev_uint32_t tag;
1191c43e99fdSEd Maste   int res = -1;
1192c43e99fdSEd Maste 
1193c43e99fdSEd Maste   struct evbuffer *tmp = evbuffer_new();
1194c43e99fdSEd Maste 
1195c43e99fdSEd Maste   if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
1196c43e99fdSEd Maste     goto error;
1197c43e99fdSEd Maste 
1198c43e99fdSEd Maste   if (run_unmarshal(msg, tmp) == -1)
1199c43e99fdSEd Maste     goto error;
1200c43e99fdSEd Maste 
1201c43e99fdSEd Maste   res = 0;
1202c43e99fdSEd Maste 
1203c43e99fdSEd Maste  error:
1204c43e99fdSEd Maste   evbuffer_free(tmp);
1205c43e99fdSEd Maste   return (res);
1206c43e99fdSEd Maste }
1207c43e99fdSEd Maste 
1208c43e99fdSEd Maste void
evtag_marshal_run(struct evbuffer * evbuf,ev_uint32_t tag,const struct run * msg)1209*b50261e2SCy Schubert evtag_marshal_run(struct evbuffer *evbuf, ev_uint32_t tag,
1210*b50261e2SCy Schubert     const struct run *msg)
1211c43e99fdSEd Maste {
1212c43e99fdSEd Maste   struct evbuffer *buf_ = evbuffer_new();
1213c43e99fdSEd Maste   assert(buf_ != NULL);
1214c43e99fdSEd Maste   run_marshal(buf_, msg);
1215c43e99fdSEd Maste   evtag_marshal_buffer(evbuf, tag, buf_);
1216c43e99fdSEd Maste   evbuffer_free(buf_);
1217c43e99fdSEd Maste }
1218c43e99fdSEd Maste 
1219