diff --git a/NEWS b/NEWS index 20026db..dcd316a 100644 --- a/NEWS +++ b/NEWS @@ -1,1062 +1,1065 @@ Noteworthy changes in version 1.39 (unreleased) [C29/A29/R1] ----------------------------------------------- + * On Windows gpgrt_fopen can now handle UTF-8 names. + + * On Windows gpgrt_chdir and gpgrt_mkdir can now handle UTF-8 names. * Interface changes relative to the 1.38 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_fcancel NEW. Noteworthy changes in version 1.38 (2020-05-29) [C29/A29/R0] ----------------------------------------------- - * New option parser features to implement system wide configuration - files. + * New option parser with features to implement system wide + configuration files. * New functions to build file names. * New function to help reallocating arrays. * Protect gpgrt_inc_errorcount against counter overflow. * Improve cross-building for new platforms. [#4774] * Support 64-bit big-endian MIPS architecture. [#4952] * Support static link for Windows with -lws2_32. [#4623] * Interface changes relative to the 1.37 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_fnameconcat NEW. gpgrt_absfnameconcat NEW. gpgrt_reallocarray NEW. gpgrt_set_confdir NEW. gpgrt_argparser NEW. ARGPARSE_FLAG_SYS NEW. ARGPARSE_FLAG_USER NEW. ARGPARSE_FLAG_VERBOSE NEW. ARGPARSE_FLAG_USERVERS NEW. ARGPARSE_FLAG_WITHATTR NEW. ARGPARSE_NO_CONFFILE NEW. ARGPARSE_CONFFILE NEW. ARGPARSE_OPT_CONFFILE NEW. ARGPARSE_ATTR_FORCE NEW. ARGPARSE_ATTR_IGNORE NEW. ARGPARSE_TYPE_MASK NEW. ARGPARSE_PERMISSION_ERROR NEW. ARGPARSE_INVALID_META NEW. ARGPARSE_UNKNOWN_META NEW. ARGPARSE_UNEXPECTED_META NEW. ARGPARSE_conffile NEW. ARGPARSE_noconffile NEW. ARGPARSE_verbatim NEW. ARGPARSE_header NEW. GPGRT_CONFDIR_USER NEW. GPGRT_CONFDIR_SYS NEW. Release-info: https://dev.gnupg.org/T4859 Noteworthy changes in version 1.37 (2020-02-07) [C28/A28/R0] ----------------------------------------------- * Fixes a build problems when using Gawk 5.0 [#4459] * Fixes Bourne shell incompatibilities on Solaris. [#4574] * Improves cross-comiling support. [#4643] * On Windows strerror_s is now used to emulate strerror_r. [#4539] * New error codes to map SQLite primary error codes. * Now uses poll(2) instead of select(2) in gpgrt_poll if possible. * Fixes a bug in gpgrt_close. [#4698] * Fixes build problem under Cygwin. [#4474] * Fixes a few minor portability bugs. * Interface changes relative to the 1.36 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NO_KEYBOXD NEW. GPG_ERR_KEYBOXD NEW. GPG_ERR_NO_SERVICE NEW. GPG_ERR_SERVICE. NEW. GPG_ERR_SQL_OK NEW. GPG_ERR_SQL_ERROR NEW. GPG_ERR_SQL_INTERNAL NEW. GPG_ERR_SQL_PERM NEW. GPG_ERR_SQL_ABORT NEW. GPG_ERR_SQL_BUSY NEW. GPG_ERR_SQL_LOCKED NEW. GPG_ERR_SQL_NOMEM NEW. GPG_ERR_SQL_READONLY NEW. GPG_ERR_SQL_INTERRUPT NEW. GPG_ERR_SQL_IOERR NEW. GPG_ERR_SQL_CORRUPT NEW. GPG_ERR_SQL_NOTFOUND NEW. GPG_ERR_SQL_FULL NEW. GPG_ERR_SQL_CANTOPEN NEW. GPG_ERR_SQL_PROTOCOL NEW. GPG_ERR_SQL_EMPTY NEW. GPG_ERR_SQL_SCHEMA NEW. GPG_ERR_SQL_TOOBIG NEW. GPG_ERR_SQL_CONSTRAINT NEW. GPG_ERR_SQL_MISMATCH NEW. GPG_ERR_SQL_MISUSE NEW. GPG_ERR_SQL_NOLFS NEW. GPG_ERR_SQL_AUTH NEW. GPG_ERR_SQL_FORMAT NEW. GPG_ERR_SQL_RANGE NEW. GPG_ERR_SQL_NOTADB NEW. GPG_ERR_SQL_NOTICE NEW. GPG_ERR_SQL_WARNING NEW. GPG_ERR_SQL_ROW NEW. GPG_ERR_SQL_DONE NEW. Release-info: https://dev.gnupg.org/T4772 Noteworthy changes in version 1.36 (2019-03-19) [C27/A27/R0] ----------------------------------------------- * Two new error codes to better support PIV cards. * Support armv7a-unknown-linux-gnueabihf. * Increased estream buffer sizes for Windows. * Interface changes relative to the 1.34 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NO_AUTH NEW. GPG_ERR_BAD_AUTH NEW. Release-info: https://dev.gnupg.org/T4413 Noteworthy changes in version 1.35 (2019-01-28) [C26/A26/R1] ----------------------------------------------- * Distribute the correct gpgrt-config. Noteworthy changes in version 1.34 (2019-01-16) [C26/A26/R0] ----------------------------------------------- * Support for riscv32. * New API to allow emergency cleanup after internal fatal errors. * Minor bug and portability fixes. [#4286,#4298 * Interface changes relative to the 1.33 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_abort NEW. gpgrt_add_emergency_cleanup NEW. Release-info: https://dev.gnupg.org/T4296 Noteworthy changes in version 1.33 (2018-12-07) [C25/A25/R0] ----------------------------------------------- * New unified config script gpgrt-config which can now be used by all GnuPG related packages. * Support for ARC and arm64ilp32. * The log functions now sanitize strings printed with the "%s" format specifier. All control characters are C-escaped in the output. Users of that function may want to remove their own escaping to avoid doubling of backslashes. * New fprintf style function to apply a custom filter for string arguments. * New function to compare version strings. * Interface changes relative to the 1.28 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_cmp_version New. gpgrt_string_filter_t New. gpgrt_fprintf_sf New. gpgrt_fprintf_sf_unlocked New. gpgrt_ftruncate New but limited functionality. gpgrt_w32_override_locale New. Release-info: https://dev.gnupg.org/T4205 Noteworthy changes in version 1.32 (2018-07-12) [C24/A24/R3] ----------------------------------------------- * Fixes a problem with gpgrt_fflush and gpgrt_fopencookie. [#4069] * Fixes a problem with the C11 header stdnoreturn.h. [#4002] * The yat2m tool can now also be build on Windows. * Updates translations for Spanish, Russian and Ukrainian. Noteworthy changes in version 1.31 (2018-05-02) [C24/A24/R2] ----------------------------------------------- * Fixes another problem with gpgrt_poll under Windows. [#3937] * New translation for Spanish. Noteworthy changes in version 1.30 (2018-04-30) [C24/A24/R1] ----------------------------------------------- * Fixes a hang on Windows when using gpgrt_poll under nPth. * Build fix for Solaris. [#3869] Noteworthy changes in version 1.29 (2018-04-11) [C24/A24/R0] ----------------------------------------------- * The yat2m tool is during cross-compile now also installed on the host platform. * New option parser and associated functions similar to the one used by GnuPG. * New Base-64 encoder. * Fixes regression in 1.28 for arm64 and w64 builds. * Interface changes relative to the 1.28 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_argparse New. gpgrt_usage New. gpgrt_strusage New. gpgrt_set_strusage New. gpgrt_set_usage_outfnc New. gpgrt_set_fixed_string_mapper New. GPGRT_ENABLE_ARGPARSE_MACROS New macro. gpgrt_b64enc_start New. gpgrt_b64enc_write New. gpgrt_b64enc_finish New. Noteworthy changes in version 1.28 (2018-03-13) [C23/A23/R0] ----------------------------------------------- * The formerly internal yat2m tool is now installed for a native build. * The new files gpgrt.m4 and gpgrt-config are now installed. They can be used instead of gpg-error.m4 and gpg-error-config. * New logging functions similar to those used by GnuPG. * New helper functions for platform abstraction. * Interface changes relative to the 1.27 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_get_errorcount New API. gpgrt_inc_errorcount New API. gpgrt_log_set_sink New API. gpgrt_log_set_socket_dir_cb New API. gpgrt_log_set_pid_suffix_cb New API. gpgrt_log_set_prefix New API. gpgrt_log_get_prefix New API. gpgrt_log_test_fd New API. gpgrt_log_get_fd New API. gpgrt_log_get_stream New API. gpgrt_log New API. gpgrt_logv New API. gpgrt_logv_prefix New API. gpgrt_log_string New API. gpgrt_log_info New API. gpgrt_log_error New API. gpgrt_log_fatal New API. gpgrt_log_bug New API. gpgrt_log_debug New API. gpgrt_log_debug_string New API. gpgrt_log_printf New API. gpgrt_log_flush New API. gpgrt_log_printhex New API. gpgrt_log_clock New API. gpgrt_assert New macro. _gpgrt_log_assert New internal API. GPGRT_LOGLVL_BEGIN New const. GPGRT_LOGLVL_CONT New const. GPGRT_LOGLVL_INFO New const. GPGRT_LOGLVL_WARN New const. GPGRT_LOGLVL_ERROR New const. GPGRT_LOGLVL_FATAL New const. GPGRT_LOGLVL_BUG New const. GPGRT_LOGLVL_DEBUG New const. gpgrt_realloc New API. gpgrt_malloc New API. gpgrt_calloc New API. gpgrt_strdup New API. gpgrt_strconcat New API. gpgrt_w32_reg_query_string New API. gpgrt_getenv New API. gpgrt_setenv New API. gpgrt_mkdir New API. gpgrt_chdir New API. gpgrt_getcwd New API. Noteworthy changes in version 1.27 (2017-02-28) [C22/A22/R0] ----------------------------------------------- * Added a Base64 decoder. * Added support for the sh3 architecture. * Added header gpgrt.h as an alias for gpg-error.h. * Fixed macro GPGRT_GCC_VERSION. * Fixed a race in non-blocking I/O on Windows. * Interface changes relative to the 1.26 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_b64state_t NEW type. gpgrt_b64dec_start NEW. gpgrt_b64dec_proc NEW. gpgrt_b64dec_finish NEW. GPG_ERR_WRONG_NAME NEW. gpgrt.h NEW header. Noteworthy changes in version 1.26 (2016-12-21) [C21/A21/R0] ----------------------------------------------- * New option --desc for gpg-error. * Interface changes relative to the 1.25 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_UNKNOWN_FLAG NEW. GPG_ERR_INV_ORDER NEW. GPG_ERR_ALREADY_FETCHED NEW. GPG_ERR_TRY_LATER NEW. GPG_ERR_SYSTEM_BUG NEW. GPG_ERR_DNS_UNKNOWN NEW. GPG_ERR_DNS_SECTION NEW. GPG_ERR_DNS_ADDRESS NEW. GPG_ERR_DNS_NO_QUERY NEW. GPG_ERR_DNS_NO_ANSWER NEW. GPG_ERR_DNS_CLOSED NEW. GPG_ERR_DNS_VERIFY NEW. GPG_ERR_DNS_TIMEOUT NEW. Noteworthy changes in version 1.25 (2016-11-14) [C20/A20/R0] ----------------------------------------------- * New interface gpgrt_get_syscall_clamp to allow libaries to make use of Libgpg-error's system call wrapper functions. * gpgrt_poll does now work under Windows. * Fixed bug in the locking code when used with the nPth threading library. * Added support for {i686,x86_64}-apple-darwin. * Added new error codes. * Interface changes relative to the 1.23 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_get_syscall_clamp NEW. GPG_ERR_ENGINE_TOO_OLD NEW. GPG_ERR_WINDOW_TOO_SMALL NEW. GPG_ERR_WINDOW_TOO_LARGE NEW. GPG_ERR_MISSING_ENVVAR NEW. GPG_ERR_USER_ID_EXISTS NEW. GPG_ERR_NAME_EXISTS NEW. GPG_ERR_DUP_NAME NEW. GPG_ERR_TOO_OLD NEW. GPG_ERR_TOO_YOUNG NEW. Noteworthy changes in version 1.24 (2016-07-14) [C19/A19/R1] ----------------------------------------------- * Fixes a bug in es_fclose_snatch when used used after es_fseek. * Fixes building without thread support. * New configure option --disable-tests. Noteworthy changes in version 1.23 (2016-06-15) [C19/A19/R0] ----------------------------------------------- * Fixes an assertion failure due to es_flush on read/write streams. * Fixes a bug with a too short memory limit is es_fopenmen. * Cross-build support for powerpc-unknown-linux-gnuspe and tilegx-unknown-linux-gnu architectures. * Interface changes relative to the 1.22 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_SUBKEYS_EXP_OR_REV NEW. Noteworthy changes in version 1.22 (2016-04-25) [C18/A18/R0] ----------------------------------------------- * New functions and macros to to provide iconv(3) on Windows. * Support for LeakSanitizer with the gpgrt_annotate_leaked_object inline function. * Interface changes relative to the 1.21 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_DB_CORRUPTED NEW. gpgrt_annotate_leaked_object NEW inline func. GPGRT_ENABLE_W32_ICONV_MACROS NEW. gpgrt_w32_iconv_open NEW. gpgrt_w32_iconv_close NEW. gpgrt_w32_iconv NEW. Noteworthy changes in version 1.21 (2015-12-12) [C17/A17/R0] ----------------------------------------------- * New functions gpgrt_poll and gpgrt_set_nonblock. For now only pipes and sockets on Unix are supported. * Fixes gettext output encoding problems on Windows. * Interface changes relative to the 1.20 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_set_nonblock NEW. gpgrt_get_nonblock NEW. gpgrt_poll NEW. gpgrt_poll_t NEW type. es_poll_t NEW type. es_set_nonblock NEW macro. es_get_nonblock NEW macro. es_poll NEW macro. GPG_ERR_TRUE NEW. GPG_ERR_FALSE NEW. GPG_ERR_NO_NAME NEW. GPG_ERR_NO_KEY NEW. GPG_ERR_SERVER_FAILED NEW. Noteworthy changes in version 1.20 (2015-08-26) [C16/A16/R0] ----------------------------------------------- * New macros for GCC attributes. * Make es_set_binary actually work for Windows. * Allow building without thread support. * Build without a build timestamp by default. * Interface changes relative to the 1.19 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPGRT_VERSION NEW macro. GPGRT_VERSION_NUMBER NEW macro. GPGRT_INLINE NEW macro. GPGRT_GCC_VERSION NEW macro. GPGRT_ATTR_NORETURN NEW macro. GPGRT_ATTR_PRINTF NEW macro. GPGRT_ATTR_NR_PRINTF NEW macro. GPGRT_ATTR_FORMAT_ARG NEW macro. GPGRT_ATTR_SENTINEL NEW macro. GPGRT_ATTR_USED NEW macro. GPGRT_ATTR_UNUSED NEW macro. GPGRT_ATTR_DEPRECATED NEW macro. GPGRT_ATTR_PURE NEW macro. GPGRT_ATTR_MALLOC NEW macro. GPGRT_HAVE_MACRO_FUNCTION NEW macro. GPGRT_HAVE_PRAGMA_GCC_PUSH NEW macro. Noteworthy changes in version 1.19 (2015-04-10) [C15/A15/R0] ----------------------------------------------- * New set of error codes for use with LDAP. * New options --help and --defines for gpg-error. * Allow building with gcc 5. * Interface changes relative to the 1.18 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_LDAP_* NEW. Noteworthy changes in version 1.18 (2015-01-26) [C14/A14/R0] ----------------------------------------------- * New translations for Hungarian, Portuguese, Russian, and traditional Chinese. Updated other translations. * New error codes. * Interface changes relative to the 1.17 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_FORBIDDEN NEW. GPG_ERR_OBJ_TERM_STATE NEW. GPG_ERR_REQUEST_TOO_SHORT NEW. GPG_ERR_REQUEST_TOO_LONG NEW. GPG_ERR_LEGACY_KEY NEW. Noteworthy changes in version 1.17 (2014-10-15) [C13/A13/R0] ----------------------------------------------- * New error codes for TLS protocol libraries. * New configure option --enable-build-timestamp. * New man page for gpg-error-config. * Interface changes relative to the 1.16 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_SOURCE_TLS NEW. GPG_ERR_NO_CERT_CHAIN NEW. GPG_ERR_CERT_TOO_LARGE NEW. GPG_ERR_INV_RECORD NEW. GPG_ERR_BAD_MAC NEW. GPG_ERR_UNEXPECTED_MSG NEW. GPG_ERR_COMPR_FAILED NEW. GPG_ERR_WOULD_WRAP NEW. GPG_ERR_FATAL_ALERT NEW. GPG_ERR_NO_CIPHER NEW. GPG_ERR_MISSING_CLIENT_CERT NEW. GPG_ERR_CLOSE_NOTIFY NEW. GPG_ERR_TICKET_EXPIRED NEW. GPG_ERR_BAD_TICKET NEW. GPG_ERR_UNKNOWN_IDENTITY NEW. GPG_ERR_BAD_HS_CERT NEW. GPG_ERR_BAD_HS_CERT_REQ NEW. GPG_ERR_BAD_HS_CERT_VER NEW. GPG_ERR_BAD_HS_CHANGE_CIPHER NEW. GPG_ERR_BAD_HS_CLIENT_HELLO NEW. GPG_ERR_BAD_HS_SERVER_HELLO NEW. GPG_ERR_BAD_HS_SERVER_HELLO_DONE NEW. GPG_ERR_BAD_HS_FINISHED NEW. GPG_ERR_BAD_HS_SERVER_KEX NEW. GPG_ERR_BAD_HS_CLIENT_KEX NEW. GPG_ERR_BOGUS_STRING NEW. gpgrt_pending NEW. gpgrt_pending_unlocked NEW. Noteworthy changes in version 1.16 (2014-09-18) [C12/A12/R2] ----------------------------------------------- * Support building for iOS. * Fixed a prototype mismatch. * Fix es_fclose for streams opened with "samethread". Noteworthy changes in version 1.15 (2014-09-11) [C12/A12/R1] ----------------------------------------------- * This releases fixes problems with the use of off_t and ssize_t by the estream functions introduced with 1.14. Although this is technically an ABI break on some platforms, we take this as a simple bug fix for 1.14. The new functions are very unlikely in use by any code and thus no breakage should happen. The 1.14 tarball will be removed from the archive. * Add type gpgrt_off_t which is guaranteed to be 64 bit. * Add type gpgrt_ssize_t to make use on Windows easier. On Unix platforms this is an alias for ssize_t. Noteworthy changes in version 1.14 (2014-09-08) [C12/A12/R0] ----------------------------------------------- * Added gpgrt_lock_trylock. * Added the estream library under the name gpgrt and a set of macros to use them with their "es_" names. * Interface changes relative to the 1.13 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_KEY_DISABLED NEW. gpgrt_init NEW macro. gpgrt_check_version NEW. gpgrt_lock_trylock NEW. gpgrt_set_syscall_clamp NEW. gpgrt_set_alloc_func NEW. gpgrt_stream_t NEW. gpgrt_cookie_io_functions_t NEW. gpgrt_syshd_t NEW. GPGRT_SYSHD_NONE NEW. GPGRT_SYSHD_FD NEW. GPGRT_SYSHD_SOCK NEW. GPGRT_SYSHD_RVID NEW. GPGRT_SYSHD_HANDLE NEW. gpgrt_stdin NEW macro. gpgrt_stdout NEW macro. gpgrt_stderr NEW macro. gpgrt_fopen NEW. gpgrt_mopen NEW. gpgrt_fopenmem NEW. gpgrt_fopenmem_init NEW. gpgrt_fdopen NEW. gpgrt_fdopen_nc NEW. gpgrt_sysopen NEW. gpgrt_sysopen_nc NEW. gpgrt_fpopen NEW. gpgrt_fpopen_nc NEW. gpgrt_freopen NEW. gpgrt_fopencookie NEW. gpgrt_fclose NEW. gpgrt_fclose_snatch NEW. gpgrt_onclose NEW. gpgrt_fileno NEW. gpgrt_fileno_unlocked NEW. gpgrt_syshd NEW. gpgrt_syshd_unlocked NEW. gpgrt_flockfile NEW. gpgrt_ftrylockfile NEW. gpgrt_funlockfile NEW. gpgrt_feof NEW. gpgrt_feof_unlocked NEW. gpgrt_ferror NEW. gpgrt_ferror_unlocked NEW. gpgrt_clearerr NEW. gpgrt_clearerr_unlocked NEW. gpgrt_fflush NEW. gpgrt_fseek NEW. gpgrt_fseeko NEW. gpgrt_ftell NEW. gpgrt_ftello NEW. gpgrt_rewind NEW. gpgrt_getc NEW macro. gpgrt_getc_unlocked NEW macro. gpgrt_fgetc NEW. gpgrt_fputc NEW. gpgrt_ungetc NEW. gpgrt_read NEW. gpgrt_write NEW. gpgrt_write_sanitized NEW. gpgrt_write_hexstring NEW. gpgrt_fread NEW. gpgrt_fwrite NEW. gpgrt_fgets NEW. gpgrt_putc NEW macro. gpgrt_putc_unlocked NEW macro. gpgrt_fputs NEW. gpgrt_fputs_unlocked NEW. gpgrt_getline NEW. gpgrt_read_line NEW. gpgrt_free NEW. gpgrt_fprintf NEW. gpgrt_fprintf_unlocked NEW. gpgrt_printf NEW. gpgrt_printf_unlocked NEW. gpgrt_vfprintf NEW. gpgrt_vfprintf_unlocked NEW. gpgrt_setvbuf NEW. gpgrt_setbuf NEW. gpgrt_set_binary NEW. gpgrt_tmpfile NEW. gpgrt_opaque_set NEW. gpgrt_opaque_get NEW. gpgrt_fname_set NEW. gpgrt_fname_get NEW. gpgrt_asprintf NEW. gpgrt_vasprintf NEW. gpgrt_bsprintf NEW. gpgrt_vbsprintf NEW. gpgrt_snprintf NEW. gpgrt_vsnprintf NEW. Noteworthy changes in version 1.13 (2014-04-15) [C11/A11/R0] ----------------------------------------------- * Added a portable mutex API. * The AM_PATH_GPG_ERROR macro now defines GPG_ERROR_MT_CFLAGS and GPG_ERROR_MT_LIBS autoconf output variables for use by programs which need gpgrt based thread support. gpg-error-config has a new option --mt. * Interface changes relative to the 1.12 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_KEY_ON_CARD NEW. GPG_ERR_MAC_ALGO NEW. GPG_ERR_INV_LOCK_OBJ NEW. gpgrt_lock_t NEW. GPGRT_LOCK_INITIALIZER NEW. GPGRT_LOCK_DEFINE NEW. gpgrt_lock_init NEW. gpgrt_lock_lock NEW. gpgrt_lock_unlock NEW. gpgrt_lock_destroy NEW. gpgrt_yield NEW. Noteworthy changes in version 1.12 (2013-06-24) ----------------------------------------------- * Add support for 64 bit Windows (use ./autogen.sh --build-w64). * Fixed parsing and installing of the Windows .def file. * Interface changes relative to the 1.11 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NO_CRYPT_CTX NEW. GPG_ERR_WRONG_CRYPT_CTX NEW. GPG_ERR_BAD_CRYPT_CTX NEW. GPG_ERR_CRYPT_CTX_CONFLICT NEW. GPG_ERR_BROKEN_PUBKEY NEW. GPG_ERR_BROKEN_SECKEY NEW. Noteworthy changes in version 1.11 (2013-02-25) ----------------------------------------------- * New error source GPG_ERR_SOURCE_ASSUAN for Libassuan related errors. * New macros GPG_ERROR_VERSION and GPG_ERROR_VERSION_NUMBER. New function gpg_error_check_version. * Interface changes relative to the 1.10 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NO_KEYSERVER NEW. GPG_ERR_INV_CURVE NEW. GPG_ERR_UNKNOWN_CURVE NEW. GPG_ERR_DUP_KEY NEW. GPG_ERR_AMBIGUOUS NEW. GPG_ERR_SOURCE_ASSUAN NEW. gpg_error_check_version NEW. GPG_ERROR_VERSION NEW. GPG_ERROR_VERSION_NUMBER NEW. Noteworthy changes in version 1.10 (2010-10-26) ----------------------------------------------- * Using a static library on W32 does now work. * Interface changes relative to the 1.9 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NOT_INITIALIZED NEW. GPG_ERR_MISSING_ISSUER_CERT NEW. GPG_ERR_FULLY_CANCELED NEW. Noteworthy changes in version 1.9 (2010-07-21) ---------------------------------------------- * New function gpg_err_deinit. * Fix building of static lib under W32. * Interface changes relative to the 1.8 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_MISSING_KEY NEW. GPG_ERR_TOO_MANY NEW. GPG_ERR_LIMIT_REACHED NEW. gpg_err_deinit NEW. Noteworthy changes in version 1.8 (2010-05-06) ---------------------------------------------- * Support for WindowsCE. * New option --list for gpg-error. * Interface changes relative to the 1.7 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NOT_ENABLED NEW. GPG_ERR_SOURCE_G13 NEW. GPG_ERR_NO_ENGINE NEW. gpg_err_set_errno NEW. Noteworthy changes in version 1.7 (2008-11-26) ---------------------------------------------- * Minor fixes and a few new error codes. * Interface changes relative to the 1.6 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NOT_OPERATIONAL NEW GPG_ERR_NO_PASSPHRASE NEW GPG_ERR_NO_PIN NEW Noteworthy changes in version 1.6 (2007-10-29) ---------------------------------------------- * Fixed a build problem under Windows. * Interface changes relative to the 1.4 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_UNFINISHED NEW GPG_ERR_SOURCE_GPA NEW GPG_ERR_SOURCE_KLEO NEW Noteworthy changes in version 1.5 (2006-11-30) ---------------------------------------------- * Minor build system fixes. * Updated gettext. Removed included gettext copy. * gpg-error has a new option --version. Noteworthy changes in version 1.4 (2006-09-14) ---------------------------------------------- * Support for Common Lisp is included. * New error codes for the Assuan IPC library. * New error code GPG_ERR_MISSING_ERRNO to be used in cases when a system accidentally does not set errno but a system error definitely occurred. * New error source GPG_ERR_SOURCE_ANY to allow proper use of libgpg-error even if a specific source is not available. * New convenience functions gpg_err_code_from_syserror and gpg_error_from_syserror which make sure never to return 0. * Interface changes relative to the 1.2 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpg_err_code_from_syserror NEW gpg_error_from_syserror NEW GPG_ERR_SOURCE_ANY NEW GPG_ERR_MISSING_ERRNO NEW GPG_ERR_UNKNOWN_OPTION NEW GPG_ERR_UNKNOWN_COMMAND NEW GPG_ERR_ASS_GENERAL NEW GPG_ERR_ASS_ACCEPT_FAILED NEW GPG_ERR_ASS_CONNECT_FAILED NEW GPG_ERR_ASS_INV_RESPONSE NEW GPG_ERR_ASS_INV_VALUE NEW GPG_ERR_ASS_INCOMPLETE_LINE NEW GPG_ERR_ASS_LINE_TOO_LONG NEW GPG_ERR_ASS_NESTED_COMMANDS NEW GPG_ERR_ASS_NO_DATA_CB NEW GPG_ERR_ASS_NO_INQUIRE_CB NEW GPG_ERR_ASS_NOT_A_SERVER NEW GPG_ERR_ASS_NOT_A_CLIENT NEW GPG_ERR_ASS_SERVER_START NEW GPG_ERR_ASS_READ_ERROR NEW GPG_ERR_ASS_WRITE_ERROR NEW GPG_ERR_ASS_TOO_MUCH_DATA NEW GPG_ERR_ASS_UNEXPECTED_CMD NEW GPG_ERR_ASS_UNKNOWN_CMD NEW GPG_ERR_ASS_SYNTAX NEW GPG_ERR_ASS_CANCELED NEW GPG_ERR_ASS_NO_INPUT NEW GPG_ERR_ASS_NO_OUTPUT NEW GPG_ERR_ASS_PARAMETER NEW GPG_ERR_ASS_UNKNOWN_INQUIRE NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.3 (2006-03-14) ---------------------------------------------- * GNU gettext is included for systems that do not provide it. Noteworthy changes in version 1.2 (2006-03-03) ---------------------------------------------- * New function gpg_err_init, which binds the locale directory to the text domain. This function is a constructor on GCC targets, so it does not need to be called explicitely. The header file defines GPG_ERR_INITIALIZED in this case. This is experimental for now. * "./autogen.sh --build-w32" does now also build a DLL for W32. Translations are not yet provided for this platform. * New error codes GPG_ERR_UNKNOWN_EXTN and GPG_ERR_UNKNOWN_CRIT_EXTN. * New error code GPG_ERR_LOCKED. * New translations included for France, Romania, and Vietnamese. * Interface changes relative to the 1.1 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_UNKNOWN_EXTN NEW GPG_ERR_UNKNOWN_CRIT_EXTN NEW GPG_ERR_LOCKED NEW gpg_err_init NEW GPG_ERR_INITIALIZED NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.1 (2005-06-20) ---------------------------------------------- * Bug fixes. Noteworthy changes in version 1.0 (2004-07-30) ---------------------------------------------- * Ported to Solaris 2.8. * Added a new error source GPG_ERR_SOURCE_GSTI, and new error codes GPG_ERR_PROTOCOL_VIOLATION and GPG_ERR_INV_MAC for this source. * Interface changes relative to the 0.7 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_SOURCE_GSTI NEW GPG_ERR_PROTOCOL_VIOLATION NEW GPG_ERR_INV_MAC NEW GPG_ERR_INV_REQUEST NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.7 (2004-03-07) ---------------------------------------------- * libgpg-error can be built on systems where the errno macros do not evaluate to plain numbers, but expressions. If you want to cross-compile, you might have to set CC_FOR_BUILD, though. * A new tool gpg-error to convert error numbers into symbols into strings is provided. * Interface changes relative to the 0.6 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_LOCALE_PROBLEM NEW GPG_ERR_NOT_LOCKED NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.6 (2003-11-14) ---------------------------------------------- * German translation included. * It is now possible to use the inline functions even for non C99 compliant compilers by given e.g. -DGPG_ERR_INLINE=inline when compiling an application using this library. Note, that gcc will use inline anyway. * Interface changes relative to the 0.5 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_SOURCE_KSBA NEW GPG_ERR_SOURCE_DIRMNGR NEW GPG_ERR_TRUNCATED NEW GPG_ERR_NO_ENCODING_METHOD NEW GPG_ERR_NO_ENCRYPTION_SCHEME NEW GPG_ERR_NO_SIGNATURE_SCHEME NEW GPG_ERR_INV_ATTR NEW GPG_ERR_NO_VALUE NEW GPG_ERR_NOT_FOUND NEW GPG_ERR_VALUE_NOT_FOUND NEW GPG_ERR_SYNTAX NEW GPG_ERR_INV_CRL NEW GPG_ERR_BAD_BER NEW GPG_ERR_INV_BER NEW GPG_ERR_ELEMENT_NOT_FOUND NEW GPG_ERR_IDENTIFIER_NOT_FOUND NEW GPG_ERR_INV_TAG NEW GPG_ERR_INV_LENGTH NEW GPG_ERR_INV_KEYINFO NEW GPG_ERR_UNEXPECTED_TAG NEW GPG_ERR_NOT_DER_ENCODED, NEW GPG_ERR_NO_CMS_OBJ NEW GPG_ERR_INV_CMS_OBJ NEW GPG_ERR_UNKNOWN_CMS_OBJ, NEW GPG_ERR_UNSUPPORTED_CMS_OBJ NEW GPG_ERR_UNSUPPORTED_ENCODING, NEW GPG_ERR_UNSUPPORTED_CMS_VERSION NEW GPG_ERR_UNKNOWN_ALGORITHM NEW GPG_ERR_ENCODING_PROBLEM NEW GPG_ERR_INV_STATE NEW GPG_ERR_DUP_VALUE, NEW GPG_ERR_MISSING_ACTION NEW GPG_ERR_MODULE_NOT_FOUND NEW GPG_ERR_INV_OID_STRING NEW GPG_ERR_INV_TIME NEW GPG_ERR_INV_CRL_OBJ NEW GPG_ERR_UNSUPPORTED_CRL_VERSION NEW GPG_ERR_INV_CERT_OBJ NEW GPG_ERR_UNKNOWN_NAME NEW GPG_ERR_BUFFER_TOO_SHORT. NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.5 (2003-10-06) ---------------------------------------------- * New thread safe interface gpg_strerror_r. * New error code GPG_ERR_PIN_NOT_SYNCED has been added. * Interface changes relative to the 0.4 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpg_strerror_r NEW GPG_ERR_PIN_NOT_SYNCED NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.4 (2003-09-03) ---------------------------------------------- * Fixed another bug that prevented that system errors were created correctly in the first place. * Use inline in public header file only on C99 compilers. Noteworthy changes in version 0.3 (2003-07-31) ---------------------------------------------- * Fixed bug that prevented that system errors were mapped to error strings correctly. Noteworthy changes in version 0.2 (2003-07-30) ---------------------------------------------- * Value of the error code GPG_ERR_CANCELED was fixed. * New error codes GPG_ERR_WRONG_CARD, GPG_ERR_HARDWARE, GPG_ERR_PIN_BLOCKED and GPG_ERR_USE_CONDITIONS have been added. * The header file has been made C++ clean. * AM_PATH_GPG_ERR has been fixed to work without explicit version number. * The header file now uses inline instead __inline__ for non-GNU compilers. Noteworthy changes in version 0.1 (2003-06-06) ---------------------------------------------- * Initial release. Copyright 2003, 2004, 2005, 2010 g10 Code GmbH This file is free software; as a special exception the author gives unlimited permission to copy and/or distribute it, with or without modifications, as long as this notice is preserved. This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, to the extent permitted by law; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. diff --git a/src/Makefile.am b/src/Makefile.am index b2bffd2..fc3acc3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,367 +1,367 @@ # Makefile.am for libgpg-error. # Copyright (C) 2003, 2004, 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+ # # We distribute the generated sources err-sources.h and err-codes.h, # because they are needed to build the po directory, and they don't # depend on the configuration anyway. # if HAVE_W32CE_SYSTEM gpg_extra_headers = gpg-extra/errno.h extra_cppflags = -idirafter gpg-extra else gpg_extra_headers = extra_cppflags = endif localedir = $(datadir)/locale bin_PROGRAMS = gpg-error if HAVE_W32_SYSTEM noinst_PROGRAMS = gen-w32-lock-obj else noinst_PROGRAMS = gen-posix-lock-obj endif # Distributed lock object definitions for cross compilation. lock_obj_pub = \ syscfg/lock-obj-pub.aarch64-unknown-linux-gnu.h \ syscfg/lock-obj-pub.aarch64-unknown-linux-gnu_ilp32.h \ syscfg/lock-obj-pub.aarch64-apple-darwin.h \ syscfg/lock-obj-pub.alpha-unknown-linux-gnu.h \ syscfg/lock-obj-pub.arm-unknown-linux-androideabi.h \ syscfg/lock-obj-pub.arm-unknown-linux-gnueabi.h \ syscfg/lock-obj-pub.arm-apple-darwin.h \ syscfg/lock-obj-pub.hppa-unknown-linux-gnu.h \ syscfg/lock-obj-pub.i386-apple-darwin.h \ syscfg/lock-obj-pub.i686-unknown-gnu.h \ syscfg/lock-obj-pub.i686-unknown-kfreebsd-gnu.h \ syscfg/lock-obj-pub.i686-unknown-linux-gnu.h \ syscfg/lock-obj-pub.m68k-unknown-linux-gnu.h \ syscfg/lock-obj-pub.mips-unknown-linux-gnu.h \ syscfg/lock-obj-pub.mips64el-unknown-linux-gnuabi64.h \ syscfg/lock-obj-pub.mips64-unknown-linux-gnuabi64.h \ syscfg/lock-obj-pub.mipsel-unknown-linux-gnu.h \ syscfg/lock-obj-pub.nios2-unknown-linux-gnu.h \ syscfg/lock-obj-pub.or1k-unknown-linux-gnu.h \ syscfg/lock-obj-pub.powerpc-unknown-linux-gnu.h \ syscfg/lock-obj-pub.powerpc64-unknown-linux-gnu.h \ syscfg/lock-obj-pub.powerpc64le-unknown-linux-gnu.h \ syscfg/lock-obj-pub.powerpc-unknown-linux-gnuspe.h \ syscfg/lock-obj-pub.riscv64-unknown-linux-gnu.h \ syscfg/lock-obj-pub.riscv32-unknown-linux-gnu.h \ syscfg/lock-obj-pub.s390x-unknown-linux-gnu.h \ syscfg/lock-obj-pub.sh3-unknown-linux-gnu.h \ syscfg/lock-obj-pub.sh4-unknown-linux-gnu.h \ syscfg/lock-obj-pub.sparc-unknown-linux-gnu.h \ syscfg/lock-obj-pub.sparc64-unknown-linux-gnu.h \ syscfg/lock-obj-pub.x86_64-apple-darwin.h \ syscfg/lock-obj-pub.x86_64-unknown-kfreebsd-gnu.h \ syscfg/lock-obj-pub.x86_64-unknown-linux-gnu.h \ syscfg/lock-obj-pub.x86_64-unknown-linux-gnux32.h \ syscfg/lock-obj-pub.x86_64-unknown-linux-musl.h \ syscfg/lock-obj-pub.tilegx-unknown-linux-gnu.h \ syscfg/lock-obj-pub.ia64-unknown-linux-gnu.h \ syscfg/lock-obj-pub.mingw32.h lib_LTLIBRARIES = libgpg-error.la nodist_include_HEADERS = gpg-error.h gpgrt.h dist_bin_SCRIPTS = gpgrt-config bin_SCRIPTS = gpg-error-config m4datadir = $(datadir)/aclocal m4data_DATA = gpg-error.m4 gpgrt.m4 pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = gpg-error.pc EXTRA_DIST = mkstrtable.awk err-sources.h.in err-codes.h.in \ mkerrnos.awk errnos.in README \ mkerrcodes.awk mkerrcodes1.awk mkerrcodes2.awk mkerrcodes.c \ mkheader.c gpg-error.h.in mkw32errmap.c w32-add.h w32ce-add.h \ err-sources.h err-codes.h gpg-error-config.in gpg-error.m4 gpgrt.m4 \ gpg-error.vers gpg-error.def.in \ versioninfo.rc.in gpg-error.w32-manifest.in \ gpg-error-config-test.sh gpg-error.pc.in \ gen-lock-obj.sh $(lock_obj_pub) BUILT_SOURCES = $(srcdir)/err-sources.h $(srcdir)/err-codes.h \ code-to-errno.h code-from-errno.h \ err-sources-sym.h err-codes-sym.h errnos-sym.h gpg-error.h gpgrt.h \ gpg-error.def mkw32errmap.map.c tmp_files = _mkerrcodes.h _gpg-error.def.h mkw32errmap.tab.h mkw32errmap.map.c CLEANFILES = code-to-errno.h code-from-errno.h \ gpg-error.h gpgrt.h \ mkerrcodes$(EXEEXT_FOR_BUILD) mkerrcodes.h gpg-error.def mkw32errmap.tab.h \ mkw32errmap.map.c err-sources-sym.h err-codes-sym.h errnos-sym.h \ gpg-extra/errno.h mkheader$(EXEEXT_FOR_BUILD) \ gpg-error-config gpg-error-config-test.log \ $(tmp_files) lock-obj-pub.native.h MAINTAINERCLEANFILES = $(srcdir)/err-sources.h $(srcdir)/err-codes.h TESTS = gpg-error-config-test.sh # # {{{ Begin Windows part # if HAVE_W32_SYSTEM arch_sources = w32-gettext.c w32-lock.c w32-lock-obj.h w32-thread.c \ w32-iconv.c w32-estream.c w32-reg.c spawn-w32.c RCCOMPILE = $(RC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ -DLOCALEDIR=\"$(localedir)\" $(AM_CPPFLAGS) $(CPPFLAGS) LTRCCOMPILE = $(LIBTOOL) --mode=compile --tag=RC $(RCCOMPILE) SUFFIXES = .rc .lo .rc.lo: $(LTRCCOMPILE) -i "$<" -o "$@" gpg_error_res = versioninfo.lo export_symbols = -export-symbols gpg-error.def # i686-w64-mingw32.gcc version 4.9.1 takes the long long helper # functions from libgcc_s_sjlj-1.dll and not from a static libgcc. As # a plain C program we do not use exception handler and thus there is # no need to use this DLL. Thus we force gcc to link that statically. extra_ltoptions = -XCClinker -static-libgcc versioninfo.lo : gpg-error.w32-manifest install-def-file: gpg-error.def -$(INSTALL) -d $(DESTDIR)$(libdir) $(INSTALL) gpg-error.def $(DESTDIR)$(libdir)/gpg-error.def uninstall-def-file: -rm $(DESTDIR)$(libdir)/gpg-error.def libgpg_error_la_DEPENDENCIES = $(gpg_error_res) gpg-error.def intllibs = # # }}} End Windows part # else # # {{{ Begin Unix part # arch_sources = posix-lock.c posix-lock-obj.h posix-thread.c spawn-posix.c gpg_error_res = export_symbols = extra_ltoptions = install-def-file: uninstall-def-file: intllibs = @LTLIBINTL@ endif # # }}} End Unix part # socklibs = @GPG_ERROR_CONFIG_LIBS_PRIVATE@ if HAVE_LD_VERSION_SCRIPT libgpg_error_vers_opt = -Wl,--version-script=$(srcdir)/gpg-error.vers else libgpg_error_vers_opt = endif libgpg_error_la_LDFLAGS = \ -no-undefined $(export_symbols) $(libgpg_error_vers_opt) \ $(extra_ltoptions) -version-info \ @LIBGPG_ERROR_LT_CURRENT@:@LIBGPG_ERROR_LT_REVISION@:@LIBGPG_ERROR_LT_AGE@ libgpg_error_la_SOURCES = gettext.h $(arch_sources) \ - gpgrt-int.h init.c init.h version.c lock.h thread.h \ + gpgrt-int.h protos.h init.c init.h version.c lock.h thread.h \ estream.c estream-printf.c estream-printf.h \ strsource.c strerror.c code-to-errno.c code-from-errno.c \ visibility.c visibility.h \ sysutils.c \ stringutils.c \ syscall-clamp.c \ logging.c \ b64dec.c b64enc.c \ argparse.c nodist_libgpg_error_la_SOURCES = gpg-error.h # libgpg_error_la_DEPENDENCIES = \ # $(srcdir)/gpg-error.vers # Note that RCCOMPILE needs the same defines as ..._la_CPPFLAGS but # without the extra_cppflags because they may include am -idirafter # which is not supported by the RC compiler. libgpg_error_la_CPPFLAGS = -DLOCALEDIR=\"$(localedir)\" $(extra_cppflags) libgpg_error_la_LIBADD = $(gpg_error_res) $(intllibs) $(socklibs) $(LIBTHREAD) gpg_error_SOURCES = strsource-sym.c strerror-sym.c gpg-error.c gpg_error_CPPFLAGS = -DPKGDATADIR=\"$(pkgdatadir)\" \ -DLOCALEDIR=\"$(localedir)\" $(extra_cppflags) gpg_error_LDADD = libgpg-error.la $(LTLIBINTL) # We build err-sources.h and err-codes.h in the source directory. # This is needed because gettext does only look into the source # directory to find the files listed in po/POTFILE.in. To make these # rules work we also need to depend on Makefile.am and not on the # generated files Makefile.in or Makefile. $(srcdir)/err-sources.h: Makefile.am mkstrtable.awk err-sources.h.in $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=3 \ $(srcdir)/err-sources.h.in >$@ err-sources-sym.h: Makefile mkstrtable.awk err-sources.h.in $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=2 -v nogettext=1 \ $(srcdir)/err-sources.h.in >$@ $(srcdir)/err-codes.h: Makefile.am mkstrtable.awk err-codes.h.in $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=3 \ $(srcdir)/err-codes.h.in >$@ err-codes-sym.h: Makefile mkstrtable.awk err-codes.h.in $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=2 -v nogettext=1 \ $(srcdir)/err-codes.h.in >$@ code-to-errno.h: Makefile mkerrnos.awk errnos.in $(AWK) -f $(srcdir)/mkerrnos.awk $(srcdir)/errnos.in >$@ # It is correct to use $(CPP). We want the host's idea of the error codes. mkerrcodes.h: Makefile mkerrcodes.awk $(gpg_extra_headers) $(AWK) -f $(srcdir)/mkerrcodes1.awk $(srcdir)/errnos.in >_$@ $(CPP) $(CPPFLAGS) $(extra_cppflags) -P _$@ | grep GPG_ERR_ | \ $(AWK) -f $(srcdir)/mkerrcodes.awk >$@ -rm _$@ if HAVE_W32CE_SYSTEM # It is correct to use $(CPP). We want the host's idea of the error codes. mkw32errmap.tab.h: Makefile mkw32errmap.c $(CPP) -DRESOLVE_MACROS $(srcdir)/mkw32errmap.c | \ grep '{&mkw32errmap_marker' >$@ mkw32errmap.map.c: mkw32errmap$(EXEEXT_FOR_BUILD) ./mkw32errmap$(EXEEXT_FOR_BUILD) --map > $@ gpg-extra/errno.h: mkw32errmap$(EXEEXT_FOR_BUILD) -$(MKDIR_P) gpg-extra ./mkw32errmap$(EXEEXT_FOR_BUILD) > $@ else mkw32errmap.map.c: echo "/*dummy*/" > $@ endif # We use CC proper for preprocessing thus we have to convince it that # the data is really to be preprocessed. gpg-error.def: Makefile gpg-error.def.in cat $(srcdir)/gpg-error.def.in >_$@.h $(CPP) $(DEFAULT_INCLUDES) $(INCLUDES) $(extra_cppflags) _$@.h | \ grep -v '^#' >$@ -rm _$@.h # It is correct to use $(CC_FOR_BUILD) here. We want to run the # program at build time. mkerrcodes$(EXEEXT_FOR_BUILD): mkerrcodes.c mkerrcodes.h Makefile $(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) \ $(CPPFLAGS_FOR_BUILD) -I. -I$(srcdir) -o $@ $(srcdir)/mkerrcodes.c if HAVE_W32CE_SYSTEM # It is correct to use $(CC_FOR_BUILD) here. We want to run the # program at build time. mkw32errmap$(EXEEXT_FOR_BUILD): mkw32errmap.c mkw32errmap.tab.h Makefile $(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) \ $(CPPFLAGS_FOR_BUILD) -I. -I$(srcdir) -o $@ $(srcdir)/mkw32errmap.c endif code-from-errno.h: mkerrcodes$(EXEEXT_FOR_BUILD) Makefile ./mkerrcodes$(EXEEXT_FOR_BUILD) | $(AWK) -f $(srcdir)/mkerrcodes2.awk >$@ errnos-sym.h: Makefile mkstrtable.awk errnos.in $(AWK) -f $(srcdir)/mkstrtable.awk -v textidx=2 -v nogettext=1 \ -v prefix=GPG_ERR_ -v pkg_namespace=errnos_ \ $(srcdir)/errnos.in >$@ mkheader$(EXEEXT_FOR_BUILD): mkheader.c Makefile $(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) \ $(CPPFLAGS_FOR_BUILD) -g -I. -I$(srcdir) -o $@ $(srcdir)/mkheader.c parts_of_gpg_error_h = \ gpg-error.h.in \ err-sources.h.in \ err-codes.h.in \ errnos.in \ w32-add.h \ w32ce-add.h \ $(lock_obj_pub) # If we are cross-compiling or building on Windows we better make sure # that no stale native lock include file will be found by mkheader. if FORCE_USE_SYSCFG pre_mkheader_cmds = if test -f lock-obj-pub.native.h; \ then rm lock-obj-pub.native.h; fi mkheader_opts = --cross else if HAVE_GENERATED_LOCK_OBJ_H # lock-obj-pub.native.h is generated at configure time pre_mkheader_cmds = : mkheader_opts = parts_of_gpg_error_h += ./lock-obj-pub.native.h else pre_mkheader_cmds = : mkheader_opts = parts_of_gpg_error_h += ./lock-obj-pub.native.h ./lock-obj-pub.native.h: Makefile gen-posix-lock-obj$(EXEEXT) posix-lock-obj.h ./gen-posix-lock-obj >$@ endif endif # We also depend on versioninfo.rc because that is build by # config.status and thus has up-to-date version numbers. gpg-error.h: Makefile mkheader$(EXEEXT_FOR_BUILD) $(parts_of_gpg_error_h) \ versioninfo.rc ../config.h $(pre_mkheader_cmds) ./mkheader$(EXEEXT_FOR_BUILD) $(mkheader_opts) \ $(host_triplet) $(srcdir)/gpg-error.h.in \ ../config.h $(PACKAGE_VERSION) $(VERSION_NUMBER) >$@ gpgrt.h: gpg-error.h cp gpg-error.h gpgrt.h gpg-error-config: gpgrt-config gpg-error-config-old @echo $(ECHO_N) "Confirm gpg-error-config works... $(ECHO_C)" @if ./gpg-error-config-test.sh --old-new; then \ echo "good"; \ else \ echo "no"; \ echo "*** Please report to with gpg-error-config-test.log"; \ exit 1; \ fi cp gpg-error-config-old $@ install-data-local: if HAVE_W32CE_SYSTEM -$(MKDIR_P) "$(DESTDIR)$(includedir)/gpg-extra" $(INSTALL_DATA) gpg-extra/errno.h \ "$(DESTDIR)$(includedir)/gpg-extra/errno.h" else : endif diff --git a/src/estream.c b/src/estream.c index 06ebdaa..0d37c1a 100644 --- a/src/estream.c +++ b/src/estream.c @@ -1,5295 +1,5414 @@ /* 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 #ifdef HAVE_W32_SYSTEM # ifdef HAVE_WINSOCK2_H # include # endif # include #else # ifdef HAVE_POLL_H # include # endif #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 #define IS_INVALID_FD(a) ((a) == -1) /* 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. + * system error code. That actually belong into sysutils but to allow + * standalone use of estream we keep it here. */ #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; + return EPERM; /* ReactOS uses EACCES ("Permission denied") and + * is likely right because they used an + * undocumented function to associate the error + * codes. However we have always used EPERM + * ("Operation not permitted", e.g. function is + * required to be called by root) and we better + * stick to that to avoid surprising bugs. */ case ERROR_INVALID_HANDLE: + return EBADF; + case ERROR_INVALID_BLOCK: - return EINVAL; + return ENOMEM; case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM; case ERROR_NO_DATA: return EPIPE; + case ERROR_ALREADY_EXISTS: + return EEXIST; + + /* This mapping has been taken from reactOS. */ + case ERROR_TOO_MANY_OPEN_FILES: return EMFILE; + case ERROR_ARENA_TRASHED: return ENOMEM; + case ERROR_BAD_ENVIRONMENT: return E2BIG; + case ERROR_BAD_FORMAT: return ENOEXEC; + case ERROR_INVALID_DRIVE: return ENOENT; + case ERROR_CURRENT_DIRECTORY: return EACCES; + case ERROR_NOT_SAME_DEVICE: return EXDEV; + case ERROR_NO_MORE_FILES: return ENOENT; + case ERROR_WRITE_PROTECT: return EACCES; + case ERROR_BAD_UNIT: return EACCES; + case ERROR_NOT_READY: return EACCES; + case ERROR_BAD_COMMAND: return EACCES; + case ERROR_CRC: return EACCES; + case ERROR_BAD_LENGTH: return EACCES; + case ERROR_SEEK: return EACCES; + case ERROR_NOT_DOS_DISK: return EACCES; + case ERROR_SECTOR_NOT_FOUND: return EACCES; + case ERROR_OUT_OF_PAPER: return EACCES; + case ERROR_WRITE_FAULT: return EACCES; + case ERROR_READ_FAULT: return EACCES; + case ERROR_GEN_FAILURE: return EACCES; + case ERROR_SHARING_VIOLATION: return EACCES; + case ERROR_LOCK_VIOLATION: return EACCES; + case ERROR_WRONG_DISK: return EACCES; + case ERROR_SHARING_BUFFER_EXCEEDED: return EACCES; + case ERROR_BAD_NETPATH: return ENOENT; + case ERROR_NETWORK_ACCESS_DENIED: return EACCES; + case ERROR_BAD_NET_NAME: return ENOENT; + case ERROR_FILE_EXISTS: return EEXIST; + case ERROR_CANNOT_MAKE: return EACCES; + case ERROR_FAIL_I24: return EACCES; + case ERROR_NO_PROC_SLOTS: return EAGAIN; + case ERROR_DRIVE_LOCKED: return EACCES; + case ERROR_BROKEN_PIPE: return EPIPE; + case ERROR_DISK_FULL: return ENOSPC; + case ERROR_INVALID_TARGET_HANDLE: return EBADF; + case ERROR_WAIT_NO_CHILDREN: return ECHILD; + case ERROR_CHILD_NOT_COMPLETE: return ECHILD; + case ERROR_DIRECT_ACCESS_HANDLE: return EBADF; + case ERROR_SEEK_ON_DEVICE: return EACCES; + case ERROR_DIR_NOT_EMPTY: return ENOTEMPTY; + case ERROR_NOT_LOCKED: return EACCES; + case ERROR_BAD_PATHNAME: return ENOENT; + case ERROR_MAX_THRDS_REACHED: return EAGAIN; + case ERROR_LOCK_FAILED: return EACCES; + case ERROR_INVALID_STARTING_CODESEG: return ENOEXEC; + case ERROR_INVALID_STACKSEG: return ENOEXEC; + case ERROR_INVALID_MODULETYPE: return ENOEXEC; + case ERROR_INVALID_EXE_SIGNATURE: return ENOEXEC; + case ERROR_EXE_MARKED_INVALID: return ENOEXEC; + case ERROR_BAD_EXE_FORMAT: return ENOEXEC; + case ERROR_ITERATED_DATA_EXCEEDS_64k: return ENOEXEC; + case ERROR_INVALID_MINALLOCSIZE: return ENOEXEC; + case ERROR_DYNLINK_FROM_INVALID_RING: return ENOEXEC; + case ERROR_IOPL_NOT_ENABLED: return ENOEXEC; + case ERROR_INVALID_SEGDPL: return ENOEXEC; + case ERROR_AUTODATASEG_EXCEEDS_64k: return ENOEXEC; + case ERROR_RING2SEG_MUST_BE_MOVABLE: return ENOEXEC; + case ERROR_RELOC_CHAIN_XEEDS_SEGLIM: return ENOEXEC; + case ERROR_INFLOOP_IN_RELOC_CHAIN: return ENOEXEC; + case ERROR_FILENAME_EXCED_RANGE: return ENOENT; + case ERROR_NESTING_NOT_ALLOWED: return EAGAIN; + case ERROR_NOT_ENOUGH_QUOTA: return ENOMEM; + default: return EIO; } } + +/* Wrapper to be used by other modules to set ERRNO from the Windows + * error. EC may be -1 to get the last error. */ +void +_gpgrt_w32_set_errno (int ec) +{ + if (ec == -1) + ec = GetLastError (); + _set_errno (map_w32_to_errno (ec)); +} + + #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) { if (item_prev) item_prev->next = item->next; else 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; } gpgrt_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; } gpgrt_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; gpgrt_assert (mem_cookie->memory_size >= mem_cookie->offset); nleft = mem_cookie->memory_size - mem_cookie->offset; gpgrt_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; } gpgrt_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. */ +#ifdef HAVE_W32_SYSTEM +static int +any8bitchar (const char *string) +{ + if (string) + for ( ; *string; string++) + if ((*string & 0x80)) + return 1; + return 0; +} +#endif /*HAVE_W32_SYSTEM*/ + /* 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; } +#ifdef HAVE_W32_SYSTEM + if (any8bitchar (path)) + { + wchar_t *wpath; + + wpath = _gpgrt_utf8_to_wchar (path); + if (!wpath) + fd = -1; + else + { + fd = _wopen (wpath, modeflags, cmode); + _gpgrt_free_wchar (wpath); + } + } + else /* Avoid unnecessary conversion. */ + fd = open (path, modeflags, cmode); +#else fd = open (path, modeflags, cmode); +#endif 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; gpgrt_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) { gpgrt_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. With CANCEL_MODE set * try to cancel as much as possible (see _gpgrt_fcancel). */ static int do_close (estream_t stream, int cancel_mode, 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); if (cancel_mode) { stream->flags.writing = 0; es_empty (stream); } 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, 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, gpgrt_string_filter_t sf, void *sfvalue, const char *_GPGRT__RESTRICT format, va_list ap) { int rc; stream->intern->print_ntotal = 0; rc = _gpgrt_estream_format (print_writer, stream, sf, sfvalue, 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)); _gpgrt_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, 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, 0); stream = NULL; } return stream; } int _gpgrt_fclose (estream_t stream) { int err; err = do_close (stream, 0, 0); return err; } /* gpgrt_fcancel does the same as gpgrt_fclose but tries to avoid * flushing out any data still held in internal buffers. It may or * may not remove a new file created for that stream by the open * function. */ int _gpgrt_fcancel (estream_t stream) { int err; err = do_close (stream, 1, 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, 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. If gpgrt_fcancel is used, the cancellation of internal buffers is done before the notifications. The notification handler 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-initialized 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, gpgrt_string_filter_t sf, void *sfvalue, const char *_GPGRT__RESTRICT format, va_list ap) { return do_print_stream (stream, sf, sfvalue, format, ap); } int _gpgrt_vfprintf (estream_t _GPGRT__RESTRICT stream, gpgrt_string_filter_t sf, void *sfvalue, const char *_GPGRT__RESTRICT format, va_list ap) { int ret; lock_stream (stream); ret = do_print_stream (stream, sf, sfvalue, 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, NULL, NULL, 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, NULL, NULL, 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 ((intptr_t)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 # ifdef HAVE_POLL_H struct pollfd *poll_fds = NULL; nfds_t poll_nfds; # else fd_set readfds, writefds, exceptfds; int any_readfd, any_writefd, any_exceptfd; int max_fd; #endif 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 */ } if (count) goto leave; /* 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*/ # ifdef HAVE_POLL_H poll_fds = xtrymalloc (sizeof (*poll_fds)*nfds); if (!poll_fds) { count = -1; goto leave; } poll_nfds = 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 || item->want_write || item->want_oob) { poll_fds[poll_nfds].fd = fd; poll_fds[poll_nfds].events = ((item->want_read ? POLLIN : 0) |(item->want_write ? POLLOUT : 0) |(item->want_oob ? POLLPRI : 0)); poll_fds[poll_nfds].revents = 0; poll_nfds++; } } _gpgrt_pre_syscall (); do ret = poll (poll_fds, poll_nfds, timeout); while (ret == -1 && (errno == EINTR || errno == EAGAIN)); _gpgrt_post_syscall (); # else /* !HAVE_POLL_H */ 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 (); # endif if (ret == -1) { # ifdef HAVE_POLL_H trace_errno (1, ("poll failed: ")); # else trace_errno (1, ("select failed: ")); # endif 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; } # ifdef HAVE_POLL_H poll_nfds = 0; 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 && (poll_fds[poll_nfds].revents & (POLLIN|POLLHUP))) { item->got_read = 1; any = 1; } if (item->want_write && (poll_fds[poll_nfds].revents & POLLOUT)) { item->got_write = 1; any = 1; } if (item->want_oob && (poll_fds[poll_nfds].revents & ~(POLLIN|POLLOUT))) { item->got_oob = 1; any = 1; } if (item->want_read || item->want_write || item->want_oob) poll_nfds++; if (any) count++; } # else 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 #endif /*!HAVE_W32_SYSTEM*/ leave: #ifndef HAVE_W32_SYSTEM # ifdef HAVE_POLL_H xfree (poll_fds); # endif #endif #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/gpgrt-int.h b/src/gpgrt-int.h index 913b9ba..58b156f 100644 --- a/src/gpgrt-int.h +++ b/src/gpgrt-int.h @@ -1,840 +1,841 @@ /* 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" +#include "protos.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_add_emergency_cleanup (void (*f)(void)); void _gpgrt_abort (void) GPGRT_ATTR_NORETURN; void _gpgrt_set_alloc_func (void *(*f)(void *a, size_t n)); void *_gpgrt_realloc (void *a, size_t n); void *_gpgrt_reallocarray (void *a, size_t oldnmemb, size_t nmemb, size_t size); 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 xtryreallocarray(a,b,c,d) _gpgrt_reallocarray ((a),(b),(c),(d)) #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. */ 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. */ /* BUFSIZ on Windows is 512 but on current Linux it is 8k. We better * use the 8k for Windows as well. */ #ifdef HAVE_W32_SYSTEM # define BUFFER_BLOCK_SIZE 8192 #else # define BUFFER_BLOCK_SIZE BUFSIZ #endif #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_fcancel (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, gpgrt_string_filter_t sf, void *sfvalue, const char *_GPGRT__RESTRICT format, va_list ap) GPGRT_ATTR_PRINTF(4,0); int _gpgrt_vfprintf_unlocked (gpgrt_stream_t _GPGRT__RESTRICT stream, gpgrt_string_filter_t sf, void *sfvalue, const char *_GPGRT__RESTRICT format, va_list ap) GPGRT_ATTR_PRINTF(4,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 */ #undef snprintf #define snprintf _gpgrt_estream_snprintf #if HAVE_W32_SYSTEM /* Prototypes for w32-estream.c. */ extern 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; unsigned int stop_seen:1; unsigned int invalid_encoding:1; unsigned 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); int _gpgrt_argparser (gpgrt_argparse_t *arg, gpgrt_opt_t *opts, const char *confname); 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*)); void _gpgrt_set_confdir (int what, const char *name); /* * 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); /* Return the home directory of user NAME. */ char *_gpgrt_getpwdir (const char *name); /* Return the account name of the current user. */ char *_gpgrt_getusername (void); /* Expand and concat file name parts. */ char *_gpgrt_vfnameconcat (int want_abs, const char *first_part, va_list arg_ptr); char *_gpgrt_fnameconcat (const char *first_part, ... ) GPGRT_ATTR_SENTINEL(0); char *_gpgrt_absfnameconcat (const char *first_part, ... ) GPGRT_ATTR_SENTINEL(0); /* * 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/protos.h b/src/protos.h new file mode 100644 index 0000000..9a8d57a --- /dev/null +++ b/src/protos.h @@ -0,0 +1,30 @@ +/* protos.h - Miscellaneous prototypes + * Copyright (C) 2020 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_PROTOS_H +#define _GPGRT_PROTOS_H + +/*-- w32-gettext.c --*/ +wchar_t *_gpgrt_utf8_to_wchar (const char *string); +void _gpgrt_free_wchar (wchar_t *wstring); +void _gpgrt_w32_set_errno (int ec); + + +#endif /*_GPGRT_PROTOS_H*/ diff --git a/src/sysutils.c b/src/sysutils.c index bc446fb..eb5249a 100644 --- a/src/sysutils.c +++ b/src/sysutils.c @@ -1,416 +1,444 @@ /* sysutils.c - Platform specific helper functions * Copyright (C) 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+ */ #include #include #include #include #include #include #ifdef HAVE_W32_SYSTEM # include #endif #ifdef HAVE_STAT # include #endif #include #include #ifdef HAVE_PWD_H # include #endif #include "gpgrt-int.h" /* Return true if FD is valid. */ int _gpgrt_fd_valid_p (int fd) { int d = dup (fd); if (d < 0) return 0; close (d); return 1; } /* Our variant of getenv. The returned string must be freed. If the * environment variable does not exists NULL is returned and ERRNO set * to 0. */ char * _gpgrt_getenv (const char *name) { if (!name || !*name || strchr (name, '=')) { _gpg_err_set_errno (EINVAL); return NULL; } #ifdef HAVE_W32_SYSTEM { int len, size; char *result; len = GetEnvironmentVariable (name, NULL, 0); if (!len && GetLastError () == ERROR_ENVVAR_NOT_FOUND) { _gpg_err_set_errno (0); return NULL; } again: size = len; result = _gpgrt_malloc (size); if (!result) return NULL; len = GetEnvironmentVariable (name, result, size); if (len >= size) { /* Changed in the meantime - retry. */ _gpgrt_free (result); goto again; } if (!len && GetLastError () == ERROR_ENVVAR_NOT_FOUND) { /* Deleted in the meantime. */ _gpgrt_free (result); _gpg_err_set_errno (0); return NULL; } if (!len) { /* Other error. FIXME: We need mapping fucntion. */ _gpgrt_free (result); _gpg_err_set_errno (EIO); return NULL; } return result; } #else /*!HAVE_W32_SYSTEM*/ { const char *s = getenv (name); if (!s) { _gpg_err_set_errno (0); return NULL; } return _gpgrt_strdup (s); } #endif /*!HAVE_W32_SYSTEM*/ } /* Wrapper around setenv so that we can have the same function in * Windows and Unix. In contrast to the standard setenv passing a * VALUE as NULL and setting OVERWRITE will remove the envvar. */ gpg_err_code_t _gpgrt_setenv (const char *name, const char *value, int overwrite) { if (!name || !*name || strchr (name, '=')) return GPG_ERR_EINVAL; #ifdef HAVE_W32_SYSTEM /* Windows maintains (at least) two sets of environment variables. * One set can be accessed by GetEnvironmentVariable and * SetEnvironmentVariable. This set is inherited by the children. * The other set is maintained in the C runtime, and is accessed * using getenv and putenv. We try to keep them in sync by * modifying both sets. Note that gpgrt_getenv ignores the libc * values - however, too much existing code still uses getenv. */ { int exists; char tmpbuf[10]; char *buf; if (!value && overwrite) { if (!SetEnvironmentVariable (name, NULL)) return GPG_ERR_EINVAL; if (getenv (name)) { /* Ugly: Leaking memory. */ buf = _gpgrt_strdup (name); if (!buf) return _gpg_err_code_from_syserror (); if (putenv (buf)) return _gpg_err_code_from_syserror (); } return 0; } exists = GetEnvironmentVariable (name, tmpbuf, sizeof tmpbuf); if ((! exists || overwrite) && !SetEnvironmentVariable (name, value)) return GPG_ERR_EINVAL; /* (Might also be ENOMEM.) */ if (overwrite || !getenv (name)) { /* Ugly: Leaking memory. */ buf = _gpgrt_strconcat (name, "=", value, NULL); if (!buf) return _gpg_err_code_from_syserror (); if (putenv (buf)) return _gpg_err_code_from_syserror (); } return 0; } #else /*!HAVE_W32_SYSTEM*/ # ifdef HAVE_SETENV { if (!value && overwrite) { if (unsetenv (name)) return _gpg_err_code_from_syserror (); } else { if (setenv (name, value ? value : "", overwrite)) return _gpg_err_code_from_syserror (); } return 0; } # else /*!HAVE_SETENV*/ # if __GNUC__ # warning no setenv - using putenv but leaking memory. # endif { char *buf; if (!value && overwrite) { if (getenv (name)) { buf = _gpgrt_strdup (name); if (!buf) return _gpg_err_code_from_syserror (); if (putenv (buf)) return -1; } } else if (overwrite || !getenv (name)) { buf = _gpgrt_strconcat (name, "=", value, NULL); if (!buf) return _gpg_err_code_from_syserror (); if (putenv (buf)) return _gpg_err_code_from_syserror (); } return 0; } # endif /*!HAVE_SETENV*/ #endif /*!HAVE_W32_SYSTEM*/ } #ifndef HAVE_W32_SYSTEM static mode_t modestr_to_mode (const char *modestr) { mode_t mode = 0; if (modestr && *modestr) { modestr++; if (*modestr && *modestr++ == 'r') mode |= S_IRUSR; if (*modestr && *modestr++ == 'w') mode |= S_IWUSR; if (*modestr && *modestr++ == 'x') mode |= S_IXUSR; if (*modestr && *modestr++ == 'r') mode |= S_IRGRP; if (*modestr && *modestr++ == 'w') mode |= S_IWGRP; if (*modestr && *modestr++ == 'x') mode |= S_IXGRP; if (*modestr && *modestr++ == 'r') mode |= S_IROTH; if (*modestr && *modestr++ == 'w') mode |= S_IWOTH; if (*modestr && *modestr++ == 'x') mode |= S_IXOTH; } return mode; } #endif /* A wrapper around mkdir which takes a string for the mode argument. * This makes it easier to handle the mode argument which is not * defined on all systems. The format of the modestring is * * "-rwxrwxrwx" * * '-' is a don't care or not set. 'r', 'w', 'x' are read allowed, * write allowed, execution allowed with the first group for the user, * the second for the group and the third for all others. If the * string is shorter than above the missing mode characters are meant - * to be not set. */ + * to be not set. + * + * Note that in addition to returning an gpg-error error code ERRNO is + * also set by this function. + */ gpg_err_code_t _gpgrt_mkdir (const char *name, const char *modestr) { -#ifdef HAVE_W32CE_SYSTEM +#ifdef HAVE_W32_SYSTEM wchar_t *wname; + gpg_err_code_t ec; (void)modestr; - wname = utf8_to_wchar (name); + /* Note: Fixme: We should set appropriate permissions. */ + wname = _gpgrt_utf8_to_wchar (name); if (!wname) return _gpg_err_code_from_syserror (); if (!CreateDirectoryW (wname, NULL)) { - xfree (wname); - return _gpg_err_code_from_syserror (); + _gpgrt_w32_set_errno (-1); + ec = _gpg_err_code_from_syserror (); } - xfree (wname); - return 0; + else + ec = 0; + _gpgrt_free_wchar (wname); + return ec; #elif MKDIR_TAKES_ONE_ARG (void)modestr; - /* Note: In the case of W32 we better use CreateDirectory and try to - set appropriate permissions. However using mkdir is easier - because this sets ERRNO. */ if (mkdir (name)) return _gpg_err_code_from_syserror (); return 0; #else if (mkdir (name, modestr_to_mode (modestr))) return _gpg_err_code_from_syserror (); return 0; #endif } /* A simple wrapper around chdir. NAME is expected to be utf8 - * encoded. */ + * encoded. + * Note that in addition to returning an gpg-error error code ERRNO is + * also set by this function. */ gpg_err_code_t _gpgrt_chdir (const char *name) { +#ifdef HAVE_W32_SYSTEM + wchar_t *wname; + gpg_err_code_t ec; + + wname = _gpgrt_utf8_to_wchar (name); + if (!wname) + return _gpg_err_code_from_syserror (); + if (!SetCurrentDirectoryW (wname)) + { + _gpgrt_w32_set_errno (-1); + ec = _gpg_err_code_from_syserror (); + } + else + ec = 0; + _gpgrt_free_wchar (wname); + return ec; + +#else /*!HAVE_W32_SYSTEM*/ if (chdir (name)) return _gpg_err_code_from_syserror (); return 0; +#endif /*!HAVE_W32_SYSTEM*/ } /* Return the current working directory as a malloced string. Return * NULL and sets ERRNO on error. */ char * _gpgrt_getcwd (void) { char *buffer; size_t size = 100; + /* FIXME: We need to support utf8 */ for (;;) { buffer = xtrymalloc (size+1); if (!buffer) return NULL; #ifdef HAVE_W32CE_SYSTEM strcpy (buffer, "/"); /* Always "/". */ return buffer; #else if (getcwd (buffer, size) == buffer) return buffer; xfree (buffer); if (errno != ERANGE) return NULL; size *= 2; #endif } } /* Get the standard home directory for user NAME. If NAME is NULL the * directory for the current user is returned. Caller must release * the returned string. */ char * _gpgrt_getpwdir (const char *name) { char *result = NULL; #ifdef HAVE_PWD_H struct passwd *pwd = NULL; if (name) { #ifdef HAVE_GETPWNAM /* Fixme: We should use getpwnam_r if available. */ pwd = getpwnam (name); #endif } else { #ifdef HAVE_GETPWUID /* Fixme: We should use getpwuid_r if available. */ pwd = getpwuid (getuid()); #endif } if (pwd) { result = _gpgrt_strdup (pwd->pw_dir); } #else /*!HAVE_PWD_H*/ /* No support at all. */ (void)name; #endif /*HAVE_PWD_H*/ return result; } /* Return a malloced copy of the current user's account name; this may * return NULL on memory failure. */ char * _gpgrt_getusername (void) { char *result = NULL; #ifdef HAVE_W32_SYSTEM char tmp[1]; DWORD size = 1; + /* FIXME: We need to support utf8 */ GetUserNameA (tmp, &size); result = _gpgrt_malloc (size); if (result && !GetUserNameA (result, &size)) { xfree (result); result = NULL; } #else /* !HAVE_W32_SYSTEM */ # if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID) struct passwd *pwd; pwd = getpwuid (getuid()); if (pwd) { result = _gpgrt_strdup (pwd->pw_name); } # endif /*HAVE_PWD_H*/ #endif /* !HAVE_W32_SYSTEM */ return result; } diff --git a/src/w32-gettext.c b/src/w32-gettext.c index 11e4f3d..a734ef2 100644 --- a/src/w32-gettext.c +++ b/src/w32-gettext.c @@ -1,1995 +1,2024 @@ /* w32-gettext.h - A simple gettext implementation for Windows targets. Copyright (C) 1995, 1996, 1997, 1999, 2005, 2007, 2008, 2010 Free Software Foundation, Inc. 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 . */ #if HAVE_CONFIG_H #include #endif #if !defined (_WIN32) && !defined (__CYGWIN32__) # error This module may only be build for Windows or Cygwin32 #endif #include #include #include #include #include #ifdef HAVE_SYS_TYPES_H #include #endif #include #ifndef HAVE_W32CE_SYSTEM # include #endif /*HAVE_W32CE_SYSTEM*/ #include #ifdef JNLIB_IN_JNLIB #include "libjnlib-config.h" #endif #ifndef jnlib_malloc # define jnlib_malloc(a) malloc ((a)) # define jnlib_calloc(a,b) calloc ((a), (b)) # define jnlib_free(a) free ((a)) # define jnlib_xstrdup(a) not_used #endif /*!jnlib_malloc*/ #include "init.h" #include "gpg-error.h" +#include "protos.h" /* Override values initialized by gpgrt_w32_override_locale. If NAME * is not the empty string LANGID will be used. */ static struct { unsigned short active; /* If not zero this override is active. */ unsigned short langid; char name[28]; } override_locale; #ifdef HAVE_W32CE_SYSTEM /* Forward declaration. */ static wchar_t *utf8_to_wchar (const char *string, size_t length, size_t *retlen); static HANDLE MyCreateFileA (LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwSharedMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) { wchar_t *filename; HANDLE result; int err; size_t size; filename = utf8_to_wchar (lpFileName, -1, &size); if (!filename) return INVALID_HANDLE_VALUE; result = CreateFileW (filename, dwDesiredAccess, dwSharedMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); err = GetLastError (); free (filename); SetLastError (err); return result; } #undef CreateFileA #define CreateFileA MyCreateFileA #endif /* localname.c from gettext BEGIN. */ /* Determine the current selected locale. Copyright (C) 1995-1999, 2000-2003 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* Written by Ulrich Drepper , 1995. */ /* Win32 code written by Tor Lillqvist . */ /* Renamed _nl_locale_name, removed unused args, removed include files, non-W32 code and changed comments . */ /* Mingw headers don't have latest language and sublanguage codes. */ #ifndef LANG_AFRIKAANS #define LANG_AFRIKAANS 0x36 #endif #ifndef LANG_ALBANIAN #define LANG_ALBANIAN 0x1c #endif #ifndef LANG_AMHARIC #define LANG_AMHARIC 0x5e #endif #ifndef LANG_ARABIC #define LANG_ARABIC 0x01 #endif #ifndef LANG_ARMENIAN #define LANG_ARMENIAN 0x2b #endif #ifndef LANG_ASSAMESE #define LANG_ASSAMESE 0x4d #endif #ifndef LANG_AZERI #define LANG_AZERI 0x2c #endif #ifndef LANG_BASQUE #define LANG_BASQUE 0x2d #endif #ifndef LANG_BELARUSIAN #define LANG_BELARUSIAN 0x23 #endif #ifndef LANG_BENGALI #define LANG_BENGALI 0x45 #endif #ifndef LANG_BURMESE #define LANG_BURMESE 0x55 #endif #ifndef LANG_CAMBODIAN #define LANG_CAMBODIAN 0x53 #endif #ifndef LANG_CATALAN #define LANG_CATALAN 0x03 #endif #ifndef LANG_CHEROKEE #define LANG_CHEROKEE 0x5c #endif #ifndef LANG_DIVEHI #define LANG_DIVEHI 0x65 #endif #ifndef LANG_EDO #define LANG_EDO 0x66 #endif #ifndef LANG_ESTONIAN #define LANG_ESTONIAN 0x25 #endif #ifndef LANG_FAEROESE #define LANG_FAEROESE 0x38 #endif #ifndef LANG_FARSI #define LANG_FARSI 0x29 #endif #ifndef LANG_FRISIAN #define LANG_FRISIAN 0x62 #endif #ifndef LANG_FULFULDE #define LANG_FULFULDE 0x67 #endif #ifndef LANG_GAELIC #define LANG_GAELIC 0x3c #endif #ifndef LANG_GALICIAN #define LANG_GALICIAN 0x56 #endif #ifndef LANG_GEORGIAN #define LANG_GEORGIAN 0x37 #endif #ifndef LANG_GUARANI #define LANG_GUARANI 0x74 #endif #ifndef LANG_GUJARATI #define LANG_GUJARATI 0x47 #endif #ifndef LANG_HAUSA #define LANG_HAUSA 0x68 #endif #ifndef LANG_HAWAIIAN #define LANG_HAWAIIAN 0x75 #endif #ifndef LANG_HEBREW #define LANG_HEBREW 0x0d #endif #ifndef LANG_HINDI #define LANG_HINDI 0x39 #endif #ifndef LANG_IBIBIO #define LANG_IBIBIO 0x69 #endif #ifndef LANG_IGBO #define LANG_IGBO 0x70 #endif #ifndef LANG_INDONESIAN #define LANG_INDONESIAN 0x21 #endif #ifndef LANG_INUKTITUT #define LANG_INUKTITUT 0x5d #endif #ifndef LANG_KANNADA #define LANG_KANNADA 0x4b #endif #ifndef LANG_KANURI #define LANG_KANURI 0x71 #endif #ifndef LANG_KASHMIRI #define LANG_KASHMIRI 0x60 #endif #ifndef LANG_KAZAK #define LANG_KAZAK 0x3f #endif #ifndef LANG_KONKANI #define LANG_KONKANI 0x57 #endif #ifndef LANG_KYRGYZ #define LANG_KYRGYZ 0x40 #endif #ifndef LANG_LAO #define LANG_LAO 0x54 #endif #ifndef LANG_LATIN #define LANG_LATIN 0x76 #endif #ifndef LANG_LATVIAN #define LANG_LATVIAN 0x26 #endif #ifndef LANG_LITHUANIAN #define LANG_LITHUANIAN 0x27 #endif #ifndef LANG_MACEDONIAN #define LANG_MACEDONIAN 0x2f #endif #ifndef LANG_MALAY #define LANG_MALAY 0x3e #endif #ifndef LANG_MALAYALAM #define LANG_MALAYALAM 0x4c #endif #ifndef LANG_MALTESE #define LANG_MALTESE 0x3a #endif #ifndef LANG_MANIPURI #define LANG_MANIPURI 0x58 #endif #ifndef LANG_MARATHI #define LANG_MARATHI 0x4e #endif #ifndef LANG_MONGOLIAN #define LANG_MONGOLIAN 0x50 #endif #ifndef LANG_NEPALI #define LANG_NEPALI 0x61 #endif #ifndef LANG_ORIYA #define LANG_ORIYA 0x48 #endif #ifndef LANG_OROMO #define LANG_OROMO 0x72 #endif #ifndef LANG_PAPIAMENTU #define LANG_PAPIAMENTU 0x79 #endif #ifndef LANG_PASHTO #define LANG_PASHTO 0x63 #endif #ifndef LANG_PUNJABI #define LANG_PUNJABI 0x46 #endif #ifndef LANG_RHAETO_ROMANCE #define LANG_RHAETO_ROMANCE 0x17 #endif #ifndef LANG_SAAMI #define LANG_SAAMI 0x3b #endif #ifndef LANG_SANSKRIT #define LANG_SANSKRIT 0x4f #endif #ifndef LANG_SERBIAN #define LANG_SERBIAN 0x1a #endif #ifndef LANG_SINDHI #define LANG_SINDHI 0x59 #endif #ifndef LANG_SINHALESE #define LANG_SINHALESE 0x5b #endif #ifndef LANG_SLOVAK #define LANG_SLOVAK 0x1b #endif #ifndef LANG_SOMALI #define LANG_SOMALI 0x77 #endif #ifndef LANG_SORBIAN #define LANG_SORBIAN 0x2e #endif #ifndef LANG_SUTU #define LANG_SUTU 0x30 #endif #ifndef LANG_SWAHILI #define LANG_SWAHILI 0x41 #endif #ifndef LANG_SYRIAC #define LANG_SYRIAC 0x5a #endif #ifndef LANG_TAGALOG #define LANG_TAGALOG 0x64 #endif #ifndef LANG_TAJIK #define LANG_TAJIK 0x28 #endif #ifndef LANG_TAMAZIGHT #define LANG_TAMAZIGHT 0x5f #endif #ifndef LANG_TAMIL #define LANG_TAMIL 0x49 #endif #ifndef LANG_TATAR #define LANG_TATAR 0x44 #endif #ifndef LANG_TELUGU #define LANG_TELUGU 0x4a #endif #ifndef LANG_THAI #define LANG_THAI 0x1e #endif #ifndef LANG_TIBETAN #define LANG_TIBETAN 0x51 #endif #ifndef LANG_TIGRINYA #define LANG_TIGRINYA 0x73 #endif #ifndef LANG_TSONGA #define LANG_TSONGA 0x31 #endif #ifndef LANG_TSWANA #define LANG_TSWANA 0x32 #endif #ifndef LANG_TURKMEN #define LANG_TURKMEN 0x42 #endif #ifndef LANG_UKRAINIAN #define LANG_UKRAINIAN 0x22 #endif #ifndef LANG_URDU #define LANG_URDU 0x20 #endif #ifndef LANG_UZBEK #define LANG_UZBEK 0x43 #endif #ifndef LANG_VENDA #define LANG_VENDA 0x33 #endif #ifndef LANG_VIETNAMESE #define LANG_VIETNAMESE 0x2a #endif #ifndef LANG_WELSH #define LANG_WELSH 0x52 #endif #ifndef LANG_XHOSA #define LANG_XHOSA 0x34 #endif #ifndef LANG_YI #define LANG_YI 0x78 #endif #ifndef LANG_YIDDISH #define LANG_YIDDISH 0x3d #endif #ifndef LANG_YORUBA #define LANG_YORUBA 0x6a #endif #ifndef LANG_ZULU #define LANG_ZULU 0x35 #endif #ifndef SUBLANG_ARABIC_SAUDI_ARABIA #define SUBLANG_ARABIC_SAUDI_ARABIA 0x01 #endif #ifndef SUBLANG_ARABIC_IRAQ #define SUBLANG_ARABIC_IRAQ 0x02 #endif #ifndef SUBLANG_ARABIC_EGYPT #define SUBLANG_ARABIC_EGYPT 0x03 #endif #ifndef SUBLANG_ARABIC_LIBYA #define SUBLANG_ARABIC_LIBYA 0x04 #endif #ifndef SUBLANG_ARABIC_ALGERIA #define SUBLANG_ARABIC_ALGERIA 0x05 #endif #ifndef SUBLANG_ARABIC_MOROCCO #define SUBLANG_ARABIC_MOROCCO 0x06 #endif #ifndef SUBLANG_ARABIC_TUNISIA #define SUBLANG_ARABIC_TUNISIA 0x07 #endif #ifndef SUBLANG_ARABIC_OMAN #define SUBLANG_ARABIC_OMAN 0x08 #endif #ifndef SUBLANG_ARABIC_YEMEN #define SUBLANG_ARABIC_YEMEN 0x09 #endif #ifndef SUBLANG_ARABIC_SYRIA #define SUBLANG_ARABIC_SYRIA 0x0a #endif #ifndef SUBLANG_ARABIC_JORDAN #define SUBLANG_ARABIC_JORDAN 0x0b #endif #ifndef SUBLANG_ARABIC_LEBANON #define SUBLANG_ARABIC_LEBANON 0x0c #endif #ifndef SUBLANG_ARABIC_KUWAIT #define SUBLANG_ARABIC_KUWAIT 0x0d #endif #ifndef SUBLANG_ARABIC_UAE #define SUBLANG_ARABIC_UAE 0x0e #endif #ifndef SUBLANG_ARABIC_BAHRAIN #define SUBLANG_ARABIC_BAHRAIN 0x0f #endif #ifndef SUBLANG_ARABIC_QATAR #define SUBLANG_ARABIC_QATAR 0x10 #endif #ifndef SUBLANG_AZERI_LATIN #define SUBLANG_AZERI_LATIN 0x01 #endif #ifndef SUBLANG_AZERI_CYRILLIC #define SUBLANG_AZERI_CYRILLIC 0x02 #endif #ifndef SUBLANG_BENGALI_INDIA #define SUBLANG_BENGALI_INDIA 0x01 #endif #ifndef SUBLANG_BENGALI_BANGLADESH #define SUBLANG_BENGALI_BANGLADESH 0x02 #endif #ifndef SUBLANG_CHINESE_MACAU #define SUBLANG_CHINESE_MACAU 0x05 #endif #ifndef SUBLANG_ENGLISH_SOUTH_AFRICA #define SUBLANG_ENGLISH_SOUTH_AFRICA 0x07 #endif #ifndef SUBLANG_ENGLISH_JAMAICA #define SUBLANG_ENGLISH_JAMAICA 0x08 #endif #ifndef SUBLANG_ENGLISH_CARIBBEAN #define SUBLANG_ENGLISH_CARIBBEAN 0x09 #endif #ifndef SUBLANG_ENGLISH_BELIZE #define SUBLANG_ENGLISH_BELIZE 0x0a #endif #ifndef SUBLANG_ENGLISH_TRINIDAD #define SUBLANG_ENGLISH_TRINIDAD 0x0b #endif #ifndef SUBLANG_ENGLISH_ZIMBABWE #define SUBLANG_ENGLISH_ZIMBABWE 0x0c #endif #ifndef SUBLANG_ENGLISH_PHILIPPINES #define SUBLANG_ENGLISH_PHILIPPINES 0x0d #endif #ifndef SUBLANG_ENGLISH_INDONESIA #define SUBLANG_ENGLISH_INDONESIA 0x0e #endif #ifndef SUBLANG_ENGLISH_HONGKONG #define SUBLANG_ENGLISH_HONGKONG 0x0f #endif #ifndef SUBLANG_ENGLISH_INDIA #define SUBLANG_ENGLISH_INDIA 0x10 #endif #ifndef SUBLANG_ENGLISH_MALAYSIA #define SUBLANG_ENGLISH_MALAYSIA 0x11 #endif #ifndef SUBLANG_ENGLISH_SINGAPORE #define SUBLANG_ENGLISH_SINGAPORE 0x12 #endif #ifndef SUBLANG_FRENCH_LUXEMBOURG #define SUBLANG_FRENCH_LUXEMBOURG 0x05 #endif #ifndef SUBLANG_FRENCH_MONACO #define SUBLANG_FRENCH_MONACO 0x06 #endif #ifndef SUBLANG_FRENCH_WESTINDIES #define SUBLANG_FRENCH_WESTINDIES 0x07 #endif #ifndef SUBLANG_FRENCH_REUNION #define SUBLANG_FRENCH_REUNION 0x08 #endif #ifndef SUBLANG_FRENCH_CONGO #define SUBLANG_FRENCH_CONGO 0x09 #endif #ifndef SUBLANG_FRENCH_SENEGAL #define SUBLANG_FRENCH_SENEGAL 0x0a #endif #ifndef SUBLANG_FRENCH_CAMEROON #define SUBLANG_FRENCH_CAMEROON 0x0b #endif #ifndef SUBLANG_FRENCH_COTEDIVOIRE #define SUBLANG_FRENCH_COTEDIVOIRE 0x0c #endif #ifndef SUBLANG_FRENCH_MALI #define SUBLANG_FRENCH_MALI 0x0d #endif #ifndef SUBLANG_FRENCH_MOROCCO #define SUBLANG_FRENCH_MOROCCO 0x0e #endif #ifndef SUBLANG_FRENCH_HAITI #define SUBLANG_FRENCH_HAITI 0x0f #endif #ifndef SUBLANG_GERMAN_LUXEMBOURG #define SUBLANG_GERMAN_LUXEMBOURG 0x04 #endif #ifndef SUBLANG_GERMAN_LIECHTENSTEIN #define SUBLANG_GERMAN_LIECHTENSTEIN 0x05 #endif #ifndef SUBLANG_KASHMIRI_INDIA #define SUBLANG_KASHMIRI_INDIA 0x02 #endif #ifndef SUBLANG_MALAY_MALAYSIA #define SUBLANG_MALAY_MALAYSIA 0x01 #endif #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02 #endif #ifndef SUBLANG_NEPALI_INDIA #define SUBLANG_NEPALI_INDIA 0x02 #endif #ifndef SUBLANG_PUNJABI_INDIA #define SUBLANG_PUNJABI_INDIA 0x01 #endif #ifndef SUBLANG_ROMANIAN_ROMANIA #define SUBLANG_ROMANIAN_ROMANIA 0x01 #endif #ifndef SUBLANG_SERBIAN_LATIN #define SUBLANG_SERBIAN_LATIN 0x02 #endif #ifndef SUBLANG_SERBIAN_CYRILLIC #define SUBLANG_SERBIAN_CYRILLIC 0x03 #endif #ifndef SUBLANG_SINDHI_INDIA #define SUBLANG_SINDHI_INDIA 0x00 #endif #ifndef SUBLANG_SINDHI_PAKISTAN #define SUBLANG_SINDHI_PAKISTAN 0x01 #endif #ifndef SUBLANG_SPANISH_GUATEMALA #define SUBLANG_SPANISH_GUATEMALA 0x04 #endif #ifndef SUBLANG_SPANISH_COSTA_RICA #define SUBLANG_SPANISH_COSTA_RICA 0x05 #endif #ifndef SUBLANG_SPANISH_PANAMA #define SUBLANG_SPANISH_PANAMA 0x06 #endif #ifndef SUBLANG_SPANISH_DOMINICAN_REPUBLIC #define SUBLANG_SPANISH_DOMINICAN_REPUBLIC 0x07 #endif #ifndef SUBLANG_SPANISH_VENEZUELA #define SUBLANG_SPANISH_VENEZUELA 0x08 #endif #ifndef SUBLANG_SPANISH_COLOMBIA #define SUBLANG_SPANISH_COLOMBIA 0x09 #endif #ifndef SUBLANG_SPANISH_PERU #define SUBLANG_SPANISH_PERU 0x0a #endif #ifndef SUBLANG_SPANISH_ARGENTINA #define SUBLANG_SPANISH_ARGENTINA 0x0b #endif #ifndef SUBLANG_SPANISH_ECUADOR #define SUBLANG_SPANISH_ECUADOR 0x0c #endif #ifndef SUBLANG_SPANISH_CHILE #define SUBLANG_SPANISH_CHILE 0x0d #endif #ifndef SUBLANG_SPANISH_URUGUAY #define SUBLANG_SPANISH_URUGUAY 0x0e #endif #ifndef SUBLANG_SPANISH_PARAGUAY #define SUBLANG_SPANISH_PARAGUAY 0x0f #endif #ifndef SUBLANG_SPANISH_BOLIVIA #define SUBLANG_SPANISH_BOLIVIA 0x10 #endif #ifndef SUBLANG_SPANISH_EL_SALVADOR #define SUBLANG_SPANISH_EL_SALVADOR 0x11 #endif #ifndef SUBLANG_SPANISH_HONDURAS #define SUBLANG_SPANISH_HONDURAS 0x12 #endif #ifndef SUBLANG_SPANISH_NICARAGUA #define SUBLANG_SPANISH_NICARAGUA 0x13 #endif #ifndef SUBLANG_SPANISH_PUERTO_RICO #define SUBLANG_SPANISH_PUERTO_RICO 0x14 #endif #ifndef SUBLANG_SWEDISH_FINLAND #define SUBLANG_SWEDISH_FINLAND 0x02 #endif #ifndef SUBLANG_TAMAZIGHT_ARABIC #define SUBLANG_TAMAZIGHT_ARABIC 0x01 #endif #ifndef SUBLANG_TAMAZIGHT_LATIN #define SUBLANG_TAMAZIGHT_LATIN 0x02 #endif #ifndef SUBLANG_TIGRINYA_ETHIOPIA #define SUBLANG_TIGRINYA_ETHIOPIA 0x00 #endif #ifndef SUBLANG_TIGRINYA_ERITREA #define SUBLANG_TIGRINYA_ERITREA 0x01 #endif #ifndef SUBLANG_URDU_PAKISTAN #define SUBLANG_URDU_PAKISTAN 0x01 #endif #ifndef SUBLANG_URDU_INDIA #define SUBLANG_URDU_INDIA 0x02 #endif #ifndef SUBLANG_UZBEK_LATIN #define SUBLANG_UZBEK_LATIN 0x01 #endif #ifndef SUBLANG_UZBEK_CYRILLIC #define SUBLANG_UZBEK_CYRILLIC 0x02 #endif /* Return an XPG style locale name language[_territory[.codeset]][@modifier]. Don't even bother determining the codeset; it's not useful in this context, because message catalogs are not specific to a single codeset. The result must not be freed; it is statically allocated. */ static const char * my_nl_locale_name (const char *categoryname) { #ifndef HAVE_W32CE_SYSTEM const char *retval; #endif LANGID langid; int primary, sub; if (override_locale.active) { if (*override_locale.name) return override_locale.name; langid = override_locale.langid; } else { LCID lcid; /* Let the user override the system settings through environment * variables, as on POSIX systems. */ #ifndef HAVE_W32CE_SYSTEM retval = getenv ("LC_ALL"); if (retval != NULL && retval[0] != '\0') return retval; retval = getenv (categoryname); if (retval != NULL && retval[0] != '\0') return retval; retval = getenv ("LANG"); if (retval != NULL && retval[0] != '\0') return retval; #endif /*!HAVE_W32CE_SYSTEM*/ /* Use native Win32 API locale ID. */ #ifdef HAVE_W32CE_SYSTEM lcid = GetSystemDefaultLCID (); #else lcid = GetThreadLocale (); #endif /* Strip off the sorting rules, keep only the language part. */ langid = LANGIDFROMLCID (lcid); } /* Split into language and territory part. */ primary = PRIMARYLANGID (langid); sub = SUBLANGID (langid); /* Dispatch on language. For details about languages, see https://www.ethnologue.com/ . */ switch (primary) { case LANG_AFRIKAANS: return "af_ZA"; case LANG_ALBANIAN: return "sq_AL"; case LANG_AMHARIC: return "am_ET"; case LANG_ARABIC: switch (sub) { case SUBLANG_ARABIC_SAUDI_ARABIA: return "ar_SA"; case SUBLANG_ARABIC_IRAQ: return "ar_IQ"; case SUBLANG_ARABIC_EGYPT: return "ar_EG"; case SUBLANG_ARABIC_LIBYA: return "ar_LY"; case SUBLANG_ARABIC_ALGERIA: return "ar_DZ"; case SUBLANG_ARABIC_MOROCCO: return "ar_MA"; case SUBLANG_ARABIC_TUNISIA: return "ar_TN"; case SUBLANG_ARABIC_OMAN: return "ar_OM"; case SUBLANG_ARABIC_YEMEN: return "ar_YE"; case SUBLANG_ARABIC_SYRIA: return "ar_SY"; case SUBLANG_ARABIC_JORDAN: return "ar_JO"; case SUBLANG_ARABIC_LEBANON: return "ar_LB"; case SUBLANG_ARABIC_KUWAIT: return "ar_KW"; case SUBLANG_ARABIC_UAE: return "ar_AE"; case SUBLANG_ARABIC_BAHRAIN: return "ar_BH"; case SUBLANG_ARABIC_QATAR: return "ar_QA"; } return "ar"; case LANG_ARMENIAN: return "hy_AM"; case LANG_ASSAMESE: return "as_IN"; case LANG_AZERI: switch (sub) { /* FIXME: Adjust this when Azerbaijani locales appear on Unix. */ case SUBLANG_AZERI_LATIN: return "az_AZ@latin"; case SUBLANG_AZERI_CYRILLIC: return "az_AZ@cyrillic"; } return "az"; case LANG_BASQUE: return "eu"; /* Ambiguous: could be "eu_ES" or "eu_FR". */ case LANG_BELARUSIAN: return "be_BY"; case LANG_BENGALI: switch (sub) { case SUBLANG_BENGALI_INDIA: return "bn_IN"; case SUBLANG_BENGALI_BANGLADESH: return "bn_BD"; } return "bn"; case LANG_BULGARIAN: return "bg_BG"; case LANG_BURMESE: return "my_MM"; case LANG_CAMBODIAN: return "km_KH"; case LANG_CATALAN: return "ca_ES"; case LANG_CHEROKEE: return "chr_US"; case LANG_CHINESE: switch (sub) { case SUBLANG_CHINESE_TRADITIONAL: return "zh_TW"; case SUBLANG_CHINESE_SIMPLIFIED: return "zh_CN"; case SUBLANG_CHINESE_HONGKONG: return "zh_HK"; case SUBLANG_CHINESE_SINGAPORE: return "zh_SG"; case SUBLANG_CHINESE_MACAU: return "zh_MO"; } return "zh"; case LANG_CROATIAN: /* LANG_CROATIAN == LANG_SERBIAN * What used to be called Serbo-Croatian * should really now be two separate * languages because of political reasons. * (Says tml, who knows nothing about Serbian * or Croatian.) * (I can feel those flames coming already.) */ switch (sub) { case SUBLANG_DEFAULT: return "hr_HR"; case SUBLANG_SERBIAN_LATIN: return "sr_CS"; case SUBLANG_SERBIAN_CYRILLIC: return "sr_CS@cyrillic"; } return "hr"; case LANG_CZECH: return "cs_CZ"; case LANG_DANISH: return "da_DK"; case LANG_DIVEHI: return "div_MV"; case LANG_DUTCH: switch (sub) { case SUBLANG_DUTCH: return "nl_NL"; case SUBLANG_DUTCH_BELGIAN: /* FLEMISH, VLAAMS */ return "nl_BE"; } return "nl"; case LANG_EDO: return "bin_NG"; case LANG_ENGLISH: switch (sub) { /* SUBLANG_ENGLISH_US == SUBLANG_DEFAULT. Heh. I thought * English was the language spoken in England. * Oh well. */ case SUBLANG_ENGLISH_US: return "en_US"; case SUBLANG_ENGLISH_UK: return "en_GB"; case SUBLANG_ENGLISH_AUS: return "en_AU"; case SUBLANG_ENGLISH_CAN: return "en_CA"; case SUBLANG_ENGLISH_NZ: return "en_NZ"; case SUBLANG_ENGLISH_EIRE: return "en_IE"; case SUBLANG_ENGLISH_SOUTH_AFRICA: return "en_ZA"; case SUBLANG_ENGLISH_JAMAICA: return "en_JM"; case SUBLANG_ENGLISH_CARIBBEAN: return "en_GD"; /* Grenada? */ case SUBLANG_ENGLISH_BELIZE: return "en_BZ"; case SUBLANG_ENGLISH_TRINIDAD: return "en_TT"; case SUBLANG_ENGLISH_ZIMBABWE: return "en_ZW"; case SUBLANG_ENGLISH_PHILIPPINES: return "en_PH"; case SUBLANG_ENGLISH_INDONESIA: return "en_ID"; case SUBLANG_ENGLISH_HONGKONG: return "en_HK"; case SUBLANG_ENGLISH_INDIA: return "en_IN"; case SUBLANG_ENGLISH_MALAYSIA: return "en_MY"; case SUBLANG_ENGLISH_SINGAPORE: return "en_SG"; } return "en"; case LANG_ESTONIAN: return "et_EE"; case LANG_FAEROESE: return "fo_FO"; case LANG_FARSI: return "fa_IR"; case LANG_FINNISH: return "fi_FI"; case LANG_FRENCH: switch (sub) { case SUBLANG_FRENCH: return "fr_FR"; case SUBLANG_FRENCH_BELGIAN: /* WALLOON */ return "fr_BE"; case SUBLANG_FRENCH_CANADIAN: return "fr_CA"; case SUBLANG_FRENCH_SWISS: return "fr_CH"; case SUBLANG_FRENCH_LUXEMBOURG: return "fr_LU"; case SUBLANG_FRENCH_MONACO: return "fr_MC"; case SUBLANG_FRENCH_WESTINDIES: return "fr"; /* Caribbean? */ case SUBLANG_FRENCH_REUNION: return "fr_RE"; case SUBLANG_FRENCH_CONGO: return "fr_CG"; case SUBLANG_FRENCH_SENEGAL: return "fr_SN"; case SUBLANG_FRENCH_CAMEROON: return "fr_CM"; case SUBLANG_FRENCH_COTEDIVOIRE: return "fr_CI"; case SUBLANG_FRENCH_MALI: return "fr_ML"; case SUBLANG_FRENCH_MOROCCO: return "fr_MA"; case SUBLANG_FRENCH_HAITI: return "fr_HT"; } return "fr"; case LANG_FRISIAN: return "fy_NL"; case LANG_FULFULDE: return "ful_NG"; case LANG_GAELIC: switch (sub) { case 0x01: /* SCOTTISH */ return "gd_GB"; case 0x02: /* IRISH */ return "ga_IE"; } return "C"; case LANG_GALICIAN: return "gl_ES"; case LANG_GEORGIAN: return "ka_GE"; case LANG_GERMAN: switch (sub) { case SUBLANG_GERMAN: return "de_DE"; case SUBLANG_GERMAN_SWISS: return "de_CH"; case SUBLANG_GERMAN_AUSTRIAN: return "de_AT"; case SUBLANG_GERMAN_LUXEMBOURG: return "de_LU"; case SUBLANG_GERMAN_LIECHTENSTEIN: return "de_LI"; } return "de"; case LANG_GREEK: return "el_GR"; case LANG_GUARANI: return "gn_PY"; case LANG_GUJARATI: return "gu_IN"; case LANG_HAUSA: return "ha_NG"; case LANG_HAWAIIAN: /* FIXME: Do they mean Hawaiian ("haw_US", 1000 speakers) or Hawaii Creole English ("cpe_US", 600000 speakers)? */ return "cpe_US"; case LANG_HEBREW: return "he_IL"; case LANG_HINDI: return "hi_IN"; case LANG_HUNGARIAN: return "hu_HU"; case LANG_IBIBIO: return "nic_NG"; case LANG_ICELANDIC: return "is_IS"; case LANG_IGBO: return "ibo_NG"; case LANG_INDONESIAN: return "id_ID"; case LANG_INUKTITUT: return "iu_CA"; case LANG_ITALIAN: switch (sub) { case SUBLANG_ITALIAN: return "it_IT"; case SUBLANG_ITALIAN_SWISS: return "it_CH"; } return "it"; case LANG_JAPANESE: return "ja_JP"; case LANG_KANNADA: return "kn_IN"; case LANG_KANURI: return "kau_NG"; case LANG_KASHMIRI: switch (sub) { case SUBLANG_DEFAULT: return "ks_PK"; case SUBLANG_KASHMIRI_INDIA: return "ks_IN"; } return "ks"; case LANG_KAZAK: return "kk_KZ"; case LANG_KONKANI: /* FIXME: Adjust this when such locales appear on Unix. */ return "kok_IN"; case LANG_KOREAN: return "ko_KR"; case LANG_KYRGYZ: return "ky_KG"; case LANG_LAO: return "lo_LA"; case LANG_LATIN: return "la_VA"; case LANG_LATVIAN: return "lv_LV"; case LANG_LITHUANIAN: return "lt_LT"; case LANG_MACEDONIAN: return "mk_MK"; case LANG_MALAY: switch (sub) { case SUBLANG_MALAY_MALAYSIA: return "ms_MY"; case SUBLANG_MALAY_BRUNEI_DARUSSALAM: return "ms_BN"; } return "ms"; case LANG_MALAYALAM: return "ml_IN"; case LANG_MALTESE: return "mt_MT"; case LANG_MANIPURI: /* FIXME: Adjust this when such locales appear on Unix. */ return "mni_IN"; case LANG_MARATHI: return "mr_IN"; case LANG_MONGOLIAN: return "mn"; /* Ambiguous: could be "mn_CN" or "mn_MN". */ case LANG_NEPALI: switch (sub) { case SUBLANG_DEFAULT: return "ne_NP"; case SUBLANG_NEPALI_INDIA: return "ne_IN"; } return "ne"; case LANG_NORWEGIAN: switch (sub) { case SUBLANG_NORWEGIAN_BOKMAL: return "no_NO"; case SUBLANG_NORWEGIAN_NYNORSK: return "nn_NO"; } return "no"; case LANG_ORIYA: return "or_IN"; case LANG_OROMO: return "om_ET"; case LANG_PAPIAMENTU: return "pap_AN"; case LANG_PASHTO: return "ps"; /* Ambiguous: could be "ps_PK" or "ps_AF". */ case LANG_POLISH: return "pl_PL"; case LANG_PORTUGUESE: switch (sub) { case SUBLANG_PORTUGUESE: return "pt_PT"; /* Hmm. SUBLANG_PORTUGUESE_BRAZILIAN == SUBLANG_DEFAULT. Same phenomenon as SUBLANG_ENGLISH_US == SUBLANG_DEFAULT. */ case SUBLANG_PORTUGUESE_BRAZILIAN: return "pt_BR"; } return "pt"; case LANG_PUNJABI: switch (sub) { case SUBLANG_PUNJABI_INDIA: return "pa_IN"; /* Gurmukhi script */ } return "pa"; case LANG_RHAETO_ROMANCE: return "rm_CH"; case LANG_ROMANIAN: switch (sub) { case SUBLANG_ROMANIAN_ROMANIA: return "ro_RO"; } return "ro"; case LANG_RUSSIAN: return "ru"; /* Ambiguous: could be "ru_RU" or "ru_UA" or "ru_MD". */ case LANG_SAAMI: /* actually Northern Sami */ return "se_NO"; case LANG_SANSKRIT: return "sa_IN"; case LANG_SINDHI: switch (sub) { case SUBLANG_SINDHI_INDIA: return "sd_IN"; case SUBLANG_SINDHI_PAKISTAN: return "sd_PK"; } return "sd"; case LANG_SINHALESE: return "si_LK"; case LANG_SLOVAK: return "sk_SK"; case LANG_SLOVENIAN: return "sl_SI"; case LANG_SOMALI: return "so_SO"; case LANG_SORBIAN: /* FIXME: Adjust this when such locales appear on Unix. */ return "wen_DE"; case LANG_SPANISH: switch (sub) { case SUBLANG_SPANISH: return "es_ES"; case SUBLANG_SPANISH_MEXICAN: return "es_MX"; case SUBLANG_SPANISH_MODERN: return "es_ES@modern"; /* not seen on Unix */ case SUBLANG_SPANISH_GUATEMALA: return "es_GT"; case SUBLANG_SPANISH_COSTA_RICA: return "es_CR"; case SUBLANG_SPANISH_PANAMA: return "es_PA"; case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: return "es_DO"; case SUBLANG_SPANISH_VENEZUELA: return "es_VE"; case SUBLANG_SPANISH_COLOMBIA: return "es_CO"; case SUBLANG_SPANISH_PERU: return "es_PE"; case SUBLANG_SPANISH_ARGENTINA: return "es_AR"; case SUBLANG_SPANISH_ECUADOR: return "es_EC"; case SUBLANG_SPANISH_CHILE: return "es_CL"; case SUBLANG_SPANISH_URUGUAY: return "es_UY"; case SUBLANG_SPANISH_PARAGUAY: return "es_PY"; case SUBLANG_SPANISH_BOLIVIA: return "es_BO"; case SUBLANG_SPANISH_EL_SALVADOR: return "es_SV"; case SUBLANG_SPANISH_HONDURAS: return "es_HN"; case SUBLANG_SPANISH_NICARAGUA: return "es_NI"; case SUBLANG_SPANISH_PUERTO_RICO: return "es_PR"; } return "es"; case LANG_SUTU: return "bnt_TZ"; /* or "st_LS" or "nso_ZA"? */ case LANG_SWAHILI: return "sw_KE"; case LANG_SWEDISH: switch (sub) { case SUBLANG_DEFAULT: return "sv_SE"; case SUBLANG_SWEDISH_FINLAND: return "sv_FI"; } return "sv"; case LANG_SYRIAC: return "syr_TR"; /* An extinct language. */ case LANG_TAGALOG: return "tl_PH"; case LANG_TAJIK: return "tg_TJ"; case LANG_TAMAZIGHT: switch (sub) { /* FIXME: Adjust this when Tamazight locales appear on Unix. */ case SUBLANG_TAMAZIGHT_ARABIC: return "ber_MA@arabic"; case SUBLANG_TAMAZIGHT_LATIN: return "ber_MA@latin"; } return "ber_MA"; case LANG_TAMIL: return "ta"; /* Ambiguous: could be "ta_IN" or "ta_LK" or "ta_SG". */ case LANG_TATAR: return "tt_RU"; case LANG_TELUGU: return "te_IN"; case LANG_THAI: return "th_TH"; case LANG_TIBETAN: return "bo_CN"; case LANG_TIGRINYA: switch (sub) { case SUBLANG_TIGRINYA_ETHIOPIA: return "ti_ET"; case SUBLANG_TIGRINYA_ERITREA: return "ti_ER"; } return "ti"; case LANG_TSONGA: return "ts_ZA"; case LANG_TSWANA: return "tn_BW"; case LANG_TURKISH: return "tr_TR"; case LANG_TURKMEN: return "tk_TM"; case LANG_UKRAINIAN: return "uk_UA"; case LANG_URDU: switch (sub) { case SUBLANG_URDU_PAKISTAN: return "ur_PK"; case SUBLANG_URDU_INDIA: return "ur_IN"; } return "ur"; case LANG_UZBEK: switch (sub) { case SUBLANG_UZBEK_LATIN: return "uz_UZ"; case SUBLANG_UZBEK_CYRILLIC: return "uz_UZ@cyrillic"; } return "uz"; case LANG_VENDA: return "ve_ZA"; case LANG_VIETNAMESE: return "vi_VN"; case LANG_WELSH: return "cy_GB"; case LANG_XHOSA: return "xh_ZA"; case LANG_YI: return "sit_CN"; case LANG_YIDDISH: return "yi_IL"; case LANG_YORUBA: return "yo_NG"; case LANG_ZULU: return "zu_ZA"; default: return "C"; } } /* localname.c from gettext END. */ /* Support functions. */ static GPG_ERR_INLINE uint32_t do_swap_u32 (uint32_t i) { return (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24); } #define SWAPIT(flag, data) ((flag) ? do_swap_u32(data) : (data)) /* We assume to have `unsigned long int' value with at least 32 bits. */ #define HASHWORDBITS 32 /* The so called `hashpjw' function by P.J. Weinberger [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools, 1986, 1987 Bell Telephone Laboratories, Inc.] */ static GPG_ERR_INLINE unsigned long hash_string (const char *str_param) { unsigned long int hval, g; const char *str = str_param; hval = 0; while (*str != '\0') { hval <<= 4; hval += (unsigned long int) *str++; g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4)); if (g != 0) { hval ^= g >> (HASHWORDBITS - 8); hval ^= g; } } return hval; } /* Generic message catalog and gettext stuff. */ /* The magic number of the GNU message catalog format. */ #define MAGIC 0x950412de #define MAGIC_SWAPPED 0xde120495 /* Revision number of the currently used .mo (binary) file format. */ #define MO_REVISION_NUMBER 0 /* Header for binary .mo file format. */ struct mo_file_header { /* The magic number. */ uint32_t magic; /* The revision number of the file format. */ uint32_t revision; /* The number of strings pairs. */ uint32_t nstrings; /* Offset of table with start offsets of original strings. */ uint32_t orig_tab_offset; /* Offset of table with start offsets of translation strings. */ uint32_t trans_tab_offset; /* Size of hashing table. */ uint32_t hash_tab_size; /* Offset of first hashing entry. */ uint32_t hash_tab_offset; }; struct string_desc { /* Length of addressed string. */ uint32_t length; /* Offset of string in file. */ uint32_t offset; }; struct overflow_space_s { struct overflow_space_s *next; uint32_t idx; uint32_t length; char d[1]; }; struct loaded_domain { char *data; char *data_native; /* Data mapped to the native version of the string. (Allocated along with DATA). */ int must_swap; uint16_t nstrings; /* Number of strings. */ uint16_t *mapped; /* Array of mapping indicators: 0 := Not mapped (original utf8). 1 := Mapped to native encoding in overflow space. >=2 := Mapped to native encoding. The value gives the length of the mapped string. Because the terminating nul is included in the length and an empty string is not allowed, values are always > 1. */ struct overflow_space_s *overflow_space; struct string_desc *orig_tab; struct string_desc *trans_tab; uint32_t hash_size; uint32_t *hash_tab; }; /* The list of domains we we are aware of. This list is protected by the criticla section DOMAINLIST_ACCESS_CS. */ static struct domainlist_s *domainlist; /* A critical section to guard access to the domainlist. */ static CRITICAL_SECTION domainlist_access_cs; /* The name of the current domain. This is a malloced string. This is a gobal variable which is not thread-safe. */ static char *current_domainname; /* Constructor for this module. This can only be used if we are a DLL. If used as a static lib we can't control the process set; for example it might be used with a main module which is not build with mingw and thus does not know how to call the constructors. */ #ifdef DLL_EXPORT static void module_init (void) _GPG_ERR_CONSTRUCTOR; #endif static void module_init (void) { static int init_done; if (!init_done) { InitializeCriticalSection (&domainlist_access_cs); init_done = 1; } } #if !defined(DLL_EXPORT) || !defined(_GPG_ERR_HAVE_CONSTRUCTOR) void _gpg_w32__init_gettext_module (void) { module_init (); } #endif /* Free the domain data. */ static void free_domain (struct loaded_domain *domain) { struct overflow_space_s *os, *os2; jnlib_free (domain->data); jnlib_free (domain->mapped); for (os = domain->overflow_space; os; os = os2) { os2 = os->next; jnlib_free (os); } jnlib_free (domain); } static struct loaded_domain * load_domain (const char *filename) { HANDLE fh; DWORD size; struct mo_file_header *data = NULL; struct loaded_domain *domain = NULL; size_t to_read; char *read_ptr; fh = CreateFileA (filename, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (fh == INVALID_HANDLE_VALUE) return NULL; size = GetFileSize (fh, NULL); if (size == INVALID_FILE_SIZE) { CloseHandle (fh); return NULL; } data = (2*size <= size)? NULL : jnlib_malloc (2*size); if (!data) { CloseHandle (fh); return NULL; } to_read = size; read_ptr = (char *) data; do { BOOL res; DWORD nb; res = ReadFile (fh, read_ptr, to_read, &nb, NULL); if (! res || nb < to_read) { CloseHandle (fh); jnlib_free (data); return NULL; } read_ptr += nb; to_read -= nb; } while (to_read > 0); CloseHandle (fh); /* Using the magic number we can test whether it really is a message catalog file. */ if (data->magic != MAGIC && data->magic != MAGIC_SWAPPED) { /* The magic number is wrong: not a message catalog file. */ jnlib_free (data); return NULL; } domain = jnlib_calloc (1, sizeof *domain); if (!domain) { jnlib_free (data); return NULL; } domain->data = (char *) data; domain->data_native = (char *) data + size; domain->must_swap = data->magic != MAGIC; /* Fill in the information about the available tables. */ switch (SWAPIT (domain->must_swap, data->revision)) { case MO_REVISION_NUMBER: { uint32_t nstrings; /* Because we use 16 bit values for the mapping array, we can't support more that 65534 strings (65535 would be okay, but it is often used as a special value). A PO file with that many translations is very unlikely given that GnuPG with its very large number of strings has only about 1600 strings + variants. */ nstrings = SWAPIT (domain->must_swap, data->nstrings); if (nstrings > 65534) goto bailout; domain->nstrings = nstrings; domain->orig_tab = (struct string_desc *) ((char *) data + SWAPIT (domain->must_swap, data->orig_tab_offset)); domain->trans_tab = (struct string_desc *) ((char *) data + SWAPIT (domain->must_swap, data->trans_tab_offset)); domain->hash_size = SWAPIT (domain->must_swap, data->hash_tab_size); domain->hash_tab = (uint32_t *) ((char *) data + SWAPIT (domain->must_swap, data->hash_tab_offset)); } break; default: /* This is an invalid revision. */ goto bailout; } /* Allocate an array to keep track of code page mappings. */ domain->mapped = jnlib_calloc (domain->nstrings, sizeof *domain->mapped); if (domain->mapped) return domain; /* Okay. */ bailout: jnlib_free (data); jnlib_free (domain); return NULL; } /* Return a malloced wide char string from an UTF-8 encoded input string STRING. Caller must free this value. On failure returns NULL. The result of calling this function with STRING set to NULL - is not defined. */ + is not defined. If LENGTH is zero and RETLEN NULL the fucntion + assumes that STRING is a nul-terminated string and returns a + (wchar_t)0-terminated string. */ static wchar_t * utf8_to_wchar (const char *string, size_t length, size_t *retlen) { int n; wchar_t *result; size_t nbytes; + int cbmultibyte; + + if (!length && !retlen) + cbmultibyte = -1; + else + cbmultibyte = length; - n = MultiByteToWideChar (CP_UTF8, 0, string, length, NULL, 0); + n = MultiByteToWideChar (CP_UTF8, 0, string, cbmultibyte, NULL, 0); if (n < 0 || (n+1) <= 0) return NULL; nbytes = (size_t)(n+1) * sizeof(*result); if (nbytes / sizeof(*result) != (n+1)) { gpg_err_set_errno (ENOMEM); return NULL; } result = jnlib_malloc (nbytes); if (!result) return NULL; - n = MultiByteToWideChar (CP_UTF8, 0, string, length, result, n); + n = MultiByteToWideChar (CP_UTF8, 0, string, cbmultibyte, result, n); if (n < 0) { jnlib_free (result); return NULL; } - *retlen = n; + if (retlen) + *retlen = n; return result; } +/* Convert an UTF8 string to a WCHAR string. Caller should use + * _gpgrt_free_wchar to release the result. */ +wchar_t * +_gpgrt_utf8_to_wchar (const char *string) +{ + return utf8_to_wchar (string, 0, NULL); +} + + +/* We provide a dedicated release function to be sure that we don't + * use a somehow mapped free function but the one which matches the + * used alloc. */ +void +_gpgrt_free_wchar (wchar_t *wstring) +{ + if (wstring) + jnlib_free (wstring); +} + + /* Return a malloced string encoded in the native console codepage from the wide char input string STRING. Caller must free this value. On failure returns NULL. The result of calling this function with STRING set to NULL is not defined. */ static char * wchar_to_native (const wchar_t *string, size_t length, size_t *retlen) { int n; char *result; unsigned int cpno = GetConsoleOutputCP (); /* GetConsoleOutputCP returns the 8-Bit codepage that should be used for console output. If the codepage is not returned we fall back to the codepage GUI programs should use (CP_ACP). */ if (!cpno) cpno = GetACP (); n = WideCharToMultiByte (cpno, 0, string, length, NULL, 0, NULL, NULL); if (n < 0 || (n+1) <= 0) return NULL; result = jnlib_malloc (n+1); if (!result) return NULL; n = WideCharToMultiByte (cpno, 0, string, length, result, n, NULL, NULL); if (n < 0) { jnlib_free (result); return NULL; } *retlen = n; return result; } /* Convert UTF8 to the native codepage. Caller must free the return value. */ static char * utf8_to_native (const char *string, size_t length, size_t *retlen) { wchar_t *wstring; char *result; size_t newlen; wstring = utf8_to_wchar (string, length, &newlen); if (wstring) { result = wchar_to_native (wstring, newlen, &newlen); jnlib_free (wstring); } else result = NULL; *retlen = result? newlen : 0; return result; } - /* Specify that the DOMAINNAME message catalog will be found in DIRNAME rather than in the system locale data base. */ const char * _gpg_w32_bindtextdomain (const char *domainname, const char *dirname) { const char *catval_full; char *catval; char *fname; const char *retvalue; if (!dirname) { struct domainlist_s *dl; retvalue = NULL; EnterCriticalSection (&domainlist_access_cs); { for (dl = domainlist; dl; dl = dl->next) if (!strcmp (dl->name, domainname)) { retvalue = dl->dname; break; } } LeaveCriticalSection (&domainlist_access_cs); return retvalue; } /* DIRNAME is "$INSTALLDIR\share\locale". */ /* First find out the category value. */ catval = NULL; catval_full = my_nl_locale_name ("LC_MESSAGES"); /* Normally we would have to loop over all returned locales and search for the right file. See gettext intl/dcigettext.c for all the gory details. Here, we only support the basic category, and ignore everything else. */ if (catval_full) { char *p; catval = jnlib_malloc (strlen (catval_full) + 1); if (catval) { strcpy (catval, catval_full); p = strchr (catval, '_'); if (p) *p = '\0'; } } if (!catval) return NULL; /* Now build the filename string. The complete filename is this: DIRNAME + \ + CATVAL + \LC_MESSAGES\ + DOMAINNAME + .mo */ { int len = (strlen (dirname) + 1 + strlen (catval) + 13 + strlen (domainname) + 3 + 1); char *p; fname = jnlib_malloc (len); if (!fname) { jnlib_free (catval); return NULL; } p = fname; strcpy (p, dirname); p += strlen (dirname); *(p++) = '\\'; strcpy (p, catval); p += strlen (catval); strcpy (p, "\\LC_MESSAGES\\"); p += 13; strcpy (p, domainname); p += strlen (domainname); strcpy (p, ".mo"); } jnlib_free (catval); /* Store the domain information in the domainlist. */ { struct domainlist_s *item, *dl; char *rel_ptr1 = NULL; char *rel_ptr2 = NULL; item = jnlib_calloc (1, sizeof *dl + strlen (domainname)); if (!item) { jnlib_free (fname); return NULL; } strcpy (item->name, domainname); item->dname = jnlib_malloc (strlen (dirname) +1); if(!item->dname) { jnlib_free (item); jnlib_free (fname); return NULL; } strcpy (item->dname, dirname); retvalue = item->dname; EnterCriticalSection (&domainlist_access_cs); { for (dl = domainlist; dl; dl = dl->next) if (!strcmp (dl->name, domainname)) break; if (!dl) /* First time called for this domainname. */ { item->fname = fname; fname = NULL; item->next = domainlist; domainlist = item; item = NULL; } else /* Update only. */ { rel_ptr1 = dl->fname; dl->fname = fname; fname = NULL; rel_ptr2 = dl->dname; dl->dname = item->dname; item->dname = NULL; } } LeaveCriticalSection (&domainlist_access_cs); jnlib_free (item); jnlib_free (rel_ptr1); jnlib_free (rel_ptr2); } return retvalue; } static const char * get_plural (const char *data, size_t datalen, unsigned long nplural) { const char *p; int idx; /* We only support the Germanic rule. */ idx = (nplural == 1? 0 : 1); for (; idx; idx--) { p = strchr (data, 0) + 1; if (p >= data+datalen) return "ERROR in GETTEXT (bad plural entry)"; datalen -= (p-data); data = p; } return data; } static const char* get_string (struct loaded_domain *domain, uint32_t idx, int use_plural, unsigned long nplural) { struct tls_space_s *tls = get_tls (); struct overflow_space_s *os; const char *trans; /* Pointer to the translated entry. */ size_t translen; /* Length of that entry. */ if (idx > 65534) return "ERROR in GETTEXT (too many strings)"; if (tls->gt_use_utf8) { trans = (domain->data + SWAPIT(domain->must_swap, domain->trans_tab[idx].offset)); translen = SWAPIT(domain->must_swap, domain->trans_tab[idx].length); } else if (!domain->mapped[idx]) { /* Not yet mapped. Map from utf-8 to native encoding now. */ const char *p_utf8; size_t plen_utf8, buflen; char *buf; p_utf8 = (domain->data + SWAPIT(domain->must_swap, domain->trans_tab[idx].offset)); plen_utf8 = SWAPIT(domain->must_swap, domain->trans_tab[idx].length); /* We need to include the nul, so that the utf8->wchar->native conversion chain works correctly and the nul is stored after the conversion. */ if (p_utf8[plen_utf8]) { trans = "ERROR in MO file"; /* Terminating zero is missing. */ translen = 0; goto leave; } plen_utf8++; buf = utf8_to_native (p_utf8, plen_utf8, &buflen); if (!buf) { trans = "ERROR in GETTEXT MALLOC"; translen = 0; } else if (buflen <= plen_utf8 && buflen > 1) { /* Copy into the DATA_NATIVE area. */ char *p_tmp; p_tmp = (domain->data_native + SWAPIT(domain->must_swap, domain->trans_tab[idx].offset)); memcpy (p_tmp, buf, buflen); domain->mapped[idx] = buflen; trans = p_tmp; translen = buflen; } else { /* There is not enough space for the translation (or for whatever reason an empty string is used): Store it in the overflow_space and mark that in the mapped array. Because UTF-8 strings are in general longer than the Windows native encoding, we expect that this won't happen too often and thus we use a linked list to manage this space. */ os = jnlib_malloc (sizeof *os + buflen); if (os) { os->idx = idx; memcpy (os->d, buf, buflen); os->length = buflen; os->next = domain->overflow_space; domain->overflow_space = os; domain->mapped[idx] = 1; trans = os->d; translen = os->length; } else { trans = "ERROR in GETTEXT MALLOC"; translen = 0; } } if (translen) translen--; /* TRANSLEN shall be the size without the nul. */ jnlib_free (buf); } else if (domain->mapped[idx] == 1) { /* The translated string is in the overflow_space. */ for (os=domain->overflow_space; os; os = os->next) if (os->idx == idx) break; if (os) { trans = os->d; translen = os->length; } else { trans = "ERROR in GETTEXT (overflow space)\n"; translen = 0; } } else { trans = (domain->data_native + SWAPIT(domain->must_swap, domain->trans_tab[idx].offset)); translen = domain->mapped[idx]; } leave: if (use_plural && translen) return get_plural (trans, translen, nplural); else return trans; } static const char * do_gettext (const char *domainname, const char *msgid, const char *msgid2, unsigned long nplural) { struct domainlist_s *dl; struct loaded_domain *domain; int load_failed; uint32_t top, bottom, nstr; char *filename; if (!domainname) domainname = current_domainname? current_domainname : ""; /* FIXME: The whole locking stuff is a bit questionable because gettext does not claim to be thread-safe. We need to investigate this further. */ load_failed = 0; domain = NULL; filename = NULL; EnterCriticalSection (&domainlist_access_cs); { for (dl = domainlist; dl; dl = dl->next) if (!strcmp (dl->name, domainname)) { load_failed = dl->load_failed; domain = dl->domain; break; } if (dl && !domain && !load_failed && dl->fname) { filename = jnlib_malloc (strlen (dl->fname) + 1); if (filename) strcpy (filename, dl->fname); } } LeaveCriticalSection (&domainlist_access_cs); if (!dl) goto not_found; /* DOMAINNAME not bound. */ if (filename) { /* No attempt so far to load the MO file. Try now. */ int updated = 0; domain = load_domain (filename); jnlib_free (filename); filename = NULL; EnterCriticalSection (&domainlist_access_cs); { for (dl = domainlist; dl; dl = dl->next) if (!strcmp (dl->name, domainname)) { if (domain) dl->domain = domain; else dl->load_failed = 1; updated = 1; break; } } LeaveCriticalSection (&domainlist_access_cs); if (!updated) { /* Ooops - lost the domain. */ free_domain (domain); domain = NULL; } } if (!domain) goto not_found; /* No MO file. */ /* First try to use the hash table. */ if (domain->hash_size > 2 && domain->hash_tab) { /* Use the hashing table. */ uint32_t len = strlen (msgid); uint32_t hash_val = hash_string (msgid); uint32_t idx = hash_val % domain->hash_size; uint32_t incr = 1 + (hash_val % (domain->hash_size - 2)); while ( (nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx])) ) { nstr--; if (nstr < domain->nstrings && SWAPIT(domain->must_swap, domain->orig_tab[nstr].length) >= len && !strcmp (msgid, (domain->data + SWAPIT(domain->must_swap, domain->orig_tab[nstr].offset)))) { return get_string (domain, nstr, !!msgid2, nplural); } if (idx >= domain->hash_size - incr) idx -= domain->hash_size - incr; else idx += incr; } } /* Now we try the default method: binary search in the sorted array of messages. */ bottom = 0; top = domain->nstrings; while (bottom < top) { int cmp_val; nstr = (bottom + top) / 2; cmp_val = strcmp (msgid, (domain->data + SWAPIT(domain->must_swap, domain->orig_tab[nstr].offset))); if (cmp_val < 0) top = nstr; else if (cmp_val > 0) bottom = nstr + 1; else { return get_string (domain, nstr, !!msgid2, nplural); } } not_found: /* We use the standard Germanic rule if plural has been requested. */ return msgid2? (nplural == 1? msgid : msgid2) : msgid; } const char * _gpg_w32_textdomain (const char *domainname) { char *string; if (!domainname) { if (!current_domainname) gpg_err_set_errno (0); } else { string = malloc (strlen (domainname) + 1); if (!string) return NULL; strcpy (string, domainname); current_domainname = string; } return current_domainname; } /* A direct implementation of gettext instead of a macro calling dngettext. This is so that the caller does not need to push dummy values on the stack. The used domain is the first one registered with bindtextdomain. */ const char * _gpg_w32_gettext (const char *msgid) { return do_gettext (NULL, msgid, NULL, 0); } /* A direct implementation of dgettext instead of a macro calling dngettext. This is so that the caller does not need to push dummy values on the stack. */ const char * _gpg_w32_dgettext (const char *domainname, const char *msgid) { return do_gettext (domainname, msgid, NULL, 0); } /* Our implementation of dngettext. This is the most genereic function we have; a macro implements ngettext. */ const char * _gpg_w32_dngettext (const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n) { /* We use the simple Germanic plural rule. */ return do_gettext (domainname, msgid1, msgid2, n); } /* Return the locale name as used by gettext. The return value will never be NULL. */ const char * _gpg_w32_gettext_localename (void) { const char *s; s = my_nl_locale_name ("LC_MESSAGES"); return s? s:""; } /* With a VALUE of 1 switch the gettext functions into utf8 mode. That is the strings are returned without translation to the native charset. A VALUE of 0 switches back to translated strings. A VALUE of -1 returns the current value. */ int _gpg_w32_gettext_use_utf8 (int value) { struct tls_space_s *tls = get_tls (); int last = tls->gt_use_utf8; if (value != -1) tls->gt_use_utf8 = value; return last; } /* Force the use of the locale NAME or if NAME is NULL the locale * derived from LANGID will be used. This function is not thread-safe * and must be used early - even before gpgrt_check_version. */ void gpgrt_w32_override_locale (const char *name, unsigned short langid) { if (name) { strncpy (override_locale.name, name, sizeof (override_locale.name) - 1); override_locale.name[sizeof (override_locale.name) - 1] = 0; } else *override_locale.name = 0; override_locale.langid = langid; override_locale.active = 1; } #ifdef TEST int main (int argc, char **argv) { const char atext1[] = "Warning: You have entered an insecure passphrase.%%0A" "A passphrase should be at least %u character long."; const char atext2[] = "Warning: You have entered an insecure passphrase.%%0A" "A passphrase should be at least %u characters long."; if (argc) { argc--; argv++; } _gpg_err_w32_bindtextdomain ("gnupg2", "c:/programme/gnu/gnupg/share/locale"); printf ("locale is `%s'\n", _gpg_err_w32_gettext_localename ()); fputs ("text with N=1:\n", stdout); fputs (_gpg_err_w32_ngettext (atext1, atext2, 1), stdout); fputs ("\n\ntext with N=2:\n", stdout); fputs (_gpg_err_w32_ngettext (atext1, atext2, 2), stdout); fputs ("\nready\n", stdout); return 0; } /* * Local Variables: * compile-command: "i586-mingw32msvc-gcc -DTEST -Wall -g w32-gettext.c" * End: */ #endif /*TEST*/