blob: 6514fcd26f6c10bce1eb0187c4dc0a86be5e0899 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, 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
David Garcia Quintase29feb22015-06-15 18:08:13 -070034#include <stdlib.h>
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070035#include <string.h>
David Garcia Quintasbeac88c2015-08-10 13:39:52 -070036
David Garcia Quintas59f905d2015-06-08 16:31:19 -070037#include <grpc/compression.h>
David Garcia Quintasbeac88c2015-08-10 13:39:52 -070038#include <grpc/support/useful.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
David Garcia Quintas1c604fd2015-07-21 16:22:58 -070040int grpc_compression_algorithm_parse(const char* name, size_t name_length,
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070041 grpc_compression_algorithm *algorithm) {
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -070042 /* we use strncmp not only because it's safer (even though in this case it
43 * doesn't matter, given that we are comparing against string literals, but
44 * because this way we needn't have "name" nil-terminated (useful for slice
45 * data, for example) */
David Garcia Quintas8ec09f62015-07-21 17:18:36 -070046 if (name_length == 0) {
47 return 0;
48 }
David Garcia Quintas1c604fd2015-07-21 16:22:58 -070049 if (strncmp(name, "none", name_length) == 0) {
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070050 *algorithm = GRPC_COMPRESS_NONE;
David Garcia Quintas1c604fd2015-07-21 16:22:58 -070051 } else if (strncmp(name, "gzip", name_length) == 0) {
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070052 *algorithm = GRPC_COMPRESS_GZIP;
David Garcia Quintas1c604fd2015-07-21 16:22:58 -070053 } else if (strncmp(name, "deflate", name_length) == 0) {
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070054 *algorithm = GRPC_COMPRESS_DEFLATE;
55 } else {
56 return 0;
57 }
58 return 1;
59}
60
61int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm,
62 char **name) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063 switch (algorithm) {
64 case GRPC_COMPRESS_NONE:
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070065 *name = "none";
66 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067 case GRPC_COMPRESS_DEFLATE:
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070068 *name = "deflate";
69 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070 case GRPC_COMPRESS_GZIP:
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070071 *name = "gzip";
72 break;
73 default:
74 return 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 }
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070076 return 1;
Craig Tiller190d3602015-02-18 09:23:38 -080077}
David Garcia Quintase29feb22015-06-15 18:08:13 -070078
79/* TODO(dgq): Add the ability to specify parameters to the individual
80 * compression algorithms */
81grpc_compression_algorithm grpc_compression_algorithm_for_level(
82 grpc_compression_level level) {
83 switch (level) {
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070084 case GRPC_COMPRESS_LEVEL_NONE:
David Garcia Quintase29feb22015-06-15 18:08:13 -070085 return GRPC_COMPRESS_NONE;
86 case GRPC_COMPRESS_LEVEL_LOW:
87 case GRPC_COMPRESS_LEVEL_MED:
David Garcia Quintase29feb22015-06-15 18:08:13 -070088 case GRPC_COMPRESS_LEVEL_HIGH:
David Garcia Quintasfd621742015-06-15 23:50:39 -070089 return GRPC_COMPRESS_DEFLATE;
David Garcia Quintase29feb22015-06-15 18:08:13 -070090 default:
91 /* we shouldn't be making it here */
92 abort();
93 }
94}
David Garcia Quintasfc0fa332015-06-25 18:11:07 -070095
96grpc_compression_level grpc_compression_level_for_algorithm(
97 grpc_compression_algorithm algorithm) {
98 grpc_compression_level clevel;
99 for (clevel = GRPC_COMPRESS_LEVEL_NONE; clevel < GRPC_COMPRESS_LEVEL_COUNT;
100 ++clevel) {
101 if (grpc_compression_algorithm_for_level(clevel) == algorithm) {
102 return clevel;
103 }
104 }
105 abort();
106}
David Garcia Quintasbeac88c2015-08-10 13:39:52 -0700107
108void grpc_compression_options_init(grpc_compression_options *opts) {
109 opts->enabled_algorithms_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT)-1;
110 opts->default_compression_algorithm = GRPC_COMPRESS_NONE;
111}
112
113void grpc_compression_options_enable_algorithm(
114 grpc_compression_options *opts, grpc_compression_algorithm algorithm) {
115 GPR_BITSET(&opts->enabled_algorithms_bitset, algorithm);
116}
117
118void grpc_compression_options_disable_algorithm(
119 grpc_compression_options *opts, grpc_compression_algorithm algorithm) {
120 GPR_BITCLEAR(&opts->enabled_algorithms_bitset, algorithm);
121}
122
123int grpc_compression_options_is_algorithm_enabled(
124 const grpc_compression_options *opts,
125 grpc_compression_algorithm algorithm) {
126 return GPR_BITGET(opts->enabled_algorithms_bitset, algorithm);
127}