diff --git a/src/estream.c b/src/estream.c index 8ad664e..72e93e0 100644 --- a/src/estream.c +++ b/src/estream.c @@ -1,5151 +1,5182 @@ /* estream.c - Extended Stream I/O Library * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011, * 2014, 2015, 2016, 2017 g10 Code GmbH * * This file is part of Libestream. * * Libestream is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Libestream is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Libestream; if not, see . * * ALTERNATIVELY, Libestream may be distributed under the terms of the * following license, in which case the provisions of this license are * required INSTEAD OF the GNU General Public License. If you wish to * allow use of your version of this file only under the terms of the * GNU General Public License, and not to allow others to use your * version of this file under the terms of the following license, * indicate your decision by deleting this paragraph and the license * below. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, and the entire permission notice in its entirety, * including the disclaimer of warranties. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef USE_ESTREAM_SUPPORT_H # include #endif #ifdef HAVE_CONFIG_H # include #endif #if defined(_WIN32) && !defined(HAVE_W32_SYSTEM) # define HAVE_W32_SYSTEM 1 # if defined(__MINGW32CE__) && !defined (HAVE_W32CE_SYSTEM) # define HAVE_W32CE_SYSTEM # endif #endif #ifdef HAVE_SYS_SELECT_H # include #endif #ifdef HAVE_SYS_TIME_H # include #endif #include #include #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_W32_SYSTEM # ifdef HAVE_WINSOCK2_H # include # endif # include #endif /* Enable tracing. The value is the module name to be printed. */ /*#define ENABLE_TRACING "estream"*/ #include "gpgrt-int.h" #include "estream-printf.h" #include "thread.h" #include "lock.h" #ifndef O_BINARY # define O_BINARY 0 #endif #ifndef HAVE_DOSISH_SYSTEM # ifdef HAVE_W32_SYSTEM # define HAVE_DOSISH_SYSTEM 1 # endif #endif #ifdef HAVE_W32_SYSTEM # ifndef S_IRGRP # define S_IRGRP S_IRUSR # endif # ifndef S_IROTH # define S_IROTH S_IRUSR # endif # ifndef S_IWGRP # define S_IWGRP S_IWUSR # endif # ifndef S_IWOTH # define S_IWOTH S_IWUSR # endif # ifndef S_IXGRP # define S_IXGRP S_IXUSR # endif # ifndef S_IXOTH # define S_IXOTH S_IXUSR # endif #endif #if !defined (EWOULDBLOCK) && defined (HAVE_W32_SYSTEM) /* Compatibility with errno.h from mingw-2.0 */ # define EWOULDBLOCK 140 #endif #ifndef EAGAIN # define EAGAIN EWOULDBLOCK #endif #ifdef HAVE_W32CE_SYSTEM # define _set_errno(a) gpg_err_set_errno ((a)) /* Setmode is missing in cegcc but available since CE 5.0. */ int _setmode (int handle, int mode); # define setmode(a,b) _setmode ((a),(b)) #else # define _set_errno(a) do { errno = (a); } while (0) #endif #ifdef HAVE_W32_SYSTEM # define IS_INVALID_FD(a) ((void*)(a) == (void*)(-1)) /* ?? FIXME. */ #else # define IS_INVALID_FD(a) ((a) == -1) #endif /* Calculate array dimension. */ #ifndef DIM #define DIM(array) (sizeof (array) / sizeof (*array)) #endif /* A helper macro used to convert to a hex string. */ #define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A')) /* Generally used types. */ typedef void *(*func_realloc_t) (void *mem, size_t size); typedef void (*func_free_t) (void *mem); /* * A linked list to hold active stream objects. * Protected by ESTREAM_LIST_LOCK. */ struct estream_list_s { struct estream_list_s *next; estream_t stream; /* Entry is not used if NULL. */ }; typedef struct estream_list_s *estream_list_t; static estream_list_t estream_list; /* * File descriptors registered for use as the standard file handles. * Protected by ESTREAM_LIST_LOCK. */ static int custom_std_fds[3]; static unsigned char custom_std_fds_valid[3]; /* * A lock object to protect ESTREAM LIST, CUSTOM_STD_FDS and * CUSTOM_STD_FDS_VALID. Used by lock_list() and unlock_list(). */ GPGRT_LOCK_DEFINE (estream_list_lock); /* * Error code replacements. */ #ifndef EOPNOTSUPP # define EOPNOTSUPP ENOSYS #endif /* Local prototypes. */ static void fname_set_internal (estream_t stream, const char *fname, int quote); /* * Memory allocation wrappers used in this file. */ static void * mem_alloc (size_t n) { return _gpgrt_malloc (n); } static void * mem_realloc (void *p, size_t n) { return _gpgrt_realloc (p, n); } static void mem_free (void *p) { if (p) _gpgrt_free (p); } /* * A Windows helper function to map a W32 API error code to a standard * system error code. */ #ifdef HAVE_W32_SYSTEM static int map_w32_to_errno (DWORD w32_err) { switch (w32_err) { case 0: return 0; case ERROR_FILE_NOT_FOUND: return ENOENT; case ERROR_PATH_NOT_FOUND: return ENOENT; case ERROR_ACCESS_DENIED: return EPERM; case ERROR_INVALID_HANDLE: case ERROR_INVALID_BLOCK: return EINVAL; case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM; case ERROR_NO_DATA: return EPIPE; default: return EIO; } } #endif /*HAVE_W32_SYSTEM*/ /* * Replacement for a missing memrchr. */ #ifndef HAVE_MEMRCHR static void * memrchr (const void *buffer, int c, size_t n) { const unsigned char *p = buffer; for (p += n; n ; n--) if (*--p == c) return (void *)p; return NULL; } #endif /*HAVE_MEMRCHR*/ /* * Wrappers to lock a stream or the list of streams. */ #if 0 # define dbg_lock_0(f) fprintf (stderr, "estream: " f); # define dbg_lock_1(f, a) fprintf (stderr, "estream: " f, (a)); # define dbg_lock_2(f, a, b) fprintf (stderr, "estream: " f, (a), (b)); #else # define dbg_lock_0(f) # define dbg_lock_1(f, a) # define dbg_lock_2(f, a, b) #endif static int init_stream_lock (estream_t _GPGRT__RESTRICT stream) { int rc; if (!stream->intern->samethread) { dbg_lock_1 ("enter init_stream_lock for %p\n", stream); memset (&stream->intern->lock, 0 , sizeof stream->intern->lock); rc = _gpgrt_lock_init (&stream->intern->lock); dbg_lock_2 ("leave init_stream_lock for %p: rc=%d\n", stream, rc); } else rc = 0; return rc; } static void destroy_stream_lock (estream_t _GPGRT__RESTRICT stream) { if (!stream->intern->samethread) { dbg_lock_1 ("enter destroy_stream_lock for %p\n", stream); _gpgrt_lock_destroy (&stream->intern->lock); dbg_lock_1 ("leave destroy_stream_lock for %p\n", stream); } } static void lock_stream (estream_t _GPGRT__RESTRICT stream) { if (!stream->intern->samethread) { dbg_lock_1 ("enter lock_stream for %p\n", stream); _gpgrt_lock_lock (&stream->intern->lock); dbg_lock_1 ("leave lock_stream for %p\n", stream); } } static int trylock_stream (estream_t _GPGRT__RESTRICT stream) { int rc; if (!stream->intern->samethread) { dbg_lock_1 ("enter trylock_stream for %p\n", stream); rc = _gpgrt_lock_trylock (&stream->intern->lock)? 0 : -1; dbg_lock_2 ("leave trylock_stream for %p: rc=%d\n", stream, rc); } else rc = 0; return rc; } static void unlock_stream (estream_t _GPGRT__RESTRICT stream) { if (!stream->intern->samethread) { dbg_lock_1 ("enter unlock_stream for %p\n", stream); _gpgrt_lock_unlock (&stream->intern->lock); dbg_lock_1 ("leave unlock_stream for %p\n", stream); } } static void lock_list (void) { dbg_lock_0 ("enter lock_list\n"); _gpgrt_lock_lock (&estream_list_lock); dbg_lock_0 ("leave lock_list\n"); } static void unlock_list (void) { dbg_lock_0 ("enter unlock_list\n"); _gpgrt_lock_unlock (&estream_list_lock); dbg_lock_0 ("leave unlock_list\n"); } #undef dbg_lock_0 #undef dbg_lock_1 #undef dbg_lock_2 /* * Manipulation of the list of stream. */ /* * Add STREAM to the list of registered stream objects. If * WITH_LOCKED_LIST is true it is assumed that the list of streams is * already locked. The implementation is straightforward: We first * look for an unused entry in the list and use that; if none is * available we put a new item at the head. We drawback of the * strategy never to shorten the list is that a one time allocation of * many streams will lead to scanning unused entries later. If that * turns out to be a problem, we may either free some items from the * list or append new entries at the end; or use a table. Returns 0 * on success; on error or non-zero is returned and ERRNO set. */ static int do_list_add (estream_t stream, int with_locked_list) { estream_list_t item; if (!with_locked_list) lock_list (); for (item = estream_list; item && item->stream; item = item->next) ; if (!item) { item = mem_alloc (sizeof *item); if (item) { item->next = estream_list; estream_list = item; } } if (item) item->stream = stream; if (!with_locked_list) unlock_list (); return item? 0 : -1; } /* * Remove STREAM from the list of registered stream objects. */ static void do_list_remove (estream_t stream, int with_locked_list) { estream_list_t item, item_prev = NULL; if (!with_locked_list) lock_list (); for (item = estream_list; item; item = item->next) if (item->stream == stream) break; else item_prev = item; if (item_prev) { item_prev->next = item->next; mem_free (item); } else { if (item) { estream_list = item->next; mem_free (item); } } if (!with_locked_list) unlock_list (); } /* * The atexit handler for the entire gpgrt. */ static void do_deinit (void) { /* Flush all streams. */ _gpgrt_fflush (NULL); /* We should release the estream_list. However there is one problem: That list is also used to search for the standard estream file descriptors. If we would remove the entire list, any use of es_foo in another atexit function may re-create the list and the streams with possible undesirable effects. Given that we don't close the stream either, it should not matter that we keep the list and let the OS clean it up at process end. */ /* Reset the syscall clamp. */ _gpgrt_set_syscall_clamp (NULL, NULL); } /* * Initialization of the estream module. */ int _gpgrt_estream_init (void) { static int initialized; if (!initialized) { initialized = 1; atexit (do_deinit); } return 0; } /* * Implementation of memory based I/O. */ /* Cookie for memory objects. */ typedef struct estream_cookie_mem { unsigned int modeflags; /* Open flags. */ unsigned char *memory; /* Allocated data buffer. */ size_t memory_size; /* Allocated size of MEMORY. */ size_t memory_limit; /* Caller supplied maximum allowed allocation size or 0 for no limit. */ size_t offset; /* Current offset in MEMORY. */ size_t data_len; /* Used length of data in MEMORY. */ size_t block_size; /* Block size. */ struct { unsigned int grow: 1; /* MEMORY is allowed to grow. */ } flags; func_realloc_t func_realloc; func_free_t func_free; } *estream_cookie_mem_t; /* * Create function for memory objects. DATA is either NULL or a user * supplied buffer with the initial conetnt of the memory buffer. If * DATA is NULL, DATA_N and DATA_LEN need to be 0 as well. If DATA is * not NULL, DATA_N gives the allocated size of DATA and DATA_LEN the * used length in DATA. If this function succeeds DATA is now owned * by this function. If GROW is false FUNC_REALLOC is not * required. */ static int func_mem_create (void *_GPGRT__RESTRICT *_GPGRT__RESTRICT cookie, unsigned char *_GPGRT__RESTRICT data, size_t data_n, size_t data_len, size_t block_size, unsigned int grow, func_realloc_t func_realloc, func_free_t func_free, unsigned int modeflags, size_t memory_limit) { estream_cookie_mem_t mem_cookie; int err; if (!data && (data_n || data_len)) { _set_errno (EINVAL); return -1; } if (grow && func_free && !func_realloc) { _set_errno (EINVAL); return -1; } /* Round a memory limit up to the next block length. */ if (memory_limit && block_size) { memory_limit += block_size - 1; memory_limit /= block_size; memory_limit *= block_size; } mem_cookie = mem_alloc (sizeof (*mem_cookie)); if (!mem_cookie) err = -1; else { mem_cookie->modeflags = modeflags; mem_cookie->memory = data; mem_cookie->memory_size = data_n; mem_cookie->memory_limit = memory_limit; mem_cookie->offset = 0; mem_cookie->data_len = data_len; mem_cookie->block_size = block_size; mem_cookie->flags.grow = !!grow; mem_cookie->func_realloc = grow? (func_realloc ? func_realloc : mem_realloc) : NULL; mem_cookie->func_free = func_free ? func_free : mem_free; *cookie = mem_cookie; err = 0; } return err; } /* * Read function for memory objects. */ static gpgrt_ssize_t func_mem_read (void *cookie, void *buffer, size_t size) { estream_cookie_mem_t mem_cookie = cookie; gpgrt_ssize_t ret; if (!size) /* Just the pending data check. */ return (mem_cookie->data_len - mem_cookie->offset)? 0 : -1; if (size > mem_cookie->data_len - mem_cookie->offset) size = mem_cookie->data_len - mem_cookie->offset; if (size) { memcpy (buffer, mem_cookie->memory + mem_cookie->offset, size); mem_cookie->offset += size; } ret = size; return ret; } /* * Write function for memory objects. */ static gpgrt_ssize_t func_mem_write (void *cookie, const void *buffer, size_t size) { estream_cookie_mem_t mem_cookie = cookie; gpgrt_ssize_t ret; size_t nleft; if (!size) return 0; /* A flush is a NOP for memory objects. */ if (mem_cookie->modeflags & O_APPEND) { /* Append to data. */ mem_cookie->offset = mem_cookie->data_len; } assert (mem_cookie->memory_size >= mem_cookie->offset); nleft = mem_cookie->memory_size - mem_cookie->offset; /* If we are not allowed to grow the buffer, limit the size to the left space. */ if (!mem_cookie->flags.grow && size > nleft) size = nleft; /* Enlarge the memory buffer if needed. */ if (size > nleft) { unsigned char *newbuf; size_t newsize; if (!mem_cookie->memory_size) newsize = size; /* Not yet allocated. */ else newsize = mem_cookie->memory_size + (size - nleft); if (newsize < mem_cookie->offset) { _set_errno (EINVAL); return -1; } /* Round up to the next block length. BLOCK_SIZE should always be set; we check anyway. */ if (mem_cookie->block_size) { newsize += mem_cookie->block_size - 1; if (newsize < mem_cookie->offset) { _set_errno (EINVAL); return -1; } newsize /= mem_cookie->block_size; newsize *= mem_cookie->block_size; } /* Check for a total limit. */ if (mem_cookie->memory_limit && newsize > mem_cookie->memory_limit) { _set_errno (ENOSPC); return -1; } assert (mem_cookie->func_realloc); newbuf = mem_cookie->func_realloc (mem_cookie->memory, newsize); if (!newbuf) return -1; mem_cookie->memory = newbuf; mem_cookie->memory_size = newsize; assert (mem_cookie->memory_size >= mem_cookie->offset); nleft = mem_cookie->memory_size - mem_cookie->offset; assert (size <= nleft); } memcpy (mem_cookie->memory + mem_cookie->offset, buffer, size); if (mem_cookie->offset + size > mem_cookie->data_len) mem_cookie->data_len = mem_cookie->offset + size; mem_cookie->offset += size; ret = size; return ret; } /* * Seek function for memory objects. */ static int func_mem_seek (void *cookie, gpgrt_off_t *offset, int whence) { estream_cookie_mem_t mem_cookie = cookie; gpgrt_off_t pos_new; switch (whence) { case SEEK_SET: pos_new = *offset; break; case SEEK_CUR: pos_new = mem_cookie->offset += *offset; break; case SEEK_END: pos_new = mem_cookie->data_len += *offset; break; default: _set_errno (EINVAL); return -1; } if (pos_new > mem_cookie->memory_size) { size_t newsize; void *newbuf; if (!mem_cookie->flags.grow) { _set_errno (ENOSPC); return -1; } newsize = pos_new + mem_cookie->block_size - 1; if (newsize < pos_new) { _set_errno (EINVAL); return -1; } newsize /= mem_cookie->block_size; newsize *= mem_cookie->block_size; if (mem_cookie->memory_limit && newsize > mem_cookie->memory_limit) { _set_errno (ENOSPC); return -1; } assert (mem_cookie->func_realloc); newbuf = mem_cookie->func_realloc (mem_cookie->memory, newsize); if (!newbuf) return -1; mem_cookie->memory = newbuf; mem_cookie->memory_size = newsize; } if (pos_new > mem_cookie->data_len) { /* Fill spare space with zeroes. */ memset (mem_cookie->memory + mem_cookie->data_len, 0, pos_new - mem_cookie->data_len); mem_cookie->data_len = pos_new; } mem_cookie->offset = pos_new; *offset = pos_new; return 0; } /* * The IOCTL function for memory objects. */ static int func_mem_ioctl (void *cookie, int cmd, void *ptr, size_t *len) { estream_cookie_mem_t mem_cookie = cookie; int ret; if (cmd == COOKIE_IOCTL_SNATCH_BUFFER) { /* Return the internal buffer of the stream to the caller and invalidate it for the stream. */ *(void**)ptr = mem_cookie->memory; *len = mem_cookie->data_len; mem_cookie->memory = NULL; mem_cookie->memory_size = 0; mem_cookie->offset = 0; ret = 0; } + else if (cmd == COOKIE_IOCTL_TRUNCATE) + { + gpgrt_off_t length = *(gpgrt_off_t *)ptr; + + ret = func_mem_seek (cookie, &length, SEEK_SET); + if (ret != -1) + mem_cookie->data_len = mem_cookie->offset; + } else { _set_errno (EINVAL); ret = -1; } return ret; } /* * The destroy function for memory objects. */ static int func_mem_destroy (void *cookie) { estream_cookie_mem_t mem_cookie = cookie; if (cookie) { mem_cookie->func_free (mem_cookie->memory); mem_free (mem_cookie); } return 0; } /* * Access object for the memory functions. */ static struct cookie_io_functions_s estream_functions_mem = { { func_mem_read, func_mem_write, func_mem_seek, func_mem_destroy, }, func_mem_ioctl, }; /* * Implementation of file descriptor based I/O. */ /* Cookie for fd objects. */ typedef struct estream_cookie_fd { int fd; /* The file descriptor we are using for actual output. */ int no_close; /* If set we won't close the file descriptor. */ int nonblock; /* Non-blocking mode is enabled. */ } *estream_cookie_fd_t; /* * Create function for objects indentified by a libc file descriptor. */ static int func_fd_create (void **cookie, int fd, unsigned int modeflags, int no_close) { estream_cookie_fd_t fd_cookie; int err; trace (("enter: fd=%d mf=%x nc=%d", fd, modeflags, no_close)); fd_cookie = mem_alloc (sizeof (*fd_cookie)); if (! fd_cookie) err = -1; else { #ifdef HAVE_DOSISH_SYSTEM /* Make sure it is in binary mode if requested. */ if ( (modeflags & O_BINARY) ) setmode (fd, O_BINARY); #endif fd_cookie->fd = fd; fd_cookie->no_close = no_close; fd_cookie->nonblock = !!(modeflags & O_NONBLOCK); *cookie = fd_cookie; err = 0; } trace_errno (err, ("leave: cookie=%p err=%d", *cookie, err)); return err; } /* * Read function for fd objects. */ static gpgrt_ssize_t func_fd_read (void *cookie, void *buffer, size_t size) { estream_cookie_fd_t file_cookie = cookie; gpgrt_ssize_t bytes_read; trace (("enter: cookie=%p buffer=%p size=%d", cookie, buffer, (int)size)); if (!size) bytes_read = -1; /* We don't know whether anything is pending. */ else if (IS_INVALID_FD (file_cookie->fd)) { _gpgrt_yield (); bytes_read = 0; } else { _gpgrt_pre_syscall (); do { bytes_read = read (file_cookie->fd, buffer, size); } while (bytes_read == -1 && errno == EINTR); _gpgrt_post_syscall (); } trace_errno (bytes_read == -1, ("leave: bytes_read=%d", (int)bytes_read)); return bytes_read; } /* * Write function for fd objects. */ static gpgrt_ssize_t func_fd_write (void *cookie, const void *buffer, size_t size) { estream_cookie_fd_t file_cookie = cookie; gpgrt_ssize_t bytes_written; trace (("enter: cookie=%p buffer=%p size=%d", cookie, buffer, (int)size)); if (IS_INVALID_FD (file_cookie->fd)) { _gpgrt_yield (); bytes_written = size; /* Yeah: Success writing to the bit bucket. */ } else if (buffer) { _gpgrt_pre_syscall (); do { bytes_written = write (file_cookie->fd, buffer, size); } while (bytes_written == -1 && errno == EINTR); _gpgrt_post_syscall (); } else bytes_written = size; /* Note that for a flush SIZE should be 0. */ trace_errno (bytes_written == -1, ("leave: bytes_written=%d", (int)bytes_written)); return bytes_written; } /* * Seek function for fd objects. */ static int func_fd_seek (void *cookie, gpgrt_off_t *offset, int whence) { estream_cookie_fd_t file_cookie = cookie; gpgrt_off_t offset_new; int err; if (IS_INVALID_FD (file_cookie->fd)) { _set_errno (ESPIPE); err = -1; } else { _gpgrt_pre_syscall (); offset_new = lseek (file_cookie->fd, *offset, whence); _gpgrt_post_syscall (); if (offset_new == -1) err = -1; else { *offset = offset_new; err = 0; } } return err; } /* * The IOCTL function for fd objects. */ static int func_fd_ioctl (void *cookie, int cmd, void *ptr, size_t *len) { estream_cookie_fd_t fd_cookie = cookie; int ret; if (cmd == COOKIE_IOCTL_NONBLOCK && !len) { fd_cookie->nonblock = !!ptr; if (IS_INVALID_FD (fd_cookie->fd)) { _set_errno (EINVAL); ret = -1; } else { #ifdef _WIN32 _set_errno (EOPNOTSUPP); /* FIXME: Implement for Windows. */ ret = -1; #else _set_errno (0); ret = fcntl (fd_cookie->fd, F_GETFL, 0); if (ret == -1 && errno) ; else if (fd_cookie->nonblock) ret = fcntl (fd_cookie->fd, F_SETFL, (ret | O_NONBLOCK)); else ret = fcntl (fd_cookie->fd, F_SETFL, (ret & ~O_NONBLOCK)); #endif } } else { _set_errno (EINVAL); ret = -1; } return ret; } /* * The destroy function for fd objects. */ static int func_fd_destroy (void *cookie) { estream_cookie_fd_t fd_cookie = cookie; int err; trace (("enter: cookie=%p", cookie)); if (fd_cookie) { if (IS_INVALID_FD (fd_cookie->fd)) err = 0; else err = fd_cookie->no_close? 0 : close (fd_cookie->fd); mem_free (fd_cookie); } else err = 0; trace_errno (err,("leave: err=%d", err)); return err; } /* * Access object for the fd functions. */ static struct cookie_io_functions_s estream_functions_fd = { { func_fd_read, func_fd_write, func_fd_seek, func_fd_destroy, }, func_fd_ioctl, }; /* * Implementation of W32 handle based I/O. */ #ifdef HAVE_W32_SYSTEM /* Cookie for fd objects. */ typedef struct estream_cookie_w32 { HANDLE hd; /* The handle we are using for actual output. */ int no_close; /* If set we won't close the handle. */ int no_syscall_clamp; /* Do not use the syscall clamp. */ } *estream_cookie_w32_t; /* * Create function for w32 handle objects. */ static int func_w32_create (void **cookie, HANDLE hd, unsigned int modeflags, int no_close, int no_syscall_clamp) { estream_cookie_w32_t w32_cookie; int err; trace (("enter: hd=%p mf=%x nc=%d nsc=%d", hd, modeflags, no_close, no_syscall_clamp)); w32_cookie = mem_alloc (sizeof (*w32_cookie)); if (!w32_cookie) err = -1; else { /* CR/LF translations are not supported when using the bare W32 API. If that is really required we need to implemented that in the upper layer. */ (void)modeflags; w32_cookie->hd = hd; w32_cookie->no_close = no_close; w32_cookie->no_syscall_clamp = no_syscall_clamp; *cookie = w32_cookie; err = 0; } trace_errno (err, ("leave: cookie=%p err=%d", *cookie, err)); return err; } /* * Read function for W32 handle objects. * * Note that this function may also be used by the reader thread of * w32-stream. In that case the NO_SYSCALL_CLAMP is set. */ static gpgrt_ssize_t func_w32_read (void *cookie, void *buffer, size_t size) { estream_cookie_w32_t w32_cookie = cookie; gpgrt_ssize_t bytes_read; trace (("enter: cookie=%p buffer=%p size=%d", cookie, buffer, (int)size)); if (!size) bytes_read = -1; /* We don't know whether anything is pending. */ else if (w32_cookie->hd == INVALID_HANDLE_VALUE) { _gpgrt_yield (); bytes_read = 0; } else { if (!w32_cookie->no_syscall_clamp) _gpgrt_pre_syscall (); do { DWORD nread, ec; trace (("cookie=%p calling ReadFile", cookie)); if (!ReadFile (w32_cookie->hd, buffer, size, &nread, NULL)) { ec = GetLastError (); trace (("cookie=%p ReadFile failed: ec=%ld", cookie,ec)); if (ec == ERROR_BROKEN_PIPE) bytes_read = 0; /* Like our pth_read we handle this as EOF. */ else { _set_errno (map_w32_to_errno (ec)); bytes_read = -1; } } else bytes_read = (int)nread; } while (bytes_read == -1 && errno == EINTR); if (!w32_cookie->no_syscall_clamp) _gpgrt_post_syscall (); } trace_errno (bytes_read==-1,("leave: bytes_read=%d", (int)bytes_read)); return bytes_read; } /* * Write function for W32 handle objects. * * Note that this function may also be used by the writer thread of * w32-stream. In that case the NO_SYSCALL_CLAMP is set. */ static gpgrt_ssize_t func_w32_write (void *cookie, const void *buffer, size_t size) { estream_cookie_w32_t w32_cookie = cookie; gpgrt_ssize_t bytes_written; trace (("enter: cookie=%p buffer=%p size=%d", cookie, buffer, (int)size)); if (w32_cookie->hd == INVALID_HANDLE_VALUE) { _gpgrt_yield (); bytes_written = size; /* Yeah: Success writing to the bit bucket. */ } else if (buffer) { if (!w32_cookie->no_syscall_clamp) _gpgrt_pre_syscall (); do { DWORD nwritten; trace (("cookie=%p calling WriteFile", cookie)); if (!WriteFile (w32_cookie->hd, buffer, size, &nwritten, NULL)) { DWORD ec = GetLastError (); trace (("cookie=%p WriteFile failed: ec=%ld", cookie, ec)); _set_errno (map_w32_to_errno (ec)); bytes_written = -1; } else bytes_written = (int)nwritten; } while (bytes_written == -1 && errno == EINTR); if (!w32_cookie->no_syscall_clamp) _gpgrt_post_syscall (); } else bytes_written = size; /* Note that for a flush SIZE should be 0. */ trace_errno (bytes_written==-1, ("leave: bytes_written=%d", (int)bytes_written)); return bytes_written; } /* * Seek function for W32 handle objects. */ static int func_w32_seek (void *cookie, gpgrt_off_t *offset, int whence) { estream_cookie_w32_t w32_cookie = cookie; DWORD method; LARGE_INTEGER distance, newoff; if (w32_cookie->hd == INVALID_HANDLE_VALUE) { _set_errno (ESPIPE); return -1; } if (whence == SEEK_SET) { method = FILE_BEGIN; distance.QuadPart = (unsigned long long)(*offset); } else if (whence == SEEK_CUR) { method = FILE_CURRENT; distance.QuadPart = (long long)(*offset); } else if (whence == SEEK_END) { method = FILE_END; distance.QuadPart = (long long)(*offset); } else { _set_errno (EINVAL); return -1; } #ifdef HAVE_W32CE_SYSTEM # warning need to use SetFilePointer #else if (!w32_cookie->no_syscall_clamp) _gpgrt_pre_syscall (); if (!SetFilePointerEx (w32_cookie->hd, distance, &newoff, method)) { _set_errno (map_w32_to_errno (GetLastError ())); _gpgrt_post_syscall (); return -1; } if (!w32_cookie->no_syscall_clamp) _gpgrt_post_syscall (); #endif /* Note that gpgrt_off_t is always 64 bit. */ *offset = (gpgrt_off_t)newoff.QuadPart; return 0; } /* * Destroy function for W32 handle objects. */ static int func_w32_destroy (void *cookie) { estream_cookie_w32_t w32_cookie = cookie; int err; trace (("enter: cookie=%p", cookie)); if (w32_cookie) { if (w32_cookie->hd == INVALID_HANDLE_VALUE) err = 0; else if (w32_cookie->no_close) err = 0; else { trace (("cookie=%p closing handle %p", cookie, w32_cookie->hd)); if (!CloseHandle (w32_cookie->hd)) { DWORD ec = GetLastError (); trace (("cookie=%p CloseHandle failed: ec=%ld", cookie,ec)); _set_errno (map_w32_to_errno (ec)); err = -1; } else err = 0; } mem_free (w32_cookie); } else err = 0; trace_errno (err, ("leave: err=%d", err)); return err; } /* * Access object for the W32 handle based objects. */ static struct cookie_io_functions_s estream_functions_w32 = { { func_w32_read, func_w32_write, func_w32_seek, func_w32_destroy, }, NULL, }; #endif /*HAVE_W32_SYSTEM*/ /* * Implementation of stdio based I/O. */ /* Cookie for fp objects. */ typedef struct estream_cookie_fp { FILE *fp; /* The file pointer we are using for actual output. */ int no_close; /* If set we won't close the file pointer. */ } *estream_cookie_fp_t; /* * Create function for stdio based objects. */ static int func_fp_create (void **cookie, FILE *fp, unsigned int modeflags, int no_close) { estream_cookie_fp_t fp_cookie; int err; fp_cookie = mem_alloc (sizeof *fp_cookie); if (!fp_cookie) err = -1; else { #ifdef HAVE_DOSISH_SYSTEM /* Make sure it is in binary mode if requested. */ if ( (modeflags & O_BINARY) ) setmode (fileno (fp), O_BINARY); #else (void)modeflags; #endif fp_cookie->fp = fp; fp_cookie->no_close = no_close; *cookie = fp_cookie; err = 0; } return err; } /* * Read function for stdio based objects. */ static gpgrt_ssize_t func_fp_read (void *cookie, void *buffer, size_t size) { estream_cookie_fp_t file_cookie = cookie; gpgrt_ssize_t bytes_read; if (!size) return -1; /* We don't know whether anything is pending. */ if (file_cookie->fp) { _gpgrt_pre_syscall (); bytes_read = fread (buffer, 1, size, file_cookie->fp); _gpgrt_post_syscall (); } else bytes_read = 0; if (!bytes_read && ferror (file_cookie->fp)) return -1; return bytes_read; } /* * Write function for stdio bases objects. */ static gpgrt_ssize_t func_fp_write (void *cookie, const void *buffer, size_t size) { estream_cookie_fp_t file_cookie = cookie; size_t bytes_written; if (file_cookie->fp) { _gpgrt_pre_syscall (); if (buffer) { #ifdef HAVE_W32_SYSTEM /* Using an fwrite to stdout connected to the console fails with the error "Not enough space" for an fwrite size of >= 52KB (tested on Windows XP SP2). To solve this we always chunk the writes up into smaller blocks. */ bytes_written = 0; while (bytes_written < size) { size_t cnt = size - bytes_written; if (cnt > 32*1024) cnt = 32*1024; if (fwrite ((const char*)buffer + bytes_written, cnt, 1, file_cookie->fp) != 1) break; /* Write error. */ bytes_written += cnt; } #else bytes_written = fwrite (buffer, 1, size, file_cookie->fp); #endif } else /* Only flush requested. */ bytes_written = size; fflush (file_cookie->fp); _gpgrt_post_syscall (); } else bytes_written = size; /* Successfully written to the bit bucket. */ if (bytes_written != size) return -1; return bytes_written; } /* * Seek function for stdio based objects. */ static int func_fp_seek (void *cookie, gpgrt_off_t *offset, int whence) { estream_cookie_fp_t file_cookie = cookie; long int offset_new; if (!file_cookie->fp) { _set_errno (ESPIPE); return -1; } _gpgrt_pre_syscall (); if ( fseek (file_cookie->fp, (long int)*offset, whence) ) { /* fprintf (stderr, "\nfseek failed: errno=%d (%s)\n", */ /* errno,strerror (errno)); */ _gpgrt_post_syscall (); return -1; } offset_new = ftell (file_cookie->fp); _gpgrt_post_syscall (); if (offset_new == -1) { /* fprintf (stderr, "\nftell failed: errno=%d (%s)\n", */ /* errno,strerror (errno)); */ return -1; } *offset = offset_new; return 0; } /* * Destroy function for stdio based objects. */ static int func_fp_destroy (void *cookie) { estream_cookie_fp_t fp_cookie = cookie; int err; if (fp_cookie) { if (fp_cookie->fp) { _gpgrt_pre_syscall (); fflush (fp_cookie->fp); _gpgrt_post_syscall (); err = fp_cookie->no_close? 0 : fclose (fp_cookie->fp); } else err = 0; mem_free (fp_cookie); } else err = 0; return err; } /* * Access object for stdio based objects. */ static struct cookie_io_functions_s estream_functions_fp = { { func_fp_read, func_fp_write, func_fp_seek, func_fp_destroy, }, NULL, }; /* * Implementation of file name based I/O. * * Note that only a create function is required because the other * operations ares handled by file descriptor based I/O. */ /* Create function for objects identified by a file name. */ static int func_file_create (void **cookie, int *filedes, const char *path, unsigned int modeflags, unsigned int cmode) { estream_cookie_fd_t file_cookie; int err; int fd; err = 0; file_cookie = mem_alloc (sizeof (*file_cookie)); if (! file_cookie) { err = -1; goto out; } fd = open (path, modeflags, cmode); if (fd == -1) { err = -1; goto out; } #ifdef HAVE_DOSISH_SYSTEM /* Make sure it is in binary mode if requested. */ if ( (modeflags & O_BINARY) ) setmode (fd, O_BINARY); #endif file_cookie->fd = fd; file_cookie->no_close = 0; *cookie = file_cookie; *filedes = fd; out: if (err) mem_free (file_cookie); return err; } /* Flags used by parse_mode and friends. */ #define X_SAMETHREAD (1 << 0) #define X_SYSOPEN (1 << 1) #define X_POLLABLE (1 << 2) /* Parse the mode flags of fopen et al. In addition to the POSIX * defined mode flags keyword parameters are supported. These are * key/value pairs delimited by comma and optional white spaces. * Keywords and values may not contain a comma or white space; unknown * keywords are skipped. Supported keywords are: * * mode= * * Creates a file and gives the new file read and write permissions * for the user and read permission for the group. The format of * the string is the same as shown by the -l option of the ls(1) * command. However the first letter must be a dash and it is * allowed to leave out trailing dashes. If this keyword parameter * is not given the default mode for creating files is "-rw-rw-r--" * (664). Note that the system still applies the current umask to * the mode when creating a file. Example: * * "wb,mode=-rw-r--" * * samethread * * Assumes that the object is only used by the creating thread and * disables any internal locking. This keyword is also found on * IBM systems. * * nonblock * * The object is opened in non-blocking mode. This is the same as * calling gpgrt_set_nonblock on the file. * * sysopen * * The object is opened in sysmode. On POSIX this is a NOP but * under Windows the direct W32 API functions (HANDLE) are used * instead of their libc counterparts (fd). * FIXME: The functionality is not yet implemented. * * pollable * * The object is opened in a way suitable for use with es_poll. On * POSIX this is a NOP but under Windows we create up to two * threads, one for reading and one for writing, do any I/O there, * and synchronize with them in order to support es_poll. * * Note: R_CMODE is optional because is only required by functions * which are able to creat a file. */ static int parse_mode (const char *modestr, unsigned int *modeflags, unsigned int *r_xmode, unsigned int *r_cmode) { unsigned int omode, oflags, cmode; int got_cmode = 0; *r_xmode = 0; switch (*modestr) { case 'r': omode = O_RDONLY; oflags = 0; break; case 'w': omode = O_WRONLY; oflags = O_TRUNC | O_CREAT; break; case 'a': omode = O_WRONLY; oflags = O_APPEND | O_CREAT; break; default: _set_errno (EINVAL); return -1; } for (modestr++; *modestr; modestr++) { switch (*modestr) { case '+': omode = O_RDWR; break; case 'b': oflags |= O_BINARY; break; case 'x': oflags |= O_EXCL; break; case ',': goto keyvalue; default: /* Ignore unknown flags. */ break; } } keyvalue: /* Parse key/value pairs (similar to fopen on mainframes). */ for (cmode=0; *modestr == ','; modestr += strcspn (modestr, ",")) { modestr++; modestr += strspn (modestr, " \t"); if (!strncmp (modestr, "mode=", 5)) { static struct { char letter; unsigned int value; } table[] = { { '-', 0 }, { 'r', S_IRUSR }, { 'w', S_IWUSR }, { 'x', S_IXUSR }, { 'r', S_IRGRP }, { 'w', S_IWGRP }, { 'x', S_IXGRP }, { 'r', S_IROTH }, { 'w', S_IWOTH }, { 'x', S_IXOTH }}; int idx; got_cmode = 1; modestr += 5; /* For now we only support a string as used by ls(1) and no octal numbers. The first character must be a dash. */ for (idx=0; idx < 10 && *modestr; idx++, modestr++) { if (*modestr == table[idx].letter) cmode |= table[idx].value; else if (*modestr != '-') break; } if (*modestr && !strchr (" \t,", *modestr)) { _set_errno (EINVAL); return -1; } } else if (!strncmp (modestr, "samethread", 10)) { modestr += 10; if (*modestr && !strchr (" \t,", *modestr)) { _set_errno (EINVAL); return -1; } *r_xmode |= X_SAMETHREAD; } else if (!strncmp (modestr, "nonblock", 8)) { modestr += 8; if (*modestr && !strchr (" \t,", *modestr)) { _set_errno (EINVAL); return -1; } oflags |= O_NONBLOCK; #if HAVE_W32_SYSTEM /* Currently, nonblock implies pollable on Windows. */ *r_xmode |= X_POLLABLE; #endif } else if (!strncmp (modestr, "sysopen", 7)) { modestr += 7; if (*modestr && !strchr (" \t,", *modestr)) { _set_errno (EINVAL); return -1; } *r_xmode |= X_SYSOPEN; } else if (!strncmp (modestr, "pollable", 8)) { modestr += 8; if (*modestr && !strchr (" \t,", *modestr)) { _set_errno (EINVAL); return -1; } *r_xmode |= X_POLLABLE; } } if (!got_cmode) cmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); *modeflags = (omode | oflags); if (r_cmode) *r_cmode = cmode; return 0; } /* * Low level stream functionality. */ static int fill_stream (estream_t stream) { size_t bytes_read = 0; int err; if (!stream->intern->func_read) { _set_errno (EOPNOTSUPP); err = -1; } else if (!stream->buffer_size) err = 0; else { gpgrt_cookie_read_function_t func_read = stream->intern->func_read; gpgrt_ssize_t ret; ret = (*func_read) (stream->intern->cookie, stream->buffer, stream->buffer_size); if (ret == -1) { bytes_read = 0; err = -1; #if EWOULDBLOCK != EAGAIN if (errno == EWOULDBLOCK) _set_errno (EAGAIN); #endif } else { bytes_read = ret; err = 0; } } if (err) { if (errno != EAGAIN) { if (errno == EPIPE) stream->intern->indicators.hup = 1; stream->intern->indicators.err = 1; } } else if (!bytes_read) stream->intern->indicators.eof = 1; stream->intern->offset += stream->data_len; stream->data_len = bytes_read; stream->data_offset = 0; return err; } static int flush_stream (estream_t stream) { gpgrt_cookie_write_function_t func_write = stream->intern->func_write; int err; assert (stream->flags.writing); if (stream->data_offset) { size_t bytes_written; size_t data_flushed; gpgrt_ssize_t ret; if (! func_write) { _set_errno (EOPNOTSUPP); err = -1; goto out; } /* Note: to prevent an endless loop caused by user-provided write-functions that pretend to have written more bytes than they were asked to write, we have to check for "(stream->data_offset - data_flushed) > 0" instead of "stream->data_offset - data_flushed". */ data_flushed = 0; err = 0; while ((((gpgrt_ssize_t) (stream->data_offset - data_flushed)) > 0) && !err) { ret = (*func_write) (stream->intern->cookie, stream->buffer + data_flushed, stream->data_offset - data_flushed); if (ret == -1) { bytes_written = 0; err = -1; #if EWOULDBLOCK != EAGAIN if (errno == EWOULDBLOCK) _set_errno (EAGAIN); #endif } else bytes_written = ret; data_flushed += bytes_written; if (err) break; } stream->data_flushed += data_flushed; if (stream->data_offset == data_flushed) { stream->intern->offset += stream->data_offset; stream->data_offset = 0; stream->data_flushed = 0; } } else err = 0; /* Always propagate flush event in case gpgrt_fflush was called * explictly to do flush buffers in caller's cookie functions. */ (*func_write) (stream->intern->cookie, NULL, 0); out: if (err && errno != EAGAIN) { if (errno == EPIPE) stream->intern->indicators.hup = 1; stream->intern->indicators.err = 1; } return err; } /* * Discard buffered data for STREAM. */ static void es_empty (estream_t stream) { assert (!stream->flags.writing); stream->data_len = 0; stream->data_offset = 0; stream->unread_data_len = 0; } /* * Initialize STREAM. */ static void init_stream_obj (estream_t stream, void *cookie, es_syshd_t *syshd, gpgrt_stream_backend_kind_t kind, struct cookie_io_functions_s functions, unsigned int modeflags, unsigned int xmode) { stream->intern->kind = kind; stream->intern->cookie = cookie; stream->intern->opaque = NULL; stream->intern->offset = 0; stream->intern->func_read = functions.public.func_read; stream->intern->func_write = functions.public.func_write; stream->intern->func_seek = functions.public.func_seek; stream->intern->func_ioctl = functions.func_ioctl; stream->intern->func_close = functions.public.func_close; stream->intern->strategy = _IOFBF; stream->intern->syshd = *syshd; stream->intern->print_ntotal = 0; stream->intern->indicators.err = 0; stream->intern->indicators.eof = 0; stream->intern->indicators.hup = 0; stream->intern->is_stdstream = 0; stream->intern->stdstream_fd = 0; stream->intern->deallocate_buffer = 0; stream->intern->printable_fname = NULL; stream->intern->printable_fname_inuse = 0; stream->intern->samethread = !! (xmode & X_SAMETHREAD); stream->intern->onclose = NULL; stream->data_len = 0; stream->data_offset = 0; stream->data_flushed = 0; stream->unread_data_len = 0; /* Depending on the modeflags we set whether we start in writing or reading mode. This is required in case we are working on a stream which is not seeekable (like stdout). Without this pre-initialization we would do a seek at the first write call and as this will fail no output will be delivered. */ if ((modeflags & O_WRONLY) || (modeflags & O_RDWR) ) stream->flags.writing = 1; else stream->flags.writing = 0; } /* * Deinitialize the STREAM object. This does _not_ free the memory, * destroys the lock, or closes the underlying descriptor. */ static int deinit_stream_obj (estream_t stream) { gpgrt_cookie_close_function_t func_close; int err, tmp_err; trace (("enter: stream %p", stream)); func_close = stream->intern->func_close; err = 0; if (stream->flags.writing) { tmp_err = flush_stream (stream); if (!err) err = tmp_err; } if (func_close) { trace (("stream %p calling func_close", stream)); tmp_err = func_close (stream->intern->cookie); if (!err) err = tmp_err; } mem_free (stream->intern->printable_fname); stream->intern->printable_fname = NULL; stream->intern->printable_fname_inuse = 0; while (stream->intern->onclose) { notify_list_t tmp = stream->intern->onclose->next; mem_free (stream->intern->onclose); stream->intern->onclose = tmp; } trace_errno (err, ("leave: stream %p err=%d", stream, err)); return err; } /* * Create a new stream and initialize it. On success the new stream * handle is tsored at R_STREAM. On failure NULL is stored at * R_STREAM. */ static int create_stream (estream_t *r_stream, void *cookie, es_syshd_t *syshd, gpgrt_stream_backend_kind_t kind, struct cookie_io_functions_s functions, unsigned int modeflags, unsigned int xmode, int with_locked_list) { estream_internal_t stream_internal_new; estream_t stream_new; int err; #if HAVE_W32_SYSTEM void *old_cookie = NULL; #endif stream_new = NULL; stream_internal_new = NULL; #if HAVE_W32_SYSTEM if ((xmode & X_POLLABLE) && kind != BACKEND_W32) { /* We require the W32 backend, because only that allows us to * write directly using the native W32 API and to disable the * system clamp. Note that func_w32_create has already been * called with the flag to disable the system call clamp. */ _set_errno (EINVAL); err = -1; goto out; } #endif /*HAVE_W32_SYSTEM*/ stream_new = mem_alloc (sizeof (*stream_new)); if (! stream_new) { err = -1; goto out; } stream_internal_new = mem_alloc (sizeof (*stream_internal_new)); if (! stream_internal_new) { err = -1; goto out; } stream_new->buffer = stream_internal_new->buffer; stream_new->buffer_size = sizeof (stream_internal_new->buffer); stream_new->unread_buffer = stream_internal_new->unread_buffer; stream_new->unread_buffer_size = sizeof (stream_internal_new->unread_buffer); stream_new->intern = stream_internal_new; #if HAVE_W32_SYSTEM if ((xmode & X_POLLABLE)) { void *new_cookie; err = _gpgrt_w32_pollable_create (&new_cookie, modeflags, functions, cookie); if (err) goto out; modeflags &= ~O_NONBLOCK; old_cookie = cookie; cookie = new_cookie; kind = BACKEND_W32_POLLABLE; functions = _gpgrt_functions_w32_pollable; } #endif /*HAVE_W32_SYSTEM*/ init_stream_obj (stream_new, cookie, syshd, kind, functions, modeflags, xmode); init_stream_lock (stream_new); err = do_list_add (stream_new, with_locked_list); if (err) goto out; *r_stream = stream_new; out: if (err) { trace_errno (err, ("leave: err=%d", err)); if (stream_new) { deinit_stream_obj (stream_new); destroy_stream_lock (stream_new); mem_free (stream_new->intern); mem_free (stream_new); } } #if HAVE_W32_SYSTEM else if (old_cookie) trace (("leave: success stream=%p cookie=%p,%p", *r_stream, old_cookie, cookie)); #endif else trace (("leave: success stream=%p cookie=%p", *r_stream, cookie)); return err; } /* * Deinitialize a stream object and destroy it. */ static int do_close (estream_t stream, int with_locked_list) { int err; trace (("stream %p %s", stream, with_locked_list? "(with locked list)":"")); if (stream) { do_list_remove (stream, with_locked_list); while (stream->intern->onclose) { notify_list_t tmp = stream->intern->onclose->next; if (stream->intern->onclose->fnc) stream->intern->onclose->fnc (stream, stream->intern->onclose->fnc_value); mem_free (stream->intern->onclose); stream->intern->onclose = tmp; } err = deinit_stream_obj (stream); destroy_stream_lock (stream); if (stream->intern->deallocate_buffer) mem_free (stream->buffer); mem_free (stream->intern); mem_free (stream); } else err = 0; trace_errno (err, ("stream %p err=%d", stream, err)); return err; } /* * The onclose worker function which is called with a locked * stream. */ static int do_onclose (estream_t stream, int mode, void (*fnc) (estream_t, void*), void *fnc_value) { notify_list_t item; if (!mode) { for (item = stream->intern->onclose; item; item = item->next) if (item->fnc && item->fnc == fnc && item->fnc_value == fnc_value) item->fnc = NULL; /* Disable this notification. */ } else { item = mem_alloc (sizeof *item); if (!item) return -1; item->fnc = fnc; item->fnc_value = fnc_value; item->next = stream->intern->onclose; stream->intern->onclose = item; } return 0; } /* * Try to read BYTES_TO_READ bytes from STREAM into BUFFER in * unbuffered-mode, storing the amount of bytes read at BYTES_READ. */ static int do_read_nbf (estream_t _GPGRT__RESTRICT stream, unsigned char *_GPGRT__RESTRICT buffer, size_t bytes_to_read, size_t *_GPGRT__RESTRICT bytes_read) { gpgrt_cookie_read_function_t func_read = stream->intern->func_read; size_t data_read; gpgrt_ssize_t ret; int err; data_read = 0; err = 0; while (bytes_to_read - data_read) { ret = (*func_read) (stream->intern->cookie, buffer + data_read, bytes_to_read - data_read); if (ret == -1) { err = -1; #if EWOULDBLOCK != EAGAIN if (errno == EWOULDBLOCK) _set_errno (EAGAIN); #endif break; } else if (ret) data_read += ret; else break; } stream->intern->offset += data_read; *bytes_read = data_read; return err; } /* * Helper for check_pending. */ static int check_pending_nbf (estream_t _GPGRT__RESTRICT stream) { gpgrt_cookie_read_function_t func_read = stream->intern->func_read; char buffer[1]; if (!(*func_read) (stream->intern->cookie, buffer, 0)) return 1; /* Pending bytes. */ return 0; /* No pending bytes or error. */ } /* * Try to read BYTES_TO_READ bytes from STREAM into BUFFER in * fully-buffered-mode, storing the amount of bytes read at * BYTES_READ. */ static int do_read_fbf (estream_t _GPGRT__RESTRICT stream, unsigned char *_GPGRT__RESTRICT buffer, size_t bytes_to_read, size_t *_GPGRT__RESTRICT bytes_read) { size_t data_available; size_t data_to_read; size_t data_read; int err; data_read = 0; err = 0; while ((bytes_to_read - data_read) && (! err)) { if (stream->data_offset == stream->data_len) { /* Nothing more to read in current container, try to fill container with new data. */ err = fill_stream (stream); if (! err) if (! stream->data_len) /* Filling did not result in any data read. */ break; } if (! err) { /* Filling resulted in some new data. */ data_to_read = bytes_to_read - data_read; data_available = stream->data_len - stream->data_offset; if (data_to_read > data_available) data_to_read = data_available; memcpy (buffer + data_read, stream->buffer + stream->data_offset, data_to_read); stream->data_offset += data_to_read; data_read += data_to_read; } } *bytes_read = data_read; return err; } /* * Helper for check_pending. */ static int check_pending_fbf (estream_t _GPGRT__RESTRICT stream) { gpgrt_cookie_read_function_t func_read = stream->intern->func_read; char buffer[1]; if (stream->data_offset == stream->data_len) { /* Nothing more to read in current container, check whether it would be possible to fill the container with new data. */ if (!(*func_read) (stream->intern->cookie, buffer, 0)) return 1; /* Pending bytes. */ } else return 1; return 0; } /* * Try to read BYTES_TO_READ bytes from STREAM into BUFFER in * line-buffered-mode, storing the amount of bytes read at BYTES_READ. */ static int do_read_lbf (estream_t _GPGRT__RESTRICT stream, unsigned char *_GPGRT__RESTRICT buffer, size_t bytes_to_read, size_t *_GPGRT__RESTRICT bytes_read) { int err; err = do_read_fbf (stream, buffer, bytes_to_read, bytes_read); return err; } /* * Try to read BYTES_TO_READ bytes from STREAM into BUFFER, storing * the amount of bytes read at BYTES_READ. */ static int es_readn (estream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT buffer_arg, size_t bytes_to_read, size_t *_GPGRT__RESTRICT bytes_read) { unsigned char *buffer = (unsigned char *)buffer_arg; size_t data_read_unread, data_read; int err; data_read_unread = 0; data_read = 0; err = 0; if (stream->flags.writing) { /* Switching to reading mode -> flush output. */ err = flush_stream (stream); if (err) goto out; stream->flags.writing = 0; } /* Read unread data first. */ while ((bytes_to_read - data_read_unread) && stream->unread_data_len) { buffer[data_read_unread] = stream->unread_buffer[stream->unread_data_len - 1]; stream->unread_data_len--; data_read_unread++; } switch (stream->intern->strategy) { case _IONBF: err = do_read_nbf (stream, buffer + data_read_unread, bytes_to_read - data_read_unread, &data_read); break; case _IOLBF: err = do_read_lbf (stream, buffer + data_read_unread, bytes_to_read - data_read_unread, &data_read); break; case _IOFBF: err = do_read_fbf (stream, buffer + data_read_unread, bytes_to_read - data_read_unread, &data_read); break; } out: if (bytes_read) *bytes_read = data_read_unread + data_read; return err; } /* * Return true if at least one byte is pending for read. This is a * best effort check and it it possible that bytes are still pending * even if false is returned. If the stream is in writing mode it is * switched to read mode. */ static int check_pending (estream_t _GPGRT__RESTRICT stream) { if (stream->flags.writing) { /* Switching to reading mode -> flush output. */ if (flush_stream (stream)) return 0; /* Better return 0 on error. */ stream->flags.writing = 0; } /* Check unread data first. */ if (stream->unread_data_len) return 1; switch (stream->intern->strategy) { case _IONBF: return check_pending_nbf (stream); case _IOLBF: case _IOFBF: return check_pending_fbf (stream); } return 0; } /* * Try to unread DATA_N bytes from DATA into STREAM, storing the * amount of bytes successfully unread at BYTES_UNREAD. */ static void es_unreadn (estream_t _GPGRT__RESTRICT stream, const unsigned char *_GPGRT__RESTRICT data, size_t data_n, size_t *_GPGRT__RESTRICT bytes_unread) { size_t space_left; space_left = stream->unread_buffer_size - stream->unread_data_len; if (data_n > space_left) data_n = space_left; if (! data_n) goto out; memcpy (stream->unread_buffer + stream->unread_data_len, data, data_n); stream->unread_data_len += data_n; stream->intern->indicators.eof = 0; out: if (bytes_unread) *bytes_unread = data_n; } /* * Seek in STREAM. */ static int es_seek (estream_t _GPGRT__RESTRICT stream, gpgrt_off_t offset, int whence, gpgrt_off_t *_GPGRT__RESTRICT offset_new) { gpgrt_cookie_seek_function_t func_seek = stream->intern->func_seek; int err, ret; gpgrt_off_t off; if (! func_seek) { _set_errno (EOPNOTSUPP); err = -1; goto out; } if (stream->flags.writing) { /* Flush data first in order to prevent flushing it to the wrong offset. */ err = flush_stream (stream); if (err) goto out; stream->flags.writing = 0; } off = offset; if (whence == SEEK_CUR) { off = off - stream->data_len + stream->data_offset; off -= stream->unread_data_len; } ret = (*func_seek) (stream->intern->cookie, &off, whence); if (ret == -1) { err = -1; #if EWOULDBLOCK != EAGAIN if (errno == EWOULDBLOCK) _set_errno (EAGAIN); #endif goto out; } err = 0; es_empty (stream); if (offset_new) *offset_new = off; stream->intern->indicators.eof = 0; stream->intern->offset = off; out: if (err) { if (errno == EPIPE) stream->intern->indicators.hup = 1; stream->intern->indicators.err = 1; } return err; } /* * Write BYTES_TO_WRITE bytes from BUFFER into STREAM in * unbuffered-mode, storing the amount of bytes written at * BYTES_WRITTEN. */ static int es_write_nbf (estream_t _GPGRT__RESTRICT stream, const unsigned char *_GPGRT__RESTRICT buffer, size_t bytes_to_write, size_t *_GPGRT__RESTRICT bytes_written) { gpgrt_cookie_write_function_t func_write = stream->intern->func_write; size_t data_written; gpgrt_ssize_t ret; int err; if (bytes_to_write && (! func_write)) { _set_errno (EOPNOTSUPP); err = -1; goto out; } data_written = 0; err = 0; while (bytes_to_write - data_written) { ret = (*func_write) (stream->intern->cookie, buffer + data_written, bytes_to_write - data_written); if (ret == -1) { err = -1; #if EWOULDBLOCK != EAGAIN if (errno == EWOULDBLOCK) _set_errno (EAGAIN); #endif break; } else data_written += ret; } stream->intern->offset += data_written; *bytes_written = data_written; out: return err; } /* * Write BYTES_TO_WRITE bytes from BUFFER into STREAM in * fully-buffered-mode, storing the amount of bytes written at * BYTES_WRITTEN. */ static int es_write_fbf (estream_t _GPGRT__RESTRICT stream, const unsigned char *_GPGRT__RESTRICT buffer, size_t bytes_to_write, size_t *_GPGRT__RESTRICT bytes_written) { size_t space_available; size_t data_to_write; size_t data_written; int err; data_written = 0; err = 0; while ((bytes_to_write - data_written) && (! err)) { if (stream->data_offset == stream->buffer_size) /* Container full, flush buffer. */ err = flush_stream (stream); if (! err) { /* Flushing resulted in empty container. */ data_to_write = bytes_to_write - data_written; space_available = stream->buffer_size - stream->data_offset; if (data_to_write > space_available) data_to_write = space_available; memcpy (stream->buffer + stream->data_offset, buffer + data_written, data_to_write); stream->data_offset += data_to_write; data_written += data_to_write; } } *bytes_written = data_written; return err; } /* Write BYTES_TO_WRITE bytes from BUFFER into STREAM in line-buffered-mode, storing the amount of bytes written in *BYTES_WRITTEN. */ static int es_write_lbf (estream_t _GPGRT__RESTRICT stream, const unsigned char *_GPGRT__RESTRICT buffer, size_t bytes_to_write, size_t *_GPGRT__RESTRICT bytes_written) { size_t data_flushed = 0; size_t data_buffered = 0; unsigned char *nlp; int err = 0; nlp = memrchr (buffer, '\n', bytes_to_write); if (nlp) { /* Found a newline, directly write up to (including) this character. */ err = flush_stream (stream); if (!err) err = es_write_nbf (stream, buffer, nlp - buffer + 1, &data_flushed); } if (!err) { /* Write remaining data fully buffered. */ err = es_write_fbf (stream, buffer + data_flushed, bytes_to_write - data_flushed, &data_buffered); } *bytes_written = data_flushed + data_buffered; return err; } /* Write BYTES_TO_WRITE bytes from BUFFER into STREAM in, storing the amount of bytes written in BYTES_WRITTEN. */ static int es_writen (estream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t bytes_to_write, size_t *_GPGRT__RESTRICT bytes_written) { size_t data_written; int err; data_written = 0; err = 0; if (!stream->flags.writing) { /* Switching to writing mode -> discard input data and seek to position at which reading has stopped. We can do this only if a seek function has been registered. */ if (stream->intern->func_seek) { err = es_seek (stream, 0, SEEK_CUR, NULL); if (err) { if (errno == ESPIPE) err = 0; else goto out; } stream->flags.writing = 1; } } switch (stream->intern->strategy) { case _IONBF: err = es_write_nbf (stream, buffer, bytes_to_write, &data_written); break; case _IOLBF: err = es_write_lbf (stream, buffer, bytes_to_write, &data_written); break; case _IOFBF: err = es_write_fbf (stream, buffer, bytes_to_write, &data_written); break; } out: if (bytes_written) *bytes_written = data_written; return err; } static int peek_stream (estream_t _GPGRT__RESTRICT stream, unsigned char **_GPGRT__RESTRICT data, size_t *_GPGRT__RESTRICT data_len) { int err; if (stream->flags.writing) { /* Switching to reading mode -> flush output. */ err = flush_stream (stream); if (err) goto out; stream->flags.writing = 0; } if (stream->data_offset == stream->data_len) { /* Refill container. */ err = fill_stream (stream); if (err) goto out; } if (data) *data = stream->buffer + stream->data_offset; if (data_len) *data_len = stream->data_len - stream->data_offset; err = 0; out: return err; } /* Skip SIZE bytes of input data contained in buffer. */ static int skip_stream (estream_t stream, size_t size) { int err; if (stream->data_offset + size > stream->data_len) { _set_errno (EINVAL); err = -1; } else { stream->data_offset += size; err = 0; } return err; } static int doreadline (estream_t _GPGRT__RESTRICT stream, size_t max_length, char *_GPGRT__RESTRICT *_GPGRT__RESTRICT line, size_t *_GPGRT__RESTRICT line_length) { size_t line_size; estream_t line_stream; char *line_new; void *line_stream_cookie; char *newline; unsigned char *data; size_t data_len; int err; es_syshd_t syshd; line_new = NULL; line_stream = NULL; line_stream_cookie = NULL; err = func_mem_create (&line_stream_cookie, NULL, 0, 0, BUFFER_BLOCK_SIZE, 1, mem_realloc, mem_free, O_RDWR, 0); if (err) goto out; memset (&syshd, 0, sizeof syshd); err = create_stream (&line_stream, line_stream_cookie, &syshd, BACKEND_MEM, estream_functions_mem, O_RDWR, 1, 0); if (err) goto out; { size_t space_left = max_length; line_size = 0; for (;;) { if (max_length && (space_left == 1)) break; err = peek_stream (stream, &data, &data_len); if (err || (! data_len)) break; if (data_len > (space_left - 1)) data_len = space_left - 1; newline = memchr (data, '\n', data_len); if (newline) { data_len = (newline - (char *) data) + 1; err = _gpgrt_write (line_stream, data, data_len, NULL); if (! err) { /* Not needed: space_left -= data_len */ line_size += data_len; skip_stream (stream, data_len); break; /* endless loop */ } } else { err = _gpgrt_write (line_stream, data, data_len, NULL); if (! err) { space_left -= data_len; line_size += data_len; skip_stream (stream, data_len); } } if (err) break; } } if (err) goto out; /* Complete line has been written to line_stream. */ if ((max_length > 1) && (! line_size)) { stream->intern->indicators.eof = 1; goto out; } err = es_seek (line_stream, 0, SEEK_SET, NULL); if (err) goto out; if (! *line) { line_new = mem_alloc (line_size + 1); if (! line_new) { err = -1; goto out; } } else line_new = *line; err = _gpgrt_read (line_stream, line_new, line_size, NULL); if (err) goto out; line_new[line_size] = '\0'; if (! *line) *line = line_new; if (line_length) *line_length = line_size; out: if (line_stream) do_close (line_stream, 0); else if (line_stream_cookie) func_mem_destroy (line_stream_cookie); if (err) { if (! *line) mem_free (line_new); stream->intern->indicators.err = 1; } return err; } /* Output function used by estream_format. */ static int print_writer (void *outfncarg, const char *buf, size_t buflen) { estream_t stream = outfncarg; size_t nwritten; int rc; nwritten = 0; rc = es_writen (stream, buf, buflen, &nwritten); stream->intern->print_ntotal += nwritten; return rc; } /* The core of our printf function. This is called in locked state. */ static int do_print_stream (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) { int rc; stream->intern->print_ntotal = 0; rc = _gpgrt_estream_format (print_writer, stream, format, ap); if (rc) return -1; return (int)stream->intern->print_ntotal; } static int es_set_buffering (estream_t _GPGRT__RESTRICT stream, char *_GPGRT__RESTRICT buffer, int mode, size_t size) { int err; /* Flush or empty buffer depending on mode. */ if (stream->flags.writing) { err = flush_stream (stream); if (err) goto out; } else es_empty (stream); stream->intern->indicators.eof = 0; /* Free old buffer in case that was allocated by this function. */ if (stream->intern->deallocate_buffer) { stream->intern->deallocate_buffer = 0; mem_free (stream->buffer); stream->buffer = NULL; } if (mode == _IONBF) stream->buffer_size = 0; else { void *buffer_new; if (buffer) buffer_new = buffer; else { if (!size) size = BUFSIZ; buffer_new = mem_alloc (size); if (! buffer_new) { err = -1; goto out; } } stream->buffer = buffer_new; stream->buffer_size = size; if (! buffer) stream->intern->deallocate_buffer = 1; } stream->intern->strategy = mode; err = 0; out: return err; } static gpgrt_off_t es_offset_calculate (estream_t stream) { gpgrt_off_t offset; offset = stream->intern->offset + stream->data_offset; if (offset < stream->unread_data_len) /* Offset undefined. */ offset = 0; else offset -= stream->unread_data_len; return offset; } static void es_opaque_ctrl (estream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT opaque_new, void **_GPGRT__RESTRICT opaque_old) { if (opaque_old) *opaque_old = stream->intern->opaque; if (opaque_new) stream->intern->opaque = opaque_new; } /* API. */ estream_t _gpgrt_fopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode) { unsigned int modeflags, cmode, xmode; int create_called; estream_t stream; void *cookie; int err; int fd; es_syshd_t syshd; stream = NULL; cookie = NULL; create_called = 0; err = parse_mode (mode, &modeflags, &xmode, &cmode); if (err) goto out; err = func_file_create (&cookie, &fd, path, modeflags, cmode); if (err) goto out; syshd.type = ES_SYSHD_FD; syshd.u.fd = fd; create_called = 1; err = create_stream (&stream, cookie, &syshd, BACKEND_FD, estream_functions_fd, modeflags, xmode, 0); if (err) goto out; if (stream && path) fname_set_internal (stream, path, 1); out: if (err && create_called) (*estream_functions_fd.public.func_close) (cookie); return stream; } /* Create a new estream object in memory. If DATA is not NULL this buffer will be used as the memory buffer; thus after this functions returns with the success the the memory at DATA belongs to the new estream. The allocated length of DATA is given by DATA_LEN and its used length by DATA_N. Usually this is malloced buffer; if a static buffer is provided, the caller must pass false for GROW and provide a dummy function for FUNC_FREE. FUNC_FREE and FUNC_REALLOC allow the caller to provide custom functions for realloc and free to be used by the new estream object. Note that the realloc function is also used for initial allocation. If DATA is NULL a buffer is internally allocated; either using internal function or those provide by the caller. It is an error to provide a realloc function but no free function. Providing only a free function is allowed as long as GROW is false. */ estream_t _gpgrt_mopen (void *_GPGRT__RESTRICT data, size_t data_n, size_t data_len, unsigned int grow, func_realloc_t func_realloc, func_free_t func_free, const char *_GPGRT__RESTRICT mode) { int create_called = 0; estream_t stream = NULL; void *cookie = NULL; unsigned int modeflags, xmode; int err; es_syshd_t syshd; err = parse_mode (mode, &modeflags, &xmode, NULL); if (err) goto out; err = func_mem_create (&cookie, data, data_n, data_len, BUFFER_BLOCK_SIZE, grow, func_realloc, func_free, modeflags, 0); if (err) goto out; memset (&syshd, 0, sizeof syshd); create_called = 1; err = create_stream (&stream, cookie, &syshd, BACKEND_MEM, estream_functions_mem, modeflags, xmode, 0); out: if (err && create_called) (*estream_functions_mem.public.func_close) (cookie); return stream; } estream_t _gpgrt_fopenmem (size_t memlimit, const char *_GPGRT__RESTRICT mode) { unsigned int modeflags, xmode; estream_t stream = NULL; void *cookie = NULL; es_syshd_t syshd; /* Memory streams are always read/write. We use MODE only to get the append flag. */ if (parse_mode (mode, &modeflags, &xmode, NULL)) return NULL; modeflags |= O_RDWR; if (func_mem_create (&cookie, NULL, 0, 0, BUFFER_BLOCK_SIZE, 1, mem_realloc, mem_free, modeflags, memlimit)) return NULL; memset (&syshd, 0, sizeof syshd); if (create_stream (&stream, cookie, &syshd, BACKEND_MEM, estream_functions_mem, modeflags, xmode, 0)) (*estream_functions_mem.public.func_close) (cookie); return stream; } /* This is the same as es_fopenmem but intializes the memory with a copy of (DATA,DATALEN). The stream is initially set to the beginning. If MEMLIMIT is not 0 but shorter than DATALEN it DATALEN will be used as the value for MEMLIMIT. */ estream_t _gpgrt_fopenmem_init (size_t memlimit, const char *_GPGRT__RESTRICT mode, const void *data, size_t datalen) { estream_t stream; if (memlimit && memlimit < datalen) memlimit = datalen; stream = _gpgrt_fopenmem (memlimit, mode); if (stream && data && datalen) { if (es_writen (stream, data, datalen, NULL)) { int saveerrno = errno; _gpgrt_fclose (stream); stream = NULL; _set_errno (saveerrno); } else { es_seek (stream, 0L, SEEK_SET, NULL); stream->intern->indicators.eof = 0; stream->intern->indicators.err = 0; } } return stream; } estream_t _gpgrt_fopencookie (void *_GPGRT__RESTRICT cookie, const char *_GPGRT__RESTRICT mode, gpgrt_cookie_io_functions_t functions) { unsigned int modeflags, xmode; estream_t stream; int err; es_syshd_t syshd; struct cookie_io_functions_s io_functions = { functions, NULL, }; stream = NULL; modeflags = 0; err = parse_mode (mode, &modeflags, &xmode, NULL); if (err) goto out; memset (&syshd, 0, sizeof syshd); err = create_stream (&stream, cookie, &syshd, BACKEND_USER, io_functions, modeflags, xmode, 0); if (err) goto out; out: return stream; } static estream_t do_fdopen (int filedes, const char *mode, int no_close, int with_locked_list) { int create_called = 0; estream_t stream = NULL; void *cookie = NULL; unsigned int modeflags, xmode; int err; es_syshd_t syshd; err = parse_mode (mode, &modeflags, &xmode, NULL); if (err) goto out; if ((xmode & X_SYSOPEN)) { /* Not allowed for fdopen. */ _set_errno (EINVAL); err = -1; goto out; } err = func_fd_create (&cookie, filedes, modeflags, no_close); if (err) goto out; syshd.type = ES_SYSHD_FD; syshd.u.fd = filedes; create_called = 1; err = create_stream (&stream, cookie, &syshd, BACKEND_FD, estream_functions_fd, modeflags, xmode, with_locked_list); if (!err && stream) { if ((modeflags & O_NONBLOCK)) err = stream->intern->func_ioctl (cookie, COOKIE_IOCTL_NONBLOCK, "", NULL); } out: if (err && create_called) (*estream_functions_fd.public.func_close) (cookie); return stream; } estream_t _gpgrt_fdopen (int filedes, const char *mode) { return do_fdopen (filedes, mode, 0, 0); } /* A variant of es_fdopen which does not close FILEDES at the end. */ estream_t _gpgrt_fdopen_nc (int filedes, const char *mode) { return do_fdopen (filedes, mode, 1, 0); } static estream_t do_fpopen (FILE *fp, const char *mode, int no_close, int with_locked_list) { unsigned int modeflags, cmode, xmode; int create_called = 0; estream_t stream = NULL; void *cookie = NULL; int err; es_syshd_t syshd; err = parse_mode (mode, &modeflags, &xmode, &cmode); if (err) goto out; if ((xmode & X_SYSOPEN)) { /* Not allowed for fpopen. */ _set_errno (EINVAL); err = -1; goto out; } if (fp) fflush (fp); err = func_fp_create (&cookie, fp, modeflags, no_close); if (err) goto out; syshd.type = ES_SYSHD_FD; syshd.u.fd = fp? fileno (fp): -1; create_called = 1; err = create_stream (&stream, cookie, &syshd, BACKEND_FP, estream_functions_fp, modeflags, xmode, with_locked_list); out: if (err && create_called) (*estream_functions_fp.public.func_close) (cookie); return stream; } /* Create an estream from the stdio stream FP. This mechanism is useful in case the stdio streams have special properties and may not be mixed with fd based functions. This is for example the case under Windows where the 3 standard streams are associated with the console whereas a duped and fd-opened stream of one of this stream won't be associated with the console. As this messes things up it is easier to keep on using the standard I/O stream as a backend for estream. */ estream_t _gpgrt_fpopen (FILE *fp, const char *mode) { return do_fpopen (fp, mode, 0, 0); } /* Same as es_fpopen but does not close FP at the end. */ estream_t _gpgrt_fpopen_nc (FILE *fp, const char *mode) { return do_fpopen (fp, mode, 1, 0); } #ifdef HAVE_W32_SYSTEM estream_t do_w32open (HANDLE hd, const char *mode, int no_close, int with_locked_list) { unsigned int modeflags, cmode, xmode; int create_called = 0; estream_t stream = NULL; void *cookie = NULL; int err; es_syshd_t syshd; /* For obvious reasons we ignore sysmode here. */ err = parse_mode (mode, &modeflags, &xmode, &cmode); if (err) goto leave; /* If we are pollable we create the function cookie with syscall * clamp disabled. This is because functions are called from * separatre reader and writer threads in w32-stream. */ err = func_w32_create (&cookie, hd, modeflags, no_close, !!(xmode & X_POLLABLE)); if (err) goto leave; syshd.type = ES_SYSHD_HANDLE; syshd.u.handle = hd; create_called = 1; err = create_stream (&stream, cookie, &syshd, BACKEND_W32, estream_functions_w32, modeflags, xmode, with_locked_list); leave: if (err && create_called) (*estream_functions_w32.public.func_close) (cookie); return stream; } #endif /*HAVE_W32_SYSTEM*/ static estream_t do_sysopen (es_syshd_t *syshd, const char *mode, int no_close) { estream_t stream; switch (syshd->type) { case ES_SYSHD_FD: case ES_SYSHD_SOCK: stream = do_fdopen (syshd->u.fd, mode, no_close, 0); break; #ifdef HAVE_W32_SYSTEM case ES_SYSHD_HANDLE: stream = do_w32open (syshd->u.handle, mode, no_close, 0); break; #endif /* FIXME: Support RVIDs under Wince? */ default: _set_errno (EINVAL); stream = NULL; } return stream; } /* On POSIX systems this function is an alias for es_fdopen. Under Windows it uses the bare W32 API and thus a HANDLE instead of a file descriptor. */ estream_t _gpgrt_sysopen (es_syshd_t *syshd, const char *mode) { return do_sysopen (syshd, mode, 0); } /* Same as es_sysopen but the handle/fd will not be closed by es_fclose. */ estream_t _gpgrt_sysopen_nc (es_syshd_t *syshd, const char *mode) { return do_sysopen (syshd, mode, 1); } /* Set custom standard descriptors to be used for stdin, stdout and stderr. This function needs to be called before any of the standard streams are accessed. This internal version uses a double dash inside its name. */ void _gpgrt__set_std_fd (int no, int fd) { /* fprintf (stderr, "es_set_std_fd(%d, %d)\n", no, fd); */ lock_list (); if (no >= 0 && no < 3 && !custom_std_fds_valid[no]) { custom_std_fds[no] = fd; custom_std_fds_valid[no] = 1; } unlock_list (); } /* Return the stream used for stdin, stdout or stderr. This internal version uses a double dash inside its name. */ estream_t _gpgrt__get_std_stream (int fd) { estream_list_t list_obj; estream_t stream = NULL; fd %= 3; /* We only allow 0, 1 or 2 but we don't want to return an error. */ lock_list (); for (list_obj = estream_list; list_obj; list_obj = list_obj->next) if (list_obj->stream && list_obj->stream->intern->is_stdstream && list_obj->stream->intern->stdstream_fd == fd) { stream = list_obj->stream; break; } if (!stream) { /* Standard stream not yet created. We first try to create them from registered file descriptors. */ if (!fd && custom_std_fds_valid[0]) stream = do_fdopen (custom_std_fds[0], "r", 1, 1); else if (fd == 1 && custom_std_fds_valid[1]) stream = do_fdopen (custom_std_fds[1], "a", 1, 1); else if (custom_std_fds_valid[2]) stream = do_fdopen (custom_std_fds[2], "a", 1, 1); if (!stream) { /* Second try is to use the standard C streams. */ if (!fd) stream = do_fpopen (stdin, "r", 1, 1); else if (fd == 1) stream = do_fpopen (stdout, "a", 1, 1); else stream = do_fpopen (stderr, "a", 1, 1); } if (!stream) { /* Last try: Create a bit bucket. */ stream = do_fpopen (NULL, fd? "a":"r", 0, 1); if (!stream) { fprintf (stderr, "fatal: error creating a dummy estream" " for %d: %s\n", fd, strerror (errno)); abort(); } } stream->intern->is_stdstream = 1; stream->intern->stdstream_fd = fd; if (fd == 2) es_set_buffering (stream, NULL, _IOLBF, 0); fname_set_internal (stream, fd == 0? "[stdin]" : fd == 1? "[stdout]" : "[stderr]", 0); } unlock_list (); return stream; } /* Note: A "samethread" keyword given in "mode" is ignored and the * value used by STREAM is used instead. Note that this function is * the reasons why some of the init and deinit code is split up into * several functions. */ estream_t _gpgrt_freopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode, estream_t _GPGRT__RESTRICT stream) { int err; if (path) { unsigned int modeflags, cmode, xmode, dummy; int create_called; void *cookie; int fd; es_syshd_t syshd; cookie = NULL; create_called = 0; xmode = stream->intern->samethread ? X_SAMETHREAD : 0; lock_stream (stream); deinit_stream_obj (stream); err = parse_mode (mode, &modeflags, &dummy, &cmode); if (err) goto leave; (void)dummy; err = func_file_create (&cookie, &fd, path, modeflags, cmode); if (err) goto leave; syshd.type = ES_SYSHD_FD; syshd.u.fd = fd; create_called = 1; init_stream_obj (stream, cookie, &syshd, BACKEND_FD, estream_functions_fd, modeflags, xmode); leave: if (err) { if (create_called) func_fd_destroy (cookie); do_close (stream, 0); stream = NULL; } else { if (path) fname_set_internal (stream, path, 1); unlock_stream (stream); } } else { /* FIXME? We don't support re-opening at the moment. */ _set_errno (EINVAL); deinit_stream_obj (stream); do_close (stream, 0); stream = NULL; } return stream; } int _gpgrt_fclose (estream_t stream) { int err; err = do_close (stream, 0); return err; } /* This is a special version of es_fclose which can be used with es_fopenmem to return the memory buffer. This is feature is useful to write to a memory buffer using estream. Note that the function does not close the stream if the stream does not support snatching the buffer. On error NULL is stored at R_BUFFER. Note that if no write operation has happened, NULL may also be stored at BUFFER on success. The caller needs to release the returned memory using gpgrt_free. */ int _gpgrt_fclose_snatch (estream_t stream, void **r_buffer, size_t *r_buflen) { int err; /* Note: There is no need to lock the stream in a close call. The object will be destroyed after the close and thus any other contender for the lock would work on a closed stream. */ if (r_buffer) { cookie_ioctl_function_t func_ioctl = stream->intern->func_ioctl; size_t buflen; *r_buffer = NULL; if (!func_ioctl) { _set_errno (EOPNOTSUPP); err = -1; goto leave; } if (stream->flags.writing) { err = flush_stream (stream); if (err) goto leave; stream->flags.writing = 0; } err = func_ioctl (stream->intern->cookie, COOKIE_IOCTL_SNATCH_BUFFER, r_buffer, &buflen); if (err) goto leave; if (r_buflen) *r_buflen = buflen; } err = do_close (stream, 0); leave: if (err && r_buffer) { mem_free (*r_buffer); *r_buffer = NULL; } return err; } /* Register or unregister a close notification function for STREAM. FNC is the function to call and FNC_VALUE the value passed as second argument. To register the notification the value for MODE must be 1. If mode is 0 the function tries to remove or disable an already registered notification; for this to work the value of FNC and FNC_VALUE must be the same as with the registration and FNC_VALUE must be a unique value. No error will be returned if MODE is 0. FIXME: I think the next comment is not anymore correct: Unregister should only be used in the error case because it may not be able to remove memory internally allocated for the onclose handler. FIXME: Unregister is not thread safe. The notification will be called right before the stream is closed. It may not call any estream function for STREAM, neither direct nor indirectly. */ int _gpgrt_onclose (estream_t stream, int mode, void (*fnc) (estream_t, void*), void *fnc_value) { int err; lock_stream (stream); err = do_onclose (stream, mode, fnc, fnc_value); unlock_stream (stream); return err; } int _gpgrt_fileno_unlocked (estream_t stream) { es_syshd_t syshd; if (_gpgrt_syshd_unlocked (stream, &syshd)) return -1; switch (syshd.type) { case ES_SYSHD_FD: return syshd.u.fd; case ES_SYSHD_SOCK: return syshd.u.sock; default: _set_errno (EINVAL); return -1; } } /* Return the handle of a stream which has been opened by es_sysopen. The caller needs to pass a structure which will be filled with the sys handle. Return 0 on success or true on error and sets errno. This is the unlocked version. */ int _gpgrt_syshd_unlocked (estream_t stream, es_syshd_t *syshd) { if (!stream || !syshd || stream->intern->syshd.type == ES_SYSHD_NONE) { if (syshd) syshd->type = ES_SYSHD_NONE; _set_errno (EINVAL); return -1; } *syshd = stream->intern->syshd; return 0; } void _gpgrt_flockfile (estream_t stream) { lock_stream (stream); } int _gpgrt_ftrylockfile (estream_t stream) { return trylock_stream (stream); } void _gpgrt_funlockfile (estream_t stream) { unlock_stream (stream); } int _gpgrt_fileno (estream_t stream) { int ret; lock_stream (stream); ret = _gpgrt_fileno_unlocked (stream); unlock_stream (stream); return ret; } /* Return the handle of a stream which has been opened by es_sysopen. The caller needs to pass a structure which will be filled with the sys handle. Return 0 on success or true on error and sets errno. This is the unlocked version. */ int _gpgrt_syshd (estream_t stream, es_syshd_t *syshd) { int ret; lock_stream (stream); ret = _gpgrt_syshd_unlocked (stream, syshd); unlock_stream (stream); return ret; } int _gpgrt__pending_unlocked (estream_t stream) { return check_pending (stream); } /* Return true if there is at least one byte pending for read on STREAM. This does only work if the backend supports checking for pending bytes and is thus mostly useful with cookie based backends. Note that if this function is used with cookie based functions, the read cookie may be called with 0 for the SIZE argument. If bytes are pending the function is expected to return -1 in this case and thus deviates from the standard behavior of read(2). */ int _gpgrt__pending (estream_t stream) { int ret; lock_stream (stream); ret = _gpgrt__pending_unlocked (stream); unlock_stream (stream); return ret; } int _gpgrt_feof_unlocked (estream_t stream) { return stream->intern->indicators.eof; } int _gpgrt_feof (estream_t stream) { int ret; lock_stream (stream); ret = _gpgrt_feof_unlocked (stream); unlock_stream (stream); return ret; } int _gpgrt_ferror_unlocked (estream_t stream) { return stream->intern->indicators.err; } int _gpgrt_ferror (estream_t stream) { int ret; lock_stream (stream); ret = _gpgrt_ferror_unlocked (stream); unlock_stream (stream); return ret; } void _gpgrt_clearerr_unlocked (estream_t stream) { stream->intern->indicators.eof = 0; stream->intern->indicators.err = 0; /* We do not reset the HUP indicator because there is no way to get out of this state. */ } void _gpgrt_clearerr (estream_t stream) { lock_stream (stream); _gpgrt_clearerr_unlocked (stream); unlock_stream (stream); } static int do_fflush (estream_t stream) { int err; if (stream->flags.writing) err = flush_stream (stream); else { es_empty (stream); err = 0; } return err; } int _gpgrt_fflush (estream_t stream) { int err; if (stream) { lock_stream (stream); err = do_fflush (stream); unlock_stream (stream); } else { estream_list_t item; err = 0; lock_list (); for (item = estream_list; item; item = item->next) if (item->stream) { lock_stream (item->stream); err |= do_fflush (item->stream); unlock_stream (item->stream); } unlock_list (); } return err ? EOF : 0; } int _gpgrt_fseek (estream_t stream, long int offset, int whence) { int err; lock_stream (stream); err = es_seek (stream, offset, whence, NULL); unlock_stream (stream); return err; } int _gpgrt_fseeko (estream_t stream, gpgrt_off_t offset, int whence) { int err; lock_stream (stream); err = es_seek (stream, offset, whence, NULL); unlock_stream (stream); return err; } long int _gpgrt_ftell (estream_t stream) { long int ret; lock_stream (stream); ret = es_offset_calculate (stream); unlock_stream (stream); return ret; } gpgrt_off_t _gpgrt_ftello (estream_t stream) { gpgrt_off_t ret = -1; lock_stream (stream); ret = es_offset_calculate (stream); unlock_stream (stream); return ret; } void _gpgrt_rewind (estream_t stream) { lock_stream (stream); es_seek (stream, 0L, SEEK_SET, NULL); /* Note that es_seek already cleared the EOF flag. */ stream->intern->indicators.err = 0; unlock_stream (stream); } +int +_gpgrt_ftruncate (estream_t stream, gpgrt_off_t length) +{ + cookie_ioctl_function_t func_ioctl; + int ret; + + lock_stream (stream); + func_ioctl = stream->intern->func_ioctl; + if (!func_ioctl) + { + _set_errno (EOPNOTSUPP); + ret = -1; + } + else + { + ret = func_ioctl (stream->intern->cookie, COOKIE_IOCTL_TRUNCATE, + &length, NULL); + } + unlock_stream (stream); + return ret; +} + + int _gpgrt__getc_underflow (estream_t stream) { int err; unsigned char c; size_t bytes_read; err = es_readn (stream, &c, 1, &bytes_read); return (err || (! bytes_read)) ? EOF : c; } int _gpgrt__putc_overflow (int c, estream_t stream) { unsigned char d = c; int err; err = es_writen (stream, &d, 1, NULL); return err ? EOF : c; } int _gpgrt_fgetc (estream_t stream) { int ret; lock_stream (stream); ret = _gpgrt_getc_unlocked (stream); unlock_stream (stream); return ret; } int _gpgrt_fputc (int c, estream_t stream) { int ret; lock_stream (stream); ret = _gpgrt_putc_unlocked (c, stream); unlock_stream (stream); return ret; } int _gpgrt_ungetc (int c, estream_t stream) { unsigned char data = (unsigned char) c; size_t data_unread; lock_stream (stream); es_unreadn (stream, &data, 1, &data_unread); unlock_stream (stream); return data_unread ? c : EOF; } int _gpgrt_read (estream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT buffer, size_t bytes_to_read, size_t *_GPGRT__RESTRICT bytes_read) { int err; if (bytes_to_read) { lock_stream (stream); err = es_readn (stream, buffer, bytes_to_read, bytes_read); unlock_stream (stream); } else err = 0; return err; } int _gpgrt_write (estream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t bytes_to_write, size_t *_GPGRT__RESTRICT bytes_written) { int err; if (bytes_to_write) { lock_stream (stream); err = es_writen (stream, buffer, bytes_to_write, bytes_written); unlock_stream (stream); } else err = 0; return err; } size_t _gpgrt_fread (void *_GPGRT__RESTRICT ptr, size_t size, size_t nitems, estream_t _GPGRT__RESTRICT stream) { size_t ret, bytes; if (size && nitems) { lock_stream (stream); es_readn (stream, ptr, size * nitems, &bytes); unlock_stream (stream); ret = bytes / size; } else ret = 0; return ret; } size_t _gpgrt_fwrite (const void *_GPGRT__RESTRICT ptr, size_t size, size_t nitems, estream_t _GPGRT__RESTRICT stream) { size_t ret, bytes; if (size && nitems) { lock_stream (stream); es_writen (stream, ptr, size * nitems, &bytes); unlock_stream (stream); ret = bytes / size; } else ret = 0; return ret; } char * _gpgrt_fgets (char *_GPGRT__RESTRICT buffer, int length, estream_t _GPGRT__RESTRICT stream) { unsigned char *s = (unsigned char*)buffer; int c; if (!length) return NULL; c = EOF; lock_stream (stream); while (length > 1 && (c = _gpgrt_getc_unlocked (stream)) != EOF && c != '\n') { *s++ = c; length--; } unlock_stream (stream); if (c == EOF && s == (unsigned char*)buffer) return NULL; /* Nothing read. */ if (c != EOF && length > 1) *s++ = c; *s = 0; return buffer; } int _gpgrt_fputs_unlocked (const char *_GPGRT__RESTRICT s, estream_t _GPGRT__RESTRICT stream) { size_t length; int err; length = strlen (s); err = es_writen (stream, s, length, NULL); return err ? EOF : 0; } int _gpgrt_fputs (const char *_GPGRT__RESTRICT s, estream_t _GPGRT__RESTRICT stream) { size_t length; int err; length = strlen (s); lock_stream (stream); err = es_writen (stream, s, length, NULL); unlock_stream (stream); return err ? EOF : 0; } gpgrt_ssize_t _gpgrt_getline (char *_GPGRT__RESTRICT *_GPGRT__RESTRICT lineptr, size_t *_GPGRT__RESTRICT n, estream_t _GPGRT__RESTRICT stream) { char *line = NULL; size_t line_n = 0; int err; lock_stream (stream); err = doreadline (stream, 0, &line, &line_n); unlock_stream (stream); if (err) goto out; if (*n) { /* Caller wants us to use his buffer. */ if (*n < (line_n + 1)) { /* Provided buffer is too small -> resize. */ void *p; p = mem_realloc (*lineptr, line_n + 1); if (! p) err = -1; else { if (*lineptr != p) *lineptr = p; } } if (! err) { memcpy (*lineptr, line, line_n + 1); if (*n != line_n) *n = line_n; } mem_free (line); } else { /* Caller wants new buffers. */ *lineptr = line; *n = line_n; } out: return err ? err : (gpgrt_ssize_t)line_n; } /* Same as fgets() but if the provided buffer is too short a larger one will be allocated. This is similar to getline. A line is considered a byte stream ending in a LF. If MAX_LENGTH is not NULL, it shall point to a value with the maximum allowed allocation. Returns the length of the line. EOF is indicated by a line of length zero. A truncated line is indicated my setting the value at MAX_LENGTH to 0. If the returned value is less then 0 not enough memory was available or another error occurred; ERRNO is then set accordingly. If a line has been truncated, the file pointer is moved forward to the end of the line so that the next read starts with the next line. Note that MAX_LENGTH must be re-initialzied in this case. The caller initially needs to provide the address of a variable, initialized to NULL, at ADDR_OF_BUFFER and don't change this value anymore with the following invocations. LENGTH_OF_BUFFER should be the address of a variable, initialized to 0, which is also maintained by this function. Thus, both paramaters should be considered the state of this function. Note: The returned buffer is allocated with enough extra space to allow the caller to append a CR,LF,Nul. The buffer should be released using gpgrt_free. */ gpgrt_ssize_t _gpgrt_read_line (estream_t stream, char **addr_of_buffer, size_t *length_of_buffer, size_t *max_length) { int c; char *buffer = *addr_of_buffer; size_t length = *length_of_buffer; size_t nbytes = 0; size_t maxlen = max_length? *max_length : 0; char *p; if (!buffer) { /* No buffer given - allocate a new one. */ length = 256; buffer = mem_alloc (length); *addr_of_buffer = buffer; if (!buffer) { *length_of_buffer = 0; if (max_length) *max_length = 0; return -1; } *length_of_buffer = length; } if (length < 4) { /* This should never happen. If it does, the function has been called with wrong arguments. */ _set_errno (EINVAL); return -1; } length -= 3; /* Reserve 3 bytes for CR,LF,EOL. */ lock_stream (stream); p = buffer; while ((c = _gpgrt_getc_unlocked (stream)) != EOF) { if (nbytes == length) { /* Enlarge the buffer. */ if (maxlen && length > maxlen) { /* We are beyond our limit: Skip the rest of the line. */ while (c != '\n' && (c=_gpgrt_getc_unlocked (stream)) != EOF) ; *p++ = '\n'; /* Always append a LF (we reserved some space). */ nbytes++; if (max_length) *max_length = 0; /* Indicate truncation. */ break; /* the while loop. */ } length += 3; /* Adjust for the reserved bytes. */ length += length < 1024? 256 : 1024; *addr_of_buffer = mem_realloc (buffer, length); if (!*addr_of_buffer) { int save_errno = errno; mem_free (buffer); *length_of_buffer = 0; if (max_length) *max_length = 0; unlock_stream (stream); _set_errno (save_errno); return -1; } buffer = *addr_of_buffer; *length_of_buffer = length; length -= 3; p = buffer + nbytes; } *p++ = c; nbytes++; if (c == '\n') break; } *p = 0; /* Make sure the line is a string. */ unlock_stream (stream); return nbytes; } /* Wrapper around free() to match the memory allocation system used by estream. Should be used for all buffers returned to the caller by libestream. If a custom allocation handler has been set with gpgrt_set_alloc_func that register function may be used instead. This function has been moved to init.c. */ /* void */ /* _gpgrt_free (void *a) */ /* { */ /* mem_free (a); */ /* } */ int _gpgrt_vfprintf_unlocked (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) { return do_print_stream (stream, format, ap); } int _gpgrt_vfprintf (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) { int ret; lock_stream (stream); ret = do_print_stream (stream, format, ap); unlock_stream (stream); return ret; } int _gpgrt_fprintf_unlocked (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) { int ret; va_list ap; va_start (ap, format); ret = do_print_stream (stream, format, ap); va_end (ap); return ret; } int _gpgrt_fprintf (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) { int ret; va_list ap; va_start (ap, format); lock_stream (stream); ret = do_print_stream (stream, format, ap); unlock_stream (stream); va_end (ap); return ret; } static int tmpfd (void) { #ifdef HAVE_W32_SYSTEM int attempts, n; #ifdef HAVE_W32CE_SYSTEM wchar_t buffer[MAX_PATH+9+12+1]; # define mystrlen(a) wcslen (a) wchar_t *name, *p; #else char buffer[MAX_PATH+9+12+1]; # define mystrlen(a) strlen (a) char *name, *p; #endif HANDLE file; int pid = GetCurrentProcessId (); unsigned int value; int i; n = GetTempPath (MAX_PATH+1, buffer); if (!n || n > MAX_PATH || mystrlen (buffer) > MAX_PATH) { _set_errno (ENOENT); return -1; } p = buffer + mystrlen (buffer); #ifdef HAVE_W32CE_SYSTEM wcscpy (p, L"_estream"); #else strcpy (p, "_estream"); #endif p += 8; /* We try to create the directory but don't care about an error as it may already exist and the CreateFile would throw an error anyway. */ CreateDirectory (buffer, NULL); *p++ = '\\'; name = p; for (attempts=0; attempts < 10; attempts++) { p = name; value = (GetTickCount () ^ ((pid<<16) & 0xffff0000)); for (i=0; i < 8; i++) { *p++ = tohex (((value >> 28) & 0x0f)); value <<= 4; } #ifdef HAVE_W32CE_SYSTEM wcscpy (p, L".tmp"); #else strcpy (p, ".tmp"); #endif file = CreateFile (buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL); if (file != INVALID_HANDLE_VALUE) { #ifdef HAVE_W32CE_SYSTEM int fd = (int)file; #else int fd = _open_osfhandle ((long)file, 0); if (fd == -1) { CloseHandle (file); return -1; } #endif return fd; } Sleep (1); /* One ms as this is the granularity of GetTickCount. */ } _set_errno (ENOENT); return -1; #else /*!HAVE_W32_SYSTEM*/ FILE *fp; int fp_fd; int fd; fp = NULL; fd = -1; fp = tmpfile (); if (! fp) goto out; fp_fd = fileno (fp); fd = dup (fp_fd); out: if (fp) fclose (fp); return fd; #endif /*!HAVE_W32_SYSTEM*/ } estream_t _gpgrt_tmpfile (void) { unsigned int modeflags; int create_called = 0; estream_t stream = NULL; void *cookie = NULL; int err; int fd; es_syshd_t syshd; modeflags = O_RDWR | O_TRUNC | O_CREAT; fd = tmpfd (); if (fd == -1) { err = -1; goto out; } err = func_fd_create (&cookie, fd, modeflags, 0); if (err) goto out; syshd.type = ES_SYSHD_FD; syshd.u.fd = fd; create_called = 1; err = create_stream (&stream, cookie, &syshd, BACKEND_FD, estream_functions_fd, modeflags, 0, 0); out: if (err) { if (create_called) func_fd_destroy (cookie); else if (fd != -1) close (fd); stream = NULL; } return stream; } int _gpgrt_setvbuf (estream_t _GPGRT__RESTRICT stream, char *_GPGRT__RESTRICT buf, int type, size_t size) { int err; if ((type == _IOFBF || type == _IOLBF || type == _IONBF) && (!buf || size || type == _IONBF)) { lock_stream (stream); err = es_set_buffering (stream, buf, type, size); unlock_stream (stream); } else { _set_errno (EINVAL); err = -1; } return err; } /* Put a stream into binary mode. This is only needed for the standard streams if they are to be used in a binary way. On Unix systems it is never needed but MSDOS based systems require such a call. It needs to be called before any I/O is done on STREAM. */ void _gpgrt_set_binary (estream_t stream) { lock_stream (stream); if (!(stream->intern->modeflags & O_BINARY)) { stream->intern->modeflags |= O_BINARY; #ifdef HAVE_DOSISH_SYSTEM if (stream->intern->func_read == func_fd_read) { estream_cookie_fd_t fd_cookie = stream->intern->cookie; if (!IS_INVALID_FD (fd_cookie->fd)) setmode (fd_cookie->fd, O_BINARY); } else if (stream->intern->func_read == func_fp_read) { estream_cookie_fp_t fp_cookie = stream->intern->cookie; if (fp_cookie->fp) setmode (fileno (fp_cookie->fp), O_BINARY); } #endif } unlock_stream (stream); } /* Set non-blocking mode for STREAM. Use true for ONOFF to enable and false to disable non-blocking mode. Returns 0 on success or -1 on error and sets ERRNO. Note that not all backends support non-blocking mode. In non-blocking mode a system call will not block but return an error and set errno to EAGAIN. The estream API always uses EAGAIN and not EWOULDBLOCK. If a buffered function like es_fgetc() or es_fgets() returns an error and both, feof() and ferror() return false the caller may assume that the error condition was EAGAIN. Switching back from non-blocking to blocking may raise problems with buffering, thus care should be taken. Although read+write sockets are supported in theory, switching from write to read may result into problems because estream may first flush the write buffers and there is no way to handle that non-blocking (EAGAIN) case. Explicit flushing should thus be done before before switching to read. */ int _gpgrt_set_nonblock (estream_t stream, int onoff) { cookie_ioctl_function_t func_ioctl; int ret; lock_stream (stream); func_ioctl = stream->intern->func_ioctl; if (!func_ioctl) { _set_errno (EOPNOTSUPP); ret = -1; } else { unsigned int save_flags = stream->intern->modeflags; if (onoff) stream->intern->modeflags |= O_NONBLOCK; else stream->intern->modeflags &= ~O_NONBLOCK; ret = func_ioctl (stream->intern->cookie, COOKIE_IOCTL_NONBLOCK, onoff?"":NULL, NULL); if (ret) stream->intern->modeflags = save_flags; } unlock_stream (stream); return ret; } /* Return true if STREAM is in non-blocking mode. */ int _gpgrt_get_nonblock (estream_t stream) { int ret; lock_stream (stream); ret = !!(stream->intern->modeflags & O_NONBLOCK); unlock_stream (stream); return ret; } /* A version of poll(2) working on estream handles. Note that not all estream types work with this function. In contrast to the standard poll function the gpgrt_poll_t object uses a set of bit flags instead of the EVENTS and REVENTS members. An item with the IGNORE flag set is entirely ignored. The TIMEOUT values is given in milliseconds, a value of -1 waits indefinitely, and a value of 0 returns immediately. A positive return value gives the number of fds with new information. A return value of 0 indicates a timeout and -1 indicates an error in which case ERRNO is set. */ int _gpgrt_poll (gpgrt_poll_t *fds, unsigned int nfds, int timeout) { gpgrt_poll_t *item; int count = 0; int idx; #ifndef HAVE_W32_SYSTEM fd_set readfds, writefds, exceptfds; int any_readfd, any_writefd, any_exceptfd; int max_fd; int fd, ret, any; #endif /*HAVE_W32_SYSTEM*/ trace (("enter: nfds=%u timeout=%d", nfds, timeout)); if (!fds) { _set_errno (EINVAL); count = -1; goto leave; } /* Clear all response fields (even for ignored items). */ for (item = fds, idx = 0; idx < nfds; item++, idx++) { item->got_read = 0; item->got_write = 0; item->got_oob = 0; item->got_rdhup = 0; item->got_err = 0; item->got_hup = 0; item->got_nval = 0; } /* Check for pending reads. */ for (item = fds, idx = 0; idx < nfds; item++, idx++) { if (item->ignore) continue; if (!item->want_read) continue; if (_gpgrt__pending (item->stream)) { item->got_read = 1; count++; } } /* Check for space in the write buffers. */ for (item = fds, idx = 0; idx < nfds; item++, idx++) { if (item->ignore) continue; if (!item->want_write) continue; /* FIXME */ } /* Now do the real select. */ #ifdef HAVE_W32_SYSTEM _gpgrt_pre_syscall (); count = _gpgrt_w32_poll (fds, nfds, timeout); _gpgrt_post_syscall (); #else /*!HAVE_W32_SYSTEM*/ any_readfd = any_writefd = any_exceptfd = 0; max_fd = 0; for (item = fds, idx = 0; idx < nfds; item++, idx++) { if (item->ignore) continue; fd = _gpgrt_fileno (item->stream); if (fd == -1) continue; /* Stream does not support polling. */ if (item->want_read) { if (!any_readfd) { FD_ZERO (&readfds); any_readfd = 1; } FD_SET (fd, &readfds); if (fd > max_fd) max_fd = fd; } if (item->want_write) { if (!any_writefd) { FD_ZERO (&writefds); any_writefd = 1; } FD_SET (fd, &writefds); if (fd > max_fd) max_fd = fd; } if (item->want_oob) { if (!any_exceptfd) { FD_ZERO (&exceptfds); any_exceptfd = 1; } FD_SET (fd, &exceptfds); if (fd > max_fd) max_fd = fd; } } _gpgrt_pre_syscall (); do { struct timeval timeout_val; timeout_val.tv_sec = timeout / 1000; timeout_val.tv_usec = (timeout % 1000) * 1000; ret = select (max_fd+1, any_readfd? &readfds : NULL, any_writefd? &writefds : NULL, any_exceptfd? &exceptfds : NULL, timeout == -1 ? NULL : &timeout_val); } while (ret == -1 && errno == EINTR); _gpgrt_post_syscall (); if (ret == -1) { trace_errno (1, ("select failed: ")); count = -1; goto leave; } if (!ret) { /* Timeout. Note that in this case we can't return got_err for * an invalid stream. */ count = 0; goto leave; } for (item = fds, idx = 0; idx < nfds; item++, idx++) { if (item->ignore) continue; fd = _gpgrt_fileno (item->stream); if (fd == -1) { item->got_err = 1; /* Stream does not support polling. */ count++; continue; } any = 0; if (item->stream->intern->indicators.hup) { item->got_hup = 1; any = 1; } if (item->want_read && FD_ISSET (fd, &readfds)) { item->got_read = 1; any = 1; } if (item->want_write && FD_ISSET (fd, &writefds)) { item->got_write = 1; any = 1; } if (item->want_oob && FD_ISSET (fd, &exceptfds)) { item->got_oob = 1; any = 1; } if (any) count++; } #endif /*!HAVE_W32_SYSTEM*/ leave: #ifdef ENABLE_TRACING trace (("leave: count=%d", count)); if (count > 0) { for (item = fds, idx = 0; idx < nfds; item++, idx++) { trace ((" %3d %c%c%c%c%c %c%c%c%c%c%c%c", idx, fds[idx].want_read? 'r':'-', fds[idx].want_write? 'w':'-', fds[idx].want_oob? 'o':'-', fds[idx].want_rdhup? 'h':'-', fds[idx].ignore? 'i':'-', fds[idx].got_read? 'r':'-', fds[idx].got_write? 'w':'-', fds[idx].got_oob? 'o':'-', fds[idx].got_rdhup? 'h':'-', fds[idx].got_hup? 'H':'-', fds[idx].got_err? 'e':'-', fds[idx].got_nval? 'n':'-' )); } } #endif /*ENABLE_TRACING*/ return count; } void _gpgrt_opaque_set (estream_t stream, void *opaque) { lock_stream (stream); es_opaque_ctrl (stream, opaque, NULL); unlock_stream (stream); } void * _gpgrt_opaque_get (estream_t stream) { void *opaque; lock_stream (stream); es_opaque_ctrl (stream, NULL, &opaque); unlock_stream (stream); return opaque; } static void fname_set_internal (estream_t stream, const char *fname, int quote) { if (stream->intern->printable_fname && !stream->intern->printable_fname_inuse) { mem_free (stream->intern->printable_fname); stream->intern->printable_fname = NULL; } if (stream->intern->printable_fname) return; /* Can't change because it is in use. */ if (*fname != '[') quote = 0; else quote = !!quote; stream->intern->printable_fname = mem_alloc (strlen (fname) + quote + 1); if (quote) stream->intern->printable_fname[0] = '\\'; strcpy (stream->intern->printable_fname+quote, fname); } /* Set the filename attribute of STREAM. There is no error return. as long as STREAM is valid. This function is called internally by functions which open a filename. */ void _gpgrt_fname_set (estream_t stream, const char *fname) { if (fname) { lock_stream (stream); fname_set_internal (stream, fname, 1); unlock_stream (stream); } } /* Return the filename attribute of STREAM. In case no filename has been set, "[?]" will be returned. The returned file name is valid as long as STREAM is valid. */ const char * _gpgrt_fname_get (estream_t stream) { const char *fname; lock_stream (stream); fname = stream->intern->printable_fname; if (fname) stream->intern->printable_fname_inuse = 1; unlock_stream (stream); if (!fname) fname = "[?]"; return fname; } /* Print a BUFFER to STREAM while replacing all control characters and the characters in DELIMITERS by standard C escape sequences. Returns 0 on success or -1 on error. If BYTES_WRITTEN is not NULL the number of bytes actually written are stored at this address. */ int _gpgrt_write_sanitized (estream_t _GPGRT__RESTRICT stream, const void * _GPGRT__RESTRICT buffer, size_t length, const char * delimiters, size_t * _GPGRT__RESTRICT bytes_written) { const unsigned char *p = buffer; size_t count = 0; int ret; lock_stream (stream); for (; length; length--, p++, count++) { if (*p < 0x20 || *p == 0x7f || (delimiters && (strchr (delimiters, *p) || *p == '\\'))) { _gpgrt_putc_unlocked ('\\', stream); count++; if (*p == '\n') { _gpgrt_putc_unlocked ('n', stream); count++; } else if (*p == '\r') { _gpgrt_putc_unlocked ('r', stream); count++; } else if (*p == '\f') { _gpgrt_putc_unlocked ('f', stream); count++; } else if (*p == '\v') { _gpgrt_putc_unlocked ('v', stream); count++; } else if (*p == '\b') { _gpgrt_putc_unlocked ('b', stream); count++; } else if (!*p) { _gpgrt_putc_unlocked('0', stream); count++; } else { _gpgrt_fprintf_unlocked (stream, "x%02x", *p); count += 3; } } else { _gpgrt_putc_unlocked (*p, stream); count++; } } if (bytes_written) *bytes_written = count; ret = _gpgrt_ferror_unlocked (stream)? -1 : 0; unlock_stream (stream); return ret; } /* Write LENGTH bytes of BUFFER to STREAM as a hex encoded string. RESERVED must be 0. Returns 0 on success or -1 on error. If BYTES_WRITTEN is not NULL the number of bytes actually written are stored at this address. */ int _gpgrt_write_hexstring (estream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t length, int reserved, size_t *_GPGRT__RESTRICT bytes_written ) { int ret; const unsigned char *s; size_t count = 0; (void)reserved; #define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A')) if (!length) return 0; lock_stream (stream); for (s = buffer; length; s++, length--) { _gpgrt_putc_unlocked ( tohex ((*s>>4)&15), stream); _gpgrt_putc_unlocked ( tohex (*s&15), stream); count += 2; } if (bytes_written) *bytes_written = count; ret = _gpgrt_ferror_unlocked (stream)? -1 : 0; unlock_stream (stream); return ret; #undef tohex } diff --git a/src/gpg-error.def.in b/src/gpg-error.def.in index c2fabb0..9b522ea 100644 --- a/src/gpg-error.def.in +++ b/src/gpg-error.def.in @@ -1,224 +1,225 @@ /* libgpg-error.def - Exported symbols for W32 * Copyright (C) 2014 g10 Code GmbH * * This file is part of libgpg-error. * * libgpg-error is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libgpg-error is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ * * Note: This file should be updated manually and the ordinals shall * never be changed. Also check gpg-error.vers and visibility.h. * * This file needs to be pre-processed. */ #include EXPORTS gpg_strerror @1 gpg_strerror_r @2 gpg_strsource @3 gpg_err_code_from_errno @4 gpg_err_code_to_errno @5 /* @6 - Not anymore used. */ gpg_err_code_from_syserror @7 gpg_err_set_errno @8 #ifdef HAVE_W32CE_SYSTEM _gpg_w32ce_get_errno @9 _gpg_w32ce_strerror @10 #endif #ifdef HAVE_W32_SYSTEM _gpg_w32_bindtextdomain @11 _gpg_w32_textdomain @12 _gpg_w32_gettext @13 _gpg_w32_dgettext @14 _gpg_w32_dngettext @15 _gpg_w32_gettext_localename @16 _gpg_w32_gettext_use_utf8 @17 #endif /* @18 - Not anymore used. */ gpg_error_check_version @19 gpgrt_lock_init @20 gpgrt_lock_lock @21 gpgrt_lock_unlock @22 gpgrt_lock_destroy @23 gpgrt_yield @24 gpgrt_lock_trylock @25 gpgrt_set_syscall_clamp @26 gpgrt_fopen @27 gpgrt_mopen @28 gpgrt_fopenmem @29 gpgrt_fopenmem_init @30 gpgrt_fdopen @31 gpgrt_fdopen_nc @32 gpgrt_sysopen @33 gpgrt_sysopen_nc @34 gpgrt_fpopen @35 gpgrt_fpopen_nc @36 gpgrt_freopen @37 gpgrt_fopencookie @38 gpgrt_fclose @39 gpgrt_fclose_snatch @40 gpgrt_onclose @41 gpgrt_fileno @42 gpgrt_fileno_unlocked @43 gpgrt_syshd @44 gpgrt_syshd_unlocked @45 _gpgrt_set_std_fd @46 _gpgrt_get_std_stream @47 gpgrt_flockfile @48 gpgrt_ftrylockfile @49 gpgrt_funlockfile @50 gpgrt_feof @51 gpgrt_feof_unlocked @52 gpgrt_ferror @53 gpgrt_ferror_unlocked @54 gpgrt_clearerr @55 gpgrt_clearerr_unlocked @56 gpgrt_fflush @57 gpgrt_fseek @58 gpgrt_fseeko @59 gpgrt_ftell @60 gpgrt_ftello @61 gpgrt_rewind @62 gpgrt_fgetc @63 _gpgrt_getc_underflow @64 gpgrt_fputc @65 _gpgrt_putc_overflow @66 gpgrt_ungetc @67 gpgrt_read @68 gpgrt_write @69 gpgrt_write_sanitized @70 gpgrt_write_hexstring @71 gpgrt_fread @72 gpgrt_fwrite @73 gpgrt_fgets @74 gpgrt_fputs @75 gpgrt_fputs_unlocked @76 gpgrt_getline @77 gpgrt_read_line @78 gpgrt_free @79 gpgrt_fprintf @80 gpgrt_fprintf_unlocked @81 gpgrt_printf @82 gpgrt_printf_unlocked @83 gpgrt_vfprintf @84 gpgrt_vfprintf_unlocked @85 gpgrt_setvbuf @86 gpgrt_setbuf @87 gpgrt_set_binary @88 gpgrt_tmpfile @89 gpgrt_opaque_set @90 gpgrt_opaque_get @91 gpgrt_fname_set @92 gpgrt_fname_get @93 gpgrt_asprintf @94 gpgrt_vasprintf @95 gpgrt_bsprintf @96 gpgrt_vbsprintf @97 gpgrt_snprintf @98 gpgrt_vsnprintf @99 gpgrt_check_version @100 gpg_err_init @101 gpg_err_deinit @102 gpgrt_set_alloc_func @103 _gpgrt_pending @104 _gpgrt_pending_unlocked @105 gpgrt_set_nonblock @106 gpgrt_get_nonblock @107 gpgrt_poll @108 #ifdef HAVE_W32_SYSTEM gpgrt_w32_iconv_open @109 gpgrt_w32_iconv_close @110 gpgrt_w32_iconv @111 #endif gpgrt_get_syscall_clamp @112 gpgrt_b64dec_start @113 gpgrt_b64dec_proc @114 gpgrt_b64dec_finish @115 gpgrt_get_errorcount @116 gpgrt_inc_errorcount @117 gpgrt_log_set_sink @118 gpgrt_log_set_socket_dir_cb @119 gpgrt_log_set_pid_suffix_cb @120 gpgrt_log_set_prefix @121 gpgrt_log_get_prefix @122 gpgrt_log_test_fd @123 gpgrt_log_get_fd @124 gpgrt_log_get_stream @125 gpgrt_log @126 gpgrt_logv @127 gpgrt_logv_prefix @128 gpgrt_log_string @129 gpgrt_log_bug @130 gpgrt_log_fatal @131 gpgrt_log_error @132 gpgrt_log_info @133 gpgrt_log_debug @134 gpgrt_log_debug_string @135 gpgrt_log_printf @136 gpgrt_log_printhex @137 gpgrt_log_clock @138 gpgrt_log_flush @139 _gpgrt_log_assert @140 gpgrt_realloc @141 gpgrt_malloc @142 gpgrt_calloc @143 gpgrt_strdup @144 gpgrt_strconcat @145 gpgrt_w32_reg_query_string @146 gpgrt_getenv @147 gpgrt_setenv @148 gpgrt_mkdir @149 gpgrt_chdir @150 gpgrt_getcwd @151 ;; API not yet finished for: ;; gpgrt_make_pipe @152 ;; gpgrt_spawn_process @153 ;; gpgrt_spawn_process_fd @154 ;; gpgrt_spawn_process_detached @155 ;; gpgrt_wait_process @156 ;; gpgrt_wait_processes @157 ;; gpgrt_kill_process @158 ;; gpgrt_release_process @159 gpgrt_argparse @160 gpgrt_usage @161 gpgrt_strusage @162 gpgrt_set_strusage @163 gpgrt_set_usage_outfnc @164 gpgrt_set_fixed_string_mapper @165 gpgrt_b64enc_start @166 gpgrt_b64enc_write @167 gpgrt_b64enc_finish @168 gpgrt_cmp_version @169 + gpgrt_ftruncate @170 ;; end of file with public symbols for Windows. diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in index 2bf6a6f..c0ac0eb 100644 --- a/src/gpg-error.h.in +++ b/src/gpg-error.h.in @@ -1,1289 +1,1291 @@ /* gpg-error.h or gpgrt.h - Common code for GnuPG and others. -*- c -*- * Copyright (C) 2001-2018 g10 Code GmbH * * This file is part of libgpg-error (aka libgpgrt). * * libgpg-error is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libgpg-error is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ * * @configure_input@ */ /* The GnuPG project consists of many components. Error codes are * exchanged between all components. The common error codes and their * user-presentable descriptions are kept into a shared library to * allow adding new error codes and components without recompiling any * of the other components. In addition to error codes this library * also features several other groups of functions which are common to * all GnuPG components. They may be used by independet project as * well. The interfaces will not change in a backward incompatible way. * * An error code together with an error source build up an error * value. As the error value is been passed from one component to * another, it preserves the information about the source and nature * of the error. * * A component of the GnuPG project can define the following macros to * tune the behaviour of the library: * * GPG_ERR_SOURCE_DEFAULT: Define to an error source of type * gpg_err_source_t to make that source the default for gpg_error(). * Otherwise GPG_ERR_SOURCE_UNKNOWN is used as default. * * GPG_ERR_ENABLE_GETTEXT_MACROS: Define to provide macros to map the * internal gettext API to standard names. This has only an effect on * Windows platforms. * * GPGRT_ENABLE_ES_MACROS: Define to provide "es_" macros for the * estream functions. * * GPGRT_ENABLE_LOG_MACROS: Define to provide short versions of the * log functions. * * GPGRT_ENABLE_ARGPARSE_MACROS: Needs to be defined to provide the * mandatory macros of the argparse interface. */ #ifndef GPG_ERROR_H #define GPG_ERROR_H 1 #ifndef GPGRT_H #define GPGRT_H 1 #include #include #include /* The version string of this header. */ #define GPG_ERROR_VERSION @version@ #define GPGRT_VERSION @version@ /* The version number of this header. */ #define GPG_ERROR_VERSION_NUMBER @version-number@ #define GPGRT_VERSION_NUMBER @version-number@ #ifdef __GNUC__ # define GPG_ERR_INLINE __inline__ #elif defined(_MSC_VER) && _MSC_VER >= 1300 # define GPG_ERR_INLINE __inline #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L # define GPG_ERR_INLINE inline #else # ifndef GPG_ERR_INLINE # define GPG_ERR_INLINE # endif #endif #ifdef __cplusplus extern "C" { #if 0 /* just to make Emacs auto-indent happy */ } #endif #endif /* __cplusplus */ /* The error source type gpg_err_source_t. * * Where as the Poo out of a welle small * Taketh his firste springing and his sours. * --Chaucer. */ /* Only use free slots, never change or reorder the existing * entries. */ typedef enum { @include:err-sources@ /* This is one more than the largest allowed entry. */ GPG_ERR_SOURCE_DIM = 128 } gpg_err_source_t; /* The error code type gpg_err_code_t. */ /* Only use free slots, never change or reorder the existing * entries. */ typedef enum { @include:err-codes@ /* The following error codes are used to map system errors. */ #define GPG_ERR_SYSTEM_ERROR (1 << 15) @include:errnos@ /* This is one more than the largest allowed entry. */ GPG_ERR_CODE_DIM = 65536 } gpg_err_code_t; /* The error value type gpg_error_t. */ /* We would really like to use bit-fields in a struct, but using * structs as return values can cause binary compatibility issues, in * particular if you want to do it efficiently (also see * -freg-struct-return option to GCC). */ typedef unsigned int gpg_error_t; /* We use the lowest 16 bits of gpg_error_t for error codes. The 16th * bit indicates system errors. */ #define GPG_ERR_CODE_MASK (GPG_ERR_CODE_DIM - 1) /* Bits 17 to 24 are reserved. */ /* We use the upper 7 bits of gpg_error_t for error sources. */ #define GPG_ERR_SOURCE_MASK (GPG_ERR_SOURCE_DIM - 1) #define GPG_ERR_SOURCE_SHIFT 24 /* The highest bit is reserved. It shouldn't be used to prevent * potential negative numbers when transmitting error values as * text. */ /* * GCC feature test. */ #if __GNUC__ # define _GPG_ERR_GCC_VERSION (__GNUC__ * 10000 \ + __GNUC_MINOR__ * 100 \ + __GNUC_PATCHLEVEL__) #else # define _GPG_ERR_GCC_VERSION 0 #endif #undef _GPG_ERR_HAVE_CONSTRUCTOR #if _GPG_ERR_GCC_VERSION > 30100 # define _GPG_ERR_CONSTRUCTOR __attribute__ ((__constructor__)) # define _GPG_ERR_HAVE_CONSTRUCTOR #else # define _GPG_ERR_CONSTRUCTOR #endif #define GPGRT_GCC_VERSION _GPG_ERR_GCC_VERSION #if _GPG_ERR_GCC_VERSION >= 29200 # define _GPGRT__RESTRICT __restrict__ #else # define _GPGRT__RESTRICT #endif /* The noreturn attribute. */ #if _GPG_ERR_GCC_VERSION >= 20500 # define GPGRT_ATTR_NORETURN __attribute__ ((__noreturn__)) #else # define GPGRT_ATTR_NORETURN #endif /* The printf attributes. */ #if _GPG_ERR_GCC_VERSION >= 40400 # define GPGRT_ATTR_PRINTF(f, a) \ __attribute__ ((format(__gnu_printf__,f,a))) # define GPGRT_ATTR_NR_PRINTF(f, a) \ __attribute__ ((__noreturn__, format(__gnu_printf__,f,a))) #elif _GPG_ERR_GCC_VERSION >= 20500 # define GPGRT_ATTR_PRINTF(f, a) \ __attribute__ ((format(printf,f,a))) # define GPGRT_ATTR_NR_PRINTF(f, a) \ __attribute__ ((__noreturn__, format(printf,f,a))) #else # define GPGRT_ATTR_PRINTF(f, a) # define GPGRT_ATTR_NR_PRINTF(f, a) #endif #if _GPG_ERR_GCC_VERSION >= 20800 # define GPGRT_ATTR_FORMAT_ARG(a) __attribute__ ((__format_arg__ (a))) #else # define GPGRT_ATTR_FORMAT_ARG(a) #endif /* The sentinel attribute. */ #if _GPG_ERR_GCC_VERSION >= 40000 # define GPGRT_ATTR_SENTINEL(a) __attribute__ ((sentinel(a))) #else # define GPGRT_ATTR_SENTINEL(a) #endif /* The used and unused attributes. * I am not sure since when the unused attribute is really supported. * In any case it it only needed for gcc versions which print a * warning. Thus let us require gcc >= 3.5. */ #if _GPG_ERR_GCC_VERSION >= 40000 # define GPGRT_ATTR_USED __attribute__ ((used)) #else # define GPGRT_ATTR_USED #endif #if _GPG_ERR_GCC_VERSION >= 30500 # define GPGRT_ATTR_UNUSED __attribute__ ((unused)) #else # define GPGRT_ATTR_UNUSED #endif /* The deprecated attribute. */ #if _GPG_ERR_GCC_VERSION >= 30100 # define GPGRT_ATTR_DEPRECATED __attribute__ ((__deprecated__)) #else # define GPGRT_ATTR_DEPRECATED #endif /* The pure attribute. */ #if _GPG_ERR_GCC_VERSION >= 29600 # define GPGRT_ATTR_PURE __attribute__ ((__pure__)) #else # define GPGRT_ATTR_PURE #endif /* The malloc attribute. */ #if _GPG_ERR_GCC_VERSION >= 30200 # define GPGRT_ATTR_MALLOC __attribute__ ((__malloc__)) #else # define GPGRT_ATTR_MALLOC #endif /* A macro defined if a GCC style __FUNCTION__ macro is available. */ #undef GPGRT_HAVE_MACRO_FUNCTION #if _GPG_ERR_GCC_VERSION >= 20500 # define GPGRT_HAVE_MACRO_FUNCTION 1 #endif /* A macro defined if the pragma GCC push_options is available. */ #undef GPGRT_HAVE_PRAGMA_GCC_PUSH #if _GPG_ERR_GCC_VERSION >= 40400 # define GPGRT_HAVE_PRAGMA_GCC_PUSH 1 #endif /* Detect LeakSanitizer (LSan) support for GCC and Clang based on * whether AddressSanitizer (ASAN) is enabled via -fsanitize=address). * Note that -fsanitize=leak just affect the linker options which * cannot be detected here. In that case you have to define the * GPGRT_HAVE_LEAK_SANITIZER macro manually. */ #ifdef __GNUC__ # ifdef __SANITIZE_ADDRESS__ # define GPGRT_HAVE_LEAK_SANITIZER # elif defined(__has_feature) # if __has_feature(address_sanitizer) # define GPGRT_HAVE_LEAK_SANITIZER # endif # endif #endif /* The new name for the inline macro. */ #define GPGRT_INLINE GPG_ERR_INLINE #ifdef GPGRT_HAVE_LEAK_SANITIZER # include #endif /* Mark heap objects as non-leaked memory. */ static GPGRT_INLINE void gpgrt_annotate_leaked_object (const void *p) { #ifdef GPGRT_HAVE_LEAK_SANITIZER __lsan_ignore_object(p); #else (void)p; #endif } /* * Initialization function. */ /* Initialize the library. This function should be run early. */ gpg_error_t gpg_err_init (void) _GPG_ERR_CONSTRUCTOR; /* If this is defined, the library is already initialized by the constructor and does not need to be initialized explicitely. */ #undef GPG_ERR_INITIALIZED #ifdef _GPG_ERR_HAVE_CONSTRUCTOR # define GPG_ERR_INITIALIZED 1 # define gpgrt_init() do { gpg_err_init (); } while (0) #else # define gpgrt_init() do { ; } while (0) #endif /* See the source on how to use the deinit function; it is usually not required. */ void gpg_err_deinit (int mode); /* Register blocking system I/O clamping functions. */ void gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void)); /* Get current I/O clamping functions. */ void gpgrt_get_syscall_clamp (void (**r_pre)(void), void (**r_post)(void)); /* Register a custom malloc/realloc/free function. */ void gpgrt_set_alloc_func (void *(*f)(void *a, size_t n)); /* * Constructor and accessor functions. */ /* Construct an error value from an error code and source. Within a * subsystem, use gpg_error. */ static GPG_ERR_INLINE gpg_error_t gpg_err_make (gpg_err_source_t source, gpg_err_code_t code) { return code == GPG_ERR_NO_ERROR ? GPG_ERR_NO_ERROR : (((source & GPG_ERR_SOURCE_MASK) << GPG_ERR_SOURCE_SHIFT) | (code & GPG_ERR_CODE_MASK)); } /* The user should define GPG_ERR_SOURCE_DEFAULT before including this * file to specify a default source for gpg_error. */ #ifndef GPG_ERR_SOURCE_DEFAULT #define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_UNKNOWN #endif static GPG_ERR_INLINE gpg_error_t gpg_error (gpg_err_code_t code) { return gpg_err_make (GPG_ERR_SOURCE_DEFAULT, code); } /* Retrieve the error code from an error value. */ static GPG_ERR_INLINE gpg_err_code_t gpg_err_code (gpg_error_t err) { return (gpg_err_code_t) (err & GPG_ERR_CODE_MASK); } /* Retrieve the error source from an error value. */ static GPG_ERR_INLINE gpg_err_source_t gpg_err_source (gpg_error_t err) { return (gpg_err_source_t) ((err >> GPG_ERR_SOURCE_SHIFT) & GPG_ERR_SOURCE_MASK); } /* String functions. */ /* Return a pointer to a string containing a description of the error * code in the error value ERR. This function is not thread-safe. */ const char *gpg_strerror (gpg_error_t err); /* Return the error string for ERR in the user-supplied buffer BUF of * size BUFLEN. This function is, in contrast to gpg_strerror, * thread-safe if a thread-safe strerror_r() function is provided by * the system. If the function succeeds, 0 is returned and BUF * contains the string describing the error. If the buffer was not * large enough, ERANGE is returned and BUF contains as much of the * beginning of the error string as fits into the buffer. */ int gpg_strerror_r (gpg_error_t err, char *buf, size_t buflen); /* Return a pointer to a string containing a description of the error * source in the error value ERR. */ const char *gpg_strsource (gpg_error_t err); /* * Mapping of system errors (errno). */ /* Retrieve the error code for the system error ERR. This returns * GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report * this). */ gpg_err_code_t gpg_err_code_from_errno (int err); /* Retrieve the system error for the error code CODE. This returns 0 * if CODE is not a system error code. */ int gpg_err_code_to_errno (gpg_err_code_t code); /* Retrieve the error code directly from the ERRNO variable. This * returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped * (report this) and GPG_ERR_MISSING_ERRNO if ERRNO has the value 0. */ gpg_err_code_t gpg_err_code_from_syserror (void); /* Set the ERRNO variable. This function is the preferred way to set * ERRNO due to peculiarities on WindowsCE. */ void gpg_err_set_errno (int err); /* Return or check the version. Both functions are identical. */ const char *gpgrt_check_version (const char *req_version); const char *gpg_error_check_version (const char *req_version); /* System specific type definitions. */ @define:pid_t@ @define:gpgrt_ssize_t@ @define:gpgrt_off_t@ @include:os-add@ /* Self-documenting convenience functions. */ static GPG_ERR_INLINE gpg_error_t gpg_err_make_from_errno (gpg_err_source_t source, int err) { return gpg_err_make (source, gpg_err_code_from_errno (err)); } static GPG_ERR_INLINE gpg_error_t gpg_error_from_errno (int err) { return gpg_error (gpg_err_code_from_errno (err)); } static GPG_ERR_INLINE gpg_error_t gpg_error_from_syserror (void) { return gpg_error (gpg_err_code_from_syserror ()); } /* * Malloc and friends */ void *gpgrt_realloc (void *a, size_t n); void *gpgrt_malloc (size_t n); void *gpgrt_calloc (size_t n, size_t m); char *gpgrt_strdup (const char *string); char *gpgrt_strconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0); void gpgrt_free (void *a); /* * System specific function wrappers. */ /* A getenv replacement which mallocs the returned string. */ char *gpgrt_getenv (const char *name); /* A setenv and a unsetenv replacement.*/ gpg_err_code_t gpgrt_setenv (const char *name, const char *value, int overwrite); #define gpgrt_unsetenv(n) gpgrt_setenv ((n), NULL, 1) /* A wrapper around mkdir using a string for the mode. */ gpg_err_code_t gpgrt_mkdir (const char *name, const char *modestr); /* A simple wrapper around chdir. */ gpg_err_code_t gpgrt_chdir (const char *name); /* Return the current WD as a malloced string. */ char *gpgrt_getcwd (void); /* * Lock functions. */ @include:lock-obj@ #define GPGRT_LOCK_DEFINE(name) \ static gpgrt_lock_t name = GPGRT_LOCK_INITIALIZER /* NB: If GPGRT_LOCK_DEFINE is not used, zero out the lock variable before passing it to gpgrt_lock_init. */ gpg_err_code_t gpgrt_lock_init (gpgrt_lock_t *lockhd); gpg_err_code_t gpgrt_lock_lock (gpgrt_lock_t *lockhd); gpg_err_code_t gpgrt_lock_trylock (gpgrt_lock_t *lockhd); gpg_err_code_t gpgrt_lock_unlock (gpgrt_lock_t *lockhd); gpg_err_code_t gpgrt_lock_destroy (gpgrt_lock_t *lockhd); /* * Thread functions. */ gpg_err_code_t gpgrt_yield (void); /* * Estream */ /* The definition of this struct is entirely private. You must not use it for anything. It is only here so some functions can be implemented as macros. */ struct _gpgrt_stream_internal; struct _gpgrt__stream { /* The layout of this struct must never change. It may be grown, but only if all functions which access the new members are versioned. */ /* Various flags. */ struct { unsigned int magic: 16; unsigned int writing: 1; unsigned int reserved: 15; } flags; /* A pointer to the stream buffer. */ unsigned char *buffer; /* The size of the buffer in bytes. */ size_t buffer_size; /* The length of the usable data in the buffer, only valid when in read mode (see flags). */ size_t data_len; /* The current position of the offset pointer, valid in read and write mode. */ size_t data_offset; size_t data_flushed; unsigned char *unread_buffer; size_t unread_buffer_size; /* The number of unread bytes. */ size_t unread_data_len; /* A pointer to our internal data for this stream. */ struct _gpgrt_stream_internal *intern; }; /* The opaque type for an estream. */ typedef struct _gpgrt__stream *gpgrt_stream_t; #ifdef GPGRT_ENABLE_ES_MACROS typedef struct _gpgrt__stream *estream_t; #endif typedef @api_ssize_t@ (*gpgrt_cookie_read_function_t) (void *cookie, void *buffer, size_t size); typedef @api_ssize_t@ (*gpgrt_cookie_write_function_t) (void *cookie, const void *buffer, size_t size); typedef int (*gpgrt_cookie_seek_function_t) (void *cookie, gpgrt_off_t *pos, int whence); typedef int (*gpgrt_cookie_close_function_t) (void *cookie); struct _gpgrt_cookie_io_functions { gpgrt_cookie_read_function_t func_read; gpgrt_cookie_write_function_t func_write; gpgrt_cookie_seek_function_t func_seek; gpgrt_cookie_close_function_t func_close; }; typedef struct _gpgrt_cookie_io_functions gpgrt_cookie_io_functions_t; #ifdef GPGRT_ENABLE_ES_MACROS typedef struct _gpgrt_cookie_io_functions es_cookie_io_functions_t; #define es_cookie_read_function_t gpgrt_cookie_read_function_t #define es_cookie_write_function_t gpgrt_cookie_read_function_t #define es_cookie_seek_function_t gpgrt_cookie_read_function_t #define es_cookie_close_function_t gpgrt_cookie_read_function_t #endif enum gpgrt_syshd_types { GPGRT_SYSHD_NONE = 0, /* No system handle available. */ GPGRT_SYSHD_FD = 1, /* A file descriptor as returned by open(). */ GPGRT_SYSHD_SOCK = 2, /* A socket as returned by socket(). */ GPGRT_SYSHD_RVID = 3, /* A rendezvous id (see libassuan's gpgcedev.c). */ GPGRT_SYSHD_HANDLE = 4 /* A HANDLE object (Windows). */ }; struct _gpgrt_syshd { enum gpgrt_syshd_types type; union { int fd; int sock; int rvid; void *handle; } u; }; typedef struct _gpgrt_syshd gpgrt_syshd_t; #ifdef GPGRT_ENABLE_ES_MACROS typedef struct _gpgrt_syshd es_syshd_t; #define ES_SYSHD_NONE GPGRT_SYSHD_NONE #define ES_SYSHD_FD GPGRT_SYSHD_FD #define ES_SYSHD_SOCK GPGRT_SYSHD_SOCK #define ES_SYSHD_RVID GPGRT_SYSHD_RVID #define ES_SYSHD_HANDLE GPGRT_SYSHD_HANDLE #endif /* The object used with gpgrt_poll. */ struct _gpgrt_poll_s { gpgrt_stream_t stream; unsigned int want_read:1; unsigned int want_write:1; unsigned int want_oob:1; unsigned int want_rdhup:1; unsigned int _reserv1:4; unsigned int got_read:1; unsigned int got_write:1; unsigned int got_oob:1; unsigned int got_rdhup:1; unsigned int _reserv2:4; unsigned int got_err:1; unsigned int got_hup:1; unsigned int got_nval:1; unsigned int _reserv3:4; unsigned int ignore:1; unsigned int user:8; /* For application use. */ }; typedef struct _gpgrt_poll_s gpgrt_poll_t; #ifdef GPGRT_ENABLE_ES_MACROS typedef struct _gpgrt_poll_s es_poll_t; #endif gpgrt_stream_t gpgrt_fopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode); gpgrt_stream_t gpgrt_mopen (void *_GPGRT__RESTRICT data, size_t data_n, size_t data_len, unsigned int grow, void *(*func_realloc) (void *mem, size_t size), void (*func_free) (void *mem), const char *_GPGRT__RESTRICT mode); gpgrt_stream_t gpgrt_fopenmem (size_t memlimit, const char *_GPGRT__RESTRICT mode); gpgrt_stream_t gpgrt_fopenmem_init (size_t memlimit, const char *_GPGRT__RESTRICT mode, const void *data, size_t datalen); gpgrt_stream_t gpgrt_fdopen (int filedes, const char *mode); gpgrt_stream_t gpgrt_fdopen_nc (int filedes, const char *mode); gpgrt_stream_t gpgrt_sysopen (gpgrt_syshd_t *syshd, const char *mode); gpgrt_stream_t gpgrt_sysopen_nc (gpgrt_syshd_t *syshd, const char *mode); gpgrt_stream_t gpgrt_fpopen (FILE *fp, const char *mode); gpgrt_stream_t gpgrt_fpopen_nc (FILE *fp, const char *mode); gpgrt_stream_t gpgrt_freopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode, gpgrt_stream_t _GPGRT__RESTRICT stream); gpgrt_stream_t gpgrt_fopencookie (void *_GPGRT__RESTRICT cookie, const char *_GPGRT__RESTRICT mode, gpgrt_cookie_io_functions_t functions); int gpgrt_fclose (gpgrt_stream_t stream); int gpgrt_fclose_snatch (gpgrt_stream_t stream, void **r_buffer, size_t *r_buflen); int gpgrt_onclose (gpgrt_stream_t stream, int mode, void (*fnc) (gpgrt_stream_t, void*), void *fnc_value); int gpgrt_fileno (gpgrt_stream_t stream); int gpgrt_fileno_unlocked (gpgrt_stream_t stream); int gpgrt_syshd (gpgrt_stream_t stream, gpgrt_syshd_t *syshd); int gpgrt_syshd_unlocked (gpgrt_stream_t stream, gpgrt_syshd_t *syshd); void _gpgrt_set_std_fd (int no, int fd); gpgrt_stream_t _gpgrt_get_std_stream (int fd); #define gpgrt_stdin _gpgrt_get_std_stream (0) #define gpgrt_stdout _gpgrt_get_std_stream (1) #define gpgrt_stderr _gpgrt_get_std_stream (2) void gpgrt_flockfile (gpgrt_stream_t stream); int gpgrt_ftrylockfile (gpgrt_stream_t stream); void gpgrt_funlockfile (gpgrt_stream_t stream); int gpgrt_feof (gpgrt_stream_t stream); int gpgrt_feof_unlocked (gpgrt_stream_t stream); int gpgrt_ferror (gpgrt_stream_t stream); int gpgrt_ferror_unlocked (gpgrt_stream_t stream); void gpgrt_clearerr (gpgrt_stream_t stream); void gpgrt_clearerr_unlocked (gpgrt_stream_t stream); int _gpgrt_pending (gpgrt_stream_t stream); /* (private) */ int _gpgrt_pending_unlocked (gpgrt_stream_t stream); /* (private) */ #define gpgrt_pending(stream) _gpgrt_pending (stream) #define gpgrt_pending_unlocked(stream) \ (((!(stream)->flags.writing) \ && (((stream)->data_offset < (stream)->data_len) \ || ((stream)->unread_data_len))) \ ? 1 : _gpgrt_pending_unlocked ((stream))) int gpgrt_fflush (gpgrt_stream_t stream); int gpgrt_fseek (gpgrt_stream_t stream, long int offset, int whence); int gpgrt_fseeko (gpgrt_stream_t stream, gpgrt_off_t offset, int whence); +int gpgrt_ftruncate (estream_t stream, gpgrt_off_t length); long int gpgrt_ftell (gpgrt_stream_t stream); gpgrt_off_t gpgrt_ftello (gpgrt_stream_t stream); void gpgrt_rewind (gpgrt_stream_t stream); int gpgrt_fgetc (gpgrt_stream_t stream); int gpgrt_fputc (int c, gpgrt_stream_t stream); int _gpgrt_getc_underflow (gpgrt_stream_t stream); /* (private) */ int _gpgrt_putc_overflow (int c, gpgrt_stream_t stream); /* (private) */ #define gpgrt_getc_unlocked(stream) \ (((!(stream)->flags.writing) \ && ((stream)->data_offset < (stream)->data_len) \ && (! (stream)->unread_data_len)) \ ? ((int) (stream)->buffer[((stream)->data_offset)++]) \ : _gpgrt_getc_underflow ((stream))) #define gpgrt_putc_unlocked(c, stream) \ (((stream)->flags.writing \ && ((stream)->data_offset < (stream)->buffer_size) \ && (c != '\n')) \ ? ((int) ((stream)->buffer[((stream)->data_offset)++] = (c))) \ : _gpgrt_putc_overflow ((c), (stream))) #define gpgrt_getc(stream) gpgrt_fgetc (stream) #define gpgrt_putc(c, stream) gpgrt_fputc (c, stream) int gpgrt_ungetc (int c, gpgrt_stream_t stream); int gpgrt_read (gpgrt_stream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT buffer, size_t bytes_to_read, size_t *_GPGRT__RESTRICT bytes_read); int gpgrt_write (gpgrt_stream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t bytes_to_write, size_t *_GPGRT__RESTRICT bytes_written); int gpgrt_write_sanitized (gpgrt_stream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t length, const char *delimiters, size_t *_GPGRT__RESTRICT bytes_written); int gpgrt_write_hexstring (gpgrt_stream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t length, int reserved, size_t *_GPGRT__RESTRICT bytes_written); size_t gpgrt_fread (void *_GPGRT__RESTRICT ptr, size_t size, size_t nitems, gpgrt_stream_t _GPGRT__RESTRICT stream); size_t gpgrt_fwrite (const void *_GPGRT__RESTRICT ptr, size_t size, size_t nitems, gpgrt_stream_t _GPGRT__RESTRICT stream); char *gpgrt_fgets (char *_GPGRT__RESTRICT s, int n, gpgrt_stream_t _GPGRT__RESTRICT stream); int gpgrt_fputs (const char *_GPGRT__RESTRICT s, gpgrt_stream_t _GPGRT__RESTRICT stream); int gpgrt_fputs_unlocked (const char *_GPGRT__RESTRICT s, gpgrt_stream_t _GPGRT__RESTRICT stream); @api_ssize_t@ gpgrt_getline (char *_GPGRT__RESTRICT *_GPGRT__RESTRICT lineptr, size_t *_GPGRT__RESTRICT n, gpgrt_stream_t stream); @api_ssize_t@ gpgrt_read_line (gpgrt_stream_t stream, char **addr_of_buffer, size_t *length_of_buffer, size_t *max_length); int gpgrt_fprintf (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(2,3); int gpgrt_fprintf_unlocked (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(2,3); int gpgrt_printf (const char *_GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(1,2); int gpgrt_printf_unlocked (const char *_GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(1,2); int gpgrt_vfprintf (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) GPGRT_ATTR_PRINTF(2,0); int gpgrt_vfprintf_unlocked (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) GPGRT_ATTR_PRINTF(2,0); int gpgrt_setvbuf (gpgrt_stream_t _GPGRT__RESTRICT stream, char *_GPGRT__RESTRICT buf, int mode, size_t size); void gpgrt_setbuf (gpgrt_stream_t _GPGRT__RESTRICT stream, char *_GPGRT__RESTRICT buf); void gpgrt_set_binary (gpgrt_stream_t stream); int gpgrt_set_nonblock (gpgrt_stream_t stream, int onoff); int gpgrt_get_nonblock (gpgrt_stream_t stream); int gpgrt_poll (gpgrt_poll_t *fdlist, unsigned int nfds, int timeout); gpgrt_stream_t gpgrt_tmpfile (void); void gpgrt_opaque_set (gpgrt_stream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT opaque); void *gpgrt_opaque_get (gpgrt_stream_t stream); void gpgrt_fname_set (gpgrt_stream_t stream, const char *fname); const char *gpgrt_fname_get (gpgrt_stream_t stream); int gpgrt_asprintf (char **r_buf, const char * _GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(2,3); int gpgrt_vasprintf (char **r_buf, const char * _GPGRT__RESTRICT format, va_list ap) GPGRT_ATTR_PRINTF(2,0); char *gpgrt_bsprintf (const char * _GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(1,2); char *gpgrt_vbsprintf (const char * _GPGRT__RESTRICT format, va_list ap) GPGRT_ATTR_PRINTF(1,0); int gpgrt_snprintf (char *buf, size_t bufsize, const char * _GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(3,4); int gpgrt_vsnprintf (char *buf,size_t bufsize, const char * _GPGRT__RESTRICT format, va_list arg_ptr) GPGRT_ATTR_PRINTF(3,0); #ifdef GPGRT_ENABLE_ES_MACROS # define es_fopen gpgrt_fopen # define es_mopen gpgrt_mopen # define es_fopenmem gpgrt_fopenmem # define es_fopenmem_init gpgrt_fopenmem_init # define es_fdopen gpgrt_fdopen # define es_fdopen_nc gpgrt_fdopen_nc # define es_sysopen gpgrt_sysopen # define es_sysopen_nc gpgrt_sysopen_nc # define es_fpopen gpgrt_fpopen # define es_fpopen_nc gpgrt_fpopen_nc # define es_freopen gpgrt_freopen # define es_fopencookie gpgrt_fopencookie # define es_fclose gpgrt_fclose # define es_fclose_snatch gpgrt_fclose_snatch # define es_onclose gpgrt_onclose # define es_fileno gpgrt_fileno # define es_fileno_unlocked gpgrt_fileno_unlocked # define es_syshd gpgrt_syshd # define es_syshd_unlocked gpgrt_syshd_unlocked # define es_stdin _gpgrt_get_std_stream (0) # define es_stdout _gpgrt_get_std_stream (1) # define es_stderr _gpgrt_get_std_stream (2) # define es_flockfile gpgrt_flockfile # define es_ftrylockfile gpgrt_ftrylockfile # define es_funlockfile gpgrt_funlockfile # define es_feof gpgrt_feof # define es_feof_unlocked gpgrt_feof_unlocked # define es_ferror gpgrt_ferror # define es_ferror_unlocked gpgrt_ferror_unlocked # define es_clearerr gpgrt_clearerr # define es_clearerr_unlocked gpgrt_clearerr_unlocked # define es_pending gpgrt_pending # define es_pending_unlocked gpgrt_pending_unlocked # define es_fflush gpgrt_fflush # define es_fseek gpgrt_fseek # define es_fseeko gpgrt_fseeko +# define es_ftruncate gpgrt_ftruncate # define es_ftell gpgrt_ftell # define es_ftello gpgrt_ftello # define es_rewind gpgrt_rewind # define es_fgetc gpgrt_fgetc # define es_fputc gpgrt_fputc # define es_getc_unlocked gpgrt_getc_unlocked # define es_putc_unlocked gpgrt_putc_unlocked # define es_getc gpgrt_getc # define es_putc gpgrt_putc # define es_ungetc gpgrt_ungetc # define es_read gpgrt_read # define es_write gpgrt_write # define es_write_sanitized gpgrt_write_sanitized # define es_write_hexstring gpgrt_write_hexstring # define es_fread gpgrt_fread # define es_fwrite gpgrt_fwrite # define es_fgets gpgrt_fgets # define es_fputs gpgrt_fputs # define es_fputs_unlocked gpgrt_fputs_unlocked # define es_getline gpgrt_getline # define es_read_line gpgrt_read_line # define es_free gpgrt_free # define es_fprintf gpgrt_fprintf # define es_fprintf_unlocked gpgrt_fprintf_unlocked # define es_printf gpgrt_printf # define es_printf_unlocked gpgrt_printf_unlocked # define es_vfprintf gpgrt_vfprintf # define es_vfprintf_unlocked gpgrt_vfprintf_unlocked # define es_setvbuf gpgrt_setvbuf # define es_setbuf gpgrt_setbuf # define es_set_binary gpgrt_set_binary # define es_set_nonblock gpgrt_set_nonblock # define es_get_nonblock gpgrt_get_nonblock # define es_poll gpgrt_poll # define es_tmpfile gpgrt_tmpfile # define es_opaque_set gpgrt_opaque_set # define es_opaque_get gpgrt_opaque_get # define es_fname_set gpgrt_fname_set # define es_fname_get gpgrt_fname_get # define es_asprintf gpgrt_asprintf # define es_vasprintf gpgrt_vasprintf # define es_bsprintf gpgrt_bsprintf # define es_vbsprintf gpgrt_vbsprintf #endif /*GPGRT_ENABLE_ES_MACROS*/ /* * Base64 encode and decode functions. */ struct _gpgrt_b64state; typedef struct _gpgrt_b64state *gpgrt_b64state_t; gpgrt_b64state_t gpgrt_b64enc_start (gpgrt_stream_t stream, const char *title); gpg_err_code_t gpgrt_b64enc_write (gpgrt_b64state_t state, const void *buffer, size_t nbytes); gpg_err_code_t gpgrt_b64enc_finish (gpgrt_b64state_t state); gpgrt_b64state_t gpgrt_b64dec_start (const char *title); gpg_error_t gpgrt_b64dec_proc (gpgrt_b64state_t state, void *buffer, size_t length, size_t *r_nbytes); gpg_error_t gpgrt_b64dec_finish (gpgrt_b64state_t state); /* * Logging functions */ /* Flag values for gpgrt_log_set_prefix. */ #define GPGRT_LOG_WITH_PREFIX 1 #define GPGRT_LOG_WITH_TIME 2 #define GPGRT_LOG_WITH_PID 4 #define GPGRT_LOG_RUN_DETACHED 256 #define GPGRT_LOG_NO_REGISTRY 512 /* Log levels as used by gpgrt_log. */ enum gpgrt_log_levels { GPGRT_LOGLVL_BEGIN, GPGRT_LOGLVL_CONT, GPGRT_LOGLVL_INFO, GPGRT_LOGLVL_WARN, GPGRT_LOGLVL_ERROR, GPGRT_LOGLVL_FATAL, GPGRT_LOGLVL_BUG, GPGRT_LOGLVL_DEBUG }; /* The next 4 functions are not thread-safe - call them early. */ void gpgrt_log_set_sink (const char *name, gpgrt_stream_t stream, int fd); void gpgrt_log_set_socket_dir_cb (const char *(*fnc)(void)); void gpgrt_log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)); void gpgrt_log_set_prefix (const char *text, unsigned int flags); int gpgrt_get_errorcount (int clear); void gpgrt_inc_errorcount (void); const char *gpgrt_log_get_prefix (unsigned int *flags); int gpgrt_log_test_fd (int fd); int gpgrt_log_get_fd (void); gpgrt_stream_t gpgrt_log_get_stream (void); void gpgrt_log (int level, const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3); void gpgrt_logv (int level, const char *fmt, va_list arg_ptr); void gpgrt_logv_prefix (int level, const char *prefix, const char *fmt, va_list arg_ptr); void gpgrt_log_string (int level, const char *string); void gpgrt_log_bug (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); void gpgrt_log_fatal (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); void gpgrt_log_error (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void gpgrt_log_info (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void gpgrt_log_debug (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void gpgrt_log_debug_string (const char *string, const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3); void gpgrt_log_printf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void gpgrt_log_printhex (const void *buffer, size_t length, const char *fmt, ...) GPGRT_ATTR_PRINTF(3,4); void gpgrt_log_clock (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void gpgrt_log_flush (void); void _gpgrt_log_assert (const char *expr, const char *file, int line, const char *func) GPGRT_ATTR_NORETURN; #ifdef GPGRT_HAVE_MACRO_FUNCTION # define gpgrt_assert(expr) \ ((expr) \ ? (void) 0 \ : _gpgrt_log_assert (#expr, __FILE__, __LINE__, __FUNCTION__)) #else /*!GPGRT_HAVE_MACRO_FUNCTION*/ # define gpgrt_assert(expr) \ ((expr) \ ? (void) 0 \ : _gpgrt_log_assert (#expr, __FILE__, __LINE__, NULL)) #endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ #ifdef GPGRT_ENABLE_LOG_MACROS # define log_get_errorcount gpgrt_get_errorcount # define log_inc_errorcount gpgrt_inc_errorcount # define log_set_file(a) gpgrt_log_set_sink ((a), NULL, -1) # define log_set_fd(a) gpgrt_log_set_sink (NULL, NULL, (a)) # define log_set_stream(a) gpgrt_log_set_sink (NULL, (a), -1) # define log_set_socket_dir_cb gpgrt_log_set_socket_dir_cb # define log_set_pid_suffix_cb gpgrt_log_set_pid_suffix_cb # define log_set_prefix gpgrt_log_set_prefix # define log_get_prefix gpgrt_log_get_prefix # define log_test_fd gpgrt_log_test_fd # define log_get_fd gpgrt_log_get_fd # define log_get_stream gpgrt_log_get_stream # define log_log gpgrt_log # define log_logv gpgrt_logv # define log_logv_prefix gpgrt_logv_prefix # define log_string gpgrt_log_string # define log_bug gpgrt_log_bug # define log_fatal gpgrt_log_fatal # define log_error gpgrt_log_error # define log_info gpgrt_log_info # define log_debug gpgrt_log_debug # define log_debug_string gpgrt_log_debug_string # define log_printf gpgrt_log_printf # define log_printhex gpgrt_log_printhex # define log_clock gpgrt_log_clock # define log_flush gpgrt_log_flush # ifdef GPGRT_HAVE_MACRO_FUNCTION # define log_assert(expr) \ ((expr) \ ? (void) 0 \ : _gpgrt_log_assert (#expr, __FILE__, __LINE__, __FUNCTION__)) # else /*!GPGRT_HAVE_MACRO_FUNCTION*/ # define log_assert(expr) \ ((expr) \ ? (void) 0 \ : _gpgrt_log_assert (#expr, __FILE__, __LINE__, NULL)) # endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ #endif /*GPGRT_ENABLE_LOG_MACROS*/ /* * Spawn functions (Not yet available) */ #define GPGRT_SPAWN_NONBLOCK 16 /* Set the streams to non-blocking. */ #define GPGRT_SPAWN_RUN_ASFW 64 /* Use AllowSetForegroundWindow on W32. */ #define GPGRT_SPAWN_DETACHED 128 /* Start the process in the background. */ #if 0 /* Function and convenience macros to create pipes. */ gpg_err_code_t gpgrt_make_pipe (int filedes[2], gpgrt_stream_t *r_fp, int direction, int nonblock); #define gpgrt_create_pipe(a) gpgrt_make_pipe ((a),NULL, 0, 0); #define gpgrt_create_inbound_pipe(a,b,c) gpgrt_make_pipe ((a), (b), -1,(c)); #define gpgrt_create_outbound_pipe(a,b,c) gpgrt_make_pipe ((a), (b), 1,(c)); /* Fork and exec PGMNAME. */ gpg_err_code_t gpgrt_spawn_process (const char *pgmname, const char *argv[], int *execpt, void (*preexec)(void), unsigned int flags, gpgrt_stream_t *r_infp, gpgrt_stream_t *r_outfp, gpgrt_stream_t *r_errfp, pid_t *pid); /* Fork and exec PGNNAME and connect the process to the given FDs. */ gpg_err_code_t gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], int infd, int outfd, int errfd, pid_t *pid); /* Fork and exec PGMNAME as a detached process. */ gpg_err_code_t gpgrt_spawn_process_detached (const char *pgmname, const char *argv[], const char *envp[] ); /* Wait for a single process. */ gpg_err_code_t gpgrt_wait_process (const char *pgmname, pid_t pid, int hang, int *r_exitcode); /* Wait for a multiple processes. */ gpg_err_code_t gpgrt_wait_processes (const char **pgmnames, pid_t *pids, size_t count, int hang, int *r_exitcodes); /* Kill the process identified by PID. */ void gpgrt_kill_process (pid_t pid); /* Release process resources identified by PID. */ void gpgrt_release_process (pid_t pid); #endif /*0*/ /* * Option parsing. */ struct _gpgrt_argparse_internal_s; typedef struct { int *argc; /* Pointer to ARGC (value subject to change). */ char ***argv; /* Pointer to ARGV (value subject to change). */ unsigned int flags; /* Global flags. May be set prior to calling the parser. The parser may change the value. */ int err; /* Print error description for last option. Either 0, ARGPARSE_PRINT_WARNING or ARGPARSE_PRINT_ERROR. */ unsigned int lineno;/* The current line number. */ int r_opt; /* Returns option code. */ int r_type; /* Returns type of option value. */ union { int ret_int; long ret_long; unsigned long ret_ulong; char *ret_str; } r; /* Return values */ struct _gpgrt_argparse_internal_s *internal; } gpgrt_argparse_t; typedef struct { int short_opt; const char *long_opt; unsigned int flags; const char *description; /* Optional description. */ } gpgrt_opt_t; #ifdef GPGRT_ENABLE_ARGPARSE_MACROS /* Global flags for (gpgrt_argparse_t).flags. */ #define ARGPARSE_FLAG_KEEP 1 /* Do not remove options form argv. */ #define ARGPARSE_FLAG_ALL 2 /* Do not stop at last option but return remaining args with R_OPT set to -1. */ #define ARGPARSE_FLAG_MIXED 4 /* Assume options and args are mixed. */ #define ARGPARSE_FLAG_NOSTOP 8 /* Do not stop processing at "--". */ #define ARGPARSE_FLAG_ARG0 16 /* Do not skip the first arg. */ #define ARGPARSE_FLAG_ONEDASH 32 /* Allow long options with one dash. */ #define ARGPARSE_FLAG_NOVERSION 64 /* No output for "--version". */ #define ARGPARSE_FLAG_RESET 128 /* Request to reset the internal state. */ #define ARGPARSE_FLAG_STOP_SEEN 256 /* Set to true if a "--" has been seen. */ #define ARGPARSE_FLAG_NOLINENO 512 /* Do not zero the lineno field. */ /* Constants for (gpgrt_argparse_t).err. */ #define ARGPARSE_PRINT_WARNING 1 /* Print a diagnostic. */ #define ARGPARSE_PRINT_ERROR 2 /* Print a diagnostic and call exit. */ /* Special return values of gpgrt_argparse. */ #define ARGPARSE_IS_ARG (-1) #define ARGPARSE_INVALID_OPTION (-2) #define ARGPARSE_MISSING_ARG (-3) #define ARGPARSE_KEYWORD_TOO_LONG (-4) #define ARGPARSE_READ_ERROR (-5) #define ARGPARSE_UNEXPECTED_ARG (-6) #define ARGPARSE_INVALID_COMMAND (-7) #define ARGPARSE_AMBIGUOUS_OPTION (-8) #define ARGPARSE_AMBIGUOUS_COMMAND (-9) #define ARGPARSE_INVALID_ALIAS (-10) #define ARGPARSE_OUT_OF_CORE (-11) #define ARGPARSE_INVALID_ARG (-12) /* Flags for the option descriptor (gpgrt_opt_t)->flags. Note that * a TYPE constant may be or-ed with the OPT constants. */ #define ARGPARSE_TYPE_NONE 0 /* Does not take an argument. */ #define ARGPARSE_TYPE_INT 1 /* Takes an int argument. */ #define ARGPARSE_TYPE_STRING 2 /* Takes a string argument. */ #define ARGPARSE_TYPE_LONG 3 /* Takes a long argument. */ #define ARGPARSE_TYPE_ULONG 4 /* Takes an unsigned long argument. */ #define ARGPARSE_OPT_OPTIONAL (1<<3) /* Argument is optional. */ #define ARGPARSE_OPT_PREFIX (1<<4) /* Allow 0x etc. prefixed values. */ #define ARGPARSE_OPT_IGNORE (1<<6) /* Ignore command or option. */ #define ARGPARSE_OPT_COMMAND (1<<7) /* The argument is a command. */ /* A set of macros to make option definitions easier to read. */ #define ARGPARSE_x(s,l,t,f,d) \ { (s), (l), ARGPARSE_TYPE_ ## t | (f), (d) } #define ARGPARSE_s(s,l,t,d) \ { (s), (l), ARGPARSE_TYPE_ ## t, (d) } #define ARGPARSE_s_n(s,l,d) \ { (s), (l), ARGPARSE_TYPE_NONE, (d) } #define ARGPARSE_s_i(s,l,d) \ { (s), (l), ARGPARSE_TYPE_INT, (d) } #define ARGPARSE_s_s(s,l,d) \ { (s), (l), ARGPARSE_TYPE_STRING, (d) } #define ARGPARSE_s_l(s,l,d) \ { (s), (l), ARGPARSE_TYPE_LONG, (d) } #define ARGPARSE_s_u(s,l,d) \ { (s), (l), ARGPARSE_TYPE_ULONG, (d) } #define ARGPARSE_o(s,l,t,d) \ { (s), (l), (ARGPARSE_TYPE_ ## t | ARGPARSE_OPT_OPTIONAL), (d) } #define ARGPARSE_o_n(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_OPTIONAL), (d) } #define ARGPARSE_o_i(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_INT | ARGPARSE_OPT_OPTIONAL), (d) } #define ARGPARSE_o_s(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_STRING | ARGPARSE_OPT_OPTIONAL), (d) } #define ARGPARSE_o_l(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_LONG | ARGPARSE_OPT_OPTIONAL), (d) } #define ARGPARSE_o_u(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_ULONG | ARGPARSE_OPT_OPTIONAL), (d) } #define ARGPARSE_p(s,l,t,d) \ { (s), (l), (ARGPARSE_TYPE_ ## t | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_p_n(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_p_i(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_INT | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_p_s(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_STRING | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_p_l(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_LONG | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_p_u(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_ULONG | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_op(s,l,t,d) \ { (s), (l), (ARGPARSE_TYPE_ ## t \ | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_op_n(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_NONE \ | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_op_i(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_INT \ | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_op_s(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_STRING \ | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_op_l(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_LONG \ | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_op_u(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_ULONG \ | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) } #define ARGPARSE_c(s,l,d) \ { (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_COMMAND), (d) } #define ARGPARSE_ignore(s,l) \ { (s), (l), (ARGPARSE_OPT_IGNORE), "@" } #define ARGPARSE_group(s,d) \ { (s), NULL, 0, (d) } /* Mark the end of the list (mandatory). */ #define ARGPARSE_end() \ { 0, NULL, 0, NULL } #endif /* GPGRT_ENABLE_ARGPARSE_MACROS */ /* Take care: gpgrt_argparse keeps state in ARG and requires that * either ARGPARSE_FLAG_RESET is used after OPTS has been changed or * gpgrt_argparse (NULL, ARG, NULL) is called first. */ int gpgrt_argparse (gpgrt_stream_t fp, gpgrt_argparse_t *arg, gpgrt_opt_t *opts); void gpgrt_usage (int level); const char *gpgrt_strusage (int level); void gpgrt_set_strusage (const char *(*f)(int)); void gpgrt_set_usage_outfnc (int (*f)(int, const char *)); void gpgrt_set_fixed_string_mapper (const char *(*f)(const char*)); /* * Various helper functions */ /* Compare arbitrary version strings. For the standard m.n.o version * numbering scheme a LEVEL of 3 is suitable; see the manual. */ int gpgrt_cmp_version (const char *a, const char *b, int level); #ifdef __cplusplus } #endif #endif /* GPGRT_H */ #endif /* GPG_ERROR_H */ diff --git a/src/gpg-error.vers b/src/gpg-error.vers index 8c50a15..f9524d7 100644 --- a/src/gpg-error.vers +++ b/src/gpg-error.vers @@ -1,197 +1,198 @@ # libgpg-error.vers - What symbols to export -*- std -*- # Copyright (C) 2014 g10 Code GmbH # # This file is part of libgpg-error. # # libgpg-error is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # libgpg-error is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, see . # SPDX-License-Identifier: LGPL-2.1+ # # NOTE: When adding new functions, please make sure to add them to # visibility.h and gpg-error.def.in as well. GPG_ERROR_1.0 { global: gpg_strerror; gpg_strerror_r; gpg_strsource; gpg_err_code_from_errno; gpg_err_code_to_errno; gpg_err_code_from_syserror; gpg_err_set_errno; gpg_error_check_version; gpgrt_lock_init; gpgrt_lock_lock; gpgrt_lock_unlock; gpgrt_lock_destroy; gpgrt_yield; gpgrt_lock_trylock; gpgrt_set_syscall_clamp; gpgrt_get_syscall_clamp; gpgrt_fopen; gpgrt_mopen; gpgrt_fopenmem; gpgrt_fopenmem_init; gpgrt_fdopen; gpgrt_fdopen_nc; gpgrt_sysopen; gpgrt_sysopen_nc; gpgrt_fpopen; gpgrt_fpopen_nc; gpgrt_freopen; gpgrt_fopencookie; gpgrt_fclose; gpgrt_fclose_snatch; gpgrt_onclose; gpgrt_fileno; gpgrt_fileno_unlocked; gpgrt_syshd; gpgrt_syshd_unlocked; _gpgrt_set_std_fd; _gpgrt_get_std_stream; gpgrt_flockfile; gpgrt_ftrylockfile; gpgrt_funlockfile; _gpgrt_pending; _gpgrt_pending_unlocked; gpgrt_feof; gpgrt_feof_unlocked; gpgrt_ferror; gpgrt_ferror_unlocked; gpgrt_clearerr; gpgrt_clearerr_unlocked; gpgrt_fflush; gpgrt_fseek; gpgrt_fseeko; gpgrt_ftell; gpgrt_ftello; gpgrt_rewind; gpgrt_fgetc; _gpgrt_getc_underflow; gpgrt_fputc; _gpgrt_putc_overflow; gpgrt_ungetc; gpgrt_read; gpgrt_write; gpgrt_write_sanitized; gpgrt_write_hexstring; gpgrt_fread; gpgrt_fwrite; gpgrt_fgets; gpgrt_fputs; gpgrt_fputs_unlocked; gpgrt_getline; gpgrt_read_line; gpgrt_free; gpgrt_fprintf; gpgrt_fprintf_unlocked; gpgrt_printf; gpgrt_printf_unlocked; gpgrt_vfprintf; gpgrt_vfprintf_unlocked; gpgrt_setvbuf; gpgrt_setbuf; gpgrt_set_binary; gpgrt_set_nonblock; gpgrt_get_nonblock; gpgrt_poll; gpgrt_tmpfile; gpgrt_opaque_set; gpgrt_opaque_get; gpgrt_fname_set; gpgrt_fname_get; gpgrt_asprintf; gpgrt_vasprintf; gpgrt_bsprintf; gpgrt_vbsprintf; gpgrt_snprintf; gpgrt_vsnprintf; gpgrt_check_version; gpg_err_init; gpg_err_deinit; gpgrt_set_alloc_func; gpgrt_b64dec_start; gpgrt_b64dec_proc; gpgrt_b64dec_finish; gpgrt_get_errorcount; gpgrt_inc_errorcount; gpgrt_log_set_sink; gpgrt_log_set_socket_dir_cb; gpgrt_log_set_pid_suffix_cb; gpgrt_log_set_prefix; gpgrt_log_get_prefix; gpgrt_log_test_fd; gpgrt_log_get_fd; gpgrt_log_get_stream; gpgrt_log; gpgrt_logv; gpgrt_logv_prefix; gpgrt_log_string; gpgrt_log_bug; gpgrt_log_fatal; gpgrt_log_error; gpgrt_log_info; gpgrt_log_debug; gpgrt_log_debug_string; gpgrt_log_printf; gpgrt_log_printhex; gpgrt_log_clock; gpgrt_log_flush; _gpgrt_log_assert; gpgrt_realloc; gpgrt_malloc; gpgrt_calloc; gpgrt_strdup; gpgrt_strconcat; gpgrt_getenv; gpgrt_setenv; gpgrt_mkdir; gpgrt_chdir; gpgrt_getcwd; ## API not yet finished for: # gpgrt_make_pipe; # gpgrt_spawn_process; # gpgrt_spawn_process_fd; # gpgrt_spawn_process_detached; # gpgrt_wait_process; # gpgrt_wait_processes; # gpgrt_kill_process; # gpgrt_release_process; gpgrt_argparse; gpgrt_usage; gpgrt_strusage; gpgrt_set_strusage; gpgrt_set_usage_outfnc; gpgrt_set_fixed_string_mapper; gpgrt_b64enc_start; gpgrt_b64enc_write; gpgrt_b64enc_finish; gpgrt_cmp_version; + gpgrt_ftruncate; local: *; }; diff --git a/src/gpgrt-int.h b/src/gpgrt-int.h index b5a4dd1..09aad11 100644 --- a/src/gpgrt-int.h +++ b/src/gpgrt-int.h @@ -1,806 +1,807 @@ /* gpgrt-int.h - Internal definitions * Copyright (C) 2014, 2017 g10 Code GmbH * * This file is part of libgpg-error. * * libgpg-error is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libgpg-error is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ #ifndef _GPGRT_GPGRT_INT_H #define _GPGRT_GPGRT_INT_H #include "gpg-error.h" #include "visibility.h" /* * Internal i18n macros. */ #ifdef ENABLE_NLS # ifdef HAVE_W32_SYSTEM # include "gettext.h" # else # include # endif # define _(a) gettext (a) # ifdef gettext_noop # define N_(a) gettext_noop (a) # else # define N_(a) (a) # endif #else /*!ENABLE_NLS*/ # define _(a) (a) # define N_(a) (a) #endif /*!ENABLE_NLS */ /* * Hacks mainly required for Slowaris. */ #ifdef _GPGRT_NEED_AFLOCAL # ifndef HAVE_W32_SYSTEM # include # include # else # ifdef HAVE_WINSOCK2_H # include # endif # include # endif # ifndef PF_LOCAL # ifdef PF_UNIX # define PF_LOCAL PF_UNIX # else # define PF_LOCAL AF_UNIX # endif # endif /*PF_LOCAL*/ # ifndef AF_LOCAL # define AF_LOCAL AF_UNIX # endif /*AF_UNIX*/ /* We used to avoid this macro in GnuPG and inlined the AF_LOCAL name * length computation directly with the little twist of adding 1 extra * byte. It seems that this was needed once on an old HP/UX box and * there are also rumours that 4.3 Reno and DEC systems need it. This * one-off buglet did not harm any current system until it came to Mac * OS X where the kernel (as of May 2009) exhibited a strange bug: The * systems basically froze in the connect call if the passed name * contained an invalid directory part. Ignore the old Unices. */ # ifndef SUN_LEN # define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \ + strlen ((ptr)->sun_path)) # endif /*SUN_LEN*/ #endif /*_GPGRT_NEED_AFLOCAL*/ /* * Common helper macros. */ #ifndef DIM # define DIM(array) (sizeof (array) / sizeof (*array)) #endif /* * Local error function prototypes. */ const char *_gpg_strerror (gpg_error_t err); int _gpg_strerror_r (gpg_error_t err, char *buf, size_t buflen); const char *_gpg_strsource (gpg_error_t err); gpg_err_code_t _gpg_err_code_from_errno (int err); int _gpg_err_code_to_errno (gpg_err_code_t code); gpg_err_code_t _gpg_err_code_from_syserror (void); void _gpg_err_set_errno (int err); gpg_error_t _gpg_err_init (void); void _gpg_err_deinit (int mode); void _gpgrt_set_alloc_func (void *(*f)(void *a, size_t n)); void *_gpgrt_realloc (void *a, size_t n); void *_gpgrt_malloc (size_t n); void *_gpgrt_calloc (size_t n, size_t m); char *_gpgrt_strdup (const char *string); char *_gpgrt_strconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0); void _gpgrt_free (void *a); /* The next is only to be used by visibility.c. */ char *_gpgrt_strconcat_core (const char *s1, va_list arg_ptr); #define xfree(a) _gpgrt_free ((a)) #define xtrymalloc(a) _gpgrt_malloc ((a)) #define xtrycalloc(a,b) _gpgrt_calloc ((a),(b)) #define xtryrealloc(a,b) _gpgrt_realloc ((a),(b)) #define xtrystrdup(a) _gpgrt_strdup ((a)) void _gpgrt_pre_syscall (void); void _gpgrt_post_syscall (void); const char *_gpg_error_check_version (const char *req_version); gpg_err_code_t _gpgrt_lock_init (gpgrt_lock_t *lockhd); gpg_err_code_t _gpgrt_lock_lock (gpgrt_lock_t *lockhd); gpg_err_code_t _gpgrt_lock_trylock (gpgrt_lock_t *lockhd); gpg_err_code_t _gpgrt_lock_unlock (gpgrt_lock_t *lockhd); gpg_err_code_t _gpgrt_lock_destroy (gpgrt_lock_t *lockhd); gpg_err_code_t _gpgrt_yield (void); /* * Tracing */ /* The trace macro is used this way: * trace (("enter - foo=%d bar=%s", foo, bar)); * Note the double parenthesis, they are important. * To append the current errno to the output, use * trace_errno (EXTPR,("leave - baz=%d", faz)); * If EXPR evaluates to true the output of strerror (errno) * is appended to the output. Note that the trace function does * not modify ERRNO. To enable tracing you need to have this * #define ENABLE_TRACING "modulename" * before you include gpgrt-int.h. */ #ifdef ENABLE_TRACING # define trace(X) do { \ _gpgrt_internal_trace_begin \ (ENABLE_TRACING, __func__, __LINE__, 0); \ _gpgrt_internal_trace X; \ _gpgrt_internal_trace_end (); \ } while (0) # define trace_errno(C,X) do { \ _gpgrt_internal_trace_begin \ (ENABLE_TRACING, __func__, __LINE__, (C)); \ _gpgrt_internal_trace X; \ _gpgrt_internal_trace_end (); \ } while (0) # define trace_start(X) do { \ _gpgrt_internal_trace_begin \ (ENABLE_TRACING, __func__, __LINE__, 0); \ _gpgrt_internal_trace_printf X; \ } while (0) # define trace_append(X) do { \ _gpgrt_internal_trace_printf X; \ } while (0) # define trace_finish(X) do { \ _gpgrt_internal_trace_printf X; \ _gpgrt_internal_trace_end (); \ } while (0) #else # define trace(X) do { } while (0) # define trace_errno(C,X) do { } while (0) # define trace_start(X) do { } while (0) # define trace_append(X) do { } while (0) # define trace_finish(X) do { } while (0) #endif /*!ENABLE_TRACING*/ void _gpgrt_internal_trace_begin (const char *mod, const char *file, int line, int with_errno); void _gpgrt_internal_trace (const char *format, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_internal_trace_printf (const char *format, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_internal_trace_end (void); /* * Local definitions for estream. */ #if HAVE_W32_SYSTEM # ifndef O_NONBLOCK # define O_NONBLOCK 0x40000000 /* FIXME: Is that safe? */ # endif #endif /* * A private cookie function to implement an internal IOCTL service. - * and ist IOCTL numbers. */ typedef int (*cookie_ioctl_function_t) (void *cookie, int cmd, void *ptr, size_t *len); #define COOKIE_IOCTL_SNATCH_BUFFER 1 #define COOKIE_IOCTL_NONBLOCK 2 +#define COOKIE_IOCTL_TRUNCATE 3 /* An internal variant of gpgrt_cookie_close_function_t with a slot * for the ioctl function. */ struct cookie_io_functions_s { struct _gpgrt_cookie_io_functions public; cookie_ioctl_function_t func_ioctl; }; typedef enum { BACKEND_MEM, BACKEND_FD, BACKEND_W32, BACKEND_FP, BACKEND_USER, BACKEND_W32_POLLABLE } gpgrt_stream_backend_kind_t; /* * A type to hold notification functions. */ struct notify_list_s { struct notify_list_s *next; void (*fnc) (estream_t, void*); /* The notification function. */ void *fnc_value; /* The value to be passed to FNC. */ }; typedef struct notify_list_s *notify_list_t; /* * Buffer management layer. */ #define BUFFER_BLOCK_SIZE BUFSIZ #define BUFFER_UNREAD_SIZE 16 /* * The private object describing a stream. */ struct _gpgrt_stream_internal { unsigned char buffer[BUFFER_BLOCK_SIZE]; unsigned char unread_buffer[BUFFER_UNREAD_SIZE]; gpgrt_lock_t lock; /* Lock. Used by *_stream_lock(). */ gpgrt_stream_backend_kind_t kind; void *cookie; /* Cookie. */ void *opaque; /* Opaque data. */ unsigned int modeflags; /* Flags for the backend. */ char *printable_fname; /* Malloced filename for es_fname_get. */ gpgrt_off_t offset; gpgrt_cookie_read_function_t func_read; gpgrt_cookie_write_function_t func_write; gpgrt_cookie_seek_function_t func_seek; gpgrt_cookie_close_function_t func_close; cookie_ioctl_function_t func_ioctl; int strategy; es_syshd_t syshd; /* A copy of the system handle. */ struct { unsigned int err: 1; unsigned int eof: 1; unsigned int hup: 1; } indicators; unsigned int deallocate_buffer: 1; unsigned int is_stdstream:1; /* This is a standard stream. */ unsigned int stdstream_fd:2; /* 0, 1 or 2 for a standard stream. */ unsigned int printable_fname_inuse: 1; /* es_fname_get has been used. */ unsigned int samethread: 1; /* The "samethread" mode keyword. */ size_t print_ntotal; /* Bytes written from in print_writer. */ notify_list_t onclose; /* On close notify function list. */ }; typedef struct _gpgrt_stream_internal *estream_internal_t; /* * Local prototypes for estream. */ int _gpgrt_estream_init (void); void _gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void)); void _gpgrt_get_syscall_clamp (void (**r_pre)(void), void (**r_post)(void)); gpgrt_stream_t _gpgrt_fopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode); gpgrt_stream_t _gpgrt_mopen (void *_GPGRT__RESTRICT data, size_t data_n, size_t data_len, unsigned int grow, void *(*func_realloc) (void *mem, size_t size), void (*func_free) (void *mem), const char *_GPGRT__RESTRICT mode); gpgrt_stream_t _gpgrt_fopenmem (size_t memlimit, const char *_GPGRT__RESTRICT mode); gpgrt_stream_t _gpgrt_fopenmem_init (size_t memlimit, const char *_GPGRT__RESTRICT mode, const void *data, size_t datalen); gpgrt_stream_t _gpgrt_fdopen (int filedes, const char *mode); gpgrt_stream_t _gpgrt_fdopen_nc (int filedes, const char *mode); gpgrt_stream_t _gpgrt_sysopen (gpgrt_syshd_t *syshd, const char *mode); gpgrt_stream_t _gpgrt_sysopen_nc (gpgrt_syshd_t *syshd, const char *mode); gpgrt_stream_t _gpgrt_fpopen (FILE *fp, const char *mode); gpgrt_stream_t _gpgrt_fpopen_nc (FILE *fp, const char *mode); gpgrt_stream_t _gpgrt_freopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode, gpgrt_stream_t _GPGRT__RESTRICT stream); gpgrt_stream_t _gpgrt_fopencookie (void *_GPGRT__RESTRICT cookie, const char *_GPGRT__RESTRICT mode, gpgrt_cookie_io_functions_t functions); int _gpgrt_fclose (gpgrt_stream_t stream); int _gpgrt_fclose_snatch (gpgrt_stream_t stream, void **r_buffer, size_t *r_buflen); int _gpgrt_onclose (gpgrt_stream_t stream, int mode, void (*fnc) (gpgrt_stream_t, void*), void *fnc_value); int _gpgrt_fileno (gpgrt_stream_t stream); int _gpgrt_fileno_unlocked (gpgrt_stream_t stream); int _gpgrt_syshd (gpgrt_stream_t stream, gpgrt_syshd_t *syshd); int _gpgrt_syshd_unlocked (gpgrt_stream_t stream, gpgrt_syshd_t *syshd); void _gpgrt__set_std_fd (int no, int fd); gpgrt_stream_t _gpgrt__get_std_stream (int fd); /* The es_stderr et al macros are pretty common so that we want to use * them too. This requires that we redefine them. */ #undef es_stdin #define es_stdin _gpgrt__get_std_stream (0) #undef es_stdout #define es_stdout _gpgrt__get_std_stream (1) #undef es_stderr #define es_stderr _gpgrt__get_std_stream (2) void _gpgrt_flockfile (gpgrt_stream_t stream); int _gpgrt_ftrylockfile (gpgrt_stream_t stream); void _gpgrt_funlockfile (gpgrt_stream_t stream); int _gpgrt_feof (gpgrt_stream_t stream); int _gpgrt_feof_unlocked (gpgrt_stream_t stream); int _gpgrt_ferror (gpgrt_stream_t stream); int _gpgrt_ferror_unlocked (gpgrt_stream_t stream); void _gpgrt_clearerr (gpgrt_stream_t stream); void _gpgrt_clearerr_unlocked (gpgrt_stream_t stream); int _gpgrt__pending (gpgrt_stream_t stream); int _gpgrt__pending_unlocked (gpgrt_stream_t stream); int _gpgrt_fflush (gpgrt_stream_t stream); int _gpgrt_fseek (gpgrt_stream_t stream, long int offset, int whence); int _gpgrt_fseeko (gpgrt_stream_t stream, gpgrt_off_t offset, int whence); long int _gpgrt_ftell (gpgrt_stream_t stream); gpgrt_off_t _gpgrt_ftello (gpgrt_stream_t stream); void _gpgrt_rewind (gpgrt_stream_t stream); +int _gpgrt_ftruncate (estream_t stream, gpgrt_off_t length); int _gpgrt_fgetc (gpgrt_stream_t stream); int _gpgrt_fputc (int c, gpgrt_stream_t stream); int _gpgrt__getc_underflow (gpgrt_stream_t stream); int _gpgrt__putc_overflow (int c, gpgrt_stream_t stream); /* Note: Keeps the next two macros in sync with their counterparts in gpg-error.h. */ #define _gpgrt_getc_unlocked(stream) \ (((!(stream)->flags.writing) \ && ((stream)->data_offset < (stream)->data_len) \ && (! (stream)->unread_data_len)) \ ? ((int) (stream)->buffer[((stream)->data_offset)++]) \ : _gpgrt__getc_underflow ((stream))) #define _gpgrt_putc_unlocked(c, stream) \ (((stream)->flags.writing \ && ((stream)->data_offset < (stream)->buffer_size) \ && (c != '\n')) \ ? ((int) ((stream)->buffer[((stream)->data_offset)++] = (c))) \ : _gpgrt__putc_overflow ((c), (stream))) int _gpgrt_ungetc (int c, gpgrt_stream_t stream); int _gpgrt_read (gpgrt_stream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT buffer, size_t bytes_to_read, size_t *_GPGRT__RESTRICT bytes_read); int _gpgrt_write (gpgrt_stream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t bytes_to_write, size_t *_GPGRT__RESTRICT bytes_written); int _gpgrt_write_sanitized (gpgrt_stream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t length, const char *delimiters, size_t *_GPGRT__RESTRICT bytes_written); int _gpgrt_write_hexstring (gpgrt_stream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t length, int reserved, size_t *_GPGRT__RESTRICT bytes_written); size_t _gpgrt_fread (void *_GPGRT__RESTRICT ptr, size_t size, size_t nitems, gpgrt_stream_t _GPGRT__RESTRICT stream); size_t _gpgrt_fwrite (const void *_GPGRT__RESTRICT ptr, size_t size, size_t memb, gpgrt_stream_t _GPGRT__RESTRICT stream); char *_gpgrt_fgets (char *_GPGRT__RESTRICT s, int n, gpgrt_stream_t _GPGRT__RESTRICT stream); int _gpgrt_fputs (const char *_GPGRT__RESTRICT s, gpgrt_stream_t _GPGRT__RESTRICT stream); int _gpgrt_fputs_unlocked (const char *_GPGRT__RESTRICT s, gpgrt_stream_t _GPGRT__RESTRICT stream); gpgrt_ssize_t _gpgrt_getline (char *_GPGRT__RESTRICT *_GPGRT__RESTRICT lineptr, size_t *_GPGRT__RESTRICT n, gpgrt_stream_t stream); gpgrt_ssize_t _gpgrt_read_line (gpgrt_stream_t stream, char **addr_of_buffer, size_t *length_of_buffer, size_t *max_length); int _gpgrt_fprintf (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(2,3); int _gpgrt_fprintf_unlocked (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) GPGRT_ATTR_PRINTF(2,3); int _gpgrt_vfprintf (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) GPGRT_ATTR_PRINTF(2,0); int _gpgrt_vfprintf_unlocked (gpgrt_stream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) GPGRT_ATTR_PRINTF(2,0); int _gpgrt_setvbuf (gpgrt_stream_t _GPGRT__RESTRICT stream, char *_GPGRT__RESTRICT buf, int mode, size_t size); void _gpgrt_set_binary (gpgrt_stream_t stream); int _gpgrt_set_nonblock (gpgrt_stream_t stream, int onoff); int _gpgrt_get_nonblock (gpgrt_stream_t stream); int _gpgrt_poll (gpgrt_poll_t *fds, unsigned int nfds, int timeout); gpgrt_stream_t _gpgrt_tmpfile (void); void _gpgrt_opaque_set (gpgrt_stream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT opaque); void *_gpgrt_opaque_get (gpgrt_stream_t stream); void _gpgrt_fname_set (gpgrt_stream_t stream, const char *fname); const char *_gpgrt_fname_get (gpgrt_stream_t stream); #include "estream-printf.h" /* Make sure we always use our snprintf */ #define snprintf _gpgrt_estream_snprintf #if HAVE_W32_SYSTEM /* Prototypes for w32-estream.c. */ struct cookie_io_functions_s _gpgrt_functions_w32_pollable; int _gpgrt_w32_pollable_create (void *_GPGRT__RESTRICT *_GPGRT__RESTRICT cookie, unsigned int modeflags, struct cookie_io_functions_s next_functions, void *next_cookie); int _gpgrt_w32_poll (gpgrt_poll_t *fds, size_t nfds, int timeout); #endif /*HAVE_W32_SYSTEM*/ /* * Local prototypes for the encoders. */ struct _gpgrt_b64state { int idx; int quad_count; estream_t stream; char *title; unsigned char radbuf[4]; unsigned int crc; gpg_err_code_t lasterr; unsigned int flags; int stop_seen:1; int invalid_encoding:1; int using_decoder:1; }; gpgrt_b64state_t _gpgrt_b64enc_start (estream_t stream, const char *title); gpg_err_code_t _gpgrt_b64enc_write (gpgrt_b64state_t state, const void *buffer, size_t nbytes); gpg_err_code_t _gpgrt_b64enc_finish (gpgrt_b64state_t state); gpgrt_b64state_t _gpgrt_b64dec_start (const char *title); gpg_err_code_t _gpgrt_b64dec_proc (gpgrt_b64state_t state, void *buffer, size_t length, size_t *r_nbytes); gpg_err_code_t _gpgrt_b64dec_finish (gpgrt_b64state_t state); /* * Local prototypes for logging */ int _gpgrt_get_errorcount (int clear); void _gpgrt_inc_errorcount (void); void _gpgrt_log_set_sink (const char *name, estream_t stream, int fd); void _gpgrt_log_set_socket_dir_cb (const char *(*fnc)(void)); void _gpgrt_log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)); void _gpgrt_log_set_prefix (const char *text, unsigned int flags); const char *_gpgrt_log_get_prefix (unsigned int *flags); int _gpgrt_log_test_fd (int fd); int _gpgrt_log_get_fd (void); estream_t _gpgrt_log_get_stream (void); void _gpgrt_log (int level, const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3); void _gpgrt_logv (int level, const char *fmt, va_list arg_ptr); void _gpgrt_logv_prefix (int level, const char *prefix, const char *fmt, va_list arg_ptr); void _gpgrt_log_string (int level, const char *string); void _gpgrt_log_bug (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); void _gpgrt_log_fatal (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); void _gpgrt_log_error (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_log_info (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_log_debug (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_log_debug_string (const char *string, const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3); void _gpgrt_log_printf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt_log_flush (void); void _gpgrt_logv_printhex (const void *buffer, size_t length, const char *fmt, va_list arg_ptr); void _gpgrt_log_printhex (const void *buffer, size_t length, const char *fmt, ...) GPGRT_ATTR_PRINTF(3,4);; void _gpgrt_logv_clock (const char *fmt, va_list arg_ptr); void _gpgrt_log_clock (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); void _gpgrt__log_assert (const char *expr, const char *file, int line, const char *func) GPGRT_ATTR_NORETURN; /* Redefine the assert macro to use our internal function. */ #undef gpgrt_assert #ifdef GPGRT_HAVE_MACRO_FUNCTION #define gpgrt_assert(expr) \ ((expr) \ ? (void) 0 \ : _gpgrt__log_assert (#expr, __FILE__, __LINE__, __FUNCTION__)) #else /*!GPGRT_HAVE_MACRO_FUNCTION*/ /* # define BUG() bug_at( __FILE__ , __LINE__ ) */ #define gpgrt_assert(expr) \ ((expr) \ ? (void) 0 \ : _gpgrt__log_assert (#expr, __FILE__, __LINE__, NULL)) #endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ /* Note: The next function is only to be used by visibility.c. */ int _gpgrt_logv_internal (int level, int ignore_arg_ptr, const char *extrastring, const char *prefmt, const char *fmt, va_list arg_ptr); /* * Local prototypes for the spawn functions. * * We put the docs here because we have separate implementations in * the files spawn-posix.c and spawn-w32.c */ /* Return the maximum number of currently allowed file descriptors. * Only useful on POSIX systems. */ /* int get_max_fds (void); */ /* Close all file descriptors starting with descriptor FIRST. If * EXCEPT is not NULL, it is expected to be a list of file descriptors * which are not to close. This list shall be sorted in ascending * order with its end marked by -1. */ /* void close_all_fds (int first, int *except); */ /* Returns an array with all currently open file descriptors. The end * of the array is marked by -1. The caller needs to release this * array using the *standard free* and not with xfree. This allow the * use of this function right at startup even before libgcrypt has * been initialized. Returns NULL on error and sets ERRNO accordingly. */ /* int *get_all_open_fds (void); */ /* Create a pipe. The DIRECTION parameter gives the type of the created pipe: * DIRECTION < 0 := Inbound pipe: On Windows the write end is inheritable. * DIRECTION > 0 := Outbound pipe: On Windows the read end is inheritable. * If R_FP is NULL a standard pipe and no stream is created, DIRECTION * should then be 0. */ gpg_err_code_t _gpgrt_make_pipe (int filedes[2], estream_t *r_fp, int direction, int nonblock); /* Convenience macros to create a pipe. */ #define _gpgrt_create_pipe(a) _gpgrt_make_pipe ((a),NULL, 0, 0); #define _gpgrt_create_inbound_pipe(a,b,c) _gpgrt_make_pipe ((a), (b), -1, (c)); #define _gpgrt_create_outbound_pipe(a,b,c) _gpgrt_make_pipe ((a), (b), 1, (c)); /* Fork and exec the program PGMNAME. * * If R_INFP is NULL connect stdin of the new process to /dev/null; if * it is not NULL store the address of a pointer to a new estream * there. If R_OUTFP is NULL connect stdout of the new process to * /dev/null; if it is not NULL store the address of a pointer to a * new estream there. If R_ERRFP is NULL connect stderr of the new * process to /dev/null; if it is not NULL store the address of a * pointer to a new estream there. On success the pid of the new * process is stored at PID. On error -1 is stored at PID and if * R_OUTFP or R_ERRFP are not NULL, NULL is stored there. * * The arguments for the process are expected in the NULL terminated * array ARGV. The program name itself should not be included there. * If PREEXEC is not NULL, the given function will be called right * before the exec. * * IF EXCEPT is not NULL, it is expected to be an ordered list of file * descriptors, terminated by an entry with the value (-1). These * file descriptors won't be closed before spawning a new program. * * Returns 0 on success or an error code. Calling gpgrt_wait_process * and gpgrt_release_process is required if the function succeeded. * * FLAGS is a bit vector: * * GPGRT_SPAWN_NONBLOCK * If set the two output streams are created in non-blocking * mode and the input stream is switched to non-blocking mode. * This is merely a convenience feature because the caller * could do the same with gpgrt_set_nonblock. Does not yet * work for Windows. * * GPGRT_SPAWN_DETACHED * If set the process will be started as a background process. * This flag is only useful under W32 (but not W32CE) systems, * so that no new console is created and pops up a console * window when starting the server. Does not work on W32CE. * * GPGRT_SPAWN_RUN_ASFW * On W32 (but not on W32CE) run AllowSetForegroundWindow for * the child. Note that due to unknown problems this actually * allows SetForegroundWindow for all children of this process. */ gpg_err_code_t _gpgrt_spawn_process (const char *pgmname, const char *argv[], int *execpt, void (*preexec)(void), unsigned int flags, estream_t *r_infp, estream_t *r_outfp, estream_t *r_errfp, pid_t *pid); /* Simplified version of gpgrt_spawn_process. This function forks and * then execs PGMNAME, while connecting INFD to stdin, OUTFD to stdout * and ERRFD to stderr (any of them may be -1 to connect them to * /dev/null). The arguments for the process are expected in the NULL * terminated array ARGV. The program name itself should not be * included there. Calling gpgrt_wait_process and * gpgrt_release_process is required. Returns 0 on success or an * error code. */ gpg_err_code_t _gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], int infd, int outfd, int errfd, pid_t *pid); /* Spawn a new process and immediately detach from it. The name of * the program to exec is PGMNAME and its arguments are in ARGV (the * programname is automatically passed as first argument). * Environment strings in ENVP are set. An error is returned if * pgmname is not executable; to make this work it is necessary to * provide an absolute file name. */ gpg_err_code_t _gpgrt_spawn_process_detached (const char *pgmname, const char *argv[], const char *envp[] ); /* If HANG is true, waits for the process identified by PID to exit; * if HANG is false, checks whether the process has terminated. * PGMNAME should be the same as supplied to the spawn function and is * only used for diagnostics. Return values: * * 0 * The process exited successful. 0 is stored at R_EXITCODE. * * GPG_ERR_GENERAL * The process exited without success. The exit code of process * is then stored at R_EXITCODE. An exit code of -1 indicates * that the process terminated abnormally (e.g. due to a signal). * * GPG_ERR_TIMEOUT * The process is still running (returned only if HANG is false). * * GPG_ERR_INV_VALUE * An invalid PID has been specified. * * Other error codes may be returned as well. Unless otherwise noted, * -1 will be stored at R_EXITCODE. R_EXITCODE may be passed as NULL * if the exit code is not required (in that case an error message will * be printed). Note that under Windows PID is not the process id but * the handle of the process. */ gpg_err_code_t _gpgrt_wait_process (const char *pgmname, pid_t pid, int hang, int *r_exitcode); /* Like _gpgrt_wait_process, but for COUNT processes. */ gpg_err_code_t _gpgrt_wait_processes (const char **pgmnames, pid_t *pids, size_t count, int hang, int *r_exitcodes); /* Kill a process; that is send an appropriate signal to the process. * gpgrt_wait_process must be called to actually remove the process * from the system. An invalid PID is ignored. */ void _gpgrt_kill_process (pid_t pid); /* Release the process identified by PID. This function is actually * only required for Windows but it does not harm to always call it. * It is a nop if PID is invalid. */ void _gpgrt_release_process (pid_t pid); /* * Local prototypes for argparse. */ int _gpgrt_argparse (estream_t fp, gpgrt_argparse_t *arg, gpgrt_opt_t *opts); void _gpgrt_usage (int level); const char *_gpgrt_strusage (int level); void _gpgrt_set_strusage (const char *(*f)(int)); void _gpgrt_set_usage_outfnc (int (*fnc)(int, const char *)); void _gpgrt_set_fixed_string_mapper (const char *(*f)(const char*)); /* * Various helper functions */ int _gpgrt_cmp_version (const char *a, const char *b, int level); /* * Internal platform abstraction functions (sysutils.c) */ /* Return true if FD is valid. */ int _gpgrt_fd_valid_p (int fd); /* A getenv variant which returns a malloced copy. */ char *_gpgrt_getenv (const char *name); /* A setenv variant which can be used for unsetenv by setting VALUE to * NULL and OVERRIDE to true. */ gpg_err_code_t _gpgrt_setenv (const char *name, const char *value, int overwrite); /* A wrapper around mkdir using a string for the mode (permissions). */ gpg_err_code_t _gpgrt_mkdir (const char *name, const char *modestr); /* A simple wrapper around chdir. */ gpg_err_code_t _gpgrt_chdir (const char *name); /* Return the current WD as a malloced string. */ char *_gpgrt_getcwd (void); /* * Platform specific functions (Windows) */ #ifdef HAVE_W32_SYSTEM char *_gpgrt_w32_reg_query_string (const char *root, const char *dir, const char *name); #endif /*HAVE_W32_SYSTEM*/ /* * Missing functions implemented inline. */ #ifndef HAVE_STPCPY static GPG_ERR_INLINE char * _gpgrt_stpcpy (char *a, const char *b) { while (*b) *a++ = *b++; *a = 0; return a; } #define stpcpy(a,b) _gpgrt_stpcpy ((a), (b)) #endif /*!HAVE_STPCPY*/ #endif /*_GPGRT_GPGRT_INT_H*/ diff --git a/src/visibility.c b/src/visibility.c index ab5e383..1947111 100644 --- a/src/visibility.c +++ b/src/visibility.c @@ -1,1145 +1,1151 @@ /* visibility.c - Wrapper for all public functions. * Copyright (C) 2014 g10 Code GmbH * * This file is part of libgpg-error. * * libgpg-error is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libgpg-error is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ #include #include #include /* For abort(). */ #define _GPGRT_INCL_BY_VISIBILITY_C 1 #include "gpgrt-int.h" const char * gpg_strerror (gpg_error_t err) { return _gpg_strerror (err); } int gpg_strerror_r (gpg_error_t err, char *buf, size_t buflen) { return _gpg_strerror_r (err, buf, buflen); } const char * gpg_strsource (gpg_error_t err) { return _gpg_strsource (err); } gpg_err_code_t gpg_err_code_from_errno (int err) { return _gpg_err_code_from_errno (err); } int gpg_err_code_to_errno (gpg_err_code_t code) { return _gpg_err_code_to_errno (code); } gpg_err_code_t gpg_err_code_from_syserror (void) { return _gpg_err_code_from_syserror (); } void gpg_err_set_errno (int err) { _gpg_err_set_errno (err); } gpg_error_t gpg_err_init (void) { return _gpg_err_init (); } void gpg_err_deinit (int mode) { _gpg_err_deinit (mode); } const char * gpg_error_check_version (const char *req_version) { return _gpg_error_check_version (req_version); } const char * gpgrt_check_version (const char *req_version) { return _gpg_error_check_version (req_version); } void gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void)) { _gpgrt_set_syscall_clamp (pre, post); } void gpgrt_get_syscall_clamp (void (**r_pre)(void), void (**r_post)(void)) { _gpgrt_get_syscall_clamp (r_pre, r_post); } void gpgrt_set_alloc_func (void *(*f)(void *a, size_t n)) { _gpgrt_set_alloc_func (f); } gpg_err_code_t gpgrt_lock_init (gpgrt_lock_t *lockhd) { return _gpgrt_lock_init (lockhd); } gpg_err_code_t gpgrt_lock_lock (gpgrt_lock_t *lockhd) { return _gpgrt_lock_lock (lockhd); } gpg_err_code_t gpgrt_lock_trylock (gpgrt_lock_t *lockhd) { return _gpgrt_lock_trylock (lockhd); } gpg_err_code_t gpgrt_lock_unlock (gpgrt_lock_t *lockhd) { return _gpgrt_lock_unlock (lockhd); } gpg_err_code_t gpgrt_lock_destroy (gpgrt_lock_t *lockhd) { return _gpgrt_lock_destroy (lockhd); } gpg_err_code_t gpgrt_yield (void) { return _gpgrt_yield (); } estream_t gpgrt_fopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode) { return _gpgrt_fopen (path, mode); } estream_t gpgrt_mopen (void *_GPGRT__RESTRICT data, size_t data_n, size_t data_len, unsigned int grow, void *(*func_realloc) (void *mem, size_t size), void (*func_free) (void *mem), const char *_GPGRT__RESTRICT mode) { return _gpgrt_mopen (data, data_n, data_len, grow, func_realloc, func_free, mode); } estream_t gpgrt_fopenmem (size_t memlimit, const char *_GPGRT__RESTRICT mode) { return _gpgrt_fopenmem (memlimit, mode); } estream_t gpgrt_fopenmem_init (size_t memlimit, const char *_GPGRT__RESTRICT mode, const void *data, size_t datalen) { return _gpgrt_fopenmem_init (memlimit, mode, data, datalen); } estream_t gpgrt_fdopen (int filedes, const char *mode) { return _gpgrt_fdopen (filedes, mode); } estream_t gpgrt_fdopen_nc (int filedes, const char *mode) { return _gpgrt_fdopen_nc (filedes, mode); } estream_t gpgrt_sysopen (es_syshd_t *syshd, const char *mode) { return _gpgrt_sysopen (syshd, mode); } estream_t gpgrt_sysopen_nc (es_syshd_t *syshd, const char *mode) { return _gpgrt_sysopen_nc (syshd, mode); } estream_t gpgrt_fpopen (FILE *fp, const char *mode) { return _gpgrt_fpopen (fp, mode); } estream_t gpgrt_fpopen_nc (FILE *fp, const char *mode) { return _gpgrt_fpopen_nc (fp, mode); } estream_t gpgrt_freopen (const char *_GPGRT__RESTRICT path, const char *_GPGRT__RESTRICT mode, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_freopen (path, mode, stream); } estream_t gpgrt_fopencookie (void *_GPGRT__RESTRICT cookie, const char *_GPGRT__RESTRICT mode, gpgrt_cookie_io_functions_t functions) { return _gpgrt_fopencookie (cookie, mode, functions); } int gpgrt_fclose (estream_t stream) { return _gpgrt_fclose (stream); } int gpgrt_fclose_snatch (estream_t stream, void **r_buffer, size_t *r_buflen) { return _gpgrt_fclose_snatch (stream, r_buffer, r_buflen); } int gpgrt_onclose (estream_t stream, int mode, void (*fnc) (estream_t, void*), void *fnc_value) { return _gpgrt_onclose (stream, mode, fnc, fnc_value); } int gpgrt_fileno (estream_t stream) { return _gpgrt_fileno (stream); } int gpgrt_fileno_unlocked (estream_t stream) { return _gpgrt_fileno_unlocked (stream); } int gpgrt_syshd (estream_t stream, es_syshd_t *syshd) { return _gpgrt_syshd (stream, syshd); } int gpgrt_syshd_unlocked (estream_t stream, es_syshd_t *syshd) { return _gpgrt_syshd_unlocked (stream, syshd); } void _gpgrt_set_std_fd (int no, int fd) { _gpgrt__set_std_fd (no, fd); /* (double dash in name) */ } estream_t _gpgrt_get_std_stream (int fd) { return _gpgrt__get_std_stream (fd); /* (double dash in name) */ } void gpgrt_flockfile (estream_t stream) { _gpgrt_flockfile (stream); } int gpgrt_ftrylockfile (estream_t stream) { return _gpgrt_ftrylockfile (stream); } void gpgrt_funlockfile (estream_t stream) { _gpgrt_funlockfile (stream); } int _gpgrt_pending (estream_t stream) { return _gpgrt__pending (stream); } int _gpgrt_pending_unlocked (estream_t stream) { return _gpgrt__pending_unlocked (stream); } int gpgrt_feof (estream_t stream) { return _gpgrt_feof (stream); } int gpgrt_feof_unlocked (estream_t stream) { return _gpgrt_feof_unlocked (stream); } int gpgrt_ferror (estream_t stream) { return _gpgrt_ferror (stream); } int gpgrt_ferror_unlocked (estream_t stream) { return _gpgrt_ferror_unlocked (stream); } void gpgrt_clearerr (estream_t stream) { _gpgrt_clearerr (stream); } void gpgrt_clearerr_unlocked (estream_t stream) { _gpgrt_clearerr_unlocked (stream); } int gpgrt_fflush (estream_t stream) { return _gpgrt_fflush (stream); } int gpgrt_fseek (estream_t stream, long int offset, int whence) { return _gpgrt_fseek (stream, offset, whence); } int gpgrt_fseeko (estream_t stream, gpgrt_off_t offset, int whence) { return _gpgrt_fseeko (stream, offset, whence); } long int gpgrt_ftell (estream_t stream) { return _gpgrt_ftell (stream); } gpgrt_off_t gpgrt_ftello (estream_t stream) { return _gpgrt_ftello (stream); } void gpgrt_rewind (estream_t stream) { _gpgrt_rewind (stream); } +int +gpgrt_ftruncate (estream_t stream, gpgrt_off_t length) +{ + return _gpgrt_ftruncate (stream, length); +} + int gpgrt_fgetc (estream_t stream) { return _gpgrt_fgetc (stream); } int _gpgrt_getc_underflow (estream_t stream) { return _gpgrt__getc_underflow (stream); } int gpgrt_fputc (int c, estream_t stream) { return _gpgrt_fputc (c, stream); } int _gpgrt_putc_overflow (int c, estream_t stream) { return _gpgrt__putc_overflow (c, stream); } int gpgrt_ungetc (int c, estream_t stream) { return _gpgrt_ungetc (c, stream); } int gpgrt_read (estream_t _GPGRT__RESTRICT stream, void *_GPGRT__RESTRICT buffer, size_t bytes_to_read, size_t *_GPGRT__RESTRICT bytes_read) { return _gpgrt_read (stream, buffer, bytes_to_read, bytes_read); } int gpgrt_write (estream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t bytes_to_write, size_t *_GPGRT__RESTRICT bytes_written) { return _gpgrt_write (stream, buffer, bytes_to_write, bytes_written); } int gpgrt_write_sanitized (estream_t _GPGRT__RESTRICT stream, const void * _GPGRT__RESTRICT buffer, size_t length, const char * delimiters, size_t * _GPGRT__RESTRICT bytes_written) { return _gpgrt_write_sanitized (stream, buffer, length, delimiters, bytes_written); } int gpgrt_write_hexstring (estream_t _GPGRT__RESTRICT stream, const void *_GPGRT__RESTRICT buffer, size_t length, int reserved, size_t *_GPGRT__RESTRICT bytes_written ) { return _gpgrt_write_hexstring (stream, buffer, length, reserved, bytes_written); } size_t gpgrt_fread (void *_GPGRT__RESTRICT ptr, size_t size, size_t nitems, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_fread (ptr, size, nitems, stream); } size_t gpgrt_fwrite (const void *_GPGRT__RESTRICT ptr, size_t size, size_t nitems, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_fwrite (ptr, size, nitems, stream); } char * gpgrt_fgets (char *_GPGRT__RESTRICT buffer, int length, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_fgets (buffer, length, stream); } int gpgrt_fputs (const char *_GPGRT__RESTRICT s, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_fputs (s, stream); } int gpgrt_fputs_unlocked (const char *_GPGRT__RESTRICT s, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_fputs_unlocked (s, stream); } gpgrt_ssize_t gpgrt_getline (char *_GPGRT__RESTRICT *_GPGRT__RESTRICT lineptr, size_t *_GPGRT__RESTRICT n, estream_t _GPGRT__RESTRICT stream) { return _gpgrt_getline (lineptr, n, stream); } gpgrt_ssize_t gpgrt_read_line (estream_t stream, char **addr_of_buffer, size_t *length_of_buffer, size_t *max_length) { return _gpgrt_read_line (stream, addr_of_buffer, length_of_buffer, max_length); } int gpgrt_vfprintf (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) { return _gpgrt_vfprintf (stream, format, ap); } int gpgrt_vfprintf_unlocked (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, va_list ap) { return _gpgrt_vfprintf_unlocked (stream, format, ap); } int gpgrt_printf (const char *_GPGRT__RESTRICT format, ...) { va_list ap; int rc; va_start (ap, format); rc = _gpgrt_vfprintf (es_stdout, format, ap); va_end (ap); return rc; } int gpgrt_printf_unlocked (const char *_GPGRT__RESTRICT format, ...) { va_list ap; int rc; va_start (ap, format); rc = _gpgrt_vfprintf_unlocked (es_stdout, format, ap); va_end (ap); return rc; } int gpgrt_fprintf (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) { va_list ap; int rc; va_start (ap, format); rc = _gpgrt_vfprintf (stream, format, ap); va_end (ap); return rc; } int gpgrt_fprintf_unlocked (estream_t _GPGRT__RESTRICT stream, const char *_GPGRT__RESTRICT format, ...) { va_list ap; int rc; va_start (ap, format); rc = _gpgrt_vfprintf_unlocked (stream, format, ap); va_end (ap); return rc; } int gpgrt_setvbuf (estream_t _GPGRT__RESTRICT stream, char *_GPGRT__RESTRICT buf, int type, size_t size) { return _gpgrt_setvbuf (stream, buf, type, size); } void gpgrt_setbuf (estream_t _GPGRT__RESTRICT stream, char *_GPGRT__RESTRICT buf) { _gpgrt_setvbuf (stream, buf, buf? _IOFBF : _IONBF, BUFSIZ); } void gpgrt_set_binary (estream_t stream) { _gpgrt_set_binary (stream); } int gpgrt_set_nonblock (estream_t stream, int onoff) { return _gpgrt_set_nonblock (stream, onoff); } int gpgrt_get_nonblock (estream_t stream) { return _gpgrt_get_nonblock (stream); } int gpgrt_poll (gpgrt_poll_t *fds, unsigned int nfds, int timeout) { return _gpgrt_poll (fds, nfds, timeout); } estream_t gpgrt_tmpfile (void) { return _gpgrt_tmpfile (); } void gpgrt_opaque_set (estream_t stream, void *opaque) { _gpgrt_opaque_set (stream, opaque); } void * gpgrt_opaque_get (estream_t stream) { return _gpgrt_opaque_get (stream); } void gpgrt_fname_set (estream_t stream, const char *fname) { _gpgrt_fname_set (stream, fname); } const char * gpgrt_fname_get (estream_t stream) { return _gpgrt_fname_get (stream); } int gpgrt_asprintf (char **r_buf, const char *_GPGRT__RESTRICT format, ...) { va_list ap; int rc; va_start (ap, format); rc = _gpgrt_estream_vasprintf (r_buf, format, ap); va_end (ap); return rc; } int gpgrt_vasprintf (char **r_buf, const char *_GPGRT__RESTRICT format, va_list ap) { return _gpgrt_estream_vasprintf (r_buf, format, ap); } char * gpgrt_bsprintf (const char *_GPGRT__RESTRICT format, ...) { int rc; va_list ap; char *buf; va_start (ap, format); rc = _gpgrt_estream_vasprintf (&buf, format, ap); va_end (ap); if (rc < 0) return NULL; return buf; } char * gpgrt_vbsprintf (const char *_GPGRT__RESTRICT format, va_list ap) { int rc; char *buf; rc = _gpgrt_estream_vasprintf (&buf, format, ap); if (rc < 0) return NULL; return buf; } int gpgrt_snprintf (char *buf, size_t bufsize, const char *format, ...) { int rc; va_list arg_ptr; va_start (arg_ptr, format); rc = _gpgrt_estream_vsnprintf (buf, bufsize, format, arg_ptr); va_end (arg_ptr); return rc; } int gpgrt_vsnprintf (char *buf, size_t bufsize, const char *format, va_list arg_ptr) { return _gpgrt_estream_vsnprintf (buf, bufsize, format, arg_ptr); } void * gpgrt_realloc (void *a, size_t n) { return _gpgrt_realloc (a, n); } void * gpgrt_malloc (size_t n) { return _gpgrt_malloc (n); } void * gpgrt_calloc (size_t n, size_t m) { return _gpgrt_calloc (n, m); } char * gpgrt_strdup (const char *string) { return _gpgrt_strdup (string); } char * gpgrt_strconcat (const char *s1, ...) { va_list arg_ptr; char *result; if (!s1) result = _gpgrt_strdup (""); else { va_start (arg_ptr, s1); result = _gpgrt_strconcat_core (s1, arg_ptr); va_end (arg_ptr); } return result; } void gpgrt_free (void *a) { if (a) _gpgrt_free (a); } char * gpgrt_getenv (const char *name) { return _gpgrt_getenv (name); } gpg_err_code_t gpgrt_setenv (const char *name, const char *value, int overwrite) { return _gpgrt_setenv (name, value, overwrite); } gpg_err_code_t gpgrt_mkdir (const char *name, const char *modestr) { return _gpgrt_mkdir (name, modestr); } gpg_err_code_t gpgrt_chdir (const char *name) { return _gpgrt_chdir (name); } char * gpgrt_getcwd (void) { return _gpgrt_getcwd (); } gpgrt_b64state_t gpgrt_b64enc_start (estream_t stream, const char *title) { return _gpgrt_b64enc_start (stream, title); } gpg_err_code_t gpgrt_b64enc_write (gpgrt_b64state_t state, const void *buffer, size_t nbytes) { return _gpgrt_b64enc_write (state, buffer, nbytes); } gpg_err_code_t gpgrt_b64enc_finish (gpgrt_b64state_t state) { return _gpgrt_b64enc_finish (state); } gpgrt_b64state_t gpgrt_b64dec_start (const char *title) { return _gpgrt_b64dec_start (title); } gpg_error_t gpgrt_b64dec_proc (gpgrt_b64state_t state, void *buffer, size_t length, size_t *r_nbytes) { return _gpgrt_b64dec_proc (state, buffer, length, r_nbytes); } gpg_error_t gpgrt_b64dec_finish (gpgrt_b64state_t state) { return _gpgrt_b64dec_finish (state); } int gpgrt_get_errorcount (int clear) { return _gpgrt_get_errorcount (clear); } void gpgrt_inc_errorcount (void) { _gpgrt_inc_errorcount (); } void gpgrt_log_set_sink (const char *name, estream_t stream, int fd) { _gpgrt_log_set_sink (name, stream, fd); } void gpgrt_log_set_socket_dir_cb (const char *(*fnc)(void)) { _gpgrt_log_set_socket_dir_cb (fnc); } void gpgrt_log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)) { _gpgrt_log_set_pid_suffix_cb (cb); } void gpgrt_log_set_prefix (const char *text, unsigned int flags) { _gpgrt_log_set_prefix (text, flags); } const char * gpgrt_log_get_prefix (unsigned int *flags) { return _gpgrt_log_get_prefix (flags); } int gpgrt_log_test_fd (int fd) { return _gpgrt_log_test_fd (fd); } int gpgrt_log_get_fd (void) { return _gpgrt_log_get_fd (); } estream_t gpgrt_log_get_stream (void) { return _gpgrt_log_get_stream (); } void gpgrt_log (int level, const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt) ; _gpgrt_logv (level, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_logv (int level, const char *fmt, va_list arg_ptr) { _gpgrt_logv (level, fmt, arg_ptr); } void gpgrt_logv_prefix (int level, const char *prefix, const char *fmt, va_list arg_ptr) { _gpgrt_logv_prefix (level, prefix, fmt, arg_ptr); } void gpgrt_log_string (int level, const char *string) { _gpgrt_log_string (level, string); } void gpgrt_log_info (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv (GPGRT_LOGLVL_INFO, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_error (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv (GPGRT_LOGLVL_ERROR, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_fatal (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv (GPGRT_LOGLVL_FATAL, fmt, arg_ptr); va_end (arg_ptr); abort (); /* Never called; just to make the compiler happy. */ } void gpgrt_log_bug (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv (GPGRT_LOGLVL_BUG, fmt, arg_ptr); va_end (arg_ptr); abort (); /* Never called; just to make the compiler happy. */ } void gpgrt_log_debug (const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); _gpgrt_logv (GPGRT_LOGLVL_DEBUG, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_debug_string (const char *string, const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); _gpgrt_logv_internal (GPGRT_LOGLVL_DEBUG, 0, string, NULL, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_printf (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv (fmt ? GPGRT_LOGLVL_CONT : GPGRT_LOGLVL_BEGIN, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_flush (void) { _gpgrt_log_flush (); } void gpgrt_log_printhex (const void *buffer, size_t length, const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv_printhex (buffer, length, fmt, arg_ptr); va_end (arg_ptr); } void gpgrt_log_clock (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); _gpgrt_logv_clock (fmt, arg_ptr); va_end (arg_ptr); } void _gpgrt_log_assert (const char *expr, const char *file, int line, const char *func) { #ifdef GPGRT_HAVE_MACRO_FUNCTION _gpgrt__log_assert (expr, file, line, func); #else _gpgrt__log_assert (expr, file, line); #endif } #if 0 gpg_err_code_t gpgrt_make_pipe (int filedes[2], estream_t *r_fp, int direction, int nonblock) { return _gpgrt_make_pipe (filedes, r_fp, direction, nonblock); } gpg_err_code_t gpgrt_spawn_process (const char *pgmname, const char *argv[], int *except, void (*preexec)(void), unsigned int flags, estream_t *r_infp, estream_t *r_outfp, estream_t *r_errfp, pid_t *pid) { return _gpgrt_spawn_process (pgmname, argv, except, preexec, flags, r_infp, r_outfp, r_errfp, pid); } gpg_err_code_t gpgrt_spawn_process_fd (const char *pgmname, const char *argv[], int infd, int outfd, int errfd, pid_t *pid) { return _gpgrt_spawn_process_fd (pgmname, argv, infd, outfd, errfd, pid); } gpg_err_code_t gpgrt_spawn_process_detached (const char *pgmname, const char *argv[], const char *envp[]) { return _gpgrt_spawn_process_detached (pgmname, argv, envp); } gpg_err_code_t gpgrt_wait_process (const char *pgmname, pid_t pid, int hang, int *r_exitcode) { return _gpgrt_wait_process (pgmname, pid, hang, r_exitcode); } gpg_err_code_t gpgrt_wait_processes (const char **pgmnames, pid_t *pids, size_t count, int hang, int *r_exitcodes) { return _gpgrt_wait_processes (pgmnames, pids, count, hang, r_exitcodes); } void gpgrt_kill_process (pid_t pid) { _gpgrt_kill_process (pid); } void gpgrt_release_process (pid_t pid) { _gpgrt_release_process (pid); } #endif /*0*/ int gpgrt_argparse (estream_t fp, gpgrt_argparse_t *arg, gpgrt_opt_t *opts) { return _gpgrt_argparse (fp, arg, opts); } void gpgrt_usage (int level) { _gpgrt_usage (level); } const char * gpgrt_strusage (int level) { return _gpgrt_strusage (level); } void gpgrt_set_strusage (const char *(*f)(int)) { _gpgrt_set_strusage (f); } void gpgrt_set_usage_outfnc (int (*f)(int, const char *)) { _gpgrt_set_usage_outfnc (f); } void gpgrt_set_fixed_string_mapper (const char *(*f)(const char*)) { _gpgrt_set_fixed_string_mapper (f); } /* Compare program versions. */ int gpgrt_cmp_version (const char *a, const char *b, int level) { return _gpgrt_cmp_version (a, b, level); } /* For consistency reasons we use function wrappers also for Windows * specific function despite that they are technically not needed. */ #ifdef HAVE_W32_SYSTEM char * gpgrt_w32_reg_query_string (const char *root, const char *dir, const char *name) { return _gpgrt_w32_reg_query_string (root, dir, name); } #endif /*HAVE_W32_SYSTEM*/ diff --git a/src/visibility.h b/src/visibility.h index d6933df..46383c7 100644 --- a/src/visibility.h +++ b/src/visibility.h @@ -1,392 +1,394 @@ /* visibility.h - Set visibility attribute * Copyright (C) 2014 g10 Code GmbH * * This file is part of libgpg-error. * * libgpg-error is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libgpg-error is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ #ifndef _GPGRT_VISIBILITY_H #define _GPGRT_VISIBILITY_H /* Include the main header here so that public symbols are mapped to the internal underscored ones. */ #ifdef _GPGRT_INCL_BY_VISIBILITY_C # include "gpgrt-int.h" #endif /* Our use of the ELF visibility feature works by passing -fvisibiliy=hidden on the command line and by explicitly marking all exported functions as visible. NOTE: When adding new functions, please make sure to add them to gpg-error.vers and gpg-error.def.in as well. */ #ifdef _GPGRT_INCL_BY_VISIBILITY_C # ifdef GPGRT_USE_VISIBILITY # define MARK_VISIBLE(name) \ extern __typeof__ (name) name __attribute__ ((visibility("default"))); # else # define MARK_VISIBLE(name) /* */ # endif MARK_VISIBLE (gpg_strerror) MARK_VISIBLE (gpg_strerror_r) MARK_VISIBLE (gpg_strsource) MARK_VISIBLE (gpg_err_code_from_errno) MARK_VISIBLE (gpg_err_code_to_errno) MARK_VISIBLE (gpg_err_code_from_syserror) MARK_VISIBLE (gpg_err_set_errno) MARK_VISIBLE (gpg_err_init) MARK_VISIBLE (gpg_err_deinit) MARK_VISIBLE (gpg_error_check_version) MARK_VISIBLE (gpgrt_check_version) MARK_VISIBLE (gpgrt_lock_init) MARK_VISIBLE (gpgrt_lock_lock) MARK_VISIBLE (gpgrt_lock_unlock) MARK_VISIBLE (gpgrt_lock_destroy) MARK_VISIBLE (gpgrt_yield) MARK_VISIBLE (gpgrt_lock_trylock) MARK_VISIBLE (gpgrt_fopen) MARK_VISIBLE (gpgrt_mopen) MARK_VISIBLE (gpgrt_fopenmem) MARK_VISIBLE (gpgrt_fopenmem_init) MARK_VISIBLE (gpgrt_fdopen) MARK_VISIBLE (gpgrt_fdopen_nc) MARK_VISIBLE (gpgrt_sysopen) MARK_VISIBLE (gpgrt_sysopen_nc) MARK_VISIBLE (gpgrt_fpopen) MARK_VISIBLE (gpgrt_fpopen_nc) MARK_VISIBLE (gpgrt_freopen) MARK_VISIBLE (gpgrt_fopencookie) MARK_VISIBLE (gpgrt_fclose) MARK_VISIBLE (gpgrt_fclose_snatch) MARK_VISIBLE (gpgrt_onclose) MARK_VISIBLE (gpgrt_fileno) MARK_VISIBLE (gpgrt_fileno_unlocked) MARK_VISIBLE (gpgrt_syshd) MARK_VISIBLE (gpgrt_syshd_unlocked) MARK_VISIBLE (_gpgrt_set_std_fd) MARK_VISIBLE (_gpgrt_get_std_stream) MARK_VISIBLE (gpgrt_flockfile) MARK_VISIBLE (gpgrt_ftrylockfile) MARK_VISIBLE (gpgrt_funlockfile) MARK_VISIBLE (_gpgrt_pending) MARK_VISIBLE (_gpgrt_pending_unlocked) MARK_VISIBLE (gpgrt_feof) MARK_VISIBLE (gpgrt_feof_unlocked) MARK_VISIBLE (gpgrt_ferror) MARK_VISIBLE (gpgrt_ferror_unlocked) MARK_VISIBLE (gpgrt_clearerr) MARK_VISIBLE (gpgrt_clearerr_unlocked) MARK_VISIBLE (gpgrt_fflush) MARK_VISIBLE (gpgrt_fseek) MARK_VISIBLE (gpgrt_fseeko) MARK_VISIBLE (gpgrt_ftell) MARK_VISIBLE (gpgrt_ftello) MARK_VISIBLE (gpgrt_rewind) +MARK_VISIBLE (gpgrt_ftruncate) MARK_VISIBLE (gpgrt_fgetc) MARK_VISIBLE (_gpgrt_getc_underflow) MARK_VISIBLE (gpgrt_fputc) MARK_VISIBLE (_gpgrt_putc_overflow) MARK_VISIBLE (gpgrt_ungetc) MARK_VISIBLE (gpgrt_read) MARK_VISIBLE (gpgrt_write) MARK_VISIBLE (gpgrt_write_sanitized) MARK_VISIBLE (gpgrt_write_hexstring) MARK_VISIBLE (gpgrt_fread) MARK_VISIBLE (gpgrt_fwrite) MARK_VISIBLE (gpgrt_fgets) MARK_VISIBLE (gpgrt_fputs) MARK_VISIBLE (gpgrt_fputs_unlocked) MARK_VISIBLE (gpgrt_getline) MARK_VISIBLE (gpgrt_read_line) MARK_VISIBLE (gpgrt_fprintf) MARK_VISIBLE (gpgrt_fprintf_unlocked) MARK_VISIBLE (gpgrt_printf) MARK_VISIBLE (gpgrt_printf_unlocked) MARK_VISIBLE (gpgrt_vfprintf) MARK_VISIBLE (gpgrt_vfprintf_unlocked) MARK_VISIBLE (gpgrt_setvbuf) MARK_VISIBLE (gpgrt_setbuf) MARK_VISIBLE (gpgrt_set_binary) MARK_VISIBLE (gpgrt_set_nonblock) MARK_VISIBLE (gpgrt_get_nonblock) MARK_VISIBLE (gpgrt_poll) MARK_VISIBLE (gpgrt_tmpfile) MARK_VISIBLE (gpgrt_opaque_set) MARK_VISIBLE (gpgrt_opaque_get) MARK_VISIBLE (gpgrt_fname_set) MARK_VISIBLE (gpgrt_fname_get) MARK_VISIBLE (gpgrt_asprintf) MARK_VISIBLE (gpgrt_vasprintf) MARK_VISIBLE (gpgrt_bsprintf) MARK_VISIBLE (gpgrt_vbsprintf) MARK_VISIBLE (gpgrt_snprintf) MARK_VISIBLE (gpgrt_vsnprintf) MARK_VISIBLE (gpgrt_set_syscall_clamp) MARK_VISIBLE (gpgrt_get_syscall_clamp) MARK_VISIBLE (gpgrt_set_alloc_func) MARK_VISIBLE (gpgrt_realloc) MARK_VISIBLE (gpgrt_malloc) MARK_VISIBLE (gpgrt_calloc) MARK_VISIBLE (gpgrt_strdup) MARK_VISIBLE (gpgrt_strconcat) MARK_VISIBLE (gpgrt_free) MARK_VISIBLE (gpgrt_getenv) MARK_VISIBLE (gpgrt_setenv) MARK_VISIBLE (gpgrt_mkdir) MARK_VISIBLE (gpgrt_chdir) MARK_VISIBLE (gpgrt_getcwd) MARK_VISIBLE (gpgrt_b64dec_start) MARK_VISIBLE (gpgrt_b64dec_proc) MARK_VISIBLE (gpgrt_b64dec_finish) MARK_VISIBLE (gpgrt_b64enc_start) MARK_VISIBLE (gpgrt_b64enc_write) MARK_VISIBLE (gpgrt_b64enc_finish) MARK_VISIBLE (gpgrt_get_errorcount) MARK_VISIBLE (gpgrt_inc_errorcount) MARK_VISIBLE (gpgrt_log_set_sink) MARK_VISIBLE (gpgrt_log_set_socket_dir_cb) MARK_VISIBLE (gpgrt_log_set_pid_suffix_cb) MARK_VISIBLE (gpgrt_log_set_prefix) MARK_VISIBLE (gpgrt_log_get_prefix) MARK_VISIBLE (gpgrt_log_test_fd) MARK_VISIBLE (gpgrt_log_get_fd) MARK_VISIBLE (gpgrt_log_get_stream) MARK_VISIBLE (gpgrt_log) MARK_VISIBLE (gpgrt_logv) MARK_VISIBLE (gpgrt_logv_prefix) MARK_VISIBLE (gpgrt_log_string) MARK_VISIBLE (gpgrt_log_bug) MARK_VISIBLE (gpgrt_log_fatal) MARK_VISIBLE (gpgrt_log_error) MARK_VISIBLE (gpgrt_log_info) MARK_VISIBLE (gpgrt_log_debug) MARK_VISIBLE (gpgrt_log_debug_string) MARK_VISIBLE (gpgrt_log_printf) MARK_VISIBLE (gpgrt_log_printhex) MARK_VISIBLE (gpgrt_log_clock) MARK_VISIBLE (gpgrt_log_flush) MARK_VISIBLE (_gpgrt_log_assert) #if 0 MARK_VISIBLE (gpgrt_make_pipe) MARK_VISIBLE (gpgrt_spawn_process) MARK_VISIBLE (gpgrt_spawn_process_fd) MARK_VISIBLE (gpgrt_spawn_process_detached) MARK_VISIBLE (gpgrt_wait_process) MARK_VISIBLE (gpgrt_wait_processes) MARK_VISIBLE (gpgrt_kill_process) MARK_VISIBLE (gpgrt_release_process) #endif MARK_VISIBLE (gpgrt_argparse) MARK_VISIBLE (gpgrt_usage) MARK_VISIBLE (gpgrt_strusage) MARK_VISIBLE (gpgrt_set_strusage) MARK_VISIBLE (gpgrt_set_fixed_string_mapper); MARK_VISIBLE (gpgrt_set_usage_outfnc); MARK_VISIBLE (gpgrt_cmp_version); #undef MARK_VISIBLE #else /*!_GPGRT_INCL_BY_VISIBILITY_C*/ /* To avoid accidental use of the public functions inside Libgpg-error, we redefine them to catch such errors. */ #define gpg_strerror _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_strerror_r _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_strsource _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_code_from_errno _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_code_to_errno _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_code_from_syserror _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_set_errno _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_init _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_err_deinit _gpgrt_USE_UNDERSCORED_FUNCTION #define gpg_error_check_version _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_check_version _gpgrt_USE_OTHER_FUNCTION #define gpgrt_lock_init _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_lock_lock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_lock_unlock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_lock_destroy _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_yield _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_lock_trylock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_mopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fopenmem _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fopenmem_init _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fdopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fdopen_nc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_sysopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_sysopen_nc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fpopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fpopen_nc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_freopen _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fopencookie _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fclose _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fclose_snatch _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_onclose _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fileno _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fileno_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_syshd _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_syshd_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_set_std_fd _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_get_std_stream _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_flockfile _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_ftrylockfile _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_funlockfile _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_pending _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_pending_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_feof _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_feof_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_ferror _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_ferror_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_clearerr _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_clearerr_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fflush _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fseek _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fseeko _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_ftell _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_ftello _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_rewind _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_ftruncate _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fgetc _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_getc_underflow _gpgrt_USE_DBLUNDERSCO_FUNCTION #define gpgrt_fputc _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_putc_overflow _gpgrt_USE_DBLUNDERSCO_FUNCTION #define gpgrt_ungetc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_read _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_write _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_write_sanitized _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_write_hexstring _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fread _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fwrite _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fgets _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fputs _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fputs_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_getline _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_read_line _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fprintf_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_printf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_printf_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vfprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vfprintf_unlocked _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_setvbuf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_setbuf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_binary _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_nonblock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_get_nonblock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_poll _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_tmpfile _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_opaque_set _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_opaque_get _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fname_set _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_fname_get _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_asprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vasprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_bsprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vbsprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_snprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_vsnprintf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_realloc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_malloc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_calloc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_strdup _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_strconcat _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_free _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_getenv _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_setenv _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_mkdir _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_chdir _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_getcwd _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_syscall_clamp _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_get_syscall_clamp _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_alloc_func _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_b64enc_start _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_b64enc_write _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_b64enc_finish _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_b64dec_start _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_b64dec_proc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_b64dec_finish _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_get_errorcount _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_inc_errorcount _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_set_sink _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_set_socket_dir_cb _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_set_pid_suffix_cb _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_set_prefix _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_get_prefix _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_test_fd _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_get_fd _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_get_stream _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_logv _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_logv_prefix _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_string _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_bug _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_fatal _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_error _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_info _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_debug _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_debug_string _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_printf _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_printhex _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_clock _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_log_flush _gpgrt_USE_UNDERSCORED_FUNCTION #define _gpgrt_log_assert _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_make_pipe _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_spawn_process _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_spawn_process_fd _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_spawn_process_detached _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_wait_process _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_wait_processes _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_kill_process _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_release_process _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_argparse _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_usage _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_strusage _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_strusage _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_usage_outfnc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_set_fixed_string_mapper _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_cmp_version _gpgrt_USE_UNDERSCORED_FUNCTION /* Windows specific functions. */ #define gpgrt_w32_reg_query_string _gpgrt_USE_UNDERSCORED_FUNCTION #endif /*!_GPGRT_INCL_BY_VISIBILITY_C*/ #endif /*_GPGRT_VISIBILITY_H*/ diff --git a/tests/t-version.c b/tests/t-version.c index 240d4b4..d71f360 100644 --- a/tests/t-version.c +++ b/tests/t-version.c @@ -1,177 +1,178 @@ /* t-version.c - Check the version info function * Copyright (C) 2013 g10 Code GmbH * * This file is part of libgpg-error. * * libgpg-error is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * libgpg-error is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "t-version" #include "t-common.h" static const char *logpfx = PGM; static void t_gpgrt_cmp_version (void) { struct { int result; int level; const char *a; const char *b; } t[] = { { 0, 1, "0", "0" }, { -1, 1, "0", "1" }, { 1, 1, "1", "0" }, { -1, 1, "0.0", "0.1" }, { -1, 1, "0.1", "1.2" }, { 1, 1, "1.0", "0.9" }, { -1, 1, "-1.0", "0.9" }, /* A is invalid */ { 0, 1, "0rc0", "0rc0" }, { 1, 1, "0rc1", "0rc0" }, { -1, 1, "0rc1", "0rc2" }, { 0, 1, "0.rc0", "0.rc0" }, { 1, 1, "0.rc1", "0.rc0" }, { -1, 1, "0.rc1", "0.rc2" }, { 0, 1, "0.rc1", "0.rc1" }, { -1, 1, "0qc1", "0rc0" }, { -1, 1, "0.qc1", "0.rc0" }, { 0, 2, "0.0", "0.0" }, { -1, 2, "0.1", "0.2" }, { -1, 2, "3.1", "3.2" }, { -1, 2, "3.1", "4.0" }, { 0, 2, "1.1rc0", "1.1rc0" }, { 1, 2, "1.1rc1", "1.1rc0" }, { -1, 2, "1.1rc0", "1.1rc1" }, { 0, 3, "7.0.0", "7.0.0" }, { -1, 3, "7.0.1", "7.0.2" }, { -1, 3, "7.3.1", "7.3.2" }, { -1, 3, "7.3.1", "7.4.0" }, { 0, 3, "7.1.1rc0", "7.1.1rc0" }, { 1, 3, "7.1.1rc1", "7.1.1rc0" }, { -1, 3, "7.1.1rc0", "7.1.1rc1" }, { 1, 3, "6.0.0", "5.0.0" }, { 0, 3, "6.0.0", "6.0.0" }, { 1, 3, "6.0.1", "6.0.0" }, { 1, 3, "6.1.0", "6.0.0" }, { 1, 3, "6.2.1", "6.2.0" }, { -1, 3, "6.2.1", "6.2.2" }, { -1, 3, "6.0.0", "6.0.2" }, { -1, 3, "6.0.0", "6.1.0" }, { -1, 3, "6.2.0", "6.2.1" }, { 1, 3, "6.0.0-beta1", "6.0.0-beta0" }, { 0, 3, "6.0.0-beta2", "6.0.0-beta2" }, { 1, 3, "6.0.0-beta20", "6.0.0-beta19" }, { -1, 3, "6.0.0-beta1", "6.0.0-beta2" }, { 1, 3, "6.0.0-beta2", "6.0.0-beta1" }, { -1, 3, "6.0.0-beta20", "6.0.0-beta21" }, { 0,13, "6.0.0-beta1", "6.0.0-beta0" }, { 0,13, "6.0.0-beta2", "6.0.0-beta2" }, { 0,13, "6.0.0-beta20", "6.0.0-beta19" }, { 0,13, "6.0.0-beta1", "6.0.0-beta2" }, { 0,13, "6.0.0-beta2", "6.0.0-beta1" }, { 0,13, "6.0.0-beta20", "6.0.0-beta21" } }; int i; int result, expected; for (i=0; i < DIM (t); i++) { expected = t[i].result; result = gpgrt_cmp_version (t[i].a, t[i].b, t[i].level); if (result != expected) fail ("test %d failed: cmp('%s','%s',%d) = %d expected %d", i, t[i].a, t[i].b, t[i].level, result, expected); } for (i=0; i < DIM (t); i++) { expected = 0 - t[i].result; result = gpgrt_cmp_version (t[i].a, t[i].b, -t[i].level); if (result != expected) fail ("test %d-rev failed: cmp('%s','%s',%d) = %d expected %d", i, t[i].a, t[i].b, -t[i].level, result, expected); } } int main (int argc, char **argv) { int last_argc = -1; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--help")) { puts ( "usage: ./version [options]\n" "\n" "Options:\n" " --verbose Show what is going on\n" ); exit (0); } if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--debug")) { verbose = debug = 1; argc--; argv++; } } t_gpgrt_cmp_version (); if (!gpg_error_check_version (GPG_ERROR_VERSION)) { fprintf (stderr, "%s: gpg_error_check_version returned an error\n", logpfx); errorcount++; } if (!gpg_error_check_version ("1.10")) { fprintf (stderr, "%s: gpg_error_check_version returned an " "error for an old version\n", logpfx); errorcount++; } if (gpg_error_check_version ("15.0")) { fprintf (stderr, "%s: gpg_error_check_version did not return an error" " for a newer version\n", logpfx); errorcount++; + show ("\n"); /* Reference this function to silence gcc. */ } if (verbose || errorcount) { printf ("Version from header: %s (0x%06x)\n", GPG_ERROR_VERSION, GPG_ERROR_VERSION_NUMBER); printf ("Version from binary: %s\n", gpg_error_check_version (NULL)); printf ("Copyright blurb ...:%s\n", gpg_error_check_version ("\x01\x01")); } return errorcount ? 1 : 0; }