aboutsummaryrefslogtreecommitdiffstats
path: root/common/miscellaneous.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2023-01-18 17:04:50 +0000
committerWerner Koch <[email protected]>2023-01-19 09:45:54 +0000
commit60963d98cfd8e60f88ee43c2d992f6dd3bbbd74c (patch)
tree766f37c0531a72f5d11e3920b87ba2e51ce23b46 /common/miscellaneous.c
parentcommon: Replace all assert by log_assert. (diff)
downloadgnupg-60963d98cfd8e60f88ee43c2d992f6dd3bbbd74c.tar.gz
gnupg-60963d98cfd8e60f88ee43c2d992f6dd3bbbd74c.zip
gpg: Detect already compressed data also when using a pipe.
* common/iobuf.c (file_filter_ctx_t): Add fields for the peek feature. (file_filter): Implement peeking. (iobuf_ioctl): Add new IOBUF_IOCTL_PEEK. * common/iobuf.h (IOBUF_IOCTL_PEEK, IOBUFCTRL_PEEK): New. * common/miscellaneous.c (is_file_compressed): Rewrite. Detect PDF. * g10/encrypt.c (encrypt_simple): Peek before detecting compression. (encrypt_crypt): Ditto. * g10/sign.c (sign_file): Also detect already compressed data. * g10/options.h (opt): Add explicit_compress_option. * g10/gpg.c (main): Set opt.explicit_compress_option for -z. -- Note that this patch also introduces a compression check for signing which was never done in the past. GnuPG-bug-id: 6332
Diffstat (limited to 'common/miscellaneous.c')
-rw-r--r--common/miscellaneous.c77
1 files changed, 30 insertions, 47 deletions
diff --git a/common/miscellaneous.c b/common/miscellaneous.c
index df6b68784..60ac4b4df 100644
--- a/common/miscellaneous.c
+++ b/common/miscellaneous.c
@@ -418,7 +418,7 @@ decode_c_string (const char *src)
/* Check whether (BUF,LEN) is valid header for an OpenPGP compressed
* packet. LEN should be at least 6. */
static int
-is_openpgp_compressed_packet (unsigned char *buf, size_t len)
+is_openpgp_compressed_packet (const unsigned char *buf, size_t len)
{
int c, ctb, pkttype;
int lenbytes;
@@ -460,63 +460,46 @@ is_openpgp_compressed_packet (unsigned char *buf, size_t len)
/*
- * Check if the file is compressed.
+ * Check if the file is compressed. You need to pass the first bytes
+ * of the file as (BUF,BUFLEN). Returns true if the buffer seems to
+ * be compressed.
*/
int
-is_file_compressed (const char *s, int *ret_rc)
+is_file_compressed (const byte *buf, unsigned int buflen)
{
- iobuf_t a;
- byte buf[6];
- int i;
- int rc = 0;
- int overflow;
-
- struct magic_compress_s {
- size_t len;
- byte magic[4];
- } magic[] = {
- { 3, { 0x42, 0x5a, 0x68, 0x00 } }, /* bzip2 */
- { 3, { 0x1f, 0x8b, 0x08, 0x00 } }, /* gzip */
- { 4, { 0x50, 0x4b, 0x03, 0x04 } }, /* (pk)zip */
- };
-
- if ( iobuf_is_pipe_filename (s) || !ret_rc )
- return 0; /* We can't check stdin or no file was given */
-
- a = iobuf_open( s );
- if ( a == NULL ) {
- *ret_rc = gpg_error_from_syserror ();
- return 0;
- }
- iobuf_ioctl (a, IOBUF_IOCTL_NO_CACHE, 1, NULL);
+ int i;
- if ( iobuf_get_filelength( a, &overflow ) < 6 && !overflow) {
- *ret_rc = 0;
- goto leave;
- }
+ struct magic_compress_s
+ {
+ byte len;
+ byte magic[5];
+ } magic[] =
+ {
+ { 3, { 0x42, 0x5a, 0x68, 0x00 } }, /* bzip2 */
+ { 3, { 0x1f, 0x8b, 0x08, 0x00 } }, /* gzip */
+ { 4, { 0x50, 0x4b, 0x03, 0x04 } }, /* (pk)zip */
+ { 5, { '%', 'P', 'D', 'F', '-'} } /* PDF */
+ };
- if ( iobuf_read( a, buf, 6 ) == -1 ) {
- *ret_rc = a->error;
- goto leave;
+ if ( buflen < 6 )
+ {
+ return 0; /* Too short to check - assume uncompressed. */
}
- for ( i = 0; i < DIM( magic ); i++ ) {
- if ( !memcmp( buf, magic[i].magic, magic[i].len ) ) {
- *ret_rc = 0;
- rc = 1;
- goto leave;
+ for ( i = 0; i < DIM (magic); i++ )
+ {
+ if ( !memcmp( buf, magic[i].magic, magic[i].len ))
+ {
+ return 1; /* Is compressed. */
}
}
- if (is_openpgp_compressed_packet (buf, 6))
- {
- *ret_rc = 0;
- rc = 1;
- }
+ if (buflen >= 6 && is_openpgp_compressed_packet (buf, buflen))
+ {
+ return 1; /* Already compressed. */
+ }
- leave:
- iobuf_close( a );
- return rc;
+ return 0; /* Not detected as compressed. */
}