Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 8a9fd52 | 2016-03-25 17:09:29 -0700 | [diff] [blame^] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 38 | grpc_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 Quintas | 25d02d5 | 2015-06-04 17:40:54 -0700 | [diff] [blame] | 42 | } |
| 43 | |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 44 | grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create( |
| 45 | gpr_slice *slices, size_t nslices, grpc_compression_algorithm compression) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | size_t i; |
| 47 | grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer)); |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 48 | bb->type = GRPC_BB_RAW; |
| 49 | bb->data.raw.compression = compression; |
| 50 | gpr_slice_buffer_init(&bb->data.raw.slice_buffer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | for (i = 0; i < nslices; i++) { |
| 52 | gpr_slice_ref(slices[i]); |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 53 | gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | return bb; |
| 56 | } |
| 57 | |
David Garcia Quintas | 3df5c44 | 2015-06-17 18:31:58 -0700 | [diff] [blame] | 58 | grpc_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 Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame] | 72 | grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) { |
| 73 | switch (bb->type) { |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 74 | 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 Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame] | 77 | } |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 78 | GPR_UNREACHABLE_CODE(return NULL); |
Craig Tiller | 80fa15c | 2015-01-13 16:10:49 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) { |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 82 | if (!bb) return; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | switch (bb->type) { |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 84 | case GRPC_BB_RAW: |
| 85 | gpr_slice_buffer_destroy(&bb->data.raw.slice_buffer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | break; |
| 87 | } |
| 88 | free(bb); |
| 89 | } |
| 90 | |
| 91 | size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) { |
| 92 | switch (bb->type) { |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 93 | case GRPC_BB_RAW: |
| 94 | return bb->data.raw.slice_buffer.length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 95 | } |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 96 | GPR_UNREACHABLE_CODE(return 0); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 97 | } |