Still merging 1.4.3 code back

This commit is contained in:
Werner Koch 2006-04-21 12:56:40 +00:00
parent d0907e64f4
commit 4459fcb032
16 changed files with 246 additions and 165 deletions

View File

@ -1,3 +1,19 @@
2006-04-19 Werner Koch <wk@g10code.com>
* iobuf.c (iobuf_get_fd): New. Taken from 1.4.3.
(iobuf_is_pipe_filename): New.
(pop_filter): Made static.
(iobuf_skip_rest): New. Orginal patch by Florian
Weimer. Added new argument PARTIAL.
(block_filter): Remove the old gpg indeterminate length mode.
(block_filter): Properly handle a partial body stream
that ends with a 5-byte length that happens to be zero.
(iobuf_set_block_mode, iobuf_in_block_mode): Removed as
superfluous.
(iobuf_get_filelength): New arg OVERFLOW.
(iobuf_get_filelength) [W32]: Use GetFileSizeEx if available
* miscellaneous.c (is_file_compressed): Take care of OVERFLOW.
2006-04-18 Werner Koch <wk@g10code.com>
* homedir.c (w32_shgetfolderpath): New. Taken from gpg 1.4.3.

View File

@ -1,5 +1,6 @@
/* iobuf.c - file handling
* Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
* Copyright (C) 1998, 1999, 2000, 2001, 2003,
* 2004, 2006 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
@ -41,6 +42,11 @@
#include "util.h"
#include "iobuf.h"
/* The size of the internal buffers.
NOTE: If you change this value you MUST also adjust the regression
test "armored_key_8192" in armor.test! */
#define IOBUF_BUFFER_SIZE 8192
#undef FILE_FILTER_USES_STDIO
#ifdef HAVE_DOSISH_SYSTEM
@ -762,32 +768,23 @@ block_filter (void *opaque, int control, iobuf_t chain, byte * buffer,
break;
}
a->size |= c;
a->partial = 2;
if (!a->size)
{
a->eof = 1;
if (!n)
rc = -1;
break;
}
}
else
{ /* next partial body length */
{ /* Next partial body length. */
a->size = 1 << (c & 0x1f);
}
/* log_debug("partial: ctx=%p c=%02x size=%u\n", a, c, a->size); */
}
else
{ /* the gnupg partial length scheme - much better :-) */
c = iobuf_get (chain);
a->size = c << 8;
c = iobuf_get (chain);
a->size |= c;
if (c == -1)
{
log_error ("block_filter: error reading length info\n");
rc = GPG_ERR_BAD_DATA;
}
if (!a->size)
{
a->eof = 1;
if (!n)
rc = -1;
break;
}
}
BUG ();
}
while (!rc && size && a->size)
@ -876,39 +873,7 @@ block_filter (void *opaque, int control, iobuf_t chain, byte * buffer,
}
}
else
{ /* the gnupg scheme (which is not openpgp compliant) */
size_t avail, n;
for (p = buf; !rc && size;)
{
n = size;
avail = a->size - a->count;
if (!avail)
{
if (n > a->size)
{
iobuf_put (chain, (a->size >> 8) & 0xff);
iobuf_put (chain, a->size & 0xff);
avail = a->size;
a->count = 0;
}
else
{
iobuf_put (chain, (n >> 8) & 0xff);
iobuf_put (chain, n & 0xff);
avail = n;
a->count = a->size - n;
}
}
if (n > avail)
n = avail;
if (iobuf_write (chain, p, n))
rc = gpg_error_from_errno (errno);
a->count += n;
p += n;
size -= n;
}
}
BUG ();
}
else if (control == IOBUFCTRL_INIT)
{
@ -976,10 +941,7 @@ block_filter (void *opaque, int control, iobuf_t chain, byte * buffer,
a->buflen = 0;
}
else
{
iobuf_writebyte (chain, 0);
iobuf_writebyte (chain, 0);
}
BUG ();
}
else if (a->size)
{
@ -1159,11 +1121,10 @@ iobuf_enable_special_filenames (int yes)
special_names_enabled = yes;
}
/*
* see whether the filename has the for "-&nnnn", where n is a
* non-zero number.
* Returns this number or -1 if it is not the case.
*/
/* See whether the filename has the form "-&nnnn", where n is a
non-zero number. Returns this number or -1 if it is not the
case. */
static int
check_special_filename (const char *fname)
{
@ -1180,6 +1141,17 @@ check_special_filename (const char *fname)
return -1;
}
/* This fucntion returns true if FNAME indicates a PIPE (stdout or
stderr) or a special file name if those are enabled. */
int
iobuf_is_pipe_filename (const char *fname)
{
if (!fname || (*fname=='-' && !fname[1]) )
return 1;
return check_special_filename (fname) != -1;
}
/****************
* Create a head iobuf for reading from a file
* returns: NULL if an error occures and sets errno
@ -1547,7 +1519,7 @@ iobuf_push_filter2 (iobuf_t a,
/****************
* Remove an i/o filter.
*/
int
static int
pop_filter (iobuf_t a, int (*f) (void *opaque, int control,
iobuf_t chain, byte * buf, size_t * len),
void *ov)
@ -2038,49 +2010,110 @@ iobuf_set_limit (iobuf_t a, off_t nlimit)
/****************
* Return the length of an open file
*/
/* Return the length of an open file A. IF OVERFLOW is not NULL it
will be set to true if the file is larger than what off_t can cope
with. The function return 0 on error or on overflow condition. */
off_t
iobuf_get_filelength (iobuf_t a)
iobuf_get_filelength (iobuf_t a, int *overflow)
{
struct stat st;
struct stat st;
if (a->directfp)
{
FILE *fp = a->directfp;
if (overflow)
*overflow = 0;
if (!fstat (fileno (fp), &st))
return st.st_size;
log_error ("fstat() failed: %s\n", strerror (errno));
return 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;
ulong size;
static int (* __stdcall get_file_size_ex)
(void *handle, LARGE_INTEGER *size);
static int get_file_size_ex_initialized;
if ((size = GetFileSize (fp, NULL)) != 0xffffffff)
return size;
log_error ("GetFileSize for handle %p failed: ec=%d\n",
fp, (int) GetLastError ());
if (!get_file_size_ex_initialized)
{
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;
}
if (get_file_size_ex)
{
/* 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;
}
}
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;
}
return 0;
}
/* Return the file descriptor of the underlying file or -1 if it is
not available. */
int
iobuf_get_fd (iobuf_t a)
{
if (a->directfp)
return fileno ( (FILE*)a->directfp );
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;
return my_fileno (fp);
}
return 0;
return -1;
}
/****************
* Tell the file position, where the next read will take place
*/
@ -2233,30 +2266,6 @@ iobuf_get_fname (iobuf_t a)
return NULL;
}
/****************
* Start the block write mode, see rfc1991.new for details.
* A value of 0 for N stops this mode (flushes and writes
* the end marker)
*/
void
iobuf_set_block_mode (iobuf_t a, size_t n)
{
block_filter_ctx_t *ctx = xcalloc (1, sizeof *ctx);
assert (a->use == 1 || a->use == 2);
ctx->use = a->use;
if (!n)
{
if (a->use == 1)
log_debug ("pop_filter called in set_block_mode - please report\n");
pop_filter (a, block_filter, NULL);
}
else
{
ctx->size = n; /* only needed for use 2 */
iobuf_push_filter (a, block_filter, ctx);
}
}
/****************
* enable partial block mode as described in the OpenPGP draft.
@ -2286,18 +2295,6 @@ iobuf_set_partial_block_mode (iobuf_t a, size_t len)
}
/****************
* Checks whether the stream is in block mode
* Note: This does not work if other filters are pushed on the stream.
*/
int
iobuf_in_block_mode (iobuf_t a)
{
if (a && a->filter == block_filter)
return 1; /* yes */
return 0; /* no */
}
/****************
* Same as fgets() but if the buffer is too short a larger one will
@ -2416,3 +2413,54 @@ translate_file_handle (int fd, int for_write)
#endif
return fd;
}
void
iobuf_skip_rest (iobuf_t a, unsigned long n, int partial)
{
if ( partial )
{
for (;;)
{
if (a->nofast || a->d.start >= a->d.len)
{
if (iobuf_readbyte (a) == -1)
{
break;
}
}
else
{
unsigned long count = a->d.len - a->d.start;
a->nbytes += count;
a->d.start = a->d.len;
}
}
}
else
{
unsigned long remaining = n;
while (remaining > 0)
{
if (a->nofast || a->d.start >= a->d.len)
{
if (iobuf_readbyte (a) == -1)
{
break;
}
--remaining;
}
else
{
unsigned long count = a->d.len - a->d.start;
if (count > remaining)
{
count = remaining;
}
a->nbytes += count;
a->d.start += count;
remaining -= count;
}
}
}
}

View File

@ -90,6 +90,7 @@ struct iobuf_struct
EXTERN_UNLESS_MAIN_MODULE int iobuf_debug_mode;
void iobuf_enable_special_filenames (int yes);
int iobuf_is_pipe_filename (const char *fname);
iobuf_t iobuf_alloc (int use, size_t bufsize);
iobuf_t iobuf_temp (void);
iobuf_t iobuf_temp_with_content (const char *buffer, size_t length);
@ -134,14 +135,13 @@ int iobuf_write_temp (iobuf_t a, iobuf_t temp);
size_t iobuf_temp_to_buffer (iobuf_t a, byte * buffer, size_t buflen);
void iobuf_unget_and_close_temp (iobuf_t a, iobuf_t temp);
off_t iobuf_get_filelength (iobuf_t a);
off_t iobuf_get_filelength (iobuf_t a, int *overflow);
#define IOBUF_FILELENGTH_LIMIT 0xffffffff
int iobuf_get_fd (iobuf_t a);
const char *iobuf_get_real_fname (iobuf_t a);
const char *iobuf_get_fname (iobuf_t a);
void iobuf_set_block_mode (iobuf_t a, size_t n);
void iobuf_set_partial_block_mode (iobuf_t a, size_t len);
int iobuf_in_block_mode (iobuf_t a);
int iobuf_translate_file_handle (int fd, int for_write);

View File

@ -81,6 +81,7 @@ is_file_compressed (const char *s, int *ret_rc)
iobuf_t a;
byte buf[4];
int i, rc = 0;
int overflow;
struct magic_compress_s {
size_t len;
@ -91,7 +92,7 @@ is_file_compressed (const char *s, int *ret_rc)
{ 4, { 0x50, 0x4b, 0x03, 0x04 } }, /* (pk)zip */
};
if ( !s || (*s == '-' && !s[1]) || !ret_rc )
if ( iobuf_is_pipe_filename (s) || !ret_rc )
return 0; /* We can't check stdin or no file was given */
a = iobuf_open( s );
@ -100,7 +101,7 @@ is_file_compressed (const char *s, int *ret_rc)
return 0;
}
if ( iobuf_get_filelength( a ) < 4 ) {
if ( iobuf_get_filelength( a, &overflow ) < 4 && !overflow) {
*ret_rc = 0;
goto leave;
}

View File

@ -473,9 +473,9 @@ int exec_write(struct exec_info **info,const char *program,
(*info)->tochild=fopen((*info)->tempfile_in,binary?"wb":"w");
if((*info)->tochild==NULL)
{
ret = gpg_error_from_errno (errno);
log_error(_("can't create `%s': %s\n"),
(*info)->tempfile_in,strerror(errno));
ret=G10ERR_WRITE_FILE;
goto fail;
}

View File

@ -134,9 +134,9 @@ maybe_create_keyring (char *filename, int force)
log_info ("can't allocate lock for `%s'\n", filename );
if (!force)
return G10ERR_OPEN_FILE;
return gpg_error (GPG_ERR_ENOENT);
else
return G10ERR_GENERAL;
return gpg_error (GPG_ERR_GENERAL);
}
if ( make_dotlock (lockhd, -1) )
@ -166,9 +166,9 @@ maybe_create_keyring (char *filename, int force)
umask (oldmask);
if (!iobuf)
{
rc = gpg_error_from_errno (errno);
log_error ( _("error creating keyring `%s': %s\n"),
filename, strerror(errno));
rc = G10ERR_OPEN_FILE;
goto leave;
}

View File

@ -1310,7 +1310,7 @@ list_keyblock_colon( KBNODE keyblock, int secret, int fpr )
case 0: sigrc = '!'; break;
case GPG_ERR_BAD_SIGNATURE: sigrc = '-'; break;
case GPG_ERR_NO_PUBKEY:
case Gpg_Err_UNUSABLE_PUBKEY: sigrc = '?'; break;
case GPG_ERR_UNUSABLE_PUBKEY: sigrc = '?'; break;
default: sigrc = '%'; break;
}

View File

@ -31,13 +31,13 @@
#endif
#include "gpg.h"
#include "iobuf.h"
#include "filter.h"
#include "keydb.h"
#include "status.h"
#include "exec.h"
#include "main.h"
#include "i18n.h"
#include "iobuf.h"
#include "ttyio.h"
#include "options.h"
#include "packet.h"

View File

@ -647,7 +647,7 @@ proc_plaintext( CTX c, PACKET *pkt )
else if(n->pkt->pkttype==PKT_SIGNATURE)
{
/* For the SIG+LITERAL case that PGP used to use. */
md_enable( c->mfx.md, n->pkt->pkt.signature->digest_algo );
gcry_md_enable ( c->mfx.md, n->pkt->pkt.signature->digest_algo );
any=1;
}
}
@ -2010,7 +2010,7 @@ proc_tree( CTX c, KBNODE node )
&& is_RSA( sig->pubkey_algo ) ) {
/* enable a workaround for a pgp2 bug */
if (gcry_md_open (&c->mfx.md2, DIGEST_ALGO_MD5, 0))
BUG ():
BUG ();
}
else if( sig->digest_algo == DIGEST_ALGO_SHA1
&& sig->pubkey_algo == PUBKEY_ALGO_DSA

View File

@ -262,6 +262,7 @@ struct {
#define DBG_CARD_IO_VALUE 2048 /* debug smart card I/O. */
#define DBG_PACKET (opt.debug & DBG_PACKET_VALUE)
#define DBG_CIPHER (opt.debug & DBG_CIPHER_VALUE)
#define DBG_FILTER (opt.debug & DBG_FILTER_VALUE)
#define DBG_CACHE (opt.debug & DBG_CACHE_VALUE)
#define DBG_TRUST (opt.debug & DBG_TRUST_VALUE)
@ -304,6 +305,7 @@ EXTERN_UNLESS_MAIN_MODULE int memory_stat_debug_mode;
#define EXPORT_RESET_SUBKEY_PASSWD (1<<3)
#define EXPORT_MINIMAL (1<<4)
#define EXPORT_CLEAN (1<<5)
#define EXPORT_SEXP_FORMAT (1<<6)
#define LIST_SHOW_PHOTOS (1<<0)
#define LIST_SHOW_POLICY_URLS (1<<1)

View File

@ -133,13 +133,13 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
else if (is_secured_filename (fname))
{
errno = EPERM;
rc = gpg_error_from_errno (errno);
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
rc = G10ERR_CREATE_FILE;
goto leave;
}
else if( !(fp = fopen(fname,"wb")) ) {
rc = gpg_error_from_errno (errno);
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
rc = G10ERR_CREATE_FILE;
goto leave;
}
#else /* __riscos__ */
@ -205,7 +205,7 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
{
log_error ("error writing to `%s': %s\n",
fname,"exceeded --max-output limit\n");
rc = gpg_error (GPG_ERR_GENERAL);
rc = gpg_error (GPG_ERR_TOO_LARGE);
goto leave;
}
else if( putc( c, fp ) == EOF )
@ -239,17 +239,17 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
{
if(opt.max_output && (count+=len)>opt.max_output)
{
log_error("Error writing to `%s': %s\n",
fname,"exceeded --max-output limit\n");
rc = G10ERR_WRITE_FILE;
log_error ("error writing to `%s': %s\n",
fname,"exceeded --max-output limit\n");
rc = gpg_error (GPG_ERR_TOO_LARGE);
xfree( buffer );
goto leave;
}
else if( fwrite( buffer, 1, len, fp ) != len )
{
log_error("Error writing to `%s': %s\n",
fname, strerror(errno) );
rc = G10ERR_WRITE_FILE;
rc = gpg_error_from_errno (errno);
log_error ("error writing to `%s': %s\n",
fname, strerror(errno) );
xfree( buffer );
goto leave;
}
@ -274,14 +274,17 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
{
log_error("Error writing to `%s': %s\n",
fname,"exceeded --max-output limit\n");
rc = G10ERR_WRITE_FILE;
rc = gpg_error (GPG_ERR_TOO_LARGE);
goto leave;
}
else if( putc( c, fp ) == EOF )
{
if ( ferror (fp ) )
rc = gpg_error_from_errno (errno);
else
rc = gpg_error (GPG_ERR_EOF);
log_error("Error writing to `%s': %s\n",
fname, strerror(errno) );
rc = G10ERR_WRITE_FILE;
goto leave;
}
}
@ -310,7 +313,7 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
{
log_error("Error writing to `%s': %s\n",
fname,"exceeded --max-output limit\n");
rc = G10ERR_WRITE_FILE;
rc = gpg_error (GPG_ERR_TOO_LARGE);
xfree( buffer );
goto leave;
}
@ -337,7 +340,7 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
{
log_error("Error writing to `%s': %s\n",
fname,"exceeded --max-output limit\n");
rc = G10ERR_WRITE_FILE;
rc = gpg_error (GPG_ERR_TOO_LARGE);
goto leave;
}
else if( putc( c, fp ) == EOF )
@ -351,8 +354,8 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
if( !mfx->md )
continue;
if( state == 2 ) {
md_putc(mfx->md, '\r' );
md_putc(mfx->md, '\n' );
gcry_md_putc (mfx->md, '\r' );
gcry_md_putc (mfx->md, '\n' );
state = 0;
}
if( !state ) {
@ -361,18 +364,18 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
else if( c == '\n' )
state = 2;
else
md_putc(mfx->md, c );
gcry_md_putc(mfx->md, c );
}
else if( state == 1 ) {
if( c == '\n' )
state = 2;
else {
md_putc(mfx->md, '\r' );
gcry_md_putc(mfx->md, '\r' );
if( c == '\r' )
state = 1;
else {
state = 0;
md_putc(mfx->md, c );
gcry_md_putc(mfx->md, c );
}
}
}

View File

@ -294,7 +294,8 @@ check_secret_key( PKT_secret_key *sk, int n )
log_info (_("%s ...\n"), _(tryagain));
}
rc = do_check( sk, tryagain, mode, &canceled );
if ( gpg_err_code (rc) == G10ERR_BAD_PASS && is_status_enabled () ) {
if ( gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE
&& is_status_enabled () ) {
u32 kid[2];
char buf[50];

View File

@ -656,7 +656,8 @@ write_signature_packets (SK_LIST sk_list, IOBUF out, gcry_md_hd_t hash,
sig->expiredate = sig->timestamp+duration;
sig->sig_class = sigclass;
md = gcry_md_copy (hash);
if (gcry_md_copy (&md, hash))
BUG ();
if (sig->version >= 4)
build_sig_subpkt_from_sig (sig);
@ -938,9 +939,9 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr,
}
if( !inp )
{
rc = gpg_error_from_errno (errno);
log_error(_("can't open `%s': %s\n"),
sl->d,strerror(errno));
rc = G10ERR_OPEN_FILE;
goto leave;
}
handle_progress (&pfx, inp, sl->d);

View File

@ -1,3 +1,8 @@
2006-04-20 Werner Koch <wk@g10code.com>
* stringhelp.c (make_basename): New arg INPUTPATH for future
riscos compatibility.
2006-04-18 Werner Koch <wk@g10code.com>
* libjnlib-config.h (JNLIB_NEED_UTF8CONF): Defined.

View File

@ -234,15 +234,19 @@ length_sans_trailing_ws (const unsigned char *line, size_t len)
*
*/
char *
make_basename(const char *filepath)
make_basename(const char *filepath, const char *inputpath)
{
char *p;
#ifdef __riscos__
return riscos_make_basename(filepath, inputpath);
#endif
if ( !(p=strrchr(filepath, '/')) )
#ifdef HAVE_DRIVE_LETTERS
#ifdef HAVE_DRIVE_LETTERS
if ( !(p=strrchr(filepath, '\\')) )
if ( !(p=strrchr(filepath, ':')) )
#endif
#endif
{
return jnlib_xstrdup(filepath);
}

View File

@ -35,7 +35,7 @@ size_t length_sans_trailing_chars (const unsigned char *line, size_t len,
size_t length_sans_trailing_ws (const unsigned char *line, size_t len);
char *make_basename(const char *filepath);
char *make_basename(const char *filepath, const char *inputpath);
char *make_dirname(const char *filepath);
char *make_filename( const char *first_part, ... );
int compare_filenames( const char *a, const char *b );