LCOV - code coverage report
Current view: top level - src/core/compression - algorithm.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 27 48 56.2 %
Date: 2015-10-10 Functions: 4 8 50.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       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             : 
      34             : #include <stdlib.h>
      35             : #include <string.h>
      36             : 
      37             : #include <grpc/compression.h>
      38             : #include <grpc/support/useful.h>
      39             : 
      40             : #include "src/core/surface/api_trace.h"
      41             : 
      42     5403729 : int grpc_compression_algorithm_parse(const char *name, size_t name_length,
      43             :                                      grpc_compression_algorithm *algorithm) {
      44             :   /* we use strncmp not only because it's safer (even though in this case it
      45             :    * doesn't matter, given that we are comparing against string literals, but
      46             :    * because this way we needn't have "name" nil-terminated (useful for slice
      47             :    * data, for example) */
      48     5403729 :   GRPC_API_TRACE(
      49             :       "grpc_compression_algorithm_parse("
      50             :       "name=%*.*s, name_length=%lu, algorithm=%p)",
      51             :       5, ((int)name_length, (int)name_length, name, (unsigned long)name_length,
      52             :           algorithm));
      53     5406398 :   if (name_length == 0) {
      54           1 :     return 0;
      55             :   }
      56     5406397 :   if (strncmp(name, "identity", name_length) == 0) {
      57           0 :     *algorithm = GRPC_COMPRESS_NONE;
      58     5407640 :   } else if (strncmp(name, "gzip", name_length) == 0) {
      59     2704094 :     *algorithm = GRPC_COMPRESS_GZIP;
      60     2703546 :   } else if (strncmp(name, "deflate", name_length) == 0) {
      61     2703543 :     *algorithm = GRPC_COMPRESS_DEFLATE;
      62             :   } else {
      63           3 :     return 0;
      64             :   }
      65     5406394 :   return 1;
      66             : }
      67             : 
      68       11692 : int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm,
      69             :                                     char **name) {
      70       11692 :   GRPC_API_TRACE("grpc_compression_algorithm_parse(algorithm=%d, name=%p)", 2,
      71             :                  ((int)algorithm, name));
      72       11693 :   switch (algorithm) {
      73             :     case GRPC_COMPRESS_NONE:
      74        3774 :       *name = "identity";
      75        3774 :       break;
      76             :     case GRPC_COMPRESS_DEFLATE:
      77        3757 :       *name = "deflate";
      78        3757 :       break;
      79             :     case GRPC_COMPRESS_GZIP:
      80        4162 :       *name = "gzip";
      81        4162 :       break;
      82             :     default:
      83           0 :       return 0;
      84             :   }
      85       11693 :   return 1;
      86             : }
      87             : 
      88             : /* TODO(dgq): Add the ability to specify parameters to the individual
      89             :  * compression algorithms */
      90           0 : grpc_compression_algorithm grpc_compression_algorithm_for_level(
      91             :     grpc_compression_level level) {
      92           0 :   GRPC_API_TRACE("grpc_compression_algorithm_for_level(level=%d)", 1,
      93             :                  ((int)level));
      94           0 :   switch (level) {
      95             :     case GRPC_COMPRESS_LEVEL_NONE:
      96           0 :       return GRPC_COMPRESS_NONE;
      97             :     case GRPC_COMPRESS_LEVEL_LOW:
      98             :     case GRPC_COMPRESS_LEVEL_MED:
      99             :     case GRPC_COMPRESS_LEVEL_HIGH:
     100           0 :       return GRPC_COMPRESS_DEFLATE;
     101             :     default:
     102             :       /* we shouldn't be making it here */
     103           0 :       abort();
     104             :       return GRPC_COMPRESS_NONE;
     105             :   }
     106             : }
     107             : 
     108           0 : grpc_compression_level grpc_compression_level_for_algorithm(
     109             :     grpc_compression_algorithm algorithm) {
     110             :   grpc_compression_level clevel;
     111           0 :   GRPC_API_TRACE("grpc_compression_level_for_algorithm(algorithm=%d)", 1,
     112             :                  ((int)algorithm));
     113           0 :   for (clevel = GRPC_COMPRESS_LEVEL_NONE; clevel < GRPC_COMPRESS_LEVEL_COUNT;
     114           0 :        ++clevel) {
     115           0 :     if (grpc_compression_algorithm_for_level(clevel) == algorithm) {
     116           0 :       return clevel;
     117             :     }
     118             :   }
     119           0 :   abort();
     120             :   return GRPC_COMPRESS_LEVEL_NONE;
     121             : }
     122             : 
     123        3898 : void grpc_compression_options_init(grpc_compression_options *opts) {
     124        3898 :   opts->enabled_algorithms_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
     125        3898 :   opts->default_compression_algorithm = GRPC_COMPRESS_NONE;
     126        3898 : }
     127             : 
     128           0 : void grpc_compression_options_enable_algorithm(
     129             :     grpc_compression_options *opts, grpc_compression_algorithm algorithm) {
     130           0 :   GPR_BITSET(&opts->enabled_algorithms_bitset, algorithm);
     131           0 : }
     132             : 
     133           0 : void grpc_compression_options_disable_algorithm(
     134             :     grpc_compression_options *opts, grpc_compression_algorithm algorithm) {
     135           0 :   GPR_BITCLEAR(&opts->enabled_algorithms_bitset, algorithm);
     136           0 : }
     137             : 
     138       15425 : int grpc_compression_options_is_algorithm_enabled(
     139             :     const grpc_compression_options *opts,
     140             :     grpc_compression_algorithm algorithm) {
     141       15425 :   return GPR_BITGET(opts->enabled_algorithms_bitset, algorithm);
     142             : }

Generated by: LCOV version 1.10