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