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))
166
155
typedef void *estream_mutex_t;
168
158
static inline void
169
159
dummy_mutex_call_void (estream_mutex_t mutex)
174
static int estream_pth_killed;
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)))
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))
188
200
/* Primitive system I/O. */
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)
195
208
# define ESTREAM_SYS_READ read
196
209
# define ESTREAM_SYS_WRITE write
412
425
es_pth_read (int fd, void *buffer, size_t size)
427
if (estream_pth_killed)
428
return read (fd, buffer, size);
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);
432
int rc = pth_read (fd, buffer, size);
433
if (rc == -1 && errno == EINVAL)
434
rc = read (fd, buffer, size);
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*/
425
443
es_pth_write (int fd, const void *buffer, size_t size)
445
if (estream_pth_killed)
446
return write (fd, buffer, size);
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);
450
int rc = pth_write (fd, buffer, size);
451
if (rc == -1 && errno == EINVAL)
452
rc = write (fd, buffer, size);
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*/
436
459
#endif /*HAVE_PTH*/
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. */
483
estream_pth_killed = 1;
449
492
* Initialization.
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. */
583
631
assert (mem_cookie->memory_size >= mem_cookie->offset);
584
632
nleft = mem_cookie->memory_size - mem_cookie->offset;
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)
596
644
if (!mem_cookie->memory_size)
597
645
newsize = size; /* Not yet allocated. */
599
newsize = mem_cookie->memory_size + (nleft - size);
647
newsize = mem_cookie->memory_size + (size - nleft);
600
648
if (newsize < mem_cookie->offset)
602
650
_set_errno (EINVAL);
623
671
_set_errno (ENOSPC);
627
675
newbuf = mem_cookie->func_realloc (mem_cookie->memory, newsize);
631
679
mem_cookie->memory = newbuf;
632
680
mem_cookie->memory_size = newsize;
634
682
assert (mem_cookie->memory_size >= mem_cookie->offset);
635
683
nleft = mem_cookie->memory_size - mem_cookie->offset;
637
685
assert (size <= nleft);
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;
902
950
/* Create function for fd objects. */
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)
907
955
estream_cookie_fp_t fp_cookie;
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". */
1199
1247
data_flushed = 0;
1202
1250
while ((((ssize_t) (stream->data_offset - data_flushed)) > 0) && (! err))
1204
1252
ret = (*func_write) (stream->intern->cookie,
1711
1759
/* Flushing resulted in empty container. */
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;
1718
1766
memcpy (stream->buffer + stream->data_offset,
1719
1767
buffer + data_written, data_to_write);
1720
1768
stream->data_offset += data_to_write;
2222
2270
create_called = 0;
2224
2272
err = es_convert_mode (mode, &modeflags);
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);
2234
2282
create_called = 1;
2235
2283
err = es_create (&stream, cookie, -1, estream_functions_mem, modeflags, 0);
2257
2305
modeflags |= O_RDWR;
2260
2308
if (es_func_mem_create (&cookie, NULL, 0, 0,
2261
2309
BUFFER_BLOCK_SIZE, 1,
2262
2310
mem_realloc, mem_free, modeflags,
2266
2314
if (es_create (&stream, cookie, -1, estream_functions_mem, modeflags, 0))
2267
2315
(*estream_functions_mem.func_close) (cookie);
2366
2414
err = es_func_fp_create (&cookie, fp, modeflags, no_close);
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);
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);
2451
2499
/* Second try is to use the standard C streams. */
2457
2505
stream = do_fpopen (stderr, "a", 1, 1);
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;
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);
2857
2905
estream_t ES__RESTRICT stream)
2859
2907
size_t ret, bytes;
2862
2909
if (size * nitems)
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);
2868
2915
ret = bytes / size;
2879
2926
estream_t ES__RESTRICT stream)
2881
2928
size_t ret, bytes;
2884
2930
if (size * nitems)
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);
2890
2936
ret = bytes / size;
3012
3058
considered a byte stream ending in a LF.
3014
3060
If MAX_LENGTH is not NULL, it shall point to a value with the
3015
maximum allowed allocation.
3061
maximum allowed allocation.
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.
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)
3077
3123
while ((c = es_getc_unlocked (stream)) != EOF)
3079
3125
if (nbytes == length)
3081
3127
/* Enlarge the buffer. */
3082
if (maxlen && length > maxlen)
3128
if (maxlen && length > maxlen)
3084
3130
/* We are beyond our limit: Skip the rest of the line. */
3085
3131
while (c != '\n' && (c=es_getc_unlocked (stream)) != EOF)
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. */
3214
3260
es_vasprintf (const char *ES__RESTRICT format, va_list ap)
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
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)
3496
3542
const unsigned char *p = buffer;
3500
3546
ESTREAM_LOCK (stream);
3501
3547
for (; length; length--, p++, count++)
3506
3552
&& (strchr (delimiters, *p) || *p == '\\')))
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.
3609
3655
THIS IS NOT A STANDARD ESTREAM FUNCTION AND ONLY USED BY GNUPG!. */
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)
3615
3661
const char *p = buffer;
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++ )
3621
3667
if ( (p[i] & 0x80) )