LCOV - code coverage report
Current view: top level - src - data-mem.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 78 117 66.7 %
Date: 2015-11-05 17:14:26 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /* data-mem.c - A memory based data object.
       2             :    Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH
       3             : 
       4             :    This file is part of GPGME.
       5             : 
       6             :    GPGME is free software; you can redistribute it and/or modify it
       7             :    under the terms of the GNU Lesser General Public License as
       8             :    published by the Free Software Foundation; either version 2.1 of
       9             :    the License, or (at your option) any later version.
      10             : 
      11             :    GPGME is distributed in the hope that it will be useful, but
      12             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :    Lesser General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU Lesser General Public
      17             :    License along with this program; if not, write to the Free Software
      18             :    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
      19             :    02111-1307, USA.  */
      20             : 
      21             : #if HAVE_CONFIG_H
      22             : #include <config.h>
      23             : #endif
      24             : 
      25             : #include <errno.h>
      26             : #include <stdlib.h>
      27             : #ifdef HAVE_UNISTD_H
      28             : # include <unistd.h>
      29             : #endif
      30             : #include <assert.h>
      31             : #include <string.h>
      32             : 
      33             : #include "data.h"
      34             : #include "util.h"
      35             : #include "debug.h"
      36             : 
      37             : 
      38             : static gpgme_ssize_t
      39         473 : mem_read (gpgme_data_t dh, void *buffer, size_t size)
      40             : {
      41         473 :   size_t amt = dh->data.mem.length - dh->data.mem.offset;
      42             :   const char *src;
      43             : 
      44         473 :   if (!amt)
      45         134 :     return 0;
      46             : 
      47         339 :   if (size < amt)
      48         206 :     amt = size;
      49             : 
      50         339 :   src = dh->data.mem.buffer ? dh->data.mem.buffer : dh->data.mem.orig_buffer;
      51         339 :   memcpy (buffer, src + dh->data.mem.offset, amt);
      52         339 :   dh->data.mem.offset += amt;
      53         339 :   return amt;
      54             : }
      55             : 
      56             : 
      57             : static gpgme_ssize_t
      58          82 : mem_write (gpgme_data_t dh, const void *buffer, size_t size)
      59             : {
      60             :   size_t unused;
      61             : 
      62          82 :   if (!dh->data.mem.buffer && dh->data.mem.orig_buffer)
      63             :     {
      64           1 :       size_t new_size = dh->data.mem.size;
      65             :       char *new_buffer;
      66             : 
      67           1 :       if (new_size < dh->data.mem.offset + size)
      68           1 :         new_size = dh->data.mem.offset + size;
      69             : 
      70           1 :       new_buffer = malloc (new_size);
      71           1 :       if (!new_buffer)
      72           0 :         return -1;
      73           1 :       memcpy (new_buffer, dh->data.mem.orig_buffer, dh->data.mem.length);
      74             : 
      75           1 :       dh->data.mem.buffer = new_buffer;
      76           1 :       dh->data.mem.size = new_size;
      77             :     }
      78             : 
      79          82 :   unused = dh->data.mem.size - dh->data.mem.offset;
      80          82 :   if (unused < size)
      81             :     {
      82             :       /* Allocate a large enough buffer with exponential backoff.  */
      83             : #define INITIAL_ALLOC 512
      84         156 :       size_t new_size = dh->data.mem.size
      85          78 :         ? (2 * dh->data.mem.size) : INITIAL_ALLOC;
      86             :       char *new_buffer;
      87             : 
      88          78 :       if (new_size < dh->data.mem.offset + size)
      89          32 :         new_size = dh->data.mem.offset + size;
      90             : 
      91          78 :       new_buffer = realloc (dh->data.mem.buffer, new_size);
      92          78 :       if (!new_buffer && new_size > dh->data.mem.offset + size)
      93             :         {
      94             :           /* Maybe we were too greedy, try again.  */
      95           0 :           new_size = dh->data.mem.offset + size;
      96           0 :           new_buffer = realloc (dh->data.mem.buffer, new_size);
      97             :         }
      98          78 :       if (!new_buffer)
      99           0 :         return -1;
     100          78 :       dh->data.mem.buffer = new_buffer;
     101          78 :       dh->data.mem.size = new_size;
     102             :     }
     103             : 
     104          82 :   memcpy (dh->data.mem.buffer + dh->data.mem.offset, buffer, size);
     105          82 :   dh->data.mem.offset += size;
     106          82 :   if (dh->data.mem.length < dh->data.mem.offset)
     107          82 :     dh->data.mem.length = dh->data.mem.offset;
     108          82 :   return size;
     109             : }
     110             : 
     111             : 
     112             : static gpgme_off_t
     113          62 : mem_seek (gpgme_data_t dh, gpgme_off_t offset, int whence)
     114             : {
     115          62 :   switch (whence)
     116             :     {
     117             :     case SEEK_SET:
     118          62 :       if (offset < 0 || offset > dh->data.mem.length)
     119             :         {
     120           0 :           gpg_err_set_errno (EINVAL);
     121           0 :           return -1;
     122             :         }
     123          62 :       dh->data.mem.offset = offset;
     124          62 :       break;
     125             :     case SEEK_CUR:
     126           0 :       if ((offset > 0 && dh->data.mem.length - dh->data.mem.offset < offset)
     127           0 :           || (offset < 0 && dh->data.mem.offset < -offset))
     128             :         {
     129           0 :           gpg_err_set_errno (EINVAL);
     130           0 :           return -1;
     131             :         }
     132           0 :       dh->data.mem.offset += offset;
     133           0 :       break;
     134             :     case SEEK_END:
     135           0 :       if (offset > 0 || -offset > dh->data.mem.length)
     136             :         {
     137           0 :           gpg_err_set_errno (EINVAL);
     138           0 :           return -1;
     139             :         }
     140           0 :       dh->data.mem.offset = dh->data.mem.length + offset;
     141           0 :       break;
     142             :     default:
     143           0 :       gpg_err_set_errno (EINVAL);
     144           0 :       return -1;
     145             :     }
     146          62 :   return dh->data.mem.offset;
     147             : }
     148             : 
     149             : 
     150             : static void
     151         145 : mem_release (gpgme_data_t dh)
     152             : {
     153         145 :   if (dh->data.mem.buffer)
     154         102 :     free (dh->data.mem.buffer);
     155         145 : }
     156             : 
     157             : 
     158             : static struct _gpgme_data_cbs mem_cbs =
     159             :   {
     160             :     mem_read,
     161             :     mem_write,
     162             :     mem_seek,
     163             :     mem_release,
     164             :     NULL
     165             :   };
     166             : 
     167             : 
     168             : /* Create a new data buffer and return it in R_DH.  */
     169             : gpgme_error_t
     170         103 : gpgme_data_new (gpgme_data_t *r_dh)
     171             : {
     172             :   gpgme_error_t err;
     173         103 :   TRACE_BEG (DEBUG_DATA, "gpgme_data_new", r_dh);
     174             : 
     175         103 :   err = _gpgme_data_new (r_dh, &mem_cbs);
     176             : 
     177         103 :   if (err)
     178           1 :     return TRACE_ERR (err);
     179             : 
     180         102 :   return TRACE_SUC1 ("dh=%p", *r_dh);
     181             : }
     182             : 
     183             : 
     184             : /* Create a new data buffer filled with SIZE bytes starting from
     185             :    BUFFER.  If COPY is zero, copying is delayed until necessary, and
     186             :    the data is taken from the original location when needed.  */
     187             : gpgme_error_t
     188          43 : gpgme_data_new_from_mem (gpgme_data_t *r_dh, const char *buffer,
     189             :                          size_t size, int copy)
     190             : {
     191             :   gpgme_error_t err;
     192          43 :   TRACE_BEG4 (DEBUG_DATA, "gpgme_data_new_from_mem", r_dh,
     193             :               "buffer=%p, size=%u, copy=%i (%s)", buffer, size,
     194             :               copy, copy ? "yes" : "no");
     195             : 
     196          43 :   err = _gpgme_data_new (r_dh, &mem_cbs);
     197          43 :   if (err)
     198           0 :     return TRACE_ERR (err);
     199             : 
     200          43 :   if (copy)
     201             :     {
     202           1 :       char *bufcpy = malloc (size);
     203           1 :       if (!bufcpy)
     204             :         {
     205           0 :           int saved_err = gpg_error_from_syserror ();
     206           0 :           _gpgme_data_release (*r_dh);
     207           0 :           return TRACE_ERR (saved_err);
     208             :         }
     209           1 :       memcpy (bufcpy, buffer, size);
     210           1 :       (*r_dh)->data.mem.buffer = bufcpy;
     211             :     }
     212             :   else
     213          42 :     (*r_dh)->data.mem.orig_buffer = buffer;
     214             : 
     215          43 :   (*r_dh)->data.mem.size = size;
     216          43 :   (*r_dh)->data.mem.length = size;
     217          43 :   return TRACE_SUC1 ("dh=%p", *r_dh);
     218             : }
     219             : 
     220             : 
     221             : /* Destroy the data buffer DH and return a pointer to its content.
     222             :    The memory has be to released with gpgme_free() by the user.  It's
     223             :    size is returned in R_LEN.  */
     224             : char *
     225           1 : gpgme_data_release_and_get_mem (gpgme_data_t dh, size_t *r_len)
     226             : {
     227           1 :   char *str = NULL;
     228             : 
     229           1 :   TRACE_BEG1 (DEBUG_DATA, "gpgme_data_release_and_get_mem", dh,
     230             :               "r_len=%p", r_len);
     231             : 
     232           1 :   if (!dh || dh->cbs != &mem_cbs)
     233             :     {
     234           0 :       gpgme_data_release (dh);
     235           0 :       TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
     236           0 :       return NULL;
     237             :     }
     238             : 
     239           1 :   str = dh->data.mem.buffer;
     240           1 :   if (!str && dh->data.mem.orig_buffer)
     241             :     {
     242           0 :       str = malloc (dh->data.mem.length);
     243           0 :       if (!str)
     244             :         {
     245           0 :           int saved_err = gpg_error_from_syserror ();
     246           0 :           gpgme_data_release (dh);
     247           0 :           TRACE_ERR (saved_err);
     248           0 :           return NULL;
     249             :         }
     250           0 :       memcpy (str, dh->data.mem.orig_buffer, dh->data.mem.length);
     251             :     }
     252             :   else
     253             :     /* Prevent mem_release from releasing the buffer memory.  We must
     254             :        not fail from this point.  */
     255           1 :     dh->data.mem.buffer = NULL;
     256             : 
     257           1 :   if (r_len)
     258           1 :     *r_len = dh->data.mem.length;
     259             : 
     260           1 :   gpgme_data_release (dh);
     261             : 
     262           1 :   if (r_len)
     263             :     {
     264           1 :       TRACE_SUC2 ("buffer=%p, len=%u", str, *r_len);
     265             :     }
     266             :   else
     267             :     {
     268           0 :       TRACE_SUC1 ("buffer=%p", str);
     269             :     }
     270           1 :   return str;
     271             : }
     272             : 
     273             : 
     274             : /* Release the memory returned by gpgme_data_release_and_get_mem() and
     275             :    some other functions.  */
     276             : void
     277           0 : gpgme_free (void *buffer)
     278             : {
     279           0 :   TRACE (DEBUG_DATA, "gpgme_free", buffer);
     280             : 
     281           0 :   if (buffer)
     282           0 :     free (buffer);
     283           0 : }

Generated by: LCOV version 1.11