LCOV - code coverage report
Current view: top level - core/surface - byte_buffer_reader.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 47 47 100.0 %
Date: 2015-12-10 22:15:08 Functions: 5 5 100.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 <string.h>
      35             : #include <grpc/byte_buffer_reader.h>
      36             : 
      37             : #include <grpc/compression.h>
      38             : #include <grpc/grpc.h>
      39             : #include <grpc/support/alloc.h>
      40             : #include <grpc/support/log.h>
      41             : #include <grpc/support/slice_buffer.h>
      42             : #include <grpc/byte_buffer.h>
      43             : 
      44             : #include "src/core/compression/message_compress.h"
      45             : 
      46     7498820 : static int is_compressed(grpc_byte_buffer *buffer) {
      47     7499005 :   switch (buffer->type) {
      48             :     case GRPC_BB_RAW:
      49     7499005 :       if (buffer->data.raw.compression == GRPC_COMPRESS_NONE) {
      50     7498684 :         return 0 /* GPR_FALSE */;
      51             :       }
      52         136 :       break;
      53             :   }
      54         136 :   return 1 /* GPR_TRUE */;
      55             : }
      56             : 
      57     3754412 : void grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader,
      58             :                                   grpc_byte_buffer *buffer) {
      59             :   gpr_slice_buffer decompressed_slices_buffer;
      60     3754412 :   reader->buffer_in = buffer;
      61     3754412 :   switch (reader->buffer_in->type) {
      62             :     case GRPC_BB_RAW:
      63     3756424 :       gpr_slice_buffer_init(&decompressed_slices_buffer);
      64     3756515 :       if (is_compressed(reader->buffer_in)) {
      65          68 :         grpc_msg_decompress(reader->buffer_in->data.raw.compression,
      66          68 :                             &reader->buffer_in->data.raw.slice_buffer,
      67             :                             &decompressed_slices_buffer);
      68          68 :         reader->buffer_out =
      69          68 :             grpc_raw_byte_buffer_create(decompressed_slices_buffer.slices,
      70             :                                         decompressed_slices_buffer.count);
      71          68 :         gpr_slice_buffer_destroy(&decompressed_slices_buffer);
      72             :       } else { /* not compressed, use the input buffer as output */
      73     3756250 :         reader->buffer_out = reader->buffer_in;
      74             :       }
      75     3756344 :       reader->current.index = 0;
      76     3756344 :       break;
      77             :   }
      78     3754332 : }
      79             : 
      80     3754617 : void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader) {
      81     3754617 :   switch (reader->buffer_in->type) {
      82             :     case GRPC_BB_RAW:
      83             :       /* keeping the same if-else structure as in the init function */
      84     3757492 :       if (is_compressed(reader->buffer_in)) {
      85          68 :         grpc_byte_buffer_destroy(reader->buffer_out);
      86             :       }
      87     3757487 :       break;
      88             :   }
      89     3754612 : }
      90             : 
      91     7522524 : int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
      92             :                                  gpr_slice *slice) {
      93     7522524 :   switch (reader->buffer_in->type) {
      94             :     case GRPC_BB_RAW: {
      95             :       gpr_slice_buffer *slice_buffer;
      96     7520683 :       slice_buffer = &reader->buffer_out->data.raw.slice_buffer;
      97     7520683 :       if (reader->current.index < slice_buffer->count) {
      98     2009474 :         *slice = gpr_slice_ref(slice_buffer->slices[reader->current.index]);
      99     2009535 :         reader->current.index += 1;
     100     2009535 :         return 1;
     101             :       }
     102     5511024 :       break;
     103             :     }
     104             :   }
     105     5512865 :   return 0;
     106             : }
     107             : 
     108           2 : gpr_slice grpc_byte_buffer_reader_readall(grpc_byte_buffer_reader *reader) {
     109             :   gpr_slice in_slice;
     110           2 :   size_t bytes_read = 0;
     111           2 :   const size_t input_size = grpc_byte_buffer_length(reader->buffer_out);
     112           2 :   gpr_slice out_slice = gpr_slice_malloc(input_size);
     113           2 :   gpr_uint8 *const outbuf = GPR_SLICE_START_PTR(out_slice); /* just an alias */
     114             : 
     115           8 :   while (grpc_byte_buffer_reader_next(reader, &in_slice) != 0) {
     116           4 :     const size_t slice_length = GPR_SLICE_LENGTH(in_slice);
     117           4 :     memcpy(&(outbuf[bytes_read]), GPR_SLICE_START_PTR(in_slice), slice_length);
     118           4 :     bytes_read += slice_length;
     119           4 :     gpr_slice_unref(in_slice);
     120           4 :     GPR_ASSERT(bytes_read <= input_size);
     121             :   }
     122           2 :   return out_slice;
     123             : }

Generated by: LCOV version 1.11