~ubuntu-branches/ubuntu/jaunty/gnupg2/jaunty

« back to all changes in this revision

Viewing changes to common/miscellaneous.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* miscellaneous.c - Stuff not fitting elsewhere
 
2
 *      Copyright (C) 2003 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <stdlib.h>
 
23
#include <errno.h>
 
24
 
 
25
#include "util.h"
 
26
#include "iobuf.h"
 
27
 
 
28
 
 
29
/* Decide whether the filename is stdout or a real filename and return
 
30
 * an appropriate string.  */
 
31
const char *
 
32
print_fname_stdout (const char *s)
 
33
{
 
34
    if( !s || (*s == '-' && !s[1]) )
 
35
        return "[stdout]";
 
36
    return s;
 
37
}
 
38
 
 
39
 
 
40
/* Decide whether the filename is stdin or a real filename and return
 
41
 * an appropriate string.  */
 
42
const char *
 
43
print_fname_stdin (const char *s)
 
44
{
 
45
    if( !s || (*s == '-' && !s[1]) )
 
46
        return "[stdin]";
 
47
    return s;
 
48
}
 
49
 
 
50
void
 
51
print_string( FILE *fp, const byte *p, size_t n, int delim )
 
52
{
 
53
  print_sanitized_buffer (fp, p, n, delim);
 
54
}
 
55
 
 
56
void
 
57
print_utf8_string2 ( FILE *fp, const byte *p, size_t n, int delim )
 
58
{
 
59
  print_sanitized_utf8_buffer (fp, p, n, delim);
 
60
}
 
61
 
 
62
void
 
63
print_utf8_string( FILE *fp, const byte *p, size_t n )
 
64
{
 
65
    print_utf8_string2 (fp, p, n, 0);
 
66
}
 
67
 
 
68
char *
 
69
make_printable_string( const byte *p, size_t n, int delim )
 
70
{
 
71
  return sanitize_buffer (p, n, delim);
 
72
}
 
73
 
 
74
 
 
75
/*
 
76
 * Check if the file is compressed.
 
77
 */
 
78
int
 
79
is_file_compressed (const char *s, int *ret_rc)
 
80
{
 
81
    iobuf_t a;
 
82
    byte buf[4];
 
83
    int i, rc = 0;
 
84
 
 
85
    struct magic_compress_s {
 
86
        size_t len;
 
87
        byte magic[4];
 
88
    } magic[] = {
 
89
        { 3, { 0x42, 0x5a, 0x68, 0x00 } }, /* bzip2 */
 
90
        { 3, { 0x1f, 0x8b, 0x08, 0x00 } }, /* gzip */
 
91
        { 4, { 0x50, 0x4b, 0x03, 0x04 } }, /* (pk)zip */
 
92
    };
 
93
    
 
94
    if ( !s || (*s == '-' && !s[1]) || !ret_rc )
 
95
        return 0; /* We can't check stdin or no file was given */
 
96
 
 
97
    a = iobuf_open( s );
 
98
    if ( a == NULL ) {
 
99
        *ret_rc = gpg_error_from_errno (errno);
 
100
        return 0;
 
101
    }
 
102
 
 
103
    if ( iobuf_get_filelength( a ) < 4 ) {
 
104
        *ret_rc = 0;
 
105
        goto leave;
 
106
    }
 
107
 
 
108
    if ( iobuf_read( a, buf, 4 ) == -1 ) {
 
109
        *ret_rc = a->error;
 
110
        goto leave;
 
111
    }
 
112
 
 
113
    for ( i = 0; i < DIM( magic ); i++ ) {
 
114
        if ( !memcmp( buf, magic[i].magic, magic[i].len ) ) {
 
115
            *ret_rc = 0;
 
116
            rc = 1;
 
117
            break;
 
118
        }
 
119
    }
 
120
 
 
121
leave:    
 
122
    iobuf_close( a );
 
123
    return rc;
 
124
}
 
125
 
 
126
 
 
127