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