//----------------------------------------------------------------------------- void target_check_options(target_options_t *options, int size, int align) { options->file_data = NULL; options->file_size = 0; if (-1 == options->offset) options->offset = 0; if (-1 == options->size) options->size = size - options->offset; if (0 != (options->offset % align)) error_exit("offset must be a multiple of %d for the selected target", align); if (0 != (options->size % align)) error_exit("size must be a multiple of %d for the selected target", align); check(options->size <= size, "size is too big for the selected target"); check(options->offset < size, "offset is too big for the selected target"); if (options->program || options->verify) { options->file_data = buf_alloc(options->size); options->file_size = load_file(options->name, options->file_data, options->size); memset(&options->file_data[options->file_size], 0xff, options->size - options->file_size); check((options->file_size + options->offset) <= size, "file is too big for the selected target"); } else if (options->read) { options->file_data = buf_alloc(options->size); options->file_size = options->size; } }
void test_task () { buf_t *p; for (;;) { /* Check received data. */ p = netif_input (ð.netif); if (p) { if (memcmp (p->payload, data_pattern, packet_size) != 0) printf (&debug, "\npacket #%ld: data error\n", eth.netif.in_packets); buf_free (p); continue; } /* Send packets - make transmit queue full. */ if (run_test_flag) { p = buf_alloc (&pool, packet_size, 16); if (p) { memcpy (p->payload, data_pattern, packet_size); netif_output (ð.netif, p, 0, 0); } } k5600bg1_poll (ð); } }
static void run_client(struct thread *t) { struct options *opts = t->opts; const int flows_in_this_thread = flows_in_thread(opts->num_flows, opts->num_threads, t->index); struct callbacks *cb = t->cb; struct epoll_event *events; int epfd, i; char *buf; LOG_INFO(cb, "flows_in_this_thread=%d", flows_in_this_thread); epfd = epoll_create1(0); if (epfd == -1) PLOG_FATAL(cb, "epoll_create1"); epoll_add_or_die(epfd, t->stop_efd, EPOLLIN, cb); for (i = 0; i < flows_in_this_thread; i++) client_connect(i, epfd, t); events = calloc(opts->maxevents, sizeof(struct epoll_event)); buf = buf_alloc(opts); pthread_barrier_wait(t->ready); while (!t->stop) { int ms = opts->nonblocking ? 10 /* milliseconds */ : -1; int nfds = epoll_wait(epfd, events, opts->maxevents, ms); if (nfds == -1) { if (errno == EINTR) continue; PLOG_FATAL(cb, "epoll_wait"); } client_events(t, epfd, events, nfds, buf); } free(buf); free(events); do_close(epfd); }
//----------------------------------------------------------------------------- static void target_cm0p_read(char *name) { uint32_t size = device->flash_size; uint32_t addr = device->flash_start; uint32_t offs = 0; uint8_t *buf; if (dap_read_word(DSU_CTRL_STATUS) & 0x00010000) error_exit("devices is locked, unable to read"); buf = buf_alloc(device->flash_size); verbose("Reading..."); while (size) { dap_read_block(addr, &buf[offs], device->row_size); addr += device->row_size; offs += device->row_size; size -= device->row_size; verbose("."); } save_file(name, buf, device->flash_size); buf_free(buf); verbose(" done.\n"); }
void tty_read_callback(CFSocketRef s, CFSocketCallBackType callbackType, CFDataRef address, const void *data, void *info) { tty_ctx *ctx; buffer *raw; int n; raw = buf_alloc(512); ctx = info; n = fread(raw->buf, 1, raw->max, ctx->fp); if (n < 1) { ERR("Failed to read from device\n"); return; } raw->len = n; if (ctx->callback_fn) { ctx->callback_fn(ctx->callback_arg, raw); } buf_free(raw); }
static BUF * import_get_rcsdiff(struct cvs_file *cf, RCSNUM *rev) { char *p1, *p2; BUF *b1, *b2; int fd1, fd2; b2 = buf_alloc(128); b1 = buf_load_fd(cf->fd); (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir); fd1 = buf_write_stmp(b1, p1, NULL); buf_free(b1); (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir); fd2 = rcs_rev_write_stmp(cf->file_rcs, rev, p2, RCS_KWEXP_NONE); diff_format = D_RCSDIFF; if (diffreg(p2, p1, fd2, fd1, b2, D_FORCEASCII) == D_ERROR) fatal("import_get_rcsdiff: failed to get RCS patch"); close(fd1); close(fd2); (void)unlink(p1); (void)unlink(p2); xfree(p1); xfree(p2); return (b2); }
int dig__fwrite_port_D(double *buf, /* DOUBLE */ int cnt, GVFILE * fp) { int i, j; unsigned char *c1, *c2; if (Cur_Head->dbl_quick) { if (dig_fwrite(buf, PORT_DOUBLE, cnt, fp) == cnt) return 1; } else { buf_alloc(cnt * PORT_DOUBLE); c1 = (unsigned char *)buf; c2 = (unsigned char *)buffer; for (i = 0; i < cnt; i++) { for (j = 0; j < PORT_DOUBLE; j++) c2[j] = c1[Cur_Head->dbl_cnvrt[j]]; c1 += sizeof(double); c2 += PORT_DOUBLE; } if (dig_fwrite(buffer, PORT_DOUBLE, cnt, fp) == cnt) return 1; } return 0; }
//----------------------------------------------------------------------------- static void target_cm7_read(char *name) { uint32_t size = device->flash_size; uint32_t addr = device->flash_start; uint32_t offs = 0; uint8_t *buf; buf = buf_alloc(device->flash_size); verbose("Reading..."); while (size) { for (uint32_t sector = 0; sector < device->page_size / SECTOR_SIZE; sector++) { dap_read_block(addr, &buf[offs], SECTOR_SIZE); addr += SECTOR_SIZE; offs += SECTOR_SIZE; size -= SECTOR_SIZE; } verbose("."); } save_file(name, buf, device->flash_size); buf_free(buf); verbose(" done.\n"); }
static struct oc_text_buf *get_qs(char **str) { struct oc_text_buf *res; int escaped = 0; char *p = *str; if (*p != '\"') return NULL; res = buf_alloc(); while (*++p) { if (!escaped && *p == '\"') { *str = p+1; if (buf_error(res)) break; return res; } if (escaped) escaped = 0; else if (*p == '\\') escaped = 1; buf_append_bytes(res, p, 1); } buf_free(res); return NULL; }
static int p9_put_user_data(struct cbuf *bufp, const char __user *data, int count, unsigned char **pdata) { *pdata = buf_alloc(bufp, count); return copy_from_user(*pdata, data, count); }
/* read doubles from the PVF file */ int dig__fread_port_D(double *buf, int cnt, GVFILE * fp) { int i, j, ret; unsigned char *c1, *c2; if (Cur_Head->dbl_quick) { ret = dig_fread(buf, PORT_DOUBLE, cnt, fp); if (ret != cnt) return 0; } else { /* read into buffer */ buf_alloc(cnt * PORT_DOUBLE); ret = dig_fread(buffer, PORT_DOUBLE, cnt, fp); if (ret != cnt) return 0; /* read from buffer in changed order */ c1 = (unsigned char *)buffer; c2 = (unsigned char *)buf; for (i = 0; i < cnt; i++) { for (j = 0; j < PORT_DOUBLE; j++) { c2[Cur_Head->dbl_cnvrt[j]] = c1[j]; } c1 += PORT_DOUBLE; c2 += sizeof(double); } } return 1; }
//----------------------------------------------------------------------------- static void target_read(char *name) { uint32_t flash_size = device->flash_size * device->n_planes; uint32_t size = flash_size; uint32_t addr = device->flash_start; uint32_t offs = 0; uint8_t *buf; buf = buf_alloc(flash_size); verbose("Reading..."); while (size) { dap_read_block(addr, &buf[offs], device->page_size); addr += device->page_size; offs += device->page_size; size -= device->page_size; verbose("."); } save_file(name, buf, flash_size); buf_free(buf); verbose(" done.\n"); }
int dig__fwrite_port_F(float *buf, /* FLOAT */ int cnt, GVFILE * fp) { int i, j; unsigned char *c1, *c2; if (Cur_Head->flt_quick) { if (dig_fwrite(buf, PORT_FLOAT, cnt, fp) == cnt) return 1; } else { buf_alloc(cnt * PORT_FLOAT); c1 = (unsigned char *)buf; c2 = (unsigned char *)buffer; for (i = 0; i < cnt; i++) { for (j = 0; j < PORT_FLOAT; j++) c2[j] = c1[Cur_Head->flt_cnvrt[j]]; c1 += sizeof(float); c2 += PORT_FLOAT; } if (dig_fwrite(buffer, PORT_FLOAT, cnt, fp) == cnt) return 1; } return 0; }
static void import_loginfo(char *repo) { int i; char pwd[MAXPATHLEN]; if (getcwd(pwd, sizeof(pwd)) == NULL) fatal("Can't get working directory"); logbuf = buf_alloc(1024); cvs_trigger_loginfo_header(logbuf, repo); buf_puts(logbuf, "Log Message:\n"); buf_puts(logbuf, logmsg); if (logmsg[0] != '\0' && logmsg[strlen(logmsg) - 1] != '\n') buf_putc(logbuf, '\n'); buf_putc(logbuf, '\n'); buf_puts(logbuf, "Status:\n\n"); buf_puts(logbuf, "Vendor Tag:\t"); buf_puts(logbuf, vendor_tag); buf_putc(logbuf, '\n'); buf_puts(logbuf, "Release Tags:\t"); for (i = 0; i < tagcount ; i++) { buf_puts(logbuf, "\t\t"); buf_puts(logbuf, release_tags[i]); buf_putc(logbuf, '\n'); } buf_putc(logbuf, '\n'); buf_putc(logbuf, '\n'); }
/* * buf_load() * * Open the file specified by <path> and load all of its contents into a * buffer. * Returns the loaded buffer on success. */ BUF * buf_load(const char *path, u_int flags) { int fd; ssize_t ret; size_t len; u_char *bp; struct stat st; BUF *buf; if ((fd = open(path, O_RDONLY, 0600)) == -1) { warn("%s", path); return (NULL); } if (fstat(fd, &st) == -1) err(1, "%s", path); buf = buf_alloc((size_t)st.st_size, flags); for (bp = buf->cb_cur; ; bp += (size_t)ret) { len = SIZE_LEFT(buf); ret = read(fd, bp, len); if (ret == -1) { buf_free(buf); err(1, "buf_load"); } else if (ret == 0) break; buf->cb_len += (size_t)ret; } (void)close(fd); return (buf); }
//----------------------------------------------------------------------------- static void target_cm0p_verify(char *name) { uint32_t addr = device->flash_start; uint32_t size, block_size; uint32_t offs = 0; uint8_t *bufa, *bufb; if (dap_read_word(DSU_CTRL_STATUS) & 0x00010000) error_exit("devices is locked, unable to verify"); bufa = buf_alloc(device->flash_size); bufb = buf_alloc(device->row_size); size = load_file(name, bufa, device->flash_size); verbose("Verification..."); while (size) { dap_read_block(addr, bufb, device->row_size); block_size = (size > device->row_size) ? device->row_size : size; for (int i = 0; i < (int)block_size; i++) { if (bufa[offs + i] != bufb[i]) { verbose("\nat address 0x%x expected 0x%02x, read 0x%02x\n", addr + i, bufa[offs + i], bufb[i]); free(bufa); free(bufb); error_exit("verification failed"); } } addr += device->row_size; offs += device->row_size; size -= block_size; verbose("."); } free(bufa); free(bufb); verbose(" done.\n"); }
void js_alloc(jsparser_t **p, FILE *in, size_t toks_size) { *p = jmalloc(sizeof(jsparser_t)); buf_alloc(&((*p)->js)); (*p)->in = in; (*p)->toks = jmalloc(sizeof(jstok_t) * toks_size); (*p)->toks_size = toks_size; js_reset(*p); }
static int p9_put_data(struct cbuf *bufp, const char *data, int count, unsigned char **pdata) { *pdata = buf_alloc(bufp, count); memmove(*pdata, data, count); return count; }
//----------------------------------------------------------------------------- static void target_verify(char *name) { uint32_t addr = device->flash_start; uint32_t flash_size = device->flash_size * device->n_planes; uint32_t size, block_size; uint32_t offs = 0; uint8_t *bufa, *bufb; bufa = buf_alloc(flash_size); bufb = buf_alloc(device->page_size); size = load_file(name, bufa, flash_size); verbose("Verification..."); while (size) { dap_read_block(addr, bufb, device->page_size); block_size = (size > device->page_size) ? device->page_size : size; for (int i = 0; i < (int)block_size; i++) { if (bufa[offs + i] != bufb[i]) { verbose("\nat address 0x%x expected 0x%02x, read 0x%02x\n", addr + i, bufa[offs + i], bufb[i]); free(bufa); free(bufb); error_exit("verification failed"); } } addr += device->page_size; offs += device->page_size; size -= block_size; verbose("."); } free(bufa); free(bufb); verbose(" done.\n"); }
/* * Receive interrupt task. */ static void slip_receiver (void *arg) { slip_t *u = arg; unsigned short len; /* Start receiver. */ mutex_lock_irq (&u->netif.lock, RECEIVE_IRQ (u->port), (handler_t) slip_receive_data, u); enable_receiver (u->port); enable_receive_interrupt (u->port); for (;;) { if (! u->in_ptr) { /* Allocate buffer for receive data. */ u->in = buf_alloc (u->pool, u->netif.mtu, 16); if (u->in) { u->in_ptr = u->in->payload; u->in_limit = u->in_ptr + u->netif.mtu; } else { /* No buffer - ignore input. */ debug_printf ("slip_receiver: out of memory\n"); ++u->netif.in_discards; } } /* Wait for the receive interrupt. */ mutex_wait (&u->netif.lock); /* Process all available received data. */ if (u->in_ptr && u->in_ptr > u->in->payload) { len = u->in_ptr - u->in->payload; debug_printf ("slip_receiver(%ld): received %d bytes\n", u->netif.in_packets, len); buf_truncate (u->in, len); ++u->netif.in_packets; if (buf_queue_is_full (&u->inq)) { debug_printf ("slip_receiver: input overflow\n"); ++u->netif.in_discards; /* Reuse the packet. */ u->in_ptr = u->in->payload; u->in_limit = u->in_ptr + u->netif.mtu; } else { /* Enqueue the received packet. */ buf_queue_put (&u->inq, u->in); u->in_ptr = 0; } } #ifndef TRANSMIT_IRQ if (u->out_free) slip_out_next (u); #endif } }
static int p9_put_data(struct cbuf *bufp, const char *data, int count, unsigned char **pdata) { *pdata = buf_alloc(bufp, count); if (*pdata == NULL) return -ENOMEM; memmove(*pdata, data, count); return 0; }
int dig__fwrite_port_L(long *buf, /* LONG */ int cnt, GVFILE * fp) { int i, j; unsigned char *c1, *c2; if (Cur_Head->lng_quick) { if (nat_lng == PORT_LONG) { if (dig_fwrite(buf, PORT_LONG, cnt, fp) == cnt) return 1; } else { buf_alloc(cnt * PORT_LONG); if (lng_order == ENDIAN_LITTLE) c1 = (unsigned char *)buf; else c1 = (unsigned char *)buf + nat_lng - PORT_LONG; c2 = (unsigned char *)buffer; for (i = 0; i < cnt; i++) { memcpy(c2, c1, PORT_LONG); c1 += PORT_LONG; c2 += sizeof(long); } if (dig_fwrite(buffer, PORT_LONG, cnt, fp) == cnt) return 1; } } else { buf_alloc(cnt * PORT_LONG); c1 = (unsigned char *)buf; c2 = (unsigned char *)buffer; for (i = 0; i < cnt; i++) { for (j = 0; j < PORT_LONG; j++) c2[j] = c1[Cur_Head->lng_cnvrt[j]]; c1 += sizeof(long); c2 += PORT_LONG; } if (dig_fwrite(buffer, PORT_LONG, cnt, fp) == cnt) return 1; } return 0; }
int dig__fwrite_port_S(short *buf, /* SHORT */ int cnt, GVFILE * fp) { int i, j; unsigned char *c1, *c2; if (Cur_Head->shrt_quick) { if (nat_shrt == PORT_SHORT) { if (dig_fwrite(buf, PORT_SHORT, cnt, fp) == cnt) return 1; } else { buf_alloc(cnt * PORT_SHORT); if (shrt_order == ENDIAN_LITTLE) c1 = (unsigned char *)buf; else c1 = (unsigned char *)buf + nat_shrt - PORT_SHORT; c2 = (unsigned char *)buffer; for (i = 0; i < cnt; i++) { memcpy(c2, c1, PORT_SHORT); c1 += PORT_SHORT; c2 += sizeof(short); } if (dig_fwrite(buffer, PORT_SHORT, cnt, fp) == cnt) return 1; } } else { buf_alloc(cnt * PORT_SHORT); c1 = (unsigned char *)buf; c2 = (unsigned char *)buffer; for (i = 0; i < cnt; i++) { for (j = 0; j < PORT_SHORT; j++) c2[j] = c1[Cur_Head->shrt_cnvrt[j]]; c1 += sizeof(short); c2 += PORT_SHORT; } if (dig_fwrite(buffer, PORT_SHORT, cnt, fp) == cnt) return 1; } return 0; }
static void test_buffer() { size_t i; SizeBuffer abuf; buf_alloc(&abuf, 8); for(i = 0; i < 100; i++) assert(i == buf_add(&abuf, i)); for(i = 0; i < 100; i++) assert(abuf.b[i] == i); buf_dealloc(&abuf); }
pstr STDCALL str_init( pstr ps ) { mem_zero( ps, sizeof( str )); buf_alloc( ps, 32 ); ps->data[0] = 0; ps->use = 1; // print("String Init ps=%x len = %i data = %x ptr=%s\n", ps, ps->use, ps->data, // ps->data ); return ps; }
buf_t * buf_dup(const buf_t *src) { buf_t *bp; bp = buf_alloc(src->max_size); buf_put(bp, src->base + src->head, src->tail - src->head); bp->addr = src->addr; bp->addrlen = src->addrlen; return bp; }
/*M \emph{Initialize a Vorbis stream.} **/ void vorbis_stream_init(vorbis_stream_t *vorbis) { assert(vorbis != NULL); ogg_page_init(&vorbis->page); assert(vorbis->page.raw.data != NULL); vorbis->segment = 0; vorbis->hdr_pages_cnt = 0; int i; for (i = 0; i < VORBIS_MAX_HDR_PAGES; i++) ogg_page_init(vorbis->hdr_pages + i); buf_alloc(&vorbis->id_hdr, 1000); assert(vorbis->id_hdr.data != NULL); buf_alloc(&vorbis->comment_hdr, 1000); assert(vorbis->comment_hdr.data != NULL); buf_alloc(&vorbis->setup_hdr, 2000); assert(vorbis->setup_hdr.data != NULL); }
void send_packets(int num) { int i; buf_t *p; for (i = 0; i < num; ++i) { p = buf_alloc(&pool, packet_size, 16); if (!p) { printf(&debug, "out of memory\n"); break; } memcpy(p->payload, data_pattern, packet_size); if (!netif_output(ð.netif, p, 0, 0)) printf(&debug, "cannot send packet\n"); } }
void ogg_page_init(ogg_page_t *page) { assert(page != NULL); page->raw.size = 0; page->raw.data = NULL; page->page_segments = 0; page->page_no = 0; page->stream = 0; page->page_cksum = 0; buf_alloc(&page->raw, 4000); assert(page->raw.data != NULL); page->size = 0; }
void send_error(struct rekey_session *sess, int errcode, char *msg) { mb_t msgbuf; char *eom = ""; msgbuf = buf_alloc(9+strlen(msg)); if (!msgbuf) return; if (buf_appendint(msgbuf, errcode) || buf_appendstring(msgbuf, msg) || buf_appenddata(msgbuf, eom, 1)) return; sess_send(sess, RESP_ERR, msgbuf); buf_free(msgbuf); }