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 
23 void event_warn(const char *fmt, ...);
24 void event_warnx(const char *fmt, ...);
25 
26 
27 /*
28  * Implementation of msg
29  */
30 
31 static 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 
43 struct msg *
msg_new(void)44 msg_new(void)
45 {
46   return msg_new_with_arg(NULL);
47 }
48 
49 struct msg *
msg_new_with_arg(void * unused)50 msg_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 
79 static int
msg_run_expand_to_hold_more(struct msg * msg)80 msg_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 
93 struct run*
msg_run_add(struct msg * msg)94 msg_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]);
105 error:
106   --msg->run_length;
107   return (NULL);
108 }
109 
110 int
msg_from_name_assign(struct msg * msg,const char * value)111 msg_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 
122 int
msg_to_name_assign(struct msg * msg,const char * value)123 msg_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 
134 int
msg_attack_assign(struct msg * msg,const struct kill * value)135 msg_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 
171 int
msg_run_assign(struct msg * msg,int off,const struct run * value)172 msg_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 
203 int
msg_from_name_get(struct msg * msg,char ** value)204 msg_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 
212 int
msg_to_name_get(struct msg * msg,char ** value)213 msg_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 
221 int
msg_attack_get(struct msg * msg,struct kill ** value)222 msg_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 
234 int
msg_run_get(struct msg * msg,int offset,struct run ** value)235 msg_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 
244 void
msg_clear(struct msg * tmp)245 msg_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 
275 void
msg_free(struct msg * tmp)276 msg_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 
299 void
msg_marshal(struct evbuffer * evbuf,const struct msg * tmp)300 msg_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 
316 int
msg_unmarshal(struct msg * tmp,struct evbuffer * evbuf)317 msg_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 
389 int
msg_complete(struct msg * msg)390 msg_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 
408 int
evtag_unmarshal_msg(struct evbuffer * evbuf,ev_uint32_t need_tag,struct msg * msg)409 evtag_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 
429 void
evtag_marshal_msg(struct evbuffer * evbuf,ev_uint32_t tag,const struct msg * msg)430 evtag_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 
443 static 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 
453 struct kill *
kill_new(void)454 kill_new(void)
455 {
456   return kill_new_with_arg(NULL);
457 }
458 
459 struct kill *
kill_new_with_arg(void * unused)460 kill_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 
485 static int
kill_how_often_expand_to_hold_more(struct kill * msg)486 kill_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 
499 ev_uint32_t *
kill_how_often_add(struct kill * msg,const ev_uint32_t value)500 kill_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]);
509 error:
510   --msg->how_often_length;
511   return (NULL);
512 }
513 
514 int
kill_weapon_assign(struct kill * msg,const char * value)515 kill_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 
526 int
kill_action_assign(struct kill * msg,const char * value)527 kill_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 
538 int
kill_how_often_assign(struct kill * msg,int off,const ev_uint32_t value)539 kill_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 
551 int
kill_weapon_get(struct kill * msg,char ** value)552 kill_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 
560 int
kill_action_get(struct kill * msg,char ** value)561 kill_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 
569 int
kill_how_often_get(struct kill * msg,int offset,ev_uint32_t * value)570 kill_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 
579 void
kill_clear(struct kill * tmp)580 kill_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 
601 void
kill_free(struct kill * tmp)602 kill_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 
619 void
kill_marshal(struct evbuffer * evbuf,const struct kill * tmp)620 kill_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 
633 int
kill_unmarshal(struct kill * tmp,struct evbuffer * evbuf)634 kill_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 
689 int
kill_complete(struct kill * msg)690 kill_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 
699 int
evtag_unmarshal_kill(struct evbuffer * evbuf,ev_uint32_t need_tag,struct kill * msg)700 evtag_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 
720 void
evtag_marshal_kill(struct evbuffer * evbuf,ev_uint32_t tag,const struct kill * msg)721 evtag_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 
734 static 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 
751 struct run *
run_new(void)752 run_new(void)
753 {
754   return run_new_with_arg(NULL);
755 }
756 
757 struct run *
run_new_with_arg(void * unused)758 run_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 
796 static int
run_notes_expand_to_hold_more(struct run * msg)797 run_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 
810 char * *
run_notes_add(struct run * msg,const char * value)811 run_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]);
827 error:
828   --msg->notes_length;
829   return (NULL);
830 }
831 
832 
833 static int
run_other_numbers_expand_to_hold_more(struct run * msg)834 run_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 
847 ev_uint32_t *
run_other_numbers_add(struct run * msg,const ev_uint32_t value)848 run_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]);
857 error:
858   --msg->other_numbers_length;
859   return (NULL);
860 }
861 
862 int
run_how_assign(struct run * msg,const char * value)863 run_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 
874 int
run_some_bytes_assign(struct run * msg,const ev_uint8_t * value,ev_uint32_t len)875 run_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 
888 int
run_fixed_bytes_assign(struct run * msg,const ev_uint8_t * value)889 run_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 
896 int
run_notes_assign(struct run * msg,int off,const char * value)897 run_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 
915 int
run_large_number_assign(struct run * msg,const ev_uint64_t value)916 run_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 
923 int
run_other_numbers_assign(struct run * msg,int off,const ev_uint32_t value)924 run_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 
936 int
run_how_get(struct run * msg,char ** value)937 run_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 
945 int
run_some_bytes_get(struct run * msg,ev_uint8_t ** value,ev_uint32_t * plen)946 run_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 
955 int
run_fixed_bytes_get(struct run * msg,ev_uint8_t ** value)956 run_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 
964 int
run_notes_get(struct run * msg,int offset,char ** value)965 run_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 
974 int
run_large_number_get(struct run * msg,ev_uint64_t * value)975 run_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 
983 int
run_other_numbers_get(struct run * msg,int offset,ev_uint32_t * value)984 run_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 
993 void
run_clear(struct run * tmp)994 run_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 
1030 void
run_free(struct run * tmp)1031 run_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 
1060 void
run_marshal(struct evbuffer * evbuf,const struct run * tmp)1061 run_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 
1088 int
run_unmarshal(struct run * tmp,struct evbuffer * evbuf)1089 run_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 
1187 int
run_complete(struct run * msg)1188 run_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 
1197 int
evtag_unmarshal_run(struct evbuffer * evbuf,ev_uint32_t need_tag,struct run * msg)1198 evtag_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 
1218 void
evtag_marshal_run(struct evbuffer * evbuf,ev_uint32_t tag,const struct run * msg)1219 evtag_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