static int _x509_from_request(char const * filename, Buffer * csr, Buffer * x509) { FILE * fp; int fd[2]; pid_t pid; if((fp = fopen(filename, "w")) == NULL) return error_set_code(1, "%s%s%s", filename, strerror(errno)); if(fwrite(buffer_get_data(csr), sizeof(char), buffer_get_size(csr), fp) != buffer_get_size(csr)) { fclose(fp); return error_set_code(1, "%s%s%s", filename, strerror(errno)); } if(fclose(fp) != 0) return error_set_code(1, "%s%s%s", filename, strerror(errno)); if(pipe(fd) != 0) return error_set_code(1, "%s%s", "pipe: ", strerror(errno)); if((pid = fork()) == -1) { close(fd[0]); close(fd[1]); return error_set_code(1, "%s%s", "fork: ", strerror(errno)); } if(pid == 0) { close(fd[0]); _request_child(filename, fd[1]); exit(0); } close(fd[1]); return _request_parent(x509, pid, fd[0]); }
static int git_receive_pack(buffer_t in_buf, buffer_t out_buf, buffer_t err_buf, void* payload) { struct git_receive_pack_data* data = (struct git_receive_pack_data*)payload; int result; if (buffer_get_size(out_buf) > 0 || buffer_get_size(err_buf) > 0) { // You still have pending write-data, wait until everything is written back // to the client return 1; } do { switch (data->current_process) { case p_receive_pack_reference_discovery: log_debug("reference discovery on %s", data->repository); result = reference_discovery(data->repository, process_receive_pack, out_buf, err_buf, libgit2_reference_discovery_cb); break; case p_receive_pack_update_request: log_debug("update request on %s", data->repository); result = update_request(data->repository, in_buf); break; case p_receive_pack_report_status: log_debug("report status on %s", data->repository); result = report_status(data->repository, out_buf); break; default: log_err("Unsupported process requested: %i", data->current_process); result = -1; break; } if (result == 0 || result == 3) { // Sucess, switch to next sub-process data->current_process++; } } while (result == 3); // result of 3 means, that the next-process should // be executed immediately. Don't wait for new // input-data. if (result == 0) { // Success if (data->current_process < p_receive_pack_finished) { // (Sub-process) finished, but there's at least another pending process. result = 1; } } else if (result == 2) { // Success, but don't execute another sub-process data->current_process = p_receive_pack_finished; result = 0; } return result; }
static int git_upload_pack(buffer_t in_buf, buffer_t out_buf, buffer_t err_buf, void* payload) { struct git_upload_pack_data* data = (struct git_upload_pack_data*)payload; int result; if (buffer_get_size(out_buf) > 0 || buffer_get_size(err_buf) > 0) { // You still have pending write-data, wait until everything is written back // to the client return 1; } switch (data->current_process) { case p_upload_pack_reference_discovery: log_debug("reference discovery on %s", data->repository); result = reference_discovery(data->repository, process_upload_pack, out_buf, err_buf, libgit2_reference_discovery_cb); break; case p_upload_pack_packfile_negotiation: log_debug("packfile negotiation on %s", data->repository); result = packfile_negotiation(data->repository, in_buf, out_buf, &data->result, libgit2_commit_log_cb, &data->packfile_negotiation); break; case p_upload_pack_packfile_transfer: log_debug("packfile transfer on %s", data->repository); result = packfile_transfer(data->repository, data->result.commits, libgit2_packfile_objects_cb, out_buf); break; default: log_err("Unsupported process requested: %i", data->current_process); result = -1; break; } if (result == 0) { // Success // Switch to the next process data->current_process++; if (data->current_process < p_upload_pack_finished) { // (Sub-process) finished, but there's at least another pending process. result = 1; } } else if (result == 2) { // Success, but don't execute another sub-process data->current_process = p_upload_pack_finished; result = 0; } return result; }
static void aqueue_grow(struct aqueue *aqueue) { unsigned int orig_area_size, count; i_assert(aqueue->full && aqueue->head == aqueue->tail); orig_area_size = aqueue->area_size; (void)array_append_space_i(aqueue->arr); aqueue->area_size = buffer_get_size(aqueue->arr->buffer) / aqueue->arr->element_size; i_assert(orig_area_size < aqueue->area_size); count = I_MIN(aqueue->area_size - orig_area_size, aqueue->head); array_copy(aqueue->arr, orig_area_size, aqueue->arr, 0, count); if (count < aqueue->area_size - orig_area_size) aqueue->head = orig_area_size + count; else { array_copy(aqueue->arr, 0, aqueue->arr, count, aqueue->head - count); aqueue->head -= count; } i_assert(aqueue->head != aqueue->tail); aqueue->full = FALSE; }
bool tcp_write_checksum(uint8_t * tcp_segment, buffer_t * ip_psh) { struct tcphdr * tcp_header = (struct tcphdr *) tcp_segment; size_t size_ip = buffer_get_size(ip_psh), size_tcp = tcp_get_header_size(tcp_segment) + 2, // hardcoded payload size size_psh = size_ip + size_tcp; uint8_t * psh; // TCP checksum computation requires the IPv* header if (!ip_psh) { errno = EINVAL; return false; } // Allocate the buffer which will contains the pseudo header if (!(psh = calloc(1, size_psh))) { return false; } // Put the excerpt of the IP header into the pseudo header memcpy(psh, buffer_get_data(ip_psh), size_ip); // Put the TCP header and its content into the pseudo header memcpy(psh + size_ip, tcp_segment, size_tcp); // Overrides the TCP checksum in psh with zeros memset(psh + size_ip + offsetof(struct tcphdr, check), 0, sizeof(uint16_t)); // Compute the checksum tcp_header->check = csum((const uint16_t *) psh, size_psh); free(psh); return true; }
static void test_fts_icu_translate_resize(void) { const char *translit_id = "Any-Hex"; const char *src_utf8 = "FOO"; buffer_t *dest, *src_utf16; UTransliterator *translit; const char *error; unsigned int i; test_begin("fts_icu_translate_resize resize"); src_utf16 = buffer_create_dynamic(pool_datastack_create(), 16); translit = get_translit(translit_id); for (i = 2; i <= 20; i++) { buffer_set_used_size(src_utf16, 0); fts_icu_utf8_to_utf16(src_utf16, src_utf8); dest = buffer_create_dynamic(pool_datastack_create(), i); test_assert(buffer_get_size(dest) == i); test_assert(fts_icu_translate(dest, src_utf16->data, src_utf16->used/sizeof(UChar), translit, &error) == 0); } utrans_close(translit); test_end(); }
/* register */ int32_t Directory_register(String const * title, Buffer * csr, Buffer * x509) { static const char cacert_csr[] = "/cacert.csr"; static const char begin[] = "-----BEGIN CERTIFICATE REQUEST-----\n"; int ret; struct stat st; size_t len; char * filename; /* validate title */ if(*title == '\0' || *title == '.' || strchr(title, '/') != NULL) return 1; /* validate request */ if((len = buffer_get_size(csr)) < sizeof(begin)) return error_set_print(PACKAGE, 1, "%s", "Request too short"); if(memcmp(buffer_get_data(csr), begin, sizeof(begin) - 1) != 0) return error_set_print(PACKAGE, 1, "%s", "Incorrect request"); /* verify the title is unique */ if(lstat(title, &st) != -1 || errno != ENOENT) return error_set_print(PACKAGE, 1, "%s%s%s", title, ": ", strerror(EEXIST)); /* request certificate */ if(mkdir(title, 0777) != 0) return error_set_print(PACKAGE, 1, "%s%s%s", title, ": ", strerror(errno)); if((filename = malloc(strlen(title) + sizeof(cacert_csr))) == NULL) return error_set_print(PACKAGE, 1, "%s", strerror(errno)); sprintf(filename, "%s%s", title, cacert_csr); ret = _x509_from_request(filename, csr, x509); free(filename); return ret; }
static void test_fts_icu_lcase_resize(void) { const char *src = "a\xC3\x84"; string_t *dest; unsigned int i; test_begin("fts_icu_lcase resize"); for (i = 2; i <= 4; i++) { dest = t_str_new(i); test_assert(buffer_get_size(dest) == i); fts_icu_lcase(dest, src); test_assert(strcmp(str_c(dest), "a\xC3\xA4") == 0); test_assert(buffer_get_size(dest) == 4); } test_end(); }
bool unabto_buffer_test(void) { char * raw_test_string = "asjdhc#21?(!?(92814skzjbcasa"; uint8_t data[7 + 2 + strlen(raw_test_string)]; unabto_buffer buf, raw_string_buf; buffer_write_t w_buf; buffer_read_t r_buf; uint32_t t32; uint16_t t16; uint8_t t8; uint8_t raw_string_data[strlen(raw_test_string) + 1]; buffer_init(&raw_string_buf, (uint8_t*)raw_test_string, strlen(raw_test_string)); buffer_init(&buf, data, sizeof(data)); buffer_write_init(&w_buf, &buf); if (! (buffer_write_uint32(&w_buf, 0x12345678) && buffer_write_uint16(&w_buf, 0x1234) && buffer_write_uint8(&w_buf, 0x12) && buffer_write_raw(&w_buf, &raw_string_buf) ) ) { NABTO_LOG_ERROR(("Buffer write test failed")); return false; } buffer_read_init(&r_buf, &buf); memset(raw_string_data, 0, sizeof(raw_string_data) + 1); buffer_init(&raw_string_buf, raw_string_data, sizeof(raw_string_data)); bool t = false; if (! ( buffer_read_uint32(&r_buf, &t32) && t32 == 0x12345678 && buffer_read_uint16(&r_buf, &t16) && t16 == 0x1234 && buffer_read_uint8(&r_buf, &t8) && t8 == 0x12 && (t = buffer_read_raw(&r_buf, &raw_string_buf)) && buffer_get_size(&raw_string_buf) == strlen(raw_test_string) && 0 == strncmp((char*)buffer_get_data(&raw_string_buf), raw_test_string, strlen(raw_test_string)) )) { NABTO_LOG_ERROR(("Failed read test failed")); return false; } if (buffer_read_uint32(&r_buf, &t32) || buffer_read_uint16(&r_buf, &t16) || buffer_read_uint8(&r_buf, &t8) || buffer_read_raw(&w_buf, &raw_string_buf) || buffer_write_uint32(&w_buf, 0x12345678) || buffer_write_uint16(&w_buf, 0x1234) || buffer_write_uint8(&w_buf, 0x12) || buffer_write_raw(&w_buf, &raw_string_buf) ) { NABTO_LOG_ERROR(("Some function should have returned false but returned true")); return false; } return true; }
static void test_fts_icu_utf8_to_utf16_ascii_resize(void) { buffer_t *dest = buffer_create_dynamic(pool_datastack_create(), 5); test_begin("fts_icu_utf8_to_utf16 ascii resize"); /* dynamic buffers reserve +1 for str_c()'s NUL, so 5 -> 4 */ test_assert(buffer_get_size(dest) == 5); fts_icu_utf8_to_utf16(dest, "12"); test_assert(dest->used == 4); test_assert(buffer_get_size(dest) == 5); fts_icu_utf8_to_utf16(dest, "123"); test_assert(dest->used == 6); test_assert(buffer_get_size(dest) == 8); fts_icu_utf8_to_utf16(dest, "12345"); test_assert(dest->used == 10); test_end(); }
struct aqueue *aqueue_init(struct array *array) { struct aqueue *aqueue; aqueue = i_new(struct aqueue, 1); aqueue->arr = array; aqueue->area_size = buffer_get_size(aqueue->arr->buffer) / aqueue->arr->element_size; i_assert(aqueue->area_size > 0); return aqueue; }
static void str_add_nul(string_t *str) { const unsigned char *data = str_data(str); size_t len = str_len(str); size_t alloc = buffer_get_size(str); if (len == alloc || data[len] != '\0') { buffer_write(str, len, "", 1); /* remove the \0 - we don't want to keep it */ buffer_set_used_size(str, len); } }
void str_vprintfa(string_t *str, const char *fmt, va_list args) { #define SNPRINTF_INITIAL_EXTRA_SIZE 128 va_list args2; char *tmp; unsigned int init_size; size_t pos = str->used; int ret, ret2; VA_COPY(args2, args); /* the format string is modified only if %m exists in it. it happens only in error conditions, so don't try to t_push() here since it'll just slow down the normal code path. */ fmt = printf_format_fix_get_len(fmt, &init_size); init_size += SNPRINTF_INITIAL_EXTRA_SIZE; /* @UNSAFE */ if (pos+init_size > buffer_get_size(str) && pos < buffer_get_size(str)) { /* avoid growing buffer larger if possible. this is also required if buffer isn't dynamically growing. */ init_size = buffer_get_size(str)-pos; } tmp = buffer_get_space_unsafe(str, pos, init_size); ret = vsnprintf(tmp, init_size, fmt, args); i_assert(ret >= 0); if ((unsigned int)ret >= init_size) { /* didn't fit with the first guess. now we know the size, so try again. */ tmp = buffer_get_space_unsafe(str, pos, ret + 1); ret2 = vsnprintf(tmp, ret + 1, fmt, args2); i_assert(ret2 == ret); } /* drop the unused data, including terminating NUL */ buffer_set_used_size(str, pos + ret); }
/* VPN_send */ int32_t VPN_send(int32_t fd, Buffer * buffer, uint32_t size, uint32_t flags) { if(_client_check(NULL, fd) == NULL) return -1; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(%d, buf, %u, %u)\n", __func__, fd, size, flags); #endif if(buffer_get_size(buffer) < size) return -error_set_code(1, "%s", strerror(EINVAL)); /* FIXME implement flags */ return send(fd, buffer_get_data(buffer), size, 0); }
END_TEST START_TEST(compress_success) { const unsigned char expected_dest[] = { 0x78, 0x9c, 0x33, 0x34, 0x32, 0x36, 0x31, 0x35, 0x33, 0xb7, 0xb0, 0x34, 0x00, 0x00, 0x0b, 0x2c, 0x02, 0x0e }; fail_unless(buffer_append(source, "1234567890", 10) == 0); fail_unless(buffer_compress(source, dest) == 0); fail_unless(buffer_get_size(dest) == 18); fail_unless(memcmp(buffer_get_data(dest), expected_dest, 8) == 0); }
static size_t o_stream_ssl_buffer(struct ssl_ostream *sstream, const struct const_iovec *iov, unsigned int iov_count, size_t bytes_sent) { size_t avail, skip_left, size; unsigned int i; if (sstream->buffer == NULL) sstream->buffer = buffer_create_dynamic(default_pool, 4096); skip_left = bytes_sent; for (i = 0; i < iov_count; i++) { if (skip_left < iov[i].iov_len) break; skip_left -= iov[i].iov_len; } if (sstream->ostream.max_buffer_size == 0) { /* we're requeted to use whatever space is available in the buffer */ avail = buffer_get_size(sstream->buffer) - sstream->buffer->used; } else { avail = sstream->ostream.max_buffer_size > sstream->buffer->used ? sstream->ostream.max_buffer_size - sstream->buffer->used : 0; } if (i < iov_count && skip_left > 0) { size = I_MIN(iov[i].iov_len - skip_left, avail); buffer_append(sstream->buffer, CONST_PTR_OFFSET(iov[i].iov_base, skip_left), size); bytes_sent += size; avail -= size; if (size != iov[i].iov_len) i = iov_count; } if (avail > 0) o_stream_set_flush_pending(sstream->ssl_io->plain_output, TRUE); for (; i < iov_count; i++) { size = I_MIN(iov[i].iov_len, avail); buffer_append(sstream->buffer, iov[i].iov_base, size); bytes_sent += size; avail -= size; if (size != iov[i].iov_len) break; } sstream->ostream.ostream.offset += bytes_sent; return bytes_sent; }
END_TEST START_TEST(no_objects) { unsigned char expected_out[] = { 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x02, 0x9d, 0x08, 0x82, 0x3b, 0xd8, 0xa8, 0xea, 0xb5, 0x10, 0xad, 0x6a, 0xc7, 0x5c, 0x82, 0x3c, 0xfd, 0x3e, 0xd3, 0x1e }; fail_unless(packfile_transfer("xxx", commits, empty_stub, out) == 0); fail_unless(buffer_get_size(out) == 32); fail_unless(memcmp(buffer_get_data(out), expected_out, 32) == 0); }
static void test_fts_icu_utf8_to_utf16_32bit_resize(void) { buffer_t *dest; unsigned int i; test_begin("fts_icu_utf8_to_utf16 32bit resize"); for (i = 2; i <= 5; i++) { dest = buffer_create_dynamic(pool_datastack_create(), i); test_assert(buffer_get_size(dest) == i); fts_icu_utf8_to_utf16(dest, "\xF0\x90\x90\x80"); /* 0x10400 */ test_assert(dest->used == 4); } test_end(); }
END_TEST START_TEST(one_object) { unsigned char expected_out[] = { 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x13, 0x78, 0x9c, 0x33, 0x34, 0x32, 0x06, 0x00, 0x01, 0x2d, 0x00, 0x97, 0x07, 0x77, 0x8d, 0x37, 0xa2, 0x12, 0x33, 0xad, 0xe4, 0x78, 0xfb, 0x0e, 0xa1, 0x9e, 0x52, 0x35, 0x10, 0xc3, 0x35, 0x2b }; fail_unless(packfile_transfer("xxx", commits, one_stub, out) == 0); fail_unless(buffer_get_size(out) == 44); fail_unless(memcmp(buffer_get_data(out), expected_out, 44) == 0); }
END_TEST START_TEST(two_objects) { unsigned char expected_out[] = { 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x13, 0x78, 0x9c, 0x33, 0x34, 0x32, 0x06, 0x00, 0x01, 0x2d, 0x00, 0x97, 0x23, 0x78, 0x9c, 0x33, 0x31, 0x35, 0x03, 0x00, 0x01, 0x3f, 0x00, 0xa0, 0x9b, 0x5b, 0x1b, 0xe5, 0xab, 0x9f, 0x00, 0x63, 0x3b, 0xd5, 0xbe, 0x8b, 0x19, 0xa6, 0x06, 0x28, 0x07, 0xfd, 0x58, 0x0b }; fail_unless(packfile_transfer("xxx", commits, two_stub, out) == 0); fail_unless(buffer_get_size(out) == 56); fail_unless(memcmp(buffer_get_data(out), expected_out, 56) == 0); }
static void test_fts_icu_utf16_to_utf8_resize(void) { string_t *dest; const UChar src = UNICODE_REPLACEMENT_CHAR; unsigned int i; test_begin("fts_icu_utf16_to_utf8 resize"); for (i = 2; i <= 6; i++) { dest = t_str_new(i); test_assert(buffer_get_size(dest) == i); fts_icu_utf16_to_utf8(dest, &src, 1); test_assert(dest->used == 3); test_assert(strcmp(str_c(dest), UNICODE_REPLACEMENT_CHAR_UTF8) == 0); } test_end(); }
// io commands static void cmd_io(void) { u08 cmd, res; u08 exit = 0; while((cmd = get_char()) != 0) { switch(cmd) { case 'd': file_dir(); break; case 's': { if(buffer_get_size()>0) { res = file_save_buffer(1); set_result(res); } else { set_result(0); } } break; case 'f': // find next disk name dir { u32 num = 0; res = file_find_disk_dir(&num); uart_send_hex_dword_crlf(num); set_result(res); } break; case 'm': // make disk dir { res = file_make_disk_dir(parse_hex_byte(0)); set_result(res); } break; case 't': // test file file_test(); break; default: set_result(ERROR_SYNTAX); case '.': exit = 1; break; } if(exit) break; } }
/* ssl_socket_queue */ static int _ssl_socket_queue(SSLSocket * sslsocket, Buffer * buffer) { uint32_t len; char * p; Variable * v; Buffer * b = NULL; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(%d)\n", __func__, sslsocket->fd); #endif /* serialize the buffer */ v = variable_new(VT_BUFFER, buffer); b = buffer_new(0, NULL); if(v == NULL || b == NULL || variable_serialize(v, b, 0) != 0) { if(v != NULL) variable_delete(v); if(b != NULL) buffer_delete(b); return -1; } variable_delete(v); len = buffer_get_size(b); /* FIXME queue the serialized buffer directly as a message instead */ if((p = realloc(sslsocket->bufout, sslsocket->bufout_cnt + len)) == NULL) { buffer_delete(b); return -1; } sslsocket->bufout = p; memcpy(&p[sslsocket->bufout_cnt], buffer_get_data(b), len); /* register the callback if necessary */ if(sslsocket->bufout_cnt == 0) event_register_io_write(sslsocket->transport->helper->event, sslsocket->fd, (EventIOFunc)_ssl_socket_callback_write, sslsocket); sslsocket->bufout_cnt += len; buffer_delete(b); #ifdef DEBUG fprintf(stderr, "DEBUG: %s(%d) => %d\n", __func__, sslsocket->fd, 0); #endif return 0; }
/* video_on_refresh */ static gboolean _video_on_refresh(gpointer data) { VideoPhonePlugin * video = data; /* FIXME no longer block on read() */ if(read(video->fd, buffer_get_data(video->buffer), buffer_get_size(video->buffer)) <= 0) { /* this error can be ignored */ if(errno == EAGAIN) return TRUE; close(video->fd); video->fd = -1; return FALSE; } /* FIXME implement the rest */ return TRUE; }
AppMessage * appmessage_new_deserialize(Buffer * buffer) { AppMessage * message; char const * data = buffer_get_data(buffer); size_t size = buffer_get_size(buffer); size_t pos = 0; size_t s; Variable * v; uint8_t u8; #ifdef DEBUG fprintf(stderr, "DEBUG: %s()\n", __func__); #endif if((message = object_new(sizeof(*message))) == NULL) return NULL; s = size; if((v = variable_new_deserialize_type(VT_UINT8, &s, &data[pos])) == NULL) { object_delete(message); return NULL; } pos += s; size -= s; /* XXX may fail */ variable_get_as(v, VT_UINT8, &u8); variable_delete(v); switch((message->type = u8)) { case AMT_ACKNOWLEDGEMENT: return _new_deserialize_acknowledgement(message, data, size, pos); case AMT_CALL: return _new_deserialize_call(message, data, size, pos); default: error_set_code(1, "%s%u", "Unknown message type ", u8); /* XXX should not happen */ object_delete(message); return NULL; } }
/* variable_serialize */ int variable_serialize(Variable * variable, Buffer * buffer, int type) { size_t size = 0; size_t offset; void * p; uint8_t u8; int16_t i16; uint16_t u16; int32_t i32; uint32_t u32; char buf[16]; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(%u)\n", __func__, variable->type); #endif switch(variable->type) { case VT_NULL: p = NULL; break; case VT_BOOL: case VT_INT8: case VT_UINT8: size = sizeof(variable->u.int8); p = &variable->u.int8; break; case VT_INT16: size = sizeof(i16); i16 = htons(variable->u.int16); p = &i16; break; case VT_UINT16: size = sizeof(u16); u16 = htons(variable->u.uint16); p = &u16; break; case VT_INT32: size = sizeof(i32); i32 = htonl(variable->u.int32); p = &i32; break; case VT_UINT32: size = sizeof(u32); u32 = htonl(variable->u.uint32); p = &u32; break; case VT_INT64: case VT_UINT64: /* FIXME convert to network endian */ size = sizeof(variable->u.int64); p = &variable->u.int64; break; case VT_FLOAT: size = snprintf(buf, sizeof(buf), "%.e", variable->u.f); p = buf; break; case VT_DOUBLE: size = snprintf(buf, sizeof(buf), "%.e", variable->u.d); p = buf; break; case VT_BUFFER: size = buffer_get_size(variable->u.buffer); u32 = buffer_get_size(variable->u.buffer); u32 = htonl(u32); p = buffer_get_data(variable->u.buffer); break; case VT_STRING: size = string_get_length(variable->u.string) + 1; p = variable->u.string; break; } #ifdef DEBUG fprintf(stderr, "DEBUG: %s() %lu\n", __func__, size); #endif if(size == 0 && variable->type != VT_NULL) return -error_set_code(1, "Unable to serialize type %u", type); if(type) { /* prefix with the type */ u8 = variable->type; if(buffer_set(buffer, sizeof(u8), (char *)&u8) != 0) return -1; offset = sizeof(u8); if(variable->type == VT_BUFFER) { if(buffer_set_data(buffer, offset, (char *)&u32, sizeof(u32)) != 0) return -1; offset += sizeof(u32); } return buffer_set_data(buffer, offset, p, size); } if(variable->type == VT_BUFFER) { if(buffer_set(buffer, sizeof(u32), (char *)&u32) != 0) return -1; return buffer_set_data(buffer, sizeof(u32), p, size); } return buffer_set(buffer, size, p); }
END_TEST START_TEST(failed_callback) { fail_unless(packfile_transfer("xxx", commits, failed_stub, out) == -1); fail_unless(buffer_get_size(out) == 0); }
/***************************************************************************** Prototype : vid_enc_thr_run Description : add osd and do jpeg encode Input : VidEncThrEnv *envp ImgMsg *msg Output : None Return Value : static Calls : Called By : History : 1.Date : 2012/3/9 Author : Sun Modification : Created function *****************************************************************************/ static Int32 vid_enc_thr_run(VidEncThrEnv *envp, ImgMsg *msg) { Int32 err; /* Add osd first & ignore error */ err = add_osd(envp->hOsd, msg, &envp->osdCfg.vidOsd); /* Init buffer and args for encode */ AlgBuf inBuf, outBuf; BufHandle hBufIn, hBufOut; H264EncInArgs inArgs; H264EncOutArgs outArgs; Int32 dispFlags = 0; hBufIn = msg->hBuf; inBuf.buf = buffer_get_user_addr(msg->hBuf); inBuf.bufSize = buffer_get_size(msg->hBuf); assert(inBuf.buf && inBuf.bufSize); dispFlags = FD_FLAG_NOT_FREE_BUF; hBufOut = envp->hBufEnc; outBuf.buf = buffer_get_user_addr(hBufOut); outBuf.bufSize = buffer_get_size(hBufOut); inArgs.size = sizeof(inArgs); inArgs.inputID = msg->index; inArgs.timeStamp = 0; outArgs.size = sizeof(outArgs); outArgs.bytesGenerated = 0; /* do h264 encode */ err = h264_enc_process(envp->hH264Enc, &inBuf, &inArgs, &outBuf, &outArgs); if(err) { ERR("h264 enc err: %s", str_err(err)); goto err_quit; } /* free input buffer */ static int cnt = 0; DBG("<%d>h264 free raw buf",++cnt); buf_pool_free(hBufIn); hBufIn = NULL; /* modify frame info */ msg->dimension.colorSpace = FMT_H264; msg->dimension.size = outArgs.bytesGenerated; msg->frameType = outArgs.frameType; msg->hBuf = hBufOut; buffer_set_bytes_used(hBufOut, outArgs.bytesGenerated); //DBG("<%d> h264 enc run ok", msg->index); /* dispatch h.264 data */ err = frame_disp_run(envp->hDispatch, envp->hMsg, msg, NULL, dispFlags); if(err) { ERR("dispatch h264 data failed"); goto err_quit; } return E_NO; err_quit: if(hBufIn) buf_pool_free(hBufIn); return err; }
static int _serialize_append(Buffer * buffer, Buffer * b) { return buffer_set_data(buffer, buffer_get_size(buffer), buffer_get_data(b), buffer_get_size(b)); }
static ssize_t data_buffer_t_len(data_t *data, fastcall_len *fargs){ // {{{ fargs->length = buffer_get_size( (buffer_t *)data->ptr ); return 0; } // }}}