~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to util/miscutil.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2006-01-24 04:31:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060124043142-pbg192or6qxv3yk2
Tags: 1.9.20-1
* New Upstream version. Closes:#306890,#344530
  * Closes:#320490: gpg-protect-tool fails to decrypt PKCS-12 files 
* Depend on libopensc2-dev, not -1-. Closes:#348106

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* miscutil.c -  miscellaneous utilities
 
2
 *      Copyright (C) 1998, 1999, 2000, 2001 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 <stdio.h>
 
24
#include <string.h>
 
25
#include <time.h>
 
26
#include <ctype.h>
 
27
#ifdef HAVE_LANGINFO_H
 
28
  #include <langinfo.h>
 
29
#endif
 
30
#include "types.h"
 
31
#include "util.h"
 
32
#include "i18n.h"
 
33
 
 
34
/****************
 
35
 * I know that the OpenPGP protocol has a Y2106 problem ;-)
 
36
 */
 
37
u32
 
38
make_timestamp()
 
39
{
 
40
    return time(NULL);
 
41
}
 
42
 
 
43
/****************
 
44
 * Scan a date string and return a timestamp.
 
45
 * The only supported format is "yyyy-mm-dd"
 
46
 * Returns 0 for an invalid date.
 
47
 */
 
48
u32
 
49
scan_isodatestr( const char *string )
 
50
{
 
51
    int year, month, day;
 
52
    struct tm tmbuf;
 
53
    time_t stamp;
 
54
    int i;
 
55
 
 
56
    if( strlen(string) != 10 || string[4] != '-' || string[7] != '-' )
 
57
        return 0;
 
58
    for( i=0; i < 4; i++ )
 
59
        if( !isdigit(string[i]) )
 
60
            return 0;
 
61
    if( !isdigit(string[5]) || !isdigit(string[6]) )
 
62
        return 0;
 
63
    if( !isdigit(string[8]) || !isdigit(string[9]) )
 
64
        return 0;
 
65
    year = atoi(string);
 
66
    month = atoi(string+5);
 
67
    day = atoi(string+8);
 
68
    /* some basic checks */
 
69
    if( year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 )
 
70
        return 0;
 
71
    memset( &tmbuf, 0, sizeof tmbuf );
 
72
    tmbuf.tm_mday = day;
 
73
    tmbuf.tm_mon = month-1;
 
74
    tmbuf.tm_year = year - 1900;
 
75
    tmbuf.tm_isdst = -1;
 
76
    stamp = mktime( &tmbuf );
 
77
    if( stamp == (time_t)-1 )
 
78
        return 0;
 
79
    return stamp;
 
80
}
 
81
 
 
82
 
 
83
u32
 
84
add_days_to_timestamp( u32 stamp, u16 days )
 
85
{
 
86
    return stamp + days*86400L;
 
87
}
 
88
 
 
89
 
 
90
/****************
 
91
 * Return a string with a time value in the form: x Y, n D, n H
 
92
 */
 
93
 
 
94
const char *
 
95
strtimevalue( u32 value )
 
96
{
 
97
    static char buffer[30];
 
98
    unsigned int years, days, hours, minutes;
 
99
 
 
100
    value /= 60;
 
101
    minutes = value % 60;
 
102
    value /= 60;
 
103
    hours = value % 24;
 
104
    value /= 24;
 
105
    days = value % 365;
 
106
    value /= 365;
 
107
    years = value;
 
108
 
 
109
    sprintf(buffer,"%uy%ud%uh%um", years, days, hours, minutes );
 
110
    if( years )
 
111
        return buffer;
 
112
    if( days )
 
113
        return strchr( buffer, 'y' ) + 1;
 
114
    return strchr( buffer, 'd' ) + 1;
 
115
}
 
116
 
 
117
 
 
118
/****************
 
119
 * Note: this function returns GMT
 
120
 */
 
121
const char *
 
122
strtimestamp( u32 stamp )
 
123
{
 
124
    static char buffer[11+5];
 
125
    struct tm *tp;
 
126
    time_t atime = stamp;
 
127
    
 
128
    if (atime < 0) {
 
129
        strcpy (buffer, "????" "-??" "-??");
 
130
    }
 
131
    else {
 
132
        tp = gmtime( &atime );
 
133
        sprintf(buffer,"%04d-%02d-%02d",
 
134
                1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
 
135
    }
 
136
    return buffer;
 
137
}
 
138
 
 
139
/****************
 
140
 * Note: this function returns local time
 
141
 */
 
142
const char *
 
143
asctimestamp( u32 stamp )
 
144
{
 
145
    static char buffer[50];
 
146
    #if defined (HAVE_STRFTIME) && defined (HAVE_NL_LANGINFO)
 
147
      static char fmt[50];
 
148
    #endif
 
149
    struct tm *tp;
 
150
    time_t atime = stamp;
 
151
 
 
152
    if (atime < 0) {
 
153
        strcpy (buffer, "????" "-??" "-??");
 
154
        return buffer;
 
155
    }
 
156
 
 
157
    tp = localtime( &atime );
 
158
  #ifdef HAVE_STRFTIME
 
159
    #if defined(HAVE_NL_LANGINFO)
 
160
      mem2str( fmt, nl_langinfo(D_T_FMT), DIM(fmt)-3 );
 
161
      if( strstr( fmt, "%Z" ) == NULL )
 
162
        strcat( fmt, " %Z");
 
163
      strftime( buffer, DIM(buffer)-1, fmt, tp );
 
164
    #else
 
165
      /* fixme: we should check whether the locale appends a " %Z"
 
166
       * These locales from glibc don't put the " %Z":
 
167
       * fi_FI hr_HR ja_JP lt_LT lv_LV POSIX ru_RU ru_SU sv_FI sv_SE zh_CN
 
168
       */
 
169
      strftime( buffer, DIM(buffer)-1, "%c %Z", tp );
 
170
    #endif
 
171
    buffer[DIM(buffer)-1] = 0;
 
172
  #else
 
173
    mem2str( buffer, asctime(tp), DIM(buffer) );
 
174
  #endif
 
175
    return buffer;
 
176
}
 
177
 
 
178
/****************
 
179
 * Print a string to FP, but filter all control characters out.
 
180
 */
 
181
void
 
182
print_string( FILE *fp, const byte *p, size_t n, int delim )
 
183
{
 
184
    for( ; n; n--, p++ )
 
185
        if( *p < 0x20 || (*p >= 0x7f && *p < 0xa0) || *p == delim ||
 
186
            (delim && *p=='\\')) {
 
187
            putc('\\', fp);
 
188
            if( *p == '\n' )
 
189
                putc('n', fp);
 
190
            else if( *p == '\r' )
 
191
                putc('r', fp);
 
192
            else if( *p == '\f' )
 
193
                putc('f', fp);
 
194
            else if( *p == '\v' )
 
195
                putc('v', fp);
 
196
            else if( *p == '\b' )
 
197
                putc('b', fp);
 
198
            else if( !*p )
 
199
                putc('0', fp);
 
200
            else
 
201
                fprintf(fp, "x%02x", *p );
 
202
        }
 
203
        else
 
204
            putc(*p, fp);
 
205
}
 
206
 
 
207
/****************
 
208
 * Print an UTF8 string to FP and filter all control characters out.
 
209
 */
 
210
void
 
211
print_utf8_string2 ( FILE *fp, const byte *p, size_t n, int delim )
 
212
{
 
213
    size_t i;
 
214
    char *buf;
 
215
 
 
216
    /* we can handle plain ascii simpler, so check for it first */
 
217
    for(i=0; i < n; i++ ) {
 
218
        if( p[i] & 0x80 )
 
219
            break;
 
220
    }
 
221
    if( i < n ) {
 
222
        buf = utf8_to_native ( p, n, delim );
 
223
        /*(utf8 conversion already does the control character quoting)*/
 
224
        fputs( buf, fp );
 
225
        m_free( buf );
 
226
    }
 
227
    else
 
228
        print_string( fp, p, n, delim );
 
229
}
 
230
 
 
231
void
 
232
print_utf8_string( FILE *fp, const byte *p, size_t n )
 
233
{
 
234
    print_utf8_string2 (fp, p, n, 0);
 
235
}
 
236
 
 
237
/****************
 
238
 * This function returns a string which is suitable for printing
 
239
 * Caller must release it with m_free()
 
240
 */
 
241
char *
 
242
make_printable_string( const byte *p, size_t n, int delim )
 
243
{
 
244
    size_t save_n, buflen;
 
245
    const byte *save_p;
 
246
    char *buffer, *d;
 
247
 
 
248
    /* first count length */
 
249
    for(save_n = n, save_p = p, buflen=1 ; n; n--, p++ ) {
 
250
        if( *p < 0x20 || (*p >= 0x7f && *p < 0xa0) || *p == delim ||
 
251
            (delim && *p=='\\')) {
 
252
            if( *p=='\n' || *p=='\r' || *p=='\f'
 
253
                || *p=='\v' || *p=='\b' || !*p )
 
254
                buflen += 2;
 
255
            else
 
256
                buflen += 4;
 
257
        }
 
258
        else
 
259
            buflen++;
 
260
    }
 
261
    p = save_p;
 
262
    n = save_n;
 
263
    /* and now make the string */
 
264
    d = buffer = m_alloc( buflen );
 
265
    for( ; n; n--, p++ ) {
 
266
        if( *p < 0x20 || (*p >= 0x7f && *p < 0xa0) || *p == delim ||
 
267
            (delim && *p=='\\')) {
 
268
            *d++ = '\\';
 
269
            if( *p == '\n' )
 
270
                *d++ = 'n';
 
271
            else if( *p == '\r' )
 
272
                *d++ = 'r';
 
273
            else if( *p == '\f' )
 
274
                *d++ = 'f';
 
275
            else if( *p == '\v' )
 
276
                *d++ = 'v';
 
277
            else if( *p == '\b' )
 
278
                *d++ = 'b';
 
279
            else if( !*p )
 
280
                *d++ = '0';
 
281
            else {
 
282
                sprintf(d, "x%02x", *p );
 
283
                d += 2;
 
284
            }
 
285
        }
 
286
        else
 
287
            *d++ = *p;
 
288
    }
 
289
    *d = 0;
 
290
    return buffer;
 
291
}
 
292
 
 
293
int
 
294
answer_is_yes_no_default( const char *s, int def_answer )
 
295
{
 
296
    const char *long_yes = _("yes");
 
297
    const char *short_yes = _("yY");
 
298
    const char *long_no = _("no");
 
299
    const char *short_no = _("nN");
 
300
 
 
301
    /* Note: we have to use the local dependent strcasecmp here */
 
302
    if( !strcasecmp(s, long_yes ) )
 
303
        return 1;
 
304
    if( *s && strchr( short_yes, *s ) && !s[1] )
 
305
        return 1;
 
306
    /* test for no strings to catch ambiguities for the next test */
 
307
    if( !strcasecmp(s, long_no ) )
 
308
        return 0;
 
309
    if( *s && strchr( short_no, *s ) && !s[1] )
 
310
        return 0;
 
311
    /* test for the english version (for those who are used to type yes) */
 
312
    if( !ascii_strcasecmp(s, "yes" ) )
 
313
        return 1;
 
314
    if( *s && strchr( "yY", *s ) && !s[1] )
 
315
        return 1;
 
316
    return def_answer;
 
317
}
 
318
 
 
319
int
 
320
answer_is_yes( const char *s )
 
321
{
 
322
  return answer_is_yes_no_default(s,0);
 
323
}
 
324
 
 
325
/****************
 
326
 * Return 1 for yes, -1 for quit, or 0 for no
 
327
 */
 
328
int
 
329
answer_is_yes_no_quit( const char *s )
 
330
{
 
331
    const char *long_yes = _("yes");
 
332
    const char *long_no = _("no");
 
333
    const char *long_quit = _("quit");
 
334
    const char *short_yes = _("yY");
 
335
    const char *short_no = _("nN");
 
336
    const char *short_quit = _("qQ");
 
337
 
 
338
    /* Note: We have to use the locale dependent strcasecmp */
 
339
    if( !strcasecmp(s, long_no ) )
 
340
        return 0;
 
341
    if( !strcasecmp(s, long_yes ) )
 
342
        return 1;
 
343
    if( !strcasecmp(s, long_quit ) )
 
344
        return -1;
 
345
    if( *s && strchr( short_no, *s ) && !s[1] )
 
346
        return 0;
 
347
    if( *s && strchr( short_yes, *s ) && !s[1] )
 
348
        return 1;
 
349
    if( *s && strchr( short_quit, *s ) && !s[1] )
 
350
        return -1;
 
351
    /* but not here */
 
352
    if( !ascii_strcasecmp(s, "yes" ) )
 
353
        return 1;
 
354
    if( !ascii_strcasecmp(s, "quit" ) )
 
355
        return -1;
 
356
    if( *s && strchr( "yY", *s ) && !s[1] )
 
357
        return 1;
 
358
    if( *s && strchr( "qQ", *s ) && !s[1] )
 
359
        return -1;
 
360
    return 0;
 
361
}