void *rstack_alloc (lua_State *L, size_t size) { RStack *rs = rstack(L); Region *r = check_exp(rs->cregnum > 0, rs->creg); RObject *top = r->top; if (top == rs->rbuf.last) { RObject *ohead = rs->rbuf.head; buf_resize(L, rs, rs->rbuf.size * 2); buf_fix(rs, ohead); top = r->top; } top->body = luaM_malloc(L, size); ++r->top; return top->body; }
INLINE void appendarray(buffer_t *dst, const buffer_unit_t *arr, size_t bits) { size_t total_bits = dst->bitpos + bits; if (total_bits >= (dst->size - 1) * BUFFER_UNIT_BITS * BUFFER_UNIT_SIZE) { size_t shift = 1; while (total_bits >= ((dst->size << shift) - 1) * BUFFER_UNIT_BITS * BUFFER_UNIT_SIZE) { shift++; } buf_resize(dst, shift); } buf_writearray(dst, arr, bits); }
AW_Result aw_session_entity (AW_Session *sess, const AW_Char *buf, AW_Size size) { AW_ASSERT(sess); if (!size) return AW_OK; if (sess->len == sess->buf_size) { buf_resize(sess, sess->len + size); } memcpy(sess->buf + sess->len, buf, size); sess->len += size; return AW_OK; }
static void append_char (AW_Session *sess, AW_Char ch) { AW_Size last = sess->pos + sess->len; if (last == sess->buf_size) { if (sess->pos) { memmove(sess->buf, sess->buf + sess->pos, sess->len); sess->pos = 0; } else { buf_resize(sess, sess->buf_size + 1); } } sess->buf[last] = ch; sess->len++; }
/* compresses len bytes from src, outputting to dest (starting from the * respective current positions. */ static void buf_compress(buffer * dest, buffer * src, unsigned int len) { unsigned int endpos = src->pos + len; int result; TRACE(("enter buf_compress")); while (1) { ses.keys->trans_zstream->avail_in = endpos - src->pos; ses.keys->trans_zstream->next_in = buf_getptr(src, ses.keys->trans_zstream->avail_in); ses.keys->trans_zstream->avail_out = dest->size - dest->pos; ses.keys->trans_zstream->next_out = buf_getwriteptr(dest, ses.keys->trans_zstream->avail_out); result = deflate(ses.keys->trans_zstream, Z_SYNC_FLUSH); buf_setpos(src, endpos - ses.keys->trans_zstream->avail_in); buf_setlen(dest, dest->size - ses.keys->trans_zstream->avail_out); buf_setpos(dest, dest->len); if (result != Z_OK) { dropbear_exit("zlib error"); } if (ses.keys->trans_zstream->avail_in == 0) { break; } assert(ses.keys->trans_zstream->avail_out == 0); /* the buffer has been filled, we must extend. This only happens in * unusual circumstances where the data grows in size after deflate(), * but it is possible */ buf_resize(dest, dest->size + ZLIB_COMPRESS_INCR); } TRACE(("leave buf_compress")); }
void buf_appendf(Buf *buf, const char *format, ...) { assert(buf->list.length); va_list ap, ap2; va_start(ap, format); va_copy(ap2, ap); int len1 = vsnprintf(nullptr, 0, format, ap); assert(len1 >= 0); size_t required_size = len1 + 1; size_t orig_len = buf_len(buf); buf_resize(buf, orig_len + len1); int len2 = vsnprintf(buf_ptr(buf) + orig_len, required_size, format, ap2); assert(len2 == len1); va_end(ap2); va_end(ap); }
/* returns a pointer to a newly created buffer */ static buffer* buf_decompress(buffer* buf, unsigned int len) { int result; buffer * ret; z_streamp zstream; zstream = ses.keys->recv_zstream; ret = buf_new(len); zstream->avail_in = len; zstream->next_in = buf_getptr(buf, len); /* decompress the payload, incrementally resizing the output buffer */ while (1) { zstream->avail_out = ret->size - ret->pos; zstream->next_out = buf_getwriteptr(ret, zstream->avail_out); result = inflate(zstream, Z_SYNC_FLUSH); buf_setlen(ret, ret->size - zstream->avail_out); buf_setpos(ret, ret->len); if (result != Z_BUF_ERROR && result != Z_OK) { dropbear_exit("zlib error"); } if (zstream->avail_in == 0 && (zstream->avail_out != 0 || result == Z_BUF_ERROR)) { /* we can only exit if avail_out hasn't all been used, * and there's no remaining input */ return ret; } if (zstream->avail_out == 0) { buf_resize(ret, ret->size + ZLIB_DECOMPRESS_INCR); } } }
void mesh_compute_normals(struct mesh *mesh) { int i, nr_faces; struct idx *idx; buf_resize(mesh->nbuf, buf_len(mesh->vbuf)); memset(mesh->nbuf, 0, buf_len(mesh->nbuf) * sizeof(*mesh->nbuf)); buf_foreach(idx, mesh->ibuf) idx->ni = idx->vi; nr_faces = mesh_face_count(mesh); for (i = 0; i < nr_faces; i++) { int j, nr_verts; nr_verts = mesh_face_vertex_count(mesh, i); for (j = 0; j < nr_verts; j++) { vector u, v, n; float *v0, *v1, *v2, *vn; v0 = mesh_get_vertex(mesh, i, j); v1 = mesh_get_vertex(mesh, i, (j + 1) % nr_verts); v2 = mesh_get_vertex(mesh, i, (j + nr_verts - 1) % nr_verts); vec_sub(u, v1, v0); vec_sub(v, v2, v0); vec_cross(n, u, v); vec_normalize(n, n); vn = mesh_get_normal(mesh, i, j); vec_add(vn, vn, n); } } for (i = 0; i < buf_len(mesh->nbuf); i += 3) { float *n = mesh->nbuf + i; vec_normalize(n, n); } }
static AW_Result recv_data (AW_Session *sess) { AW_Size left, cnt; if (sess->flags & AW_SESSION_FL_ERROR) return AW_FAILED; if (sess->flags & AW_SESSION_FL_END) return 0; cnt = sess->len; left = sess->buf_size - cnt; if (cnt && sess->pos) { memmove(sess->buf, sess->buf + sess->pos, cnt); sess->pos = 0; } if (left == 0) { buf_resize(sess, 0); left = sess->buf_size - sess->len; } cnt = aw_session_recv(sess, sess->buf + sess->len, left); if (cnt <= 0) { sess->flags |= AW_SESSION_FL_END; if (cnt < 0) sess->flags |= AW_SESSION_FL_ERROR; return cnt; } sess->len += cnt; return AW_OK; }
/* handle the received packet */ void decrypt_packet() { unsigned char blocksize; unsigned char macsize; unsigned int padlen; unsigned int len; TRACE(("enter decrypt_packet")); blocksize = ses.keys->recv_algo_crypt->blocksize; macsize = ses.keys->recv_algo_mac->hashsize; ses.kexstate.datarecv += ses.readbuf->len; /* we've already decrypted the first blocksize in read_packet_init */ buf_setpos(ses.readbuf, blocksize); buf_resize(ses.decryptreadbuf, ses.readbuf->len - macsize); buf_setlen(ses.decryptreadbuf, ses.decryptreadbuf->size); buf_setpos(ses.decryptreadbuf, blocksize); /* decrypt if encryption is set, memcpy otherwise */ if (ses.keys->recv_algo_crypt->cipherdesc == NULL) { /* copy it */ len = ses.readbuf->len - macsize - blocksize; memcpy(buf_getwriteptr(ses.decryptreadbuf, len), buf_getptr(ses.readbuf, len), len); } else { /* decrypt */ while (ses.readbuf->pos < ses.readbuf->len - macsize) { if (cbc_decrypt(buf_getptr(ses.readbuf, blocksize), buf_getwriteptr(ses.decryptreadbuf, blocksize), &ses.keys->recv_symmetric_struct) != CRYPT_OK) { dropbear_exit("error decrypting"); } buf_incrpos(ses.readbuf, blocksize); buf_incrwritepos(ses.decryptreadbuf, blocksize); } } /* check the hmac */ buf_setpos(ses.readbuf, ses.readbuf->len - macsize); if (checkmac(ses.readbuf, ses.decryptreadbuf) != DROPBEAR_SUCCESS) { dropbear_exit("Integrity error"); } /* readbuf no longer required */ buf_free(ses.readbuf); ses.readbuf = NULL; /* get padding length */ buf_setpos(ses.decryptreadbuf, PACKET_PADDING_OFF); padlen = buf_getbyte(ses.decryptreadbuf); /* payload length */ /* - 4 - 1 is for LEN and PADLEN values */ len = ses.decryptreadbuf->len - padlen - 4 - 1; if ((len > MAX_PAYLOAD_LEN) || (len < 1)) { dropbear_exit("bad packet size"); } buf_setpos(ses.decryptreadbuf, PACKET_PAYLOAD_OFF); #ifndef DISABLE_ZLIB if (ses.keys->recv_algo_comp == DROPBEAR_COMP_ZLIB) { /* decompress */ ses.payload = buf_decompress(ses.decryptreadbuf, len); } else #endif { /* copy payload */ ses.payload = buf_new(len); memcpy(ses.payload->data, buf_getptr(ses.decryptreadbuf, len), len); buf_incrlen(ses.payload, len); } buf_free(ses.decryptreadbuf); ses.decryptreadbuf = NULL; buf_setpos(ses.payload, 0); ses.recvseq++; TRACE(("leave decrypt_packet")); }
/* Function used to read the initial portion of a packet, and determine the * length. Only called during the first BLOCKSIZE of a packet. */ static void read_packet_init() { unsigned int maxlen; int len; unsigned char blocksize; unsigned char macsize; blocksize = ses.keys->recv_algo_crypt->blocksize; macsize = ses.keys->recv_algo_mac->hashsize; if (ses.readbuf == NULL) { /* start of a new packet */ ses.readbuf = buf_new(INIT_READBUF); assert(ses.decryptreadbuf == NULL); ses.decryptreadbuf = buf_new(blocksize); } maxlen = blocksize - ses.readbuf->pos; /* read the rest of the packet if possible */ len = read(ses.sock, buf_getwriteptr(ses.readbuf, maxlen), maxlen); if (len == 0) { dropbear_close("remote host closed connection"); } if (len < 0) { if (errno == EINTR) { TRACE(("leave read_packet_init: EINTR")); return; } dropbear_exit("error reading"); } buf_incrwritepos(ses.readbuf, len); if ((unsigned int)len != maxlen) { /* don't have enough bytes to determine length, get next time */ return; } /* now we have the first block, need to get packet length, so we decrypt * the first block (only need first 4 bytes) */ buf_setpos(ses.readbuf, 0); if (ses.keys->recv_algo_crypt->cipherdesc == NULL) { /* copy it */ memcpy(buf_getwriteptr(ses.decryptreadbuf, blocksize), buf_getptr(ses.readbuf, blocksize), blocksize); } else { /* decrypt it */ if (cbc_decrypt(buf_getptr(ses.readbuf, blocksize), buf_getwriteptr(ses.decryptreadbuf,blocksize), &ses.keys->recv_symmetric_struct) != CRYPT_OK) { dropbear_exit("error decrypting"); } } buf_setlen(ses.decryptreadbuf, blocksize); len = buf_getint(ses.decryptreadbuf) + 4 + macsize; buf_setpos(ses.readbuf, blocksize); /* check packet length */ if ((len > MAX_PACKET_LEN) || (len < MIN_PACKET_LEN + macsize) || ((len - macsize) % blocksize != 0)) { dropbear_exit("bad packet size"); } buf_resize(ses.readbuf, len); buf_setlen(ses.readbuf, len); }
static void * audio_capture_thr(void *args) { int error; PSAUDIORECTHRPARAMS params = (PSAUDIORECTHRPARAMS)args; pa_simple *pa_context = NULL; uint8_t active = 1; uint32_t buffer_size; SSAMPLEHEADER sample_header = {0}; HBUF sample; TIMING_MEASURE_AREA; buffer_size = HEADER_SIZE + params->sample_spec->channels * params->buffer_size * pa_sample_size(params->sample_spec); sample_header.number = 0; sample_header.buf_type = BUF_TYPE_INTERLEAVED; sample_header.sample_size = pa_sample_size(params->sample_spec); sample_header.samples = params->buffer_size; sample_header.channels = params->sample_spec->channels; sample_header.samplerate = params->sample_spec->rate; if ( !(pa_context = pa_simple_new(NULL, params->argv[0], PA_STREAM_RECORD, NULL, "record", params->sample_spec, NULL, NULL, &error)) ) { fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); goto audio_thr_finish; } printf("[source] audio thr\n"); while (active) { TIMING_START(); sample = buf_alloc(NULL); buf_resize(sample, buffer_size); sample->full_size = sample->size = sample->alloced_size; sample_zero_buffer(sample); memcpy(sample->buf, &sample_header, sizeof(SSAMPLEHEADER)); if (pa_simple_read(pa_context, sample->buf + HEADER_SIZE, sample->alloced_size - HEADER_SIZE, &error) < 0) { fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); goto audio_thr_finish; } pa_gettimeofday(&(sample_header.timestamp)); sample_header.number += 1; /* printf("[audio] read %p\n", sample); */ if ( (error = write(params->pipefd, &sample, sizeof(sample))) != sizeof(sample)) { if (error == -1) { handle_error("[audio] write()"); } buf_free(sample); perror("[audio] "); printf("[audio] uverrun. free buffer\n"); } active = params->active; TIMING_END(" audio"); } audio_thr_finish: pa_simple_free(pa_context); return NULL; }
static int os_exec_process_windows(const char *exe, ZigList<const char *> &args, int *return_code, Buf *out_stderr, Buf *out_stdout) { Buf command_line = BUF_INIT; buf_resize(&command_line, 0); buf_append_char(&command_line, '\"'); buf_append_str(&command_line, exe); buf_append_char(&command_line, '\"'); for (int arg_i = 0; arg_i < args.length; arg_i += 1) { buf_append_str(&command_line, " \""); const char *arg = args.at(arg_i); int arg_len = strlen(arg); for (int c_i = 0; c_i < arg_len; c_i += 1) { if (arg[c_i] == '\"') { zig_panic("TODO"); } buf_append_char(&command_line, arg[c_i]); } buf_append_char(&command_line, '\"'); } HANDLE g_hChildStd_IN_Rd = NULL; HANDLE g_hChildStd_IN_Wr = NULL; HANDLE g_hChildStd_OUT_Rd = NULL; HANDLE g_hChildStd_OUT_Wr = NULL; HANDLE g_hChildStd_ERR_Rd = NULL; HANDLE g_hChildStd_ERR_Wr = NULL; SECURITY_ATTRIBUTES saAttr; saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); saAttr.bInheritHandle = TRUE; saAttr.lpSecurityDescriptor = NULL; if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)) { zig_panic("StdoutRd CreatePipe"); } if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)) { zig_panic("Stdout SetHandleInformation"); } if (!CreatePipe(&g_hChildStd_ERR_Rd, &g_hChildStd_ERR_Wr, &saAttr, 0)) { zig_panic("stderr CreatePipe"); } if (!SetHandleInformation(g_hChildStd_ERR_Rd, HANDLE_FLAG_INHERIT, 0)) { zig_panic("stderr SetHandleInformation"); } if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)) { zig_panic("Stdin CreatePipe"); } if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0)) { zig_panic("Stdin SetHandleInformation"); } PROCESS_INFORMATION piProcInfo = {0}; STARTUPINFO siStartInfo = {0}; siStartInfo.cb = sizeof(STARTUPINFO); siStartInfo.hStdError = g_hChildStd_ERR_Wr; siStartInfo.hStdOutput = g_hChildStd_OUT_Wr; siStartInfo.hStdInput = g_hChildStd_IN_Rd; siStartInfo.dwFlags |= STARTF_USESTDHANDLES; BOOL success = CreateProcess(exe, buf_ptr(&command_line), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &siStartInfo, &piProcInfo); if (!success) { if (GetLastError() == ERROR_FILE_NOT_FOUND) { CloseHandle(piProcInfo.hProcess); CloseHandle(piProcInfo.hThread); return ErrorFileNotFound; } zig_panic("CreateProcess failed. exe: %s command_line: %s", exe, buf_ptr(&command_line)); } if (!CloseHandle(g_hChildStd_IN_Wr)) { zig_panic("stdinwr closehandle"); } CloseHandle(g_hChildStd_IN_Rd); CloseHandle(g_hChildStd_ERR_Wr); CloseHandle(g_hChildStd_OUT_Wr); static const int BUF_SIZE = 4 * 1024; { DWORD dwRead; char chBuf[BUF_SIZE]; buf_resize(out_stdout, 0); for (;;) { success = ReadFile( g_hChildStd_OUT_Rd, chBuf, BUF_SIZE, &dwRead, NULL); if (!success || dwRead == 0) break; buf_append_mem(out_stdout, chBuf, dwRead); } CloseHandle(g_hChildStd_OUT_Rd); } { DWORD dwRead; char chBuf[BUF_SIZE]; buf_resize(out_stderr, 0); for (;;) { success = ReadFile( g_hChildStd_ERR_Rd, chBuf, BUF_SIZE, &dwRead, NULL); if (!success || dwRead == 0) break; buf_append_mem(out_stderr, chBuf, dwRead); } CloseHandle(g_hChildStd_ERR_Rd); } WaitForSingleObject(piProcInfo.hProcess, INFINITE); DWORD exit_code; if (!GetExitCodeProcess(piProcInfo.hProcess, &exit_code)) { zig_panic("GetExitCodeProcess failed"); } *return_code = exit_code; CloseHandle(piProcInfo.hProcess); CloseHandle(piProcInfo.hThread); return 0; }
void rstack_close (lua_State *L) { RStack *rs = rstack(L); lua_assert(rs->cregnum == 0); lua_assert(rs->creg->top == rs->rbuf.head); buf_resize(L, rs, 0); }