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