Home | History | Annotate | Line # | Download | only in src
      1 /*
      2  * Copyright (C) 1996-2005 The Free Software Foundation, Inc.
      3  *
      4  * This program is free software; you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation; either version 2, or (at your option)
      7  * any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  */
     14 #include <sys/cdefs.h>
     15 __RCSID("$NetBSD: buffer.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
     16 
     17 /* Code for the buffer data structure.  */
     18 
     19 #include "cvs.h"
     20 #include "buffer.h"
     21 #include "pagealign_alloc.h"
     22 
     23 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
     24 
     25 # include <sys/socket.h>
     26 
     27 /* OS/2 doesn't have EIO.  FIXME: this whole notion of turning
     28    a different error into EIO strikes me as pretty dubious.  */
     29 # if !defined( EIO )
     30 #   define EIO EBADPOS
     31 # endif
     32 
     33 /* Local functions.  */
     34 static void buf_default_memory_error (struct buffer *);
     35 static struct buffer_data *get_buffer_data (void);
     36 
     37 
     38 
     39 /* Initialize a buffer structure.  */
     40 struct buffer *
     41 buf_initialize (type_buf_input input,
     42                 type_buf_output output,
     43                 type_buf_flush flush,
     44                 type_buf_block block,
     45                 type_buf_get_fd get_fd,
     46                 type_buf_shutdown shutdown,
     47                 type_buf_memory_error memory_error,
     48                 void *closure)
     49 {
     50     struct buffer *buf;
     51 
     52     buf = xmalloc (sizeof (struct buffer));
     53     buf->data = NULL;
     54     buf->last = NULL;
     55     buf->nonblocking = false;
     56     buf->input = input;
     57     buf->output = output;
     58     buf->flush = flush;
     59     buf->block = block;
     60     buf->get_fd = get_fd;
     61     buf->shutdown = shutdown;
     62     buf->memory_error = memory_error ? memory_error : buf_default_memory_error;
     63     buf->closure = closure;
     64     return buf;
     65 }
     66 
     67 
     68 
     69 /* Free a buffer structure.  */
     70 void
     71 buf_free (struct buffer *buf)
     72 {
     73     if (buf->closure != NULL)
     74     {
     75 	free (buf->closure);
     76 	buf->closure = NULL;
     77     }
     78     buf_free_data (buf);
     79     free (buf);
     80 }
     81 
     82 
     83 
     84 /* Initialize a buffer structure which is not to be used for I/O.  */
     85 struct buffer *
     86 buf_nonio_initialize( void (*memory) (struct buffer *) )
     87 {
     88     return buf_initialize (NULL, NULL, NULL, NULL, NULL, NULL, memory, NULL);
     89 }
     90 
     91 
     92 
     93 /* Default memory error handler.  */
     94 static void
     95 buf_default_memory_error (struct buffer *buf)
     96 {
     97     error (1, 0, "out of memory");
     98 }
     99 
    100 
    101 
    102 /* Allocate more buffer_data structures.  */
    103 /* Get a new buffer_data structure.  */
    104 static struct buffer_data *
    105 get_buffer_data (void)
    106 {
    107     struct buffer_data *ret;
    108 
    109     ret = xmalloc (sizeof (struct buffer_data));
    110     ret->text = pagealign_xalloc (BUFFER_DATA_SIZE);
    111 
    112     return ret;
    113 }
    114 
    115 
    116 
    117 /* See whether a buffer and its file descriptor is empty.  */
    118 int
    119 buf_empty (buf)
    120     struct buffer *buf;
    121 {
    122 	/* Try and read any data on the file descriptor first.
    123 	 * We already know the descriptor is non-blocking.
    124 	 */
    125 	buf_input_data (buf, NULL);
    126 	return buf_empty_p (buf);
    127 }
    128 
    129 
    130 
    131 /* See whether a buffer is empty.  */
    132 int
    133 buf_empty_p (struct buffer *buf)
    134 {
    135     struct buffer_data *data;
    136 
    137     for (data = buf->data; data != NULL; data = data->next)
    138 	if (data->size > 0)
    139 	    return 0;
    140     return 1;
    141 }
    142 
    143 
    144 
    145 # if defined (SERVER_FLOWCONTROL) || defined (PROXY_SUPPORT)
    146 /*
    147  * Count how much data is stored in the buffer..
    148  * Note that each buffer is a xmalloc'ed chunk BUFFER_DATA_SIZE.
    149  */
    150 int
    151 buf_count_mem (struct buffer *buf)
    152 {
    153     struct buffer_data *data;
    154     int mem = 0;
    155 
    156     for (data = buf->data; data != NULL; data = data->next)
    157 	mem += BUFFER_DATA_SIZE;
    158 
    159     return mem;
    160 }
    161 # endif /* SERVER_FLOWCONTROL || PROXY_SUPPORT */
    162 
    163 
    164 
    165 /* Add data DATA of length LEN to BUF.  */
    166 void
    167 buf_output (struct buffer *buf, const char *data, size_t len)
    168 {
    169     if (buf->data != NULL
    170 	&& (((buf->last->text + BUFFER_DATA_SIZE)
    171 	     - (buf->last->bufp + buf->last->size))
    172 	    >= len))
    173     {
    174 	memcpy (buf->last->bufp + buf->last->size, data, len);
    175 	buf->last->size += len;
    176 	return;
    177     }
    178 
    179     while (1)
    180     {
    181 	struct buffer_data *newdata;
    182 
    183 	newdata = get_buffer_data ();
    184 	if (newdata == NULL)
    185 	{
    186 	    (*buf->memory_error) (buf);
    187 	    return;
    188 	}
    189 
    190 	if (buf->data == NULL)
    191 	    buf->data = newdata;
    192 	else
    193 	    buf->last->next = newdata;
    194 	newdata->next = NULL;
    195 	buf->last = newdata;
    196 
    197 	newdata->bufp = newdata->text;
    198 
    199 	if (len <= BUFFER_DATA_SIZE)
    200 	{
    201 	    newdata->size = len;
    202 	    memcpy (newdata->text, data, len);
    203 	    return;
    204 	}
    205 
    206 	newdata->size = BUFFER_DATA_SIZE;
    207 	memcpy (newdata->text, data, BUFFER_DATA_SIZE);
    208 
    209 	data += BUFFER_DATA_SIZE;
    210 	len -= BUFFER_DATA_SIZE;
    211     }
    212 
    213     /*NOTREACHED*/
    214 }
    215 
    216 
    217 
    218 /* Add a '\0' terminated string to BUF.  */
    219 void
    220 buf_output0 (struct buffer *buf, const char *string)
    221 {
    222     buf_output (buf, string, strlen (string));
    223 }
    224 
    225 
    226 
    227 /* Add a single character to BUF.  */
    228 void
    229 buf_append_char (struct buffer *buf, int ch)
    230 {
    231     if (buf->data != NULL
    232 	&& (buf->last->text + BUFFER_DATA_SIZE
    233 	    != buf->last->bufp + buf->last->size))
    234     {
    235 	*(buf->last->bufp + buf->last->size) = ch;
    236 	++buf->last->size;
    237     }
    238     else
    239     {
    240 	char b;
    241 
    242 	b = ch;
    243 	buf_output (buf, &b, 1);
    244     }
    245 }
    246 
    247 
    248 
    249 /* Free struct buffer_data's from the list starting with FIRST and ending at
    250  * LAST, inclusive.
    251  */
    252 static inline void
    253 buf_free_datas (struct buffer_data *first, struct buffer_data *last)
    254 {
    255     struct buffer_data *b, *n, *p;
    256     b = first;
    257     do
    258     {
    259 	p = b;
    260 	n = b->next;
    261 	pagealign_free (b->text);
    262 	free (b);
    263 	b = n;
    264     } while (p != last);
    265 }
    266 
    267 
    268 
    269 /*
    270  * Send all the output we've been saving up.  Returns 0 for success or
    271  * errno code.  If the buffer has been set to be nonblocking, this
    272  * will just write until the write would block.
    273  */
    274 int
    275 buf_send_output (struct buffer *buf)
    276 {
    277     assert (buf->output != NULL);
    278 
    279     while (buf->data != NULL)
    280     {
    281 	struct buffer_data *data;
    282 
    283 	data = buf->data;
    284 
    285 	if (data->size > 0)
    286 	{
    287 	    int status;
    288 	    size_t nbytes;
    289 
    290 	    status = (*buf->output) (buf->closure, data->bufp, data->size,
    291 				     &nbytes);
    292 	    if (status != 0)
    293 	    {
    294 		/* Some sort of error.  Discard the data, and return.  */
    295 		buf_free_data (buf);
    296 	        return status;
    297 	    }
    298 
    299 	    if (nbytes != data->size)
    300 	    {
    301 		/* Not all the data was written out.  This is only
    302                    permitted in nonblocking mode.  Adjust the buffer,
    303                    and return.  */
    304 
    305 		assert (buf->nonblocking);
    306 
    307 		data->size -= nbytes;
    308 		data->bufp += nbytes;
    309 
    310 		return 0;
    311 	    }
    312 	}
    313 
    314 	buf->data = data->next;
    315 	buf_free_datas (data, data);
    316     }
    317 
    318     buf->last = NULL;
    319 
    320     return 0;
    321 }
    322 
    323 
    324 
    325 /*
    326  * Flush any data queued up in the buffer.  If BLOCK is nonzero, then
    327  * if the buffer is in nonblocking mode, put it into blocking mode for
    328  * the duration of the flush.  This returns 0 on success, or an error
    329  * code.
    330  */
    331 int
    332 buf_flush (struct buffer *buf, bool block)
    333 {
    334     int nonblocking;
    335     int status;
    336 
    337     assert (buf->flush != NULL);
    338 
    339     nonblocking = buf->nonblocking;
    340     if (nonblocking && block)
    341     {
    342         status = set_block (buf);
    343 	if (status != 0)
    344 	    return status;
    345     }
    346 
    347     status = buf_send_output (buf);
    348     if (status == 0)
    349         status = (*buf->flush) (buf->closure);
    350 
    351     if (nonblocking && block)
    352     {
    353         int blockstat;
    354 
    355         blockstat = set_nonblock (buf);
    356 	if (status == 0)
    357 	    status = blockstat;
    358     }
    359 
    360     return status;
    361 }
    362 
    363 
    364 
    365 /*
    366  * Set buffer BUF to nonblocking I/O.  Returns 0 for success or errno
    367  * code.
    368  */
    369 int
    370 set_nonblock (struct buffer *buf)
    371 {
    372     int status;
    373 
    374     if (buf->nonblocking)
    375 	return 0;
    376     assert (buf->block != NULL);
    377     status = (*buf->block) (buf->closure, 0);
    378     if (status != 0)
    379 	return status;
    380     buf->nonblocking = true;
    381     return 0;
    382 }
    383 
    384 
    385 
    386 /*
    387  * Set buffer BUF to blocking I/O.  Returns 0 for success or errno
    388  * code.
    389  */
    390 int
    391 set_block (struct buffer *buf)
    392 {
    393     int status;
    394 
    395     if (! buf->nonblocking)
    396 	return 0;
    397     assert (buf->block != NULL);
    398     status = (*buf->block) (buf->closure, 1);
    399     if (status != 0)
    400 	return status;
    401     buf->nonblocking = false;
    402     return 0;
    403 }
    404 
    405 
    406 
    407 /*
    408  * Send a character count and some output.  Returns errno code or 0 for
    409  * success.
    410  *
    411  * Sending the count in binary is OK since this is only used on a pipe
    412  * within the same system.
    413  */
    414 int
    415 buf_send_counted (struct buffer *buf)
    416 {
    417     int size;
    418     struct buffer_data *data;
    419 
    420     size = 0;
    421     for (data = buf->data; data != NULL; data = data->next)
    422 	size += data->size;
    423 
    424     data = get_buffer_data ();
    425     if (data == NULL)
    426     {
    427 	(*buf->memory_error) (buf);
    428 	return ENOMEM;
    429     }
    430 
    431     data->next = buf->data;
    432     buf->data = data;
    433     if (buf->last == NULL)
    434 	buf->last = data;
    435 
    436     data->bufp = data->text;
    437     data->size = sizeof (int);
    438 
    439     *((int *) data->text) = size;
    440 
    441     return buf_send_output (buf);
    442 }
    443 
    444 
    445 
    446 /*
    447  * Send a special count.  COUNT should be negative.  It will be
    448  * handled specially by buf_copy_counted.  This function returns 0 or
    449  * an errno code.
    450  *
    451  * Sending the count in binary is OK since this is only used on a pipe
    452  * within the same system.
    453  */
    454 int
    455 buf_send_special_count (struct buffer *buf, int count)
    456 {
    457     struct buffer_data *data;
    458 
    459     data = get_buffer_data ();
    460     if (data == NULL)
    461     {
    462 	(*buf->memory_error) (buf);
    463 	return ENOMEM;
    464     }
    465 
    466     data->next = buf->data;
    467     buf->data = data;
    468     if (buf->last == NULL)
    469 	buf->last = data;
    470 
    471     data->bufp = data->text;
    472     data->size = sizeof (int);
    473 
    474     *((int *) data->text) = count;
    475 
    476     return buf_send_output (buf);
    477 }
    478 
    479 
    480 
    481 /* Append a list of buffer_data structures to an buffer.  */
    482 void
    483 buf_append_data (struct buffer *buf, struct buffer_data *data,
    484                  struct buffer_data *last)
    485 {
    486     if (data != NULL)
    487     {
    488 	if (buf->data == NULL)
    489 	    buf->data = data;
    490 	else
    491 	    buf->last->next = data;
    492 	buf->last = last;
    493     }
    494 }
    495 
    496 
    497 
    498 # ifdef PROXY_SUPPORT
    499 /* Copy data structures and append them to a buffer.
    500  *
    501  * ERRORS
    502  *   Failure to allocate memory here is fatal.
    503  */
    504 void
    505 buf_copy_data (struct buffer *buf, struct buffer_data *data,
    506                struct buffer_data *last)
    507 {
    508     struct buffer_data *first, *new, *cur, *prev;
    509 
    510     assert (buf);
    511     assert (data);
    512 
    513     prev = first = NULL;
    514     cur = data;
    515     while (1)
    516     {
    517 	new = get_buffer_data ();
    518 	if (!new) error (1, errno, "Failed to allocate buffer data.");
    519 
    520 	if (!first) first = new;
    521 	memcpy (new->text, cur->bufp, cur->size);
    522 	new->bufp = new->text;
    523 	new->size = cur->size;
    524 	new->next = NULL;
    525 	if (prev) prev->next = new;
    526 	if (cur == last) break;
    527 	prev = new;
    528 	cur = cur->next;
    529     }
    530 
    531     buf_append_data (buf, first, new);
    532 }
    533 # endif /* PROXY_SUPPORT */
    534 
    535 
    536 
    537 /* Dispose of any remaining data in the buffer.  */
    538 void
    539 buf_free_data (struct buffer *buffer)
    540 {
    541     if (buf_empty_p (buffer)) return;
    542     buf_free_datas (buffer->data, buffer->last);
    543     buffer->data = buffer->last = NULL;
    544 }
    545 
    546 
    547 
    548 /* Append the data in one buffer to another.  This removes the data
    549  * from the source buffer.
    550  */
    551 void
    552 buf_append_buffer (struct buffer *to, struct buffer *from)
    553 {
    554     struct buffer_data *n;
    555 
    556     /* Copy the data pointer to the new buf.  */
    557     buf_append_data (to, from->data, from->last);
    558 
    559     n = from->data;
    560     while (n)
    561     {
    562 	if (n == from->last) break;
    563 	n = n->next;
    564     }
    565 
    566     /* Remove from the original location.  */
    567     from->data = NULL;
    568     from->last = NULL;
    569 }
    570 
    571 
    572 
    573 /*
    574  * Copy the contents of file F into buffer_data structures.  We can't
    575  * copy directly into an buffer, because we want to handle failure and
    576  * success differently.  Returns 0 on success, or -2 if out of
    577  * memory, or a status code on error.  Since the caller happens to
    578  * know the size of the file, it is passed in as SIZE.  On success,
    579  * this function sets *RETP and *LASTP, which may be passed to
    580  * buf_append_data.
    581  */
    582 int
    583 buf_read_file (FILE *f, long int size, struct buffer_data **retp,
    584                struct buffer_data **lastp)
    585 {
    586     int status;
    587 
    588     *retp = NULL;
    589     *lastp = NULL;
    590 
    591     while (size > 0)
    592     {
    593 	struct buffer_data *data;
    594 	int get;
    595 
    596 	data = get_buffer_data ();
    597 	if (data == NULL)
    598 	{
    599 	    status = -2;
    600 	    goto error_return;
    601 	}
    602 
    603 	if (*retp == NULL)
    604 	    *retp = data;
    605 	else
    606 	    (*lastp)->next = data;
    607 	data->next = NULL;
    608 	*lastp = data;
    609 
    610 	data->bufp = data->text;
    611 	data->size = 0;
    612 
    613 	if (size > BUFFER_DATA_SIZE)
    614 	    get = BUFFER_DATA_SIZE;
    615 	else
    616 	    get = size;
    617 
    618 	errno = EIO;
    619 	if (fread (data->text, get, 1, f) != 1)
    620 	{
    621 	    status = errno;
    622 	    goto error_return;
    623 	}
    624 
    625 	data->size += get;
    626 	size -= get;
    627     }
    628 
    629     return 0;
    630 
    631   error_return:
    632     if (*retp != NULL)
    633 	buf_free_datas (*retp, (*lastp)->next);
    634     return status;
    635 }
    636 
    637 
    638 
    639 /*
    640  * Copy the contents of file F into buffer_data structures.  We can't
    641  * copy directly into an buffer, because we want to handle failure and
    642  * success differently.  Returns 0 on success, or -2 if out of
    643  * memory, or a status code on error.  On success, this function sets
    644  * *RETP and *LASTP, which may be passed to buf_append_data.
    645  */
    646 int
    647 buf_read_file_to_eof (FILE *f, struct buffer_data **retp,
    648                       struct buffer_data **lastp)
    649 {
    650     int status;
    651 
    652     *retp = NULL;
    653     *lastp = NULL;
    654 
    655     while (!feof (f))
    656     {
    657 	struct buffer_data *data;
    658 	int get, nread;
    659 
    660 	data = get_buffer_data ();
    661 	if (data == NULL)
    662 	{
    663 	    status = -2;
    664 	    goto error_return;
    665 	}
    666 
    667 	if (*retp == NULL)
    668 	    *retp = data;
    669 	else
    670 	    (*lastp)->next = data;
    671 	data->next = NULL;
    672 	*lastp = data;
    673 
    674 	data->bufp = data->text;
    675 	data->size = 0;
    676 
    677 	get = BUFFER_DATA_SIZE;
    678 
    679 	errno = EIO;
    680 	nread = fread (data->text, 1, get, f);
    681 	if (nread == 0 && !feof (f))
    682 	{
    683 	    status = errno;
    684 	    goto error_return;
    685 	}
    686 
    687 	data->size = nread;
    688     }
    689 
    690     return 0;
    691 
    692   error_return:
    693     if (*retp != NULL)
    694 	buf_free_datas (*retp, (*lastp)->next);
    695     return status;
    696 }
    697 
    698 
    699 
    700 /* Return the number of bytes in a chain of buffer_data structures.  */
    701 int
    702 buf_chain_length (struct buffer_data *buf)
    703 {
    704     int size = 0;
    705     while (buf)
    706     {
    707 	size += buf->size;
    708 	buf = buf->next;
    709     }
    710     return size;
    711 }
    712 
    713 
    714 
    715 /* Return the number of bytes in a buffer.  */
    716 int
    717 buf_length (struct buffer *buf)
    718 {
    719     return buf_chain_length (buf->data);
    720 }
    721 
    722 
    723 
    724 /*
    725  * Read an arbitrary amount of data into an input buffer.  The buffer
    726  * will be in nonblocking mode, and we just grab what we can.  Return
    727  * 0 on success, or -1 on end of file, or -2 if out of memory, or an
    728  * error code.  If COUNTP is not NULL, *COUNTP is set to the number of
    729  * bytes read.
    730  */
    731 int
    732 buf_input_data (struct buffer *buf, size_t *countp)
    733 {
    734     assert (buf->input != NULL);
    735 
    736     if (countp != NULL)
    737 	*countp = 0;
    738 
    739     while (1)
    740     {
    741 	int status;
    742 	size_t get, nbytes;
    743 
    744 	if (buf->data == NULL
    745 	    || (buf->last->bufp + buf->last->size
    746 		== buf->last->text + BUFFER_DATA_SIZE))
    747 	{
    748 	    struct buffer_data *data;
    749 
    750 	    data = get_buffer_data ();
    751 	    if (data == NULL)
    752 	    {
    753 		(*buf->memory_error) (buf);
    754 		return -2;
    755 	    }
    756 
    757 	    if (buf->data == NULL)
    758 		buf->data = data;
    759 	    else
    760 		buf->last->next = data;
    761 	    data->next = NULL;
    762 	    buf->last = data;
    763 
    764 	    data->bufp = data->text;
    765 	    data->size = 0;
    766 	}
    767 
    768 	get = ((buf->last->text + BUFFER_DATA_SIZE)
    769 	       - (buf->last->bufp + buf->last->size));
    770 
    771 	status = (*buf->input) (buf->closure,
    772 				buf->last->bufp + buf->last->size,
    773 				0, get, &nbytes);
    774 	if (status != 0)
    775 	    return status;
    776 
    777 	buf->last->size += nbytes;
    778 	if (countp != NULL)
    779 	    *countp += nbytes;
    780 
    781 	if (nbytes < get)
    782 	{
    783 	    /* If we did not fill the buffer, then presumably we read
    784                all the available data.  */
    785 	    return 0;
    786 	}
    787     }
    788 
    789     /*NOTREACHED*/
    790 }
    791 
    792 
    793 
    794 /*
    795  * Read a line (characters up to a \012) from an input buffer.  (We
    796  * use \012 rather than \n for the benefit of non Unix clients for
    797  * which \n means something else).  This returns 0 on success, or -1
    798  * on end of file, or -2 if out of memory, or an error code.  If it
    799  * succeeds, it sets *LINE to an allocated buffer holding the contents
    800  * of the line.  The trailing \012 is not included in the buffer.  If
    801  * LENP is not NULL, then *LENP is set to the number of bytes read;
    802  * strlen may not work, because there may be embedded null bytes.
    803  */
    804 int
    805 buf_read_line (struct buffer *buf, char **line, size_t *lenp)
    806 {
    807     return buf_read_short_line (buf, line, lenp, SIZE_MAX);
    808 }
    809 
    810 
    811 
    812 /* Like buf_read_line, but return -2 if no newline is found in MAX characters.
    813  */
    814 int
    815 buf_read_short_line (struct buffer *buf, char **line, size_t *lenp,
    816                      size_t max)
    817 {
    818     assert (buf->input != NULL);
    819 
    820     *line = NULL;
    821 
    822     while (1)
    823     {
    824 	size_t len, finallen, predicted_len;
    825 	struct buffer_data *data;
    826 	char *nl;
    827 
    828 	/* See if there is a newline in BUF.  */
    829 	len = 0;
    830 	for (data = buf->data; data != NULL; data = data->next)
    831 	{
    832 	    nl = memchr (data->bufp, '\012', data->size);
    833 	    if (nl != NULL)
    834 	    {
    835 	        finallen = nl - data->bufp;
    836 		if (xsum (len, finallen) >= max) return -2;
    837 	        len += finallen;
    838 		break;
    839 	    }
    840 	    else if (xsum (len, data->size) >= max) return -2;
    841 	    len += data->size;
    842 	}
    843 
    844 	/* If we found a newline, copy the line into a memory buffer,
    845            and remove it from BUF.  */
    846 	if (data != NULL)
    847 	{
    848 	    char *p;
    849 	    struct buffer_data *nldata;
    850 
    851 	    p = xmalloc (len + 1);
    852 	    if (p == NULL)
    853 		return -2;
    854 	    *line = p;
    855 
    856 	    nldata = data;
    857 	    data = buf->data;
    858 	    while (data != nldata)
    859 	    {
    860 		struct buffer_data *next;
    861 
    862 		memcpy (p, data->bufp, data->size);
    863 		p += data->size;
    864 		next = data->next;
    865 		buf_free_datas (data, data);
    866 		data = next;
    867 	    }
    868 
    869 	    memcpy (p, data->bufp, finallen);
    870 	    p[finallen] = '\0';
    871 
    872 	    data->size -= finallen + 1;
    873 	    data->bufp = nl + 1;
    874 	    buf->data = data;
    875 
    876 	    if (lenp != NULL)
    877 	        *lenp = len;
    878 
    879 	    return 0;
    880 	}
    881 
    882 	/* Read more data until we get a newline or MAX characters.  */
    883 	predicted_len = 0;
    884 	while (1)
    885 	{
    886 	    int status;
    887 	    size_t size, nbytes;
    888 	    char *mem;
    889 
    890 	    if (buf->data == NULL
    891 		|| (buf->last->bufp + buf->last->size
    892 		    == buf->last->text + BUFFER_DATA_SIZE))
    893 	    {
    894 		data = get_buffer_data ();
    895 		if (data == NULL)
    896 		{
    897 		    (*buf->memory_error) (buf);
    898 		    return -2;
    899 		}
    900 
    901 		if (buf->data == NULL)
    902 		    buf->data = data;
    903 		else
    904 		    buf->last->next = data;
    905 		data->next = NULL;
    906 		buf->last = data;
    907 
    908 		data->bufp = data->text;
    909 		data->size = 0;
    910 	    }
    911 
    912 	    mem = buf->last->bufp + buf->last->size;
    913 	    size = (buf->last->text + BUFFER_DATA_SIZE) - mem;
    914 
    915 	    /* We need to read at least 1 byte.  We can handle up to
    916                SIZE bytes.  This will only be efficient if the
    917                underlying communication stream does its own buffering,
    918                or is clever about getting more than 1 byte at a time.  */
    919 	    status = (*buf->input) (buf->closure, mem, 1, size, &nbytes);
    920 	    if (status != 0)
    921 		return status;
    922 
    923 	    predicted_len += nbytes;
    924 	    buf->last->size += nbytes;
    925 
    926 	    /* Optimize slightly to avoid an unnecessary call to memchr.  */
    927 	    if (nbytes == 1)
    928 	    {
    929 		if (*mem == '\012')
    930 		    break;
    931 	    }
    932 	    else
    933 	    {
    934 		if (memchr (mem, '\012', nbytes) != NULL)
    935 		    break;
    936 	    }
    937 	    if (xsum (len, predicted_len) >= max) return -2;
    938 	}
    939     }
    940 }
    941 
    942 
    943 
    944 /*
    945  * Extract data from the input buffer BUF.  This will read up to WANT
    946  * bytes from the buffer.  It will set *RETDATA to point at the bytes,
    947  * and set *GOT to the number of bytes to be found there.  Any buffer
    948  * call which uses BUF may change the contents of the buffer at *DATA,
    949  * so the data should be fully processed before any further calls are
    950  * made.  This returns 0 on success, or -1 on end of file, or -2 if
    951  * out of memory, or an error code.
    952  */
    953 int
    954 buf_read_data (struct buffer *buf, size_t want, char **retdata, size_t *got)
    955 {
    956     assert (buf->input != NULL);
    957 
    958     while (buf->data != NULL && buf->data->size == 0)
    959     {
    960 	struct buffer_data *next;
    961 
    962 	next = buf->data->next;
    963 	buf_free_datas (buf->data, buf->data);
    964 	buf->data = next;
    965 	if (next == NULL)
    966 	    buf->last = NULL;
    967     }
    968 
    969     if (buf->data == NULL)
    970     {
    971 	struct buffer_data *data;
    972 	int status;
    973 	size_t get, nbytes;
    974 
    975 	data = get_buffer_data ();
    976 	if (data == NULL)
    977 	{
    978 	    (*buf->memory_error) (buf);
    979 	    return -2;
    980 	}
    981 
    982 	buf->data = data;
    983 	buf->last = data;
    984 	data->next = NULL;
    985 	data->bufp = data->text;
    986 	data->size = 0;
    987 
    988 	if (want < BUFFER_DATA_SIZE)
    989 	    get = want;
    990 	else
    991 	    get = BUFFER_DATA_SIZE;
    992 	status = (*buf->input) (buf->closure, data->bufp, get,
    993 				BUFFER_DATA_SIZE, &nbytes);
    994 	if (status != 0)
    995 	    return status;
    996 
    997 	data->size = nbytes;
    998     }
    999 
   1000     *retdata = buf->data->bufp;
   1001     if (want < buf->data->size)
   1002     {
   1003         *got = want;
   1004 	buf->data->size -= want;
   1005 	buf->data->bufp += want;
   1006     }
   1007     else
   1008     {
   1009         *got = buf->data->size;
   1010 	buf->data->size = 0;
   1011     }
   1012 
   1013     return 0;
   1014 }
   1015 
   1016 
   1017 
   1018 /*
   1019  * Copy lines from an input buffer to an output buffer.
   1020  * This copies all complete lines (characters up to a
   1021  * newline) from INBUF to OUTBUF.  Each line in OUTBUF is preceded by the
   1022  * character COMMAND and a space.
   1023  */
   1024 void
   1025 buf_copy_lines (struct buffer *outbuf, struct buffer *inbuf, int command)
   1026 {
   1027     while (1)
   1028     {
   1029 	struct buffer_data *data;
   1030 	struct buffer_data *nldata;
   1031 	char *nl;
   1032 	int len;
   1033 
   1034 	/* See if there is a newline in INBUF.  */
   1035 	nldata = NULL;
   1036 	nl = NULL;
   1037 	for (data = inbuf->data; data != NULL; data = data->next)
   1038 	{
   1039 	    nl = memchr (data->bufp, '\n', data->size);
   1040 	    if (nl != NULL)
   1041 	    {
   1042 		nldata = data;
   1043 		break;
   1044 	    }
   1045 	}
   1046 
   1047 	if (nldata == NULL)
   1048 	{
   1049 	    /* There are no more lines in INBUF.  */
   1050 	    return;
   1051 	}
   1052 
   1053 	/* Put in the command.  */
   1054 	buf_append_char (outbuf, command);
   1055 	buf_append_char (outbuf, ' ');
   1056 
   1057 	if (inbuf->data != nldata)
   1058 	{
   1059 	    /*
   1060 	     * Simply move over all the buffers up to the one containing
   1061 	     * the newline.
   1062 	     */
   1063 	    for (data = inbuf->data; data->next != nldata; data = data->next);
   1064 	    data->next = NULL;
   1065 	    buf_append_data (outbuf, inbuf->data, data);
   1066 	    inbuf->data = nldata;
   1067 	}
   1068 
   1069 	/*
   1070 	 * If the newline is at the very end of the buffer, just move
   1071 	 * the buffer onto OUTBUF.  Otherwise we must copy the data.
   1072 	 */
   1073 	len = nl + 1 - nldata->bufp;
   1074 	if (len == nldata->size)
   1075 	{
   1076 	    inbuf->data = nldata->next;
   1077 	    if (inbuf->data == NULL)
   1078 		inbuf->last = NULL;
   1079 
   1080 	    nldata->next = NULL;
   1081 	    buf_append_data (outbuf, nldata, nldata);
   1082 	}
   1083 	else
   1084 	{
   1085 	    buf_output (outbuf, nldata->bufp, len);
   1086 	    nldata->bufp += len;
   1087 	    nldata->size -= len;
   1088 	}
   1089     }
   1090 }
   1091 
   1092 
   1093 
   1094 /*
   1095  * Copy counted data from one buffer to another.  The count is an
   1096  * integer, host size, host byte order (it is only used across a
   1097  * pipe).  If there is enough data, it should be moved over.  If there
   1098  * is not enough data, it should remain on the original buffer.  A
   1099  * negative count is a special case.  if one is seen, *SPECIAL is set
   1100  * to the (negative) count value and no additional data is gathered
   1101  * from the buffer; normally *SPECIAL is set to 0.  This function
   1102  * returns the number of bytes it needs to see in order to actually
   1103  * copy something over.
   1104  */
   1105 int
   1106 buf_copy_counted (struct buffer *outbuf, struct buffer *inbuf, int *special)
   1107 {
   1108     *special = 0;
   1109 
   1110     while (1)
   1111     {
   1112 	struct buffer_data *data;
   1113 	int need;
   1114 	union
   1115 	{
   1116 	    char intbuf[sizeof (int)];
   1117 	    int i;
   1118 	} u;
   1119 	char *intp;
   1120 	int count;
   1121 	struct buffer_data *start;
   1122 	int startoff;
   1123 	struct buffer_data *stop;
   1124 	int stopwant;
   1125 
   1126 	/* See if we have enough bytes to figure out the count.  */
   1127 	need = sizeof (int);
   1128 	intp = u.intbuf;
   1129 	for (data = inbuf->data; data != NULL; data = data->next)
   1130 	{
   1131 	    if (data->size >= need)
   1132 	    {
   1133 		memcpy (intp, data->bufp, need);
   1134 		break;
   1135 	    }
   1136 	    memcpy (intp, data->bufp, data->size);
   1137 	    intp += data->size;
   1138 	    need -= data->size;
   1139 	}
   1140 	if (data == NULL)
   1141 	{
   1142 	    /* We don't have enough bytes to form an integer.  */
   1143 	    return need;
   1144 	}
   1145 
   1146 	count = u.i;
   1147 	start = data;
   1148 	startoff = need;
   1149 
   1150 	if (count < 0)
   1151 	{
   1152 	    /* A negative COUNT is a special case meaning that we
   1153                don't need any further information.  */
   1154 	    stop = start;
   1155 	    stopwant = 0;
   1156 	}
   1157 	else
   1158 	{
   1159 	    /*
   1160 	     * We have an integer in COUNT.  We have gotten all the
   1161 	     * data from INBUF in all buffers before START, and we
   1162 	     * have gotten STARTOFF bytes from START.  See if we have
   1163 	     * enough bytes remaining in INBUF.
   1164 	     */
   1165 	    need = count - (start->size - startoff);
   1166 	    if (need <= 0)
   1167 	    {
   1168 		stop = start;
   1169 		stopwant = count;
   1170 	    }
   1171 	    else
   1172 	    {
   1173 		for (data = start->next; data != NULL; data = data->next)
   1174 		{
   1175 		    if (need <= data->size)
   1176 			break;
   1177 		    need -= data->size;
   1178 		}
   1179 		if (data == NULL)
   1180 		{
   1181 		    /* We don't have enough bytes.  */
   1182 		    return need;
   1183 		}
   1184 		stop = data;
   1185 		stopwant = need;
   1186 	    }
   1187 	}
   1188 
   1189 	/*
   1190 	 * We have enough bytes.  Free any buffers in INBUF before
   1191 	 * START, and remove STARTOFF bytes from START, so that we can
   1192 	 * forget about STARTOFF.
   1193 	 */
   1194 	start->bufp += startoff;
   1195 	start->size -= startoff;
   1196 
   1197 	if (start->size == 0)
   1198 	    start = start->next;
   1199 
   1200 	if (stop->size == stopwant)
   1201 	{
   1202 	    stop = stop->next;
   1203 	    stopwant = 0;
   1204 	}
   1205 
   1206 	while (inbuf->data != start)
   1207 	{
   1208 	    data = inbuf->data;
   1209 	    inbuf->data = data->next;
   1210 	    buf_free_datas (data, data);
   1211 	}
   1212 
   1213 	/* If COUNT is negative, set *SPECIAL and get out now.  */
   1214 	if (count < 0)
   1215 	{
   1216 	    *special = count;
   1217 	    return 0;
   1218 	}
   1219 
   1220 	/*
   1221 	 * We want to copy over the bytes from START through STOP.  We
   1222 	 * only want STOPWANT bytes from STOP.
   1223 	 */
   1224 
   1225 	if (start != stop)
   1226 	{
   1227 	    /* Attach the buffers from START through STOP to OUTBUF.  */
   1228 	    for (data = start; data->next != stop; data = data->next);
   1229 	    inbuf->data = stop;
   1230 	    data->next = NULL;
   1231 	    buf_append_data (outbuf, start, data);
   1232 	}
   1233 
   1234 	if (stopwant > 0)
   1235 	{
   1236 	    buf_output (outbuf, stop->bufp, stopwant);
   1237 	    stop->bufp += stopwant;
   1238 	    stop->size -= stopwant;
   1239 	}
   1240     }
   1241 
   1242     /*NOTREACHED*/
   1243 }
   1244 
   1245 
   1246 
   1247 int
   1248 buf_get_fd (struct buffer *buf)
   1249 {
   1250     if (buf->get_fd)
   1251 	return (*buf->get_fd) (buf->closure);
   1252     return -1;
   1253 }
   1254 
   1255 
   1256 
   1257 /* Shut down a buffer.  This returns 0 on success, or an errno code.  */
   1258 int
   1259 buf_shutdown (struct buffer *buf)
   1260 {
   1261     if (buf->shutdown) return (*buf->shutdown) (buf);
   1262     return 0;
   1263 }
   1264 
   1265 
   1266 
   1267 /* Certain types of communication input and output data in packets,
   1268    where each packet is translated in some fashion.  The packetizing
   1269    buffer type supports that, given a buffer which handles lower level
   1270    I/O and a routine to translate the data in a packet.
   1271 
   1272    This code uses two bytes for the size of a packet, so packets are
   1273    restricted to 65536 bytes in total.
   1274 
   1275    The translation functions should just translate; they may not
   1276    significantly increase or decrease the amount of data.  The actual
   1277    size of the initial data is part of the translated data.  The
   1278    output translation routine may add up to PACKET_SLOP additional
   1279    bytes, and the input translation routine should shrink the data
   1280    correspondingly.  */
   1281 
   1282 # define PACKET_SLOP (100)
   1283 
   1284 /* This structure is the closure field of a packetizing buffer.  */
   1285 
   1286 struct packetizing_buffer
   1287 {
   1288     /* The underlying buffer.  */
   1289     struct buffer *buf;
   1290     /* The input translation function.  Exactly one of inpfn and outfn
   1291        will be NULL.  The input translation function should
   1292        untranslate the data in INPUT, storing the result in OUTPUT.
   1293        SIZE is the amount of data in INPUT, and is also the size of
   1294        OUTPUT.  This should return 0 on success, or an errno code.  */
   1295     int (*inpfn) (void *fnclosure, const char *input, char *output,
   1296 			size_t size);
   1297     /* The output translation function.  This should translate the
   1298        data in INPUT, storing the result in OUTPUT.  The first two
   1299        bytes in INPUT will be the size of the data, and so will SIZE.
   1300        This should set *TRANSLATED to the amount of translated data in
   1301        OUTPUT.  OUTPUT is large enough to hold SIZE + PACKET_SLOP
   1302        bytes.  This should return 0 on success, or an errno code.  */
   1303     int (*outfn) (void *fnclosure, const char *input, char *output,
   1304 			size_t size, size_t *translated);
   1305     /* A closure for the translation function.  */
   1306     void *fnclosure;
   1307     /* For an input buffer, we may have to buffer up data here.  */
   1308     /* This is non-zero if the buffered data has been translated.
   1309        Otherwise, the buffered data has not been translated, and starts
   1310        with the two byte packet size.  */
   1311     bool translated;
   1312     /* The amount of buffered data.  */
   1313     size_t holdsize;
   1314     /* The buffer allocated to hold the data.  */
   1315     char *holdbuf;
   1316     /* The size of holdbuf.  */
   1317     size_t holdbufsize;
   1318     /* If translated is set, we need another data pointer to track
   1319        where we are in holdbuf.  If translated is clear, then this
   1320        pointer is not used.  */
   1321     char *holddata;
   1322 };
   1323 
   1324 
   1325 
   1326 static int packetizing_buffer_input (void *, char *, size_t, size_t, size_t *);
   1327 static int packetizing_buffer_output (void *, const char *, size_t, size_t *);
   1328 static int packetizing_buffer_flush (void *);
   1329 static int packetizing_buffer_block (void *, bool);
   1330 static int packetizing_buffer_get_fd (void *);
   1331 static int packetizing_buffer_shutdown (struct buffer *);
   1332 
   1333 
   1334 
   1335 /* Create a packetizing buffer.  */
   1336 struct buffer *
   1337 packetizing_buffer_initialize (struct buffer *buf,
   1338                                int (*inpfn) (void *, const char *, char *,
   1339                                              size_t),
   1340                                int (*outfn) (void *, const char *, char *,
   1341                                              size_t, size_t *),
   1342                                void *fnclosure,
   1343                                void (*memory) (struct buffer *))
   1344 {
   1345     struct packetizing_buffer *pb;
   1346 
   1347     pb = xmalloc (sizeof *pb);
   1348     memset (pb, 0, sizeof *pb);
   1349 
   1350     pb->buf = buf;
   1351     pb->inpfn = inpfn;
   1352     pb->outfn = outfn;
   1353     pb->fnclosure = fnclosure;
   1354 
   1355     if (inpfn != NULL)
   1356     {
   1357 	/* Add PACKET_SLOP to handle larger translated packets, and
   1358            add 2 for the count.  This buffer is increased if
   1359            necessary.  */
   1360 	pb->holdbufsize = BUFFER_DATA_SIZE + PACKET_SLOP + 2;
   1361 	pb->holdbuf = xmalloc (pb->holdbufsize);
   1362     }
   1363 
   1364     return buf_initialize (inpfn != NULL ? packetizing_buffer_input : NULL,
   1365 			   inpfn != NULL ? NULL : packetizing_buffer_output,
   1366 			   inpfn != NULL ? NULL : packetizing_buffer_flush,
   1367 			   packetizing_buffer_block,
   1368 			   packetizing_buffer_get_fd,
   1369 			   packetizing_buffer_shutdown,
   1370 			   memory,
   1371 			   pb);
   1372 }
   1373 
   1374 
   1375 
   1376 /* Input data from a packetizing buffer.  */
   1377 static int
   1378 packetizing_buffer_input (void *closure, char *data, size_t need, size_t size,
   1379                           size_t *got)
   1380 {
   1381     struct packetizing_buffer *pb = closure;
   1382 
   1383     *got = 0;
   1384 
   1385     if (pb->holdsize > 0 && pb->translated)
   1386     {
   1387 	size_t copy;
   1388 
   1389 	copy = pb->holdsize;
   1390 
   1391 	if (copy > size)
   1392 	{
   1393 	    memcpy (data, pb->holddata, size);
   1394 	    pb->holdsize -= size;
   1395 	    pb->holddata += size;
   1396 	    *got = size;
   1397 	    return 0;
   1398 	}
   1399 
   1400 	memcpy (data, pb->holddata, copy);
   1401 	pb->holdsize = 0;
   1402 	pb->translated = false;
   1403 
   1404 	data += copy;
   1405 	need -= copy;
   1406 	size -= copy;
   1407 	*got = copy;
   1408     }
   1409 
   1410     while (need > 0 || *got == 0)
   1411     {
   1412 	int status;
   1413 	size_t get, nread, count, tcount;
   1414 	char *bytes;
   1415 	static char *stackoutbuf = NULL;
   1416 	char *inbuf, *outbuf;
   1417 
   1418 	if (!stackoutbuf)
   1419 	    stackoutbuf = xmalloc (BUFFER_DATA_SIZE + PACKET_SLOP);
   1420 
   1421 	/* If we don't already have the two byte count, get it.  */
   1422 	if (pb->holdsize < 2)
   1423 	{
   1424 	    get = 2 - pb->holdsize;
   1425 	    status = buf_read_data (pb->buf, get, &bytes, &nread);
   1426 	    if (status != 0)
   1427 	    {
   1428 		/* buf_read_data can return -2, but a buffer input
   1429                    function is only supposed to return -1, 0, or an
   1430                    error code.  */
   1431 		if (status == -2)
   1432 		    status = ENOMEM;
   1433 		return status;
   1434 	    }
   1435 
   1436 	    if (nread == 0)
   1437 	    {
   1438 		/* The buffer is in nonblocking mode, and we didn't
   1439                    manage to read anything.  */
   1440 		return 0;
   1441 	    }
   1442 
   1443 	    if (get == 1)
   1444 		pb->holdbuf[1] = bytes[0];
   1445 	    else
   1446 	    {
   1447 		pb->holdbuf[0] = bytes[0];
   1448 		if (nread < 2)
   1449 		{
   1450 		    /* We only got one byte, but we needed two.  Stash
   1451                        the byte we got, and try again.  */
   1452 		    pb->holdsize = 1;
   1453 		    continue;
   1454 		}
   1455 		pb->holdbuf[1] = bytes[1];
   1456 	    }
   1457 	    pb->holdsize = 2;
   1458 	}
   1459 
   1460 	/* Read the packet.  */
   1461 
   1462 	count = (((pb->holdbuf[0] & 0xff) << 8)
   1463 		 + (pb->holdbuf[1] & 0xff));
   1464 
   1465 	if (count + 2 > pb->holdbufsize)
   1466 	{
   1467 	    char *n;
   1468 
   1469 	    /* We didn't allocate enough space in the initialize
   1470                function.  */
   1471 
   1472 	    n = xrealloc (pb->holdbuf, count + 2);
   1473 	    if (n == NULL)
   1474 	    {
   1475 		(*pb->buf->memory_error) (pb->buf);
   1476 		return ENOMEM;
   1477 	    }
   1478 	    pb->holdbuf = n;
   1479 	    pb->holdbufsize = count + 2;
   1480 	}
   1481 
   1482 	get = count - (pb->holdsize - 2);
   1483 
   1484 	status = buf_read_data (pb->buf, get, &bytes, &nread);
   1485 	if (status != 0)
   1486 	{
   1487 	    /* buf_read_data can return -2, but a buffer input
   1488                function is only supposed to return -1, 0, or an error
   1489                code.  */
   1490 	    if (status == -2)
   1491 		status = ENOMEM;
   1492 	    return status;
   1493 	}
   1494 
   1495 	if (nread == 0)
   1496 	{
   1497 	    /* We did not get any data.  Presumably the buffer is in
   1498                nonblocking mode.  */
   1499 	    return 0;
   1500 	}
   1501 
   1502 	if (nread < get)
   1503 	{
   1504 	    /* We did not get all the data we need to fill the packet.
   1505                buf_read_data does not promise to return all the bytes
   1506                requested, so we must try again.  */
   1507 	    memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
   1508 	    pb->holdsize += nread;
   1509 	    continue;
   1510 	}
   1511 
   1512 	/* We have a complete untranslated packet of COUNT bytes.  */
   1513 
   1514 	if (pb->holdsize == 2)
   1515 	{
   1516 	    /* We just read the entire packet (the 2 bytes in
   1517                PB->HOLDBUF are the size).  Save a memcpy by
   1518                translating directly from BYTES.  */
   1519 	    inbuf = bytes;
   1520 	}
   1521 	else
   1522 	{
   1523 	    /* We already had a partial packet in PB->HOLDBUF.  We
   1524                need to copy the new data over to make the input
   1525                contiguous.  */
   1526 	    memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
   1527 	    inbuf = pb->holdbuf + 2;
   1528 	}
   1529 
   1530 	if (count <= BUFFER_DATA_SIZE + PACKET_SLOP)
   1531 	    outbuf = stackoutbuf;
   1532 	else
   1533 	{
   1534 	    outbuf = xmalloc (count);
   1535 	    if (outbuf == NULL)
   1536 	    {
   1537 		(*pb->buf->memory_error) (pb->buf);
   1538 		return ENOMEM;
   1539 	    }
   1540 	}
   1541 
   1542 	status = (*pb->inpfn) (pb->fnclosure, inbuf, outbuf, count);
   1543 	if (status != 0)
   1544 	    return status;
   1545 
   1546 	/* The first two bytes in the translated buffer are the real
   1547            length of the translated data.  */
   1548 	tcount = ((outbuf[0] & 0xff) << 8) + (outbuf[1] & 0xff);
   1549 
   1550 	if (tcount > count)
   1551 	    error (1, 0, "Input translation failure");
   1552 
   1553 	if (tcount > size)
   1554 	{
   1555 	    /* We have more data than the caller has provided space
   1556                for.  We need to save some of it for the next call.  */
   1557 
   1558 	    memcpy (data, outbuf + 2, size);
   1559 	    *got += size;
   1560 
   1561 	    pb->holdsize = tcount - size;
   1562 	    memcpy (pb->holdbuf, outbuf + 2 + size, tcount - size);
   1563 	    pb->holddata = pb->holdbuf;
   1564 	    pb->translated = true;
   1565 
   1566 	    if (outbuf != stackoutbuf)
   1567 		free (outbuf);
   1568 
   1569 	    return 0;
   1570 	}
   1571 
   1572 	memcpy (data, outbuf + 2, tcount);
   1573 
   1574 	if (outbuf != stackoutbuf)
   1575 	    free (outbuf);
   1576 
   1577 	pb->holdsize = 0;
   1578 
   1579 	data += tcount;
   1580 	need -= tcount;
   1581 	size -= tcount;
   1582 	*got += tcount;
   1583     }
   1584 
   1585     return 0;
   1586 }
   1587 
   1588 
   1589 
   1590 /* Output data to a packetizing buffer.  */
   1591 static int
   1592 packetizing_buffer_output (void *closure, const char *data, size_t have,
   1593                            size_t *wrote)
   1594 {
   1595     struct packetizing_buffer *pb = closure;
   1596     static char *inbuf = NULL;  /* These two buffers are static so that they
   1597 				 * depend on the size of BUFFER_DATA_SIZE yet
   1598 				 * still only be allocated once per run.
   1599 				 */
   1600     static char *stack_outbuf = NULL;
   1601     struct buffer_data *outdata = NULL; /* Initialize to silence -Wall.  Dumb.
   1602 					 */
   1603     char *outbuf;
   1604     size_t size, translated;
   1605     int status;
   1606 
   1607     /* It would be easy to xmalloc a buffer, but I don't think this
   1608        case can ever arise.  */
   1609     assert (have <= BUFFER_DATA_SIZE);
   1610 
   1611     if (!inbuf)
   1612     {
   1613 	inbuf = xmalloc (BUFFER_DATA_SIZE + 2);
   1614         stack_outbuf = xmalloc (BUFFER_DATA_SIZE + PACKET_SLOP + 4);
   1615     }
   1616 
   1617     inbuf[0] = (have >> 8) & 0xff;
   1618     inbuf[1] = have & 0xff;
   1619     memcpy (inbuf + 2, data, have);
   1620 
   1621     size = have + 2;
   1622 
   1623     /* The output function is permitted to add up to PACKET_SLOP
   1624        bytes, and we need 2 bytes for the size of the translated data.
   1625        If we can guarantee that the result will fit in a buffer_data,
   1626        we translate directly into one to avoid a memcpy in buf_output.  */
   1627     if (size + PACKET_SLOP + 2 > BUFFER_DATA_SIZE)
   1628 	outbuf = stack_outbuf;
   1629     else
   1630     {
   1631 	outdata = get_buffer_data ();
   1632 	if (outdata == NULL)
   1633 	{
   1634 	    (*pb->buf->memory_error) (pb->buf);
   1635 	    return ENOMEM;
   1636 	}
   1637 
   1638 	outdata->next = NULL;
   1639 	outdata->bufp = outdata->text;
   1640 
   1641 	outbuf = outdata->text;
   1642     }
   1643 
   1644     status = (*pb->outfn) (pb->fnclosure, inbuf, outbuf + 2, size,
   1645 			   &translated);
   1646     if (status != 0)
   1647 	return status;
   1648 
   1649     /* The output function is permitted to add up to PACKET_SLOP
   1650        bytes.  */
   1651     assert (translated <= size + PACKET_SLOP);
   1652 
   1653     outbuf[0] = (translated >> 8) & 0xff;
   1654     outbuf[1] = translated & 0xff;
   1655 
   1656     if (outbuf == stack_outbuf)
   1657 	buf_output (pb->buf, outbuf, translated + 2);
   1658     else
   1659     {
   1660 	outdata->size = translated + 2;
   1661 	buf_append_data (pb->buf, outdata, outdata);
   1662     }
   1663 
   1664     *wrote = have;
   1665 
   1666     /* We will only be here because buf_send_output was called on the
   1667        packetizing buffer.  That means that we should now call
   1668        buf_send_output on the underlying buffer.  */
   1669     return buf_send_output (pb->buf);
   1670 }
   1671 
   1672 
   1673 
   1674 /* Flush data to a packetizing buffer.  */
   1675 static int
   1676 packetizing_buffer_flush (void *closure)
   1677 {
   1678     struct packetizing_buffer *pb = closure;
   1679 
   1680     /* Flush the underlying buffer.  Note that if the original call to
   1681        buf_flush passed 1 for the BLOCK argument, then the buffer will
   1682        already have been set into blocking mode, so we should always
   1683        pass 0 here.  */
   1684     return buf_flush (pb->buf, 0);
   1685 }
   1686 
   1687 
   1688 
   1689 /* The block routine for a packetizing buffer.  */
   1690 static int
   1691 packetizing_buffer_block (void *closure, bool block)
   1692 {
   1693     struct packetizing_buffer *pb = closure;
   1694 
   1695     if (block)
   1696 	return set_block (pb->buf);
   1697     else
   1698 	return set_nonblock (pb->buf);
   1699 }
   1700 
   1701 
   1702 
   1703 /* Return the file descriptor underlying any child buffers.  */
   1704 static int
   1705 packetizing_buffer_get_fd (void *closure)
   1706 {
   1707     struct packetizing_buffer *cb = closure;
   1708     return buf_get_fd (cb->buf);
   1709 }
   1710 
   1711 
   1712 
   1713 /* Shut down a packetizing buffer.  */
   1714 static int
   1715 packetizing_buffer_shutdown (struct buffer *buf)
   1716 {
   1717     struct packetizing_buffer *pb = buf->closure;
   1718 
   1719     return buf_shutdown (pb->buf);
   1720 }
   1721 
   1722 
   1723 
   1724 /* All server communication goes through buffer structures.  Most of
   1725    the buffers are built on top of a file descriptor.  This structure
   1726    is used as the closure field in a buffer.  */
   1727 
   1728 struct fd_buffer
   1729 {
   1730     /* The file descriptor.  */
   1731     int fd;
   1732     /* Nonzero if the file descriptor is in blocking mode.  */
   1733     int blocking;
   1734     /* The child process id when fd is a pipe.  */
   1735     pid_t child_pid;
   1736     /* The connection info, when fd is a pipe to a server.  */
   1737     cvsroot_t *root;
   1738 };
   1739 
   1740 static int fd_buffer_input (void *, char *, size_t, size_t, size_t *);
   1741 static int fd_buffer_output (void *, const char *, size_t, size_t *);
   1742 static int fd_buffer_flush (void *);
   1743 static int fd_buffer_block (void *, bool);
   1744 static int fd_buffer_get_fd (void *);
   1745 static int fd_buffer_shutdown (struct buffer *);
   1746 
   1747 /* Initialize a buffer built on a file descriptor.  FD is the file
   1748    descriptor.  INPUT is nonzero if this is for input, zero if this is
   1749    for output.  MEMORY is the function to call when a memory error
   1750    occurs.  */
   1751 
   1752 struct buffer *
   1753 fd_buffer_initialize (int fd, pid_t child_pid, cvsroot_t *root, bool input,
   1754                       void (*memory) (struct buffer *))
   1755 {
   1756     struct fd_buffer *n;
   1757 
   1758     n = xmalloc (sizeof *n);
   1759     n->fd = fd;
   1760     n->child_pid = child_pid;
   1761     n->root = root;
   1762     fd_buffer_block (n, true);
   1763     return buf_initialize (input ? fd_buffer_input : NULL,
   1764 			   input ? NULL : fd_buffer_output,
   1765 			   input ? NULL : fd_buffer_flush,
   1766 			   fd_buffer_block, fd_buffer_get_fd,
   1767 			   fd_buffer_shutdown,
   1768 			   memory,
   1769 			   n);
   1770 }
   1771 
   1772 
   1773 
   1774 /* The buffer input function for a buffer built on a file descriptor.
   1775  *
   1776  * In non-blocking mode, this function will read as many bytes as it can in a
   1777  * single try, up to SIZE bytes, and return.
   1778  *
   1779  * In blocking mode with NEED > 0, this function will read as many bytes as it
   1780  * can but will not return until it has read at least NEED bytes.
   1781  *
   1782  * In blocking mode with NEED == 0, this function will block until it can read
   1783  * either at least one byte or EOF, then read as many bytes as are available
   1784  * and return.  At the very least, compress_buffer_shutdown depends on this
   1785  * behavior to read EOF and can loop indefinitely without it.
   1786  *
   1787  * ASSUMPTIONS
   1788  *   NEED <= SIZE.
   1789  *
   1790  * INPUTS
   1791  *   closure	Our FD_BUFFER struct.
   1792  *   data	The start of our input buffer.
   1793  *   need	How many bytes our caller needs.
   1794  *   size	How many bytes are available in DATA.
   1795  *   got	Where to store the number of bytes read.
   1796  *
   1797  * OUTPUTS
   1798  *   data	Filled with bytes read.
   1799  *   *got	Number of bytes actually read into DATA.
   1800  *
   1801  * RETURNS
   1802  *   errno	On error.
   1803  *   -1		On EOF.
   1804  *   0		Otherwise.
   1805  *
   1806  * ERRORS
   1807  *   This function can return an error if fd_buffer_block(), or the system
   1808  *   read() or select() calls do.
   1809  */
   1810 static int
   1811 fd_buffer_input (void *closure, char *data, size_t need, size_t size,
   1812 		 size_t *got)
   1813 {
   1814     struct fd_buffer *fb = closure;
   1815     int nbytes;
   1816 
   1817     assert (need <= size);
   1818 
   1819     *got = 0;
   1820 
   1821     if (fb->blocking)
   1822     {
   1823 	int status;
   1824 	fd_set readfds;
   1825 
   1826 	/* Set non-block.  */
   1827         status = fd_buffer_block (fb, false);
   1828 	if (status != 0) return status;
   1829 
   1830 	FD_ZERO (&readfds);
   1831 	FD_SET (fb->fd, &readfds);
   1832 	do
   1833 	{
   1834 	    int numfds;
   1835 
   1836 	    do {
   1837 		/* This used to select on exceptions too, but as far
   1838 		   as I know there was never any reason to do that and
   1839 		   SCO doesn't let you select on exceptions on pipes.  */
   1840 		numfds = fd_select (fb->fd + 1, &readfds, NULL, NULL, NULL);
   1841 		if (numfds < 0 && errno != EINTR)
   1842 		{
   1843 		    status = errno;
   1844 		    goto block_done;
   1845 		}
   1846 	    } while (numfds < 0);
   1847 
   1848 	    nbytes = read (fb->fd, data + *got, size - *got);
   1849 
   1850 	    if (nbytes == 0)
   1851 	    {
   1852 		/* End of file.  This assumes that we are using POSIX or BSD
   1853 		   style nonblocking I/O.  On System V we will get a zero
   1854 		   return if there is no data, even when not at EOF.  */
   1855 		if (*got)
   1856 		{
   1857 		    /* We already read some data, so return no error, counting
   1858 		     * on the fact that we will read EOF again next time.
   1859 		     */
   1860 		    status = 0;
   1861 		    break;
   1862 		}
   1863 		else
   1864 		{
   1865 		    /* Return EOF.  */
   1866 		    status = -1;
   1867 		    break;
   1868 		}
   1869 	    }
   1870 
   1871 	    if (nbytes < 0)
   1872 	    {
   1873 		/* Some error occurred.  */
   1874 		if (!blocking_error (errno))
   1875 		{
   1876 		    status = errno;
   1877 		    break;
   1878 		}
   1879 		/* else Everything's fine, we just didn't get any data.  */
   1880 	    }
   1881 
   1882 	    *got += nbytes;
   1883 	} while (*got < need);
   1884 
   1885 block_done:
   1886 	if (status == 0 || status == -1)
   1887 	{
   1888 	    int newstatus;
   1889 
   1890 	    /* OK or EOF - Reset block.  */
   1891 	    newstatus = fd_buffer_block (fb, true);
   1892 	    if (newstatus) status = newstatus;
   1893 	}
   1894 	return status;
   1895     }
   1896 
   1897     /* The above will always return.  Handle non-blocking read.  */
   1898     nbytes = read (fb->fd, data, size);
   1899 
   1900     if (nbytes > 0)
   1901     {
   1902 	*got = nbytes;
   1903 	return 0;
   1904     }
   1905 
   1906     if (nbytes == 0)
   1907 	/* End of file.  This assumes that we are using POSIX or BSD
   1908 	   style nonblocking I/O.  On System V we will get a zero
   1909 	   return if there is no data, even when not at EOF.  */
   1910 	return -1;
   1911 
   1912     /* Some error occurred.  */
   1913     if (blocking_error (errno))
   1914 	/* Everything's fine, we just didn't get any data.  */
   1915 	return 0;
   1916 
   1917     return errno;
   1918 }
   1919 
   1920 
   1921 
   1922 /* The buffer output function for a buffer built on a file descriptor.  */
   1923 
   1924 static int
   1925 fd_buffer_output (void *closure, const char *data, size_t have, size_t *wrote)
   1926 {
   1927     struct fd_buffer *fd = closure;
   1928 
   1929     *wrote = 0;
   1930 
   1931     while (have > 0)
   1932     {
   1933 	int nbytes;
   1934 
   1935 	nbytes = write (fd->fd, data, have);
   1936 
   1937 	if (nbytes <= 0)
   1938 	{
   1939 	    if (! fd->blocking
   1940 		&& (nbytes == 0 || blocking_error (errno)))
   1941 	    {
   1942 		/* A nonblocking write failed to write any data.  Just
   1943 		   return.  */
   1944 		return 0;
   1945 	    }
   1946 
   1947 	    /* Some sort of error occurred.  */
   1948 
   1949 	    if (nbytes == 0)
   1950 		return EIO;
   1951 
   1952 	    return errno;
   1953 	}
   1954 
   1955 	*wrote += nbytes;
   1956 	data += nbytes;
   1957 	have -= nbytes;
   1958     }
   1959 
   1960     return 0;
   1961 }
   1962 
   1963 
   1964 
   1965 /* The buffer flush function for a buffer built on a file descriptor.  */
   1966 static int
   1967 fd_buffer_flush (void *closure)
   1968 {
   1969     /* We don't need to do anything here.  Our fd doesn't have its own buffer
   1970      * and syncing won't do anything but slow us down.
   1971      *
   1972      * struct fd_buffer *fb = closure;
   1973      *
   1974      * if (fsync (fb->fd) < 0 && errno != EROFS && errno != EINVAL)
   1975      *     return errno;
   1976      */
   1977     return 0;
   1978 }
   1979 
   1980 
   1981 
   1982 static struct stat devnull;
   1983 static int devnull_set = -1;
   1984 
   1985 /* The buffer block function for a buffer built on a file descriptor.  */
   1986 static int
   1987 fd_buffer_block (void *closure, bool block)
   1988 {
   1989     struct fd_buffer *fb = closure;
   1990 # if defined (F_GETFL) && defined (O_NONBLOCK) && defined (F_SETFL)
   1991     int flags;
   1992 
   1993     flags = fcntl (fb->fd, F_GETFL, 0);
   1994     if (flags < 0)
   1995 	return errno;
   1996 
   1997     if (block)
   1998 	flags &= ~O_NONBLOCK;
   1999     else
   2000 	flags |= O_NONBLOCK;
   2001 
   2002     if (fcntl (fb->fd, F_SETFL, flags) < 0)
   2003     {
   2004 	/*
   2005 	 * BSD returns ENODEV when we try to set block/nonblock on /dev/null.
   2006 	 * BSDI returns ENOTTY when we try to set block/nonblock on /dev/null.
   2007 	 */
   2008 	struct stat sb;
   2009 	int save_errno = errno;
   2010 	bool isdevnull = false;
   2011 
   2012 	if (devnull_set == -1)
   2013 	    devnull_set = stat ("/dev/null", &devnull);
   2014 
   2015 	if (devnull_set >= 0)
   2016 	    /* Equivalent to /dev/null ? */
   2017 	    isdevnull = (fstat (fb->fd, &sb) >= 0
   2018 			 && sb.st_dev == devnull.st_dev
   2019 			 && sb.st_ino == devnull.st_ino
   2020 			 && sb.st_mode == devnull.st_mode
   2021 			 && sb.st_uid == devnull.st_uid
   2022 			 && sb.st_gid == devnull.st_gid
   2023 			 && sb.st_size == devnull.st_size
   2024 			 && sb.st_blocks == devnull.st_blocks
   2025 			 && sb.st_blksize == devnull.st_blksize);
   2026 	if (isdevnull)
   2027 	    errno = 0;
   2028 	else
   2029 	{
   2030 	    errno = save_errno;
   2031 	    return errno;
   2032 	}
   2033     }
   2034 # endif /* F_GETFL && O_NONBLOCK && F_SETFL */
   2035 
   2036     fb->blocking = block;
   2037 
   2038     return 0;
   2039 }
   2040 
   2041 
   2042 
   2043 static int
   2044 fd_buffer_get_fd (void *closure)
   2045 {
   2046     struct fd_buffer *fb = closure;
   2047     return fb->fd;
   2048 }
   2049 
   2050 
   2051 
   2052 /* The buffer shutdown function for a buffer built on a file descriptor.
   2053  *
   2054  * This function disposes of memory allocated for this buffer.
   2055  */
   2056 static int
   2057 fd_buffer_shutdown (struct buffer *buf)
   2058 {
   2059     struct fd_buffer *fb = buf->closure;
   2060     struct stat s;
   2061     bool closefd, statted;
   2062 
   2063     /* Must be an open pipe, socket, or file.  What could go wrong? */
   2064     if (fstat (fb->fd, &s) == -1) statted = false;
   2065     else statted = true;
   2066     /* Don't bother to try closing the FD if we couldn't stat it.  This
   2067      * probably won't work.
   2068      *
   2069      * (buf_shutdown() on some of the server/child communication pipes is
   2070      * getting EBADF on both the fstat and the close.  I'm not sure why -
   2071      * perhaps they were alredy closed somehow?
   2072      */
   2073     closefd = statted;
   2074 
   2075     /* Flush the buffer if possible.  */
   2076     if (buf->flush)
   2077     {
   2078 	buf_flush (buf, 1);
   2079 	buf->flush = NULL;
   2080     }
   2081 
   2082     if (buf->input)
   2083     {
   2084 	/* There used to be a check here for unread data in the buffer of
   2085 	 * the pipe, but it was deemed unnecessary and possibly dangerous.  In
   2086 	 * some sense it could be second-guessing the caller who requested it
   2087 	 * closed, as well.
   2088 	 */
   2089 
   2090 /* FIXME:
   2091  *
   2092  * This mess of #ifdefs is hard to read.  There must be some relation between
   2093  * the macros being checked which at least deserves comments - if
   2094  * SHUTDOWN_SERVER, NO_SOCKET_TO_FD, & START_RSH_WITH_POPEN_RW were completely
   2095  * independant, then the next few lines could easily refuse to compile.
   2096  *
   2097  * The note below about START_RSH_WITH_POPEN_RW never being set when
   2098  * SHUTDOWN_SERVER is defined means that this code would always break on
   2099  * systems with SHUTDOWN_SERVER defined and thus the comment must now be
   2100  * incorrect or the code was broken since the comment was written.
   2101  */
   2102 # ifdef SHUTDOWN_SERVER
   2103 	if (fb->root && fb->root->method != server_method)
   2104 # endif
   2105 # ifndef NO_SOCKET_TO_FD
   2106 	{
   2107 	    /* shutdown() sockets */
   2108 	    if (statted && S_ISSOCK (s.st_mode))
   2109 		shutdown (fb->fd, 0);
   2110 	}
   2111 # endif /* NO_SOCKET_TO_FD */
   2112 # ifdef START_RSH_WITH_POPEN_RW
   2113 /* Can't be set with SHUTDOWN_SERVER defined */
   2114 	/* FIXME: This is now certainly broken since pclose is defined by ANSI
   2115 	 * C to accept a FILE * argument.  The switch will need to happen at a
   2116 	 * higher abstraction level to switch between initializing stdio & fd
   2117 	 * buffers on systems that need this (or maybe an fd buffer that keeps
   2118 	 * track of the FILE * could be used - I think flushing the stream
   2119 	 * before beginning exclusive access via the FD is OK.
   2120 	 */
   2121 	else if (fb->root && pclose (fb->fd) == EOF)
   2122 	{
   2123 	    error (1, errno, "closing connection to %s",
   2124 		   fb->root->hostname);
   2125 	    closefd = false;
   2126 	}
   2127 # endif /* START_RSH_WITH_POPEN_RW */
   2128 
   2129 	buf->input = NULL;
   2130     }
   2131     else if (buf->output)
   2132     {
   2133 # ifdef SHUTDOWN_SERVER
   2134 	/* FIXME:  Should have a SHUTDOWN_SERVER_INPUT &
   2135 	 * SHUTDOWN_SERVER_OUTPUT
   2136 	 */
   2137 	if (fb->root && fb->root->method == server_method)
   2138 	    SHUTDOWN_SERVER (fb->fd);
   2139 	else
   2140 # endif
   2141 # ifndef NO_SOCKET_TO_FD
   2142 	/* shutdown() sockets */
   2143 	if (statted && S_ISSOCK (s.st_mode))
   2144 	    shutdown (fb->fd, 1);
   2145 # else
   2146 	{
   2147 	/* I'm not sure I like this empty block, but the alternative
   2148 	 * is another nested NO_SOCKET_TO_FD switch as above.
   2149 	 */
   2150 	}
   2151 # endif /* NO_SOCKET_TO_FD */
   2152 
   2153 	buf->output = NULL;
   2154     }
   2155 
   2156     if (statted && closefd && close (fb->fd) == -1)
   2157     {
   2158 	if (server_active)
   2159 	{
   2160             /* Syslog this? */
   2161 	}
   2162 # ifdef CLIENT_SUPPORT
   2163 	else if (fb->root)
   2164             error (1, errno, "closing down connection to %s",
   2165                    fb->root->hostname);
   2166 	    /* EXITS */
   2167 # endif /* CLIENT_SUPPORT */
   2168 
   2169 	error (0, errno, "closing down buffer");
   2170     }
   2171 
   2172     /* If we were talking to a process, make sure it exited */
   2173     if (fb->child_pid)
   2174     {
   2175 	int w;
   2176 
   2177 	do
   2178 	    w = waitpid (fb->child_pid, NULL, 0);
   2179 	while (w == -1 && errno == EINTR);
   2180 	if (w == -1)
   2181 	    error (1, errno, "waiting for process %d", fb->child_pid);
   2182     }
   2183 
   2184     free (buf->closure);
   2185     buf->closure = NULL;
   2186 
   2187     return 0;
   2188 }
   2189 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */
   2190