LCOV - code coverage report
Current view: top level - src - data-compat.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 92 32.6 %
Date: 2016-11-29 15:07:43 Functions: 2 7 28.6 %

          Line data    Source code
       1             : /* data-compat.c - Compatibility interfaces for data objects.
       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             : #ifdef HAVE_SYS_TIME_H
      27             : # include <sys/time.h>
      28             : #endif
      29             : #ifdef HAVE_SYS_STAT_H
      30             : # include <sys/stat.h>
      31             : #endif
      32             : #include <stdlib.h>
      33             : 
      34             : #include "data.h"
      35             : #include "util.h"
      36             : #include "debug.h"
      37             : 
      38             : 
      39             : /* Create a new data buffer filled with LENGTH bytes starting from
      40             :    OFFSET within the file FNAME or stream STREAM (exactly one must be
      41             :    non-zero).  */
      42             : gpgme_error_t
      43          44 : gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname,
      44             :                               FILE *stream, gpgme_off_t offset, size_t length)
      45             : {
      46             : #if defined (HAVE_W32CE_SYSTEM) && defined (_MSC_VER)
      47             :   return gpgme_error (GPG_ERR_NOT_IMPLEMENTED);
      48             : #else
      49             :   gpgme_error_t err;
      50          44 :   char *buf = NULL;
      51             :   int res;
      52             : 
      53          44 :   TRACE_BEG4 (DEBUG_DATA, "gpgme_data_new_from_filepart", r_dh,
      54             :               "file_name=%s, stream=%p, offset=%lli, length=%u",
      55             :               fname, stream, offset, length);
      56             : 
      57          44 :   if (stream && fname)
      58           0 :     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
      59             : 
      60          44 :   if (fname)
      61          41 :     stream = fopen (fname, "rb");
      62          44 :   if (!stream)
      63           1 :     return TRACE_ERR (gpg_error_from_syserror ());
      64             : 
      65             : #ifdef HAVE_FSEEKO
      66          43 :   res = fseeko (stream, offset, SEEK_SET);
      67             : #else
      68             :   /* FIXME: Check for overflow, or at least bail at compilation.  */
      69             :   res = fseek (stream, offset, SEEK_SET);
      70             : #endif
      71             : 
      72          43 :   if (res)
      73             :     {
      74           0 :       int saved_err = gpg_error_from_syserror ();
      75           0 :       if (fname)
      76           0 :         fclose (stream);
      77           0 :       return TRACE_ERR (saved_err);
      78             :     }
      79             : 
      80          43 :   buf = malloc (length);
      81          43 :   if (!buf)
      82             :     {
      83           0 :       int saved_err = gpg_error_from_syserror ();
      84           0 :       if (fname)
      85           0 :         fclose (stream);
      86           0 :       return TRACE_ERR (saved_err);
      87             :     }
      88             : 
      89          43 :   while (fread (buf, length, 1, stream) < 1
      90           0 :          && ferror (stream) && errno == EINTR);
      91          43 :   if (ferror (stream))
      92             :     {
      93           0 :       int saved_err = gpg_error_from_syserror ();
      94           0 :       if (buf)
      95           0 :         free (buf);
      96           0 :       if (fname)
      97           0 :         fclose (stream);
      98           0 :       return TRACE_ERR (saved_err);
      99             :     }
     100             : 
     101          43 :   if (fname)
     102          40 :     fclose (stream);
     103             : 
     104          43 :   err = gpgme_data_new (r_dh);
     105          43 :   if (err)
     106             :     {
     107           0 :       if (buf)
     108           0 :         free (buf);
     109           0 :       return err;
     110             :     }
     111             : 
     112          43 :   (*r_dh)->data.mem.buffer = buf;
     113          43 :   (*r_dh)->data.mem.size = length;
     114          43 :   (*r_dh)->data.mem.length = length;
     115             : 
     116          43 :   return TRACE_SUC1 ("r_dh=%p", *r_dh);
     117             : #endif
     118             : }
     119             : 
     120             : 
     121             : /* Create a new data buffer filled with the content of file FNAME.
     122             :    COPY must be non-zero (delayed reads are not supported yet).  */
     123             : gpgme_error_t
     124          39 : gpgme_data_new_from_file (gpgme_data_t *r_dh, const char *fname, int copy)
     125             : {
     126             : #if defined (HAVE_W32CE_SYSTEM) && defined (_MSC_VER)
     127             :   return gpgme_error (GPG_ERR_NOT_IMPLEMENTED);
     128             : #else
     129             :   gpgme_error_t err;
     130             :   struct stat statbuf;
     131          39 :   TRACE_BEG3 (DEBUG_DATA, "gpgme_data_new_from_file", r_dh,
     132             :               "file_name=%s, copy=%i (%s)", fname, copy, copy ? "yes" : "no");
     133             : 
     134          39 :   if (!fname || !copy)
     135           1 :     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
     136             : 
     137          38 :   if (stat (fname, &statbuf) < 0)
     138           1 :     return TRACE_ERR (gpg_error_from_syserror ());
     139             : 
     140          37 :   err = gpgme_data_new_from_filepart (r_dh, fname, NULL, 0, statbuf.st_size);
     141          37 :   return TRACE_ERR (err);
     142             : #endif
     143             : }
     144             : 
     145             : 
     146             : static int
     147           0 : gpgme_error_to_errno (gpgme_error_t err)
     148             : {
     149           0 :   int res = gpg_err_code_to_errno (gpg_err_code (err));
     150             : 
     151           0 :   if (!err)
     152             :     {
     153           0 :       switch (gpg_err_code (err))
     154             :         {
     155             :         case GPG_ERR_EOF:
     156           0 :           res = 0;
     157           0 :           break;
     158             :         case GPG_ERR_INV_VALUE:
     159           0 :           res = EINVAL;
     160           0 :           break;
     161             :         case GPG_ERR_NOT_SUPPORTED:
     162           0 :           res = ENOSYS;
     163           0 :           break;
     164             :         default:
     165             :           /* FIXME: Yeah, well.  */
     166           0 :           res = EINVAL;
     167           0 :           break;
     168             :         }
     169             :     }
     170           0 :   TRACE3 (DEBUG_DATA, "gpgme:gpgme_error_to_errno", 0,
     171             :           "mapping %s <%s> to: %s", gpgme_strerror (err),
     172             :           gpgme_strsource (err), strerror (res));
     173           0 :   gpg_err_set_errno (res);
     174           0 :   return res ? -1 : 0;
     175             : }
     176             : 
     177             : 
     178             : static gpgme_ssize_t
     179           0 : old_user_read (gpgme_data_t dh, void *buffer, size_t size)
     180             : {
     181             :   gpgme_error_t err;
     182             :   size_t amt;
     183           0 :   TRACE_BEG2 (DEBUG_DATA, "gpgme:old_user_read", dh,
     184             :               "buffer=%p, size=%u", buffer, size);
     185             : 
     186           0 :   err = (*dh->data.old_user.cb) (dh->data.old_user.handle,
     187             :                                  buffer, size, &amt);
     188           0 :   if (err)
     189           0 :     return TRACE_SYSRES (gpgme_error_to_errno (err));
     190           0 :   return TRACE_SYSRES ((gpgme_ssize_t)amt);
     191             : }
     192             : 
     193             : 
     194             : static gpgme_off_t
     195           0 : old_user_seek (gpgme_data_t dh, gpgme_off_t offset, int whence)
     196             : {
     197             :   gpgme_error_t err;
     198           0 :   TRACE_BEG2 (DEBUG_DATA, "gpgme:old_user_seek", dh,
     199             :               "offset=%llu, whence=%i", offset, whence);
     200             : 
     201           0 :   if (whence != SEEK_SET || offset)
     202             :     {
     203           0 :       gpg_err_set_errno (EINVAL);
     204           0 :       return TRACE_SYSRES (-1);
     205             :     }
     206           0 :   err = (*dh->data.old_user.cb) (dh->data.old_user.handle, NULL, 0, NULL);
     207           0 :   if (err)
     208           0 :     return TRACE_SYSRES (gpgme_error_to_errno (err));
     209           0 :   return TRACE_SYSRES (0);
     210             : }
     211             : 
     212             : 
     213             : static struct _gpgme_data_cbs old_user_cbs =
     214             :   {
     215             :     old_user_read,
     216             :     NULL,
     217             :     old_user_seek,
     218             :     NULL
     219             :   };
     220             : 
     221             : 
     222             : /* Create a new data buffer which retrieves the data from the callback
     223             :    function READ_CB.  */
     224             : gpgme_error_t
     225           0 : gpgme_data_new_with_read_cb (gpgme_data_t *r_dh,
     226             :                              int (*read_cb) (void *, char *, size_t, size_t *),
     227             :                              void *read_cb_value)
     228             : {
     229             :   gpgme_error_t err;
     230           0 :   TRACE_BEG2 (DEBUG_DATA, "gpgme_data_new_with_read_cb", r_dh,
     231             :               "read_cb=%p/%p", read_cb, read_cb_value);
     232             : 
     233           0 :   err = _gpgme_data_new (r_dh, &old_user_cbs);
     234             : 
     235           0 :   if (err)
     236           0 :     return TRACE_ERR (err);
     237             : 
     238           0 :   (*r_dh)->data.old_user.cb = read_cb;
     239           0 :   (*r_dh)->data.old_user.handle = read_cb_value;
     240           0 :   return TRACE_ERR (0);
     241             : }
     242             : 
     243             : 
     244             : gpgme_error_t
     245           0 : gpgme_data_rewind (gpgme_data_t dh)
     246             : {
     247             :   gpgme_error_t err;
     248           0 :   TRACE_BEG (DEBUG_DATA, "gpgme_data_rewind", dh);
     249             : 
     250           0 :   err = ((gpgme_data_seek (dh, 0, SEEK_SET) == -1)
     251           0 :          ? gpg_error_from_syserror () : 0);
     252             : 
     253           0 :   return TRACE_ERR (err);
     254             : }

Generated by: LCOV version 1.11