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