LCOV - code coverage report
Current view: top level - src - data-mem.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 88 129 68.2 %
Date: 2018-11-14 16:53:58 Functions: 8 8 100.0 %

          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        1123 : mem_read (gpgme_data_t dh, void *buffer, size_t size)
      40             : {
      41        1123 :   size_t amt = dh->data.mem.length - dh->data.mem.offset;
      42             :   const char *src;
      43             : 
      44        1123 :   if (!amt)
      45         463 :     return 0;
      46             : 
      47         660 :   if (size < amt)
      48         219 :     amt = size;
      49             : 
      50         660 :   src = dh->data.mem.buffer ? dh->data.mem.buffer : dh->data.mem.orig_buffer;
      51         660 :   memcpy (buffer, src + dh->data.mem.offset, amt);
      52         660 :   dh->data.mem.offset += amt;
      53         660 :   return amt;
      54             : }
      55             : 
      56             : 
      57             : static gpgme_ssize_t
      58        1723 : mem_write (gpgme_data_t dh, const void *buffer, size_t size)
      59             : {
      60             :   size_t unused;
      61             : 
      62        1723 :   if (!dh->data.mem.buffer && dh->data.mem.orig_buffer)
      63             :     {
      64           2 :       size_t new_size = dh->data.mem.size;
      65             :       char *new_buffer;
      66             : 
      67           2 :       if (new_size < dh->data.mem.offset + size)
      68           1 :         new_size = dh->data.mem.offset + size;
      69             : 
      70           2 :       new_buffer = malloc (new_size);
      71           2 :       if (!new_buffer)
      72           0 :         return -1;
      73           2 :       memcpy (new_buffer, dh->data.mem.orig_buffer, dh->data.mem.length);
      74             : 
      75           2 :       dh->data.mem.buffer = new_buffer;
      76           2 :       dh->data.mem.size = new_size;
      77             :     }
      78             : 
      79        1723 :   unused = dh->data.mem.size - dh->data.mem.offset;
      80        1723 :   if (unused < size)
      81             :     {
      82             :       /* Allocate a large enough buffer with exponential backoff.  */
      83             : #define INITIAL_ALLOC 512
      84        1292 :       size_t new_size = dh->data.mem.size
      85         646 :         ? (2 * dh->data.mem.size) : INITIAL_ALLOC;
      86             :       char *new_buffer;
      87             : 
      88         646 :       if (new_size < dh->data.mem.offset + size)
      89          55 :         new_size = dh->data.mem.offset + size;
      90             : 
      91         646 :       new_buffer = realloc (dh->data.mem.buffer, new_size);
      92         646 :       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         646 :       if (!new_buffer)
      99           0 :         return -1;
     100         646 :       dh->data.mem.buffer = new_buffer;
     101         646 :       dh->data.mem.size = new_size;
     102             :     }
     103             : 
     104        1723 :   memcpy (dh->data.mem.buffer + dh->data.mem.offset, buffer, size);
     105        1723 :   dh->data.mem.offset += size;
     106        1723 :   if (dh->data.mem.length < dh->data.mem.offset)
     107        1722 :     dh->data.mem.length = dh->data.mem.offset;
     108        1723 :   return size;
     109             : }
     110             : 
     111             : 
     112             : static gpgme_off_t
     113         222 : mem_seek (gpgme_data_t dh, gpgme_off_t offset, int whence)
     114             : {
     115         222 :   switch (whence)
     116             :     {
     117             :     case SEEK_SET:
     118         222 :       if (offset < 0 || offset > dh->data.mem.length)
     119             :         {
     120           0 :           gpg_err_set_errno (EINVAL);
     121           0 :           return -1;
     122             :         }
     123         222 :       dh->data.mem.offset = offset;
     124         222 :       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         222 :   return dh->data.mem.offset;
     147             : }
     148             : 
     149             : 
     150             : static void
     151        1290 : mem_release (gpgme_data_t dh)
     152             : {
     153        1290 :   if (dh->data.mem.buffer)
     154         656 :     free (dh->data.mem.buffer);
     155        1290 : }
     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        1135 : gpgme_data_new (gpgme_data_t *r_dh)
     171             : {
     172             :   gpgme_error_t err;
     173        1135 :   TRACE_BEG (DEBUG_DATA, "gpgme_data_new", r_dh);
     174             : 
     175        1138 :   err = _gpgme_data_new (r_dh, &mem_cbs);
     176             : 
     177        1139 :   if (err)
     178           1 :     return TRACE_ERR (err);
     179             : 
     180        1138 :   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         193 : 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         193 :   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         193 :   err = _gpgme_data_new (r_dh, &mem_cbs);
     197         193 :   if (err)
     198           0 :     return TRACE_ERR (err);
     199             : 
     200         193 :   if (copy)
     201             :     {
     202          53 :       char *bufcpy = malloc (size);
     203          53 :       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          53 :       memcpy (bufcpy, buffer, size);
     210          53 :       (*r_dh)->data.mem.buffer = bufcpy;
     211             :     }
     212             :   else
     213         140 :     (*r_dh)->data.mem.orig_buffer = buffer;
     214             : 
     215         193 :   (*r_dh)->data.mem.size = size;
     216         193 :   (*r_dh)->data.mem.length = size;
     217         193 :   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           3 : gpgme_data_release_and_get_mem (gpgme_data_t dh, size_t *r_len)
     226             : {
     227             :   gpg_error_t err;
     228           3 :   char *str = NULL;
     229             :   size_t len;
     230             :   int blankout;
     231             : 
     232           3 :   TRACE_BEG1 (DEBUG_DATA, "gpgme_data_release_and_get_mem", dh,
     233             :               "r_len=%p", r_len);
     234             : 
     235           3 :   if (!dh || dh->cbs != &mem_cbs)
     236             :     {
     237           0 :       gpgme_data_release (dh);
     238           0 :       TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
     239           0 :       return NULL;
     240             :     }
     241             : 
     242           3 :   err = _gpgme_data_get_prop (dh, 0, DATA_PROP_BLANKOUT, &blankout);
     243           3 :   if (err)
     244             :     {
     245           0 :       gpgme_data_release (dh);
     246           0 :       TRACE_ERR (err);
     247           0 :       return NULL;
     248             :     }
     249             : 
     250           3 :   str = dh->data.mem.buffer;
     251           3 :   len = dh->data.mem.length;
     252           3 :   if (blankout && len)
     253           0 :     len = 1;
     254             : 
     255           3 :   if (!str && dh->data.mem.orig_buffer)
     256             :     {
     257           0 :       str = malloc (len);
     258           0 :       if (!str)
     259             :         {
     260           0 :           int saved_err = gpg_error_from_syserror ();
     261           0 :           gpgme_data_release (dh);
     262           0 :           TRACE_ERR (saved_err);
     263           0 :           return NULL;
     264             :         }
     265           0 :       if (blankout)
     266           0 :         memset (str, 0, len);
     267             :       else
     268           0 :         memcpy (str, dh->data.mem.orig_buffer, len);
     269             :     }
     270             :   else
     271             :     {
     272           3 :       if (blankout && len)
     273           0 :         *str = 0;
     274             :       /* Prevent mem_release from releasing the buffer memory.  We
     275             :        * must not fail from this point.  */
     276           3 :       dh->data.mem.buffer = NULL;
     277             :     }
     278             : 
     279           3 :   if (r_len)
     280           3 :     *r_len = len;
     281             : 
     282           3 :   gpgme_data_release (dh);
     283             : 
     284           3 :   if (r_len)
     285             :     {
     286           3 :       TRACE_SUC2 ("buffer=%p, len=%u", str, *r_len);
     287             :     }
     288             :   else
     289             :     {
     290           0 :       TRACE_SUC1 ("buffer=%p", str);
     291             :     }
     292           3 :   return str;
     293             : }
     294             : 
     295             : 
     296             : /* Release the memory returned by gpgme_data_release_and_get_mem() and
     297             :    some other functions.  */
     298             : void
     299          20 : gpgme_free (void *buffer)
     300             : {
     301          20 :   TRACE (DEBUG_DATA, "gpgme_free", buffer);
     302             : 
     303          20 :   if (buffer)
     304          20 :     free (buffer);
     305          20 : }

Generated by: LCOV version 1.13