2006-12-21 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / miscellaneous.c
blob364f1348990608903cf236f7e7fb7c00b440018d
1 /* miscellaneous.c - Stuff not fitting elsewhere
2 * Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <stdlib.h>
24 #include <errno.h>
26 #include "util.h"
27 #include "iobuf.h"
30 /* Decide whether the filename is stdout or a real filename and return
31 * an appropriate string. */
32 const char *
33 print_fname_stdout (const char *s)
35 if( !s || (*s == '-' && !s[1]) )
36 return "[stdout]";
37 return s;
41 /* Decide whether the filename is stdin or a real filename and return
42 * an appropriate string. */
43 const char *
44 print_fname_stdin (const char *s)
46 if( !s || (*s == '-' && !s[1]) )
47 return "[stdin]";
48 return s;
51 /* fixme: Globally replace it by print_sanitized_buffer. */
52 void
53 print_string( FILE *fp, const byte *p, size_t n, int delim )
55 print_sanitized_buffer (fp, p, n, delim);
58 void
59 print_utf8_string2 ( FILE *fp, const byte *p, size_t n, int delim )
61 print_sanitized_utf8_buffer (fp, p, n, delim);
64 void
65 print_utf8_string( FILE *fp, const byte *p, size_t n )
67 print_utf8_string2 (fp, p, n, 0);
70 char *
71 make_printable_string (const void *p, size_t n, int delim )
73 return sanitize_buffer (p, n, delim);
78 * Check if the file is compressed.
80 int
81 is_file_compressed (const char *s, int *ret_rc)
83 iobuf_t a;
84 byte buf[4];
85 int i, rc = 0;
86 int overflow;
88 struct magic_compress_s {
89 size_t len;
90 byte magic[4];
91 } magic[] = {
92 { 3, { 0x42, 0x5a, 0x68, 0x00 } }, /* bzip2 */
93 { 3, { 0x1f, 0x8b, 0x08, 0x00 } }, /* gzip */
94 { 4, { 0x50, 0x4b, 0x03, 0x04 } }, /* (pk)zip */
97 if ( iobuf_is_pipe_filename (s) || !ret_rc )
98 return 0; /* We can't check stdin or no file was given */
100 a = iobuf_open( s );
101 if ( a == NULL ) {
102 *ret_rc = gpg_error_from_syserror ();
103 return 0;
106 if ( iobuf_get_filelength( a, &overflow ) < 4 && !overflow) {
107 *ret_rc = 0;
108 goto leave;
111 if ( iobuf_read( a, buf, 4 ) == -1 ) {
112 *ret_rc = a->error;
113 goto leave;
116 for ( i = 0; i < DIM( magic ); i++ ) {
117 if ( !memcmp( buf, magic[i].magic, magic[i].len ) ) {
118 *ret_rc = 0;
119 rc = 1;
120 break;
124 leave:
125 iobuf_close( a );
126 return rc;
130 /* Try match against each substring of multistr, delimited by | */
132 match_multistr (const char *multistr,const char *match)
136 size_t seglen = strcspn (multistr,"|");
137 if (!seglen)
138 break;
139 /* Using the localized strncasecmp! */
140 if (strncasecmp(multistr,match,seglen)==0)
141 return 1;
142 multistr += seglen;
143 if (*multistr == '|')
144 multistr++;
146 while (*multistr);
148 return 0;