static int pack_entry(MDB_val *data, struct entry *entry) { char *stream = data->mv_data; char *bptr = stream; pack_bin(&stream, &entry->data.type, sizeof(entry->data.type)); knot_rdataset_t *rrs = &entry->data.rrs; pack_bin(&stream, &rrs->rr_count, sizeof(rrs->rr_count)); pack_bin(&stream, rrs->data, knot_rdataset_size(rrs)); pack_str(&stream, entry->threat_code); pack_str(&stream, entry->syslog_ip); data->mv_size = (stream - bptr); return KNOT_EOK; }
int put_db_online_fdb(int session, const char_t* filename) { int4 response; session_desc* s = sessions.get(session); if (s == NULL) { return cli_bad_descriptor; } int len = sizeof(cli_request); dbSmallBuffer buf(len); cli_request* req = (cli_request*)buf.base(); req->length = len; req->cmd = cli_cmd_is_db_online; req->stmt_id = 0; req->pack(); if (!s->sock->write(buf, len)) { return cli_network_error; } if (!s->sock->read(&response, sizeof response)) { return cli_network_error; } unpack4(response); if(response == cli_database_is_offline) { int len = sizeof(cli_request) + (strlen(filename)+ 1)*sizeof(char_t); dbSmallBuffer buf(len); char_t* p = (char_t*)buf.base(); cli_request* req = (cli_request*)p; req->length = len; req->cmd = cli_cmd_put_db_online; req->stmt_id = 0; p += sizeof(cli_request); p = pack_str(p, filename); req->pack(); if (!s->sock->write(buf, len)) { return cli_network_error; } if (!s->sock->read(&response, sizeof response)) { return cli_network_error; } unpack4(response); } return response; }
/** * Pack a message to be sent to whack * * @param wp The whack message * @return err_t */ err_t pack_whack_msg (struct whackpacker *wp) { err_t ugh = NULL; /** * Pack strings */ wp->str_next = wp->msg->string; wp->str_roof = &wp->msg->string[sizeof(wp->msg->string)]; if (!pack_str(wp, &wp->msg->name) /* string 1 */ || !pack_str(wp, &wp->msg->left.id) /* string 2 */ || !pack_str(wp, &wp->msg->left.cert) /* string 3 */ || !pack_str(wp, &wp->msg->left.ca) /* string 4 */ || !pack_str(wp, &wp->msg->left.groups) /* string 5 */ || !pack_str(wp, &wp->msg->left.updown) /* string 6 */ #ifdef VIRTUAL_IP || !pack_str(wp,&wp->msg->left.virt) /* string 7 */ #endif || !pack_str(wp, &wp->msg->right.id) /* string 8 */ || !pack_str(wp, &wp->msg->right.cert) /* string 9 */ || !pack_str(wp, &wp->msg->right.ca) /* string 10 */ || !pack_str(wp, &wp->msg->right.groups)/* string 11 */ || !pack_str(wp, &wp->msg->right.updown)/* string 12 */ #ifdef VIRTUAL_IP || !pack_str(wp, &wp->msg->right.virt) /* string 13 */ #endif || !pack_str(wp, &wp->msg->keyid) /* string 14 */ || !pack_str(wp, &wp->msg->myid) /* string 15 */ || !pack_str(wp, &wp->msg->ike) /* string 16 */ || !pack_str(wp, &wp->msg->esp) /* string 17 */ || wp->str_roof - wp->str_next < (ptrdiff_t)wp->msg->keyval.len) /* chunk (sort of string 16) */ { ugh = "too many bytes of strings to fit in message to pluto"; return ugh; } if(wp->msg->keyval.ptr) { memcpy(wp->str_next, wp->msg->keyval.ptr, wp->msg->keyval.len); } wp->msg->keyval.ptr = NULL; wp->str_next += wp->msg->keyval.len; return ugh; }
void PackToBin::PageIndex(const std::string& filepath, const std::vector<Page*>& pages, bool compress, float scale, const std::set<int>& ref_pkgs) { const std::map<std::string, int>& exports = ExportNameSet::Instance()->GetData(); /************************************************************************/ /* size */ /************************************************************************/ int sz = 0; // version sz += sizeof(uint16_t); // version flag sz += sizeof(uint16_t); // version // export sz += sizeof(uint16_t); // num std::map<std::string, int>::const_iterator itr = exports.begin(); for ( ; itr != exports.end(); ++itr) { sz += sizeof_pack_str(itr->first); // name sz += sizeof(uint32_t); // id } // pages sz += sizeof(uint16_t); // num for (int i = 0, n = pages.size(); i < n; ++i) { sz += sizeof(uint32_t); // size sz += sizeof(uint32_t); // min sz += sizeof(uint32_t); // max } // scale sz += sizeof(float); // ref others sz += sizeof(uint16_t); sz += sizeof(uint16_t) * ref_pkgs.size(); /************************************************************************/ /* fill */ /************************************************************************/ uint8_t* buf = new uint8_t[sz]; uint8_t* ptr = buf; // version uint16_t version_flag = 0xffff; pack(version_flag, &ptr); uint16_t version = VERSION; pack(version, &ptr); // export uint16_t export_n = exports.size(); pack(export_n, &ptr); // num for (itr = exports.begin(); itr != exports.end(); ++itr) { pack_str(itr->first, &ptr); // name uint32_t id = itr->second; pack(id, &ptr); // id } // pages uint16_t page_n = pages.size(); pack(page_n, &ptr); for (int i = 0, n = pages.size(); i < n; ++i) { uint32_t size = pages[i]->GetSize(); pack(size, &ptr); uint32_t min = pages[i]->GetMinID(), max = pages[i]->GetMaxID(); pack(min, &ptr); pack(max, &ptr); } // scale pack(scale, &ptr); // ref others uint16_t ref_count = ref_pkgs.size(); pack(ref_count, &ptr); std::set<int>::const_iterator itr_ref = ref_pkgs.begin(); for ( ; itr_ref != ref_pkgs.end(); ++itr_ref) { uint16_t pkg = *itr_ref; pack(pkg, &ptr); } /************************************************************************/ /* output */ /************************************************************************/ std::locale::global(std::locale("")); std::ofstream fout(filepath.c_str(), std::ios::binary); std::locale::global(std::locale("C")); if (compress) { uint8_t* dst = NULL; size_t dst_sz; erespacker::Lzma::Compress(&dst, &dst_sz, buf, sz); if (dst_sz > 0) { fout.write(reinterpret_cast<const char*>(&dst_sz), sizeof(dst_sz)); fout.write(reinterpret_cast<const char*>(dst), dst_sz); } } else { int32_t _sz = -(int)sz; fout.write(reinterpret_cast<const char*>(&_sz), sizeof(sz)); fout.write(reinterpret_cast<const char*>(buf), sz); } delete[] buf; fout.close(); }
/** * Pack a message to be sent to whack * * @param wp The whack message * @return err_t */ err_t pack_whack_msg (struct whackpacker *wp) { err_t ugh = NULL; /** * Pack strings */ wp->str_next = wp->msg->string; wp->str_roof = &wp->msg->string[sizeof(wp->msg->string)]; if (!pack_str(wp, &wp->msg->name) /* string 1 */ || !pack_str(wp, &wp->msg->left.id) /* string 2 */ || !pack_str(wp, &wp->msg->left.cert) /* string 3 */ || !pack_str(wp, &wp->msg->left.ca) /* string 4 */ || !pack_str(wp, &wp->msg->left.groups) /* string 5 */ || !pack_str(wp, &wp->msg->left.updown) /* string 6 */ || !pack_str(wp, &wp->msg->left.virt) /* string 7 */ || !pack_str(wp, &wp->msg->right.id) /* string 8 */ || !pack_str(wp, &wp->msg->right.cert) /* string 9 */ || !pack_str(wp, &wp->msg->right.ca) /* string 10 */ || !pack_str(wp, &wp->msg->right.groups)/* string 11 */ || !pack_str(wp, &wp->msg->right.updown)/* string 12 */ || !pack_str(wp, &wp->msg->right.virt) /* string 13 */ || !pack_str(wp, &wp->msg->keyid) /* string 14 */ || !pack_str(wp, &wp->msg->myid) /* string 15 */ || !pack_str(wp, &wp->msg->ike) /* string 16 */ || !pack_str(wp, &wp->msg->esp) /* string 17 */ || !pack_str(wp, &wp->msg->tpmeval) /* string 18 */ || !pack_str(wp, &wp->msg->left.xauth_name) /* string 19 */ || !pack_str(wp, &wp->msg->right.xauth_name) /* string 20 */ || !pack_str(wp, &wp->msg->connalias) /* string 21 */ || !pack_str(wp, &wp->msg->left.host_addr_name) /* string 22 */ || !pack_str(wp, &wp->msg->right.host_addr_name) /* string 23 */ || !pack_str(wp, &wp->msg->string1) /* string 24 */ || !pack_str(wp, &wp->msg->string2) /* string 25 */ || !pack_str(wp, &wp->msg->string3) /* string 26 */ || !pack_str(wp, &wp->msg->dnshostname) /* string 27 ? */ || wp->str_roof - wp->str_next < (ptrdiff_t)wp->msg->keyval.len) /* chunk (sort of string 19) */ { ugh = "too many bytes of strings to fit in message to pluto"; return ugh; } if(wp->msg->keyval.ptr) { memcpy(wp->str_next, wp->msg->keyval.ptr, wp->msg->keyval.len); } wp->msg->keyval.ptr = NULL; wp->str_next += wp->msg->keyval.len; return ugh; }