| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 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 |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 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. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/compression/message_compress.h" |
| 22 |
|
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include <zconf.h> |
| 26 |
#include <zlib.h> |
| 27 |
|
| 28 |
#include <grpc/slice_buffer.h> |
| 29 |
#include <grpc/support/alloc.h> |
| 30 |
#include <grpc/support/log.h> |
| 31 |
|
| 32 |
#include "src/core/lib/slice/slice.h" |
| 33 |
|
| 34 |
#define OUTPUT_BLOCK_SIZE 1024 |
| 35 |
|
| 36 |
static int zlib_body(z_stream* zs, grpc_slice_buffer* input, |
| 37 |
grpc_slice_buffer* output, |
| 38 |
int (*flate)(z_stream* zs, int flush)) { |
| 39 |
int r = Z_STREAM_END; /* Do not fail on an empty input. */ |
| 40 |
int flush; |
| 41 |
size_t i; |
| 42 |
grpc_slice outbuf = GRPC_SLICE_MALLOC(OUTPUT_BLOCK_SIZE); |
| 43 |
const uInt uint_max = ~static_cast<uInt>(0); |
| 44 |
|
| 45 |
GPR_ASSERT(GRPC_SLICE_LENGTH(outbuf) <= uint_max); |
| 46 |
zs->avail_out = static_cast<uInt> GRPC_SLICE_LENGTH(outbuf); |
| 47 |
zs->next_out = GRPC_SLICE_START_PTR(outbuf); |
| 48 |
flush = Z_NO_FLUSH; |
| 49 |
for (i = 0; i < input->count; i++) { |
| 50 |
if (i == input->count - 1) flush = Z_FINISH; |
| 51 |
GPR_ASSERT(GRPC_SLICE_LENGTH(input->slices[i]) <= uint_max); |
| 52 |
zs->avail_in = static_cast<uInt> GRPC_SLICE_LENGTH(input->slices[i]); |
| 53 |
zs->next_in = GRPC_SLICE_START_PTR(input->slices[i]); |
| 54 |
do { |
| 55 |
if (zs->avail_out == 0) { |
| 56 |
grpc_slice_buffer_add_indexed(output, outbuf); |
| 57 |
outbuf = GRPC_SLICE_MALLOC(OUTPUT_BLOCK_SIZE); |
| 58 |
GPR_ASSERT(GRPC_SLICE_LENGTH(outbuf) <= uint_max); |
| 59 |
zs->avail_out = static_cast<uInt> GRPC_SLICE_LENGTH(outbuf); |
| 60 |
zs->next_out = GRPC_SLICE_START_PTR(outbuf); |
| 61 |
} |
| 62 |
r = flate(zs, flush); |
| 63 |
if (r < 0 && r != Z_BUF_ERROR /* not fatal */) { |
| 64 |
gpr_log(GPR_INFO, "zlib error (%d)", r); |
| 65 |
goto error; |
| 66 |
} |
| 67 |
} while (zs->avail_out == 0); |
| 68 |
if (zs->avail_in) { |
| 69 |
gpr_log(GPR_INFO, "zlib: not all input consumed"); |
| 70 |
goto error; |
| 71 |
} |
| 72 |
} |
| 73 |
if (r != Z_STREAM_END) { |
| 74 |
gpr_log(GPR_INFO, "zlib: Data error"); |
| 75 |
goto error; |
| 76 |
} |
| 77 |
|
| 78 |
GPR_ASSERT(outbuf.refcount); |
| 79 |
outbuf.data.refcounted.length -= zs->avail_out; |
| 80 |
grpc_slice_buffer_add_indexed(output, outbuf); |
| 81 |
|
| 82 |
return 1; |
| 83 |
|
| 84 |
error: |
| 85 |
grpc_core::CSliceUnref(outbuf); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static void* zalloc_gpr(void* /*opaque*/, unsigned int items, |
| 90 |
unsigned int size) { |
| 91 |
return gpr_malloc(items * size); |
| 92 |
} |
| 93 |
|
| 94 |
static void zfree_gpr(void* /*opaque*/, void* address) { gpr_free(address); } |
| 95 |
|
| 96 |
static int zlib_compress(grpc_slice_buffer* input, grpc_slice_buffer* output, |
| 97 |
int gzip) { |
| 98 |
z_stream zs; |
| 99 |
int r; |
| 100 |
size_t i; |
| 101 |
size_t count_before = output->count; |
| 102 |
size_t length_before = output->length; |
| 103 |
memset(&zs, 0, sizeof(zs)); |
| 104 |
zs.zalloc = zalloc_gpr; |
| 105 |
zs.zfree = zfree_gpr; |
| 106 |
r = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | (gzip ? 16 : 0), |
| 107 |
8, Z_DEFAULT_STRATEGY); |
| 108 |
GPR_ASSERT(r == Z_OK); |
| 109 |
r = zlib_body(&zs, input, output, deflate) && output->length < input->length; |
| 110 |
if (!r) { |
| 111 |
for (i = count_before; i < output->count; i++) { |
| 112 |
grpc_core::CSliceUnref(output->slices[i]); |
| 113 |
} |
| 114 |
output->count = count_before; |
| 115 |
output->length = length_before; |
| 116 |
} |
| 117 |
deflateEnd(&zs); |
| 118 |
return r; |
| 119 |
} |
| 120 |
|
| 121 |
static int zlib_decompress(grpc_slice_buffer* input, grpc_slice_buffer* output, |
| 122 |
int gzip) { |
| 123 |
z_stream zs; |
| 124 |
int r; |
| 125 |
size_t i; |
| 126 |
size_t count_before = output->count; |
| 127 |
size_t length_before = output->length; |
| 128 |
memset(&zs, 0, sizeof(zs)); |
| 129 |
zs.zalloc = zalloc_gpr; |
| 130 |
zs.zfree = zfree_gpr; |
| 131 |
r = inflateInit2(&zs, 15 | (gzip ? 16 : 0)); |
| 132 |
GPR_ASSERT(r == Z_OK); |
| 133 |
r = zlib_body(&zs, input, output, inflate); |
| 134 |
if (!r) { |
| 135 |
for (i = count_before; i < output->count; i++) { |
| 136 |
grpc_core::CSliceUnref(output->slices[i]); |
| 137 |
} |
| 138 |
output->count = count_before; |
| 139 |
output->length = length_before; |
| 140 |
} |
| 141 |
inflateEnd(&zs); |
| 142 |
return r; |
| 143 |
} |
| 144 |
|
| 145 |
static int copy(grpc_slice_buffer* input, grpc_slice_buffer* output) { |
| 146 |
size_t i; |
| 147 |
for (i = 0; i < input->count; i++) { |
| 148 |
grpc_slice_buffer_add(output, grpc_core::CSliceRef(input->slices[i])); |
| 149 |
} |
| 150 |
return 1; |
| 151 |
} |
| 152 |
|
| 153 |
static int compress_inner(grpc_compression_algorithm algorithm, |
| 154 |
grpc_slice_buffer* input, grpc_slice_buffer* output) { |
| 155 |
switch (algorithm) { |
| 156 |
case GRPC_COMPRESS_NONE: |
| 157 |
/* the fallback path always needs to be send uncompressed: we simply |
| 158 |
rely on that here */ |
| 159 |
return 0; |
| 160 |
case GRPC_COMPRESS_DEFLATE: |
| 161 |
return zlib_compress(input, output, 0); |
| 162 |
case GRPC_COMPRESS_GZIP: |
| 163 |
return zlib_compress(input, output, 1); |
| 164 |
case GRPC_COMPRESS_ALGORITHMS_COUNT: |
| 165 |
break; |
| 166 |
} |
| 167 |
gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm); |
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
int grpc_msg_compress(grpc_compression_algorithm algorithm, |
| 172 |
grpc_slice_buffer* input, grpc_slice_buffer* output) { |
| 173 |
if (!compress_inner(algorithm, input, output)) { |
| 174 |
copy(input, output); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
return 1; |
| 178 |
} |
| 179 |
|
| 180 |
int grpc_msg_decompress(grpc_compression_algorithm algorithm, |
| 181 |
grpc_slice_buffer* input, grpc_slice_buffer* output) { |
| 182 |
switch (algorithm) { |
| 183 |
case GRPC_COMPRESS_NONE: |
| 184 |
return copy(input, output); |
| 185 |
case GRPC_COMPRESS_DEFLATE: |
| 186 |
return zlib_decompress(input, output, 0); |
| 187 |
case GRPC_COMPRESS_GZIP: |
| 188 |
return zlib_decompress(input, output, 1); |
| 189 |
case GRPC_COMPRESS_ALGORITHMS_COUNT: |
| 190 |
break; |
| 191 |
} |
| 192 |
gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm); |
| 193 |
return 0; |
| 194 |
} |