Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 19 | #include "src/core/lib/compression/message_compress.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <grpc/support/alloc.h> |
| 24 | #include <grpc/support/log.h> |
| 25 | |
| 26 | #include <zlib.h> |
| 27 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 28 | #include "src/core/lib/slice/slice_internal.h" |
| 29 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 30 | #define OUTPUT_BLOCK_SIZE 1024 |
| 31 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 32 | static int zlib_body(grpc_exec_ctx* exec_ctx, z_stream* zs, |
| 33 | grpc_slice_buffer* input, grpc_slice_buffer* output, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 34 | int (*flate)(z_stream* zs, int flush)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | int r; |
| 36 | int flush; |
| 37 | size_t i; |
Craig Tiller | 423d6fd | 2017-04-12 13:15:45 -0700 | [diff] [blame] | 38 | grpc_slice outbuf = GRPC_SLICE_MALLOC(OUTPUT_BLOCK_SIZE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 39 | const uInt uint_max = ~(uInt)0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 41 | GPR_ASSERT(GRPC_SLICE_LENGTH(outbuf) <= uint_max); |
| 42 | zs->avail_out = (uInt)GRPC_SLICE_LENGTH(outbuf); |
| 43 | zs->next_out = GRPC_SLICE_START_PTR(outbuf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | flush = Z_NO_FLUSH; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 45 | for (i = 0; i < input->count; i++) { |
| 46 | if (i == input->count - 1) flush = Z_FINISH; |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 47 | GPR_ASSERT(GRPC_SLICE_LENGTH(input->slices[i]) <= uint_max); |
| 48 | zs->avail_in = (uInt)GRPC_SLICE_LENGTH(input->slices[i]); |
| 49 | zs->next_in = GRPC_SLICE_START_PTR(input->slices[i]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 50 | do { |
| 51 | if (zs->avail_out == 0) { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 52 | grpc_slice_buffer_add_indexed(output, outbuf); |
Craig Tiller | 423d6fd | 2017-04-12 13:15:45 -0700 | [diff] [blame] | 53 | outbuf = GRPC_SLICE_MALLOC(OUTPUT_BLOCK_SIZE); |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 54 | GPR_ASSERT(GRPC_SLICE_LENGTH(outbuf) <= uint_max); |
| 55 | zs->avail_out = (uInt)GRPC_SLICE_LENGTH(outbuf); |
| 56 | zs->next_out = GRPC_SLICE_START_PTR(outbuf); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 57 | } |
| 58 | r = flate(zs, flush); |
David Garcia Quintas | 5e0da58 | 2015-12-10 18:27:32 -0800 | [diff] [blame] | 59 | if (r < 0 && r != Z_BUF_ERROR /* not fatal */) { |
| 60 | gpr_log(GPR_INFO, "zlib error (%d)", r); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 61 | goto error; |
| 62 | } |
| 63 | } while (zs->avail_out == 0); |
| 64 | if (zs->avail_in) { |
| 65 | gpr_log(GPR_INFO, "zlib: not all input consumed"); |
| 66 | goto error; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 70 | GPR_ASSERT(outbuf.refcount); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | outbuf.data.refcounted.length -= zs->avail_out; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 72 | grpc_slice_buffer_add_indexed(output, outbuf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 73 | |
| 74 | return 1; |
| 75 | |
| 76 | error: |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 77 | grpc_slice_unref_internal(exec_ctx, outbuf); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 81 | static void* zalloc_gpr(void* opaque, unsigned int items, unsigned int size) { |
David Garcia Quintas | 0a08791 | 2015-12-09 10:46:14 -0800 | [diff] [blame] | 82 | return gpr_malloc(items * size); |
| 83 | } |
| 84 | |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 85 | static void zfree_gpr(void* opaque, void* address) { gpr_free(address); } |
David Garcia Quintas | 0a08791 | 2015-12-09 10:46:14 -0800 | [diff] [blame] | 86 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 87 | static int zlib_compress(grpc_exec_ctx* exec_ctx, grpc_slice_buffer* input, |
| 88 | grpc_slice_buffer* output, int gzip) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 89 | z_stream zs; |
| 90 | int r; |
| 91 | size_t i; |
| 92 | size_t count_before = output->count; |
| 93 | size_t length_before = output->length; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 94 | memset(&zs, 0, sizeof(zs)); |
David Garcia Quintas | 0a08791 | 2015-12-09 10:46:14 -0800 | [diff] [blame] | 95 | zs.zalloc = zalloc_gpr; |
| 96 | zs.zfree = zfree_gpr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | r = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | (gzip ? 16 : 0), |
| 98 | 8, Z_DEFAULT_STRATEGY); |
David Garcia Quintas | e9fa311 | 2015-12-09 17:25:10 -0800 | [diff] [blame] | 99 | GPR_ASSERT(r == Z_OK); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 100 | r = zlib_body(exec_ctx, &zs, input, output, deflate) && |
| 101 | output->length < input->length; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | if (!r) { |
| 103 | for (i = count_before; i < output->count; i++) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 104 | grpc_slice_unref_internal(exec_ctx, output->slices[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | output->count = count_before; |
| 107 | output->length = length_before; |
| 108 | } |
| 109 | deflateEnd(&zs); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | return r; |
| 111 | } |
| 112 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 113 | static int zlib_decompress(grpc_exec_ctx* exec_ctx, grpc_slice_buffer* input, |
| 114 | grpc_slice_buffer* output, int gzip) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 115 | z_stream zs; |
| 116 | int r; |
| 117 | size_t i; |
| 118 | size_t count_before = output->count; |
| 119 | size_t length_before = output->length; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 120 | memset(&zs, 0, sizeof(zs)); |
David Garcia Quintas | 0a08791 | 2015-12-09 10:46:14 -0800 | [diff] [blame] | 121 | zs.zalloc = zalloc_gpr; |
| 122 | zs.zfree = zfree_gpr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | r = inflateInit2(&zs, 15 | (gzip ? 16 : 0)); |
David Garcia Quintas | e9fa311 | 2015-12-09 17:25:10 -0800 | [diff] [blame] | 124 | GPR_ASSERT(r == Z_OK); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 125 | r = zlib_body(exec_ctx, &zs, input, output, inflate); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 126 | if (!r) { |
| 127 | for (i = count_before; i < output->count; i++) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 128 | grpc_slice_unref_internal(exec_ctx, output->slices[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | output->count = count_before; |
| 131 | output->length = length_before; |
| 132 | } |
| 133 | inflateEnd(&zs); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 134 | return r; |
| 135 | } |
| 136 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 137 | static int copy(grpc_slice_buffer* input, grpc_slice_buffer* output) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 138 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 139 | for (i = 0; i < input->count; i++) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 140 | grpc_slice_buffer_add(output, grpc_slice_ref_internal(input->slices[i])); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 141 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | return 1; |
| 143 | } |
| 144 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 145 | static int compress_inner(grpc_exec_ctx* exec_ctx, |
| 146 | grpc_compression_algorithm algorithm, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 147 | grpc_slice_buffer* input, grpc_slice_buffer* output) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 148 | switch (algorithm) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | case GRPC_COMPRESS_NONE: |
| 150 | /* the fallback path always needs to be send uncompressed: we simply |
| 151 | rely on that here */ |
| 152 | return 0; |
| 153 | case GRPC_COMPRESS_DEFLATE: |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 154 | return zlib_compress(exec_ctx, input, output, 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | case GRPC_COMPRESS_GZIP: |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 156 | return zlib_compress(exec_ctx, input, output, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 157 | case GRPC_COMPRESS_ALGORITHMS_COUNT: |
| 158 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | } |
| 160 | gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 164 | int grpc_msg_compress(grpc_exec_ctx* exec_ctx, |
| 165 | grpc_compression_algorithm algorithm, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 166 | grpc_slice_buffer* input, grpc_slice_buffer* output) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 167 | if (!compress_inner(exec_ctx, algorithm, input, output)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 168 | copy(input, output); |
| 169 | return 0; |
| 170 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 171 | return 1; |
| 172 | } |
| 173 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 174 | int grpc_msg_decompress(grpc_exec_ctx* exec_ctx, |
| 175 | grpc_compression_algorithm algorithm, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 176 | grpc_slice_buffer* input, grpc_slice_buffer* output) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 177 | switch (algorithm) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | case GRPC_COMPRESS_NONE: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 179 | return copy(input, output); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 180 | case GRPC_COMPRESS_DEFLATE: |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 181 | return zlib_decompress(exec_ctx, input, output, 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | case GRPC_COMPRESS_GZIP: |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 183 | return zlib_decompress(exec_ctx, input, output, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 184 | case GRPC_COMPRESS_ALGORITHMS_COUNT: |
| 185 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | } |
| 187 | gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 188 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 189 | } |