aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/compression/message_compress.c
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2015-09-22 10:42:19 -0700
committerGravatar Craig Tiller <ctiller@google.com>2015-09-22 10:42:19 -0700
commit45724b35e411fef7c5da66a74c78428c11d56843 (patch)
tree9264034aca675c89444e02f72ef58e67d7043604 /src/core/compression/message_compress.c
parent298751c1195523ef6228595043b583c3a6270e08 (diff)
indent pass to get logical source lines on one physical line
Diffstat (limited to 'src/core/compression/message_compress.c')
-rw-r--r--src/core/compression/message_compress.c207
1 files changed, 115 insertions, 92 deletions
diff --git a/src/core/compression/message_compress.c b/src/core/compression/message_compress.c
index 01db7134c3..ac079f8724 100644
--- a/src/core/compression/message_compress.c
+++ b/src/core/compression/message_compress.c
@@ -42,153 +42,176 @@
#define OUTPUT_BLOCK_SIZE 1024
-static int zlib_body(z_stream *zs, gpr_slice_buffer *input,
- gpr_slice_buffer *output,
- int (*flate)(z_stream *zs, int flush)) {
+static int
+zlib_body (z_stream * zs, gpr_slice_buffer * input, gpr_slice_buffer * output, int (*flate) (z_stream * zs, int flush))
+{
int r;
int flush;
size_t i;
- gpr_slice outbuf = gpr_slice_malloc(OUTPUT_BLOCK_SIZE);
- const uInt uint_max = ~(uInt)0;
+ gpr_slice outbuf = gpr_slice_malloc (OUTPUT_BLOCK_SIZE);
+ const uInt uint_max = ~(uInt) 0;
- GPR_ASSERT(GPR_SLICE_LENGTH(outbuf) <= uint_max);
- zs->avail_out = (uInt)GPR_SLICE_LENGTH(outbuf);
- zs->next_out = GPR_SLICE_START_PTR(outbuf);
+ GPR_ASSERT (GPR_SLICE_LENGTH (outbuf) <= uint_max);
+ zs->avail_out = (uInt) GPR_SLICE_LENGTH (outbuf);
+ zs->next_out = GPR_SLICE_START_PTR (outbuf);
flush = Z_NO_FLUSH;
- for (i = 0; i < input->count; i++) {
- if (i == input->count - 1) flush = Z_FINISH;
- GPR_ASSERT(GPR_SLICE_LENGTH(input->slices[i]) <= uint_max);
- zs->avail_in = (uInt)GPR_SLICE_LENGTH(input->slices[i]);
- zs->next_in = GPR_SLICE_START_PTR(input->slices[i]);
- do {
- if (zs->avail_out == 0) {
- gpr_slice_buffer_add_indexed(output, outbuf);
- outbuf = gpr_slice_malloc(OUTPUT_BLOCK_SIZE);
- GPR_ASSERT(GPR_SLICE_LENGTH(outbuf) <= uint_max);
- zs->avail_out = (uInt)GPR_SLICE_LENGTH(outbuf);
- zs->next_out = GPR_SLICE_START_PTR(outbuf);
- }
- r = flate(zs, flush);
- if (r == Z_STREAM_ERROR) {
- gpr_log(GPR_INFO, "zlib: stream error");
- goto error;
- }
- } while (zs->avail_out == 0);
- if (zs->avail_in) {
- gpr_log(GPR_INFO, "zlib: not all input consumed");
- goto error;
+ for (i = 0; i < input->count; i++)
+ {
+ if (i == input->count - 1)
+ flush = Z_FINISH;
+ GPR_ASSERT (GPR_SLICE_LENGTH (input->slices[i]) <= uint_max);
+ zs->avail_in = (uInt) GPR_SLICE_LENGTH (input->slices[i]);
+ zs->next_in = GPR_SLICE_START_PTR (input->slices[i]);
+ do
+ {
+ if (zs->avail_out == 0)
+ {
+ gpr_slice_buffer_add_indexed (output, outbuf);
+ outbuf = gpr_slice_malloc (OUTPUT_BLOCK_SIZE);
+ GPR_ASSERT (GPR_SLICE_LENGTH (outbuf) <= uint_max);
+ zs->avail_out = (uInt) GPR_SLICE_LENGTH (outbuf);
+ zs->next_out = GPR_SLICE_START_PTR (outbuf);
+ }
+ r = flate (zs, flush);
+ if (r == Z_STREAM_ERROR)
+ {
+ gpr_log (GPR_INFO, "zlib: stream error");
+ goto error;
+ }
+ }
+ while (zs->avail_out == 0);
+ if (zs->avail_in)
+ {
+ gpr_log (GPR_INFO, "zlib: not all input consumed");
+ goto error;
+ }
}
- }
- GPR_ASSERT(outbuf.refcount);
+ GPR_ASSERT (outbuf.refcount);
outbuf.data.refcounted.length -= zs->avail_out;
- gpr_slice_buffer_add_indexed(output, outbuf);
+ gpr_slice_buffer_add_indexed (output, outbuf);
return 1;
error:
- gpr_slice_unref(outbuf);
+ gpr_slice_unref (outbuf);
return 0;
}
-static int zlib_compress(gpr_slice_buffer *input, gpr_slice_buffer *output,
- int gzip) {
+static int
+zlib_compress (gpr_slice_buffer * input, gpr_slice_buffer * output, int gzip)
+{
z_stream zs;
int r;
size_t i;
size_t count_before = output->count;
size_t length_before = output->length;
- memset(&zs, 0, sizeof(zs));
- r = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | (gzip ? 16 : 0),
- 8, Z_DEFAULT_STRATEGY);
- if (r != Z_OK) {
- gpr_log(GPR_ERROR, "deflateInit2 returns %d", r);
- return 0;
- }
- r = zlib_body(&zs, input, output, deflate) && output->length < input->length;
- if (!r) {
- for (i = count_before; i < output->count; i++) {
- gpr_slice_unref(output->slices[i]);
+ memset (&zs, 0, sizeof (zs));
+ r = deflateInit2 (&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | (gzip ? 16 : 0), 8, Z_DEFAULT_STRATEGY);
+ if (r != Z_OK)
+ {
+ gpr_log (GPR_ERROR, "deflateInit2 returns %d", r);
+ return 0;
+ }
+ r = zlib_body (&zs, input, output, deflate) && output->length < input->length;
+ if (!r)
+ {
+ for (i = count_before; i < output->count; i++)
+ {
+ gpr_slice_unref (output->slices[i]);
+ }
+ output->count = count_before;
+ output->length = length_before;
}
- output->count = count_before;
- output->length = length_before;
- }
- deflateEnd(&zs);
+ deflateEnd (&zs);
return r;
}
-static int zlib_decompress(gpr_slice_buffer *input, gpr_slice_buffer *output,
- int gzip) {
+static int
+zlib_decompress (gpr_slice_buffer * input, gpr_slice_buffer * output, int gzip)
+{
z_stream zs;
int r;
size_t i;
size_t count_before = output->count;
size_t length_before = output->length;
- memset(&zs, 0, sizeof(zs));
- r = inflateInit2(&zs, 15 | (gzip ? 16 : 0));
- if (r != Z_OK) {
- gpr_log(GPR_ERROR, "inflateInit2 returns %d", r);
- return 0;
- }
- r = zlib_body(&zs, input, output, inflate);
- if (!r) {
- for (i = count_before; i < output->count; i++) {
- gpr_slice_unref(output->slices[i]);
+ memset (&zs, 0, sizeof (zs));
+ r = inflateInit2 (&zs, 15 | (gzip ? 16 : 0));
+ if (r != Z_OK)
+ {
+ gpr_log (GPR_ERROR, "inflateInit2 returns %d", r);
+ return 0;
}
- output->count = count_before;
- output->length = length_before;
- }
- inflateEnd(&zs);
+ r = zlib_body (&zs, input, output, inflate);
+ if (!r)
+ {
+ for (i = count_before; i < output->count; i++)
+ {
+ gpr_slice_unref (output->slices[i]);
+ }
+ output->count = count_before;
+ output->length = length_before;
+ }
+ inflateEnd (&zs);
return r;
}
-static int copy(gpr_slice_buffer *input, gpr_slice_buffer *output) {
+static int
+copy (gpr_slice_buffer * input, gpr_slice_buffer * output)
+{
size_t i;
- for (i = 0; i < input->count; i++) {
- gpr_slice_buffer_add(output, gpr_slice_ref(input->slices[i]));
- }
+ for (i = 0; i < input->count; i++)
+ {
+ gpr_slice_buffer_add (output, gpr_slice_ref (input->slices[i]));
+ }
return 1;
}
-int compress_inner(grpc_compression_algorithm algorithm,
- gpr_slice_buffer *input, gpr_slice_buffer *output) {
- switch (algorithm) {
+int
+compress_inner (grpc_compression_algorithm algorithm, gpr_slice_buffer * input, gpr_slice_buffer * output)
+{
+ switch (algorithm)
+ {
case GRPC_COMPRESS_NONE:
/* the fallback path always needs to be send uncompressed: we simply
rely on that here */
return 0;
case GRPC_COMPRESS_DEFLATE:
- return zlib_compress(input, output, 0);
+ return zlib_compress (input, output, 0);
case GRPC_COMPRESS_GZIP:
- return zlib_compress(input, output, 1);
+ return zlib_compress (input, output, 1);
case GRPC_COMPRESS_ALGORITHMS_COUNT:
break;
- }
- gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm);
+ }
+ gpr_log (GPR_ERROR, "invalid compression algorithm %d", algorithm);
return 0;
}
-int grpc_msg_compress(grpc_compression_algorithm algorithm,
- gpr_slice_buffer *input, gpr_slice_buffer *output) {
- if (!compress_inner(algorithm, input, output)) {
- copy(input, output);
- return 0;
- }
+int
+grpc_msg_compress (grpc_compression_algorithm algorithm, gpr_slice_buffer * input, gpr_slice_buffer * output)
+{
+ if (!compress_inner (algorithm, input, output))
+ {
+ copy (input, output);
+ return 0;
+ }
return 1;
}
-int grpc_msg_decompress(grpc_compression_algorithm algorithm,
- gpr_slice_buffer *input, gpr_slice_buffer *output) {
- switch (algorithm) {
+int
+grpc_msg_decompress (grpc_compression_algorithm algorithm, gpr_slice_buffer * input, gpr_slice_buffer * output)
+{
+ switch (algorithm)
+ {
case GRPC_COMPRESS_NONE:
- return copy(input, output);
+ return copy (input, output);
case GRPC_COMPRESS_DEFLATE:
- return zlib_decompress(input, output, 0);
+ return zlib_decompress (input, output, 0);
case GRPC_COMPRESS_GZIP:
- return zlib_decompress(input, output, 1);
+ return zlib_decompress (input, output, 1);
case GRPC_COMPRESS_ALGORITHMS_COUNT:
break;
- }
- gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm);
+ }
+ gpr_log (GPR_ERROR, "invalid compression algorithm %d", algorithm);
return 0;
}