xref: /freebsd/contrib/ntp/sntp/libevent/test/regress.gen.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert 
2052d159aSCy Schubert /*
32d4e511cSCy Schubert  * Automatically generated from /ntpbuild/data/snaps/ntp-stable/sntp/libevent/test/regress.rpc
4052d159aSCy Schubert  * by event_rpcgen.py/0.1.  DO NOT EDIT THIS FILE.
5052d159aSCy Schubert  */
6052d159aSCy Schubert 
7052d159aSCy Schubert #include <stdlib.h>
8052d159aSCy Schubert #include <string.h>
9052d159aSCy Schubert #include <assert.h>
10052d159aSCy Schubert #include <event2/event-config.h>
11052d159aSCy Schubert #include <event2/event.h>
12052d159aSCy Schubert #include <event2/buffer.h>
13052d159aSCy Schubert #include <event2/tag.h>
14052d159aSCy Schubert 
15*a466cc55SCy Schubert #if defined(EVENT__HAVE___func__)
16*a466cc55SCy Schubert # ifndef __func__
17*a466cc55SCy Schubert #  define __func__ __func__
18*a466cc55SCy Schubert # endif
19*a466cc55SCy Schubert #elif defined(EVENT__HAVE___FUNCTION__)
20*a466cc55SCy Schubert # define __func__ __FUNCTION__
21*a466cc55SCy Schubert #else
22*a466cc55SCy Schubert # define __func__ __FILE__
23052d159aSCy Schubert #endif
24052d159aSCy Schubert 
25052d159aSCy Schubert 
26052d159aSCy Schubert #include "regress.gen.h"
27052d159aSCy Schubert 
28052d159aSCy Schubert void event_warn(const char *fmt, ...);
29052d159aSCy Schubert void event_warnx(const char *fmt, ...);
30052d159aSCy Schubert 
31052d159aSCy Schubert /*
32052d159aSCy Schubert  * Implementation of msg
33052d159aSCy Schubert  */
34052d159aSCy Schubert 
35052d159aSCy Schubert static struct msg_access_ msg_base__ = {
36052d159aSCy Schubert   msg_from_name_assign,
37052d159aSCy Schubert   msg_from_name_get,
38052d159aSCy Schubert   msg_to_name_assign,
39052d159aSCy Schubert   msg_to_name_get,
40052d159aSCy Schubert   msg_attack_assign,
41052d159aSCy Schubert   msg_attack_get,
42052d159aSCy Schubert   msg_run_assign,
43052d159aSCy Schubert   msg_run_get,
44052d159aSCy Schubert   msg_run_add,
45052d159aSCy Schubert };
46052d159aSCy Schubert 
47052d159aSCy Schubert struct msg *
msg_new(void)48052d159aSCy Schubert msg_new(void)
49052d159aSCy Schubert {
50052d159aSCy Schubert   return msg_new_with_arg(NULL);
51052d159aSCy Schubert }
52052d159aSCy Schubert 
53052d159aSCy Schubert struct msg *
msg_new_with_arg(void * unused)54052d159aSCy Schubert msg_new_with_arg(void *unused)
55052d159aSCy Schubert {
56052d159aSCy Schubert   struct msg *tmp;
57052d159aSCy Schubert   if ((tmp = malloc(sizeof(struct msg))) == NULL) {
58052d159aSCy Schubert     event_warn("%s: malloc", __func__);
59052d159aSCy Schubert     return (NULL);
60052d159aSCy Schubert   }
61052d159aSCy Schubert   tmp->base = &msg_base__;
62052d159aSCy Schubert 
63052d159aSCy Schubert   tmp->from_name_data = NULL;
64052d159aSCy Schubert   tmp->from_name_set = 0;
65052d159aSCy Schubert 
66052d159aSCy Schubert   tmp->to_name_data = NULL;
67052d159aSCy Schubert   tmp->to_name_set = 0;
68052d159aSCy Schubert 
69052d159aSCy Schubert   tmp->attack_data = NULL;
70052d159aSCy Schubert   tmp->attack_set = 0;
71052d159aSCy Schubert 
72052d159aSCy Schubert   tmp->run_data = NULL;
73052d159aSCy Schubert   tmp->run_length = 0;
74052d159aSCy Schubert   tmp->run_num_allocated = 0;
75052d159aSCy Schubert   tmp->run_set = 0;
76052d159aSCy Schubert 
77052d159aSCy Schubert   return (tmp);
78052d159aSCy Schubert }
79052d159aSCy Schubert 
80052d159aSCy Schubert 
81052d159aSCy Schubert 
82052d159aSCy Schubert 
83052d159aSCy Schubert static int
msg_run_expand_to_hold_more(struct msg * msg)84052d159aSCy Schubert msg_run_expand_to_hold_more(struct msg *msg)
85052d159aSCy Schubert {
86052d159aSCy Schubert   int tobe_allocated = msg->run_num_allocated;
87052d159aSCy Schubert   struct run** new_data = NULL;
88052d159aSCy Schubert   tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
89052d159aSCy Schubert   new_data = (struct run**) realloc(msg->run_data,
90052d159aSCy Schubert       tobe_allocated * sizeof(struct run*));
91052d159aSCy Schubert   if (new_data == NULL)
92052d159aSCy Schubert     return -1;
93052d159aSCy Schubert   msg->run_data = new_data;
94052d159aSCy Schubert   msg->run_num_allocated = tobe_allocated;
95*a466cc55SCy Schubert   return 0;
96*a466cc55SCy Schubert }
97052d159aSCy Schubert 
98052d159aSCy Schubert struct run*
msg_run_add(struct msg * msg)99052d159aSCy Schubert msg_run_add(struct msg *msg)
100052d159aSCy Schubert {
101052d159aSCy Schubert   if (++msg->run_length >= msg->run_num_allocated) {
102052d159aSCy Schubert     if (msg_run_expand_to_hold_more(msg)<0)
103052d159aSCy Schubert       goto error;
104052d159aSCy Schubert   }
105052d159aSCy Schubert   msg->run_data[msg->run_length - 1] = run_new();
106052d159aSCy Schubert   if (msg->run_data[msg->run_length - 1] == NULL)
107052d159aSCy Schubert     goto error;
108052d159aSCy Schubert   msg->run_set = 1;
109052d159aSCy Schubert   return (msg->run_data[msg->run_length - 1]);
110052d159aSCy Schubert error:
111052d159aSCy Schubert   --msg->run_length;
112052d159aSCy Schubert   return (NULL);
113052d159aSCy Schubert }
114052d159aSCy Schubert 
115052d159aSCy Schubert int
msg_from_name_assign(struct msg * msg,const char * value)116052d159aSCy Schubert msg_from_name_assign(struct msg *msg,
117052d159aSCy Schubert     const char * value)
118052d159aSCy Schubert {
119052d159aSCy Schubert   if (msg->from_name_data != NULL)
120052d159aSCy Schubert     free(msg->from_name_data);
121052d159aSCy Schubert   if ((msg->from_name_data = strdup(value)) == NULL)
122052d159aSCy Schubert     return (-1);
123052d159aSCy Schubert   msg->from_name_set = 1;
124052d159aSCy Schubert   return (0);
125052d159aSCy Schubert }
126052d159aSCy Schubert 
127052d159aSCy Schubert int
msg_to_name_assign(struct msg * msg,const char * value)128052d159aSCy Schubert msg_to_name_assign(struct msg *msg,
129052d159aSCy Schubert     const char * value)
130052d159aSCy Schubert {
131052d159aSCy Schubert   if (msg->to_name_data != NULL)
132052d159aSCy Schubert     free(msg->to_name_data);
133052d159aSCy Schubert   if ((msg->to_name_data = strdup(value)) == NULL)
134052d159aSCy Schubert     return (-1);
135052d159aSCy Schubert   msg->to_name_set = 1;
136052d159aSCy Schubert   return (0);
137052d159aSCy Schubert }
138052d159aSCy Schubert 
139052d159aSCy Schubert int
msg_attack_assign(struct msg * msg,const struct kill * value)140052d159aSCy Schubert msg_attack_assign(struct msg *msg,
141052d159aSCy Schubert     const struct kill* value)
142052d159aSCy Schubert {
143052d159aSCy Schubert    struct evbuffer *tmp = NULL;
144052d159aSCy Schubert    if (msg->attack_set) {
145052d159aSCy Schubert      kill_clear(msg->attack_data);
146052d159aSCy Schubert      msg->attack_set = 0;
147052d159aSCy Schubert    } else {
148052d159aSCy Schubert      msg->attack_data = kill_new();
149052d159aSCy Schubert      if (msg->attack_data == NULL) {
150052d159aSCy Schubert        event_warn("%s: kill_new()", __func__);
151052d159aSCy Schubert        goto error;
152052d159aSCy Schubert      }
153052d159aSCy Schubert    }
154052d159aSCy Schubert    if ((tmp = evbuffer_new()) == NULL) {
155052d159aSCy Schubert      event_warn("%s: evbuffer_new()", __func__);
156052d159aSCy Schubert      goto error;
157052d159aSCy Schubert    }
158052d159aSCy Schubert    kill_marshal(tmp, value);
159052d159aSCy Schubert    if (kill_unmarshal(msg->attack_data, tmp) == -1) {
160052d159aSCy Schubert      event_warnx("%s: kill_unmarshal", __func__);
161052d159aSCy Schubert      goto error;
162052d159aSCy Schubert    }
163052d159aSCy Schubert    msg->attack_set = 1;
164052d159aSCy Schubert    evbuffer_free(tmp);
165052d159aSCy Schubert    return (0);
166052d159aSCy Schubert  error:
167052d159aSCy Schubert    if (tmp != NULL)
168052d159aSCy Schubert      evbuffer_free(tmp);
169052d159aSCy Schubert    if (msg->attack_data != NULL) {
170052d159aSCy Schubert      kill_free(msg->attack_data);
171052d159aSCy Schubert      msg->attack_data = NULL;
172052d159aSCy Schubert    }
173052d159aSCy Schubert    return (-1);
174052d159aSCy Schubert }
175052d159aSCy Schubert 
176052d159aSCy Schubert int
msg_run_assign(struct msg * msg,int off,const struct run * value)177052d159aSCy Schubert msg_run_assign(struct msg *msg, int off,
178052d159aSCy Schubert   const struct run* value)
179052d159aSCy Schubert {
180052d159aSCy Schubert   if (!msg->run_set || off < 0 || off >= msg->run_length)
181052d159aSCy Schubert     return (-1);
182052d159aSCy Schubert 
183052d159aSCy Schubert   {
184052d159aSCy Schubert     int had_error = 0;
185052d159aSCy Schubert     struct evbuffer *tmp = NULL;
186052d159aSCy Schubert     run_clear(msg->run_data[off]);
187052d159aSCy Schubert     if ((tmp = evbuffer_new()) == NULL) {
188052d159aSCy Schubert       event_warn("%s: evbuffer_new()", __func__);
189052d159aSCy Schubert       had_error = 1;
190052d159aSCy Schubert       goto done;
191052d159aSCy Schubert     }
192052d159aSCy Schubert     run_marshal(tmp, value);
193052d159aSCy Schubert     if (run_unmarshal(msg->run_data[off], tmp) == -1) {
194052d159aSCy Schubert       event_warnx("%s: run_unmarshal", __func__);
195052d159aSCy Schubert       had_error = 1;
196052d159aSCy Schubert       goto done;
197052d159aSCy Schubert     }
198*a466cc55SCy Schubert     done:
199*a466cc55SCy Schubert     if (tmp != NULL)
200052d159aSCy Schubert       evbuffer_free(tmp);
201052d159aSCy Schubert     if (had_error) {
202052d159aSCy Schubert       run_clear(msg->run_data[off]);
203052d159aSCy Schubert       return (-1);
204052d159aSCy Schubert     }
205052d159aSCy Schubert   }
206052d159aSCy Schubert   return (0);
207052d159aSCy Schubert }
208052d159aSCy Schubert 
209052d159aSCy Schubert int
msg_from_name_get(struct msg * msg,char ** value)210052d159aSCy Schubert msg_from_name_get(struct msg *msg, char * *value)
211052d159aSCy Schubert {
212052d159aSCy Schubert   if (msg->from_name_set != 1)
213052d159aSCy Schubert     return (-1);
214052d159aSCy Schubert   *value = msg->from_name_data;
215052d159aSCy Schubert   return (0);
216052d159aSCy Schubert }
217052d159aSCy Schubert 
218052d159aSCy Schubert int
msg_to_name_get(struct msg * msg,char ** value)219052d159aSCy Schubert msg_to_name_get(struct msg *msg, char * *value)
220052d159aSCy Schubert {
221052d159aSCy Schubert   if (msg->to_name_set != 1)
222052d159aSCy Schubert     return (-1);
223052d159aSCy Schubert   *value = msg->to_name_data;
224052d159aSCy Schubert   return (0);
225052d159aSCy Schubert }
226052d159aSCy Schubert 
227052d159aSCy Schubert int
msg_attack_get(struct msg * msg,struct kill ** value)228052d159aSCy Schubert msg_attack_get(struct msg *msg, struct kill* *value)
229052d159aSCy Schubert {
230052d159aSCy Schubert   if (msg->attack_set != 1) {
231052d159aSCy Schubert     msg->attack_data = kill_new();
232052d159aSCy Schubert     if (msg->attack_data == NULL)
233052d159aSCy Schubert       return (-1);
234052d159aSCy Schubert     msg->attack_set = 1;
235052d159aSCy Schubert   }
236052d159aSCy Schubert   *value = msg->attack_data;
237052d159aSCy Schubert   return (0);
238052d159aSCy Schubert }
239052d159aSCy Schubert 
240052d159aSCy Schubert int
msg_run_get(struct msg * msg,int offset,struct run ** value)241052d159aSCy Schubert msg_run_get(struct msg *msg, int offset,
242052d159aSCy Schubert     struct run* *value)
243052d159aSCy Schubert {
244052d159aSCy Schubert   if (!msg->run_set || offset < 0 || offset >= msg->run_length)
245052d159aSCy Schubert     return (-1);
246052d159aSCy Schubert   *value = msg->run_data[offset];
247052d159aSCy Schubert   return (0);
248052d159aSCy Schubert }
249052d159aSCy Schubert 
250052d159aSCy Schubert void
msg_clear(struct msg * tmp)251052d159aSCy Schubert msg_clear(struct msg *tmp)
252052d159aSCy Schubert {
253052d159aSCy Schubert   if (tmp->from_name_set == 1) {
254052d159aSCy Schubert     free(tmp->from_name_data);
255052d159aSCy Schubert     tmp->from_name_data = NULL;
256052d159aSCy Schubert     tmp->from_name_set = 0;
257052d159aSCy Schubert   }
258052d159aSCy Schubert   if (tmp->to_name_set == 1) {
259052d159aSCy Schubert     free(tmp->to_name_data);
260052d159aSCy Schubert     tmp->to_name_data = NULL;
261052d159aSCy Schubert     tmp->to_name_set = 0;
262052d159aSCy Schubert   }
263052d159aSCy Schubert   if (tmp->attack_set == 1) {
264052d159aSCy Schubert     kill_free(tmp->attack_data);
265052d159aSCy Schubert     tmp->attack_data = NULL;
266052d159aSCy Schubert     tmp->attack_set = 0;
267052d159aSCy Schubert   }
268052d159aSCy Schubert   if (tmp->run_set == 1) {
269052d159aSCy Schubert     int i;
270052d159aSCy Schubert     for (i = 0; i < tmp->run_length; ++i) {
271052d159aSCy Schubert       run_free(tmp->run_data[i]);
272052d159aSCy Schubert     }
273052d159aSCy Schubert     free(tmp->run_data);
274052d159aSCy Schubert     tmp->run_data = NULL;
275052d159aSCy Schubert     tmp->run_set = 0;
276052d159aSCy Schubert     tmp->run_length = 0;
277052d159aSCy Schubert     tmp->run_num_allocated = 0;
278052d159aSCy Schubert   }
279052d159aSCy Schubert }
280052d159aSCy Schubert 
281052d159aSCy Schubert void
msg_free(struct msg * tmp)282052d159aSCy Schubert msg_free(struct msg *tmp)
283052d159aSCy Schubert {
284052d159aSCy Schubert   if (tmp->from_name_data != NULL)
285052d159aSCy Schubert       free (tmp->from_name_data);
286052d159aSCy Schubert   if (tmp->to_name_data != NULL)
287052d159aSCy Schubert       free (tmp->to_name_data);
288052d159aSCy Schubert   if (tmp->attack_data != NULL)
289052d159aSCy Schubert       kill_free(tmp->attack_data);
290052d159aSCy Schubert   if (tmp->run_set == 1) {
291052d159aSCy Schubert     int i;
292052d159aSCy Schubert     for (i = 0; i < tmp->run_length; ++i) {
293052d159aSCy Schubert       run_free(tmp->run_data[i]);
294052d159aSCy Schubert     }
295052d159aSCy Schubert     free(tmp->run_data);
296052d159aSCy Schubert     tmp->run_data = NULL;
297052d159aSCy Schubert     tmp->run_set = 0;
298052d159aSCy Schubert     tmp->run_length = 0;
299052d159aSCy Schubert     tmp->run_num_allocated = 0;
300052d159aSCy Schubert   }
301052d159aSCy Schubert   free(tmp->run_data);
302052d159aSCy Schubert   free(tmp);
303052d159aSCy Schubert }
304052d159aSCy Schubert 
305052d159aSCy Schubert void
msg_marshal(struct evbuffer * evbuf,const struct msg * tmp)306052d159aSCy Schubert msg_marshal(struct evbuffer *evbuf, const struct msg *tmp) {
307052d159aSCy Schubert   evtag_marshal_string(evbuf, MSG_FROM_NAME, tmp->from_name_data);
308052d159aSCy Schubert   evtag_marshal_string(evbuf, MSG_TO_NAME, tmp->to_name_data);
309052d159aSCy Schubert   if (tmp->attack_set) {
310052d159aSCy Schubert     evtag_marshal_kill(evbuf, MSG_ATTACK, tmp->attack_data);
311052d159aSCy Schubert   }
312052d159aSCy Schubert   if (tmp->run_set) {
313052d159aSCy Schubert     {
314052d159aSCy Schubert       int i;
315052d159aSCy Schubert       for (i = 0; i < tmp->run_length; ++i) {
316052d159aSCy Schubert     evtag_marshal_run(evbuf, MSG_RUN, tmp->run_data[i]);
317052d159aSCy Schubert       }
318052d159aSCy Schubert     }
319052d159aSCy Schubert   }
320052d159aSCy Schubert }
321052d159aSCy Schubert 
322052d159aSCy Schubert int
msg_unmarshal(struct msg * tmp,struct evbuffer * evbuf)323052d159aSCy Schubert msg_unmarshal(struct msg *tmp, struct evbuffer *evbuf)
324052d159aSCy Schubert {
325052d159aSCy Schubert   ev_uint32_t tag;
326052d159aSCy Schubert   while (evbuffer_get_length(evbuf) > 0) {
327052d159aSCy Schubert     if (evtag_peek(evbuf, &tag) == -1)
328052d159aSCy Schubert       return (-1);
329052d159aSCy Schubert     switch (tag) {
330052d159aSCy Schubert 
331052d159aSCy Schubert       case MSG_FROM_NAME:
332052d159aSCy Schubert         if (tmp->from_name_set)
333052d159aSCy Schubert           return (-1);
334052d159aSCy Schubert         if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) {
335052d159aSCy Schubert           event_warnx("%s: failed to unmarshal from_name", __func__);
336052d159aSCy Schubert           return (-1);
337052d159aSCy Schubert         }
338052d159aSCy Schubert         tmp->from_name_set = 1;
339052d159aSCy Schubert         break;
340052d159aSCy Schubert       case MSG_TO_NAME:
341052d159aSCy Schubert         if (tmp->to_name_set)
342052d159aSCy Schubert           return (-1);
343052d159aSCy Schubert         if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) {
344052d159aSCy Schubert           event_warnx("%s: failed to unmarshal to_name", __func__);
345052d159aSCy Schubert           return (-1);
346052d159aSCy Schubert         }
347052d159aSCy Schubert         tmp->to_name_set = 1;
348052d159aSCy Schubert         break;
349052d159aSCy Schubert       case MSG_ATTACK:
350052d159aSCy Schubert         if (tmp->attack_set)
351052d159aSCy Schubert           return (-1);
352052d159aSCy Schubert         tmp->attack_data = kill_new();
353052d159aSCy Schubert         if (tmp->attack_data == NULL)
354052d159aSCy Schubert           return (-1);
355*a466cc55SCy Schubert         if (evtag_unmarshal_kill(evbuf, MSG_ATTACK,
356*a466cc55SCy Schubert             tmp->attack_data) == -1) {
357052d159aSCy Schubert           event_warnx("%s: failed to unmarshal attack", __func__);
358052d159aSCy Schubert           return (-1);
359052d159aSCy Schubert         }
360052d159aSCy Schubert         tmp->attack_set = 1;
361052d159aSCy Schubert         break;
362052d159aSCy Schubert       case MSG_RUN:
363052d159aSCy Schubert         if (tmp->run_length >= tmp->run_num_allocated &&
364052d159aSCy Schubert             msg_run_expand_to_hold_more(tmp) < 0) {
365052d159aSCy Schubert           puts("HEY NOW");
366052d159aSCy Schubert           return (-1);
367052d159aSCy Schubert         }
368052d159aSCy Schubert         tmp->run_data[tmp->run_length] = run_new();
369052d159aSCy Schubert         if (tmp->run_data[tmp->run_length] == NULL)
370052d159aSCy Schubert           return (-1);
371*a466cc55SCy Schubert         if (evtag_unmarshal_run(evbuf, MSG_RUN,
372*a466cc55SCy Schubert             tmp->run_data[tmp->run_length]) == -1) {
373052d159aSCy Schubert           event_warnx("%s: failed to unmarshal run", __func__);
374052d159aSCy Schubert           return (-1);
375052d159aSCy Schubert         }
376052d159aSCy Schubert         ++tmp->run_length;
377052d159aSCy Schubert         tmp->run_set = 1;
378052d159aSCy Schubert         break;
379052d159aSCy Schubert       default:
380052d159aSCy Schubert         return -1;
381052d159aSCy Schubert     }
382052d159aSCy Schubert   }
383052d159aSCy Schubert 
384052d159aSCy Schubert   if (msg_complete(tmp) == -1)
385052d159aSCy Schubert     return (-1);
386052d159aSCy Schubert   return (0);
387052d159aSCy Schubert }
388052d159aSCy Schubert 
389052d159aSCy Schubert int
msg_complete(struct msg * msg)390052d159aSCy Schubert msg_complete(struct msg *msg)
391052d159aSCy Schubert {
392052d159aSCy Schubert   if (!msg->from_name_set)
393052d159aSCy Schubert     return (-1);
394052d159aSCy Schubert   if (!msg->to_name_set)
395052d159aSCy Schubert     return (-1);
396052d159aSCy Schubert   if (msg->attack_set && kill_complete(msg->attack_data) == -1)
397052d159aSCy Schubert     return (-1);
398052d159aSCy Schubert   {
399052d159aSCy Schubert     int i;
400052d159aSCy Schubert     for (i = 0; i < msg->run_length; ++i) {
401052d159aSCy Schubert       if (msg->run_set && run_complete(msg->run_data[i]) == -1)
402052d159aSCy Schubert         return (-1);
403052d159aSCy Schubert     }
404052d159aSCy Schubert   }
405052d159aSCy Schubert   return (0);
406052d159aSCy Schubert }
407052d159aSCy Schubert 
408052d159aSCy Schubert int
evtag_unmarshal_msg(struct evbuffer * evbuf,ev_uint32_t need_tag,struct msg * msg)409*a466cc55SCy Schubert evtag_unmarshal_msg(struct evbuffer *evbuf, ev_uint32_t need_tag,
410*a466cc55SCy Schubert   struct msg *msg)
411052d159aSCy Schubert {
412052d159aSCy Schubert   ev_uint32_t tag;
413052d159aSCy Schubert   int res = -1;
414052d159aSCy Schubert 
415052d159aSCy Schubert   struct evbuffer *tmp = evbuffer_new();
416052d159aSCy Schubert 
417052d159aSCy Schubert   if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
418052d159aSCy Schubert     goto error;
419052d159aSCy Schubert 
420052d159aSCy Schubert   if (msg_unmarshal(msg, tmp) == -1)
421052d159aSCy Schubert     goto error;
422052d159aSCy Schubert 
423052d159aSCy Schubert   res = 0;
424052d159aSCy Schubert 
425052d159aSCy Schubert  error:
426052d159aSCy Schubert   evbuffer_free(tmp);
427052d159aSCy Schubert   return (res);
428052d159aSCy Schubert }
429052d159aSCy Schubert 
430052d159aSCy Schubert void
evtag_marshal_msg(struct evbuffer * evbuf,ev_uint32_t tag,const struct msg * msg)431*a466cc55SCy Schubert evtag_marshal_msg(struct evbuffer *evbuf, ev_uint32_t tag,
432*a466cc55SCy Schubert     const struct msg *msg)
433052d159aSCy Schubert {
434052d159aSCy Schubert   struct evbuffer *buf_ = evbuffer_new();
435052d159aSCy Schubert   assert(buf_ != NULL);
436052d159aSCy Schubert   msg_marshal(buf_, msg);
437052d159aSCy Schubert   evtag_marshal_buffer(evbuf, tag, buf_);
438052d159aSCy Schubert   evbuffer_free(buf_);
439052d159aSCy Schubert }
440052d159aSCy Schubert 
441052d159aSCy Schubert /*
442052d159aSCy Schubert  * Implementation of kill
443052d159aSCy Schubert  */
444052d159aSCy Schubert 
445052d159aSCy Schubert static struct kill_access_ kill_base__ = {
446052d159aSCy Schubert   kill_weapon_assign,
447052d159aSCy Schubert   kill_weapon_get,
448052d159aSCy Schubert   kill_action_assign,
449052d159aSCy Schubert   kill_action_get,
450052d159aSCy Schubert   kill_how_often_assign,
451052d159aSCy Schubert   kill_how_often_get,
452052d159aSCy Schubert   kill_how_often_add,
453052d159aSCy Schubert };
454052d159aSCy Schubert 
455052d159aSCy Schubert struct kill *
kill_new(void)456052d159aSCy Schubert kill_new(void)
457052d159aSCy Schubert {
458052d159aSCy Schubert   return kill_new_with_arg(NULL);
459052d159aSCy Schubert }
460052d159aSCy Schubert 
461052d159aSCy Schubert struct kill *
kill_new_with_arg(void * unused)462052d159aSCy Schubert kill_new_with_arg(void *unused)
463052d159aSCy Schubert {
464052d159aSCy Schubert   struct kill *tmp;
465052d159aSCy Schubert   if ((tmp = malloc(sizeof(struct kill))) == NULL) {
466052d159aSCy Schubert     event_warn("%s: malloc", __func__);
467052d159aSCy Schubert     return (NULL);
468052d159aSCy Schubert   }
469052d159aSCy Schubert   tmp->base = &kill_base__;
470052d159aSCy Schubert 
471052d159aSCy Schubert   tmp->weapon_data = NULL;
472052d159aSCy Schubert   tmp->weapon_set = 0;
473052d159aSCy Schubert 
474052d159aSCy Schubert   tmp->action_data = NULL;
475052d159aSCy Schubert   tmp->action_set = 0;
476052d159aSCy Schubert 
477052d159aSCy Schubert   tmp->how_often_data = NULL;
478052d159aSCy Schubert   tmp->how_often_length = 0;
479052d159aSCy Schubert   tmp->how_often_num_allocated = 0;
480052d159aSCy Schubert   tmp->how_often_set = 0;
481052d159aSCy Schubert 
482052d159aSCy Schubert   return (tmp);
483052d159aSCy Schubert }
484052d159aSCy Schubert 
485052d159aSCy Schubert 
486052d159aSCy Schubert 
487052d159aSCy Schubert static int
kill_how_often_expand_to_hold_more(struct kill * msg)488052d159aSCy Schubert kill_how_often_expand_to_hold_more(struct kill *msg)
489052d159aSCy Schubert {
490052d159aSCy Schubert   int tobe_allocated = msg->how_often_num_allocated;
491052d159aSCy Schubert   ev_uint32_t* new_data = NULL;
492052d159aSCy Schubert   tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
493052d159aSCy Schubert   new_data = (ev_uint32_t*) realloc(msg->how_often_data,
494052d159aSCy Schubert       tobe_allocated * sizeof(ev_uint32_t));
495052d159aSCy Schubert   if (new_data == NULL)
496052d159aSCy Schubert     return -1;
497052d159aSCy Schubert   msg->how_often_data = new_data;
498052d159aSCy Schubert   msg->how_often_num_allocated = tobe_allocated;
499*a466cc55SCy Schubert   return 0;
500*a466cc55SCy Schubert }
501052d159aSCy Schubert 
502052d159aSCy Schubert ev_uint32_t *
kill_how_often_add(struct kill * msg,const ev_uint32_t value)503052d159aSCy Schubert kill_how_often_add(struct kill *msg, const ev_uint32_t value)
504052d159aSCy Schubert {
505052d159aSCy Schubert   if (++msg->how_often_length >= msg->how_often_num_allocated) {
506052d159aSCy Schubert     if (kill_how_often_expand_to_hold_more(msg)<0)
507052d159aSCy Schubert       goto error;
508052d159aSCy Schubert   }
509052d159aSCy Schubert   msg->how_often_data[msg->how_often_length - 1] = value;
510052d159aSCy Schubert   msg->how_often_set = 1;
511052d159aSCy Schubert   return &(msg->how_often_data[msg->how_often_length - 1]);
512052d159aSCy Schubert error:
513052d159aSCy Schubert   --msg->how_often_length;
514052d159aSCy Schubert   return (NULL);
515052d159aSCy Schubert }
516052d159aSCy Schubert 
517052d159aSCy Schubert int
kill_weapon_assign(struct kill * msg,const char * value)518052d159aSCy Schubert kill_weapon_assign(struct kill *msg,
519052d159aSCy Schubert     const char * value)
520052d159aSCy Schubert {
521052d159aSCy Schubert   if (msg->weapon_data != NULL)
522052d159aSCy Schubert     free(msg->weapon_data);
523052d159aSCy Schubert   if ((msg->weapon_data = strdup(value)) == NULL)
524052d159aSCy Schubert     return (-1);
525052d159aSCy Schubert   msg->weapon_set = 1;
526052d159aSCy Schubert   return (0);
527052d159aSCy Schubert }
528052d159aSCy Schubert 
529052d159aSCy Schubert int
kill_action_assign(struct kill * msg,const char * value)530052d159aSCy Schubert kill_action_assign(struct kill *msg,
531052d159aSCy Schubert     const char * value)
532052d159aSCy Schubert {
533052d159aSCy Schubert   if (msg->action_data != NULL)
534052d159aSCy Schubert     free(msg->action_data);
535052d159aSCy Schubert   if ((msg->action_data = strdup(value)) == NULL)
536052d159aSCy Schubert     return (-1);
537052d159aSCy Schubert   msg->action_set = 1;
538052d159aSCy Schubert   return (0);
539052d159aSCy Schubert }
540052d159aSCy Schubert 
541052d159aSCy Schubert int
kill_how_often_assign(struct kill * msg,int off,const ev_uint32_t value)542052d159aSCy Schubert kill_how_often_assign(struct kill *msg, int off,
543052d159aSCy Schubert   const ev_uint32_t value)
544052d159aSCy Schubert {
545052d159aSCy Schubert   if (!msg->how_often_set || off < 0 || off >= msg->how_often_length)
546052d159aSCy Schubert     return (-1);
547052d159aSCy Schubert 
548052d159aSCy Schubert   {
549052d159aSCy Schubert     msg->how_often_data[off] = value;
550052d159aSCy Schubert   }
551052d159aSCy Schubert   return (0);
552052d159aSCy Schubert }
553052d159aSCy Schubert 
554052d159aSCy Schubert int
kill_weapon_get(struct kill * msg,char ** value)555052d159aSCy Schubert kill_weapon_get(struct kill *msg, char * *value)
556052d159aSCy Schubert {
557052d159aSCy Schubert   if (msg->weapon_set != 1)
558052d159aSCy Schubert     return (-1);
559052d159aSCy Schubert   *value = msg->weapon_data;
560052d159aSCy Schubert   return (0);
561052d159aSCy Schubert }
562052d159aSCy Schubert 
563052d159aSCy Schubert int
kill_action_get(struct kill * msg,char ** value)564052d159aSCy Schubert kill_action_get(struct kill *msg, char * *value)
565052d159aSCy Schubert {
566052d159aSCy Schubert   if (msg->action_set != 1)
567052d159aSCy Schubert     return (-1);
568052d159aSCy Schubert   *value = msg->action_data;
569052d159aSCy Schubert   return (0);
570052d159aSCy Schubert }
571052d159aSCy Schubert 
572052d159aSCy Schubert int
kill_how_often_get(struct kill * msg,int offset,ev_uint32_t * value)573052d159aSCy Schubert kill_how_often_get(struct kill *msg, int offset,
574052d159aSCy Schubert     ev_uint32_t *value)
575052d159aSCy Schubert {
576052d159aSCy Schubert   if (!msg->how_often_set || offset < 0 || offset >= msg->how_often_length)
577052d159aSCy Schubert     return (-1);
578052d159aSCy Schubert   *value = msg->how_often_data[offset];
579052d159aSCy Schubert   return (0);
580052d159aSCy Schubert }
581052d159aSCy Schubert 
582052d159aSCy Schubert void
kill_clear(struct kill * tmp)583052d159aSCy Schubert kill_clear(struct kill *tmp)
584052d159aSCy Schubert {
585052d159aSCy Schubert   if (tmp->weapon_set == 1) {
586052d159aSCy Schubert     free(tmp->weapon_data);
587052d159aSCy Schubert     tmp->weapon_data = NULL;
588052d159aSCy Schubert     tmp->weapon_set = 0;
589052d159aSCy Schubert   }
590052d159aSCy Schubert   if (tmp->action_set == 1) {
591052d159aSCy Schubert     free(tmp->action_data);
592052d159aSCy Schubert     tmp->action_data = NULL;
593052d159aSCy Schubert     tmp->action_set = 0;
594052d159aSCy Schubert   }
595052d159aSCy Schubert   if (tmp->how_often_set == 1) {
596052d159aSCy Schubert     free(tmp->how_often_data);
597052d159aSCy Schubert     tmp->how_often_data = NULL;
598052d159aSCy Schubert     tmp->how_often_set = 0;
599052d159aSCy Schubert     tmp->how_often_length = 0;
600052d159aSCy Schubert     tmp->how_often_num_allocated = 0;
601052d159aSCy Schubert   }
602052d159aSCy Schubert }
603052d159aSCy Schubert 
604052d159aSCy Schubert void
kill_free(struct kill * tmp)605052d159aSCy Schubert kill_free(struct kill *tmp)
606052d159aSCy Schubert {
607052d159aSCy Schubert   if (tmp->weapon_data != NULL)
608052d159aSCy Schubert       free (tmp->weapon_data);
609052d159aSCy Schubert   if (tmp->action_data != NULL)
610052d159aSCy Schubert       free (tmp->action_data);
611052d159aSCy Schubert   if (tmp->how_often_set == 1) {
612052d159aSCy Schubert     free(tmp->how_often_data);
613052d159aSCy Schubert     tmp->how_often_data = NULL;
614052d159aSCy Schubert     tmp->how_often_set = 0;
615052d159aSCy Schubert     tmp->how_often_length = 0;
616052d159aSCy Schubert     tmp->how_often_num_allocated = 0;
617052d159aSCy Schubert   }
618052d159aSCy Schubert   free(tmp->how_often_data);
619052d159aSCy Schubert   free(tmp);
620052d159aSCy Schubert }
621052d159aSCy Schubert 
622052d159aSCy Schubert void
kill_marshal(struct evbuffer * evbuf,const struct kill * tmp)623052d159aSCy Schubert kill_marshal(struct evbuffer *evbuf, const struct kill *tmp) {
624052d159aSCy Schubert   evtag_marshal_string(evbuf, KILL_WEAPON, tmp->weapon_data);
625052d159aSCy Schubert   evtag_marshal_string(evbuf, KILL_ACTION, tmp->action_data);
626052d159aSCy Schubert   if (tmp->how_often_set) {
627052d159aSCy Schubert     {
628052d159aSCy Schubert       int i;
629052d159aSCy Schubert       for (i = 0; i < tmp->how_often_length; ++i) {
630052d159aSCy Schubert     evtag_marshal_int(evbuf, KILL_HOW_OFTEN, tmp->how_often_data[i]);
631052d159aSCy Schubert       }
632052d159aSCy Schubert     }
633052d159aSCy Schubert   }
634052d159aSCy Schubert }
635052d159aSCy Schubert 
636052d159aSCy Schubert int
kill_unmarshal(struct kill * tmp,struct evbuffer * evbuf)637052d159aSCy Schubert kill_unmarshal(struct kill *tmp, struct evbuffer *evbuf)
638052d159aSCy Schubert {
639052d159aSCy Schubert   ev_uint32_t tag;
640052d159aSCy Schubert   while (evbuffer_get_length(evbuf) > 0) {
641052d159aSCy Schubert     if (evtag_peek(evbuf, &tag) == -1)
642052d159aSCy Schubert       return (-1);
643052d159aSCy Schubert     switch (tag) {
644052d159aSCy Schubert 
645052d159aSCy Schubert       case KILL_WEAPON:
646052d159aSCy Schubert         if (tmp->weapon_set)
647052d159aSCy Schubert           return (-1);
648052d159aSCy Schubert         if (evtag_unmarshal_string(evbuf, KILL_WEAPON, &tmp->weapon_data) == -1) {
649052d159aSCy Schubert           event_warnx("%s: failed to unmarshal weapon", __func__);
650052d159aSCy Schubert           return (-1);
651052d159aSCy Schubert         }
652052d159aSCy Schubert         tmp->weapon_set = 1;
653052d159aSCy Schubert         break;
654052d159aSCy Schubert       case KILL_ACTION:
655052d159aSCy Schubert         if (tmp->action_set)
656052d159aSCy Schubert           return (-1);
657052d159aSCy Schubert         if (evtag_unmarshal_string(evbuf, KILL_ACTION, &tmp->action_data) == -1) {
658052d159aSCy Schubert           event_warnx("%s: failed to unmarshal action", __func__);
659052d159aSCy Schubert           return (-1);
660052d159aSCy Schubert         }
661052d159aSCy Schubert         tmp->action_set = 1;
662052d159aSCy Schubert         break;
663052d159aSCy Schubert       case KILL_HOW_OFTEN:
664052d159aSCy Schubert         if (tmp->how_often_length >= tmp->how_often_num_allocated &&
665052d159aSCy Schubert             kill_how_often_expand_to_hold_more(tmp) < 0) {
666052d159aSCy Schubert           puts("HEY NOW");
667052d159aSCy Schubert           return (-1);
668052d159aSCy Schubert         }
669052d159aSCy Schubert         if (evtag_unmarshal_int(evbuf, KILL_HOW_OFTEN, &tmp->how_often_data[tmp->how_often_length]) == -1) {
670052d159aSCy Schubert           event_warnx("%s: failed to unmarshal how_often", __func__);
671052d159aSCy Schubert           return (-1);
672052d159aSCy Schubert         }
673052d159aSCy Schubert         ++tmp->how_often_length;
674052d159aSCy Schubert         tmp->how_often_set = 1;
675052d159aSCy Schubert         break;
676052d159aSCy Schubert       default:
677052d159aSCy Schubert         return -1;
678052d159aSCy Schubert     }
679052d159aSCy Schubert   }
680052d159aSCy Schubert 
681052d159aSCy Schubert   if (kill_complete(tmp) == -1)
682052d159aSCy Schubert     return (-1);
683052d159aSCy Schubert   return (0);
684052d159aSCy Schubert }
685052d159aSCy Schubert 
686052d159aSCy Schubert int
kill_complete(struct kill * msg)687052d159aSCy Schubert kill_complete(struct kill *msg)
688052d159aSCy Schubert {
689052d159aSCy Schubert   if (!msg->weapon_set)
690052d159aSCy Schubert     return (-1);
691052d159aSCy Schubert   if (!msg->action_set)
692052d159aSCy Schubert     return (-1);
693052d159aSCy Schubert   return (0);
694052d159aSCy Schubert }
695052d159aSCy Schubert 
696052d159aSCy Schubert int
evtag_unmarshal_kill(struct evbuffer * evbuf,ev_uint32_t need_tag,struct kill * msg)697*a466cc55SCy Schubert evtag_unmarshal_kill(struct evbuffer *evbuf, ev_uint32_t need_tag,
698*a466cc55SCy Schubert   struct kill *msg)
699052d159aSCy Schubert {
700052d159aSCy Schubert   ev_uint32_t tag;
701052d159aSCy Schubert   int res = -1;
702052d159aSCy Schubert 
703052d159aSCy Schubert   struct evbuffer *tmp = evbuffer_new();
704052d159aSCy Schubert 
705052d159aSCy Schubert   if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
706052d159aSCy Schubert     goto error;
707052d159aSCy Schubert 
708052d159aSCy Schubert   if (kill_unmarshal(msg, tmp) == -1)
709052d159aSCy Schubert     goto error;
710052d159aSCy Schubert 
711052d159aSCy Schubert   res = 0;
712052d159aSCy Schubert 
713052d159aSCy Schubert  error:
714052d159aSCy Schubert   evbuffer_free(tmp);
715052d159aSCy Schubert   return (res);
716052d159aSCy Schubert }
717052d159aSCy Schubert 
718052d159aSCy Schubert void
evtag_marshal_kill(struct evbuffer * evbuf,ev_uint32_t tag,const struct kill * msg)719*a466cc55SCy Schubert evtag_marshal_kill(struct evbuffer *evbuf, ev_uint32_t tag,
720*a466cc55SCy Schubert     const struct kill *msg)
721052d159aSCy Schubert {
722052d159aSCy Schubert   struct evbuffer *buf_ = evbuffer_new();
723052d159aSCy Schubert   assert(buf_ != NULL);
724052d159aSCy Schubert   kill_marshal(buf_, msg);
725052d159aSCy Schubert   evtag_marshal_buffer(evbuf, tag, buf_);
726052d159aSCy Schubert   evbuffer_free(buf_);
727052d159aSCy Schubert }
728052d159aSCy Schubert 
729052d159aSCy Schubert /*
730052d159aSCy Schubert  * Implementation of run
731052d159aSCy Schubert  */
732052d159aSCy Schubert 
733052d159aSCy Schubert static struct run_access_ run_base__ = {
734052d159aSCy Schubert   run_how_assign,
735052d159aSCy Schubert   run_how_get,
736052d159aSCy Schubert   run_some_bytes_assign,
737052d159aSCy Schubert   run_some_bytes_get,
738052d159aSCy Schubert   run_fixed_bytes_assign,
739052d159aSCy Schubert   run_fixed_bytes_get,
740052d159aSCy Schubert   run_notes_assign,
741052d159aSCy Schubert   run_notes_get,
742052d159aSCy Schubert   run_notes_add,
743052d159aSCy Schubert   run_large_number_assign,
744052d159aSCy Schubert   run_large_number_get,
745052d159aSCy Schubert   run_other_numbers_assign,
746052d159aSCy Schubert   run_other_numbers_get,
747052d159aSCy Schubert   run_other_numbers_add,
748052d159aSCy Schubert };
749052d159aSCy Schubert 
750052d159aSCy Schubert struct run *
run_new(void)751052d159aSCy Schubert run_new(void)
752052d159aSCy Schubert {
753052d159aSCy Schubert   return run_new_with_arg(NULL);
754052d159aSCy Schubert }
755052d159aSCy Schubert 
756052d159aSCy Schubert struct run *
run_new_with_arg(void * unused)757052d159aSCy Schubert run_new_with_arg(void *unused)
758052d159aSCy Schubert {
759052d159aSCy Schubert   struct run *tmp;
760052d159aSCy Schubert   if ((tmp = malloc(sizeof(struct run))) == NULL) {
761052d159aSCy Schubert     event_warn("%s: malloc", __func__);
762052d159aSCy Schubert     return (NULL);
763052d159aSCy Schubert   }
764052d159aSCy Schubert   tmp->base = &run_base__;
765052d159aSCy Schubert 
766052d159aSCy Schubert   tmp->how_data = NULL;
767052d159aSCy Schubert   tmp->how_set = 0;
768052d159aSCy Schubert 
769052d159aSCy Schubert   tmp->some_bytes_data = NULL;
770052d159aSCy Schubert   tmp->some_bytes_length = 0;
771052d159aSCy Schubert   tmp->some_bytes_set = 0;
772052d159aSCy Schubert 
773052d159aSCy Schubert   memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
774052d159aSCy Schubert   tmp->fixed_bytes_set = 0;
775052d159aSCy Schubert 
776052d159aSCy Schubert   tmp->notes_data = NULL;
777052d159aSCy Schubert   tmp->notes_length = 0;
778052d159aSCy Schubert   tmp->notes_num_allocated = 0;
779052d159aSCy Schubert   tmp->notes_set = 0;
780052d159aSCy Schubert 
781052d159aSCy Schubert   tmp->large_number_data = 0;
782052d159aSCy Schubert   tmp->large_number_set = 0;
783052d159aSCy Schubert 
784052d159aSCy Schubert   tmp->other_numbers_data = NULL;
785052d159aSCy Schubert   tmp->other_numbers_length = 0;
786052d159aSCy Schubert   tmp->other_numbers_num_allocated = 0;
787052d159aSCy Schubert   tmp->other_numbers_set = 0;
788052d159aSCy Schubert 
789052d159aSCy Schubert   return (tmp);
790052d159aSCy Schubert }
791052d159aSCy Schubert 
792052d159aSCy Schubert 
793052d159aSCy Schubert 
794052d159aSCy Schubert 
795052d159aSCy Schubert static int
run_notes_expand_to_hold_more(struct run * msg)796052d159aSCy Schubert run_notes_expand_to_hold_more(struct run *msg)
797052d159aSCy Schubert {
798052d159aSCy Schubert   int tobe_allocated = msg->notes_num_allocated;
799052d159aSCy Schubert   char ** new_data = NULL;
800052d159aSCy Schubert   tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
801052d159aSCy Schubert   new_data = (char **) realloc(msg->notes_data,
802052d159aSCy Schubert       tobe_allocated * sizeof(char *));
803052d159aSCy Schubert   if (new_data == NULL)
804052d159aSCy Schubert     return -1;
805052d159aSCy Schubert   msg->notes_data = new_data;
806052d159aSCy Schubert   msg->notes_num_allocated = tobe_allocated;
807*a466cc55SCy Schubert   return 0;
808*a466cc55SCy Schubert }
809052d159aSCy Schubert 
810052d159aSCy Schubert char * *
run_notes_add(struct run * msg,const char * value)811052d159aSCy Schubert run_notes_add(struct run *msg, const char * value)
812052d159aSCy Schubert {
813052d159aSCy Schubert   if (++msg->notes_length >= msg->notes_num_allocated) {
814052d159aSCy Schubert     if (run_notes_expand_to_hold_more(msg)<0)
815052d159aSCy Schubert       goto error;
816052d159aSCy Schubert   }
817052d159aSCy Schubert   if (value != NULL) {
818052d159aSCy Schubert     msg->notes_data[msg->notes_length - 1] = strdup(value);
819052d159aSCy Schubert     if (msg->notes_data[msg->notes_length - 1] == NULL) {
820052d159aSCy Schubert       goto error;
821052d159aSCy Schubert     }
822052d159aSCy Schubert   } else {
823052d159aSCy Schubert     msg->notes_data[msg->notes_length - 1] = NULL;
824052d159aSCy Schubert   }
825052d159aSCy Schubert   msg->notes_set = 1;
826052d159aSCy Schubert   return &(msg->notes_data[msg->notes_length - 1]);
827052d159aSCy Schubert error:
828052d159aSCy Schubert   --msg->notes_length;
829052d159aSCy Schubert   return (NULL);
830052d159aSCy Schubert }
831052d159aSCy Schubert 
832052d159aSCy Schubert 
833052d159aSCy Schubert static int
run_other_numbers_expand_to_hold_more(struct run * msg)834052d159aSCy Schubert run_other_numbers_expand_to_hold_more(struct run *msg)
835052d159aSCy Schubert {
836052d159aSCy Schubert   int tobe_allocated = msg->other_numbers_num_allocated;
837052d159aSCy Schubert   ev_uint32_t* new_data = NULL;
838052d159aSCy Schubert   tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
839052d159aSCy Schubert   new_data = (ev_uint32_t*) realloc(msg->other_numbers_data,
840052d159aSCy Schubert       tobe_allocated * sizeof(ev_uint32_t));
841052d159aSCy Schubert   if (new_data == NULL)
842052d159aSCy Schubert     return -1;
843052d159aSCy Schubert   msg->other_numbers_data = new_data;
844052d159aSCy Schubert   msg->other_numbers_num_allocated = tobe_allocated;
845*a466cc55SCy Schubert   return 0;
846*a466cc55SCy Schubert }
847052d159aSCy Schubert 
848052d159aSCy Schubert ev_uint32_t *
run_other_numbers_add(struct run * msg,const ev_uint32_t value)849052d159aSCy Schubert run_other_numbers_add(struct run *msg, const ev_uint32_t value)
850052d159aSCy Schubert {
851052d159aSCy Schubert   if (++msg->other_numbers_length >= msg->other_numbers_num_allocated) {
852052d159aSCy Schubert     if (run_other_numbers_expand_to_hold_more(msg)<0)
853052d159aSCy Schubert       goto error;
854052d159aSCy Schubert   }
855052d159aSCy Schubert   msg->other_numbers_data[msg->other_numbers_length - 1] = value;
856052d159aSCy Schubert   msg->other_numbers_set = 1;
857052d159aSCy Schubert   return &(msg->other_numbers_data[msg->other_numbers_length - 1]);
858052d159aSCy Schubert error:
859052d159aSCy Schubert   --msg->other_numbers_length;
860052d159aSCy Schubert   return (NULL);
861052d159aSCy Schubert }
862052d159aSCy Schubert 
863052d159aSCy Schubert int
run_how_assign(struct run * msg,const char * value)864052d159aSCy Schubert run_how_assign(struct run *msg,
865052d159aSCy Schubert     const char * value)
866052d159aSCy Schubert {
867052d159aSCy Schubert   if (msg->how_data != NULL)
868052d159aSCy Schubert     free(msg->how_data);
869052d159aSCy Schubert   if ((msg->how_data = strdup(value)) == NULL)
870052d159aSCy Schubert     return (-1);
871052d159aSCy Schubert   msg->how_set = 1;
872052d159aSCy Schubert   return (0);
873052d159aSCy Schubert }
874052d159aSCy Schubert 
875052d159aSCy Schubert int
run_some_bytes_assign(struct run * msg,const ev_uint8_t * value,ev_uint32_t len)876052d159aSCy Schubert run_some_bytes_assign(struct run *msg, const ev_uint8_t * value, ev_uint32_t len)
877052d159aSCy Schubert {
878052d159aSCy Schubert   if (msg->some_bytes_data != NULL)
879052d159aSCy Schubert     free (msg->some_bytes_data);
880052d159aSCy Schubert   msg->some_bytes_data = malloc(len);
881052d159aSCy Schubert   if (msg->some_bytes_data == NULL)
882052d159aSCy Schubert     return (-1);
883052d159aSCy Schubert   msg->some_bytes_set = 1;
884052d159aSCy Schubert   msg->some_bytes_length = len;
885052d159aSCy Schubert   memcpy(msg->some_bytes_data, value, len);
886052d159aSCy Schubert   return (0);
887052d159aSCy Schubert }
888052d159aSCy Schubert 
889052d159aSCy Schubert int
run_fixed_bytes_assign(struct run * msg,const ev_uint8_t * value)890052d159aSCy Schubert run_fixed_bytes_assign(struct run *msg, const ev_uint8_t *value)
891052d159aSCy Schubert {
892052d159aSCy Schubert   msg->fixed_bytes_set = 1;
893052d159aSCy Schubert   memcpy(msg->fixed_bytes_data, value, 24);
894052d159aSCy Schubert   return (0);
895052d159aSCy Schubert }
896052d159aSCy Schubert 
897052d159aSCy Schubert int
run_notes_assign(struct run * msg,int off,const char * value)898052d159aSCy Schubert run_notes_assign(struct run *msg, int off,
899052d159aSCy Schubert   const char * value)
900052d159aSCy Schubert {
901052d159aSCy Schubert   if (!msg->notes_set || off < 0 || off >= msg->notes_length)
902052d159aSCy Schubert     return (-1);
903052d159aSCy Schubert 
904052d159aSCy Schubert   {
905052d159aSCy Schubert     if (msg->notes_data[off] != NULL)
906052d159aSCy Schubert       free(msg->notes_data[off]);
907052d159aSCy Schubert     msg->notes_data[off] = strdup(value);
908052d159aSCy Schubert     if (msg->notes_data[off] == NULL) {
909052d159aSCy Schubert       event_warnx("%s: strdup", __func__);
910052d159aSCy Schubert       return (-1);
911052d159aSCy Schubert     }
912052d159aSCy Schubert   }
913052d159aSCy Schubert   return (0);
914052d159aSCy Schubert }
915052d159aSCy Schubert 
916052d159aSCy Schubert int
run_large_number_assign(struct run * msg,const ev_uint64_t value)917052d159aSCy Schubert run_large_number_assign(struct run *msg, const ev_uint64_t value)
918052d159aSCy Schubert {
919052d159aSCy Schubert   msg->large_number_set = 1;
920052d159aSCy Schubert   msg->large_number_data = value;
921052d159aSCy Schubert   return (0);
922052d159aSCy Schubert }
923052d159aSCy Schubert 
924052d159aSCy Schubert int
run_other_numbers_assign(struct run * msg,int off,const ev_uint32_t value)925052d159aSCy Schubert run_other_numbers_assign(struct run *msg, int off,
926052d159aSCy Schubert   const ev_uint32_t value)
927052d159aSCy Schubert {
928052d159aSCy Schubert   if (!msg->other_numbers_set || off < 0 || off >= msg->other_numbers_length)
929052d159aSCy Schubert     return (-1);
930052d159aSCy Schubert 
931052d159aSCy Schubert   {
932052d159aSCy Schubert     msg->other_numbers_data[off] = value;
933052d159aSCy Schubert   }
934052d159aSCy Schubert   return (0);
935052d159aSCy Schubert }
936052d159aSCy Schubert 
937052d159aSCy Schubert int
run_how_get(struct run * msg,char ** value)938052d159aSCy Schubert run_how_get(struct run *msg, char * *value)
939052d159aSCy Schubert {
940052d159aSCy Schubert   if (msg->how_set != 1)
941052d159aSCy Schubert     return (-1);
942052d159aSCy Schubert   *value = msg->how_data;
943052d159aSCy Schubert   return (0);
944052d159aSCy Schubert }
945052d159aSCy Schubert 
946052d159aSCy Schubert int
run_some_bytes_get(struct run * msg,ev_uint8_t ** value,ev_uint32_t * plen)947052d159aSCy Schubert run_some_bytes_get(struct run *msg, ev_uint8_t * *value, ev_uint32_t *plen)
948052d159aSCy Schubert {
949052d159aSCy Schubert   if (msg->some_bytes_set != 1)
950052d159aSCy Schubert     return (-1);
951052d159aSCy Schubert   *value = msg->some_bytes_data;
952052d159aSCy Schubert   *plen = msg->some_bytes_length;
953052d159aSCy Schubert   return (0);
954052d159aSCy Schubert }
955052d159aSCy Schubert 
956052d159aSCy Schubert int
run_fixed_bytes_get(struct run * msg,ev_uint8_t ** value)957052d159aSCy Schubert run_fixed_bytes_get(struct run *msg, ev_uint8_t **value)
958052d159aSCy Schubert {
959052d159aSCy Schubert   if (msg->fixed_bytes_set != 1)
960052d159aSCy Schubert     return (-1);
961052d159aSCy Schubert   *value = msg->fixed_bytes_data;
962052d159aSCy Schubert   return (0);
963052d159aSCy Schubert }
964052d159aSCy Schubert 
965052d159aSCy Schubert int
run_notes_get(struct run * msg,int offset,char ** value)966052d159aSCy Schubert run_notes_get(struct run *msg, int offset,
967052d159aSCy Schubert     char * *value)
968052d159aSCy Schubert {
969052d159aSCy Schubert   if (!msg->notes_set || offset < 0 || offset >= msg->notes_length)
970052d159aSCy Schubert     return (-1);
971052d159aSCy Schubert   *value = msg->notes_data[offset];
972052d159aSCy Schubert   return (0);
973052d159aSCy Schubert }
974052d159aSCy Schubert 
975052d159aSCy Schubert int
run_large_number_get(struct run * msg,ev_uint64_t * value)976052d159aSCy Schubert run_large_number_get(struct run *msg, ev_uint64_t *value)
977052d159aSCy Schubert {
978052d159aSCy Schubert   if (msg->large_number_set != 1)
979052d159aSCy Schubert     return (-1);
980052d159aSCy Schubert   *value = msg->large_number_data;
981052d159aSCy Schubert   return (0);
982052d159aSCy Schubert }
983052d159aSCy Schubert 
984052d159aSCy Schubert int
run_other_numbers_get(struct run * msg,int offset,ev_uint32_t * value)985052d159aSCy Schubert run_other_numbers_get(struct run *msg, int offset,
986052d159aSCy Schubert     ev_uint32_t *value)
987052d159aSCy Schubert {
988052d159aSCy Schubert   if (!msg->other_numbers_set || offset < 0 || offset >= msg->other_numbers_length)
989052d159aSCy Schubert     return (-1);
990052d159aSCy Schubert   *value = msg->other_numbers_data[offset];
991052d159aSCy Schubert   return (0);
992052d159aSCy Schubert }
993052d159aSCy Schubert 
994052d159aSCy Schubert void
run_clear(struct run * tmp)995052d159aSCy Schubert run_clear(struct run *tmp)
996052d159aSCy Schubert {
997052d159aSCy Schubert   if (tmp->how_set == 1) {
998052d159aSCy Schubert     free(tmp->how_data);
999052d159aSCy Schubert     tmp->how_data = NULL;
1000052d159aSCy Schubert     tmp->how_set = 0;
1001052d159aSCy Schubert   }
1002052d159aSCy Schubert   if (tmp->some_bytes_set == 1) {
1003052d159aSCy Schubert     free (tmp->some_bytes_data);
1004052d159aSCy Schubert     tmp->some_bytes_data = NULL;
1005052d159aSCy Schubert     tmp->some_bytes_length = 0;
1006052d159aSCy Schubert     tmp->some_bytes_set = 0;
1007052d159aSCy Schubert   }
1008052d159aSCy Schubert   tmp->fixed_bytes_set = 0;
1009052d159aSCy Schubert   memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
1010052d159aSCy Schubert   if (tmp->notes_set == 1) {
1011052d159aSCy Schubert     int i;
1012052d159aSCy Schubert     for (i = 0; i < tmp->notes_length; ++i) {
1013052d159aSCy Schubert       if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]);
1014052d159aSCy Schubert     }
1015052d159aSCy Schubert     free(tmp->notes_data);
1016052d159aSCy Schubert     tmp->notes_data = NULL;
1017052d159aSCy Schubert     tmp->notes_set = 0;
1018052d159aSCy Schubert     tmp->notes_length = 0;
1019052d159aSCy Schubert     tmp->notes_num_allocated = 0;
1020052d159aSCy Schubert   }
1021052d159aSCy Schubert   tmp->large_number_set = 0;
1022052d159aSCy Schubert   if (tmp->other_numbers_set == 1) {
1023052d159aSCy Schubert     free(tmp->other_numbers_data);
1024052d159aSCy Schubert     tmp->other_numbers_data = NULL;
1025052d159aSCy Schubert     tmp->other_numbers_set = 0;
1026052d159aSCy Schubert     tmp->other_numbers_length = 0;
1027052d159aSCy Schubert     tmp->other_numbers_num_allocated = 0;
1028052d159aSCy Schubert   }
1029052d159aSCy Schubert }
1030052d159aSCy Schubert 
1031052d159aSCy Schubert void
run_free(struct run * tmp)1032052d159aSCy Schubert run_free(struct run *tmp)
1033052d159aSCy Schubert {
1034052d159aSCy Schubert   if (tmp->how_data != NULL)
1035052d159aSCy Schubert       free (tmp->how_data);
1036052d159aSCy Schubert   if (tmp->some_bytes_data != NULL)
1037052d159aSCy Schubert       free(tmp->some_bytes_data);
1038052d159aSCy Schubert   if (tmp->notes_set == 1) {
1039052d159aSCy Schubert     int i;
1040052d159aSCy Schubert     for (i = 0; i < tmp->notes_length; ++i) {
1041052d159aSCy Schubert       if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]);
1042052d159aSCy Schubert     }
1043052d159aSCy Schubert     free(tmp->notes_data);
1044052d159aSCy Schubert     tmp->notes_data = NULL;
1045052d159aSCy Schubert     tmp->notes_set = 0;
1046052d159aSCy Schubert     tmp->notes_length = 0;
1047052d159aSCy Schubert     tmp->notes_num_allocated = 0;
1048052d159aSCy Schubert   }
1049052d159aSCy Schubert   free(tmp->notes_data);
1050052d159aSCy Schubert   if (tmp->other_numbers_set == 1) {
1051052d159aSCy Schubert     free(tmp->other_numbers_data);
1052052d159aSCy Schubert     tmp->other_numbers_data = NULL;
1053052d159aSCy Schubert     tmp->other_numbers_set = 0;
1054052d159aSCy Schubert     tmp->other_numbers_length = 0;
1055052d159aSCy Schubert     tmp->other_numbers_num_allocated = 0;
1056052d159aSCy Schubert   }
1057052d159aSCy Schubert   free(tmp->other_numbers_data);
1058052d159aSCy Schubert   free(tmp);
1059052d159aSCy Schubert }
1060052d159aSCy Schubert 
1061052d159aSCy Schubert void
run_marshal(struct evbuffer * evbuf,const struct run * tmp)1062052d159aSCy Schubert run_marshal(struct evbuffer *evbuf, const struct run *tmp) {
1063052d159aSCy Schubert   evtag_marshal_string(evbuf, RUN_HOW, tmp->how_data);
1064052d159aSCy Schubert   if (tmp->some_bytes_set) {
1065052d159aSCy Schubert     evtag_marshal(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length);
1066052d159aSCy Schubert   }
1067052d159aSCy Schubert   evtag_marshal(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24));
1068052d159aSCy Schubert   if (tmp->notes_set) {
1069052d159aSCy Schubert     {
1070052d159aSCy Schubert       int i;
1071052d159aSCy Schubert       for (i = 0; i < tmp->notes_length; ++i) {
1072052d159aSCy Schubert     evtag_marshal_string(evbuf, RUN_NOTES, tmp->notes_data[i]);
1073052d159aSCy Schubert       }
1074052d159aSCy Schubert     }
1075052d159aSCy Schubert   }
1076052d159aSCy Schubert   if (tmp->large_number_set) {
1077052d159aSCy Schubert     evtag_marshal_int64(evbuf, RUN_LARGE_NUMBER, tmp->large_number_data);
1078052d159aSCy Schubert   }
1079052d159aSCy Schubert   if (tmp->other_numbers_set) {
1080052d159aSCy Schubert     {
1081052d159aSCy Schubert       int i;
1082052d159aSCy Schubert       for (i = 0; i < tmp->other_numbers_length; ++i) {
1083052d159aSCy Schubert     evtag_marshal_int(evbuf, RUN_OTHER_NUMBERS, tmp->other_numbers_data[i]);
1084052d159aSCy Schubert       }
1085052d159aSCy Schubert     }
1086052d159aSCy Schubert   }
1087052d159aSCy Schubert }
1088052d159aSCy Schubert 
1089052d159aSCy Schubert int
run_unmarshal(struct run * tmp,struct evbuffer * evbuf)1090052d159aSCy Schubert run_unmarshal(struct run *tmp, struct evbuffer *evbuf)
1091052d159aSCy Schubert {
1092052d159aSCy Schubert   ev_uint32_t tag;
1093052d159aSCy Schubert   while (evbuffer_get_length(evbuf) > 0) {
1094052d159aSCy Schubert     if (evtag_peek(evbuf, &tag) == -1)
1095052d159aSCy Schubert       return (-1);
1096052d159aSCy Schubert     switch (tag) {
1097052d159aSCy Schubert 
1098052d159aSCy Schubert       case RUN_HOW:
1099052d159aSCy Schubert         if (tmp->how_set)
1100052d159aSCy Schubert           return (-1);
1101052d159aSCy Schubert         if (evtag_unmarshal_string(evbuf, RUN_HOW, &tmp->how_data) == -1) {
1102052d159aSCy Schubert           event_warnx("%s: failed to unmarshal how", __func__);
1103052d159aSCy Schubert           return (-1);
1104052d159aSCy Schubert         }
1105052d159aSCy Schubert         tmp->how_set = 1;
1106052d159aSCy Schubert         break;
1107052d159aSCy Schubert       case RUN_SOME_BYTES:
1108052d159aSCy Schubert         if (tmp->some_bytes_set)
1109052d159aSCy Schubert           return (-1);
1110052d159aSCy Schubert         if (evtag_payload_length(evbuf, &tmp->some_bytes_length) == -1)
1111052d159aSCy Schubert           return (-1);
1112052d159aSCy Schubert         if (tmp->some_bytes_length > evbuffer_get_length(evbuf))
1113052d159aSCy Schubert           return (-1);
1114052d159aSCy Schubert         if ((tmp->some_bytes_data = malloc(tmp->some_bytes_length)) == NULL)
1115052d159aSCy Schubert           return (-1);
1116052d159aSCy Schubert         if (evtag_unmarshal_fixed(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length) == -1) {
1117052d159aSCy Schubert           event_warnx("%s: failed to unmarshal some_bytes", __func__);
1118052d159aSCy Schubert           return (-1);
1119052d159aSCy Schubert         }
1120052d159aSCy Schubert         tmp->some_bytes_set = 1;
1121052d159aSCy Schubert         break;
1122052d159aSCy Schubert       case RUN_FIXED_BYTES:
1123052d159aSCy Schubert         if (tmp->fixed_bytes_set)
1124052d159aSCy Schubert           return (-1);
1125052d159aSCy Schubert         if (evtag_unmarshal_fixed(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24)) == -1) {
1126052d159aSCy Schubert           event_warnx("%s: failed to unmarshal fixed_bytes", __func__);
1127052d159aSCy Schubert           return (-1);
1128052d159aSCy Schubert         }
1129052d159aSCy Schubert         tmp->fixed_bytes_set = 1;
1130052d159aSCy Schubert         break;
1131052d159aSCy Schubert       case RUN_NOTES:
1132052d159aSCy Schubert         if (tmp->notes_length >= tmp->notes_num_allocated &&
1133052d159aSCy Schubert             run_notes_expand_to_hold_more(tmp) < 0) {
1134052d159aSCy Schubert           puts("HEY NOW");
1135052d159aSCy Schubert           return (-1);
1136052d159aSCy Schubert         }
1137052d159aSCy Schubert         if (evtag_unmarshal_string(evbuf, RUN_NOTES, &tmp->notes_data[tmp->notes_length]) == -1) {
1138052d159aSCy Schubert           event_warnx("%s: failed to unmarshal notes", __func__);
1139052d159aSCy Schubert           return (-1);
1140052d159aSCy Schubert         }
1141052d159aSCy Schubert         ++tmp->notes_length;
1142052d159aSCy Schubert         tmp->notes_set = 1;
1143052d159aSCy Schubert         break;
1144052d159aSCy Schubert       case RUN_LARGE_NUMBER:
1145052d159aSCy Schubert         if (tmp->large_number_set)
1146052d159aSCy Schubert           return (-1);
1147052d159aSCy Schubert         if (evtag_unmarshal_int64(evbuf, RUN_LARGE_NUMBER, &tmp->large_number_data) == -1) {
1148052d159aSCy Schubert           event_warnx("%s: failed to unmarshal large_number", __func__);
1149052d159aSCy Schubert           return (-1);
1150052d159aSCy Schubert         }
1151052d159aSCy Schubert         tmp->large_number_set = 1;
1152052d159aSCy Schubert         break;
1153052d159aSCy Schubert       case RUN_OTHER_NUMBERS:
1154052d159aSCy Schubert         if (tmp->other_numbers_length >= tmp->other_numbers_num_allocated &&
1155052d159aSCy Schubert             run_other_numbers_expand_to_hold_more(tmp) < 0) {
1156052d159aSCy Schubert           puts("HEY NOW");
1157052d159aSCy Schubert           return (-1);
1158052d159aSCy Schubert         }
1159052d159aSCy Schubert         if (evtag_unmarshal_int(evbuf, RUN_OTHER_NUMBERS, &tmp->other_numbers_data[tmp->other_numbers_length]) == -1) {
1160052d159aSCy Schubert           event_warnx("%s: failed to unmarshal other_numbers", __func__);
1161052d159aSCy Schubert           return (-1);
1162052d159aSCy Schubert         }
1163052d159aSCy Schubert         ++tmp->other_numbers_length;
1164052d159aSCy Schubert         tmp->other_numbers_set = 1;
1165052d159aSCy Schubert         break;
1166052d159aSCy Schubert       default:
1167052d159aSCy Schubert         return -1;
1168052d159aSCy Schubert     }
1169052d159aSCy Schubert   }
1170052d159aSCy Schubert 
1171052d159aSCy Schubert   if (run_complete(tmp) == -1)
1172052d159aSCy Schubert     return (-1);
1173052d159aSCy Schubert   return (0);
1174052d159aSCy Schubert }
1175052d159aSCy Schubert 
1176052d159aSCy Schubert int
run_complete(struct run * msg)1177052d159aSCy Schubert run_complete(struct run *msg)
1178052d159aSCy Schubert {
1179052d159aSCy Schubert   if (!msg->how_set)
1180052d159aSCy Schubert     return (-1);
1181052d159aSCy Schubert   if (!msg->fixed_bytes_set)
1182052d159aSCy Schubert     return (-1);
1183052d159aSCy Schubert   return (0);
1184052d159aSCy Schubert }
1185052d159aSCy Schubert 
1186052d159aSCy Schubert int
evtag_unmarshal_run(struct evbuffer * evbuf,ev_uint32_t need_tag,struct run * msg)1187*a466cc55SCy Schubert evtag_unmarshal_run(struct evbuffer *evbuf, ev_uint32_t need_tag,
1188*a466cc55SCy Schubert   struct run *msg)
1189052d159aSCy Schubert {
1190052d159aSCy Schubert   ev_uint32_t tag;
1191052d159aSCy Schubert   int res = -1;
1192052d159aSCy Schubert 
1193052d159aSCy Schubert   struct evbuffer *tmp = evbuffer_new();
1194052d159aSCy Schubert 
1195052d159aSCy Schubert   if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
1196052d159aSCy Schubert     goto error;
1197052d159aSCy Schubert 
1198052d159aSCy Schubert   if (run_unmarshal(msg, tmp) == -1)
1199052d159aSCy Schubert     goto error;
1200052d159aSCy Schubert 
1201052d159aSCy Schubert   res = 0;
1202052d159aSCy Schubert 
1203052d159aSCy Schubert  error:
1204052d159aSCy Schubert   evbuffer_free(tmp);
1205052d159aSCy Schubert   return (res);
1206052d159aSCy Schubert }
1207052d159aSCy Schubert 
1208052d159aSCy Schubert void
evtag_marshal_run(struct evbuffer * evbuf,ev_uint32_t tag,const struct run * msg)1209*a466cc55SCy Schubert evtag_marshal_run(struct evbuffer *evbuf, ev_uint32_t tag,
1210*a466cc55SCy Schubert     const struct run *msg)
1211052d159aSCy Schubert {
1212052d159aSCy Schubert   struct evbuffer *buf_ = evbuffer_new();
1213052d159aSCy Schubert   assert(buf_ != NULL);
1214052d159aSCy Schubert   run_marshal(buf_, msg);
1215052d159aSCy Schubert   evtag_marshal_buffer(evbuf, tag, buf_);
1216052d159aSCy Schubert   evbuffer_free(buf_);
1217052d159aSCy Schubert }
12182b15cb3dSCy Schubert 
1219