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

« back to all changes in this revision

Viewing changes to common/iobuf.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* iobuf.c  -  File Handling for OpenPGP.
2
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2006,
3
 
 *               2007  Free Software Foundation, Inc.
 
3
 *               2007, 2008  Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of GnuPG.
6
6
 *
78
78
     we are using the low-evel backend.
79
79
 
80
80
   fp_or_fd_t
81
 
     Is the type we use for the backend stream or fiel descriptor.
 
81
     Is the type we use for the backend stream or file descriptor.
82
82
 
83
83
   INVALID_FP, FILEP_OR_FD_FOR_STDIN, FILEP_OR_FD_FOR_STDOUT
84
84
     Are macros defined depending on the used backend.
412
412
  size_t nbytes = 0;
413
413
  int rc = 0;
414
414
 
 
415
  (void)chain; /* Not used.  */
 
416
 
415
417
#ifdef FILE_FILTER_USES_STDIO
416
418
  if (control == IOBUFCTRL_UNDERFLOW)
417
419
    {
418
 
      assert (size);            /* need a buffer */
 
420
      assert (size);  /* We need a buffer. */
419
421
      if (feof (f))
420
 
        {                       /* On terminals you could easiely read as many EOFs as you call         */
421
 
          rc = -1;              /* fread() or fgetc() repeatly. Every call will block until you press   */
422
 
          *ret_len = 0;         /* CTRL-D. So we catch this case before we call fread() again.          */
 
422
        { 
 
423
          /* On terminals you could easily read as many EOFs as you
 
424
             call fread() or fgetc() repeatly.  Every call will block
 
425
             until you press CTRL-D. So we catch this case before we
 
426
             call fread() again.  */
 
427
          rc = -1;              
 
428
          *ret_len = 0;         
423
429
        }
424
430
      else
425
431
        {
427
433
          nbytes = fread (buf, 1, size, f);
428
434
          if (feof (f) && !nbytes)
429
435
            {
430
 
              rc = -1;          /* okay: we can return EOF now. */
 
436
              rc = -1;  /* Okay: we can return EOF now. */
431
437
            }
432
438
          else if (ferror (f) && errno != EPIPE)
433
439
            {
469
475
            fclose (f);
470
476
        }
471
477
      f = NULL;
472
 
      xfree (a);                /* we can free our context now */
 
478
      xfree (a); /* We can free our context now. */
473
479
    }
474
480
#else /* !stdio implementation */
475
481
 
476
482
  if (control == IOBUFCTRL_UNDERFLOW)
477
483
    {
478
 
      assert (size);            /* need a buffer */
 
484
      assert (size); /* We need a buffer.  */
479
485
      if (a->eof_seen)
480
486
        {
481
487
          rc = -1;
620
626
        }
621
627
      f = INVALID_FP;
622
628
#endif
623
 
      xfree (a);                /* we can free our context now */
 
629
      xfree (a); /* We can free our context now. */
624
630
    }
625
 
#endif /* !stdio implementation */
 
631
#endif /* !stdio implementation. */
626
632
  return rc;
627
633
}
628
634
 
639
645
  size_t nbytes = 0;
640
646
  int rc = 0;
641
647
 
 
648
  (void)chain;
 
649
 
642
650
  if (control == IOBUFCTRL_UNDERFLOW)
643
651
    {
644
652
      assert (size);            /* need a buffer */
1012
1020
                   (byte *) & desc, &dummy_len);
1013
1021
 
1014
1022
      log_debug ("iobuf chain: %d.%d `%s' filter_eof=%d start=%d len=%d\n",
1015
 
                 a->no, a->subno, desc, a->filter_eof,
 
1023
                 a->no, a->subno, desc?desc:"?", a->filter_eof,
1016
1024
                 (int) a->d.start, (int) a->d.len);
1017
1025
    }
1018
1026
}
1069
1077
        log_error ("iobuf_flush failed on close: %s\n", gpg_strerror (rc));
1070
1078
 
1071
1079
      if (DBG_IOBUF)
1072
 
        log_debug ("iobuf-%d.%d: close `%s'\n", a->no, a->subno, a->desc);
 
1080
        log_debug ("iobuf-%d.%d: close `%s'\n", a->no, a->subno,
 
1081
                   a->desc?a->desc:"?");
1073
1082
      if (a->filter && (rc = a->filter (a->filter_ov, IOBUFCTRL_FREE,
1074
1083
                                        a->chain, NULL, &dummy_len)))
1075
1084
        log_error ("IOBUFCTRL_FREE failed on close: %s\n", gpg_strerror (rc));
1336
1345
  file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1337
1346
  file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1338
1347
  if (DBG_IOBUF)
1339
 
    log_debug ("iobuf-%d.%d: create `%s'\n", a->no, a->subno, a->desc);
 
1348
    log_debug ("iobuf-%d.%d: create `%s'\n", a->no, a->subno,
 
1349
               a->desc?a->desc:"?");
1340
1350
 
1341
1351
  return a;
1342
1352
}
1369
1379
  file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1370
1380
  file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1371
1381
  if (DBG_IOBUF)
1372
 
    log_debug ("iobuf-%d.%d: append `%s'\n", a->no, a->subno, a->desc);
 
1382
    log_debug ("iobuf-%d.%d: append `%s'\n", a->no, a->subno,
 
1383
               a->desc?a->desc:"?");
1373
1384
 
1374
1385
  return a;
1375
1386
}
1397
1408
  file_filter (fcx, IOBUFCTRL_DESC, NULL, (byte *) & a->desc, &len);
1398
1409
  file_filter (fcx, IOBUFCTRL_INIT, NULL, NULL, &len);
1399
1410
  if (DBG_IOBUF)
1400
 
    log_debug ("iobuf-%d.%d: openrw `%s'\n", a->no, a->subno, a->desc);
 
1411
    log_debug ("iobuf-%d.%d: openrw `%s'\n", a->no, a->subno,
 
1412
               a->desc?a->desc:"?");
1401
1413
 
1402
1414
  return a;
1403
1415
}
1410
1422
    {                           /* keep system filepointer/descriptor open */
1411
1423
      if (DBG_IOBUF)
1412
1424
        log_debug ("iobuf-%d.%d: ioctl `%s' keep=%d\n",
1413
 
                   a ? a->no : -1, a ? a->subno : -1, a ? a->desc : "?",
 
1425
                   a ? a->no : -1, a ? a->subno : -1, 
 
1426
                   a && a->desc ? a->desc : "?",
1414
1427
                   intval);
1415
1428
      for (; a; a = a->chain)
1416
1429
        if (!a->chain && a->filter == file_filter)
1445
1458
    {                           /* disallow/allow caching */
1446
1459
      if (DBG_IOBUF)
1447
1460
        log_debug ("iobuf-%d.%d: ioctl `%s' no_cache=%d\n",
1448
 
                   a ? a->no : -1, a ? a->subno : -1, a ? a->desc : "?",
 
1461
                   a ? a->no : -1, a ? a->subno : -1, 
 
1462
                   a && a->desc? a->desc : "?",
1449
1463
                   intval);
1450
1464
      for (; a; a = a->chain)
1451
1465
        if (!a->chain && a->filter == file_filter)
1546
1560
 
1547
1561
  if (DBG_IOBUF)
1548
1562
    {
1549
 
      log_debug ("iobuf-%d.%d: push `%s'\n", a->no, a->subno, a->desc);
 
1563
      log_debug ("iobuf-%d.%d: push `%s'\n", a->no, a->subno, 
 
1564
                 a->desc?a->desc:"?");
1550
1565
      print_chain (a);
1551
1566
    }
1552
1567
 
1573
1588
    BUG ();
1574
1589
 
1575
1590
  if (DBG_IOBUF)
1576
 
    log_debug ("iobuf-%d.%d: pop `%s'\n", a->no, a->subno, a->desc);
 
1591
    log_debug ("iobuf-%d.%d: pop `%s'\n", a->no, a->subno,
 
1592
               a->desc?a->desc:"?");
1577
1593
  if (!a->filter)
1578
1594
    {                           /* this is simple */
1579
1595
      b = a->chain;
1660
1676
          iobuf_t b = a->chain;
1661
1677
          if (DBG_IOBUF)
1662
1678
            log_debug ("iobuf-%d.%d: pop `%s' in underflow\n",
1663
 
                       a->no, a->subno, a->desc);
 
1679
                       a->no, a->subno, a->desc?a->desc:"?");
1664
1680
          xfree (a->d.buf);
1665
1681
          xfree (a->real_fname);
1666
1682
          memcpy (a, b, sizeof *a);
1733
1749
            {
1734
1750
              iobuf_t b = a->chain;
1735
1751
              if (DBG_IOBUF)
1736
 
                log_debug ("iobuf-%d.%d: pop `%s' in underflow (!len)\n",
1737
 
                           a->no, a->subno, a->desc);
 
1752
                log_debug ("iobuf-%d.%d: pop in underflow (!len)\n",
 
1753
                           a->no, a->subno);
1738
1754
              xfree (a->d.buf);
1739
1755
              xfree (a->real_fname);
1740
1756
              memcpy (a, b, sizeof *a);
2400
2416
# else
2401
2417
  {
2402
2418
    int x;
 
2419
    
 
2420
    (void)for_write;
2403
2421
 
2404
2422
    if (fd == 0)
2405
2423
      x = (int) GetStdHandle (STD_INPUT_HANDLE);
2417
2435
    fd = x;
2418
2436
  }
2419
2437
# endif
 
2438
#else
 
2439
  (void)for_write;
2420
2440
#endif
2421
2441
  return fd;
2422
2442
}