static int decode(nl_stream_t *s, nl_decoder_t *d, int depth, char *buf, size_t len) { nl_buf_t in, out; int n; log_trace("#%d decode", s->sock.fd); if (d->remain.len == 0) { in.buf = buf; in.len = len; } else { if (enlarge_buffer(d, d->remain.len + len) < 0) { return -1; } memcpy(d->remain.buf + d->remain.len, buf, len); d->remain.len += len; in = d->remain; } while ((n = d->decoder(d->data, &in, &out)) > 0) { in.buf += n; in.len -= n; if (out.len > 0) { if (d->next == NULL) { log_trace("#%d decode %zu bytes", s->sock.fd, out.len); s->cbs.on_received(s, &out); } else { if (decode(s, d->next, depth + 1, out.buf, out.len) < 0) { return -1; } } } if (!is_decoder_valid(s, depth)) { out = in; if (out.len > 0) { log_trace("#%d decode remain %zu bytes", s->sock.fd, out.len); s->cbs.on_received(s, &out); } return 0; } } if (n < 0) { return -1; } if (in.len > 0) { if (enlarge_buffer(d, in.len) < 0) { return -1; } memmove(d->remain.buf, in.buf, in.len); } d->remain.len = in.len; return 0; }
/** * Move all bytes after the last record separator in the current buffer * to the overflow buffer. */ inline void move_trailing_data_after_last_record(Buffer *tgt, Buffer *src) { int trailing_bytes_begin = src->end_of_last_record + 1; int num_trailing_bytes_to_copy = src->size - (trailing_bytes_begin - src->begin); if (num_trailing_bytes_to_copy > 0) { // ensure capacity DEBUG(" trailing data found in buffer %p (%d bytes, from %d)", src, num_trailing_bytes_to_copy, trailing_bytes_begin); int capacity = tgt->capacity; while (capacity - tgt->size < num_trailing_bytes_to_copy) { capacity *= 2; } if (capacity > tgt->capacity) { DEBUG(" enlarging capacity of buffer %p to %d bytes from %d bytes", tgt, capacity, tgt->capacity); enlarge_buffer(tgt, capacity); } // copy data DEBUG(" copying trailing data to buffer %p from %p", tgt, src); memcpy(tgt->data + tgt->begin, src->data + trailing_bytes_begin, num_trailing_bytes_to_copy); tgt->size += num_trailing_bytes_to_copy; src->size -= num_trailing_bytes_to_copy; } }
int main(int argc, char** argv) { char c; char* buff = malloc(sizeof(char)*INITIAL_BUFF_SIZE); int buffsize = INITIAL_BUFF_SIZE; int charsread = 0; puts("Enter text. Include a dot ('.') in your input to exit:"); do { c = getchar(); charsread++; // Enlarge the buffer if we need to. if (charsread > buffsize) { buffsize = buffsize * 2; buff = enlarge_buffer(buff, buffsize); } buff[charsread] = c; } while (c != '.'); size_t i; for (i = 0; i < charsread; i++) { putchar(buff[i]); } free(buff); }
/** * Writes byte to buffer. * * @param buffer * @param val */ void write_byte(type_buffer *buffer, uint8_t val) { if(buffer->bytes_count == buffer->size) { enlarge_buffer(buffer); } buffer->data[buffer->bytes_count] = val; // printf("(%u,%x)\n", val, val); buffer->bytes_count++; }
static void ocl_info(QSP_ARG_DECL Compute_Platform *cpp) { int s; sprintf(MSG_STR,"Vendor: %s",OCLPF_VENDOR(cpp)); prt_msg(MSG_STR); sprintf(MSG_STR,"Version: %s",OCLPF_VERSION(cpp)); prt_msg(MSG_STR); sprintf(MSG_STR,"Profile: %s",OCLPF_PROFILE(cpp)); prt_msg(MSG_STR); // The extensions can be long... s = (int) strlen(OCLPF_EXTENSIONS(cpp))+strlen(EXTENSIONS_PREFIX)+2; if( s > sb_size(QS_SCRATCH) ) enlarge_buffer( QS_SCRATCH, s ); sprintf(sb_buffer(QS_SCRATCH),"%s%s\n",EXTENSIONS_PREFIX,OCLPF_EXTENSIONS(cpp)); prt_msg(sb_buffer(QS_SCRATCH)); }
/* Parse and sanity check user_spec. * * If successful, set global variables 'uid' and 'gid' * with the parsed results. Global variable 'user' * will be pointing to a string that stores the name * of the user to be switched into. * * Also set 'switch_to_new_user' to true, The actual * user switching is done as soon as daemonize_start() * is called. I/O access before calling daemonize_start() * will still be with root's credential. */ void daemon_set_new_user(const char *user_spec) { char *pos = strchr(user_spec, ':'); size_t init_bufsize, bufsize; init_bufsize = get_sysconf_buffer_size(); uid = getuid(); gid = getgid(); if (geteuid() || uid) { VLOG_FATAL("%s: only root can use --user option", pidfile); } user_spec += strspn(user_spec, " \t\r\n"); size_t len = pos ? pos - user_spec : strlen(user_spec); char *buf; struct passwd pwd, *res; int e; bufsize = init_bufsize; buf = xmalloc(bufsize); if (len) { user = xmemdup0(user_spec, len); while ((e = getpwnam_r(user, &pwd, buf, bufsize, &res)) == ERANGE) { if (!enlarge_buffer(&buf, &bufsize)) { break; } } if (e != 0) { VLOG_FATAL("%s: Failed to retrive user %s's uid (%s), aborting.", pidfile, user, ovs_strerror(e)); } } else { /* User name is not specified, use current user. */ while ((e = getpwuid_r(uid, &pwd, buf, bufsize, &res)) == ERANGE) { if (!enlarge_buffer(&buf, &bufsize)) { break; } } if (e != 0) { VLOG_FATAL("%s: Failed to retrive current user's name " "(%s), aborting.", pidfile, ovs_strerror(e)); } user = xstrdup(pwd.pw_name); } uid = pwd.pw_uid; gid = pwd.pw_gid; free(buf); if (pos) { char *grpstr = pos + 1; grpstr += strspn(grpstr, " \t\r\n"); if (*grpstr) { struct group grp, *res; bufsize = init_bufsize; buf = xmalloc(bufsize); while ((e = getgrnam_r(grpstr, &grp, buf, bufsize, &res)) == ERANGE) { if (!enlarge_buffer(&buf, &bufsize)) { break; } } if (e) { VLOG_FATAL("%s: Failed to get group entry for %s, " "(%s), aborting.", pidfile, grpstr, ovs_strerror(e)); } if (gid != grp.gr_gid) { char **mem; for (mem = grp.gr_mem; *mem; ++mem) { if (!strcmp(*mem, user)) { break; } } if (!*mem) { VLOG_FATAL("%s: Invalid --user option %s (user %s is " "not in group %s), aborting.", pidfile, user_spec, user, grpstr); } gid = grp.gr_gid; } free(buf); } } switch_user = true; }
int import_compress_finish(ImportCompress *c, void **buffer, size_t *buffer_size, size_t *buffer_allocated) { int r; assert(c); assert(buffer); assert(buffer_size); assert(buffer_allocated); if (!c->encoding) return -EINVAL; *buffer_size = 0; switch (c->type) { case IMPORT_COMPRESS_XZ: { lzma_ret lzr; c->xz.avail_in = 0; do { r = enlarge_buffer(buffer, buffer_size, buffer_allocated); if (r < 0) return r; c->xz.next_out = (uint8_t*) *buffer + *buffer_size; c->xz.avail_out = *buffer_allocated - *buffer_size; lzr = lzma_code(&c->xz, LZMA_FINISH); if (lzr != LZMA_OK && lzr != LZMA_STREAM_END) return -EIO; *buffer_size += (*buffer_allocated - *buffer_size) - c->xz.avail_out; } while (lzr != LZMA_STREAM_END); break; } case IMPORT_COMPRESS_GZIP: c->gzip.avail_in = 0; do { r = enlarge_buffer(buffer, buffer_size, buffer_allocated); if (r < 0) return r; c->gzip.next_out = (uint8_t*) *buffer + *buffer_size; c->gzip.avail_out = *buffer_allocated - *buffer_size; r = deflate(&c->gzip, Z_FINISH); if (r != Z_OK && r != Z_STREAM_END) return -EIO; *buffer_size += (*buffer_allocated - *buffer_size) - c->gzip.avail_out; } while (r != Z_STREAM_END); break; case IMPORT_COMPRESS_BZIP2: c->bzip2.avail_in = 0; do { r = enlarge_buffer(buffer, buffer_size, buffer_allocated); if (r < 0) return r; c->bzip2.next_out = (void*) ((uint8_t*) *buffer + *buffer_size); c->bzip2.avail_out = *buffer_allocated - *buffer_size; r = BZ2_bzCompress(&c->bzip2, BZ_FINISH); if (r != BZ_FINISH_OK && r != BZ_STREAM_END) return -EIO; *buffer_size += (*buffer_allocated - *buffer_size) - c->bzip2.avail_out; } while (r != BZ_STREAM_END); break; case IMPORT_COMPRESS_UNCOMPRESSED: break; default: return -EOPNOTSUPP; } return 0; }
int import_compress(ImportCompress *c, const void *data, size_t size, void **buffer, size_t *buffer_size, size_t *buffer_allocated) { int r; assert(c); assert(buffer); assert(buffer_size); assert(buffer_allocated); if (!c->encoding) return -EINVAL; if (size <= 0) return 0; assert(data); *buffer_size = 0; switch (c->type) { case IMPORT_COMPRESS_XZ: c->xz.next_in = data; c->xz.avail_in = size; while (c->xz.avail_in > 0) { lzma_ret lzr; r = enlarge_buffer(buffer, buffer_size, buffer_allocated); if (r < 0) return r; c->xz.next_out = (uint8_t*) *buffer + *buffer_size; c->xz.avail_out = *buffer_allocated - *buffer_size; lzr = lzma_code(&c->xz, LZMA_RUN); if (lzr != LZMA_OK) return -EIO; *buffer_size += (*buffer_allocated - *buffer_size) - c->xz.avail_out; } break; case IMPORT_COMPRESS_GZIP: c->gzip.next_in = (void*) data; c->gzip.avail_in = size; while (c->gzip.avail_in > 0) { r = enlarge_buffer(buffer, buffer_size, buffer_allocated); if (r < 0) return r; c->gzip.next_out = (uint8_t*) *buffer + *buffer_size; c->gzip.avail_out = *buffer_allocated - *buffer_size; r = deflate(&c->gzip, Z_NO_FLUSH); if (r != Z_OK) return -EIO; *buffer_size += (*buffer_allocated - *buffer_size) - c->gzip.avail_out; } break; case IMPORT_COMPRESS_BZIP2: c->bzip2.next_in = (void*) data; c->bzip2.avail_in = size; while (c->bzip2.avail_in > 0) { r = enlarge_buffer(buffer, buffer_size, buffer_allocated); if (r < 0) return r; c->bzip2.next_out = (void*) ((uint8_t*) *buffer + *buffer_size); c->bzip2.avail_out = *buffer_allocated - *buffer_size; r = BZ2_bzCompress(&c->bzip2, BZ_RUN); if (r != BZ_RUN_OK) return -EIO; *buffer_size += (*buffer_allocated - *buffer_size) - c->bzip2.avail_out; } break; case IMPORT_COMPRESS_UNCOMPRESSED: if (*buffer_allocated < size) { void *p; p = realloc(*buffer, size); if (!p) return -ENOMEM; *buffer = p; *buffer_allocated = size; } memcpy(*buffer, data, size); *buffer_size = size; break; default: return -EOPNOTSUPP; } return 0; }