~ubuntu-branches/ubuntu/raring/gnupg2/raring

« back to all changes in this revision

Viewing changes to common/estream.c

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2012-11-06 11:25:58 UTC
  • mfrom: (1.1.16) (7.1.8 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20121106112558-lkpndvv4gqthgrn4
Tags: 2.0.19-1ubuntu1
* Resynchronize on Debian, remaining changes:
  - Add udev rules to give gpg access to some smartcard readers;
    Debian #543217.
    . debian/gnupg2.dev: udev rules to set ACLs on SCM smartcard readers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
150
150
/* Locking.  */
151
151
 
152
152
#ifdef HAVE_PTH
153
 
 
154
153
typedef pth_mutex_t estream_mutex_t;
155
 
# define ESTREAM_MUTEX_INITIALIZER PTH_MUTEX_INIT
156
 
# define ESTREAM_MUTEX_LOCK(mutex)        \
157
 
  pth_mutex_acquire (&(mutex), 0, NULL)
158
 
# define ESTREAM_MUTEX_UNLOCK(mutex)      \
159
 
  pth_mutex_release (&(mutex))
160
 
# define ESTREAM_MUTEX_TRYLOCK(mutex)     \
161
 
  ((pth_mutex_acquire (&(mutex), 1, NULL) == TRUE) ? 0 : -1)
162
 
# define ESTREAM_MUTEX_INITIALIZE(mutex)  \
163
 
  pth_mutex_init    (&(mutex))
164
 
#else
165
 
 
 
154
#else /*!HAVE_PTH*/
166
155
typedef void *estream_mutex_t;
 
156
#endif /*!HAVE_PTH*/
167
157
 
168
158
static inline void
169
159
dummy_mutex_call_void (estream_mutex_t mutex)
178
168
  return 0;
179
169
}
180
170
 
 
171
 
 
172
#ifdef HAVE_PTH
 
173
 
 
174
static int estream_pth_killed;
 
175
 
 
176
# define ESTREAM_MUTEX_INITIALIZER PTH_MUTEX_INIT
 
177
# define ESTREAM_MUTEX_LOCK(mutex)                              \
 
178
  (estream_pth_killed ? dummy_mutex_call_void ((mutex))         \
 
179
   : pth_mutex_acquire (&(mutex), 0, NULL))
 
180
# define ESTREAM_MUTEX_UNLOCK(mutex)                            \
 
181
  (estream_pth_killed ? dummy_mutex_call_void ((mutex))         \
 
182
   : pth_mutex_release (&(mutex)))
 
183
# define ESTREAM_MUTEX_TRYLOCK(mutex)                                   \
 
184
  (estream_pth_killed ? dummy_mutex_call_int ((mutex))                  \
 
185
   : ((pth_mutex_acquire (&(mutex), 1, NULL) == TRUE)? 0:-1))
 
186
# define ESTREAM_MUTEX_INITIALIZE(mutex)                        \
 
187
  (estream_pth_killed ? dummy_mutex_call_void ((mutex))         \
 
188
   : pth_mutex_init (&(mutex)))
 
189
 
 
190
#else /*!HAVE_PTH*/
 
191
 
181
192
# define ESTREAM_MUTEX_INITIALIZER NULL
182
193
# define ESTREAM_MUTEX_LOCK(mutex) dummy_mutex_call_void ((mutex))
183
194
# define ESTREAM_MUTEX_UNLOCK(mutex) dummy_mutex_call_void ((mutex))
184
195
# define ESTREAM_MUTEX_TRYLOCK(mutex) dummy_mutex_call_int ((mutex))
185
196
# define ESTREAM_MUTEX_INITIALIZE(mutex) dummy_mutex_call_void ((mutex))
186
 
#endif
 
197
 
 
198
#endif /*!HAVE_PTH*/
187
199
 
188
200
/* Primitive system I/O.  */
189
201
 
190
202
#ifdef HAVE_PTH
191
203
# define ESTREAM_SYS_READ  es_pth_read
192
204
# define ESTREAM_SYS_WRITE es_pth_write
193
 
# define ESTREAM_SYS_YIELD() pth_yield (NULL)
 
205
# define ESTREAM_SYS_YIELD() \
 
206
  do { if (!estream_pth_killed) pth_yield (NULL); } while (0)
194
207
#else
195
208
# define ESTREAM_SYS_READ  read
196
209
# define ESTREAM_SYS_WRITE write
361
374
es_list_remove (estream_t stream, int with_locked_list)
362
375
{
363
376
  estream_list_t list_obj;
364
 
  
 
377
 
365
378
  if (!with_locked_list)
366
379
    ESTREAM_LIST_LOCK;
367
380
  for (list_obj = estream_list; list_obj; list_obj = list_obj->cdr)
411
424
static int
412
425
es_pth_read (int fd, void *buffer, size_t size)
413
426
{
 
427
  if (estream_pth_killed)
 
428
    return read (fd, buffer, size);
 
429
  else
 
430
    {
414
431
# ifdef HAVE_W32_SYSTEM
415
 
  int rc = pth_read (fd, buffer, size);
416
 
  if (rc == -1 && errno == EINVAL)
417
 
    rc = read (fd, buffer, size);
418
 
  return rc;
 
432
      int rc = pth_read (fd, buffer, size);
 
433
      if (rc == -1 && errno == EINVAL)
 
434
        rc = read (fd, buffer, size);
 
435
      return rc;
419
436
# else /*!HAVE_W32_SYSTEM*/
420
 
  return pth_read (fd, buffer, size);
 
437
      return pth_read (fd, buffer, size);
421
438
# endif /* !HAVE_W32_SYSTEM*/
 
439
    }
422
440
}
423
441
 
424
442
static int
425
443
es_pth_write (int fd, const void *buffer, size_t size)
426
444
{
 
445
  if (estream_pth_killed)
 
446
    return write (fd, buffer, size);
 
447
  else
 
448
    {
427
449
# ifdef HAVE_W32_SYSTEM
428
 
  int rc = pth_write (fd, buffer, size);
429
 
  if (rc == -1 && errno == EINVAL)
430
 
    rc = write (fd, buffer, size);
431
 
  return rc;
 
450
      int rc = pth_write (fd, buffer, size);
 
451
      if (rc == -1 && errno == EINVAL)
 
452
        rc = write (fd, buffer, size);
 
453
      return rc;
432
454
# else /*!HAVE_W32_SYSTEM*/
433
 
  return pth_write (fd, buffer, size);
 
455
      return pth_write (fd, buffer, size);
434
456
# endif /* !HAVE_W32_SYSTEM*/
 
457
    }
435
458
}
436
459
#endif /*HAVE_PTH*/
437
460
 
445
468
}
446
469
 
447
470
 
 
471
/* A replacement for pth_kill.  The reason we need this is that after
 
472
   a pth_kill all our pth functions may not be used anymore.  Thus
 
473
   applications using estream and pth need to use this function
 
474
   instead of a plain pth_kill.  */
 
475
int
 
476
es_pth_kill (void)
 
477
{
 
478
#ifdef HAVE_PTH
 
479
  int rc;
 
480
 
 
481
  rc = pth_kill ();
 
482
  if (rc)
 
483
    estream_pth_killed = 1;
 
484
  return rc;
 
485
#else /*!HAVE_PTH*/
 
486
  return 0;
 
487
#endif /*!HAVE_PTH*/
 
488
}
 
489
 
 
490
 
448
491
/*
449
492
 * Initialization.
450
493
 */
457
500
  if (!initialized)
458
501
    {
459
502
#ifdef HAVE_PTH
460
 
      if (!pth_init () && errno != EPERM )
461
 
        return -1;
462
 
      if (pth_mutex_init (&estream_list_lock))
 
503
      if (estream_pth_killed)
463
504
        initialized = 1;
 
505
      else
 
506
        {
 
507
          if (!pth_init () && errno != EPERM )
 
508
            return -1;
 
509
          if (pth_mutex_init (&estream_list_lock))
 
510
            initialized = 1;
 
511
        }
464
512
#else
465
513
      initialized = 1;
466
514
#endif
467
 
      atexit (es_deinit);  
 
515
      atexit (es_deinit);
468
516
    }
469
517
  return 0;
470
518
}
497
545
 
498
546
 
499
547
/* Create function for memory objects.  DATA is either NULL or a user
500
 
   supplied buffer with the initial conetnt of the memory buffer.  If
 
548
   supplied buffer with the initial content of the memory buffer.  If
501
549
   DATA is NULL, DATA_N and DATA_LEN need to be 0 as well.  If DATA is
502
550
   not NULL, DATA_N gives the allocated size of DATA and DATA_LEN the
503
551
   used length in DATA.  */
557
605
      memcpy (buffer, mem_cookie->memory + mem_cookie->offset, size);
558
606
      mem_cookie->offset += size;
559
607
    }
560
 
  
 
608
 
561
609
  ret = size;
562
610
  return ret;
563
611
}
582
630
 
583
631
  assert (mem_cookie->memory_size >= mem_cookie->offset);
584
632
  nleft = mem_cookie->memory_size - mem_cookie->offset;
585
 
  
 
633
 
586
634
  /* If we are not allowed to grow limit the size to the left space.  */
587
635
  if (!mem_cookie->flags.grow && size > nleft)
588
636
    size = nleft;
596
644
      if (!mem_cookie->memory_size)
597
645
        newsize = size;  /* Not yet allocated.  */
598
646
      else
599
 
        newsize = mem_cookie->memory_size + (nleft - size);
 
647
        newsize = mem_cookie->memory_size + (size - nleft);
600
648
      if (newsize < mem_cookie->offset)
601
649
        {
602
650
          _set_errno (EINVAL);
623
671
          _set_errno (ENOSPC);
624
672
          return -1;
625
673
        }
626
 
      
 
674
 
627
675
      newbuf = mem_cookie->func_realloc (mem_cookie->memory, newsize);
628
676
      if (!newbuf)
629
677
        return -1;
630
 
      
 
678
 
631
679
      mem_cookie->memory = newbuf;
632
680
      mem_cookie->memory_size = newsize;
633
681
 
634
682
      assert (mem_cookie->memory_size >= mem_cookie->offset);
635
683
      nleft = mem_cookie->memory_size - mem_cookie->offset;
636
 
      
 
684
 
637
685
      assert (size <= nleft);
638
686
    }
639
 
      
 
687
 
640
688
  memcpy (mem_cookie->memory + mem_cookie->offset, buffer, size);
641
689
  if (mem_cookie->offset + size > mem_cookie->data_len)
642
690
    mem_cookie->data_len = mem_cookie->offset + size;
698
746
          _set_errno (ENOSPC);
699
747
          return -1;
700
748
        }
701
 
      
 
749
 
702
750
      newbuf = mem_cookie->func_realloc (mem_cookie->memory, newsize);
703
751
      if (!newbuf)
704
752
        return -1;
780
828
      *cookie = fd_cookie;
781
829
      err = 0;
782
830
    }
783
 
  
 
831
 
784
832
  return err;
785
833
}
786
834
 
791
839
{
792
840
  estream_cookie_fd_t file_cookie = cookie;
793
841
  ssize_t bytes_read;
794
 
  
 
842
 
795
843
  if (IS_INVALID_FD (file_cookie->fd))
796
844
    {
797
845
      ESTREAM_SYS_YIELD ();
799
847
    }
800
848
  else
801
849
    {
802
 
      do 
 
850
      do
803
851
        bytes_read = ESTREAM_SYS_READ (file_cookie->fd, buffer, size);
804
852
      while (bytes_read == -1 && errno == EINTR);
805
853
    }
901
949
 
902
950
/* Create function for fd objects.  */
903
951
static int
904
 
es_func_fp_create (void **cookie, FILE *fp, 
 
952
es_func_fp_create (void **cookie, FILE *fp,
905
953
                   unsigned int modeflags, int no_close)
906
954
{
907
955
  estream_cookie_fp_t fp_cookie;
924
972
      *cookie = fp_cookie;
925
973
      err = 0;
926
974
    }
927
 
  
 
975
 
928
976
  return err;
929
977
}
930
978
 
948
996
/* Write function for FILE* objects.  */
949
997
static ssize_t
950
998
es_func_fp_write (void *cookie, const void *buffer, size_t size)
951
 
                           
 
999
 
952
1000
{
953
1001
  estream_cookie_fp_t file_cookie = cookie;
954
1002
  size_t bytes_written;
973
1021
  if (!file_cookie->fp)
974
1022
    {
975
1023
      _set_errno (ESPIPE);
976
 
      return -1; 
 
1024
      return -1;
977
1025
    }
978
1026
 
979
1027
  if ( fseek (file_cookie->fp, (long int)*offset, whence) )
1114
1162
          oflags |= O_EXCL;
1115
1163
          break;
1116
1164
        default: /* Ignore unknown flags.  */
1117
 
          break; 
 
1165
          break;
1118
1166
        }
1119
1167
    }
1120
1168
 
1195
1243
         they were asked to write, we have to check for
1196
1244
         "(stream->data_offset - data_flushed) > 0" instead of
1197
1245
         "stream->data_offset - data_flushed".  */
1198
 
      
 
1246
 
1199
1247
      data_flushed = 0;
1200
1248
      err = 0;
1201
 
      
 
1249
 
1202
1250
      while ((((ssize_t) (stream->data_offset - data_flushed)) > 0) && (! err))
1203
1251
        {
1204
1252
          ret = (*func_write) (stream->intern->cookie,
1232
1280
    err = 0;
1233
1281
 
1234
1282
 out:
1235
 
    
 
1283
 
1236
1284
  if (err)
1237
1285
    stream->intern->indicators.err = 1;
1238
1286
 
1518
1566
      if (err)
1519
1567
        goto out;
1520
1568
      stream->flags.writing = 0;
1521
 
    }  
 
1569
    }
1522
1570
 
1523
1571
  /* Read unread data first.  */
1524
1572
  while ((bytes_to_read - data_read_unread) && stream->unread_data_len)
1615
1663
      off = off - stream->data_len + stream->data_offset;
1616
1664
      off -= stream->unread_data_len;
1617
1665
    }
1618
 
  
 
1666
 
1619
1667
  ret = (*func_seek) (stream->intern->cookie, &off, whence);
1620
1668
  if (ret == -1)
1621
1669
    {
1633
1681
  stream->intern->offset = off;
1634
1682
 
1635
1683
 out:
1636
 
  
 
1684
 
1637
1685
  if (err)
1638
1686
    stream->intern->indicators.err = 1;
1639
1687
 
1657
1705
    {
1658
1706
      err = EOPNOTSUPP;
1659
1707
      goto out;
1660
 
    }  
 
1708
    }
1661
1709
 
1662
1710
  data_written = 0;
1663
1711
  err = 0;
1664
 
  
 
1712
 
1665
1713
  while (bytes_to_write - data_written)
1666
1714
    {
1667
1715
      ret = (*func_write) (stream->intern->cookie,
1709
1757
      if (! err)
1710
1758
        {
1711
1759
          /* Flushing resulted in empty container.  */
1712
 
          
 
1760
 
1713
1761
          data_to_write = bytes_to_write - data_written;
1714
1762
          space_available = stream->buffer_size - stream->data_offset;
1715
1763
          if (data_to_write > space_available)
1716
1764
            data_to_write = space_available;
1717
 
              
 
1765
 
1718
1766
          memcpy (stream->buffer + stream->data_offset,
1719
1767
                  buffer + data_written, data_to_write);
1720
1768
          stream->data_offset += data_to_write;
1775
1823
 
1776
1824
  data_written = 0;
1777
1825
  err = 0;
1778
 
  
 
1826
 
1779
1827
  if (!stream->flags.writing)
1780
1828
    {
1781
1829
      /* Switching to writing mode -> discard input data and seek to
1810
1858
    }
1811
1859
 
1812
1860
 out:
1813
 
    
 
1861
 
1814
1862
  if (bytes_written)
1815
1863
    *bytes_written = data_written;
1816
1864
  if (data_written)
1834
1882
      if (err)
1835
1883
        goto out;
1836
1884
      stream->flags.writing = 0;
1837
 
    }  
 
1885
    }
1838
1886
 
1839
1887
  if (stream->data_offset == stream->data_len)
1840
1888
    {
1843
1891
      if (err)
1844
1892
        goto out;
1845
1893
    }
1846
 
  
 
1894
 
1847
1895
  if (data)
1848
1896
    *data = stream->buffer + stream->data_offset;
1849
1897
  if (data_len)
1898
1946
 
1899
1947
  err = es_func_mem_create (&line_stream_cookie, NULL, 0, 0,
1900
1948
                            BUFFER_BLOCK_SIZE, 1,
1901
 
                            mem_realloc, mem_free, 
 
1949
                            mem_realloc, mem_free,
1902
1950
                            O_RDWR,
1903
1951
                            0);
1904
1952
  if (err)
1953
2001
    goto out;
1954
2002
 
1955
2003
  /* Complete line has been written to line_stream.  */
1956
 
  
 
2004
 
1957
2005
  if ((max_length > 1) && (! line_size))
1958
2006
    {
1959
2007
      stream->intern->indicators.eof = 1;
2049
2097
es_get_indicator (estream_t stream, int ind_err, int ind_eof)
2050
2098
{
2051
2099
  int ret = 0;
2052
 
  
 
2100
 
2053
2101
  if (ind_err)
2054
2102
    ret = stream->intern->indicators.err;
2055
2103
  else if (ind_eof)
2076
2124
    es_empty (stream);
2077
2125
 
2078
2126
  es_set_indicators (stream, -1, 0);
2079
 
  
 
2127
 
2080
2128
  /* Free old buffer in case that was allocated by this function.  */
2081
2129
  if (stream->intern->deallocate_buffer)
2082
2130
    {
2090
2138
  else
2091
2139
    {
2092
2140
      void *buffer_new;
2093
 
      
 
2141
 
2094
2142
      if (buffer)
2095
2143
        buffer_new = buffer;
2096
2144
      else
2183
2231
  err = es_convert_mode (mode, &modeflags);
2184
2232
  if (err)
2185
2233
    goto out;
2186
 
  
 
2234
 
2187
2235
  err = es_func_file_create (&cookie, &fd, path, modeflags);
2188
2236
  if (err)
2189
2237
    goto out;
2197
2245
    fname_set_internal (stream, path, 1);
2198
2246
 
2199
2247
 out:
2200
 
  
 
2248
 
2201
2249
  if (err && create_called)
2202
2250
    (*estream_functions_fd.func_close) (cookie);
2203
2251
 
2220
2268
  cookie = 0;
2221
2269
  stream = NULL;
2222
2270
  create_called = 0;
2223
 
  
 
2271
 
2224
2272
  err = es_convert_mode (mode, &modeflags);
2225
2273
  if (err)
2226
2274
    goto out;
2227
2275
 
2228
2276
  err = es_func_mem_create (&cookie, data, data_n, data_len,
2229
 
                            BUFFER_BLOCK_SIZE, grow, 
 
2277
                            BUFFER_BLOCK_SIZE, grow,
2230
2278
                            func_realloc, func_free, modeflags, 0);
2231
2279
  if (err)
2232
2280
    goto out;
2233
 
  
 
2281
 
2234
2282
  create_called = 1;
2235
2283
  err = es_create (&stream, cookie, -1, estream_functions_mem, modeflags, 0);
2236
2284
 
2256
2304
    return NULL;
2257
2305
  modeflags |= O_RDWR;
2258
2306
 
2259
 
  
 
2307
 
2260
2308
  if (es_func_mem_create (&cookie, NULL, 0, 0,
2261
2309
                          BUFFER_BLOCK_SIZE, 1,
2262
2310
                          mem_realloc, mem_free, modeflags,
2263
2311
                          memlimit))
2264
2312
    return NULL;
2265
 
  
 
2313
 
2266
2314
  if (es_create (&stream, cookie, -1, estream_functions_mem, modeflags, 0))
2267
2315
    (*estream_functions_mem.func_close) (cookie);
2268
2316
 
2282
2330
 
2283
2331
  stream = NULL;
2284
2332
  modeflags = 0;
2285
 
  
 
2333
 
2286
2334
  err = es_convert_mode (mode, &modeflags);
2287
2335
  if (err)
2288
2336
    goto out;
2366
2414
  err = es_func_fp_create (&cookie, fp, modeflags, no_close);
2367
2415
  if (err)
2368
2416
    goto out;
2369
 
  
 
2417
 
2370
2418
  create_called = 1;
2371
2419
  err = es_create (&stream, cookie, fp? fileno (fp):-1, estream_functions_fp,
2372
2420
                   modeflags, with_locked_list);
2379
2427
  return stream;
2380
2428
}
2381
2429
 
2382
 
  
 
2430
 
2383
2431
/* Create an estream from the stdio stream FP.  This mechanism is
2384
2432
   useful in case the stdio streams have special properties and may
2385
2433
   not be mixed with fd based functions.  This is for example the case
2445
2493
        stream = do_fdopen (custom_std_fds[1], "a", 1, 1);
2446
2494
      else if (custom_std_fds_valid[2])
2447
2495
        stream = do_fdopen (custom_std_fds[2], "a", 1, 1);
2448
 
      
 
2496
 
2449
2497
      if (!stream)
2450
2498
        {
2451
2499
          /* Second try is to use the standard C streams.  */
2456
2504
          else
2457
2505
            stream = do_fpopen (stderr, "a", 1, 1);
2458
2506
        }
2459
 
      
2460
 
      if (!stream) 
 
2507
 
 
2508
      if (!stream)
2461
2509
        {
2462
2510
          /* Last try: Create a bit bucket.  */
2463
2511
          stream = do_fpopen (NULL, fd? "a":"r", 0, 1);
2473
2521
      stream->intern->stdstream_fd = fd;
2474
2522
      if (fd == 2)
2475
2523
        es_set_buffering (stream, NULL, _IOLBF, 0);
2476
 
      fname_set_internal (stream, 
 
2524
      fname_set_internal (stream,
2477
2525
                          fd == 0? "[stdin]" :
2478
2526
                          fd == 1? "[stdout]" : "[stderr]", 0);
2479
2527
    }
2497
2545
 
2498
2546
      cookie = NULL;
2499
2547
      create_called = 0;
2500
 
      
 
2548
 
2501
2549
      ESTREAM_LOCK (stream);
2502
2550
 
2503
2551
      es_deinitialize (stream);
2505
2553
      err = es_convert_mode (mode, &modeflags);
2506
2554
      if (err)
2507
2555
        goto leave;
2508
 
      
 
2556
 
2509
2557
      err = es_func_file_create (&cookie, &fd, path, modeflags);
2510
2558
      if (err)
2511
2559
        goto leave;
2519
2567
        {
2520
2568
          if (create_called)
2521
2569
            es_func_fd_destroy (cookie);
2522
 
      
 
2570
 
2523
2571
          es_destroy (stream, 0);
2524
2572
          stream = NULL;
2525
2573
        }
2654
2702
do_fflush (estream_t stream)
2655
2703
{
2656
2704
  int err;
2657
 
  
 
2705
 
2658
2706
  if (stream->flags.writing)
2659
2707
    err = es_flush (stream);
2660
2708
  else
2671
2719
es_fflush (estream_t stream)
2672
2720
{
2673
2721
  int err;
2674
 
  
 
2722
 
2675
2723
  if (stream)
2676
2724
    {
2677
2725
      ESTREAM_LOCK (stream);
2702
2750
es_fseeko (estream_t stream, off_t offset, int whence)
2703
2751
{
2704
2752
  int err;
2705
 
  
 
2753
 
2706
2754
  ESTREAM_LOCK (stream);
2707
2755
  err = es_seek (stream, offset, whence, NULL);
2708
2756
  ESTREAM_UNLOCK (stream);
2715
2763
es_ftell (estream_t stream)
2716
2764
{
2717
2765
  long int ret;
2718
 
  
 
2766
 
2719
2767
  ESTREAM_LOCK (stream);
2720
2768
  ret = es_offset_calculate (stream);
2721
2769
  ESTREAM_UNLOCK (stream);
2776
2824
es_fgetc (estream_t stream)
2777
2825
{
2778
2826
  int ret;
2779
 
  
 
2827
 
2780
2828
  ESTREAM_LOCK (stream);
2781
2829
  ret = es_getc_unlocked (stream);
2782
2830
  ESTREAM_UNLOCK (stream);
2789
2837
es_fputc (int c, estream_t stream)
2790
2838
{
2791
2839
  int ret;
2792
 
  
 
2840
 
2793
2841
  ESTREAM_LOCK (stream);
2794
2842
  ret = es_putc_unlocked (c, stream);
2795
2843
  ESTREAM_UNLOCK (stream);
2857
2905
          estream_t ES__RESTRICT stream)
2858
2906
{
2859
2907
  size_t ret, bytes;
2860
 
  int err;
2861
2908
 
2862
2909
  if (size * nitems)
2863
2910
    {
2864
2911
      ESTREAM_LOCK (stream);
2865
 
      err = es_readn (stream, ptr, size * nitems, &bytes);
 
2912
      es_readn (stream, ptr, size * nitems, &bytes);
2866
2913
      ESTREAM_UNLOCK (stream);
2867
2914
 
2868
2915
      ret = bytes / size;
2879
2926
           estream_t ES__RESTRICT stream)
2880
2927
{
2881
2928
  size_t ret, bytes;
2882
 
  int err;
2883
2929
 
2884
2930
  if (size * nitems)
2885
2931
    {
2886
2932
      ESTREAM_LOCK (stream);
2887
 
      err = es_writen (stream, ptr, size * nitems, &bytes);
 
2933
      es_writen (stream, ptr, size * nitems, &bytes);
2888
2934
      ESTREAM_UNLOCK (stream);
2889
2935
 
2890
2936
      ret = bytes / size;
2901
2947
{
2902
2948
  unsigned char *s = (unsigned char*)buffer;
2903
2949
  int c;
2904
 
   
 
2950
 
2905
2951
  if (!length)
2906
2952
    return NULL;
2907
 
     
 
2953
 
2908
2954
  c = EOF;
2909
2955
  ESTREAM_LOCK (stream);
2910
2956
  while (length > 1 && (c = es_getc_unlocked (stream)) != EOF && c != '\n')
2968
3014
  if (*n)
2969
3015
    {
2970
3016
      /* Caller wants us to use his buffer.  */
2971
 
      
 
3017
 
2972
3018
      if (*n < (line_n + 1))
2973
3019
        {
2974
3020
          /* Provided buffer is too small -> resize.  */
3012
3058
   considered a byte stream ending in a LF.
3013
3059
 
3014
3060
   If MAX_LENGTH is not NULL, it shall point to a value with the
3015
 
   maximum allowed allocation.  
 
3061
   maximum allowed allocation.
3016
3062
 
3017
3063
   Returns the length of the line. EOF is indicated by a line of
3018
3064
   length zero. A truncated line is indicated my setting the value at
3036
3082
   released using es_free.
3037
3083
 */
3038
3084
ssize_t
3039
 
es_read_line (estream_t stream, 
 
3085
es_read_line (estream_t stream,
3040
3086
              char **addr_of_buffer, size_t *length_of_buffer,
3041
3087
              size_t *max_length)
3042
3088
{
3048
3094
  char *p;
3049
3095
 
3050
3096
  if (!buffer)
3051
 
    { 
 
3097
    {
3052
3098
      /* No buffer given - allocate a new one. */
3053
3099
      length = 256;
3054
3100
      buffer = mem_alloc (length);
3077
3123
  while  ((c = es_getc_unlocked (stream)) != EOF)
3078
3124
    {
3079
3125
      if (nbytes == length)
3080
 
        { 
 
3126
        {
3081
3127
          /* Enlarge the buffer. */
3082
 
          if (maxlen && length > maxlen) 
 
3128
          if (maxlen && length > maxlen)
3083
3129
            {
3084
3130
              /* We are beyond our limit: Skip the rest of the line. */
3085
3131
              while (c != '\n' && (c=es_getc_unlocked (stream)) != EOF)
3096
3142
          if (!*addr_of_buffer)
3097
3143
            {
3098
3144
              int save_errno = errno;
3099
 
              mem_free (buffer); 
 
3145
              mem_free (buffer);
3100
3146
              *length_of_buffer = 0;
3101
3147
              if (max_length)
3102
3148
                *max_length = 0;
3106
3152
            }
3107
3153
          buffer = *addr_of_buffer;
3108
3154
          *length_of_buffer = length;
3109
 
          length -= 3; 
 
3155
          length -= 3;
3110
3156
          p = buffer + nbytes;
3111
3157
        }
3112
3158
      *p++ = c;
3144
3190
             va_list ap)
3145
3191
{
3146
3192
  int ret;
3147
 
  
 
3193
 
3148
3194
  ESTREAM_LOCK (stream);
3149
3195
  ret = es_print (stream, format, ap);
3150
3196
  ESTREAM_UNLOCK (stream);
3158
3204
                     const char *ES__RESTRICT format, ...)
3159
3205
{
3160
3206
  int ret;
3161
 
  
 
3207
 
3162
3208
  va_list ap;
3163
3209
  va_start (ap, format);
3164
3210
  ret = es_print (stream, format, ap);
3173
3219
            const char *ES__RESTRICT format, ...)
3174
3220
{
3175
3221
  int ret;
3176
 
  
 
3222
 
3177
3223
  va_list ap;
3178
3224
  va_start (ap, format);
3179
3225
  ESTREAM_LOCK (stream);
3210
3256
   should use es_free to release the buffer.  This function actually
3211
3257
   belongs into estream-printf but we put it here as a convenience
3212
3258
   and because es_free is required anyway.  */
3213
 
char * 
 
3259
char *
3214
3260
es_vasprintf (const char *ES__RESTRICT format, va_list ap)
3215
3261
{
3216
3262
  int rc;
3241
3287
  int pid = GetCurrentProcessId ();
3242
3288
  unsigned int value;
3243
3289
  int i;
3244
 
  
 
3290
 
3245
3291
  n = GetTempPath (MAX_PATH+1, buffer);
3246
3292
  if (!n || n > MAX_PATH || mystrlen (buffer) > MAX_PATH)
3247
3293
    {
3307
3353
 
3308
3354
  fp = NULL;
3309
3355
  fd = -1;
3310
 
  
 
3356
 
3311
3357
  fp = tmpfile ();
3312
3358
  if (! fp)
3313
3359
    goto out;
3338
3384
  stream = NULL;
3339
3385
  modeflags = O_RDWR | O_TRUNC | O_CREAT;
3340
3386
  cookie = NULL;
3341
 
  
 
3387
 
3342
3388
  fd = tmpfd ();
3343
3389
  if (fd == -1)
3344
3390
    {
3363
3409
        close (fd);
3364
3410
      stream = NULL;
3365
3411
    }
3366
 
  
 
3412
 
3367
3413
  return stream;
3368
3414
}
3369
3415
 
3373
3419
            char *ES__RESTRICT buf, int type, size_t size)
3374
3420
{
3375
3421
  int err;
3376
 
  
 
3422
 
3377
3423
  if ((type == _IOFBF || type == _IOLBF || type == _IONBF)
3378
3424
      && (!buf || size || type == _IONBF))
3379
3425
    {
3412
3458
es_opaque_get (estream_t stream)
3413
3459
{
3414
3460
  void *opaque;
3415
 
  
 
3461
 
3416
3462
  ESTREAM_LOCK (stream);
3417
3463
  es_opaque_ctrl (stream, NULL, &opaque);
3418
3464
  ESTREAM_UNLOCK (stream);
3487
3533
   Returns 0 on success or -1 on error.  If BYTES_WRITTEN is not NULL
3488
3534
   the number of bytes actually written are stored at this
3489
3535
   address.  */
3490
 
int 
 
3536
int
3491
3537
es_write_sanitized (estream_t ES__RESTRICT stream,
3492
3538
                    const void * ES__RESTRICT buffer, size_t length,
3493
 
                    const char * delimiters, 
 
3539
                    const char * delimiters,
3494
3540
                    size_t * ES__RESTRICT bytes_written)
3495
3541
{
3496
3542
  const unsigned char *p = buffer;
3500
3546
  ESTREAM_LOCK (stream);
3501
3547
  for (; length; length--, p++, count++)
3502
3548
    {
3503
 
      if (*p < 0x20 
 
3549
      if (*p < 0x20
3504
3550
          || *p == 0x7f
3505
 
          || (delimiters 
 
3551
          || (delimiters
3506
3552
              && (strchr (delimiters, *p) || *p == '\\')))
3507
3553
        {
3508
3554
          es_putc_unlocked ('\\', stream);
3604
3650
#ifdef GNUPG_MAJOR_VERSION
3605
3651
/* Special estream function to print an UTF8 string in the native
3606
3652
   encoding.  The interface is the same as es_write_sanitized, however
3607
 
   only one delimiter may be supported. 
 
3653
   only one delimiter may be supported.
3608
3654
 
3609
3655
   THIS IS NOT A STANDARD ESTREAM FUNCTION AND ONLY USED BY GNUPG!. */
3610
3656
int
3611
3657
es_write_sanitized_utf8_buffer (estream_t stream,
3612
 
                                const void *buffer, size_t length, 
 
3658
                                const void *buffer, size_t length,
3613
3659
                                const char *delimiters, size_t *bytes_written)
3614
3660
{
3615
3661
  const char *p = buffer;
3616
3662
  size_t i;
3617
3663
 
3618
3664
  /* We can handle plain ascii simpler, so check for it first. */
3619
 
  for (i=0; i < length; i++ ) 
 
3665
  for (i=0; i < length; i++ )
3620
3666
    {
3621
3667
      if ( (p[i] & 0x80) )
3622
3668
        break;