~ubuntu-branches/ubuntu/feisty/gnupg2/feisty

« back to all changes in this revision

Viewing changes to jnlib/stringhelp.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-07-11 11:38:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060711113813-zaw7unlbuh7gyxtl
Tags: 1.9.21-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* stringhelp.c -  standard string helper functions
2
 
 * Copyright (C) 1998, 1999, 2000, 2001, 2003,
3
 
 *               2004, 2005  Free Software Foundation, Inc.
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005,
 
3
 *               2006  Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of GnuPG.
6
6
 *
16
16
 *
17
17
 * You should have received a copy of the GNU General Public License
18
18
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 
20
 * USA.
20
21
 */
21
22
 
22
23
#include <config.h>
218
219
  return len;
219
220
}
220
221
 
221
 
/****************
222
 
 * remove trailing white spaces and return the length of the buffer
 
222
/*
 
223
 *  Return the length of line ignoring trailing white-space.
223
224
 */
224
225
size_t
225
226
length_sans_trailing_ws (const unsigned char *line, size_t len)
234
235
 *
235
236
 */
236
237
char *
237
 
make_basename(const char *filepath)
 
238
make_basename(const char *filepath, const char *inputpath)
238
239
{
239
240
    char *p;
240
241
 
 
242
#ifdef __riscos__
 
243
    return riscos_make_basename(filepath, inputpath);
 
244
#endif
 
245
 
241
246
    if ( !(p=strrchr(filepath, '/')) )
242
 
      #ifdef HAVE_DRIVE_LETTERS
 
247
#ifdef HAVE_DRIVE_LETTERS
243
248
        if ( !(p=strrchr(filepath, '\\')) )
244
249
            if ( !(p=strrchr(filepath, ':')) )
245
 
      #endif
 
250
#endif
246
251
              {
247
252
                return jnlib_xstrdup(filepath);
248
253
              }
332
337
#endif
333
338
}
334
339
 
 
340
 
 
341
/* Convert 2 hex characters at S to a byte value.  Return this value
 
342
   or -1 if there is an error. */
 
343
int
 
344
hextobyte (const char *s)
 
345
{
 
346
  int c;
 
347
 
 
348
  if ( *s >= '0' && *s <= '9' )
 
349
    c = 16 * (*s - '0');
 
350
  else if ( *s >= 'A' && *s <= 'F' )
 
351
    c = 16 * (10 + *s - 'A');
 
352
  else if ( *s >= 'a' && *s <= 'f' )
 
353
    c = 16 * (10 + *s - 'a');
 
354
  else
 
355
    return -1;
 
356
  s++;
 
357
  if ( *s >= '0' && *s <= '9' )
 
358
    c += *s - '0';
 
359
  else if ( *s >= 'A' && *s <= 'F' )
 
360
    c += 10 + *s - 'A';
 
361
  else if ( *s >= 'a' && *s <= 'f' )
 
362
    c += 10 + *s - 'a';
 
363
  else
 
364
    return -1;
 
365
  return c;
 
366
}
 
367
 
 
368
 
335
369
/* Print a BUFFER to stream FP while replacing all control characters
336
 
   and the character DELIM with standard C escape sequences.  Returns
337
 
   the number of characters printed. */
 
370
   and the characters DELIM and DELIM2 with standard C escape
 
371
   sequences.  Returns the number of characters printed. */
338
372
size_t 
339
 
print_sanitized_buffer (FILE *fp, const void *buffer, size_t length,
340
 
                        int delim)
 
373
print_sanitized_buffer2 (FILE *fp, const void *buffer, size_t length,
 
374
                         int delim, int delim2)
341
375
{
342
376
  const unsigned char *p = buffer;
343
377
  size_t count = 0;
344
378
 
345
379
  for (; length; length--, p++, count++)
346
380
    {
347
 
      if (*p < 0x20 || *p == 0x7f || *p == delim)
 
381
      /* Fixme: Check whether *p < 0xa0 is correct for utf8 encoding. */
 
382
      if (*p < 0x20 
 
383
          || (*p >= 0x7f && *p < 0xa0)
 
384
          || *p == delim 
 
385
          || *p == delim2
 
386
          || ((delim || delim2) && *p=='\\'))
348
387
        {
349
388
          putc ('\\', fp);
350
389
          count++;
351
390
          if (*p == '\n')
352
 
            putc ('n', fp);
 
391
            {
 
392
              putc ('n', fp);
 
393
              count++;
 
394
            }
353
395
          else if (*p == '\r')
354
 
            putc ('r', fp);
 
396
            {
 
397
              putc ('r', fp);
 
398
              count++;
 
399
            }
355
400
          else if (*p == '\f')
356
 
            putc ('f', fp);
 
401
            {
 
402
              putc ('f', fp);
 
403
              count++;
 
404
            }
357
405
          else if (*p == '\v')
358
 
            putc ('v', fp);
 
406
            {
 
407
              putc ('v', fp);
 
408
              count++;
 
409
            }
359
410
          else if (*p == '\b')
360
 
            putc ('b', fp);
 
411
            {
 
412
              putc ('b', fp);
 
413
              count++;
 
414
            }
361
415
          else if (!*p)
362
 
            putc('0', fp);
 
416
            {
 
417
              putc('0', fp);
 
418
              count++;
 
419
            }
363
420
          else
364
421
            {
365
422
              fprintf (fp, "x%02x", *p);
366
 
              count += 2;
 
423
              count += 3;
367
424
            }
368
425
        }
369
426
      else
370
 
        putc (*p, fp);
 
427
        {
 
428
          putc (*p, fp);
 
429
          count++;
 
430
        }
371
431
    }
372
432
 
373
433
  return count;
374
434
}
375
435
 
 
436
/* Same as print_sanitized_buffer2 but with just one delimiter. */
 
437
size_t 
 
438
print_sanitized_buffer (FILE *fp, const void *buffer, size_t length,
 
439
                        int delim)
 
440
{
 
441
  return print_sanitized_buffer2 (fp, buffer, length, delim, 0);
 
442
}
 
443
 
 
444
 
376
445
size_t 
377
446
print_sanitized_utf8_buffer (FILE *fp, const void *buffer,
378
447
                             size_t length, int delim)
401
470
 
402
471
 
403
472
size_t 
 
473
print_sanitized_string2 (FILE *fp, const char *string, int delim, int delim2)
 
474
{
 
475
  return string? print_sanitized_buffer2 (fp, string, strlen (string),
 
476
                                          delim, delim2):0;
 
477
}
 
478
 
 
479
size_t 
404
480
print_sanitized_string (FILE *fp, const char *string, int delim)
405
481
{
406
482
  return string? print_sanitized_buffer (fp, string, strlen (string), delim):0;
424
500
  const unsigned char *save_p;
425
501
  char *buffer, *d;
426
502
 
427
 
  /* first count length */
 
503
  /* First count length. */
428
504
  for (save_n = n, save_p = p, buflen=1 ; n; n--, p++ ) 
429
505
    {
430
506
      if ( *p < 0x20 || *p == 0x7f || *p == delim  || (delim && *p=='\\'))
433
509
               || *p=='\v' || *p=='\b' || !*p )
434
510
            buflen += 2;
435
511
          else
436
 
            buflen += 4;
 
512
            buflen += 5;
437
513
        }
438
514
      else
439
515
        buflen++;
440
516
    }
441
517
  p = save_p;
442
518
  n = save_n;
443
 
  /* and now make the string */
 
519
  /* And now make the string */
444
520
  d = buffer = jnlib_xmalloc( buflen );
445
521
  for ( ; n; n--, p++ )
446
522
    {
460
536
          *d++ = '0';
461
537
        else {
462
538
          sprintf(d, "x%02x", *p );
463
 
          d += 2;
 
539
          d += 3;
464
540
        }
465
541
      }
466
542
      else