aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/ChangeLog16
-rw-r--r--common/iobuf.c304
-rw-r--r--common/iobuf.h6
-rw-r--r--common/miscellaneous.c5
4 files changed, 198 insertions, 133 deletions
diff --git a/common/ChangeLog b/common/ChangeLog
index 54bce4538..f1b11fc57 100644
--- a/common/ChangeLog
+++ b/common/ChangeLog
@@ -1,3 +1,19 @@
+2006-04-19 Werner Koch <[email protected]>
+
+ * 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 <[email protected]>
* homedir.c (w32_shgetfolderpath): New. Taken from gpg 1.4.3.
diff --git a/common/iobuf.c b/common/iobuf.c
index 32b9e18c6..bbb666f67 100644
--- a/common/iobuf.c
+++ b/common/iobuf.c
@@ -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;
-
- if ((size = GetFileSize (fp, NULL)) != 0xffffffff)
- return size;
- log_error ("GetFileSize for handle %p failed: ec=%d\n",
- fp, (int) GetLastError ());
+ ulong size;
+ static int (* __stdcall get_file_size_ex)
+ (void *handle, LARGE_INTEGER *size);
+ static int get_file_size_ex_initialized;
+
+ 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;
+ }
+ }
+ }
+}
diff --git a/common/iobuf.h b/common/iobuf.h
index def0a6506..431d573a1 100644
--- a/common/iobuf.h
+++ b/common/iobuf.h
@@ -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);
diff --git a/common/miscellaneous.c b/common/miscellaneous.c
index d81213ef9..14d6f020d 100644
--- a/common/miscellaneous.c
+++ b/common/miscellaneous.c
@@ -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;
}