Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | befa1ff | 2016-01-05 07:54:11 -0800 | [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 | |
David Garcia Quintas | e29feb2 | 2015-06-15 18:08:13 -0700 | [diff] [blame] | 34 | #include <stdlib.h> |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 35 | #include <string.h> |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 36 | |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 37 | #include <grpc/compression.h> |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 38 | #include <grpc/support/useful.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 40 | #include "src/core/compression/algorithm_metadata.h" |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 41 | #include "src/core/surface/api_trace.h" |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 42 | #include "src/core/transport/static_metadata.h" |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 43 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 44 | int grpc_compression_algorithm_parse(const char *name, size_t name_length, |
| 45 | grpc_compression_algorithm *algorithm) { |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 46 | /* we use strncmp not only because it's safer (even though in this case it |
| 47 | * doesn't matter, given that we are comparing against string literals, but |
| 48 | * because this way we needn't have "name" nil-terminated (useful for slice |
| 49 | * data, for example) */ |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 50 | GRPC_API_TRACE( |
| 51 | "grpc_compression_algorithm_parse(" |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 52 | "name=%*.*s, name_length=%lu, algorithm=%p)", |
| 53 | 5, ((int)name_length, (int)name_length, name, (unsigned long)name_length, |
| 54 | algorithm)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 55 | if (name_length == 0) { |
| 56 | return 0; |
| 57 | } |
| 58 | if (strncmp(name, "identity", name_length) == 0) { |
| 59 | *algorithm = GRPC_COMPRESS_NONE; |
| 60 | } else if (strncmp(name, "gzip", name_length) == 0) { |
| 61 | *algorithm = GRPC_COMPRESS_GZIP; |
| 62 | } else if (strncmp(name, "deflate", name_length) == 0) { |
| 63 | *algorithm = GRPC_COMPRESS_DEFLATE; |
| 64 | } else { |
| 65 | return 0; |
| 66 | } |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 67 | return 1; |
| 68 | } |
| 69 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 70 | int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm, |
| 71 | char **name) { |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 72 | GRPC_API_TRACE("grpc_compression_algorithm_parse(algorithm=%d, name=%p)", 2, |
| 73 | ((int)algorithm, name)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 74 | switch (algorithm) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | case GRPC_COMPRESS_NONE: |
David Garcia Quintas | cddd95b | 2015-08-13 16:59:16 -0700 | [diff] [blame] | 76 | *name = "identity"; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 77 | return 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | case GRPC_COMPRESS_DEFLATE: |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 79 | *name = "deflate"; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 80 | return 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | case GRPC_COMPRESS_GZIP: |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 82 | *name = "gzip"; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 83 | return 1; |
| 84 | case GRPC_COMPRESS_ALGORITHMS_COUNT: |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 85 | return 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | } |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 87 | return 0; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | grpc_compression_algorithm grpc_compression_algorithm_from_mdstr( |
| 91 | grpc_mdstr *str) { |
| 92 | if (str == GRPC_MDSTR_IDENTITY) return GRPC_COMPRESS_NONE; |
| 93 | if (str == GRPC_MDSTR_DEFLATE) return GRPC_COMPRESS_DEFLATE; |
| 94 | if (str == GRPC_MDSTR_GZIP) return GRPC_COMPRESS_GZIP; |
| 95 | return GRPC_COMPRESS_ALGORITHMS_COUNT; |
| 96 | } |
| 97 | |
| 98 | grpc_mdstr *grpc_compression_algorithm_mdstr( |
| 99 | grpc_compression_algorithm algorithm) { |
| 100 | switch (algorithm) { |
| 101 | case GRPC_COMPRESS_NONE: |
| 102 | return GRPC_MDSTR_IDENTITY; |
| 103 | case GRPC_COMPRESS_DEFLATE: |
| 104 | return GRPC_MDSTR_DEFLATE; |
| 105 | case GRPC_COMPRESS_GZIP: |
| 106 | return GRPC_MDSTR_GZIP; |
| 107 | case GRPC_COMPRESS_ALGORITHMS_COUNT: |
| 108 | return NULL; |
| 109 | } |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | grpc_mdelem *grpc_compression_encoding_mdelem( |
| 114 | grpc_compression_algorithm algorithm) { |
| 115 | switch (algorithm) { |
| 116 | case GRPC_COMPRESS_NONE: |
| 117 | return GRPC_MDELEM_GRPC_ENCODING_IDENTITY; |
| 118 | case GRPC_COMPRESS_DEFLATE: |
| 119 | return GRPC_MDELEM_GRPC_ENCODING_DEFLATE; |
| 120 | case GRPC_COMPRESS_GZIP: |
| 121 | return GRPC_MDELEM_GRPC_ENCODING_GZIP; |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 122 | default: |
| 123 | break; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 124 | } |
| 125 | return NULL; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 126 | } |
David Garcia Quintas | e29feb2 | 2015-06-15 18:08:13 -0700 | [diff] [blame] | 127 | |
| 128 | /* TODO(dgq): Add the ability to specify parameters to the individual |
| 129 | * compression algorithms */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | grpc_compression_algorithm grpc_compression_algorithm_for_level( |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 131 | grpc_compression_level level, uint32_t accepted_encodings) { |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 132 | GRPC_API_TRACE("grpc_compression_algorithm_for_level(level=%d)", 1, |
| 133 | ((int)level)); |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 134 | if (level > GRPC_COMPRESS_LEVEL_HIGH) { |
| 135 | gpr_log(GPR_ERROR, "Unknown compression level %d.", (int)level); |
| 136 | abort(); |
| 137 | } |
| 138 | |
| 139 | const size_t num_supported = |
| 140 | GPR_BITCOUNT(accepted_encodings) - 1; /* discard NONE */ |
| 141 | if (level == GRPC_COMPRESS_LEVEL_NONE || num_supported == 0) { |
| 142 | return GRPC_COMPRESS_NONE; |
| 143 | } |
| 144 | |
| 145 | GPR_ASSERT(level > 0); |
| 146 | |
| 147 | /* Establish a "ranking" or compression algorithms in increasing order of |
| 148 | * compression. |
| 149 | * This is simplistic and we will probably want to introduce other dimensions |
| 150 | * in the future (cpu/memory cost, etc). */ |
| 151 | const grpc_compression_algorithm algos_ranking[] = {GRPC_COMPRESS_GZIP, |
| 152 | GRPC_COMPRESS_DEFLATE}; |
| 153 | |
| 154 | /* intersect algos_ranking with the supported ones keeping the ranked order */ |
David Garcia Quintas | 0a5fce8 | 2016-03-18 09:58:55 -0700 | [diff] [blame] | 155 | grpc_compression_algorithm |
| 156 | sorted_supported_algos[GRPC_COMPRESS_ALGORITHMS_COUNT]; |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 157 | size_t algos_supported_idx = 0; |
| 158 | for (size_t i = 0; i < GPR_ARRAY_SIZE(algos_ranking); i++) { |
| 159 | const grpc_compression_algorithm alg = algos_ranking[i]; |
| 160 | for (size_t j = 0; j < num_supported; j++) { |
| 161 | if (GPR_BITGET(accepted_encodings, alg) == 1) { |
| 162 | /* if \a alg in supported */ |
| 163 | sorted_supported_algos[algos_supported_idx++] = alg; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | if (algos_supported_idx == num_supported) break; |
| 168 | } |
| 169 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 170 | switch (level) { |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 171 | case GRPC_COMPRESS_LEVEL_NONE: |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 172 | abort(); /* should have been handled already */ |
David Garcia Quintas | e29feb2 | 2015-06-15 18:08:13 -0700 | [diff] [blame] | 173 | case GRPC_COMPRESS_LEVEL_LOW: |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 174 | return sorted_supported_algos[0]; |
David Garcia Quintas | e29feb2 | 2015-06-15 18:08:13 -0700 | [diff] [blame] | 175 | case GRPC_COMPRESS_LEVEL_MED: |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 176 | return sorted_supported_algos[num_supported / 2]; |
David Garcia Quintas | e29feb2 | 2015-06-15 18:08:13 -0700 | [diff] [blame] | 177 | case GRPC_COMPRESS_LEVEL_HIGH: |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 178 | return sorted_supported_algos[num_supported - 1]; |
David Garcia Quintas | e29feb2 | 2015-06-15 18:08:13 -0700 | [diff] [blame] | 179 | default: |
yang-g | 3fd2836 | 2016-01-04 10:08:58 -0800 | [diff] [blame] | 180 | abort(); |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 181 | }; |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 182 | } |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 183 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 184 | void grpc_compression_options_init(grpc_compression_options *opts) { |
Craig Tiller | af73d78 | 2015-09-22 09:30:46 -0700 | [diff] [blame] | 185 | opts->enabled_algorithms_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 186 | opts->default_compression_algorithm = GRPC_COMPRESS_NONE; |
| 187 | } |
| 188 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | void grpc_compression_options_enable_algorithm( |
| 190 | grpc_compression_options *opts, grpc_compression_algorithm algorithm) { |
| 191 | GPR_BITSET(&opts->enabled_algorithms_bitset, algorithm); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 194 | void grpc_compression_options_disable_algorithm( |
| 195 | grpc_compression_options *opts, grpc_compression_algorithm algorithm) { |
| 196 | GPR_BITCLEAR(&opts->enabled_algorithms_bitset, algorithm); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | int grpc_compression_options_is_algorithm_enabled( |
| 200 | const grpc_compression_options *opts, |
| 201 | grpc_compression_algorithm algorithm) { |
| 202 | return GPR_BITGET(opts->enabled_algorithms_bitset, algorithm); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 203 | } |