LCOV - code coverage report
Current view: top level - core/compression - algorithm.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 36 67 53.7 %
Date: 2015-12-10 22:15:08 Functions: 6 11 54.5 %

          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/compression/algorithm_metadata.h"
      41             : #include "src/core/surface/api_trace.h"
      42             : #include "src/core/transport/static_metadata.h"
      43             : 
      44         491 : int grpc_compression_algorithm_parse(const char *name, size_t name_length,
      45             :                                      grpc_compression_algorithm *algorithm) {
      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) */
      50         491 :   GRPC_API_TRACE(
      51             :       "grpc_compression_algorithm_parse("
      52             :       "name=%*.*s, name_length=%lu, algorithm=%p)",
      53             :       5, ((int)name_length, (int)name_length, name, (unsigned long)name_length,
      54             :           algorithm));
      55         491 :   if (name_length == 0) {
      56           1 :     return 0;
      57             :   }
      58         490 :   if (strncmp(name, "identity", name_length) == 0) {
      59          30 :     *algorithm = GRPC_COMPRESS_NONE;
      60         460 :   } else if (strncmp(name, "gzip", name_length) == 0) {
      61         453 :     *algorithm = GRPC_COMPRESS_GZIP;
      62           7 :   } else if (strncmp(name, "deflate", name_length) == 0) {
      63           4 :     *algorithm = GRPC_COMPRESS_DEFLATE;
      64             :   } else {
      65           3 :     return 0;
      66             :   }
      67         487 :   return 1;
      68             : }
      69             : 
      70         463 : int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm,
      71             :                                     char **name) {
      72         463 :   GRPC_API_TRACE("grpc_compression_algorithm_parse(algorithm=%d, name=%p)", 2,
      73             :                  ((int)algorithm, name));
      74         463 :   switch (algorithm) {
      75             :     case GRPC_COMPRESS_NONE:
      76          22 :       *name = "identity";
      77          22 :       return 1;
      78             :     case GRPC_COMPRESS_DEFLATE:
      79          18 :       *name = "deflate";
      80          18 :       return 1;
      81             :     case GRPC_COMPRESS_GZIP:
      82         423 :       *name = "gzip";
      83         423 :       return 1;
      84             :     case GRPC_COMPRESS_ALGORITHMS_COUNT:
      85           0 :       return 0;
      86             :   }
      87           0 :   return 0;
      88             : }
      89             : 
      90     4336754 : grpc_compression_algorithm grpc_compression_algorithm_from_mdstr(
      91             :     grpc_mdstr *str) {
      92     4336754 :   if (str == GRPC_MDSTR_IDENTITY) return GRPC_COMPRESS_NONE;
      93      105193 :   if (str == GRPC_MDSTR_DEFLATE) return GRPC_COMPRESS_DEFLATE;
      94      105161 :   if (str == GRPC_MDSTR_GZIP) return GRPC_COMPRESS_GZIP;
      95           0 :   return GRPC_COMPRESS_ALGORITHMS_COUNT;
      96             : }
      97             : 
      98           0 : grpc_mdstr *grpc_compression_algorithm_mdstr(
      99             :     grpc_compression_algorithm algorithm) {
     100           0 :   switch (algorithm) {
     101             :     case GRPC_COMPRESS_NONE:
     102           0 :       return GRPC_MDSTR_IDENTITY;
     103             :     case GRPC_COMPRESS_DEFLATE:
     104           0 :       return GRPC_MDSTR_DEFLATE;
     105             :     case GRPC_COMPRESS_GZIP:
     106           0 :       return GRPC_MDSTR_GZIP;
     107             :     case GRPC_COMPRESS_ALGORITHMS_COUNT:
     108           0 :       return NULL;
     109             :   }
     110           0 :   return NULL;
     111             : }
     112             : 
     113     4333524 : grpc_mdelem *grpc_compression_encoding_mdelem(
     114             :     grpc_compression_algorithm algorithm) {
     115     4333524 :   switch (algorithm) {
     116             :     case GRPC_COMPRESS_NONE:
     117     4228126 :       return GRPC_MDELEM_GRPC_ENCODING_IDENTITY;
     118             :     case GRPC_COMPRESS_DEFLATE:
     119          32 :       return GRPC_MDELEM_GRPC_ENCODING_DEFLATE;
     120             :     case GRPC_COMPRESS_GZIP:
     121      105193 :       return GRPC_MDELEM_GRPC_ENCODING_GZIP;
     122             :     case GRPC_COMPRESS_ALGORITHMS_COUNT:
     123           0 :       return NULL;
     124             :   }
     125           0 :   return NULL;
     126             : }
     127             : 
     128             : /* TODO(dgq): Add the ability to specify parameters to the individual
     129             :  * compression algorithms */
     130           0 : grpc_compression_algorithm grpc_compression_algorithm_for_level(
     131             :     grpc_compression_level level) {
     132           0 :   GRPC_API_TRACE("grpc_compression_algorithm_for_level(level=%d)", 1,
     133             :                  ((int)level));
     134           0 :   switch (level) {
     135             :     case GRPC_COMPRESS_LEVEL_NONE:
     136           0 :       return GRPC_COMPRESS_NONE;
     137             :     case GRPC_COMPRESS_LEVEL_LOW:
     138             :     case GRPC_COMPRESS_LEVEL_MED:
     139             :     case GRPC_COMPRESS_LEVEL_HIGH:
     140           0 :       return GRPC_COMPRESS_DEFLATE;
     141             :     default:
     142             :       /* we shouldn't be making it here */
     143           0 :       abort();
     144             :       return GRPC_COMPRESS_NONE;
     145             :   }
     146             : }
     147             : 
     148           0 : grpc_compression_level grpc_compression_level_for_algorithm(
     149             :     grpc_compression_algorithm algorithm) {
     150             :   grpc_compression_level clevel;
     151           0 :   GRPC_API_TRACE("grpc_compression_level_for_algorithm(algorithm=%d)", 1,
     152             :                  ((int)algorithm));
     153           0 :   for (clevel = GRPC_COMPRESS_LEVEL_NONE; clevel < GRPC_COMPRESS_LEVEL_COUNT;
     154           0 :        ++clevel) {
     155           0 :     if (grpc_compression_algorithm_for_level(clevel) == algorithm) {
     156           0 :       return clevel;
     157             :     }
     158             :   }
     159           0 :   abort();
     160             :   return GRPC_COMPRESS_LEVEL_NONE;
     161             : }
     162             : 
     163        5920 : void grpc_compression_options_init(grpc_compression_options *opts) {
     164        5920 :   opts->enabled_algorithms_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
     165        5920 :   opts->default_compression_algorithm = GRPC_COMPRESS_NONE;
     166        5920 : }
     167             : 
     168           0 : void grpc_compression_options_enable_algorithm(
     169             :     grpc_compression_options *opts, grpc_compression_algorithm algorithm) {
     170           0 :   GPR_BITSET(&opts->enabled_algorithms_bitset, algorithm);
     171           0 : }
     172             : 
     173           0 : void grpc_compression_options_disable_algorithm(
     174             :     grpc_compression_options *opts, grpc_compression_algorithm algorithm) {
     175           0 :   GPR_BITCLEAR(&opts->enabled_algorithms_bitset, algorithm);
     176           0 : }
     177             : 
     178       23411 : int grpc_compression_options_is_algorithm_enabled(
     179             :     const grpc_compression_options *opts,
     180             :     grpc_compression_algorithm algorithm) {
     181       23411 :   return GPR_BITGET(opts->enabled_algorithms_bitset, algorithm);
     182             : }

Generated by: LCOV version 1.11