static void sip_serialize(struct proto_info const *info_, uint8_t **buf) { struct sip_proto_info const *info = DOWNCAST(info_, info, sip_proto_info); proto_info_serialize(info_, buf); serialize_2(buf, info->set_values); if (info->set_values & SIP_CMD_SET) serialize_1(buf, info->cmd); if (info->set_values & SIP_FROM_SET) serialize_str(buf, info->from); if (info->set_values & SIP_TO_SET) serialize_str(buf, info->to); if (info->set_values & SIP_CALLID_SET) serialize_str(buf, info->call_id); if (info->set_values & SIP_MIME_SET) serialize_str(buf, info->mime_type); if (info->set_values & SIP_VIA_SET) { serialize_1(buf, info->via.protocol); ip_addr_serialize(&info->via.addr, buf); serialize_2(buf, info->via.port); } if (info->set_values & SIP_LENGTH_SET) serialize_4(buf, info->content_length); if (info->set_values & SIP_CODE_SET) serialize_4(buf, info->code); if (info->set_values & SIP_CSEQ_SET) serialize_4(buf, info->cseq); }
int main(void) { struct serialize_stream s = INIT_SERIALIZE_STREAM(s); struct unserialize_stream_mem u0; struct unserialize_stream_mem u1; serialize_u32(&s, 23); serialize_u64(&s, 42); serialize_time_t(&s, 1); serialize_buf(&s, BUF0, sizeof BUF0 - 1); serialize_buf(&s, BUF1, sizeof BUF1 - 1); serialize_buf(&s, BUF2, sizeof BUF2 - 1); serialize_buf(&s, BUF3, sizeof BUF3 - 1); serialize_allocate(&s, 80); serialize_str(&s, STR0); serialize_str(&s, STR1); serialize_str(&s, STR2); serialize_str(&s, STR3); u0.s.type = UNSERIALIZE_SRC_MEM; u0.ptr = s.base; u0.cnt = s.cnt; { uint32_t v; assert(unserialize_u32(&u0.s, &v) && v == 23); } { uint64_t v; assert(unserialize_u64(&u0.s, &v) && v == 42); } { time_t v; assert(unserialize_time_t(&u0.s, &v) && v == 1); } u1 = u0; u0 = u1; test0(&u0); u0 = u1; test1(&u0); u0 = u1; test2(&u0, false); u0 = u1; test2(&u0, true); serialize_destroy(&s, true); return 0; }
static int serialize_param(sstream_t *ss, param_t *p) { int res = 0; res = serialize_ptype(ss, &p->type) | res; res = serialize_str(ss, &p->name) | res; res = serialize_str(ss, &p->body) | res; res = serialize_int(ss, &p->len) | res; return res; }
static int serialize_route_ex(sstream_t *ss, rr_t **_r) { int do_it = 0; int res = 0; if (is_input_sstream(ss)) { /* read route */ if (serialize_int(ss, &do_it) != 0) return -1; if (!do_it) *_r = NULL; else { *_r = (rr_t*)cds_malloc(sizeof(rr_t)); if (!*_r) { ERROR_LOG("serialize_route(): can't allocate memory\n"); return -1; } (*_r)->r2 = NULL; (*_r)->params = NULL; (*_r)->next = NULL; } } else { /* store route */ if (*_r) do_it = 1; else do_it = 0; if (serialize_int(ss, &do_it) != 0) return -1; } if (do_it) { rr_t *r = *_r; str s = { r->nameaddr.name.s, r->len }; int delta = r->nameaddr.uri.s - r->nameaddr.name.s; res = serialize_str(ss, &s) | res; res = serialize_int(ss, &r->nameaddr.name.len) | res; res = serialize_int(ss, &r->nameaddr.uri.len) | res; res = serialize_int(ss, &r->nameaddr.len) | res; res = serialize_int(ss, &delta) | res; if (is_input_sstream(ss)) { r->nameaddr.name.s = s.s; r->nameaddr.uri.s = s.s + delta; } /* !!! optimalized strings - use carefuly !!! */ /*res = serialize_str(ss, &r->nameaddr.name) | res; res = serialize_str(ss, &r->nameaddr.uri) | res; res = serialize_int(ss, &r->nameaddr.len) | res;*/ /* ??? res = serialize_r2(ss, &r->nameaddr.params) | res; ??? */ res = serialize_params(ss, &r->params) | res; res = serialize_int(ss, &r->len) | res; TRACE_LOG("ROUTE: rlen=%d name=%.*s, uri=%.*s\n len=%d WHOLE=%.*s\n", r->len, FMT_STR(r->nameaddr.name), FMT_STR(r->nameaddr.uri), r->nameaddr.len, r->nameaddr.len, r->nameaddr.name.s ); } return res; }
static void dhcp_serialize(struct proto_info const *info_, uint8_t **buf) { struct dhcp_proto_info const *info = DOWNCAST(info_, info, dhcp_proto_info); proto_info_serialize(info_, buf); serialize_1(buf, info->hw_addr_is_eth + (info->opcode << 1U)); serialize_4(buf, info->xid); serialize_1(buf, info->set_values); if (info->set_values & DHCP_CLIENT_SET) ip_addr_serialize(&info->client, buf); serialize_n(buf, info->client_mac, sizeof(info->client_mac)); serialize_str(buf, info->server_name); }
int serialize_dlg(sstream_t *ss, dlg_t *dlg) { int res = 0; if (is_input_sstream(ss)) { memset(dlg, 0, sizeof(*dlg)); } res = serialize_str(ss, &dlg->id.call_id) | res; res = serialize_str(ss, &dlg->id.rem_tag) | res; res = serialize_str(ss, &dlg->id.loc_tag) | res; res = serialize_uint(ss, &dlg->loc_seq.value) | res; res = serialize_uchar(ss, &dlg->loc_seq.is_set) | res; res = serialize_uint(ss, &dlg->rem_seq.value) | res; res = serialize_uchar(ss, &dlg->rem_seq.is_set) | res; res = serialize_str(ss, &dlg->loc_uri) | res; res = serialize_str(ss, &dlg->rem_uri) | res; res = serialize_str(ss, &dlg->rem_target) | res; res = serialize_uchar(ss, &dlg->secure) | res; res = serialize_dlg_state(ss, &dlg->state) | res; res = serialize_route_set(ss, &dlg->route_set) | res; if ((res == 0) && (is_input_sstream(ss))) { /* tmb.w_calculate_hooks(dlg); */ res = tmb.calculate_hooks(dlg); if (res < 0) { ERROR_LOG("error during calculate_hooks (%d)!\n", res); } } return res; }
int serialize_version(struct buff *buf, const btc_msg_version *v) { int res; res = serialize_uint32(buf, v->version); res |= serialize_uint64(buf, v->services); res |= serialize_uint64(buf, v->time); res |= serialize_addr(buf, &v->addrTo); res |= serialize_addr(buf, &v->addrFrom); res |= serialize_uint64(buf, v->nonce); res |= serialize_str(buf, v->strVersion); res |= serialize_uint32(buf, v->startingHeight); return res; }
template <typename Iter> void value::_serialize(Iter oi, int indent) const { switch (type_) { case string_type: serialize_str(*u_.string_, oi); break; case array_type: { *oi++ = '['; if (indent != -1) { ++indent; } for (array::const_iterator i = u_.array_->begin(); i != u_.array_->end(); ++i) { if (i != u_.array_->begin()) { *oi++ = ','; } if (indent != -1) { _indent(oi, indent); } i->_serialize(oi, indent); } if (indent != -1) { --indent; if (! u_.array_->empty()) { _indent(oi, indent); } } *oi++ = ']'; break; } case object_type: { *oi++ = '{'; if (indent != -1) { ++indent; } for (object::const_iterator i = u_.object_->begin(); i != u_.object_->end(); ++i) { if (i != u_.object_->begin()) { *oi++ = ','; } if (indent != -1) { _indent(oi, indent); } serialize_str(i->first, oi); *oi++ = ':'; if (indent != -1) { *oi++ = ' '; } i->second._serialize(oi, indent); } if (indent != -1) { --indent; if (! u_.object_->empty()) { _indent(oi, indent); } } *oi++ = '}'; break; } default: copy(to_str(), oi); break; } if (indent == 0) { *oi++ = '\n'; } }