struct ostream *openssl_o_stream_create_ssl(struct ssl_iostream *ssl_io) { struct ssl_ostream *sstream; ssl_io->refcount++; sstream = i_new(struct ssl_ostream, 1); sstream->ssl_io = ssl_io; sstream->ostream.max_buffer_size = ssl_io->plain_output->real_stream->max_buffer_size; sstream->ostream.iostream.close = o_stream_ssl_close; sstream->ostream.iostream.destroy = o_stream_ssl_destroy; sstream->ostream.sendv = o_stream_ssl_sendv; sstream->ostream.flush = o_stream_ssl_flush; sstream->ostream.switch_ioloop = o_stream_ssl_switch_ioloop; sstream->ostream.flush_pending = o_stream_ssl_flush_pending; sstream->ostream.iostream.set_max_buffer_size = o_stream_ssl_set_max_buffer_size; sstream->ostream.callback = ssl_io->plain_output->real_stream->callback; sstream->ostream.context = ssl_io->plain_output->real_stream->context; o_stream_set_flush_callback(ssl_io->plain_output, plain_flush_callback, sstream); return o_stream_create(&sstream->ostream, NULL, o_stream_get_fd(ssl_io->plain_output)); }
struct ostream *o_stream_create_lzma(struct ostream *output, int level) { struct lzma_ostream *zstream; lzma_ret ret; i_assert(level >= 1 && level <= 9); zstream = i_new(struct lzma_ostream, 1); zstream->ostream.sendv = o_stream_lzma_sendv; zstream->ostream.flush = o_stream_lzma_flush; zstream->ostream.iostream.close = o_stream_lzma_close; ret = lzma_easy_encoder(&zstream->strm, level, LZMA_CHECK_CRC64); switch (ret) { case LZMA_OK: break; case LZMA_MEM_ERROR: i_fatal_status(FATAL_OUTOFMEM, "lzma: Out of memory"); case LZMA_OPTIONS_ERROR: i_fatal("lzma: Invalid level"); default: i_fatal("lzma_easy_encoder() failed with %d", ret); } zstream->strm.next_out = zstream->outbuf; zstream->strm.avail_out = sizeof(zstream->outbuf); return o_stream_create(&zstream->ostream, output, o_stream_get_fd(output)); }
void connection_init_from_streams(struct connection_list *list, struct connection *conn, const char *name, struct istream *input, struct ostream *output) { i_assert(name != NULL); conn->list = list; conn->name = i_strdup(name); conn->fd_in = i_stream_get_fd(input); conn->fd_out = o_stream_get_fd(output); i_assert(conn->fd_in >= 0); i_assert(conn->fd_out >= 0); i_assert(conn->io == NULL); i_assert(conn->input == NULL); i_assert(conn->output == NULL); i_assert(conn->to == NULL); conn->input = input; i_stream_ref(conn->input); i_stream_set_name(conn->input, conn->name); conn->output = output; o_stream_ref(conn->output); o_stream_set_no_error_handling(conn->output, TRUE); o_stream_set_name(conn->output, conn->name); conn->io = io_add_istream(conn->input, *list->v.input, conn); DLLIST_PREPEND(&list->connections, conn); list->connections_count++; if (list->v.client_connected != NULL) list->v.client_connected(conn, TRUE); }
struct ostream *o_stream_create_bz2(struct ostream *output, int level) { struct bzlib_ostream *zstream; int ret; i_assert(level >= 1 && level <= 9); zstream = i_new(struct bzlib_ostream, 1); zstream->ostream.sendv = o_stream_bzlib_sendv; zstream->ostream.flush = o_stream_bzlib_flush; zstream->ostream.iostream.close = o_stream_bzlib_close; ret = BZ2_bzCompressInit(&zstream->zs, level, 0, 0); switch (ret) { case BZ_OK: break; case BZ_MEM_ERROR: i_fatal_status(FATAL_OUTOFMEM, "bzlib: Out of memory"); case BZ_CONFIG_ERROR: i_fatal("Wrong bzlib library version (broken compilation)"); case BZ_PARAM_ERROR: i_fatal("bzlib: Invalid parameters"); default: i_fatal("BZ2_bzCompressInit() failed with %d", ret); } zstream->zs.next_out = zstream->outbuf; zstream->zs.avail_out = sizeof(zstream->outbuf); return o_stream_create(&zstream->ostream, output, o_stream_get_fd(output)); }
static void test_iostream_temp_create_sized_disk(void) { struct ostream *output; test_begin("iostream_temp_create_sized() disk"); output = iostream_temp_create_sized(".", 0, "test", 4); test_assert(o_stream_send(output, "123", 3) == 3); test_assert(output->offset == 3); test_assert(o_stream_send(output, "4", 1) == 1); test_assert(output->offset == 4); test_assert(o_stream_get_fd(output) == -1); test_assert(o_stream_send(output, "5", 1) == 1); test_assert(output->offset == 5); test_assert(o_stream_get_fd(output) != -1); o_stream_destroy(&output); test_end(); }
static void test_iostream_temp_create_sized_memory(void) { struct ostream *output; test_begin("iostream_temp_create_sized() memory"); output = iostream_temp_create_sized(".intentional-nonexistent-error/", 0, "test", 4); test_assert(o_stream_send(output, "123", 3) == 3); test_assert(output->offset == 3); test_assert(o_stream_send(output, "4", 1) == 1); test_assert(output->offset == 4); test_assert(o_stream_get_fd(output) == -1); /* now we'll try to switch to writing to a file, but it'll fail */ test_expect_error_string("safe_mkstemp"); test_assert(o_stream_send(output, "5", 1) == 1); test_expect_no_more_errors(); test_assert(o_stream_get_fd(output) == -1); o_stream_destroy(&output); test_end(); }
struct ostream * o_stream_create_failure_at_flush(struct ostream *output, const char *error_string) { struct failure_at_ostream *fstream; fstream = i_new(struct failure_at_ostream, 1); fstream->ostream.flush = o_stream_failure_at_flush; fstream->ostream.iostream.destroy = o_stream_failure_at_destroy; fstream->error_string = i_strdup(error_string); fstream->failed = TRUE; return o_stream_create(&fstream->ostream, output, o_stream_get_fd(output)); }
struct ostream * o_stream_create_hash(struct ostream *output, const struct hash_method *method, void *hash_context) { struct hash_ostream *hstream; hstream = i_new(struct hash_ostream, 1); hstream->ostream.sendv = o_stream_hash_sendv; hstream->method = method; hstream->hash_context = hash_context; return o_stream_create(&hstream->ostream, output, o_stream_get_fd(output)); }
struct ostream * o_stream_create_cmp(struct ostream *output, struct istream *input) { struct cmp_ostream *cstream; cstream = i_new(struct cmp_ostream, 1); cstream->ostream.sendv = o_stream_cmp_sendv; cstream->ostream.iostream.close = o_stream_cmp_close; cstream->input = input; cstream->equals = TRUE; i_stream_ref(input); return o_stream_create(&cstream->ostream, output, o_stream_get_fd(output)); }
static void test_iostream_temp_create_write_error(void) { struct ostream *output; test_begin("iostream_temp_create_sized() write error"); output = iostream_temp_create_sized(".", 0, "test", 1); test_assert(o_stream_send(output, "123", 3) == 3); test_assert(o_stream_get_fd(output) != -1); test_assert(output->offset == 3); test_assert(o_stream_temp_move_to_memory(output) == 0); test_assert(o_stream_get_fd(output) == -1); test_assert(o_stream_send(output, "45", 2) == 2); test_assert(output->offset == 5); const unsigned char *data; size_t size; struct istream *input = iostream_temp_finish(&output, 128); test_assert(i_stream_read_bytes(input, &data, &size, 5) == 1 && memcmp(data, "12345", 5) == 0); i_stream_destroy(&input); test_end(); }
struct ostream * o_stream_create_failure_at(struct ostream *output, uoff_t failure_offset, const char *error_string) { struct failure_at_ostream *fstream; fstream = i_new(struct failure_at_ostream, 1); fstream->ostream.sendv = o_stream_failure_at_sendv; fstream->ostream.flush = o_stream_failure_at_flush; fstream->ostream.iostream.destroy = o_stream_failure_at_destroy; fstream->failure_offset = failure_offset; fstream->error_string = i_strdup(error_string); return o_stream_create(&fstream->ostream, output, o_stream_get_fd(output)); }
struct ostream * o_stream_create_escaped(struct ostream *output, ostream_escaped_escape_formatter_t format) { struct escaped_ostream *estream; estream = i_new(struct escaped_ostream, 1); estream->ostream.sendv = o_stream_escaped_sendv; estream->ostream.flush = o_stream_escaped_flush; estream->ostream.max_buffer_size = o_stream_get_max_buffer_size(output); estream->ostream.iostream.destroy = o_stream_escaped_destroy; estream->buf = str_new(default_pool, 512); estream->format = format; estream->flushed = FALSE; return o_stream_create(&estream->ostream, output, o_stream_get_fd(output)); }
struct ostream * http_transfer_chunked_ostream_create(struct ostream *output) { struct http_transfer_chunked_ostream *tcstream; size_t max_size; tcstream = i_new(struct http_transfer_chunked_ostream, 1); tcstream->ostream.sendv = http_transfer_chunked_ostream_sendv; tcstream->ostream.iostream.close = http_transfer_chunked_ostream_close; if (output->real_stream->max_buffer_size > 0) max_size = output->real_stream->max_buffer_size; else max_size = DEFAULT_MAX_BUFFER_SIZE; tcstream->ostream.max_buffer_size = _max_chunk_size(max_size); return o_stream_create(&tcstream->ostream, output, o_stream_get_fd(output)); }