Annotation of embedaddon/libevent/test/regress.gen.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Automatically generated from ./regress.rpc
                      3:  * by event_rpcgen.py/0.1.  DO NOT EDIT THIS FILE.
                      4:  */
                      5: 
                      6: #include <sys/types.h>
                      7: #ifdef _EVENT_HAVE_SYS_TIME_H
                      8: #include <sys/time.h>
                      9: #endif
                     10: #include <stdlib.h>
                     11: #include <string.h>
                     12: #include <assert.h>
                     13: #define EVENT_NO_STRUCT
                     14: #include <event.h>
                     15: 
                     16: #ifdef _EVENT___func__
                     17: #define __func__ _EVENT___func__
                     18: #endif
                     19: 
                     20: #include "./regress.gen.h"
                     21: 
                     22: void event_err(int eval, const char *fmt, ...);
                     23: void event_warn(const char *fmt, ...);
                     24: void event_errx(int eval, const char *fmt, ...);
                     25: void event_warnx(const char *fmt, ...);
                     26: 
                     27: 
                     28: /*
                     29:  * Implementation of msg
                     30:  */
                     31: 
                     32: static struct msg_access_ __msg_base = {
                     33:   msg_from_name_assign,
                     34:   msg_from_name_get,
                     35:   msg_to_name_assign,
                     36:   msg_to_name_get,
                     37:   msg_attack_assign,
                     38:   msg_attack_get,
                     39:   msg_run_assign,
                     40:   msg_run_get,
                     41:   msg_run_add,
                     42: };
                     43: 
                     44: struct msg *
                     45: msg_new(void)
                     46: {
                     47:   struct msg *tmp;
                     48:   if ((tmp = malloc(sizeof(struct msg))) == NULL) {
                     49:     event_warn("%s: malloc", __func__);
                     50:     return (NULL);
                     51:   }
                     52:   tmp->base = &__msg_base;
                     53: 
                     54:   tmp->from_name_data = NULL;
                     55:   tmp->from_name_set = 0;
                     56: 
                     57:   tmp->to_name_data = NULL;
                     58:   tmp->to_name_set = 0;
                     59: 
                     60:   tmp->attack_data = NULL;
                     61:   tmp->attack_set = 0;
                     62: 
                     63:   tmp->run_data = NULL;
                     64:   tmp->run_length = 0;
                     65:   tmp->run_num_allocated = 0;
                     66:   tmp->run_set = 0;
                     67: 
                     68:   return (tmp);
                     69: }
                     70: 
                     71: 
                     72: 
                     73: 
                     74: struct run *
                     75: msg_run_add(struct msg *msg)
                     76: {
                     77:   if (++msg->run_length >= msg->run_num_allocated) {
                     78:     int tobe_allocated = msg->run_num_allocated;
                     79:     struct run ** new_data = NULL;
                     80:     tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
                     81:     new_data = (struct run **) realloc(msg->run_data,
                     82:         tobe_allocated * sizeof(struct run *));
                     83:     if (new_data == NULL)
                     84:       goto error;
                     85:     msg->run_data = new_data;
                     86:     msg->run_num_allocated = tobe_allocated;
                     87:   }
                     88:   msg->run_data[msg->run_length - 1] = run_new();
                     89:   if (msg->run_data[msg->run_length - 1] == NULL)
                     90:     goto error;
                     91:   msg->run_set = 1;
                     92:   return (msg->run_data[msg->run_length - 1]);
                     93: error:
                     94:   --msg->run_length;
                     95:   return (NULL);
                     96: }
                     97:         
                     98: 
                     99: int
                    100: msg_from_name_assign(struct msg *msg,
                    101:     const char * value)
                    102: {
                    103:   if (msg->from_name_data != NULL)
                    104:     free(msg->from_name_data);
                    105:   if ((msg->from_name_data = strdup(value)) == NULL)
                    106:     return (-1);
                    107:   msg->from_name_set = 1;
                    108:   return (0);
                    109: }
                    110: 
                    111: int
                    112: msg_to_name_assign(struct msg *msg,
                    113:     const char * value)
                    114: {
                    115:   if (msg->to_name_data != NULL)
                    116:     free(msg->to_name_data);
                    117:   if ((msg->to_name_data = strdup(value)) == NULL)
                    118:     return (-1);
                    119:   msg->to_name_set = 1;
                    120:   return (0);
                    121: }
                    122: 
                    123: int
                    124: msg_attack_assign(struct msg *msg,
                    125:     const struct kill* value)
                    126: {
                    127:    struct evbuffer *tmp = NULL;
                    128:    if (msg->attack_set) {
                    129:      kill_clear(msg->attack_data);
                    130:      msg->attack_set = 0;
                    131:    } else {
                    132:      msg->attack_data = kill_new();
                    133:      if (msg->attack_data == NULL) {
                    134:        event_warn("%s: kill_new()", __func__);
                    135:        goto error;
                    136:      }
                    137:    }
                    138:    if ((tmp = evbuffer_new()) == NULL) {
                    139:      event_warn("%s: evbuffer_new()", __func__);
                    140:      goto error;
                    141:    }
                    142:    kill_marshal(tmp, value);
                    143:    if (kill_unmarshal(msg->attack_data, tmp) == -1) {
                    144:      event_warnx("%s: kill_unmarshal", __func__);
                    145:      goto error;
                    146:    }
                    147:    msg->attack_set = 1;
                    148:    evbuffer_free(tmp);
                    149:    return (0);
                    150:  error:
                    151:    if (tmp != NULL)
                    152:      evbuffer_free(tmp);
                    153:    if (msg->attack_data != NULL) {
                    154:      kill_free(msg->attack_data);
                    155:      msg->attack_data = NULL;
                    156:    }
                    157:    return (-1);
                    158: }
                    159: 
                    160: int
                    161: msg_run_assign(struct msg *msg, int off,
                    162:     const struct run * value)
                    163: {
                    164:   struct evbuffer *tmp = NULL;
                    165:   if (!msg->run_set || off < 0 || off >= msg->run_length)
                    166:     return (-1);
                    167:   run_clear(msg->run_data[off]);
                    168:   if ((tmp = evbuffer_new()) == NULL) {
                    169:     event_warn("%s: evbuffer_new()", __func__);
                    170:     goto error;
                    171:   }
                    172:   run_marshal(tmp, value);
                    173:   if (run_unmarshal(msg->run_data[off], tmp) == -1) {
                    174:     event_warnx("%s: run_unmarshal", __func__);
                    175:     goto error;
                    176:   }
                    177:   evbuffer_free(tmp);
                    178:   return (0);
                    179: error:
                    180:   if (tmp != NULL)
                    181:     evbuffer_free(tmp);
                    182:   run_clear(msg->run_data[off]);
                    183:   return (-1);
                    184: }
                    185: 
                    186: int
                    187: msg_from_name_get(struct msg *msg, char * *value)
                    188: {
                    189:   if (msg->from_name_set != 1)
                    190:     return (-1);
                    191:   *value = msg->from_name_data;
                    192:   return (0);
                    193: }
                    194: 
                    195: int
                    196: msg_to_name_get(struct msg *msg, char * *value)
                    197: {
                    198:   if (msg->to_name_set != 1)
                    199:     return (-1);
                    200:   *value = msg->to_name_data;
                    201:   return (0);
                    202: }
                    203: 
                    204: int
                    205: msg_attack_get(struct msg *msg, struct kill* *value)
                    206: {
                    207:   if (msg->attack_set != 1) {
                    208:     msg->attack_data = kill_new();
                    209:     if (msg->attack_data == NULL)
                    210:       return (-1);
                    211:     msg->attack_set = 1;
                    212:   }
                    213:   *value = msg->attack_data;
                    214:   return (0);
                    215: }
                    216: 
                    217: int
                    218: msg_run_get(struct msg *msg, int offset,
                    219:     struct run * *value)
                    220: {
                    221:   if (!msg->run_set || offset < 0 || offset >= msg->run_length)
                    222:     return (-1);
                    223:   *value = msg->run_data[offset];
                    224:   return (0);
                    225: }
                    226: 
                    227: void
                    228: msg_clear(struct msg *tmp)
                    229: {
                    230:   if (tmp->from_name_set == 1) {
                    231:     free (tmp->from_name_data);
                    232:     tmp->from_name_data = NULL;
                    233:     tmp->from_name_set = 0;
                    234:   }
                    235:   if (tmp->to_name_set == 1) {
                    236:     free (tmp->to_name_data);
                    237:     tmp->to_name_data = NULL;
                    238:     tmp->to_name_set = 0;
                    239:   }
                    240:   if (tmp->attack_set == 1) {
                    241:     kill_free(tmp->attack_data);
                    242:     tmp->attack_data = NULL;
                    243:     tmp->attack_set = 0;
                    244:   }
                    245:   if (tmp->run_set == 1) {
                    246:     int i;
                    247:     for (i = 0; i < tmp->run_length; ++i) {
                    248:       run_free(tmp->run_data[i]);
                    249:     }
                    250:     free(tmp->run_data);
                    251:     tmp->run_data = NULL;
                    252:     tmp->run_set = 0;
                    253:     tmp->run_length = 0;
                    254:     tmp->run_num_allocated = 0;
                    255:   }
                    256: }
                    257: 
                    258: void
                    259: msg_free(struct msg *tmp)
                    260: {
                    261:   if (tmp->from_name_data != NULL)
                    262:       free (tmp->from_name_data); 
                    263:   if (tmp->to_name_data != NULL)
                    264:       free (tmp->to_name_data); 
                    265:   if (tmp->attack_data != NULL)
                    266:       kill_free(tmp->attack_data); 
                    267:   if (tmp->run_data != NULL) {
                    268:     int i;
                    269:     for (i = 0; i < tmp->run_length; ++i) {
                    270:       run_free(tmp->run_data[i]); 
                    271:       tmp->run_data[i] = NULL;
                    272:     }
                    273:     free(tmp->run_data);
                    274:     tmp->run_data = NULL;
                    275:     tmp->run_length = 0;
                    276:     tmp->run_num_allocated = 0;
                    277:   }
                    278:   free(tmp);
                    279: }
                    280: 
                    281: void
                    282: msg_marshal(struct evbuffer *evbuf, const struct msg *tmp){
                    283:   evtag_marshal_string(evbuf, MSG_FROM_NAME, tmp->from_name_data);
                    284:   evtag_marshal_string(evbuf, MSG_TO_NAME, tmp->to_name_data);
                    285:   if (tmp->attack_set) {
                    286:     evtag_marshal_kill(evbuf, MSG_ATTACK, tmp->attack_data);
                    287:   }
                    288:   {
                    289:     int i;
                    290:     for (i = 0; i < tmp->run_length; ++i) {
                    291:       evtag_marshal_run(evbuf, MSG_RUN, tmp->run_data[i]);
                    292:     }
                    293:   }
                    294: }
                    295: 
                    296: int
                    297: msg_unmarshal(struct msg *tmp,  struct evbuffer *evbuf)
                    298: {
                    299:   ev_uint32_t tag;
                    300:   while (EVBUFFER_LENGTH(evbuf) > 0) {
                    301:     if (evtag_peek(evbuf, &tag) == -1)
                    302:       return (-1);
                    303:     switch (tag) {
                    304: 
                    305:       case MSG_FROM_NAME:
                    306: 
                    307:         if (tmp->from_name_set)
                    308:           return (-1);
                    309:         if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) {
                    310:           event_warnx("%s: failed to unmarshal from_name", __func__);
                    311:           return (-1);
                    312:         }
                    313:         tmp->from_name_set = 1;
                    314:         break;
                    315: 
                    316:       case MSG_TO_NAME:
                    317: 
                    318:         if (tmp->to_name_set)
                    319:           return (-1);
                    320:         if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) {
                    321:           event_warnx("%s: failed to unmarshal to_name", __func__);
                    322:           return (-1);
                    323:         }
                    324:         tmp->to_name_set = 1;
                    325:         break;
                    326: 
                    327:       case MSG_ATTACK:
                    328: 
                    329:         if (tmp->attack_set)
                    330:           return (-1);
                    331:         tmp->attack_data = kill_new();
                    332:         if (tmp->attack_data == NULL)
                    333:           return (-1);
                    334:         if (evtag_unmarshal_kill(evbuf, MSG_ATTACK, tmp->attack_data) == -1) {
                    335:           event_warnx("%s: failed to unmarshal attack", __func__);
                    336:           return (-1);
                    337:         }
                    338:         tmp->attack_set = 1;
                    339:         break;
                    340: 
                    341:       case MSG_RUN:
                    342: 
                    343:         if (msg_run_add(tmp) == NULL)
                    344:           return (-1);
                    345:         if (evtag_unmarshal_run(evbuf, MSG_RUN,
                    346:           tmp->run_data[tmp->run_length - 1]) == -1) {
                    347:           --tmp->run_length;
                    348:           event_warnx("%s: failed to unmarshal run", __func__);
                    349:           return (-1);
                    350:         }
                    351:         tmp->run_set = 1;
                    352:         break;
                    353: 
                    354:       default:
                    355:         return -1;
                    356:     }
                    357:   }
                    358: 
                    359:   if (msg_complete(tmp) == -1)
                    360:     return (-1);
                    361:   return (0);
                    362: }
                    363: 
                    364: int
                    365: msg_complete(struct msg *msg)
                    366: {
                    367:   if (!msg->from_name_set)
                    368:     return (-1);
                    369:   if (!msg->to_name_set)
                    370:     return (-1);
                    371:   if (msg->attack_set && kill_complete(msg->attack_data) == -1)
                    372:     return (-1);
                    373:   {
                    374:     int i;
                    375:     for (i = 0; i < msg->run_length; ++i) {
                    376:       if (run_complete(msg->run_data[i]) == -1)
                    377:         return (-1);
                    378:     }
                    379:   }
                    380:   return (0);
                    381: }
                    382: 
                    383: int
                    384: evtag_unmarshal_msg(struct evbuffer *evbuf, ev_uint32_t need_tag, struct msg *msg)
                    385: {
                    386:   ev_uint32_t tag;
                    387:   int res = -1;
                    388: 
                    389:   struct evbuffer *tmp = evbuffer_new();
                    390: 
                    391:   if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
                    392:     goto error;
                    393: 
                    394:   if (msg_unmarshal(msg, tmp) == -1)
                    395:     goto error;
                    396: 
                    397:   res = 0;
                    398: 
                    399:  error:
                    400:   evbuffer_free(tmp);
                    401:   return (res);
                    402: }
                    403: 
                    404: void
                    405: evtag_marshal_msg(struct evbuffer *evbuf, ev_uint32_t tag, const struct msg *msg)
                    406: {
                    407:   struct evbuffer *_buf = evbuffer_new();
                    408:   assert(_buf != NULL);
                    409:   evbuffer_drain(_buf, -1);
                    410:   msg_marshal(_buf, msg);
                    411:   evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
                    412:   evbuffer_free(_buf);
                    413: }
                    414: 
                    415: /*
                    416:  * Implementation of kill
                    417:  */
                    418: 
                    419: static struct kill_access_ __kill_base = {
                    420:   kill_weapon_assign,
                    421:   kill_weapon_get,
                    422:   kill_action_assign,
                    423:   kill_action_get,
                    424:   kill_how_often_assign,
                    425:   kill_how_often_get,
                    426: };
                    427: 
                    428: struct kill *
                    429: kill_new(void)
                    430: {
                    431:   struct kill *tmp;
                    432:   if ((tmp = malloc(sizeof(struct kill))) == NULL) {
                    433:     event_warn("%s: malloc", __func__);
                    434:     return (NULL);
                    435:   }
                    436:   tmp->base = &__kill_base;
                    437: 
                    438:   tmp->weapon_data = NULL;
                    439:   tmp->weapon_set = 0;
                    440: 
                    441:   tmp->action_data = NULL;
                    442:   tmp->action_set = 0;
                    443: 
                    444:   tmp->how_often_data = 0;
                    445:   tmp->how_often_set = 0;
                    446: 
                    447:   return (tmp);
                    448: }
                    449: 
                    450: 
                    451: 
                    452: 
                    453: int
                    454: kill_weapon_assign(struct kill *msg,
                    455:     const char * value)
                    456: {
                    457:   if (msg->weapon_data != NULL)
                    458:     free(msg->weapon_data);
                    459:   if ((msg->weapon_data = strdup(value)) == NULL)
                    460:     return (-1);
                    461:   msg->weapon_set = 1;
                    462:   return (0);
                    463: }
                    464: 
                    465: int
                    466: kill_action_assign(struct kill *msg,
                    467:     const char * value)
                    468: {
                    469:   if (msg->action_data != NULL)
                    470:     free(msg->action_data);
                    471:   if ((msg->action_data = strdup(value)) == NULL)
                    472:     return (-1);
                    473:   msg->action_set = 1;
                    474:   return (0);
                    475: }
                    476: 
                    477: int
                    478: kill_how_often_assign(struct kill *msg, const ev_uint32_t value)
                    479: {
                    480:   msg->how_often_set = 1;
                    481:   msg->how_often_data = value;
                    482:   return (0);
                    483: }
                    484: 
                    485: int
                    486: kill_weapon_get(struct kill *msg, char * *value)
                    487: {
                    488:   if (msg->weapon_set != 1)
                    489:     return (-1);
                    490:   *value = msg->weapon_data;
                    491:   return (0);
                    492: }
                    493: 
                    494: int
                    495: kill_action_get(struct kill *msg, char * *value)
                    496: {
                    497:   if (msg->action_set != 1)
                    498:     return (-1);
                    499:   *value = msg->action_data;
                    500:   return (0);
                    501: }
                    502: 
                    503: int
                    504: kill_how_often_get(struct kill *msg, ev_uint32_t *value)
                    505: {
                    506:   if (msg->how_often_set != 1)
                    507:     return (-1);
                    508:   *value = msg->how_often_data;
                    509:   return (0);
                    510: }
                    511: 
                    512: void
                    513: kill_clear(struct kill *tmp)
                    514: {
                    515:   if (tmp->weapon_set == 1) {
                    516:     free (tmp->weapon_data);
                    517:     tmp->weapon_data = NULL;
                    518:     tmp->weapon_set = 0;
                    519:   }
                    520:   if (tmp->action_set == 1) {
                    521:     free (tmp->action_data);
                    522:     tmp->action_data = NULL;
                    523:     tmp->action_set = 0;
                    524:   }
                    525:   tmp->how_often_set = 0;
                    526: }
                    527: 
                    528: void
                    529: kill_free(struct kill *tmp)
                    530: {
                    531:   if (tmp->weapon_data != NULL)
                    532:       free (tmp->weapon_data); 
                    533:   if (tmp->action_data != NULL)
                    534:       free (tmp->action_data); 
                    535:   free(tmp);
                    536: }
                    537: 
                    538: void
                    539: kill_marshal(struct evbuffer *evbuf, const struct kill *tmp){
                    540:   evtag_marshal_string(evbuf, KILL_WEAPON, tmp->weapon_data);
                    541:   evtag_marshal_string(evbuf, KILL_ACTION, tmp->action_data);
                    542:   if (tmp->how_often_set) {
                    543:     evtag_marshal_int(evbuf, KILL_HOW_OFTEN, tmp->how_often_data);
                    544:   }
                    545: }
                    546: 
                    547: int
                    548: kill_unmarshal(struct kill *tmp,  struct evbuffer *evbuf)
                    549: {
                    550:   ev_uint32_t tag;
                    551:   while (EVBUFFER_LENGTH(evbuf) > 0) {
                    552:     if (evtag_peek(evbuf, &tag) == -1)
                    553:       return (-1);
                    554:     switch (tag) {
                    555: 
                    556:       case KILL_WEAPON:
                    557: 
                    558:         if (tmp->weapon_set)
                    559:           return (-1);
                    560:         if (evtag_unmarshal_string(evbuf, KILL_WEAPON, &tmp->weapon_data) == -1) {
                    561:           event_warnx("%s: failed to unmarshal weapon", __func__);
                    562:           return (-1);
                    563:         }
                    564:         tmp->weapon_set = 1;
                    565:         break;
                    566: 
                    567:       case KILL_ACTION:
                    568: 
                    569:         if (tmp->action_set)
                    570:           return (-1);
                    571:         if (evtag_unmarshal_string(evbuf, KILL_ACTION, &tmp->action_data) == -1) {
                    572:           event_warnx("%s: failed to unmarshal action", __func__);
                    573:           return (-1);
                    574:         }
                    575:         tmp->action_set = 1;
                    576:         break;
                    577: 
                    578:       case KILL_HOW_OFTEN:
                    579: 
                    580:         if (tmp->how_often_set)
                    581:           return (-1);
                    582:         if (evtag_unmarshal_int(evbuf, KILL_HOW_OFTEN, &tmp->how_often_data) == -1) {
                    583:           event_warnx("%s: failed to unmarshal how_often", __func__);
                    584:           return (-1);
                    585:         }
                    586:         tmp->how_often_set = 1;
                    587:         break;
                    588: 
                    589:       default:
                    590:         return -1;
                    591:     }
                    592:   }
                    593: 
                    594:   if (kill_complete(tmp) == -1)
                    595:     return (-1);
                    596:   return (0);
                    597: }
                    598: 
                    599: int
                    600: kill_complete(struct kill *msg)
                    601: {
                    602:   if (!msg->weapon_set)
                    603:     return (-1);
                    604:   if (!msg->action_set)
                    605:     return (-1);
                    606:   return (0);
                    607: }
                    608: 
                    609: int
                    610: evtag_unmarshal_kill(struct evbuffer *evbuf, ev_uint32_t need_tag, struct kill *msg)
                    611: {
                    612:   ev_uint32_t tag;
                    613:   int res = -1;
                    614: 
                    615:   struct evbuffer *tmp = evbuffer_new();
                    616: 
                    617:   if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
                    618:     goto error;
                    619: 
                    620:   if (kill_unmarshal(msg, tmp) == -1)
                    621:     goto error;
                    622: 
                    623:   res = 0;
                    624: 
                    625:  error:
                    626:   evbuffer_free(tmp);
                    627:   return (res);
                    628: }
                    629: 
                    630: void
                    631: evtag_marshal_kill(struct evbuffer *evbuf, ev_uint32_t tag, const struct kill *msg)
                    632: {
                    633:   struct evbuffer *_buf = evbuffer_new();
                    634:   assert(_buf != NULL);
                    635:   evbuffer_drain(_buf, -1);
                    636:   kill_marshal(_buf, msg);
                    637:   evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
                    638:   evbuffer_free(_buf);
                    639: }
                    640: 
                    641: /*
                    642:  * Implementation of run
                    643:  */
                    644: 
                    645: static struct run_access_ __run_base = {
                    646:   run_how_assign,
                    647:   run_how_get,
                    648:   run_some_bytes_assign,
                    649:   run_some_bytes_get,
                    650:   run_fixed_bytes_assign,
                    651:   run_fixed_bytes_get,
                    652: };
                    653: 
                    654: struct run *
                    655: run_new(void)
                    656: {
                    657:   struct run *tmp;
                    658:   if ((tmp = malloc(sizeof(struct run))) == NULL) {
                    659:     event_warn("%s: malloc", __func__);
                    660:     return (NULL);
                    661:   }
                    662:   tmp->base = &__run_base;
                    663: 
                    664:   tmp->how_data = NULL;
                    665:   tmp->how_set = 0;
                    666: 
                    667:   tmp->some_bytes_data = NULL;
                    668:   tmp->some_bytes_length = 0;
                    669:   tmp->some_bytes_set = 0;
                    670: 
                    671:   memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
                    672:   tmp->fixed_bytes_set = 0;
                    673: 
                    674:   return (tmp);
                    675: }
                    676: 
                    677: 
                    678: 
                    679: 
                    680: int
                    681: run_how_assign(struct run *msg,
                    682:     const char * value)
                    683: {
                    684:   if (msg->how_data != NULL)
                    685:     free(msg->how_data);
                    686:   if ((msg->how_data = strdup(value)) == NULL)
                    687:     return (-1);
                    688:   msg->how_set = 1;
                    689:   return (0);
                    690: }
                    691: 
                    692: int
                    693: run_some_bytes_assign(struct run *msg, const ev_uint8_t * value, ev_uint32_t len)
                    694: {
                    695:   if (msg->some_bytes_data != NULL)
                    696:     free (msg->some_bytes_data);
                    697:   msg->some_bytes_data = malloc(len);
                    698:   if (msg->some_bytes_data == NULL)
                    699:     return (-1);
                    700:   msg->some_bytes_set = 1;
                    701:   msg->some_bytes_length = len;
                    702:   memcpy(msg->some_bytes_data, value, len);
                    703:   return (0);
                    704: }
                    705: 
                    706: int
                    707: run_fixed_bytes_assign(struct run *msg, const ev_uint8_t *value)
                    708: {
                    709:   msg->fixed_bytes_set = 1;
                    710:   memcpy(msg->fixed_bytes_data, value, 24);
                    711:   return (0);
                    712: }
                    713: 
                    714: int
                    715: run_how_get(struct run *msg, char * *value)
                    716: {
                    717:   if (msg->how_set != 1)
                    718:     return (-1);
                    719:   *value = msg->how_data;
                    720:   return (0);
                    721: }
                    722: 
                    723: int
                    724: run_some_bytes_get(struct run *msg, ev_uint8_t * *value, ev_uint32_t *plen)
                    725: {
                    726:   if (msg->some_bytes_set != 1)
                    727:     return (-1);
                    728:   *value = msg->some_bytes_data;
                    729:   *plen = msg->some_bytes_length;
                    730:   return (0);
                    731: }
                    732: 
                    733: int
                    734: run_fixed_bytes_get(struct run *msg, ev_uint8_t **value)
                    735: {
                    736:   if (msg->fixed_bytes_set != 1)
                    737:     return (-1);
                    738:   *value = msg->fixed_bytes_data;
                    739:   return (0);
                    740: }
                    741: 
                    742: void
                    743: run_clear(struct run *tmp)
                    744: {
                    745:   if (tmp->how_set == 1) {
                    746:     free (tmp->how_data);
                    747:     tmp->how_data = NULL;
                    748:     tmp->how_set = 0;
                    749:   }
                    750:   if (tmp->some_bytes_set == 1) {
                    751:     free (tmp->some_bytes_data);
                    752:     tmp->some_bytes_data = NULL;
                    753:     tmp->some_bytes_length = 0;
                    754:     tmp->some_bytes_set = 0;
                    755:   }
                    756:   tmp->fixed_bytes_set = 0;
                    757:   memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
                    758: }
                    759: 
                    760: void
                    761: run_free(struct run *tmp)
                    762: {
                    763:   if (tmp->how_data != NULL)
                    764:       free (tmp->how_data); 
                    765:   if (tmp->some_bytes_data != NULL)
                    766:       free (tmp->some_bytes_data); 
                    767:   free(tmp);
                    768: }
                    769: 
                    770: void
                    771: run_marshal(struct evbuffer *evbuf, const struct run *tmp){
                    772:   evtag_marshal_string(evbuf, RUN_HOW, tmp->how_data);
                    773:   if (tmp->some_bytes_set) {
                    774:     evtag_marshal(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length);
                    775:   }
                    776:   evtag_marshal(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, sizeof(tmp->fixed_bytes_data));
                    777: }
                    778: 
                    779: int
                    780: run_unmarshal(struct run *tmp,  struct evbuffer *evbuf)
                    781: {
                    782:   ev_uint32_t tag;
                    783:   while (EVBUFFER_LENGTH(evbuf) > 0) {
                    784:     if (evtag_peek(evbuf, &tag) == -1)
                    785:       return (-1);
                    786:     switch (tag) {
                    787: 
                    788:       case RUN_HOW:
                    789: 
                    790:         if (tmp->how_set)
                    791:           return (-1);
                    792:         if (evtag_unmarshal_string(evbuf, RUN_HOW, &tmp->how_data) == -1) {
                    793:           event_warnx("%s: failed to unmarshal how", __func__);
                    794:           return (-1);
                    795:         }
                    796:         tmp->how_set = 1;
                    797:         break;
                    798: 
                    799:       case RUN_SOME_BYTES:
                    800: 
                    801:         if (tmp->some_bytes_set)
                    802:           return (-1);
                    803:         if (evtag_payload_length(evbuf, &tmp->some_bytes_length) == -1)
                    804:           return (-1);
                    805:         if (tmp->some_bytes_length > EVBUFFER_LENGTH(evbuf))
                    806:           return (-1);
                    807:         if ((tmp->some_bytes_data = malloc(tmp->some_bytes_length)) == NULL)
                    808:           return (-1);
                    809:         if (evtag_unmarshal_fixed(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length) == -1) {
                    810:           event_warnx("%s: failed to unmarshal some_bytes", __func__);
                    811:           return (-1);
                    812:         }
                    813:         tmp->some_bytes_set = 1;
                    814:         break;
                    815: 
                    816:       case RUN_FIXED_BYTES:
                    817: 
                    818:         if (tmp->fixed_bytes_set)
                    819:           return (-1);
                    820:         if (evtag_unmarshal_fixed(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, sizeof(tmp->fixed_bytes_data)) == -1) {
                    821:           event_warnx("%s: failed to unmarshal fixed_bytes", __func__);
                    822:           return (-1);
                    823:         }
                    824:         tmp->fixed_bytes_set = 1;
                    825:         break;
                    826: 
                    827:       default:
                    828:         return -1;
                    829:     }
                    830:   }
                    831: 
                    832:   if (run_complete(tmp) == -1)
                    833:     return (-1);
                    834:   return (0);
                    835: }
                    836: 
                    837: int
                    838: run_complete(struct run *msg)
                    839: {
                    840:   if (!msg->how_set)
                    841:     return (-1);
                    842:   if (!msg->fixed_bytes_set)
                    843:     return (-1);
                    844:   return (0);
                    845: }
                    846: 
                    847: int
                    848: evtag_unmarshal_run(struct evbuffer *evbuf, ev_uint32_t need_tag, struct run *msg)
                    849: {
                    850:   ev_uint32_t tag;
                    851:   int res = -1;
                    852: 
                    853:   struct evbuffer *tmp = evbuffer_new();
                    854: 
                    855:   if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
                    856:     goto error;
                    857: 
                    858:   if (run_unmarshal(msg, tmp) == -1)
                    859:     goto error;
                    860: 
                    861:   res = 0;
                    862: 
                    863:  error:
                    864:   evbuffer_free(tmp);
                    865:   return (res);
                    866: }
                    867: 
                    868: void
                    869: evtag_marshal_run(struct evbuffer *evbuf, ev_uint32_t tag, const struct run *msg)
                    870: {
                    871:   struct evbuffer *_buf = evbuffer_new();
                    872:   assert(_buf != NULL);
                    873:   evbuffer_drain(_buf, -1);
                    874:   run_marshal(_buf, msg);
                    875:   evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
                    876:   evbuffer_free(_buf);
                    877: }
                    878: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>