static void screen_read_close(void) { unsigned i, j; int vcsa_fd; char *data; // Close & re-open vcsa in case they have swapped virtual consoles vcsa_fd = xopen(G.vcsa_name, O_RDONLY); xread(vcsa_fd, &G.remote, 4); i = G.remote.cols * 2; G.first_line_offset = G.y * i; i *= G.remote.lines; if (G.data == NULL) { G.size = i; G.data = xzalloc(2 * i); } else if (G.size != i) { cleanup(1); } data = G.data + G.current; xread(vcsa_fd, data, G.size); close(vcsa_fd); for (i = 0; i < G.remote.lines; i++) { for (j = 0; j < G.remote.cols; j++, NEXT(data)) { unsigned x = j - G.x; // if will catch j < G.x too unsigned y = i - G.y; // if will catch i < G.y too if (CHAR(data) < ' ') CHAR(data) = ' '; if (y >= G.height || x >= G.width) DATA(data) = 0; } } }
static bool siemens_meas_setup(int fd, struct hdr_s* hdr) { off_t start = 0; xseek(fd, start); xread(fd, hdr, sizeof(struct hdr_s)); // check for VD version bool vd = ((hdr->offset < 10000) && (hdr->nscans < 64)); if (vd) { debug_printf(DP_INFO, "VD Header. MeasID: %d FileID: %d Scans: %d\n", hdr->measid, hdr->fileid, hdr->nscans); start += hdr->datoff; xseek(fd, start); // reread offset xread(fd, &hdr->offset, sizeof(hdr->offset)); } else { debug_printf(DP_INFO, "VB Header.\n"); hdr->nscans = 1; } start += hdr->offset; xseek(fd, start); return vd; }
static void unpackExtent( Extent *const xi, // input Extent *const xo, // output f_expand *const f_decompress, f_unfilter *f_unf ) { while (xo->size) { struct b_info h; // Note: if h.sz_unc == h.sz_cpr then the block was not // compressible and is stored in its uncompressed form. // Read and check block sizes. xread(xi, (char *)&h, sizeof(h)); if (h.sz_unc == 0) { // uncompressed size 0 -> EOF if (h.sz_cpr != UPX_MAGIC_LE32) // h.sz_cpr must be h->magic err_exit(2); if (xi->size != 0) // all bytes must be written err_exit(3); break; } if (h.sz_cpr <= 0) { err_exit(4); ERR_LAB } if (h.sz_cpr > h.sz_unc || h.sz_unc > xo->size ) { err_exit(5); } // Now we have: // assert(h.sz_cpr <= h.sz_unc); // assert(h.sz_unc > 0 && h.sz_unc <= blocksize); // assert(h.sz_cpr > 0 && h.sz_cpr <= blocksize); if (h.sz_cpr < h.sz_unc) { // Decompress block nrv_uint out_len = h.sz_unc; // EOF for lzma int const j = (*f_decompress)((unsigned char *)xi->buf, h.sz_cpr, (unsigned char *)xo->buf, &out_len, *(int *)(void *)&h.b_method ); if (j != 0 || out_len != (nrv_uint)h.sz_unc) err_exit(7); // Skip Ehdr+Phdrs: separate 1st block, not filtered if (h.b_ftid!=0 && f_unf // have filter && ((512 < out_len) // this block is longer than Ehdr+Phdrs || (xo->size==(unsigned)h.sz_unc) ) // block is last in Extent ) { (*f_unf)((unsigned char *)xo->buf, out_len, h.b_cto8, h.b_ftid); } xi->buf += h.sz_cpr; xi->size -= h.sz_cpr; } else { // copy literal block xread(xi, xo->buf, h.sz_cpr); } xo->buf += h.sz_unc; xo->size -= h.sz_unc; }
static int do_epoch_log_read(uint32_t epoch, struct sd_node *nodes, int len, time_t *timestamp) { int fd, ret, nr_nodes; char path[PATH_MAX]; struct stat epoch_stat; snprintf(path, sizeof(path), "%s%08u", epoch_path, epoch); fd = open(path, O_RDONLY); if (fd < 0) { sd_eprintf("failed to open epoch %"PRIu32" log, %m", epoch); goto err; } memset(&epoch_stat, 0, sizeof(epoch_stat)); ret = fstat(fd, &epoch_stat); if (ret < 0) { sd_eprintf("failed to stat epoch %"PRIu32" log, %m", epoch); goto err; } if (len < epoch_stat.st_size - sizeof(*timestamp)) { sd_eprintf("invalid epoch %"PRIu32" log", epoch); goto err; } ret = xread(fd, nodes, epoch_stat.st_size - sizeof(*timestamp)); if (ret < 0) { sd_eprintf("failed to read epoch %"PRIu32" log, %m", epoch); goto err; } /* Broken epoch, just ignore */ if (ret % sizeof(struct sd_node) != 0) { sd_eprintf("invalid epoch %"PRIu32" log", epoch); goto err; } nr_nodes = ret / sizeof(struct sd_node); if (timestamp) { ret = xread(fd, timestamp, sizeof(*timestamp)); if (ret != sizeof(*timestamp)) { sd_eprintf("invalid epoch %"PRIu32" log", epoch); goto err; } } close(fd); return nr_nodes; err: if (fd >= 0) close(fd); return -1; }
/* * Link KGZ file and loader. */ void kgzld(struct kgz_hdr * kh, const char *f1, const char *f2) { char addr[16]; struct iodesc idi; pid_t pid; size_t n; int status; if (strcmp(kh->ident, "KGZ")) { if ((idi.fd = open(idi.fname = f1, O_RDONLY)) == -1) err(1, "%s", idi.fname); if (!format) { union { struct exec ex; Elf32_Ehdr ee; } hdr; n = xread(&idi, &hdr, sizeof(hdr), 0); if (n >= sizeof(hdr.ee) && IS_ELF(hdr.ee)) format = F_ELF; else if (n >= sizeof(hdr.ex) && N_GETMAGIC(hdr.ex) == OMAGIC) format = F_AOUT; if (!format) errx(1, "%s: Format not supported", idi.fname); } n = xread(&idi, kh, sizeof(*kh), format == F_AOUT ? sizeof(struct kgz_aouthdr0) : sizeof(struct kgz_elfhdr)); xclose(&idi); if (n != sizeof(*kh) || strcmp(kh->ident, "KGZ")) errx(1, "%s: Invalid format", idi.fname); } sprintf(addr, "%#x", align(kh->dload + kh->dsize, 0x1000)); switch (pid = fork()) { case -1: err(1, NULL); case 0: if (format == F_AOUT) execlp("ld", "ld", "-aout", "-Z", "-T", addr, "-o", f2, loader, f1, (char *)NULL); else execlp("ld", "ld", "-Ttext", addr, "-o", f2, loader, f1, (char *)NULL); warn(NULL); _exit(1); default: if ((pid = waitpid(pid, &status, 0)) == -1) err(1, NULL); if (WIFSIGNALED(status) || WEXITSTATUS(status)) exit(1); } }
void decode(int fd, dataset_t* bek_data, external_info_header_t* header, key_header_t* key_header) { memset (bek_data, 0, sizeof(dataset_t)); memset (header, 0, sizeof(external_info_header_t)); memset (key_header, 0, sizeof(key_header_t)); xread (fd, bek_data, sizeof(dataset_t)); xread (fd, header, sizeof(external_info_header_t)); xread (fd, key_header, sizeof(key_header_t)); }
static void unpackExtent( struct Extent *const xi, // input struct Extent *const xo, // output f_expand *const f_decompress ) { while (xo->size) { struct b_info h; // Note: if h.sz_unc == h.sz_cpr then the block was not // compressible and is stored in its uncompressed form. // Read and check block sizes. xread(xi, (char *)&h, sizeof(h)); if (h.sz_unc == 0) { // uncompressed size 0 -> EOF if (h.sz_cpr != UPX_MAGIC_LE32) // h.sz_cpr must be h->magic err_exit(2); if (xi->size != 0) // all bytes must be written err_exit(3); break; } if (h.sz_cpr <= 0) { err_exit(4); ERR_LAB } if (h.sz_cpr > h.sz_unc || h.sz_unc > xo->size ) { err_exit(5); } // Now we have: // assert(h.sz_cpr <= h.sz_unc); // assert(h.sz_unc > 0 && h.sz_unc <= blocksize); // assert(h.sz_cpr > 0 && h.sz_cpr <= blocksize); if (h.sz_cpr < h.sz_unc) { // Decompress block nrv_uint out_len = h.sz_unc; // EOF for lzma int const j = (*f_decompress)((unsigned char *)xi->buf, h.sz_cpr, (unsigned char *)xo->buf, &out_len, *(int *)(void *)&h.b_method ); if (j != 0 || out_len != (nrv_uint)h.sz_unc) err_exit(7); xi->buf += h.sz_cpr; xi->size -= h.sz_cpr; } else { // copy literal block xread(xi, xo->buf, h.sz_cpr); } xo->buf += h.sz_unc; xo->size -= h.sz_unc; }
/* wait for a character on the serial port */ unsigned char get_char(void) { unsigned char c; assert(portfd >= 0); xread(portfd, &c, 1); return c; }
static Ioclust* getclust(Xdata *dev, long addr, ulong tag) { Ioclust *c, *f; f = nil; for(c=iohead; c; c=c->next){ if(!c->busy && c->tag == tag) f = c; if(c->addr == addr && c->dev == dev){ c->busy++; return c; } } if(f == nil) panic(0, "out of buffers"); f->addr = addr; f->dev = dev; f->busy++; if(waserror()){ f->addr = -1; /* stop caching */ putclust(f); nexterror(); } xread(f); poperror(); return f; }
char *xreadstr(int fd) { int size = 0; int len = 0; char *str = NULL; int n; do { if (size - len - 1 <= 0) { str = (size == 0) ? xmalloc(CHUNKSIZE) : xrealloc(str, size + CHUNKSIZE); size += CHUNKSIZE; } //n = xread(fd, str + len, size - len - 1); n = xread(fd, str + len, 1); if (n < 0) err_exit (TRUE, "read"); if (n == 0) err_exit (FALSE, "EOF on read"); len += n; str[len] = '\0'; } while (len < 2 || strcmp(&str[len - 2], "\r\n") != 0); str[len - 2] = '\0'; return str; }
char get_header_tar_gz(archive_handle_t *archive_handle) { #if BB_MMU unsigned char magic[2]; #endif /* Can't lseek over pipes */ archive_handle->seek = seek_by_read; /* Check gzip magic only if open_transformer will invoke unpack_gz_stream (MMU case). * Otherwise, it will invoke an external helper "gunzip -cf" (NOMMU case) which will * need the header. */ #if BB_MMU xread(archive_handle->src_fd, &magic, 2); if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { bb_error_msg_and_die("invalid gzip magic"); } #endif archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_gz_stream, "gunzip"); archive_handle->offset = 0; while (get_header_tar(archive_handle) == EXIT_SUCCESS) continue; /* Can only do one file at a time */ return EXIT_FAILURE; }
ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint) { size_t oldlen = sb->len; size_t oldalloc = sb->alloc; strbuf_grow(sb, hint ? hint : 8192); for (;;) { ssize_t cnt; cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1); if (cnt < 0) { if (oldalloc == 0) strbuf_release(sb); else strbuf_setlen(sb, oldlen); return -1; } if (!cnt) break; sb->len += cnt; strbuf_grow(sb, 8192); } sb->buf[sb->len] = '\0'; return sb->len - oldlen; }
static void send_local_file(const char *the_type, const char *name) { const char *p = git_path("%s", name); size_t buf_alloc = 8192; char *buf = xmalloc(buf_alloc); int fd; struct stat sb; fd = open(p, O_RDONLY); if (fd < 0) not_found("Cannot open '%s': %s", p, strerror(errno)); if (fstat(fd, &sb) < 0) die_errno("Cannot stat '%s'", p); hdr_int(content_length, sb.st_size); hdr_str(content_type, the_type); hdr_date(last_modified, sb.st_mtime); end_headers(); for (;;) { ssize_t n = xread(fd, buf, buf_alloc); if (n < 0) die_errno("Cannot read '%s'", p); if (!n) break; write_or_die(1, buf, n); } close(fd); free(buf); }
int init_config_file(void) { int fd, ret; check_tmp_config(); fd = open(config_path, O_RDONLY); if (fd < 0) { if (errno != ENOENT) { sd_eprintf("failed to read config file, %m"); return -1; } goto create; } ret = xread(fd, &config, sizeof(config)); if (ret == 0) { close(fd); goto create; } if (ret < 0) { sd_eprintf("failed to read config file, %m"); goto out; } if (config.version != SD_FORMAT_VERSION) { sd_eprintf("This sheep version is not compatible with" " the existing data layout, %d", config.version); if (sys->upgrade) { /* upgrade sheep store */ ret = sd_migrate_store(config.version, SD_FORMAT_VERSION); if (ret == 0) { /* reload config file */ ret = xpread(fd, &config, sizeof(config), 0); if (ret != sizeof(config)) { sd_eprintf("failed to reload config" " file, %m"); ret = -1; } else ret = 0; } goto out; } sd_eprintf("use '-u' option to upgrade sheep store"); ret = -1; goto out; } ret = 0; out: close(fd); return ret; create: config.version = SD_FORMAT_VERSION; if (write_config() != SD_RES_SUCCESS) return -1; return 0; }
/* * read over a non-block fd */ static int qmp_read(int fd, void *buf, size_t *len) { struct pollfd pfd; size_t tread = 0, nread; int r; pfd.fd = fd; pfd.events = POLLIN; while ((r = poll(&pfd, 1, QMP_POLL_TIMEOUT)) != 0) { if (r == -1) return -1; if (pfd.revents & POLLIN) { /* read at max 1k at a time */ nread = xread(fd, buf, 1024); tread += nread; buf += nread; } } *len = tread; return 0; }
char FAST_FUNC get_header_tar_gz(archive_handle_t *archive_handle) { #if BB_MMU uint16_t magic; #endif /* Can't lseek over pipes */ archive_handle->seek = seek_by_read; /* Check gzip magic only if open_transformer will invoke unpack_gz_stream (MMU case). * Otherwise, it will invoke an external helper "gunzip -cf" (NOMMU case) which will * need the header. */ #if BB_MMU xread(archive_handle->src_fd, &magic, 2); /* Can skip this check, but error message will be less clear */ if (magic != GZIP_MAGIC) { bb_error_msg_and_die("invalid gzip magic"); } #endif open_transformer(archive_handle->src_fd, unpack_gz_stream, "gunzip"); archive_handle->offset = 0; while (get_header_tar(archive_handle) == EXIT_SUCCESS) continue; /* Can only do one file at a time */ return EXIT_FAILURE; }
static void extract_cpio_gz(int fd) { archive_handle_t *archive_handle; unsigned char magic[2]; /* Initialise */ archive_handle = init_handle(); archive_handle->seek = seek_by_read; //archive_handle->action_header = header_list; archive_handle->action_data = data_extract_all; archive_handle->flags |= ARCHIVE_PRESERVE_DATE; archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS; archive_handle->src_fd = fd; archive_handle->offset = 0; xread(archive_handle->src_fd, &magic, 2); if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { bb_error_msg_and_die("invalid gzip magic"); } check_header_gzip(archive_handle->src_fd); xchdir("/"); /* Install RPM's to root */ archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip); archive_handle->offset = 0; while (get_header_cpio(archive_handle) == EXIT_SUCCESS) /* loop */; }
static void *read_working_object(uint64_t oid, int length) { void *buf = NULL; char path[PATH_MAX]; int fd, ret; snprintf(path, sizeof(path), "%s%016" PRIx64, obj_path, oid); fd = open(path, O_RDONLY, def_fmode); if (fd < 0) { dprintf("object %"PRIx64" not found\n", oid); goto out; } buf = malloc(length); if (!buf) { eprintf("no memory to allocate buffer.\n"); goto out; } ret = xread(fd, buf, length); if (length != ret) { eprintf("object read error.\n"); free(buf); buf = NULL; goto out; } out: if (fd > 0) close(fd); return buf; }
void* _elf_read(Elf *elf, void *buffer, size_t off, size_t len) { void *tmp; elf_assert(elf); elf_assert(elf->e_magic == ELF_MAGIC); elf_assert(off >= 0 && off + len <= elf->e_size); if (elf->e_disabled) { seterr(ERROR_FDDISABLED); } else if (len) { off += elf->e_base; if (lseek(elf->e_fd, (off_t)off, SEEK_SET) != (off_t)off) { seterr(ERROR_IO_SEEK); } else if (!(tmp = buffer) && !(tmp = malloc(len))) { seterr(ERROR_IO_2BIG); } else if (xread(elf->e_fd, tmp, len)) { seterr(ERROR_IO_READ); if (tmp != buffer) { free(tmp); } } else { return tmp; } } return NULL; }
char *target_gets(target_context_t *tc, char *s, int size) { unsigned char c; char *p = s; assert(tc); assert(s); assert(size >= 0); if (!size--) return s; while (size--) { ssize_t nread = xread(tc->portfd, &c, 1); if(nread == ERROR_VALUE) break; if(nread == 0) break; //timeout if (c == '\r') ++size; else *p++ = c; if (c == '\n') break; } *p = '\0'; /* report error lines */ if (ok_or_ng(s) < 0) warnf("from target: %s", s); return s; }
void retr_opt(client_ftp_t *client_ftp, char *cmd) { FILE *fs; char buff[1024]; char put[1024]; int nb; int i; int j; if ((i = init_download(client_ftp, cmd)) == 1) return; if ((fs = fopen(cmd + 4, "w+")) == NULL) { printf("Failed to open file\n"); close(client_ftp->s_data); return; } bzero(buff, 1024); while ((nb = xread(client_ftp->s_data, buff, 1024))) { bzero(put, 1024); for (i = 0, j = 0; i != nb; i++, j++) put[j] = buff[i]; fwrite(put, 1, j, fs); bzero(buff, 1024); } fclose(fs); i = xrecv(client_ftp->s, buff, 1024, 0); clean_buff(buff, i); printf("%s\n", buff); close(client_ftp->s_data); }
int init_node_config_file(void) { int fd, ret; struct stat st; ret = stat(node_config_path, &st); if (ret < 0) /* this node doesn't have a node config, do nothing */ return 0; fd = open(node_config_path, O_RDONLY); if (fd < 0) { sd_err("failed to open node config (%s): %m", node_config_path); return -1; } ret = xread(fd, &sys->ninfo, sizeof(sys->ninfo)); if (ret != sizeof(sys->ninfo)) { sd_err("failed to read node config (%s): %m", node_config_path); return -1; } close(fd); return 0; }
void tun_serv_in(int fd_udp, int fd_tun, struct tun_state *state, char *buf) { int recvd=xread(fd_tun, buf, BUFF_SIZE); debug_print("serv: recvd %dB from tun\n", recvd); if (recvd > MIN_PKT_SIZE) { /* Remove PlanetLab TUN PPI header */ if (state->planetlab) { buf+=4;recvd-=4; } struct tun_rec *rec = NULL; /* read sport for clients mapping */ int sport = (int) ntohs( *((uint16_t *)(buf+22)) ); if ( (rec = g_hash_table_lookup(state->serv, &sport)) ) { int sent = xsendto(fd_udp, rec->sa, buf, recvd); debug_print("serv: wrote %dB to udp\n",sent); } else { errno=EFAULT; die("lookup"); } } }
static bool read_page_header(struct urf_context *ctx) { if (!xread(ctx, ctx->page_hdr, sizeof(struct urf_page_header))) { return false; } struct urf_page_header *hdr = ctx->page_hdr; if (!hdr->bpp || hdr->bpp > 32 || hdr->bpp % 8) { URF_SET_ERROR(ctx, "invalid bpp", -hdr->bpp); return false; } if (hdr->bpp != 24) { URF_SET_ERROR(ctx, "unsupported bpp", -hdr->bpp); return false; } if (hdr->colorspace != 1) { URF_SET_ERROR(ctx, "unsupported colorspace", -hdr->colorspace); return false; } SWAP32(hdr->unknown0); SWAP32(hdr->unknown1); SWAP32(hdr->width); SWAP32(hdr->height); SWAP32(hdr->dpi); SWAP32(hdr->unknown2); SWAP32(hdr->unknown3); return true; }
static void parent_finish(test_data* td) { char buf; pipe_state *ps = (pipe_state *)td->data; xread(ps->fin_fds[0], &buf, 1); }
void *snap_log_read(int *out_nr, int user) { struct strbuf buf = STRBUF_INIT; struct stat st; void *buffer = NULL; int len, fd; strbuf_addstr(&buf, farm_dir); strbuf_addf(&buf, "/%s", user ? "user_snap" : "sys_snap"); fd = open(buf.buf, O_RDONLY); if (fd < 0) { dprintf("%m\n"); goto out; } if (fstat(fd, &st) < 0) { dprintf("%m\n"); goto out_close; } len = st.st_size; buffer = xmalloc(len); len = xread(fd, buffer, len); if (len != st.st_size) { free(buffer); buffer = NULL; goto out_close; } *out_nr = len / sizeof(struct snap_log); out_close: close(fd); out: strbuf_release(&buf); return buffer; }
char *get_next_line(int fd) { static int nbread; static char buff[BUFF_SIZE]; char *line; int pos; for (line = 0, pos = 0; ; pos++) { if (begin >= nbread) { begin = 0; if (!(nbread = xread(fd, buff, BUFF_SIZE))) return (line); pos = 0; } if (buff[begin + pos] == '\n') { line = add_line(line, buff, pos); return (line); } if (begin + pos + 1 == nbread) line = add_line(line, buff, pos + 1); } }
int read_chunk(void) { ssize_t s, len, offset; chunk_no++; len = chunk_size + spare_size; offset = 0; memset(chunk_data, 0xff, sizeof(chunk_data)); if (buf_len > buf_idx) { /* copy from buffer */ s = buf_len - buf_idx; if (s > len) s = len; memcpy(data, buffer+buf_idx, s); buf_idx += s; offset += s; } if (offset < len) { /* read from file */ s = xread(img_file, data+offset, len-offset); if (s < 0) prt_err(1, errno, "Read image file"); offset += s; } if (offset != 0 && offset != len) /* partial chunk */ prt_err(1, 0, "Broken image file"); return offset != 0; }
/* Open an existing cache slot and fill the cache buffer with * (part of) the content of the cache file. Return 0 on success * and errno otherwise. */ static int open_slot(struct cache_slot *slot) { char *bufz; ssize_t bufkeylen = -1; slot->cache_fd = open(slot->cache_name, O_RDONLY); if (slot->cache_fd == -1) return errno; if (fstat(slot->cache_fd, &slot->cache_st)) return errno; slot->bufsize = xread(slot->cache_fd, slot->buf, sizeof(slot->buf)); if (slot->bufsize < 0) return errno; bufz = memchr(slot->buf, 0, slot->bufsize); if (bufz) bufkeylen = bufz - slot->buf; if (slot->key) slot->match = bufkeylen == slot->keylen && !memcmp(slot->key, slot->buf, bufkeylen + 1); return 0; }
int var_read_block_header(FILE *fi, var_block_header *header) { // read block number header->num = xread32(fi); //TOFIX: 64 bit num ?? xread32(fi); // read compressed size header->in_len = xread32(fi); // read uncompressed size header->out_len = xread32(fi); // empty / 64 ? xread32(fi); // read block method ? header->method = xread32(fi); // read block chk xread(fi, header->chk, 16, 1); // empty xskip(fi, 56); // read block pos header->pos = xread64(fi); //TOFIX: use block_pos ? xskip(fi, 8); return 1; }