1344884Scy/*
2358659Scy * Automatically generated from /ntpbuild/data/snaps/ntp-stable/sntp/libevent/test/regress.rpc
3344884Scy * by event_rpcgen.py/0.1.  DO NOT EDIT THIS FILE.
4344884Scy */
5344884Scy
6344884Scy#include <stdlib.h>
7344884Scy#include <string.h>
8344884Scy#include <assert.h>
9344884Scy#include <event2/event-config.h>
10344884Scy#include <event2/event.h>
11344884Scy#include <event2/buffer.h>
12344884Scy#include <event2/tag.h>
13344884Scy
14344884Scy#ifdef EVENT____func__
15344884Scy#define __func__ EVENT____func__
16344884Scy#endif
17344884Scy
18344884Scy
19344884Scy#include "regress.gen.h"
20344884Scy
21344884Scyvoid event_warn(const char *fmt, ...);
22344884Scyvoid event_warnx(const char *fmt, ...);
23344884Scy
24344884Scy
25344884Scy/*
26344884Scy * Implementation of msg
27344884Scy */
28344884Scy
29344884Scystatic struct msg_access_ msg_base__ = {
30344884Scy  msg_from_name_assign,
31344884Scy  msg_from_name_get,
32344884Scy  msg_to_name_assign,
33344884Scy  msg_to_name_get,
34344884Scy  msg_attack_assign,
35344884Scy  msg_attack_get,
36344884Scy  msg_run_assign,
37344884Scy  msg_run_get,
38344884Scy  msg_run_add,
39344884Scy};
40344884Scy
41344884Scystruct msg *
42344884Scymsg_new(void)
43344884Scy{
44344884Scy  return msg_new_with_arg(NULL);
45344884Scy}
46344884Scy
47344884Scystruct msg *
48344884Scymsg_new_with_arg(void *unused)
49344884Scy{
50344884Scy  struct msg *tmp;
51344884Scy  if ((tmp = malloc(sizeof(struct msg))) == NULL) {
52344884Scy    event_warn("%s: malloc", __func__);
53344884Scy    return (NULL);
54344884Scy  }
55344884Scy  tmp->base = &msg_base__;
56344884Scy
57344884Scy  tmp->from_name_data = NULL;
58344884Scy  tmp->from_name_set = 0;
59344884Scy
60344884Scy  tmp->to_name_data = NULL;
61344884Scy  tmp->to_name_set = 0;
62344884Scy
63344884Scy  tmp->attack_data = NULL;
64344884Scy  tmp->attack_set = 0;
65344884Scy
66344884Scy  tmp->run_data = NULL;
67344884Scy  tmp->run_length = 0;
68344884Scy  tmp->run_num_allocated = 0;
69344884Scy  tmp->run_set = 0;
70344884Scy
71344884Scy  return (tmp);
72344884Scy}
73344884Scy
74344884Scy
75344884Scy
76344884Scy
77344884Scystatic int
78344884Scymsg_run_expand_to_hold_more(struct msg *msg)
79344884Scy{
80344884Scy  int tobe_allocated = msg->run_num_allocated;
81344884Scy  struct run** new_data = NULL;
82344884Scy  tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
83344884Scy  new_data = (struct run**) realloc(msg->run_data,
84344884Scy      tobe_allocated * sizeof(struct run*));
85344884Scy  if (new_data == NULL)
86344884Scy    return -1;
87344884Scy  msg->run_data = new_data;
88344884Scy  msg->run_num_allocated = tobe_allocated;
89344884Scy  return 0;}
90344884Scy
91344884Scystruct run*
92344884Scymsg_run_add(struct msg *msg)
93344884Scy{
94344884Scy  if (++msg->run_length >= msg->run_num_allocated) {
95344884Scy    if (msg_run_expand_to_hold_more(msg)<0)
96344884Scy      goto error;
97344884Scy  }
98344884Scy  msg->run_data[msg->run_length - 1] = run_new();
99344884Scy  if (msg->run_data[msg->run_length - 1] == NULL)
100344884Scy    goto error;
101344884Scy  msg->run_set = 1;
102344884Scy  return (msg->run_data[msg->run_length - 1]);
103344884Scyerror:
104344884Scy  --msg->run_length;
105344884Scy  return (NULL);
106344884Scy}
107344884Scy
108344884Scyint
109344884Scymsg_from_name_assign(struct msg *msg,
110344884Scy    const char * value)
111344884Scy{
112344884Scy  if (msg->from_name_data != NULL)
113344884Scy    free(msg->from_name_data);
114344884Scy  if ((msg->from_name_data = strdup(value)) == NULL)
115344884Scy    return (-1);
116344884Scy  msg->from_name_set = 1;
117344884Scy  return (0);
118344884Scy}
119344884Scy
120344884Scyint
121344884Scymsg_to_name_assign(struct msg *msg,
122344884Scy    const char * value)
123344884Scy{
124344884Scy  if (msg->to_name_data != NULL)
125344884Scy    free(msg->to_name_data);
126344884Scy  if ((msg->to_name_data = strdup(value)) == NULL)
127344884Scy    return (-1);
128344884Scy  msg->to_name_set = 1;
129344884Scy  return (0);
130344884Scy}
131344884Scy
132344884Scyint
133344884Scymsg_attack_assign(struct msg *msg,
134344884Scy    const struct kill* value)
135344884Scy{
136344884Scy   struct evbuffer *tmp = NULL;
137344884Scy   if (msg->attack_set) {
138344884Scy     kill_clear(msg->attack_data);
139344884Scy     msg->attack_set = 0;
140344884Scy   } else {
141344884Scy     msg->attack_data = kill_new();
142344884Scy     if (msg->attack_data == NULL) {
143344884Scy       event_warn("%s: kill_new()", __func__);
144344884Scy       goto error;
145344884Scy     }
146344884Scy   }
147344884Scy   if ((tmp = evbuffer_new()) == NULL) {
148344884Scy     event_warn("%s: evbuffer_new()", __func__);
149344884Scy     goto error;
150344884Scy   }
151344884Scy   kill_marshal(tmp, value);
152344884Scy   if (kill_unmarshal(msg->attack_data, tmp) == -1) {
153344884Scy     event_warnx("%s: kill_unmarshal", __func__);
154344884Scy     goto error;
155344884Scy   }
156344884Scy   msg->attack_set = 1;
157344884Scy   evbuffer_free(tmp);
158344884Scy   return (0);
159344884Scy error:
160344884Scy   if (tmp != NULL)
161344884Scy     evbuffer_free(tmp);
162344884Scy   if (msg->attack_data != NULL) {
163344884Scy     kill_free(msg->attack_data);
164344884Scy     msg->attack_data = NULL;
165344884Scy   }
166344884Scy   return (-1);
167344884Scy}
168344884Scy
169344884Scyint
170344884Scymsg_run_assign(struct msg *msg, int off,
171344884Scy    const struct run* value)
172344884Scy{
173344884Scy  if (!msg->run_set || off < 0 || off >= msg->run_length)
174344884Scy    return (-1);
175344884Scy
176344884Scy  {
177344884Scy    int had_error = 0;
178344884Scy    struct evbuffer *tmp = NULL;
179344884Scy    run_clear(msg->run_data[off]);
180344884Scy    if ((tmp = evbuffer_new()) == NULL) {
181344884Scy      event_warn("%s: evbuffer_new()", __func__);
182344884Scy      had_error = 1;
183344884Scy      goto done;
184344884Scy    }
185344884Scy    run_marshal(tmp, value);
186344884Scy    if (run_unmarshal(msg->run_data[off], tmp) == -1) {
187344884Scy      event_warnx("%s: run_unmarshal", __func__);
188344884Scy      had_error = 1;
189344884Scy      goto done;
190344884Scy    }
191344884Scy    done:if (tmp != NULL)
192344884Scy      evbuffer_free(tmp);
193344884Scy    if (had_error) {
194344884Scy      run_clear(msg->run_data[off]);
195344884Scy      return (-1);
196344884Scy    }
197344884Scy  }
198344884Scy  return (0);
199344884Scy}
200344884Scy
201344884Scyint
202344884Scymsg_from_name_get(struct msg *msg, char * *value)
203344884Scy{
204344884Scy  if (msg->from_name_set != 1)
205344884Scy    return (-1);
206344884Scy  *value = msg->from_name_data;
207344884Scy  return (0);
208344884Scy}
209344884Scy
210344884Scyint
211344884Scymsg_to_name_get(struct msg *msg, char * *value)
212344884Scy{
213344884Scy  if (msg->to_name_set != 1)
214344884Scy    return (-1);
215344884Scy  *value = msg->to_name_data;
216344884Scy  return (0);
217344884Scy}
218344884Scy
219344884Scyint
220344884Scymsg_attack_get(struct msg *msg, struct kill* *value)
221344884Scy{
222344884Scy  if (msg->attack_set != 1) {
223344884Scy    msg->attack_data = kill_new();
224344884Scy    if (msg->attack_data == NULL)
225344884Scy      return (-1);
226344884Scy    msg->attack_set = 1;
227344884Scy  }
228344884Scy  *value = msg->attack_data;
229344884Scy  return (0);
230344884Scy}
231344884Scy
232344884Scyint
233344884Scymsg_run_get(struct msg *msg, int offset,
234344884Scy    struct run* *value)
235344884Scy{
236344884Scy  if (!msg->run_set || offset < 0 || offset >= msg->run_length)
237344884Scy    return (-1);
238344884Scy  *value = msg->run_data[offset];
239344884Scy  return (0);
240344884Scy}
241344884Scy
242344884Scyvoid
243344884Scymsg_clear(struct msg *tmp)
244344884Scy{
245344884Scy  if (tmp->from_name_set == 1) {
246344884Scy    free(tmp->from_name_data);
247344884Scy    tmp->from_name_data = NULL;
248344884Scy    tmp->from_name_set = 0;
249344884Scy  }
250344884Scy  if (tmp->to_name_set == 1) {
251344884Scy    free(tmp->to_name_data);
252344884Scy    tmp->to_name_data = NULL;
253344884Scy    tmp->to_name_set = 0;
254344884Scy  }
255344884Scy  if (tmp->attack_set == 1) {
256344884Scy    kill_free(tmp->attack_data);
257344884Scy    tmp->attack_data = NULL;
258344884Scy    tmp->attack_set = 0;
259344884Scy  }
260344884Scy  if (tmp->run_set == 1) {
261344884Scy    int i;
262344884Scy    for (i = 0; i < tmp->run_length; ++i) {
263344884Scy      run_free(tmp->run_data[i]);
264344884Scy    }
265344884Scy    free(tmp->run_data);
266344884Scy    tmp->run_data = NULL;
267344884Scy    tmp->run_set = 0;
268344884Scy    tmp->run_length = 0;
269344884Scy    tmp->run_num_allocated = 0;
270344884Scy  }
271344884Scy}
272344884Scy
273344884Scyvoid
274344884Scymsg_free(struct msg *tmp)
275344884Scy{
276344884Scy  if (tmp->from_name_data != NULL)
277344884Scy      free (tmp->from_name_data);
278344884Scy  if (tmp->to_name_data != NULL)
279344884Scy      free (tmp->to_name_data);
280344884Scy  if (tmp->attack_data != NULL)
281344884Scy      kill_free(tmp->attack_data);
282344884Scy  if (tmp->run_set == 1) {
283344884Scy    int i;
284344884Scy    for (i = 0; i < tmp->run_length; ++i) {
285344884Scy      run_free(tmp->run_data[i]);
286344884Scy    }
287344884Scy    free(tmp->run_data);
288344884Scy    tmp->run_data = NULL;
289344884Scy    tmp->run_set = 0;
290344884Scy    tmp->run_length = 0;
291344884Scy    tmp->run_num_allocated = 0;
292344884Scy  }
293344884Scy  free(tmp->run_data);
294344884Scy  free(tmp);
295344884Scy}
296344884Scy
297344884Scyvoid
298344884Scymsg_marshal(struct evbuffer *evbuf, const struct msg *tmp){
299344884Scy  evtag_marshal_string(evbuf, MSG_FROM_NAME, tmp->from_name_data);
300344884Scy  evtag_marshal_string(evbuf, MSG_TO_NAME, tmp->to_name_data);
301344884Scy  if (tmp->attack_set) {
302344884Scy    evtag_marshal_kill(evbuf, MSG_ATTACK, tmp->attack_data);
303344884Scy  }
304344884Scy  if (tmp->run_set) {
305344884Scy    {
306344884Scy      int i;
307344884Scy      for (i = 0; i < tmp->run_length; ++i) {
308344884Scy    evtag_marshal_run(evbuf, MSG_RUN, tmp->run_data[i]);
309344884Scy      }
310344884Scy    }
311344884Scy  }
312344884Scy}
313344884Scy
314344884Scyint
315344884Scymsg_unmarshal(struct msg *tmp,  struct evbuffer *evbuf)
316344884Scy{
317344884Scy  ev_uint32_t tag;
318344884Scy  while (evbuffer_get_length(evbuf) > 0) {
319344884Scy    if (evtag_peek(evbuf, &tag) == -1)
320344884Scy      return (-1);
321344884Scy    switch (tag) {
322344884Scy
323344884Scy      case MSG_FROM_NAME:
324344884Scy
325344884Scy        if (tmp->from_name_set)
326344884Scy          return (-1);
327344884Scy        if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) {
328344884Scy          event_warnx("%s: failed to unmarshal from_name", __func__);
329344884Scy          return (-1);
330344884Scy        }
331344884Scy        tmp->from_name_set = 1;
332344884Scy        break;
333344884Scy
334344884Scy      case MSG_TO_NAME:
335344884Scy
336344884Scy        if (tmp->to_name_set)
337344884Scy          return (-1);
338344884Scy        if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) {
339344884Scy          event_warnx("%s: failed to unmarshal to_name", __func__);
340344884Scy          return (-1);
341344884Scy        }
342344884Scy        tmp->to_name_set = 1;
343344884Scy        break;
344344884Scy
345344884Scy      case MSG_ATTACK:
346344884Scy
347344884Scy        if (tmp->attack_set)
348344884Scy          return (-1);
349344884Scy        tmp->attack_data = kill_new();
350344884Scy        if (tmp->attack_data == NULL)
351344884Scy          return (-1);
352344884Scy        if (evtag_unmarshal_kill(evbuf, MSG_ATTACK, tmp->attack_data) == -1) {
353344884Scy          event_warnx("%s: failed to unmarshal attack", __func__);
354344884Scy          return (-1);
355344884Scy        }
356344884Scy        tmp->attack_set = 1;
357344884Scy        break;
358344884Scy
359344884Scy      case MSG_RUN:
360344884Scy
361344884Scy        if (tmp->run_length >= tmp->run_num_allocated &&
362344884Scy            msg_run_expand_to_hold_more(tmp) < 0) {
363344884Scy          puts("HEY NOW");
364344884Scy          return (-1);
365344884Scy        }
366344884Scy        tmp->run_data[tmp->run_length] = run_new();
367344884Scy        if (tmp->run_data[tmp->run_length] == NULL)
368344884Scy          return (-1);
369344884Scy        if (evtag_unmarshal_run(evbuf, MSG_RUN, tmp->run_data[tmp->run_length]) == -1) {
370344884Scy          event_warnx("%s: failed to unmarshal run", __func__);
371344884Scy          return (-1);
372344884Scy        }
373344884Scy        ++tmp->run_length;
374344884Scy        tmp->run_set = 1;
375344884Scy        break;
376344884Scy
377344884Scy      default:
378344884Scy        return -1;
379344884Scy    }
380344884Scy  }
381344884Scy
382344884Scy  if (msg_complete(tmp) == -1)
383344884Scy    return (-1);
384344884Scy  return (0);
385344884Scy}
386344884Scy
387344884Scyint
388344884Scymsg_complete(struct msg *msg)
389344884Scy{
390344884Scy  if (!msg->from_name_set)
391344884Scy    return (-1);
392344884Scy  if (!msg->to_name_set)
393344884Scy    return (-1);
394344884Scy  if (msg->attack_set && kill_complete(msg->attack_data) == -1)
395344884Scy    return (-1);
396344884Scy  {
397344884Scy    int i;
398344884Scy    for (i = 0; i < msg->run_length; ++i) {
399344884Scy      if (msg->run_set && run_complete(msg->run_data[i]) == -1)
400344884Scy        return (-1);
401344884Scy    }
402344884Scy  }
403344884Scy  return (0);
404344884Scy}
405344884Scy
406344884Scyint
407344884Scyevtag_unmarshal_msg(struct evbuffer *evbuf, ev_uint32_t need_tag, struct msg *msg)
408344884Scy{
409344884Scy  ev_uint32_t tag;
410344884Scy  int res = -1;
411344884Scy
412344884Scy  struct evbuffer *tmp = evbuffer_new();
413344884Scy
414344884Scy  if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
415344884Scy    goto error;
416344884Scy
417344884Scy  if (msg_unmarshal(msg, tmp) == -1)
418344884Scy    goto error;
419344884Scy
420344884Scy  res = 0;
421344884Scy
422344884Scy error:
423344884Scy  evbuffer_free(tmp);
424344884Scy  return (res);
425344884Scy}
426344884Scy
427344884Scyvoid
428344884Scyevtag_marshal_msg(struct evbuffer *evbuf, ev_uint32_t tag, const struct msg *msg)
429344884Scy{
430344884Scy  struct evbuffer *buf_ = evbuffer_new();
431344884Scy  assert(buf_ != NULL);
432344884Scy  msg_marshal(buf_, msg);
433344884Scy  evtag_marshal_buffer(evbuf, tag, buf_);
434344884Scy   evbuffer_free(buf_);
435344884Scy}
436344884Scy
437344884Scy/*
438344884Scy * Implementation of kill
439344884Scy */
440344884Scy
441344884Scystatic struct kill_access_ kill_base__ = {
442344884Scy  kill_weapon_assign,
443344884Scy  kill_weapon_get,
444344884Scy  kill_action_assign,
445344884Scy  kill_action_get,
446344884Scy  kill_how_often_assign,
447344884Scy  kill_how_often_get,
448344884Scy  kill_how_often_add,
449344884Scy};
450344884Scy
451344884Scystruct kill *
452344884Scykill_new(void)
453344884Scy{
454344884Scy  return kill_new_with_arg(NULL);
455344884Scy}
456344884Scy
457344884Scystruct kill *
458344884Scykill_new_with_arg(void *unused)
459344884Scy{
460344884Scy  struct kill *tmp;
461344884Scy  if ((tmp = malloc(sizeof(struct kill))) == NULL) {
462344884Scy    event_warn("%s: malloc", __func__);
463344884Scy    return (NULL);
464344884Scy  }
465344884Scy  tmp->base = &kill_base__;
466344884Scy
467344884Scy  tmp->weapon_data = NULL;
468344884Scy  tmp->weapon_set = 0;
469344884Scy
470344884Scy  tmp->action_data = NULL;
471344884Scy  tmp->action_set = 0;
472344884Scy
473344884Scy  tmp->how_often_data = NULL;
474344884Scy  tmp->how_often_length = 0;
475344884Scy  tmp->how_often_num_allocated = 0;
476344884Scy  tmp->how_often_set = 0;
477344884Scy
478344884Scy  return (tmp);
479344884Scy}
480344884Scy
481344884Scy
482344884Scy
483344884Scystatic int
484344884Scykill_how_often_expand_to_hold_more(struct kill *msg)
485344884Scy{
486344884Scy  int tobe_allocated = msg->how_often_num_allocated;
487344884Scy  ev_uint32_t* new_data = NULL;
488344884Scy  tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
489344884Scy  new_data = (ev_uint32_t*) realloc(msg->how_often_data,
490344884Scy      tobe_allocated * sizeof(ev_uint32_t));
491344884Scy  if (new_data == NULL)
492344884Scy    return -1;
493344884Scy  msg->how_often_data = new_data;
494344884Scy  msg->how_often_num_allocated = tobe_allocated;
495344884Scy  return 0;}
496344884Scy
497344884Scyev_uint32_t *
498344884Scykill_how_often_add(struct kill *msg, const ev_uint32_t value)
499344884Scy{
500344884Scy  if (++msg->how_often_length >= msg->how_often_num_allocated) {
501344884Scy    if (kill_how_often_expand_to_hold_more(msg)<0)
502344884Scy      goto error;
503344884Scy  }
504344884Scy  msg->how_often_data[msg->how_often_length - 1] = value;
505344884Scy  msg->how_often_set = 1;
506344884Scy  return &(msg->how_often_data[msg->how_often_length - 1]);
507344884Scyerror:
508344884Scy  --msg->how_often_length;
509344884Scy  return (NULL);
510344884Scy}
511344884Scy
512344884Scyint
513344884Scykill_weapon_assign(struct kill *msg,
514344884Scy    const char * value)
515344884Scy{
516344884Scy  if (msg->weapon_data != NULL)
517344884Scy    free(msg->weapon_data);
518344884Scy  if ((msg->weapon_data = strdup(value)) == NULL)
519344884Scy    return (-1);
520344884Scy  msg->weapon_set = 1;
521344884Scy  return (0);
522344884Scy}
523344884Scy
524344884Scyint
525344884Scykill_action_assign(struct kill *msg,
526344884Scy    const char * value)
527344884Scy{
528344884Scy  if (msg->action_data != NULL)
529344884Scy    free(msg->action_data);
530344884Scy  if ((msg->action_data = strdup(value)) == NULL)
531344884Scy    return (-1);
532344884Scy  msg->action_set = 1;
533344884Scy  return (0);
534344884Scy}
535344884Scy
536344884Scyint
537344884Scykill_how_often_assign(struct kill *msg, int off,
538344884Scy    const ev_uint32_t value)
539344884Scy{
540344884Scy  if (!msg->how_often_set || off < 0 || off >= msg->how_often_length)
541344884Scy    return (-1);
542344884Scy
543344884Scy  {
544344884Scy    msg->how_often_data[off] = value;
545344884Scy  }
546344884Scy  return (0);
547344884Scy}
548344884Scy
549344884Scyint
550344884Scykill_weapon_get(struct kill *msg, char * *value)
551344884Scy{
552344884Scy  if (msg->weapon_set != 1)
553344884Scy    return (-1);
554344884Scy  *value = msg->weapon_data;
555344884Scy  return (0);
556344884Scy}
557344884Scy
558344884Scyint
559344884Scykill_action_get(struct kill *msg, char * *value)
560344884Scy{
561344884Scy  if (msg->action_set != 1)
562344884Scy    return (-1);
563344884Scy  *value = msg->action_data;
564344884Scy  return (0);
565344884Scy}
566344884Scy
567344884Scyint
568344884Scykill_how_often_get(struct kill *msg, int offset,
569344884Scy    ev_uint32_t *value)
570344884Scy{
571344884Scy  if (!msg->how_often_set || offset < 0 || offset >= msg->how_often_length)
572344884Scy    return (-1);
573344884Scy  *value = msg->how_often_data[offset];
574344884Scy  return (0);
575344884Scy}
576344884Scy
577344884Scyvoid
578344884Scykill_clear(struct kill *tmp)
579344884Scy{
580344884Scy  if (tmp->weapon_set == 1) {
581344884Scy    free(tmp->weapon_data);
582344884Scy    tmp->weapon_data = NULL;
583344884Scy    tmp->weapon_set = 0;
584344884Scy  }
585344884Scy  if (tmp->action_set == 1) {
586344884Scy    free(tmp->action_data);
587344884Scy    tmp->action_data = NULL;
588344884Scy    tmp->action_set = 0;
589344884Scy  }
590344884Scy  if (tmp->how_often_set == 1) {
591344884Scy    free(tmp->how_often_data);
592344884Scy    tmp->how_often_data = NULL;
593344884Scy    tmp->how_often_set = 0;
594344884Scy    tmp->how_often_length = 0;
595344884Scy    tmp->how_often_num_allocated = 0;
596344884Scy  }
597344884Scy}
598344884Scy
599344884Scyvoid
600344884Scykill_free(struct kill *tmp)
601344884Scy{
602344884Scy  if (tmp->weapon_data != NULL)
603344884Scy      free (tmp->weapon_data);
604344884Scy  if (tmp->action_data != NULL)
605344884Scy      free (tmp->action_data);
606344884Scy  if (tmp->how_often_set == 1) {
607344884Scy    free(tmp->how_often_data);
608344884Scy    tmp->how_often_data = NULL;
609344884Scy    tmp->how_often_set = 0;
610344884Scy    tmp->how_often_length = 0;
611344884Scy    tmp->how_often_num_allocated = 0;
612344884Scy  }
613344884Scy  free(tmp->how_often_data);
614344884Scy  free(tmp);
615344884Scy}
616344884Scy
617344884Scyvoid
618344884Scykill_marshal(struct evbuffer *evbuf, const struct kill *tmp){
619344884Scy  evtag_marshal_string(evbuf, KILL_WEAPON, tmp->weapon_data);
620344884Scy  evtag_marshal_string(evbuf, KILL_ACTION, tmp->action_data);
621344884Scy  if (tmp->how_often_set) {
622344884Scy    {
623344884Scy      int i;
624344884Scy      for (i = 0; i < tmp->how_often_length; ++i) {
625344884Scy    evtag_marshal_int(evbuf, KILL_HOW_OFTEN, tmp->how_often_data[i]);
626344884Scy      }
627344884Scy    }
628344884Scy  }
629344884Scy}
630344884Scy
631344884Scyint
632344884Scykill_unmarshal(struct kill *tmp,  struct evbuffer *evbuf)
633344884Scy{
634344884Scy  ev_uint32_t tag;
635344884Scy  while (evbuffer_get_length(evbuf) > 0) {
636344884Scy    if (evtag_peek(evbuf, &tag) == -1)
637344884Scy      return (-1);
638344884Scy    switch (tag) {
639344884Scy
640344884Scy      case KILL_WEAPON:
641344884Scy
642344884Scy        if (tmp->weapon_set)
643344884Scy          return (-1);
644344884Scy        if (evtag_unmarshal_string(evbuf, KILL_WEAPON, &tmp->weapon_data) == -1) {
645344884Scy          event_warnx("%s: failed to unmarshal weapon", __func__);
646344884Scy          return (-1);
647344884Scy        }
648344884Scy        tmp->weapon_set = 1;
649344884Scy        break;
650344884Scy
651344884Scy      case KILL_ACTION:
652344884Scy
653344884Scy        if (tmp->action_set)
654344884Scy          return (-1);
655344884Scy        if (evtag_unmarshal_string(evbuf, KILL_ACTION, &tmp->action_data) == -1) {
656344884Scy          event_warnx("%s: failed to unmarshal action", __func__);
657344884Scy          return (-1);
658344884Scy        }
659344884Scy        tmp->action_set = 1;
660344884Scy        break;
661344884Scy
662344884Scy      case KILL_HOW_OFTEN:
663344884Scy
664344884Scy        if (tmp->how_often_length >= tmp->how_often_num_allocated &&
665344884Scy            kill_how_often_expand_to_hold_more(tmp) < 0) {
666344884Scy          puts("HEY NOW");
667344884Scy          return (-1);
668344884Scy        }
669344884Scy        if (evtag_unmarshal_int(evbuf, KILL_HOW_OFTEN, &tmp->how_often_data[tmp->how_often_length]) == -1) {
670344884Scy          event_warnx("%s: failed to unmarshal how_often", __func__);
671344884Scy          return (-1);
672344884Scy        }
673344884Scy        ++tmp->how_often_length;
674344884Scy        tmp->how_often_set = 1;
675344884Scy        break;
676344884Scy
677344884Scy      default:
678344884Scy        return -1;
679344884Scy    }
680344884Scy  }
681344884Scy
682344884Scy  if (kill_complete(tmp) == -1)
683344884Scy    return (-1);
684344884Scy  return (0);
685344884Scy}
686344884Scy
687344884Scyint
688344884Scykill_complete(struct kill *msg)
689344884Scy{
690344884Scy  if (!msg->weapon_set)
691344884Scy    return (-1);
692344884Scy  if (!msg->action_set)
693344884Scy    return (-1);
694344884Scy  return (0);
695344884Scy}
696344884Scy
697344884Scyint
698344884Scyevtag_unmarshal_kill(struct evbuffer *evbuf, ev_uint32_t need_tag, struct kill *msg)
699344884Scy{
700344884Scy  ev_uint32_t tag;
701344884Scy  int res = -1;
702344884Scy
703344884Scy  struct evbuffer *tmp = evbuffer_new();
704344884Scy
705344884Scy  if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
706344884Scy    goto error;
707344884Scy
708344884Scy  if (kill_unmarshal(msg, tmp) == -1)
709344884Scy    goto error;
710344884Scy
711344884Scy  res = 0;
712344884Scy
713344884Scy error:
714344884Scy  evbuffer_free(tmp);
715344884Scy  return (res);
716344884Scy}
717344884Scy
718344884Scyvoid
719344884Scyevtag_marshal_kill(struct evbuffer *evbuf, ev_uint32_t tag, const struct kill *msg)
720344884Scy{
721344884Scy  struct evbuffer *buf_ = evbuffer_new();
722344884Scy  assert(buf_ != NULL);
723344884Scy  kill_marshal(buf_, msg);
724344884Scy  evtag_marshal_buffer(evbuf, tag, buf_);
725344884Scy   evbuffer_free(buf_);
726344884Scy}
727344884Scy
728344884Scy/*
729344884Scy * Implementation of run
730344884Scy */
731344884Scy
732344884Scystatic struct run_access_ run_base__ = {
733344884Scy  run_how_assign,
734344884Scy  run_how_get,
735344884Scy  run_some_bytes_assign,
736344884Scy  run_some_bytes_get,
737344884Scy  run_fixed_bytes_assign,
738344884Scy  run_fixed_bytes_get,
739344884Scy  run_notes_assign,
740344884Scy  run_notes_get,
741344884Scy  run_notes_add,
742344884Scy  run_large_number_assign,
743344884Scy  run_large_number_get,
744344884Scy  run_other_numbers_assign,
745344884Scy  run_other_numbers_get,
746344884Scy  run_other_numbers_add,
747344884Scy};
748344884Scy
749344884Scystruct run *
750344884Scyrun_new(void)
751344884Scy{
752344884Scy  return run_new_with_arg(NULL);
753344884Scy}
754344884Scy
755344884Scystruct run *
756344884Scyrun_new_with_arg(void *unused)
757344884Scy{
758344884Scy  struct run *tmp;
759344884Scy  if ((tmp = malloc(sizeof(struct run))) == NULL) {
760344884Scy    event_warn("%s: malloc", __func__);
761344884Scy    return (NULL);
762344884Scy  }
763344884Scy  tmp->base = &run_base__;
764344884Scy
765344884Scy  tmp->how_data = NULL;
766344884Scy  tmp->how_set = 0;
767344884Scy
768344884Scy  tmp->some_bytes_data = NULL;
769344884Scy  tmp->some_bytes_length = 0;
770344884Scy  tmp->some_bytes_set = 0;
771344884Scy
772344884Scy  memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
773344884Scy  tmp->fixed_bytes_set = 0;
774344884Scy
775344884Scy  tmp->notes_data = NULL;
776344884Scy  tmp->notes_length = 0;
777344884Scy  tmp->notes_num_allocated = 0;
778344884Scy  tmp->notes_set = 0;
779344884Scy
780344884Scy  tmp->large_number_data = 0;
781344884Scy  tmp->large_number_set = 0;
782344884Scy
783344884Scy  tmp->other_numbers_data = NULL;
784344884Scy  tmp->other_numbers_length = 0;
785344884Scy  tmp->other_numbers_num_allocated = 0;
786344884Scy  tmp->other_numbers_set = 0;
787344884Scy
788344884Scy  return (tmp);
789344884Scy}
790344884Scy
791344884Scy
792344884Scy
793344884Scy
794344884Scystatic int
795344884Scyrun_notes_expand_to_hold_more(struct run *msg)
796344884Scy{
797344884Scy  int tobe_allocated = msg->notes_num_allocated;
798344884Scy  char ** new_data = NULL;
799344884Scy  tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
800344884Scy  new_data = (char **) realloc(msg->notes_data,
801344884Scy      tobe_allocated * sizeof(char *));
802344884Scy  if (new_data == NULL)
803344884Scy    return -1;
804344884Scy  msg->notes_data = new_data;
805344884Scy  msg->notes_num_allocated = tobe_allocated;
806344884Scy  return 0;}
807344884Scy
808344884Scychar * *
809344884Scyrun_notes_add(struct run *msg, const char * value)
810344884Scy{
811344884Scy  if (++msg->notes_length >= msg->notes_num_allocated) {
812344884Scy    if (run_notes_expand_to_hold_more(msg)<0)
813344884Scy      goto error;
814344884Scy  }
815344884Scy  if (value != NULL) {
816344884Scy    msg->notes_data[msg->notes_length - 1] = strdup(value);
817344884Scy    if (msg->notes_data[msg->notes_length - 1] == NULL) {
818344884Scy      goto error;
819344884Scy    }
820344884Scy  } else {
821344884Scy    msg->notes_data[msg->notes_length - 1] = NULL;
822344884Scy  }
823344884Scy  msg->notes_set = 1;
824344884Scy  return &(msg->notes_data[msg->notes_length - 1]);
825344884Scyerror:
826344884Scy  --msg->notes_length;
827344884Scy  return (NULL);
828344884Scy}
829344884Scy
830344884Scy
831344884Scystatic int
832344884Scyrun_other_numbers_expand_to_hold_more(struct run *msg)
833344884Scy{
834344884Scy  int tobe_allocated = msg->other_numbers_num_allocated;
835344884Scy  ev_uint32_t* new_data = NULL;
836344884Scy  tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
837344884Scy  new_data = (ev_uint32_t*) realloc(msg->other_numbers_data,
838344884Scy      tobe_allocated * sizeof(ev_uint32_t));
839344884Scy  if (new_data == NULL)
840344884Scy    return -1;
841344884Scy  msg->other_numbers_data = new_data;
842344884Scy  msg->other_numbers_num_allocated = tobe_allocated;
843344884Scy  return 0;}
844344884Scy
845344884Scyev_uint32_t *
846344884Scyrun_other_numbers_add(struct run *msg, const ev_uint32_t value)
847344884Scy{
848344884Scy  if (++msg->other_numbers_length >= msg->other_numbers_num_allocated) {
849344884Scy    if (run_other_numbers_expand_to_hold_more(msg)<0)
850344884Scy      goto error;
851344884Scy  }
852344884Scy  msg->other_numbers_data[msg->other_numbers_length - 1] = value;
853344884Scy  msg->other_numbers_set = 1;
854344884Scy  return &(msg->other_numbers_data[msg->other_numbers_length - 1]);
855344884Scyerror:
856344884Scy  --msg->other_numbers_length;
857344884Scy  return (NULL);
858344884Scy}
859344884Scy
860344884Scyint
861344884Scyrun_how_assign(struct run *msg,
862344884Scy    const char * value)
863344884Scy{
864344884Scy  if (msg->how_data != NULL)
865344884Scy    free(msg->how_data);
866344884Scy  if ((msg->how_data = strdup(value)) == NULL)
867344884Scy    return (-1);
868344884Scy  msg->how_set = 1;
869344884Scy  return (0);
870344884Scy}
871344884Scy
872344884Scyint
873344884Scyrun_some_bytes_assign(struct run *msg, const ev_uint8_t * value, ev_uint32_t len)
874344884Scy{
875344884Scy  if (msg->some_bytes_data != NULL)
876344884Scy    free (msg->some_bytes_data);
877344884Scy  msg->some_bytes_data = malloc(len);
878344884Scy  if (msg->some_bytes_data == NULL)
879344884Scy    return (-1);
880344884Scy  msg->some_bytes_set = 1;
881344884Scy  msg->some_bytes_length = len;
882344884Scy  memcpy(msg->some_bytes_data, value, len);
883344884Scy  return (0);
884344884Scy}
885344884Scy
886344884Scyint
887344884Scyrun_fixed_bytes_assign(struct run *msg, const ev_uint8_t *value)
888344884Scy{
889344884Scy  msg->fixed_bytes_set = 1;
890344884Scy  memcpy(msg->fixed_bytes_data, value, 24);
891344884Scy  return (0);
892344884Scy}
893344884Scy
894344884Scyint
895344884Scyrun_notes_assign(struct run *msg, int off,
896344884Scy    const char * value)
897344884Scy{
898344884Scy  if (!msg->notes_set || off < 0 || off >= msg->notes_length)
899344884Scy    return (-1);
900344884Scy
901344884Scy  {
902344884Scy    if (msg->notes_data[off] != NULL)
903344884Scy      free(msg->notes_data[off]);
904344884Scy    msg->notes_data[off] = strdup(value);
905344884Scy    if (msg->notes_data[off] == NULL) {
906344884Scy      event_warnx("%s: strdup", __func__);
907344884Scy      return (-1);
908344884Scy    }
909344884Scy  }
910344884Scy  return (0);
911344884Scy}
912344884Scy
913344884Scyint
914344884Scyrun_large_number_assign(struct run *msg, const ev_uint64_t value)
915344884Scy{
916344884Scy  msg->large_number_set = 1;
917344884Scy  msg->large_number_data = value;
918344884Scy  return (0);
919344884Scy}
920344884Scy
921344884Scyint
922344884Scyrun_other_numbers_assign(struct run *msg, int off,
923344884Scy    const ev_uint32_t value)
924344884Scy{
925344884Scy  if (!msg->other_numbers_set || off < 0 || off >= msg->other_numbers_length)
926344884Scy    return (-1);
927344884Scy
928344884Scy  {
929344884Scy    msg->other_numbers_data[off] = value;
930344884Scy  }
931344884Scy  return (0);
932344884Scy}
933344884Scy
934344884Scyint
935344884Scyrun_how_get(struct run *msg, char * *value)
936344884Scy{
937344884Scy  if (msg->how_set != 1)
938344884Scy    return (-1);
939344884Scy  *value = msg->how_data;
940344884Scy  return (0);
941344884Scy}
942344884Scy
943344884Scyint
944344884Scyrun_some_bytes_get(struct run *msg, ev_uint8_t * *value, ev_uint32_t *plen)
945344884Scy{
946344884Scy  if (msg->some_bytes_set != 1)
947344884Scy    return (-1);
948344884Scy  *value = msg->some_bytes_data;
949344884Scy  *plen = msg->some_bytes_length;
950344884Scy  return (0);
951344884Scy}
952344884Scy
953344884Scyint
954344884Scyrun_fixed_bytes_get(struct run *msg, ev_uint8_t **value)
955344884Scy{
956344884Scy  if (msg->fixed_bytes_set != 1)
957344884Scy    return (-1);
958344884Scy  *value = msg->fixed_bytes_data;
959344884Scy  return (0);
960344884Scy}
961344884Scy
962344884Scyint
963344884Scyrun_notes_get(struct run *msg, int offset,
964344884Scy    char * *value)
965344884Scy{
966344884Scy  if (!msg->notes_set || offset < 0 || offset >= msg->notes_length)
967344884Scy    return (-1);
968344884Scy  *value = msg->notes_data[offset];
969344884Scy  return (0);
970344884Scy}
971344884Scy
972344884Scyint
973344884Scyrun_large_number_get(struct run *msg, ev_uint64_t *value)
974344884Scy{
975344884Scy  if (msg->large_number_set != 1)
976344884Scy    return (-1);
977344884Scy  *value = msg->large_number_data;
978344884Scy  return (0);
979344884Scy}
980344884Scy
981344884Scyint
982344884Scyrun_other_numbers_get(struct run *msg, int offset,
983344884Scy    ev_uint32_t *value)
984344884Scy{
985344884Scy  if (!msg->other_numbers_set || offset < 0 || offset >= msg->other_numbers_length)
986344884Scy    return (-1);
987344884Scy  *value = msg->other_numbers_data[offset];
988344884Scy  return (0);
989344884Scy}
990344884Scy
991344884Scyvoid
992344884Scyrun_clear(struct run *tmp)
993344884Scy{
994344884Scy  if (tmp->how_set == 1) {
995344884Scy    free(tmp->how_data);
996344884Scy    tmp->how_data = NULL;
997344884Scy    tmp->how_set = 0;
998344884Scy  }
999344884Scy  if (tmp->some_bytes_set == 1) {
1000344884Scy    free (tmp->some_bytes_data);
1001344884Scy    tmp->some_bytes_data = NULL;
1002344884Scy    tmp->some_bytes_length = 0;
1003344884Scy    tmp->some_bytes_set = 0;
1004344884Scy  }
1005344884Scy  tmp->fixed_bytes_set = 0;
1006344884Scy  memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
1007344884Scy  if (tmp->notes_set == 1) {
1008344884Scy    int i;
1009344884Scy    for (i = 0; i < tmp->notes_length; ++i) {
1010344884Scy      if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]);
1011344884Scy    }
1012344884Scy    free(tmp->notes_data);
1013344884Scy    tmp->notes_data = NULL;
1014344884Scy    tmp->notes_set = 0;
1015344884Scy    tmp->notes_length = 0;
1016344884Scy    tmp->notes_num_allocated = 0;
1017344884Scy  }
1018344884Scy  tmp->large_number_set = 0;
1019344884Scy  if (tmp->other_numbers_set == 1) {
1020344884Scy    free(tmp->other_numbers_data);
1021344884Scy    tmp->other_numbers_data = NULL;
1022344884Scy    tmp->other_numbers_set = 0;
1023344884Scy    tmp->other_numbers_length = 0;
1024344884Scy    tmp->other_numbers_num_allocated = 0;
1025344884Scy  }
1026344884Scy}
1027344884Scy
1028344884Scyvoid
1029344884Scyrun_free(struct run *tmp)
1030344884Scy{
1031344884Scy  if (tmp->how_data != NULL)
1032344884Scy      free (tmp->how_data);
1033344884Scy  if (tmp->some_bytes_data != NULL)
1034344884Scy      free(tmp->some_bytes_data);
1035344884Scy  if (tmp->notes_set == 1) {
1036344884Scy    int i;
1037344884Scy    for (i = 0; i < tmp->notes_length; ++i) {
1038344884Scy      if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]);
1039344884Scy    }
1040344884Scy    free(tmp->notes_data);
1041344884Scy    tmp->notes_data = NULL;
1042344884Scy    tmp->notes_set = 0;
1043344884Scy    tmp->notes_length = 0;
1044344884Scy    tmp->notes_num_allocated = 0;
1045344884Scy  }
1046344884Scy  free(tmp->notes_data);
1047344884Scy  if (tmp->other_numbers_set == 1) {
1048344884Scy    free(tmp->other_numbers_data);
1049344884Scy    tmp->other_numbers_data = NULL;
1050344884Scy    tmp->other_numbers_set = 0;
1051344884Scy    tmp->other_numbers_length = 0;
1052344884Scy    tmp->other_numbers_num_allocated = 0;
1053344884Scy  }
1054344884Scy  free(tmp->other_numbers_data);
1055344884Scy  free(tmp);
1056344884Scy}
1057344884Scy
1058344884Scyvoid
1059344884Scyrun_marshal(struct evbuffer *evbuf, const struct run *tmp){
1060344884Scy  evtag_marshal_string(evbuf, RUN_HOW, tmp->how_data);
1061344884Scy  if (tmp->some_bytes_set) {
1062344884Scy    evtag_marshal(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length);
1063344884Scy  }
1064344884Scy  evtag_marshal(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24));
1065344884Scy  if (tmp->notes_set) {
1066344884Scy    {
1067344884Scy      int i;
1068344884Scy      for (i = 0; i < tmp->notes_length; ++i) {
1069344884Scy    evtag_marshal_string(evbuf, RUN_NOTES, tmp->notes_data[i]);
1070344884Scy      }
1071344884Scy    }
1072344884Scy  }
1073344884Scy  if (tmp->large_number_set) {
1074344884Scy    evtag_marshal_int64(evbuf, RUN_LARGE_NUMBER, tmp->large_number_data);
1075344884Scy  }
1076344884Scy  if (tmp->other_numbers_set) {
1077344884Scy    {
1078344884Scy      int i;
1079344884Scy      for (i = 0; i < tmp->other_numbers_length; ++i) {
1080344884Scy    evtag_marshal_int(evbuf, RUN_OTHER_NUMBERS, tmp->other_numbers_data[i]);
1081344884Scy      }
1082344884Scy    }
1083344884Scy  }
1084344884Scy}
1085344884Scy
1086344884Scyint
1087344884Scyrun_unmarshal(struct run *tmp,  struct evbuffer *evbuf)
1088344884Scy{
1089344884Scy  ev_uint32_t tag;
1090344884Scy  while (evbuffer_get_length(evbuf) > 0) {
1091344884Scy    if (evtag_peek(evbuf, &tag) == -1)
1092344884Scy      return (-1);
1093344884Scy    switch (tag) {
1094344884Scy
1095344884Scy      case RUN_HOW:
1096344884Scy
1097344884Scy        if (tmp->how_set)
1098344884Scy          return (-1);
1099344884Scy        if (evtag_unmarshal_string(evbuf, RUN_HOW, &tmp->how_data) == -1) {
1100344884Scy          event_warnx("%s: failed to unmarshal how", __func__);
1101344884Scy          return (-1);
1102344884Scy        }
1103344884Scy        tmp->how_set = 1;
1104344884Scy        break;
1105344884Scy
1106344884Scy      case RUN_SOME_BYTES:
1107344884Scy
1108344884Scy        if (tmp->some_bytes_set)
1109344884Scy          return (-1);
1110344884Scy        if (evtag_payload_length(evbuf, &tmp->some_bytes_length) == -1)
1111344884Scy          return (-1);
1112344884Scy        if (tmp->some_bytes_length > evbuffer_get_length(evbuf))
1113344884Scy          return (-1);
1114344884Scy        if ((tmp->some_bytes_data = malloc(tmp->some_bytes_length)) == NULL)
1115344884Scy          return (-1);
1116344884Scy        if (evtag_unmarshal_fixed(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length) == -1) {
1117344884Scy          event_warnx("%s: failed to unmarshal some_bytes", __func__);
1118344884Scy          return (-1);
1119344884Scy        }
1120344884Scy        tmp->some_bytes_set = 1;
1121344884Scy        break;
1122344884Scy
1123344884Scy      case RUN_FIXED_BYTES:
1124344884Scy
1125344884Scy        if (tmp->fixed_bytes_set)
1126344884Scy          return (-1);
1127344884Scy        if (evtag_unmarshal_fixed(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24)) == -1) {
1128344884Scy          event_warnx("%s: failed to unmarshal fixed_bytes", __func__);
1129344884Scy          return (-1);
1130344884Scy        }
1131344884Scy        tmp->fixed_bytes_set = 1;
1132344884Scy        break;
1133344884Scy
1134344884Scy      case RUN_NOTES:
1135344884Scy
1136344884Scy        if (tmp->notes_length >= tmp->notes_num_allocated &&
1137344884Scy            run_notes_expand_to_hold_more(tmp) < 0) {
1138344884Scy          puts("HEY NOW");
1139344884Scy          return (-1);
1140344884Scy        }
1141344884Scy        if (evtag_unmarshal_string(evbuf, RUN_NOTES, &tmp->notes_data[tmp->notes_length]) == -1) {
1142344884Scy          event_warnx("%s: failed to unmarshal notes", __func__);
1143344884Scy          return (-1);
1144344884Scy        }
1145344884Scy        ++tmp->notes_length;
1146344884Scy        tmp->notes_set = 1;
1147344884Scy        break;
1148344884Scy
1149344884Scy      case RUN_LARGE_NUMBER:
1150344884Scy
1151344884Scy        if (tmp->large_number_set)
1152344884Scy          return (-1);
1153344884Scy        if (evtag_unmarshal_int64(evbuf, RUN_LARGE_NUMBER, &tmp->large_number_data) == -1) {
1154344884Scy          event_warnx("%s: failed to unmarshal large_number", __func__);
1155344884Scy          return (-1);
1156344884Scy        }
1157344884Scy        tmp->large_number_set = 1;
1158344884Scy        break;
1159344884Scy
1160344884Scy      case RUN_OTHER_NUMBERS:
1161344884Scy
1162344884Scy        if (tmp->other_numbers_length >= tmp->other_numbers_num_allocated &&
1163344884Scy            run_other_numbers_expand_to_hold_more(tmp) < 0) {
1164344884Scy          puts("HEY NOW");
1165344884Scy          return (-1);
1166344884Scy        }
1167344884Scy        if (evtag_unmarshal_int(evbuf, RUN_OTHER_NUMBERS, &tmp->other_numbers_data[tmp->other_numbers_length]) == -1) {
1168344884Scy          event_warnx("%s: failed to unmarshal other_numbers", __func__);
1169344884Scy          return (-1);
1170344884Scy        }
1171344884Scy        ++tmp->other_numbers_length;
1172344884Scy        tmp->other_numbers_set = 1;
1173344884Scy        break;
1174344884Scy
1175344884Scy      default:
1176344884Scy        return -1;
1177344884Scy    }
1178344884Scy  }
1179344884Scy
1180344884Scy  if (run_complete(tmp) == -1)
1181344884Scy    return (-1);
1182344884Scy  return (0);
1183344884Scy}
1184344884Scy
1185344884Scyint
1186344884Scyrun_complete(struct run *msg)
1187344884Scy{
1188344884Scy  if (!msg->how_set)
1189344884Scy    return (-1);
1190344884Scy  if (!msg->fixed_bytes_set)
1191344884Scy    return (-1);
1192344884Scy  return (0);
1193344884Scy}
1194344884Scy
1195344884Scyint
1196344884Scyevtag_unmarshal_run(struct evbuffer *evbuf, ev_uint32_t need_tag, struct run *msg)
1197344884Scy{
1198344884Scy  ev_uint32_t tag;
1199344884Scy  int res = -1;
1200344884Scy
1201344884Scy  struct evbuffer *tmp = evbuffer_new();
1202344884Scy
1203344884Scy  if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
1204344884Scy    goto error;
1205344884Scy
1206344884Scy  if (run_unmarshal(msg, tmp) == -1)
1207344884Scy    goto error;
1208344884Scy
1209344884Scy  res = 0;
1210344884Scy
1211344884Scy error:
1212344884Scy  evbuffer_free(tmp);
1213344884Scy  return (res);
1214344884Scy}
1215344884Scy
1216344884Scyvoid
1217344884Scyevtag_marshal_run(struct evbuffer *evbuf, ev_uint32_t tag, const struct run *msg)
1218344884Scy{
1219344884Scy  struct evbuffer *buf_ = evbuffer_new();
1220344884Scy  assert(buf_ != NULL);
1221344884Scy  run_marshal(buf_, msg);
1222344884Scy  evtag_marshal_buffer(evbuf, tag, buf_);
1223344884Scy   evbuffer_free(buf_);
1224344884Scy}
1225344884Scy
1226