int bacdb_ulog_log(bacdb_ulog_t *ulog, bacdb_op_t *op) { int ret = 1; uint8_t temp_buf[16]; buffer_t buf; buffer_init(&buf, temp_buf, 16); if(!buffer_write_u32(&buf, (uint32_t)op->type) || !buffer_write_u64(&buf, (uint64_t)op->timestamp) || !buffer_write_u32(&buf, (uint32_t)op->size)) { ret = 0; goto done; } if(fwrite(temp_buf, 16, 1, ulog->ulog_file) != 1 || fwrite(op->buf, op->size, 1, ulog->ulog_file) != 1) { ret = 0; goto done; } buffer_destroy(&buf); done: if(!ret) { } return ret; }
TEST(Buffer, Append) { char * text; buffer * buf; buf = buffer_new(8); buffer_prepare(buf, 2); EXPECT_EQ(buf->size, 8); buffer_prepare(buf, 10); EXPECT_EQ(buf->size, 18); buffer_append_string(buf, "Hello"); EXPECT_EQ(buf->used, 5); buffer_append_char(buf, ' '); EXPECT_TRUE(strncmp((char*)buf->data, "Hello ", buf->used) == 0); text = strdup("WORLD"); buffer_append_data_tolower(buf, (unsigned char*) text, strlen(text)); EXPECT_TRUE(strncmp((char*)buf->data, "Hello world", buf->used) == 0); free(text); text = strdup("\nWORLD\n..."); buffer_append_string_n(buf, text, 6); EXPECT_TRUE(strncmp((char*)buf->data, "Hello world\nWORLD", buf->used) == 0); free(text); buffer_destroy(buf); }
/* 1 integer id 2 string collection 3 integer number 4 cursor_id (8 bytes string/ 64bit) return string package */ static int op_get_more(lua_State *L) { int id = luaL_checkinteger(L, 1); size_t sz = 0; const char * name = luaL_checklstring(L,2,&sz); int number = luaL_checkinteger(L, 3); size_t cursor_len = 0; const char * cursor_id = luaL_tolstring(L, 4, &cursor_len); if (cursor_len != 8) { return luaL_error(L, "Invalid cursor id"); } struct buffer buf; buffer_create(&buf); int len = reserve_length(&buf); write_int32(&buf, id); write_int32(&buf, 0); write_int32(&buf, OP_GET_MORE); write_int32(&buf, 0); write_string(&buf, name, sz); write_int32(&buf, number); write_bytes(&buf, cursor_id, 8); write_length(&buf, buf.size, len); lua_pushlstring(L, (const char *)buf.ptr, buf.size); buffer_destroy(&buf); return 1; }
uint16_t packet_peek_session_id(uint8_t *data, size_t length) { buffer_t *buffer = NULL; uint16_t session_id = -1; /* Create a buffer of the first 5 bytes. */ if(length < 5) { LOG_FATAL("Packet is too short!\n"); return -1; } /* Create a buffer with the first 5 bytes of data. */ buffer = buffer_create_with_data(BO_BIG_ENDIAN, data, 5); /* Discard packet_id. */ buffer_consume(buffer, 2); /* Discard packet_type. */ buffer_consume(buffer, 1); /* Finally, get the session_id. */ session_id = buffer_read_next_int16(buffer); /* Kill the buffer. */ buffer_destroy(buffer); /* Done! */ return session_id; }
/* attempts to open a file, tokenize and then process it as a haserl script */ int h_lua_loadfile (lua_State * L) { script_t *scriptchain; token_t *tokenchain; buffer_t script_text; int status; /* get the filename */ const char *filename = luaL_checkstring (L, 1); scriptchain = load_script ((char *) filename, NULL); tokenchain = build_token_list (scriptchain, NULL); preprocess_token_list (tokenchain); process_token_list (&script_text, tokenchain); free_token_list (tokenchain); free_script_list (scriptchain); /* script_text has the include file */ status = luaL_loadbuffer (L, (char *) script_text.data, script_text.ptr - script_text.data, filename); buffer_destroy (&script_text); if (status) { lua_error (L); } return (1); /* we return one value, the buffer, as a function */ }
void tcpclient_destroy(tcpclient_t *client, int drop_queue) { if (client == NULL) { return; } ev_timer_stop(client->loop, &client->timeout_watcher); if (client->connect_watcher.started) { stats_debug_log("tcpclient_destroy: stopping connect watcher"); ev_io_stop(client->loop, &client->connect_watcher.watcher); client->connect_watcher.started = false; } if (client->read_watcher.started) { stats_debug_log("tcpclient_destroy: stopping read watcher"); ev_io_stop(client->loop, &client->read_watcher.watcher); client->read_watcher.started = false; } if (client->write_watcher.started) { stats_debug_log("tcpclient_destroy: stopping write watcher"); ev_io_stop(client->loop, &client->write_watcher.watcher); client->write_watcher.started = false; } stats_debug_log("closing client->sd %d", client->sd); close(client->sd); if (client->addr != NULL) { freeaddrinfo(client->addr); } buffer_destroy(&client->send_queue); free(client->host); free(client->port); free(client->protocol); }
static void session_free(sessions_t *sessions, session_t *session) { fprintf(stderr, "Killling session: %s\n", session->name); if(sessions->exec) { if(session->pid > 0) { fprintf(stderr, "Killing process %d\n", session->pid); #ifdef WIN32 select_group_remove_socket(sessions->select_group, session->socket_id); CloseHandle(session->exec_stdin[PIPE_WRITE]); CloseHandle(session->exec_stdout[PIPE_READ]); /*kill(session->pid, SIGINT);*/ TerminateProcess(session->exec_handle, 0); #else select_group_remove_socket(sessions->select_group, session->exec_stdout[PIPE_READ]); close(session->exec_stdin[PIPE_WRITE]); close(session->exec_stdout[PIPE_READ]); kill(session->pid, SIGINT); #endif } } safe_free(session->name); session->name = NULL; buffer_destroy(session->buffer); session->buffer = NULL; safe_free(session); }
/* 1 string cursor_id return string package */ static int op_kill(lua_State *L) { size_t cursor_len = 0; const char * cursor_id = luaL_tolstring(L, 1, &cursor_len); if (cursor_len != 8) { return luaL_error(L, "Invalid cursor id"); } struct buffer buf; buffer_create(&buf); int len = reserve_length(&buf); write_int32(&buf, 0); write_int32(&buf, 0); write_int32(&buf, OP_KILL_CURSORS); write_int32(&buf, 0); write_int32(&buf, 1); write_bytes(&buf, cursor_id, 8); write_length(&buf, buf.size, len); lua_pushlstring(L, (const char *)buf.ptr, buf.size); buffer_destroy(&buf); return 1; }
/* 1 string collection 2 integer single remove 3 document selector return string package */ static int op_delete(lua_State *L) { document selector = lua_touserdata(L,3); if (selector == NULL) { luaL_error(L, "Invalid param"); } size_t sz = 0; const char * name = luaL_checklstring(L,1,&sz); luaL_Buffer b; luaL_buffinit(L,&b); struct buffer buf; buffer_create(&buf); int len = reserve_length(&buf); write_int32(&buf, 0); write_int32(&buf, 0); write_int32(&buf, OP_DELETE); write_int32(&buf, 0); write_string(&buf, name, sz); write_int32(&buf, lua_tointeger(L,2)); int32_t selector_len = get_length(selector); int total = buf.size + selector_len; write_length(&buf, total, len); luaL_addlstring(&b, (const char *)buf.ptr, buf.size); buffer_destroy(&buf); luaL_addlstring(&b, (const char *)selector, selector_len); luaL_pushresult(&b); return 1; }
void check_spinn_packet_con_teardown(void) { scheduler_destroy(&s); buffer_destroy(&b); spinn_packet_pool_destroy(&pool); spinn_packet_con_destroy(&c); }
void sprite_destroy(Sprite* sprite) { texture_destroy(sprite->texture); int i; for(i = 0; i < NB_VERTEX_ATTRIB; ++i) { buffer_destroy(sprite->attributes[i].buffer); } free(sprite); }
static int tree_resize_block(tree_t *tree, unsigned int block_vid, unsigned int new_size){ // {{{ unsigned int delta; ssize_t ret; block_info block; int j; buffer_t req_buffer; buffer_init_from_bare(&req_buffer, &block, sizeof(block)); /* read block_info */ hash_t req_read[] = { { HK(action), DATA_UINT32T( ACTION_READ ) }, { HK(offset), DATA_OFFT ( block_vid * sizeof(block_info) ) }, { HK(size), DATA_SIZET( sizeof(block_info) ) }, { HK(buffer), DATA_BUFFERT(&req_buffer) }, hash_end }; if( (ret = chain_next_query(tree->chain, req_read)) <= 0){ buffer_destroy(&req_buffer); return -1; } /* fix block_info */ delta = new_size - block.size; block.size = new_size; /* write block info */ hash_t req_write[] = { { HK(action), DATA_UINT32T( ACTION_WRITE ) }, { HK(offset), DATA_OFFT ( block_vid * sizeof(block_info) ) }, { HK(size), DATA_SIZET( sizeof(block_info) ) }, { HK(buffer), DATA_BUFFERT(&req_buffer) }, hash_end }; chain_next_query(tree->chain, req_write); // TODO lock for(j=0; j < tree->nlevels; j++){ tree->table[ tree->tof[j] + (block_vid / tree->lss[j]) ] += delta; } // TODO unlock buffer_destroy(&req_buffer); return 0; } // }}}
#include "work.h" #include <stdio.h> void get_time(imagedata_t *img, const char *fn, logger_t *l) { /*{{{*/ // this is a hack that is targeted to a // specific small set of jpeg images, // not expected, or more so, expected to // not work with arbitrary files log(l, "trying to open \"%s\"\n", fn); FILE *fd = fopen(fn, "rb"); assert(fd); char dstr[20]; fseek(fd, 188, SEEK_SET); fread(dstr, sizeof(char), 19, fd); fclose(fd); dstr[19] = '\0'; // 2013:12:27 18:20:32 // 0123456789012345678 img->time.tm_year = atoi(dstr)-1900; img->time.tm_mon = atoi(dstr+5); img->time.tm_mday = atoi(dstr+8); img->time.tm_hour = atoi(dstr+11); img->time.tm_min = atoi(dstr+14); img->time.tm_sec = atoi(dstr+17); img->stamp = mktime(&img->time); log(l, "%s -> %s -> %04d-%02d-%02d %02d:%02d:%02d -> %Ld\n", fn, dstr, img->time.tm_year, img->time.tm_mon, img->time.tm_mday, img->time.tm_hour, img->time.tm_min, img->time.tm_sec, img->stamp ); } /*}}}*/ void scalepow2(bitmap_t *out, bitmap_t *in, unsigned int spow) { /*{{{*/ unsigned int limit = 1<<spow; for (size_t ty=0; ty<out->height; ++ty) { for (size_t tx=0; tx<out->width; ++tx) { size_t tp = tx + ty*out->width; unsigned int ys = 0; unsigned int cbs = 0; unsigned int crs = 0; size_t ctr = 0; for (size_t y=0; y<limit; ++y) { size_t sy = ty*limit + y; if (sy >= in->height) { break; } for (size_t x=0; x<limit; ++x) { size_t sx = tx*limit + x; if (sx >= in->width) { break; } size_t sp = sx + sy*in->width; ys += in->data[sp].x[0]; cbs += in->data[sp].x[1]; crs += in->data[sp].x[2]; ++ctr; } } ctr = ctr==0?1:ctr; out->data[tp].x[0] = ys/ctr; out->data[tp].x[1] = cbs/ctr; out->data[tp].x[2] = crs/ctr; } } } /*}}}*/ void init_threaddata(threaddata_t *td, const char *ofmt, const char *ifmt, int start, int stop) { /*{{{*/ log_init(&td->common.l, stdout); log(&td->common.l, "- logger created\n"); td->common.writerstr = calloc(strlen(ofmt)+32, sizeof(char)); assert(td->common.writerstr != NULL); sprintf(td->common.writerstr, "pnmtojpeg -quality=100 > %s", ofmt); log(&td->common.l, "- output shape is '%s'\n", td->common.writerstr); td->common.readerfmt = ifmt; td->common.readerstr = calloc(strlen(ifmt)+16, sizeof(char)); assert(td->common.readerstr != NULL); sprintf(td->common.readerstr, "jpegtopnm < %s", ifmt); log(&td->common.l, "- input shape is '%s'\n", td->common.readerstr); log(&td->common.l, "- initializing counter\n"); counter_init(&td->counter, start, stop); log(&td->common.l, "- initializing buffer\n"); buffer_init(&td->buffer, BUFFERSIZE, loader, unloader); //font_load_gz(&td->font, "data/sun12x22.psfu"); font_load_gz(&td->font, "/usr/share/kbd/consolefonts/sun12x22.psfu.gz"); } /*}}}*/ void destroy_threaddata(threaddata_t *td) { /*{{{*/ buffer_destroy(&td->buffer, &td->common); log(&td->common.l, "- destroying counter\n"); counter_destroy(&td->counter); log(&td->common.l, "- destroying logger\n"); log_destroy(&td->common.l); free(td->common.writerstr); free(td->common.readerstr); } /*}}}*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Parse Falcon's webpage containing the list of 1-minute * resolution CSV files and populate file_list with the * names of these files. */ int csv_get_file_list( csv_context_t *file_list, buffer_t* buf ) { int result = 1; int i = 0; regex_t regex; regmatch_t match_list[MAX_MATCHES]; regmatch_t* match = NULL; buffer_t* file_name = NULL; char* content_string = NULL; size_t max_off = 0; if (buffer_size(buf)) { file_name = buffer_init(); memset(&match_list, 0, sizeof(match_list)); content_string = (char*)buf->content; if (regcomp(®ex, "HREF[=]\"(/data/[^\"]+?[.]csv)\"", REG_EXTENDED)) { goto unclean; } // Find the names of all the csv files with minute resolution while (!regexec(®ex, content_string, (size_t)MAX_MATCHES, match_list, 0)) { match = match_list; match++; max_off = 0; for (i = 1; i < MAX_MATCHES; i++, match++) { if (match->rm_so && (match->rm_eo > match->rm_so)) { // Add this file name to the list buffer_write(file_name, (uint8_t*)(content_string + match->rm_so), (size_t)(match->rm_eo - match->rm_so) ); buffer_terminate(file_name); if (gDebug) { printf("found CSV file: %s\n", file_name->content); } list_append(file_list, buffer_detach(file_name)); if (max_off < match->rm_eo) { max_off = match->rm_eo; } } } content_string += max_off; memset(&match_list, 0, sizeof(match_list)); } } goto clean; unclean: result = 0; clean: regfree(®ex); file_name = buffer_destroy(file_name); return result; }
SELECT_RESPONSE_t incoming(void *group, int s, uint8_t *data, size_t length, char *addr, uint16_t port, void *param) { buffer_t *buffer; switch(state) { case RECV_STATE_HEADER: { /* Sanity check -- this should never happen with select_group. */ if(length != 4) DIE("Received incorrect number of bytes for NetBIOS header (likely a problem with select_group)."); /* The header is 24-bytes in network byte order. */ buffer = buffer_create_with_data(BO_NETWORK, data, 4); current_length = (buffer_read_next_int32(buffer) & 0x00FFFFFF); buffer_destroy(buffer); /* Enter body-receiving mode. */ select_group_wait_for_bytes((select_group_t*)group, s, current_length); state = RECV_STATE_BODY; break; } case RECV_STATE_BODY: { SMB_t *smb; /* Sanity check -- this should never happen with select_group. */ if(length != current_length) DIE("Received incorrect number of bytes for SMB (likely a problem with select_group)."); smb = smb_create_from_data(data, length, -1, NULL); /* TODO: Fix. */ switch(smb->header.command) { case SMB_COM_NEGOTIATE: parse_SMB_COM_NEGOTIATE(s, smb); break; case SMB_COM_SESSION_SETUP_ANDX: break; default: fprintf(stderr, "Don't know how to handle 0x%02x yet!\n", smb->header.command); } smb_destroy(smb); /* Return to header state. */ select_group_wait_for_bytes((select_group_t*)group, s, 4); state = RECV_STATE_HEADER; break; } default: DIE("Entered an unknown state."); } return SELECT_OK; }
void exit_cleanup () { if (audio_buffer != NULL) { buffer_destroy (audio_buffer); audio_buffer = NULL; } ao_onexit (options.devices); }
packet_t *packet_parse(uint8_t *data, size_t length, options_t options) { packet_t *packet = (packet_t*) safe_malloc(sizeof(packet_t)); buffer_t *buffer = buffer_create_with_data(BO_BIG_ENDIAN, data, length); /* Validate the size */ if(buffer_get_length(buffer) > MAX_PACKET_SIZE) { LOG_FATAL("Packet is too long: %zu bytes\n", buffer_get_length(buffer)); exit(1); } packet->packet_id = buffer_read_next_int16(buffer); packet->packet_type = (packet_type_t) buffer_read_next_int8(buffer); packet->session_id = buffer_read_next_int16(buffer); switch(packet->packet_type) { case PACKET_TYPE_SYN: packet->body.syn.seq = buffer_read_next_int16(buffer); packet->body.syn.options = buffer_read_next_int16(buffer); break; case PACKET_TYPE_MSG: if(options & OPT_CHUNKED_DOWNLOAD) { packet->body.msg.options.chunked.chunk = buffer_read_next_int32(buffer); } else { packet->body.msg.options.normal.seq = buffer_read_next_int16(buffer); packet->body.msg.options.normal.ack = buffer_read_next_int16(buffer); } packet->body.msg.data = buffer_read_remaining_bytes(buffer, &packet->body.msg.data_length, -1, FALSE); break; case PACKET_TYPE_FIN: packet->body.fin.reason = buffer_alloc_next_ntstring(buffer); break; case PACKET_TYPE_PING: packet->body.ping.data = buffer_alloc_next_ntstring(buffer); break; default: LOG_FATAL("Error: unknown message type (0x%02x)\n", packet->packet_type); exit(0); } buffer_destroy(buffer); return packet; }
int pdu_ser_destroy(struct pdu_ser * pdu) { if (!pdu) return -1; if (pdu->buf) buffer_destroy(pdu->buf); rkfree(pdu); return 0; }
void *buffer_destroy(buffer *l) { if ((*l)) { buffer_destroy(&((*l)->next)); if ((*l)->plus) free((*l)->plus); free((*l)); *l = NULL; } return (NULL); }
void driver_command_destroy(driver_command_t *driver) { if(!driver->is_shutdown) driver_command_close(driver); if(driver->name) safe_free(driver->name); if(driver->stream) buffer_destroy(driver->stream); safe_free(driver); }
int pdu_destroy(struct pdu * p) { if (!p) return -1; if (p->pci) pci_destroy(p->pci); if (p->buffer) buffer_destroy(p->buffer); rkfree(p); return 0; }
int pdu_buffer_set(struct pdu * pdu, struct buffer * buffer) { if (!pdu) return -1; if (pdu->buffer) { buffer_destroy(pdu->buffer); } pdu->buffer = buffer; return 0; }
TEST(Buffer, Simple) { buffer * buf = NULL; buf = buffer_new(8); EXPECT_TRUE(buf != NULL); EXPECT_EQ(buf->used, 0); EXPECT_EQ(buf->size, 8); buffer_delete(buf); EXPECT_EQ(buf->used, 0); buffer_destroy(buf); }
static void pop_readfile_response (void) { readfile_response_t* res = readfile_response_head; readfile_response_head = res->next; if (readfile_response_head == 0) { readfile_response_tail = &readfile_response_head; } if (res->bd != -1) { buffer_destroy (res->bd); } free (res); }
TEST(Buffer, SimpleCase) { buffer * buf; buf = buffer_new(3); buffer_append_string(buf, "K1"); EXPECT_EQ(buf->used, 2); buffer_camelify(buf); EXPECT_TRUE(strncmp((char*)buf->data, "K1", buf->used) == 0); buffer_destroy(buf); }
void config_set_token(const char *szInput) { if (szInput == NULL) { return; } if (config.bufToken) { buffer_destroy(config.bufToken); } config.bufToken = buffer_from(szInput); }
static int op_query(lua_State *L) { int id = luaL_checkinteger(L,1); document query = lua_touserdata(L,6); if (query == NULL) { return luaL_error(L, "require query document"); } document selector = lua_touserdata(L,7); int flags = luaL_checkinteger(L, 2); size_t nsz = 0; const char *name = luaL_checklstring(L,3,&nsz); int skip = luaL_checkinteger(L, 4); int number = luaL_checkinteger(L, 5); luaL_Buffer b; luaL_buffinit(L,&b); struct buffer buf; buffer_create(&buf); int len = reserve_length(&buf); write_int32(&buf, id); write_int32(&buf, 0); write_int32(&buf, OP_QUERY); write_int32(&buf, flags); write_string(&buf, name, nsz); write_int32(&buf, skip); write_int32(&buf, number); int32_t query_len = get_length(query); int total = buf.size + query_len; int32_t selector_len = 0; if (selector) { selector_len = get_length(selector); total += selector_len; } write_length(&buf, total, len); luaL_addlstring(&b, (const char *)buf.ptr, buf.size); buffer_destroy(&buf); luaL_addlstring(&b, (const char *)query, query_len); if (selector) { luaL_addlstring(&b, (const char *)selector, selector_len); } luaL_pushresult(&b); return 1; }
static void rinoo_fs_entry_destroy(t_fs_entry *entry) { if (entry == NULL) { return; } if (entry->entry != NULL) { free(entry->entry); } if (entry->path != NULL) { buffer_destroy(entry->path); } rinoolist_flush(&entry->stack, rinoo_fs_stack_destroy_node); free(entry); }
void tty_close(struct tty *tty, int no_stop) { if (tty->fd == -1) return; if (tty->log_fd != -1) { close(tty->log_fd); tty->log_fd = -1; } if (!no_stop) tty_stop_tty(tty); tty_term_free(tty->term); tty_keys_free(tty); close(tty->fd); tty->fd = -1; buffer_destroy(tty->in); buffer_destroy(tty->out); }
int bacdb_ulog_reader_read(bacdb_ulog_reader_t *ulog, bacdb_op_t *out) { int ret = 1; uint32_t type; uint8_t buffer[16]; buffer_t buf; buffer_init(&buf, buffer, 16); if(fread(buffer, 16, 1, ulog->ulog_file) != 1) { fprintf(stderr, "Failed to read op header"); ret = 0; goto done; } if(!buffer_read_u32(&buf, &type) || !buffer_read_u64(&buf, &out->timestamp) || !buffer_read_u32(&buf, &out->size)) { fprintf(stderr, "Failed to parse op header"); ret = 0; goto done; } out->type = (bacdb_optype_t)type; out->buf = malloc(out->size); out->owns_buf = 1; if(out->buf == NULL) { fprintf(stderr, "Failed to allocate op buffer"); ret = 0; goto done; } if(fread(out->buf, out->size, 1, ulog->ulog_file) != 1) { fprintf(stderr, "Failed to read op buffer"); ret = 0; goto done; } done: if(!ret) { if(out->buf != NULL) { free(out->buf); out->buf = NULL; } } buffer_destroy(&buf); return ret; }