1
0
Fork 0
mirror of git://git.gnupg.org/gnupg.git synced 2025-07-03 22:56:33 +02:00

First steps towards supporting W32.

This is mainly source code reorganization.
Update gnulib.
g10/ does currently not build.
This commit is contained in:
Werner Koch 2007-06-06 18:12:30 +00:00
parent c2b08ff908
commit 2c9791db55
116 changed files with 2722 additions and 5565 deletions

View file

@ -1,3 +1,40 @@
2007-06-06 Werner Koch <wk@g10code.com>
* Makefile.am: Include am/cmacros.am.
* sysutils.h [W32]: Remove prototypes for the registry access.
* w32reg.c: Move to ../jnlib/w32-reg.c.
* i18n.c (i18n_init): New.
* simple-gettext.c: Remove.
* iobuf.c (iobuf_get_filelength): Rename SIZE to EXSIZE to silent
shadowing warning.
2007-06-04 Werner Koch <wk@g10code.com>
* http.c [W32]: Include unistd.h also in this case.
(write_server) [W32]: Fixed error code.
(init_sockets): Fixed syntax error.
(cookie_close): Replace close by sock_close macro.
* estream.c [w32]: Do not init Mutex.
* Makefile.am (common_sources) [USE_SNS_SRV]: Build srv.c only
when needed.
* ttyio.c (init_ttyfp) [W32]: Do not use TTYFP.
* util.h: Include ../jnlib/dynload.h.
* dynload.h: Move to ../jnlib.
2007-05-30 Werner Koch <wk@g10code.com>
* estream.c (MEM_FREE, MEM_ALLOC, MEM_REALLOC): Prefix with ES_ as
windows.h also has such definitions,
2007-05-15 Werner Koch <wk@g10code.com>
* util.h: Do not include gnulib's vasprintf. Redefine asprintf

View file

@ -28,9 +28,11 @@ AM_CPPFLAGS = -I$(top_srcdir)/gl -I$(top_srcdir)/intl
AM_CFLAGS = $(LIBGCRYPT_CFLAGS) $(KSBA_CFLAGS)
include $(top_srcdir)/am/cmacros.am
common_sources = \
common-defs.h \
util.h i18n.h \
util.h i18n.c i18n.h \
errors.h \
openpgpdefs.h \
keyserver.h \
@ -50,20 +52,23 @@ common_sources = \
ttyio.c ttyio.h \
asshelp.c asshelp.h \
exechelp.c exechelp.h \
simple-gettext.c \
w32reg.c \
signal.c \
dynload.h \
estream.c estream.h estream-printf.c estream-printf.h \
srv.c srv.h \
srv.h \
dns-cert.c dns-cert.h \
pka.c pka.h \
http.c http.h
libcommon_a_SOURCES = $(common_sources)
if USE_DNS_SRV
libcommon_a_SOURCES += srv.c
endif
libcommon_a_CFLAGS = $(AM_CFLAGS) $(LIBASSUAN_CFLAGS) -DWITHOUT_GNU_PTH=1
libcommonpth_a_SOURCES = $(common_sources)
if USE_DNS_SRV
libcommonpth_a_SOURCES += srv.c
endif
libcommonpth_a_CFLAGS = $(AM_CFLAGS) $(LIBASSUAN_PTH_CFLAGS) $(PTH_CFLAGS)
libsimple_pwquery_a_SOURCES = \

View file

@ -1,72 +0,0 @@
/* dlfcn.h - W32 functions for run-time dynamic loading
* Copyright (C) 2003 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GnuPG 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#ifndef GNUPG_DYNLOAD_H
#define GNUPG_DYNLOAD_H
#ifndef __MINGW32__
#include <dlfcn.h>
#else
#include <windows.h>
#define RTLD_LAZY 0
static inline void *
dlopen (const char * name, int flag)
{
void * hd = LoadLibrary (name);
return hd;
}
static inline void *
dlsym (void *hd, const char *sym)
{
if (hd && sym)
{
void * fnc = GetProcAddress (hd, sym);
if (!fnc)
return NULL;
return fnc;
}
return NULL;
}
static inline const char *
dlerror (void)
{
static char buf[32];
sprintf (buf, "ec=%lu", GetLastError ());
return buf;
}
static inline int
dlclose (void * hd)
{
if (hd)
{
CloseHandle (hd);
return 0;
}
return -1;
}
#endif /*__MINGW32__*/
#endif /*GNUPG_DYNLOAD_H*/

View file

@ -125,9 +125,9 @@ typedef void *estream_mutex_t;
/* Memory allocator functions. */
#define MEM_ALLOC malloc
#define MEM_REALLOC realloc
#define MEM_FREE free
#define ES_MEM_ALLOC malloc
#define ES_MEM_REALLOC realloc
#define ES_MEM_FREE free
/* Primitive system I/O. */
@ -194,7 +194,13 @@ struct estream_list
static estream_list_t estream_list;
#ifdef HAVE_PTH
static estream_mutex_t estream_list_lock = ESTREAM_MUTEX_INITIALIZER;
/* Note that we can't use a static initialization with W32Pth; however
W32Pth does an implicit initialization anyway. */
static estream_mutex_t estream_list_lock
# ifndef _W32_PTH_H
= ESTREAM_MUTEX_INITIALIZER
# endif
;
#endif
#define ESTREAM_LIST_LOCK ESTREAM_MUTEX_LOCK (estream_list_lock)
@ -236,7 +242,7 @@ es_list_add (estream_t stream)
estream_list_t list_obj;
int ret;
list_obj = MEM_ALLOC (sizeof (*list_obj));
list_obj = ES_MEM_ALLOC (sizeof (*list_obj));
if (! list_obj)
ret = -1;
else
@ -268,7 +274,7 @@ es_list_remove (estream_t stream)
*list_obj->prev_cdr = list_obj->cdr;
if (list_obj->cdr)
list_obj->cdr->prev_cdr = list_obj->prev_cdr;
MEM_FREE (list_obj);
ES_MEM_FREE (list_obj);
break;
}
ESTREAM_LIST_UNLOCK;
@ -349,7 +355,7 @@ es_func_mem_create (void *ES__RESTRICT *ES__RESTRICT cookie,
estream_cookie_mem_t mem_cookie;
int err;
mem_cookie = MEM_ALLOC (sizeof (*mem_cookie));
mem_cookie = ES_MEM_ALLOC (sizeof (*mem_cookie));
if (! mem_cookie)
err = -1;
else
@ -365,8 +371,8 @@ es_func_mem_create (void *ES__RESTRICT *ES__RESTRICT cookie,
mem_cookie->dont_free = dont_free ? 1 : 0;
mem_cookie->ptr = ptr;
mem_cookie->size = size;
mem_cookie->func_realloc = func_realloc ? func_realloc : MEM_REALLOC;
mem_cookie->func_free = func_free ? func_free : MEM_FREE;
mem_cookie->func_realloc = func_realloc ? func_realloc : ES_MEM_REALLOC;
mem_cookie->func_free = func_free ? func_free : ES_MEM_FREE;
mem_cookie->offset = 0;
*cookie = mem_cookie;
err = 0;
@ -575,7 +581,7 @@ es_func_mem_destroy (void *cookie)
if (! mem_cookie->dont_free)
(*func_free) (mem_cookie->memory);
MEM_FREE (mem_cookie);
ES_MEM_FREE (mem_cookie);
return 0;
}
@ -603,7 +609,7 @@ es_func_fd_create (void **cookie, int fd, unsigned int flags)
estream_cookie_fd_t fd_cookie;
int err;
fd_cookie = MEM_ALLOC (sizeof (*fd_cookie));
fd_cookie = ES_MEM_ALLOC (sizeof (*fd_cookie));
if (! fd_cookie)
err = -1;
else
@ -676,7 +682,7 @@ es_func_fd_destroy (void *cookie)
if (fd_cookie)
{
err = close (fd_cookie->fd);
MEM_FREE (fd_cookie);
ES_MEM_FREE (fd_cookie);
}
else
err = 0;
@ -706,7 +712,7 @@ es_func_file_create (void **cookie, int *filedes,
err = 0;
fd = -1;
file_cookie = MEM_ALLOC (sizeof (*file_cookie));
file_cookie = ES_MEM_ALLOC (sizeof (*file_cookie));
if (! file_cookie)
{
err = -1;
@ -727,7 +733,7 @@ es_func_file_create (void **cookie, int *filedes,
out:
if (err)
MEM_FREE (file_cookie);
ES_MEM_FREE (file_cookie);
return err;
}
@ -1004,14 +1010,14 @@ es_create (estream_t *stream, void *cookie, int fd,
stream_new = NULL;
stream_internal_new = NULL;
stream_new = MEM_ALLOC (sizeof (*stream_new));
stream_new = ES_MEM_ALLOC (sizeof (*stream_new));
if (! stream_new)
{
err = -1;
goto out;
}
stream_internal_new = MEM_ALLOC (sizeof (*stream_internal_new));
stream_internal_new = ES_MEM_ALLOC (sizeof (*stream_internal_new));
if (! stream_internal_new)
{
err = -1;
@ -1040,7 +1046,7 @@ es_create (estream_t *stream, void *cookie, int fd,
if (stream_new)
{
es_deinitialize (stream_new);
MEM_FREE (stream_new);
ES_MEM_FREE (stream_new);
}
}
@ -1057,8 +1063,8 @@ es_destroy (estream_t stream)
{
es_list_remove (stream);
err = es_deinitialize (stream);
MEM_FREE (stream->intern);
MEM_FREE (stream);
ES_MEM_FREE (stream->intern);
ES_MEM_FREE (stream);
}
return err;
@ -1567,7 +1573,7 @@ doreadline (estream_t ES__RESTRICT stream, size_t max_length,
line_stream_cookie = NULL;
err = es_func_mem_create (&line_stream_cookie, NULL, 0, 0, BUFFER_BLOCK_SIZE,
1, 0, 0, NULL, 0, MEM_REALLOC, MEM_FREE, O_RDWR);
1, 0, 0, NULL, 0, ES_MEM_REALLOC, ES_MEM_FREE, O_RDWR);
if (err)
goto out;
@ -1633,7 +1639,7 @@ doreadline (estream_t ES__RESTRICT stream, size_t max_length,
if (! *line)
{
line_new = MEM_ALLOC (line_size + 1);
line_new = ES_MEM_ALLOC (line_size + 1);
if (! line_new)
{
err = -1;
@ -1664,7 +1670,7 @@ doreadline (estream_t ES__RESTRICT stream, size_t max_length,
if (err)
{
if (! *line)
MEM_FREE (line_new);
ES_MEM_FREE (line_new);
stream->intern->indicators.err = 1;
}
@ -1748,7 +1754,7 @@ es_set_buffering (estream_t ES__RESTRICT stream,
if (stream->intern->deallocate_buffer)
{
stream->intern->deallocate_buffer = 0;
MEM_FREE (stream->buffer);
ES_MEM_FREE (stream->buffer);
stream->buffer = NULL;
}
@ -1762,7 +1768,7 @@ es_set_buffering (estream_t ES__RESTRICT stream,
buffer_new = buffer;
else
{
buffer_new = MEM_ALLOC (size);
buffer_new = ES_MEM_ALLOC (size);
if (! buffer_new)
{
err = -1;
@ -1921,7 +1927,7 @@ es_open_memstream (char **ptr, size_t *size)
err = es_func_mem_create (&cookie, NULL, 0, 0,
BUFFER_BLOCK_SIZE, 1, 1, 1,
ptr, size, MEM_REALLOC, MEM_FREE, flags);
ptr, size, ES_MEM_REALLOC, ES_MEM_FREE, flags);
if (err)
goto out;
@ -2454,7 +2460,7 @@ es_getline (char *ES__RESTRICT *ES__RESTRICT lineptr, size_t *ES__RESTRICT n,
void *p;
p = MEM_REALLOC (*lineptr, line_n + 1);
p = ES_MEM_REALLOC (*lineptr, line_n + 1);
if (! p)
err = -1;
else
@ -2470,7 +2476,7 @@ es_getline (char *ES__RESTRICT *ES__RESTRICT lineptr, size_t *ES__RESTRICT n,
if (*n != line_n)
*n = line_n;
}
MEM_FREE (line);
ES_MEM_FREE (line);
}
else
{
@ -2530,7 +2536,7 @@ es_read_line (estream_t stream,
{
/* No buffer given - allocate a new one. */
length = 256;
buffer = MEM_ALLOC (length);
buffer = ES_MEM_ALLOC (length);
*addr_of_buffer = buffer;
if (!buffer)
{
@ -2571,11 +2577,11 @@ es_read_line (estream_t stream,
}
length += 3; /* Adjust for the reserved bytes. */
length += length < 1024? 256 : 1024;
*addr_of_buffer = MEM_REALLOC (buffer, length);
*addr_of_buffer = ES_MEM_REALLOC (buffer, length);
if (!*addr_of_buffer)
{
int save_errno = errno;
MEM_FREE (buffer);
ES_MEM_FREE (buffer);
*length_of_buffer = *max_length = 0;
ESTREAM_UNLOCK (stream);
errno = save_errno;
@ -2604,7 +2610,7 @@ void
es_free (void *a)
{
if (a)
MEM_FREE (a);
ES_MEM_FREE (a);
}

View file

@ -41,11 +41,11 @@
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#ifdef HAVE_W32_SYSTEM
# include <windows.h>
#else /*!HAVE_W32_SYSTEM*/
# include <unistd.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <sys/time.h>
@ -236,7 +236,7 @@ init_sockets (void)
|| HIBYTE(wsdata.wVersion) != REQ_WINSOCK_MINOR )
{
log_error ("socket library version is %x.%x - but %d.%d needed\n",
LOBYTE(wsdata.wVersion), HIBYTE(wsdata.wVersion)
LOBYTE(wsdata.wVersion), HIBYTE(wsdata.wVersion),
REQ_WINSOCK_MAJOR, REQ_WINSOCK_MINOR);
WSACleanup();
return;
@ -1693,7 +1693,7 @@ write_server (int sock, const char *data, size_t length)
if ( nwritten == SOCKET_ERROR )
{
log_info ("network write failed: ec=%d\n", (int)WSAGetLastError ());
return G10ERR_NETWORK;
return gpg_error (GPG_ERR_NETWORK);
}
#else /*!HAVE_W32_SYSTEM*/
int nwritten = write (sock, data, nleft);
@ -1829,14 +1829,14 @@ cookie_close (void *cookie)
if (!c)
return 0;
#ifdef HTTP_USE_GNUTLS
#ifdef HTTP_USE_GNUTLS
if (c->tls_session && !c->keep_socket)
{
gnutls_bye (c->tls_session, GNUTLS_SHUT_RDWR);
}
#endif /*HTTP_USE_GNUTLS*/
if (c->fd != -1 && !c->keep_socket)
close (c->fd);
sock_close (c->fd);
xfree (c);
return 0;

39
common/i18n.c Normal file
View file

@ -0,0 +1,39 @@
/* i18n.c - gettext initialization
* Copyright (C) 2007 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GnuPG 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include <config.h>
#include "i18n.h"
void
i18n_init (void)
{
#ifdef USE_SIMPLE_GETTEXT
set_gettext_file (PACKAGE_GT, "Software\\GNU\\GnuPG");
#else
# ifdef ENABLE_NLS
setlocale (LC_ALL, "" );
bindtextdomain (PACKAGE_GT, LOCALEDIR);
textdomain (PACKAGE_GT);
# endif
#endif
}

View file

@ -14,11 +14,9 @@
#ifndef GNUPG_COMMON_I18N_H
#define GNUPG_COMMON_I18N_H
#ifdef USE_SIMPLE_GETTEXT
int set_gettext_file( const char *filename );
const char *gettext( const char *msgid );
const char *ngettext(const char *msgid1, const char *msgid2,
unsigned long int n);
# include "../jnlib/w32help.h"
# define _(a) gettext (a)
# define N_(a) (a)
#else
@ -40,4 +38,7 @@
# endif
#endif /*!USE_SIMPLE_GETTEXT*/
void i18n_init (void);
#endif /*GNUPG_COMMON_I18N_H*/

View file

@ -271,7 +271,7 @@ fd_cache_close (const char *fname, FILEP_OR_FD fp)
close (fp);
#endif
if (DBG_IOBUF)
log_debug ("fd_cache_close (%d) real\n", fp);
log_debug ("fd_cache_close (%d) real\n", (int)fp);
return;
}
/* try to reuse a slot */
@ -2006,78 +2006,79 @@ iobuf_set_limit (iobuf_t a, off_t nlimit)
off_t
iobuf_get_filelength (iobuf_t a, int *overflow)
{
struct stat st;
struct stat st;
if (overflow)
*overflow = 0;
if( a->directfp ) {
FILE *fp = a->directfp;
if( !fstat(fileno(fp), &st) )
return st.st_size;
log_error("fstat() failed: %s\n", strerror(errno) );
return 0;
if (overflow)
*overflow = 0;
if ( a->directfp )
{
FILE *fp = a->directfp;
if ( !fstat(fileno(fp), &st) )
return st.st_size;
log_error("fstat() failed: %s\n", strerror(errno) );
return 0;
}
/* Hmmm: file_filter may have already been removed */
for( ; a; a = a->chain )
if( !a->chain && a->filter == file_filter ) {
file_filter_ctx_t *b = a->filter_ov;
FILEP_OR_FD fp = b->fp;
/* Hmmm: file_filter may have already been removed */
for ( ; a; a = a->chain )
if ( !a->chain && a->filter == file_filter )
{
file_filter_ctx_t *b = a->filter_ov;
FILEP_OR_FD fp = b->fp;
#if defined(HAVE_DOSISH_SYSTEM) && !defined(FILE_FILTER_USES_STDIO)
ulong size;
static int (* __stdcall get_file_size_ex)
(void *handle, LARGE_INTEGER *size);
static int get_file_size_ex_initialized;
ulong size;
static int (* __stdcall get_file_size_ex) (void *handle,
LARGE_INTEGER *r_size);
static int get_file_size_ex_initialized;
if (!get_file_size_ex_initialized)
if (!get_file_size_ex_initialized)
{
void *handle;
handle = dlopen ("kernel32.dll", RTLD_LAZY);
if (handle)
{
void *handle;
handle = dlopen ("kernel32.dll", RTLD_LAZY);
if (handle)
{
get_file_size_ex = dlsym (handle, "GetFileSizeEx");
if (!get_file_size_ex)
dlclose (handle);
}
get_file_size_ex_initialized = 1;
get_file_size_ex = dlsym (handle, "GetFileSizeEx");
if (!get_file_size_ex)
dlclose (handle);
}
if (get_file_size_ex)
get_file_size_ex_initialized = 1;
}
if (get_file_size_ex)
{
/* This is a newer system with GetFileSizeEx; we use this
then because it seem that GetFileSize won't return a
proper error in case a file is larger than 4GB. */
LARGE_INTEGER exsize;
if (get_file_size_ex (fp, &exsize))
{
/* This is a newer system with GetFileSizeEx; we use
this then becuase it seem that GetFileSize won't
return a proper error in case a file is larger than
4GB. */
LARGE_INTEGER size;
if (get_file_size_ex (fp, &size))
{
if (!size.u.HighPart)
return size.u.LowPart;
if (overflow)
*overflow = 1;
return 0;
}
if (!exsize.u.HighPart)
return exsize.u.LowPart;
if (overflow)
*overflow = 1;
return 0;
}
else
{
if ((size=GetFileSize (fp, NULL)) != 0xffffffff)
return size;
}
log_error ("GetFileSize for handle %p failed: %s\n",
fp, w32_strerror (0));
}
else
{
if ((size=GetFileSize (fp, NULL)) != 0xffffffff)
return size;
}
log_error ("GetFileSize for handle %p failed: %s\n",
fp, w32_strerror (0));
#else
if( !fstat(my_fileno(fp), &st) )
return st.st_size;
log_error("fstat() failed: %s\n", strerror(errno) );
if ( !fstat(my_fileno(fp), &st) )
return st.st_size;
log_error("fstat() failed: %s\n", strerror(errno) );
#endif
break;
}
break/*the for loop*/;
}
return 0;
}

View file

@ -1,447 +0,0 @@
/* simple-gettext.c - a simplified version of gettext.
* Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GnuPG 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
/* This is a simplified version of gettext written by Ulrich Drepper.
* It is used for the Win32 version of GnuPG beucase all the overhead
* of gettext is not needed and we have to do some special Win32 stuff.
* I decided that this is far easier than to tweak gettext for the special
* cases (I tried it but it is a lot of code). wk 15.09.99
*/
#include <config.h>
#ifdef USE_SIMPLE_GETTEXT
#if !defined (_WIN32) && !defined (__CYGWIN32__)
#error This file can only be used under Windows or Cygwin32
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "util.h"
#include "sysutils.h"
/* 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. */
u32 magic;
/* The revision number of the file format. */
u32 revision;
/* The number of strings pairs. */
u32 nstrings;
/* Offset of table with start offsets of original strings. */
u32 orig_tab_offset;
/* Offset of table with start offsets of translation strings. */
u32 trans_tab_offset;
/* Size of hashing table. */
u32 hash_tab_size;
/* Offset of first hashing entry. */
u32 hash_tab_offset;
};
struct string_desc
{
/* Length of addressed string. */
u32 length;
/* Offset of string in file. */
u32 offset;
};
struct overflow_space_s
{
struct overflow_space_s *next;
u32 idx;
char d[1];
};
struct loaded_domain
{
char *data;
int must_swap;
u32 nstrings;
char *mapped; /* 0 = not yet mapped, 1 = mapped,
2 = mapped to
overflow space */
struct overflow_space_s *overflow_space;
struct string_desc *orig_tab;
struct string_desc *trans_tab;
u32 hash_size;
u32 *hash_tab;
};
static struct loaded_domain *the_domain;
static __inline__ u32
do_swap_u32( u32 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 __inline__ ulong
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;
}
static struct loaded_domain *
load_domain( const char *filename )
{
FILE *fp;
size_t size;
struct stat st;
struct mo_file_header *data = NULL;
struct loaded_domain *domain = NULL;
size_t to_read;
char *read_ptr;
fp = fopen( filename, "rb" );
if( !fp )
return NULL; /* can't open the file */
/* we must know about the size of the file */
if( fstat( fileno(fp ), &st )
|| (size = (size_t)st.st_size) != st.st_size
|| size < sizeof (struct mo_file_header) ) {
fclose( fp );
return NULL;
}
data = malloc( size );
if( !data ) {
fclose( fp );
return NULL; /* out of memory */
}
to_read = size;
read_ptr = (char *) data;
do {
long int nb = fread( read_ptr, 1, to_read, fp );
if( nb < to_read ) {
fclose (fp);
free(data);
return NULL; /* read error */
}
read_ptr += nb;
to_read -= nb;
} while( to_read > 0 );
fclose (fp);
/* 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. */
free( data );
return NULL;
}
domain = calloc( 1, sizeof *domain );
if( !domain ) {
free( data );
return NULL;
}
domain->data = (char *) data;
domain->must_swap = data->magic != MAGIC;
/* Fill in the information about the available tables. */
switch( SWAPIT(domain->must_swap, data->revision) ) {
case 0:
domain->nstrings = SWAPIT(domain->must_swap, data->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 = (u32 *)
((char *) data + SWAPIT(domain->must_swap, data->hash_tab_offset));
break;
default: /* This is an invalid revision. */
free( data );
free( domain );
return NULL;
}
/* Allocate an array to keep track of code page mappings. */
domain->mapped = calloc( 1, domain->nstrings );
if( !domain->mapped ) {
free( data );
free( domain );
return NULL;
}
return domain;
}
/****************
* Set the file used for translations. Pass a NULL to disable
* translation. A new filename may be set at anytime.
* WARNING: After changing the filename you should not access any data
* retrieved by gettext().
*/
int
set_gettext_file( const char *filename )
{
struct loaded_domain *domain = NULL;
if( filename && *filename ) {
if( filename[0] == '/'
#ifdef HAVE_DRIVE_LETTERS
|| ( isalpha(filename[0])
&& filename[1] == ':'
&& (filename[2] == '/' || filename[2] == '\\') )
#endif
) {
/* absolute path - use it as is */
domain = load_domain( filename );
}
else { /* relative path - append ".mo" and get dir from the environment */
char *buf = NULL;
char *dir;
char *p;
dir = read_w32_registry_string( NULL,
"Control Panel\\Mingw32\\NLS",
"MODir" );
if( dir && (buf=malloc(strlen(dir)+strlen(filename)+1+3+1)) ) {
strcpy(stpcpy(stpcpy(stpcpy( buf, dir),"\\"), filename),".mo");
/* Better make sure that we don't mix forward and
backward slashes. It seems that some Windoze
versions don't accept this. */
for (p=buf; *p; p++)
{
if (*p == '/')
*p = '\\';
}
domain = load_domain( buf );
free(buf);
}
free(dir);
}
if( !domain )
return -1;
}
if( the_domain ) {
struct overflow_space_s *os, *os2;
free( the_domain->data );
free( the_domain->mapped );
for (os=the_domain->overflow_space; os; os = os2) {
os2 = os->next;
free (os);
}
free( the_domain );
the_domain = NULL;
}
the_domain = domain;
return 0;
}
static const char*
get_string( struct loaded_domain *domain, u32 idx )
{
struct overflow_space_s *os;
char *p;
p = domain->data + SWAPIT(domain->must_swap, domain->trans_tab[idx].offset);
if (!domain->mapped[idx])
{
size_t plen, buflen;
char *buf;
domain->mapped[idx] = 1;
plen = strlen (p);
buf = utf8_to_native (p, plen, -1);
buflen = strlen (buf);
if (buflen <= plen)
strcpy (p, buf);
else
{
/* There is not enough space for the translation - store it
in the overflow_space else and mark that in the mapped
array. Because we expect that this won't happen too
often, we use a simple linked list. */
os = malloc (sizeof *os + buflen);
if (os)
{
os->idx = idx;
strcpy (os->d, buf);
os->next = domain->overflow_space;
domain->overflow_space = os;
p = os->d;
}
else
p = "ERROR in GETTEXT MALLOC";
}
xfree (buf);
}
else if (domain->mapped[idx] == 2)
{ /* We need to get the string from the overflow_space. */
for (os=domain->overflow_space; os; os = os->next)
if (os->idx == idx)
return (const char*)os->d;
p = "ERROR in GETTEXT\n";
}
return (const char*)p;
}
const char *
gettext( const char *msgid )
{
struct loaded_domain *domain;
size_t act = 0;
size_t top, bottom;
if( !(domain = the_domain) )
goto not_found;
/* Locate the MSGID and its translation. */
if( domain->hash_size > 2 && domain->hash_tab ) {
/* Use the hashing table. */
u32 len = strlen (msgid);
u32 hash_val = hash_string (msgid);
u32 idx = hash_val % domain->hash_size;
u32 incr = 1 + (hash_val % (domain->hash_size - 2));
u32 nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx]);
if ( !nstr ) /* Hash table entry is empty. */
goto not_found;
if( SWAPIT(domain->must_swap,
domain->orig_tab[nstr - 1].length) == len
&& !strcmp( msgid,
domain->data + SWAPIT(domain->must_swap,
domain->orig_tab[nstr - 1].offset)) )
return get_string( domain, nstr - 1 );
for(;;) {
if (idx >= domain->hash_size - incr)
idx -= domain->hash_size - incr;
else
idx += incr;
nstr = SWAPIT(domain->must_swap, domain->hash_tab[idx]);
if( !nstr )
goto not_found; /* Hash table entry is empty. */
if ( SWAPIT(domain->must_swap,
domain->orig_tab[nstr - 1].length) == len
&& !strcmp (msgid,
domain->data + SWAPIT(domain->must_swap,
domain->orig_tab[nstr - 1].offset)))
return get_string( domain, nstr-1 );
}
/* NOTREACHED */
}
/* 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;
act = (bottom + top) / 2;
cmp_val = strcmp(msgid, domain->data
+ SWAPIT(domain->must_swap,
domain->orig_tab[act].offset));
if (cmp_val < 0)
top = act;
else if (cmp_val > 0)
bottom = act + 1;
else
return get_string( domain, act );
}
not_found:
return msgid;
}
const char *
ngettext (const char *msgid1, const char *msgid2, unsigned long int n)
{
/* We use the simple Germanic plural rule. */
return gettext (n==1? msgid1 : msgid2);
}
#if 0
unsigned int cp1, cp2;
cp1 = GetConsoleCP();
cp2 = GetConsoleOutputCP();
log_info("InputCP=%u OutputCP=%u\n", cp1, cp2 );
if( !SetConsoleOutputCP( 1252 ) )
log_info("SetConsoleOutputCP failed: %s\n", w32_strerror (0));
cp1 = GetConsoleCP();
cp2 = GetConsoleOutputCP();
log_info("InputCP=%u OutputCP=%u after switch1\n", cp1, cp2 );
#endif
#endif /* USE_SIMPLE_GETTEXT */

View file

@ -23,13 +23,13 @@
#define GNUPG_COMMON_SRV_H
#ifdef USE_DNS_SRV
#ifdef _WIN32
#include <windows.h>
#else
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#endif /* !_WIN32 */
# ifdef _WIN32
# include <windows.h>
# else
# include <netinet/in.h>
# include <arpa/nameser.h>
# include <resolv.h>
# endif /* !_WIN32 */
#endif /* USE_DNS_SRV */

View file

@ -125,7 +125,7 @@ get_session_marker( size_t *rlen )
ulong a, b;
initialized = 1;
/* also this marker is guessable it is not easy to use this
/* Although this marker is guessable it is not easy to use
* for a faked control packet because an attacker does not
* have enough control about the time the verification does
* take place. Of course, we can add just more random but

View file

@ -33,11 +33,7 @@ int check_permissions (const char *path,int extension,int checkonly);
_sleep but non for the still existing sleep. */
#define sleep(a) _sleep ((a))
/*-- w32reg.c --*/
char *read_w32_registry_string( const char *root,
const char *dir, const char *name );
int write_w32_registry_string(const char *root, const char *dir,
const char *name, const char *value);
#include "../jnlib/w32help.h"
#endif /*HAVE_W32_SYSTEM*/

View file

@ -168,6 +168,8 @@ init_ttyfp(void)
#elif defined(__EMX__)
ttyfp = stdout; /* Fixme: replace by the real functions: see wklib */
if (my_rl_init_stream)
my_rl_init_stream (ttyfp);
#else
ttyfp = batchmode? stderr : fopen (tty_get_ttyname (), "r+");
if( !ttyfp ) {
@ -175,10 +177,10 @@ init_ttyfp(void)
strerror(errno) );
exit(2);
}
#endif
if (my_rl_init_stream)
my_rl_init_stream (ttyfp);
#endif
#ifdef HAVE_TCGETATTR
atexit( cleanup );

View file

@ -38,6 +38,7 @@
#include "../jnlib/strlist.h"
#include "../jnlib/dotlock.h"
#include "../jnlib/utf8conv.h"
#include "../jnlib/dynload.h"
/* Redefine asprintf by our estream version which uses our own memory
allocator.. */
@ -204,8 +205,8 @@ int match_multistr (const char *multistr,const char *match);
static inline char *
ttyname (int fd)
{
return NULL
};
return NULL;
}
#endif /* !HAVE_TTYNAME */

View file

@ -1,174 +0,0 @@
/* w32reg.c - MS-Windows Registry access
* Copyright (C) 1999, 2002 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GnuPG 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include <config.h>
#ifdef HAVE_W32_SYSTEM
/* This module is only used in this environment */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <windows.h>
#include "util.h"
#include "sysutils.h"
static HKEY
get_root_key(const char *root)
{
HKEY root_key;
if( !root )
root_key = HKEY_CURRENT_USER;
else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
root_key = HKEY_CLASSES_ROOT;
else if( !strcmp( root, "HKEY_CURRENT_USER" ) )
root_key = HKEY_CURRENT_USER;
else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
root_key = HKEY_LOCAL_MACHINE;
else if( !strcmp( root, "HKEY_USERS" ) )
root_key = HKEY_USERS;
else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
root_key = HKEY_PERFORMANCE_DATA;
else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
root_key = HKEY_CURRENT_CONFIG;
else
return NULL;
return root_key;
}
/****************
* Return a string from the Win32 Registry or NULL in case of
* error. Caller must release the return value. A NULL for root
* is an alias for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn.
* NOTE: The value is allocated with a plain malloc() - use free() and not
* the usual m_free()!!!
*/
char *
read_w32_registry_string( const char *root, const char *dir, const char *name )
{
HKEY root_key, key_handle;
DWORD n1, nbytes, type;
char *result = NULL;
if ( !(root_key = get_root_key(root) ) )
return NULL;
if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
{
if (root)
return NULL; /* no need for a RegClose, so return direct */
/* It seems to be common practise to fall back to HLM. */
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
return NULL; /* still no need for a RegClose, so return direct */
}
nbytes = 1;
if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) )
goto leave;
result = malloc( (n1=nbytes+1) );
if( !result )
goto leave;
if( RegQueryValueEx( key_handle, name, 0, &type, result, &n1 ) ) {
free(result); result = NULL;
goto leave;
}
result[nbytes] = 0; /* make sure it is really a string */
if (type == REG_EXPAND_SZ && strchr (result, '%')) {
char *tmp;
n1 += 1000;
tmp = malloc (n1+1);
if (!tmp)
goto leave;
nbytes = ExpandEnvironmentStrings (result, tmp, n1);
if (nbytes && nbytes > n1) {
free (tmp);
n1 = nbytes;
tmp = malloc (n1 + 1);
if (!tmp)
goto leave;
nbytes = ExpandEnvironmentStrings (result, tmp, n1);
if (nbytes && nbytes > n1) {
free (tmp); /* oops - truncated, better don't expand at all */
goto leave;
}
tmp[nbytes] = 0;
free (result);
result = tmp;
}
else if (nbytes) { /* okay, reduce the length */
tmp[nbytes] = 0;
free (result);
result = malloc (strlen (tmp)+1);
if (!result)
result = tmp;
else {
strcpy (result, tmp);
free (tmp);
}
}
else { /* error - don't expand */
free (tmp);
}
}
leave:
RegCloseKey( key_handle );
return result;
}
int
write_w32_registry_string(const char *root, const char *dir,
const char *name, const char *value)
{
HKEY root_key, reg_key;
if ( !(root_key = get_root_key(root) ) )
return -1;
if ( RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key )
!= ERROR_SUCCESS )
return -1;
if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
strlen( value ) ) != ERROR_SUCCESS ) {
if ( RegCreateKey( root_key, name, &reg_key ) != ERROR_SUCCESS ) {
RegCloseKey(reg_key);
return -1;
}
if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
strlen( value ) ) != ERROR_SUCCESS ) {
RegCloseKey(reg_key);
return -1;
}
}
RegCloseKey( reg_key );
return 0;
}
#endif /*HAVE_W32_SYSTEM*/