blob: 03071ef92c699475b09a416f362cbb74c5e39439 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller8a9fd522016-03-25 17:09:29 -07003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <grpc/byte_buffer.h>
35#include <grpc/support/alloc.h>
36#include <grpc/support/log.h>
37
David Garcia Quintas59f905d2015-06-08 16:31:19 -070038grpc_byte_buffer *grpc_raw_byte_buffer_create(gpr_slice *slices,
39 size_t nslices) {
40 return grpc_raw_compressed_byte_buffer_create(slices, nslices,
41 GRPC_COMPRESS_NONE);
David Garcia Quintas25d02d52015-06-04 17:40:54 -070042}
43
David Garcia Quintas59f905d2015-06-08 16:31:19 -070044grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create(
45 gpr_slice *slices, size_t nslices, grpc_compression_algorithm compression) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046 size_t i;
47 grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer));
David Garcia Quintas59f905d2015-06-08 16:31:19 -070048 bb->type = GRPC_BB_RAW;
49 bb->data.raw.compression = compression;
50 gpr_slice_buffer_init(&bb->data.raw.slice_buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051 for (i = 0; i < nslices; i++) {
52 gpr_slice_ref(slices[i]);
David Garcia Quintas59f905d2015-06-08 16:31:19 -070053 gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055 return bb;
56}
57
David Garcia Quintas3df5c442015-06-17 18:31:58 -070058grpc_byte_buffer *grpc_raw_byte_buffer_from_reader(
59 grpc_byte_buffer_reader *reader) {
60 grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer));
61 gpr_slice slice;
62 bb->type = GRPC_BB_RAW;
63 bb->data.raw.compression = GRPC_COMPRESS_NONE;
64 gpr_slice_buffer_init(&bb->data.raw.slice_buffer);
65
66 while (grpc_byte_buffer_reader_next(reader, &slice)) {
67 gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slice);
68 }
69 return bb;
70}
71
Craig Tiller80fa15c2015-01-13 16:10:49 -080072grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) {
73 switch (bb->type) {
David Garcia Quintas59f905d2015-06-08 16:31:19 -070074 case GRPC_BB_RAW:
75 return grpc_raw_byte_buffer_create(bb->data.raw.slice_buffer.slices,
76 bb->data.raw.slice_buffer.count);
Craig Tiller80fa15c2015-01-13 16:10:49 -080077 }
yang-gb063c872015-10-07 11:40:13 -070078 GPR_UNREACHABLE_CODE(return NULL);
Craig Tiller80fa15c2015-01-13 16:10:49 -080079}
80
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) {
Craig Tillerfee91b42015-02-05 16:03:13 -080082 if (!bb) return;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083 switch (bb->type) {
David Garcia Quintas59f905d2015-06-08 16:31:19 -070084 case GRPC_BB_RAW:
85 gpr_slice_buffer_destroy(&bb->data.raw.slice_buffer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086 break;
87 }
88 free(bb);
89}
90
91size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) {
92 switch (bb->type) {
David Garcia Quintas59f905d2015-06-08 16:31:19 -070093 case GRPC_BB_RAW:
94 return bb->data.raw.slice_buffer.length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095 }
yang-gb063c872015-10-07 11:40:13 -070096 GPR_UNREACHABLE_CODE(return 0);
Craig Tiller190d3602015-02-18 09:23:38 -080097}