Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, 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 | |
David Garcia Quintas | 1c604fd | 2015-07-21 16:22:58 -0700 | [diff] [blame] | 40 | int grpc_compression_algorithm_parse(const char* name, size_t name_length, |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 41 | grpc_compression_algorithm *algorithm) { |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 42 | /* 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 Quintas | 8ec09f6 | 2015-07-21 17:18:36 -0700 | [diff] [blame] | 46 | if (name_length == 0) { |
| 47 | return 0; |
| 48 | } |
David Garcia Quintas | 1c604fd | 2015-07-21 16:22:58 -0700 | [diff] [blame] | 49 | if (strncmp(name, "none", name_length) == 0) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 50 | *algorithm = GRPC_COMPRESS_NONE; |
David Garcia Quintas | 1c604fd | 2015-07-21 16:22:58 -0700 | [diff] [blame] | 51 | } else if (strncmp(name, "gzip", name_length) == 0) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 52 | *algorithm = GRPC_COMPRESS_GZIP; |
David Garcia Quintas | 1c604fd | 2015-07-21 16:22:58 -0700 | [diff] [blame] | 53 | } else if (strncmp(name, "deflate", name_length) == 0) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 54 | *algorithm = GRPC_COMPRESS_DEFLATE; |
| 55 | } else { |
| 56 | return 0; |
| 57 | } |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm, |
| 62 | char **name) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | switch (algorithm) { |
| 64 | case GRPC_COMPRESS_NONE: |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 65 | *name = "none"; |
| 66 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | case GRPC_COMPRESS_DEFLATE: |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 68 | *name = "deflate"; |
| 69 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | case GRPC_COMPRESS_GZIP: |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 71 | *name = "gzip"; |
| 72 | break; |
| 73 | default: |
| 74 | return 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | } |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 76 | return 1; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 77 | } |
David Garcia Quintas | e29feb2 | 2015-06-15 18:08:13 -0700 | [diff] [blame] | 78 | |
| 79 | /* TODO(dgq): Add the ability to specify parameters to the individual |
| 80 | * compression algorithms */ |
| 81 | grpc_compression_algorithm grpc_compression_algorithm_for_level( |
| 82 | grpc_compression_level level) { |
| 83 | switch (level) { |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 84 | case GRPC_COMPRESS_LEVEL_NONE: |
David Garcia Quintas | e29feb2 | 2015-06-15 18:08:13 -0700 | [diff] [blame] | 85 | return GRPC_COMPRESS_NONE; |
| 86 | case GRPC_COMPRESS_LEVEL_LOW: |
| 87 | case GRPC_COMPRESS_LEVEL_MED: |
David Garcia Quintas | e29feb2 | 2015-06-15 18:08:13 -0700 | [diff] [blame] | 88 | case GRPC_COMPRESS_LEVEL_HIGH: |
David Garcia Quintas | fd62174 | 2015-06-15 23:50:39 -0700 | [diff] [blame] | 89 | return GRPC_COMPRESS_DEFLATE; |
David Garcia Quintas | e29feb2 | 2015-06-15 18:08:13 -0700 | [diff] [blame] | 90 | default: |
| 91 | /* we shouldn't be making it here */ |
| 92 | abort(); |
| 93 | } |
| 94 | } |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 95 | |
| 96 | grpc_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 Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 107 | |
| 108 | void 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 | |
| 113 | void grpc_compression_options_enable_algorithm( |
| 114 | grpc_compression_options *opts, grpc_compression_algorithm algorithm) { |
| 115 | GPR_BITSET(&opts->enabled_algorithms_bitset, algorithm); |
| 116 | } |
| 117 | |
| 118 | void grpc_compression_options_disable_algorithm( |
| 119 | grpc_compression_options *opts, grpc_compression_algorithm algorithm) { |
| 120 | GPR_BITCLEAR(&opts->enabled_algorithms_bitset, algorithm); |
| 121 | } |
| 122 | |
| 123 | int 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 | } |