buffer.c revision 102840
1/* Code for the buffer data structure. */ 2 3#include <assert.h> 4#include "cvs.h" 5#include "buffer.h" 6 7#if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) 8 9#ifdef HAVE_WINSOCK_H 10# include <winsock.h> 11#else 12# include <sys/socket.h> 13#endif 14 15/* OS/2 doesn't have EIO. FIXME: this whole notion of turning 16 a different error into EIO strikes me as pretty dubious. */ 17#if !defined (EIO) 18#define EIO EBADPOS 19#endif 20 21/* Linked list of available buffer_data structures. */ 22static struct buffer_data *free_buffer_data; 23 24/* Local functions. */ 25static void buf_default_memory_error PROTO ((struct buffer *)); 26static void allocate_buffer_datas PROTO((void)); 27static struct buffer_data *get_buffer_data PROTO((void)); 28 29/* Initialize a buffer structure. */ 30 31struct buffer * 32buf_initialize (input, output, flush, block, shutdown, memory, closure) 33 int (*input) PROTO((void *, char *, int, int, int *)); 34 int (*output) PROTO((void *, const char *, int, int *)); 35 int (*flush) PROTO((void *)); 36 int (*block) PROTO((void *, int)); 37 int (*shutdown) PROTO((struct buffer *)); 38 void (*memory) PROTO((struct buffer *)); 39 void *closure; 40{ 41 struct buffer *buf; 42 43 buf = (struct buffer *) xmalloc (sizeof (struct buffer)); 44 buf->data = NULL; 45 buf->last = NULL; 46 buf->nonblocking = 0; 47 buf->input = input; 48 buf->output = output; 49 buf->flush = flush; 50 buf->block = block; 51 buf->shutdown = shutdown; 52 buf->memory_error = memory ? memory : buf_default_memory_error; 53 buf->closure = closure; 54 return buf; 55} 56 57/* Free a buffer structure. */ 58 59void 60buf_free (buf) 61 struct buffer *buf; 62{ 63 if (buf->data != NULL) 64 { 65 buf->last->next = free_buffer_data; 66 free_buffer_data = buf->data; 67 } 68 free (buf); 69} 70 71/* Initialize a buffer structure which is not to be used for I/O. */ 72 73struct buffer * 74buf_nonio_initialize (memory) 75 void (*memory) PROTO((struct buffer *)); 76{ 77 return (buf_initialize 78 ((int (*) PROTO((void *, char *, int, int, int *))) NULL, 79 (int (*) PROTO((void *, const char *, int, int *))) NULL, 80 (int (*) PROTO((void *))) NULL, 81 (int (*) PROTO((void *, int))) NULL, 82 (int (*) PROTO((struct buffer *))) NULL, 83 memory, 84 (void *) NULL)); 85} 86 87/* Default memory error handler. */ 88 89static void 90buf_default_memory_error (buf) 91 struct buffer *buf; 92{ 93 error (1, 0, "out of memory"); 94} 95 96/* Allocate more buffer_data structures. */ 97 98static void 99allocate_buffer_datas () 100{ 101 struct buffer_data *alc; 102 char *space; 103 int i; 104 105 /* Allocate buffer_data structures in blocks of 16. */ 106#define ALLOC_COUNT (16) 107 108 alc = ((struct buffer_data *) 109 malloc (ALLOC_COUNT * sizeof (struct buffer_data))); 110 space = (char *) valloc (ALLOC_COUNT * BUFFER_DATA_SIZE); 111 if (alc == NULL || space == NULL) 112 return; 113 for (i = 0; i < ALLOC_COUNT; i++, alc++, space += BUFFER_DATA_SIZE) 114 { 115 alc->next = free_buffer_data; 116 free_buffer_data = alc; 117 alc->text = space; 118 } 119} 120 121/* Get a new buffer_data structure. */ 122 123static struct buffer_data * 124get_buffer_data () 125{ 126 struct buffer_data *ret; 127 128 if (free_buffer_data == NULL) 129 { 130 allocate_buffer_datas (); 131 if (free_buffer_data == NULL) 132 return NULL; 133 } 134 135 ret = free_buffer_data; 136 free_buffer_data = ret->next; 137 return ret; 138} 139 140/* See whether a buffer is empty. */ 141 142int 143buf_empty_p (buf) 144 struct buffer *buf; 145{ 146 struct buffer_data *data; 147 148 for (data = buf->data; data != NULL; data = data->next) 149 if (data->size > 0) 150 return 0; 151 return 1; 152} 153 154#ifdef SERVER_FLOWCONTROL 155/* 156 * Count how much data is stored in the buffer.. 157 * Note that each buffer is a malloc'ed chunk BUFFER_DATA_SIZE. 158 */ 159 160int 161buf_count_mem (buf) 162 struct buffer *buf; 163{ 164 struct buffer_data *data; 165 int mem = 0; 166 167 for (data = buf->data; data != NULL; data = data->next) 168 mem += BUFFER_DATA_SIZE; 169 170 return mem; 171} 172#endif /* SERVER_FLOWCONTROL */ 173 174/* Add data DATA of length LEN to BUF. */ 175 176void 177buf_output (buf, data, len) 178 struct buffer *buf; 179 const char *data; 180 int len; 181{ 182 if (buf->data != NULL 183 && (((buf->last->text + BUFFER_DATA_SIZE) 184 - (buf->last->bufp + buf->last->size)) 185 >= len)) 186 { 187 memcpy (buf->last->bufp + buf->last->size, data, len); 188 buf->last->size += len; 189 return; 190 } 191 192 while (1) 193 { 194 struct buffer_data *newdata; 195 196 newdata = get_buffer_data (); 197 if (newdata == NULL) 198 { 199 (*buf->memory_error) (buf); 200 return; 201 } 202 203 if (buf->data == NULL) 204 buf->data = newdata; 205 else 206 buf->last->next = newdata; 207 newdata->next = NULL; 208 buf->last = newdata; 209 210 newdata->bufp = newdata->text; 211 212 if (len <= BUFFER_DATA_SIZE) 213 { 214 newdata->size = len; 215 memcpy (newdata->text, data, len); 216 return; 217 } 218 219 newdata->size = BUFFER_DATA_SIZE; 220 memcpy (newdata->text, data, BUFFER_DATA_SIZE); 221 222 data += BUFFER_DATA_SIZE; 223 len -= BUFFER_DATA_SIZE; 224 } 225 226 /*NOTREACHED*/ 227} 228 229/* Add a '\0' terminated string to BUF. */ 230 231void 232buf_output0 (buf, string) 233 struct buffer *buf; 234 const char *string; 235{ 236 buf_output (buf, string, strlen (string)); 237} 238 239/* Add a single character to BUF. */ 240 241void 242buf_append_char (buf, ch) 243 struct buffer *buf; 244 int ch; 245{ 246 if (buf->data != NULL 247 && (buf->last->text + BUFFER_DATA_SIZE 248 != buf->last->bufp + buf->last->size)) 249 { 250 *(buf->last->bufp + buf->last->size) = ch; 251 ++buf->last->size; 252 } 253 else 254 { 255 char b; 256 257 b = ch; 258 buf_output (buf, &b, 1); 259 } 260} 261 262/* 263 * Send all the output we've been saving up. Returns 0 for success or 264 * errno code. If the buffer has been set to be nonblocking, this 265 * will just write until the write would block. 266 */ 267 268int 269buf_send_output (buf) 270 struct buffer *buf; 271{ 272 if (buf->output == NULL) 273 abort (); 274 275 while (buf->data != NULL) 276 { 277 struct buffer_data *data; 278 279 data = buf->data; 280 281 if (data->size > 0) 282 { 283 int status, nbytes; 284 285 status = (*buf->output) (buf->closure, data->bufp, data->size, 286 &nbytes); 287 if (status != 0) 288 { 289 /* Some sort of error. Discard the data, and return. */ 290 291 buf->last->next = free_buffer_data; 292 free_buffer_data = buf->data; 293 buf->data = NULL; 294 buf->last = NULL; 295 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 data->next = free_buffer_data; 316 free_buffer_data = data; 317 } 318 319 buf->last = NULL; 320 321 return 0; 322} 323 324/* 325 * Flush any data queued up in the buffer. If BLOCK is nonzero, then 326 * if the buffer is in nonblocking mode, put it into blocking mode for 327 * the duration of the flush. This returns 0 on success, or an error 328 * code. 329 */ 330 331int 332buf_flush (buf, block) 333 struct buffer *buf; 334 int block; 335{ 336 int nonblocking; 337 int status; 338 339 if (buf->flush == NULL) 340 abort (); 341 342 nonblocking = buf->nonblocking; 343 if (nonblocking && block) 344 { 345 status = set_block (buf); 346 if (status != 0) 347 return status; 348 } 349 350 status = buf_send_output (buf); 351 if (status == 0) 352 status = (*buf->flush) (buf->closure); 353 354 if (nonblocking && block) 355 { 356 int blockstat; 357 358 blockstat = set_nonblock (buf); 359 if (status == 0) 360 status = blockstat; 361 } 362 363 return status; 364} 365 366/* 367 * Set buffer BUF to nonblocking I/O. Returns 0 for success or errno 368 * code. 369 */ 370 371int 372set_nonblock (buf) 373 struct buffer *buf; 374{ 375 int status; 376 377 if (buf->nonblocking) 378 return 0; 379 if (buf->block == NULL) 380 abort (); 381 status = (*buf->block) (buf->closure, 0); 382 if (status != 0) 383 return status; 384 buf->nonblocking = 1; 385 return 0; 386} 387 388/* 389 * Set buffer BUF to blocking I/O. Returns 0 for success or errno 390 * code. 391 */ 392 393int 394set_block (buf) 395 struct buffer *buf; 396{ 397 int status; 398 399 if (! buf->nonblocking) 400 return 0; 401 if (buf->block == NULL) 402 abort (); 403 status = (*buf->block) (buf->closure, 1); 404 if (status != 0) 405 return status; 406 buf->nonblocking = 0; 407 return 0; 408} 409 410/* 411 * Send a character count and some output. Returns errno code or 0 for 412 * success. 413 * 414 * Sending the count in binary is OK since this is only used on a pipe 415 * within the same system. 416 */ 417 418int 419buf_send_counted (buf) 420 struct buffer *buf; 421{ 422 int size; 423 struct buffer_data *data; 424 425 size = 0; 426 for (data = buf->data; data != NULL; data = data->next) 427 size += data->size; 428 429 data = get_buffer_data (); 430 if (data == NULL) 431 { 432 (*buf->memory_error) (buf); 433 return ENOMEM; 434 } 435 436 data->next = buf->data; 437 buf->data = data; 438 if (buf->last == NULL) 439 buf->last = data; 440 441 data->bufp = data->text; 442 data->size = sizeof (int); 443 444 *((int *) data->text) = size; 445 446 return buf_send_output (buf); 447} 448 449/* 450 * Send a special count. COUNT should be negative. It will be 451 * handled speciallyi by buf_copy_counted. This function returns 0 or 452 * an errno code. 453 * 454 * Sending the count in binary is OK since this is only used on a pipe 455 * within the same system. 456 */ 457 458int 459buf_send_special_count (buf, count) 460 struct buffer *buf; 461 int count; 462{ 463 struct buffer_data *data; 464 465 data = get_buffer_data (); 466 if (data == NULL) 467 { 468 (*buf->memory_error) (buf); 469 return ENOMEM; 470 } 471 472 data->next = buf->data; 473 buf->data = data; 474 if (buf->last == NULL) 475 buf->last = data; 476 477 data->bufp = data->text; 478 data->size = sizeof (int); 479 480 *((int *) data->text) = count; 481 482 return buf_send_output (buf); 483} 484 485/* Append a list of buffer_data structures to an buffer. */ 486 487void 488buf_append_data (buf, data, last) 489 struct buffer *buf; 490 struct buffer_data *data; 491 struct buffer_data *last; 492{ 493 if (data != NULL) 494 { 495 if (buf->data == NULL) 496 buf->data = data; 497 else 498 buf->last->next = data; 499 buf->last = last; 500 } 501} 502 503/* Append the data on one buffer to another. This removes the data 504 from the source buffer. */ 505 506void 507buf_append_buffer (to, from) 508 struct buffer *to; 509 struct buffer *from; 510{ 511 buf_append_data (to, from->data, from->last); 512 from->data = NULL; 513 from->last = NULL; 514} 515 516/* 517 * Copy the contents of file F into buffer_data structures. We can't 518 * copy directly into an buffer, because we want to handle failure and 519 * succeess differently. Returns 0 on success, or -2 if out of 520 * memory, or a status code on error. Since the caller happens to 521 * know the size of the file, it is passed in as SIZE. On success, 522 * this function sets *RETP and *LASTP, which may be passed to 523 * buf_append_data. 524 */ 525 526int 527buf_read_file (f, size, retp, lastp) 528 FILE *f; 529 long size; 530 struct buffer_data **retp; 531 struct buffer_data **lastp; 532{ 533 int status; 534 535 *retp = NULL; 536 *lastp = NULL; 537 538 while (size > 0) 539 { 540 struct buffer_data *data; 541 int get; 542 543 data = get_buffer_data (); 544 if (data == NULL) 545 { 546 status = -2; 547 goto error_return; 548 } 549 550 if (*retp == NULL) 551 *retp = data; 552 else 553 (*lastp)->next = data; 554 data->next = NULL; 555 *lastp = data; 556 557 data->bufp = data->text; 558 data->size = 0; 559 560 if (size > BUFFER_DATA_SIZE) 561 get = BUFFER_DATA_SIZE; 562 else 563 get = size; 564 565 errno = EIO; 566 if (fread (data->text, get, 1, f) != 1) 567 { 568 status = errno; 569 goto error_return; 570 } 571 572 data->size += get; 573 size -= get; 574 } 575 576 return 0; 577 578 error_return: 579 if (*retp != NULL) 580 { 581 (*lastp)->next = free_buffer_data; 582 free_buffer_data = *retp; 583 } 584 return status; 585} 586 587/* 588 * Copy the contents of file F into buffer_data structures. We can't 589 * copy directly into an buffer, because we want to handle failure and 590 * succeess differently. Returns 0 on success, or -2 if out of 591 * memory, or a status code on error. On success, this function sets 592 * *RETP and *LASTP, which may be passed to buf_append_data. 593 */ 594 595int 596buf_read_file_to_eof (f, retp, lastp) 597 FILE *f; 598 struct buffer_data **retp; 599 struct buffer_data **lastp; 600{ 601 int status; 602 603 *retp = NULL; 604 *lastp = NULL; 605 606 while (!feof (f)) 607 { 608 struct buffer_data *data; 609 int get, nread; 610 611 data = get_buffer_data (); 612 if (data == NULL) 613 { 614 status = -2; 615 goto error_return; 616 } 617 618 if (*retp == NULL) 619 *retp = data; 620 else 621 (*lastp)->next = data; 622 data->next = NULL; 623 *lastp = data; 624 625 data->bufp = data->text; 626 data->size = 0; 627 628 get = BUFFER_DATA_SIZE; 629 630 errno = EIO; 631 nread = fread (data->text, 1, get, f); 632 if (nread == 0 && !feof (f)) 633 { 634 status = errno; 635 goto error_return; 636 } 637 638 data->size = nread; 639 } 640 641 return 0; 642 643 error_return: 644 if (*retp != NULL) 645 { 646 (*lastp)->next = free_buffer_data; 647 free_buffer_data = *retp; 648 } 649 return status; 650} 651 652/* Return the number of bytes in a chain of buffer_data structures. */ 653 654int 655buf_chain_length (buf) 656 struct buffer_data *buf; 657{ 658 int size = 0; 659 while (buf) 660 { 661 size += buf->size; 662 buf = buf->next; 663 } 664 return size; 665} 666 667/* Return the number of bytes in a buffer. */ 668 669int 670buf_length (buf) 671 struct buffer *buf; 672{ 673 return buf_chain_length (buf->data); 674} 675 676/* 677 * Read an arbitrary amount of data into an input buffer. The buffer 678 * will be in nonblocking mode, and we just grab what we can. Return 679 * 0 on success, or -1 on end of file, or -2 if out of memory, or an 680 * error code. If COUNTP is not NULL, *COUNTP is set to the number of 681 * bytes read. 682 */ 683 684int 685buf_input_data (buf, countp) 686 struct buffer *buf; 687 int *countp; 688{ 689 if (buf->input == NULL) 690 abort (); 691 692 if (countp != NULL) 693 *countp = 0; 694 695 while (1) 696 { 697 int get; 698 int status, nbytes; 699 700 if (buf->data == NULL 701 || (buf->last->bufp + buf->last->size 702 == buf->last->text + BUFFER_DATA_SIZE)) 703 { 704 struct buffer_data *data; 705 706 data = get_buffer_data (); 707 if (data == NULL) 708 { 709 (*buf->memory_error) (buf); 710 return -2; 711 } 712 713 if (buf->data == NULL) 714 buf->data = data; 715 else 716 buf->last->next = data; 717 data->next = NULL; 718 buf->last = data; 719 720 data->bufp = data->text; 721 data->size = 0; 722 } 723 724 get = ((buf->last->text + BUFFER_DATA_SIZE) 725 - (buf->last->bufp + buf->last->size)); 726 727 status = (*buf->input) (buf->closure, 728 buf->last->bufp + buf->last->size, 729 0, get, &nbytes); 730 if (status != 0) 731 return status; 732 733 buf->last->size += nbytes; 734 if (countp != NULL) 735 *countp += nbytes; 736 737 if (nbytes < get) 738 { 739 /* If we did not fill the buffer, then presumably we read 740 all the available data. */ 741 return 0; 742 } 743 } 744 745 /*NOTREACHED*/ 746} 747 748/* 749 * Read a line (characters up to a \012) from an input buffer. (We 750 * use \012 rather than \n for the benefit of non Unix clients for 751 * which \n means something else). This returns 0 on success, or -1 752 * on end of file, or -2 if out of memory, or an error code. If it 753 * succeeds, it sets *LINE to an allocated buffer holding the contents 754 * of the line. The trailing \012 is not included in the buffer. If 755 * LENP is not NULL, then *LENP is set to the number of bytes read; 756 * strlen may not work, because there may be embedded null bytes. 757 */ 758 759int 760buf_read_line (buf, line, lenp) 761 struct buffer *buf; 762 char **line; 763 int *lenp; 764{ 765 if (buf->input == NULL) 766 abort (); 767 768 *line = NULL; 769 770 while (1) 771 { 772 int len, finallen = 0; 773 struct buffer_data *data; 774 char *nl; 775 776 /* See if there is a newline in BUF. */ 777 len = 0; 778 for (data = buf->data; data != NULL; data = data->next) 779 { 780 nl = memchr (data->bufp, '\012', data->size); 781 if (nl != NULL) 782 { 783 finallen = nl - data->bufp; 784 len += finallen; 785 break; 786 } 787 len += data->size; 788 } 789 790 /* If we found a newline, copy the line into a memory buffer, 791 and remove it from BUF. */ 792 if (data != NULL) 793 { 794 char *p; 795 struct buffer_data *nldata; 796 797 p = malloc (len + 1); 798 if (p == NULL) 799 return -2; 800 *line = p; 801 802 nldata = data; 803 data = buf->data; 804 while (data != nldata) 805 { 806 struct buffer_data *next; 807 808 memcpy (p, data->bufp, data->size); 809 p += data->size; 810 next = data->next; 811 data->next = free_buffer_data; 812 free_buffer_data = data; 813 data = next; 814 } 815 816 memcpy (p, data->bufp, finallen); 817 p[finallen] = '\0'; 818 819 data->size -= finallen + 1; 820 data->bufp = nl + 1; 821 buf->data = data; 822 823 if (lenp != NULL) 824 *lenp = len; 825 826 return 0; 827 } 828 829 /* Read more data until we get a newline. */ 830 while (1) 831 { 832 int size, status, nbytes; 833 char *mem; 834 835 if (buf->data == NULL 836 || (buf->last->bufp + buf->last->size 837 == buf->last->text + BUFFER_DATA_SIZE)) 838 { 839 data = get_buffer_data (); 840 if (data == NULL) 841 { 842 (*buf->memory_error) (buf); 843 return -2; 844 } 845 846 if (buf->data == NULL) 847 buf->data = data; 848 else 849 buf->last->next = data; 850 data->next = NULL; 851 buf->last = data; 852 853 data->bufp = data->text; 854 data->size = 0; 855 } 856 857 mem = buf->last->bufp + buf->last->size; 858 size = (buf->last->text + BUFFER_DATA_SIZE) - mem; 859 860 /* We need to read at least 1 byte. We can handle up to 861 SIZE bytes. This will only be efficient if the 862 underlying communication stream does its own buffering, 863 or is clever about getting more than 1 byte at a time. */ 864 status = (*buf->input) (buf->closure, mem, 1, size, &nbytes); 865 if (status != 0) 866 return status; 867 868 buf->last->size += nbytes; 869 870 /* Optimize slightly to avoid an unnecessary call to 871 memchr. */ 872 if (nbytes == 1) 873 { 874 if (*mem == '\012') 875 break; 876 } 877 else 878 { 879 if (memchr (mem, '\012', nbytes) != NULL) 880 break; 881 } 882 } 883 } 884} 885 886/* 887 * Extract data from the input buffer BUF. This will read up to WANT 888 * bytes from the buffer. It will set *RETDATA to point at the bytes, 889 * and set *GOT to the number of bytes to be found there. Any buffer 890 * call which uses BUF may change the contents of the buffer at *DATA, 891 * so the data should be fully processed before any further calls are 892 * made. This returns 0 on success, or -1 on end of file, or -2 if 893 * out of memory, or an error code. 894 */ 895 896int 897buf_read_data (buf, want, retdata, got) 898 struct buffer *buf; 899 int want; 900 char **retdata; 901 int *got; 902{ 903 if (buf->input == NULL) 904 abort (); 905 906 while (buf->data != NULL && buf->data->size == 0) 907 { 908 struct buffer_data *next; 909 910 next = buf->data->next; 911 buf->data->next = free_buffer_data; 912 free_buffer_data = buf->data; 913 buf->data = next; 914 if (next == NULL) 915 buf->last = NULL; 916 } 917 918 if (buf->data == NULL) 919 { 920 struct buffer_data *data; 921 int get, status, nbytes; 922 923 data = get_buffer_data (); 924 if (data == NULL) 925 { 926 (*buf->memory_error) (buf); 927 return -2; 928 } 929 930 buf->data = data; 931 buf->last = data; 932 data->next = NULL; 933 data->bufp = data->text; 934 data->size = 0; 935 936 if (want < BUFFER_DATA_SIZE) 937 get = want; 938 else 939 get = BUFFER_DATA_SIZE; 940 status = (*buf->input) (buf->closure, data->bufp, get, 941 BUFFER_DATA_SIZE, &nbytes); 942 if (status != 0) 943 return status; 944 945 data->size = nbytes; 946 } 947 948 *retdata = buf->data->bufp; 949 if (want < buf->data->size) 950 { 951 *got = want; 952 buf->data->size -= want; 953 buf->data->bufp += want; 954 } 955 else 956 { 957 *got = buf->data->size; 958 buf->data->size = 0; 959 } 960 961 return 0; 962} 963 964/* 965 * Copy lines from an input buffer to an output buffer. This copies 966 * all complete lines (characters up to a newline) from INBUF to 967 * OUTBUF. Each line in OUTBUF is preceded by the character COMMAND 968 * and a space. 969 */ 970 971void 972buf_copy_lines (outbuf, inbuf, command) 973 struct buffer *outbuf; 974 struct buffer *inbuf; 975 int command; 976{ 977 while (1) 978 { 979 struct buffer_data *data; 980 struct buffer_data *nldata; 981 char *nl; 982 int len; 983 984 /* See if there is a newline in INBUF. */ 985 nldata = NULL; 986 nl = NULL; 987 for (data = inbuf->data; data != NULL; data = data->next) 988 { 989 nl = memchr (data->bufp, '\n', data->size); 990 if (nl != NULL) 991 { 992 nldata = data; 993 break; 994 } 995 } 996 997 if (nldata == NULL) 998 { 999 /* There are no more lines in INBUF. */ 1000 return; 1001 } 1002 1003 /* Put in the command. */ 1004 buf_append_char (outbuf, command); 1005 buf_append_char (outbuf, ' '); 1006 1007 if (inbuf->data != nldata) 1008 { 1009 /* 1010 * Simply move over all the buffers up to the one containing 1011 * the newline. 1012 */ 1013 for (data = inbuf->data; data->next != nldata; data = data->next) 1014 ; 1015 data->next = NULL; 1016 buf_append_data (outbuf, inbuf->data, data); 1017 inbuf->data = nldata; 1018 } 1019 1020 /* 1021 * If the newline is at the very end of the buffer, just move 1022 * the buffer onto OUTBUF. Otherwise we must copy the data. 1023 */ 1024 len = nl + 1 - nldata->bufp; 1025 if (len == nldata->size) 1026 { 1027 inbuf->data = nldata->next; 1028 if (inbuf->data == NULL) 1029 inbuf->last = NULL; 1030 1031 nldata->next = NULL; 1032 buf_append_data (outbuf, nldata, nldata); 1033 } 1034 else 1035 { 1036 buf_output (outbuf, nldata->bufp, len); 1037 nldata->bufp += len; 1038 nldata->size -= len; 1039 } 1040 } 1041} 1042 1043/* 1044 * Copy counted data from one buffer to another. The count is an 1045 * integer, host size, host byte order (it is only used across a 1046 * pipe). If there is enough data, it should be moved over. If there 1047 * is not enough data, it should remain on the original buffer. A 1048 * negative count is a special case. if one is seen, *SPECIAL is set 1049 * to the (negative) count value and no additional data is gathered 1050 * from the buffer; normally *SPECIAL is set to 0. This function 1051 * returns the number of bytes it needs to see in order to actually 1052 * copy something over. 1053 */ 1054 1055int 1056buf_copy_counted (outbuf, inbuf, special) 1057 struct buffer *outbuf; 1058 struct buffer *inbuf; 1059 int *special; 1060{ 1061 *special = 0; 1062 1063 while (1) 1064 { 1065 struct buffer_data *data; 1066 int need; 1067 union 1068 { 1069 char intbuf[sizeof (int)]; 1070 int i; 1071 } u; 1072 char *intp; 1073 int count; 1074 struct buffer_data *start; 1075 int startoff; 1076 struct buffer_data *stop; 1077 int stopwant; 1078 1079 /* See if we have enough bytes to figure out the count. */ 1080 need = sizeof (int); 1081 intp = u.intbuf; 1082 for (data = inbuf->data; data != NULL; data = data->next) 1083 { 1084 if (data->size >= need) 1085 { 1086 memcpy (intp, data->bufp, need); 1087 break; 1088 } 1089 memcpy (intp, data->bufp, data->size); 1090 intp += data->size; 1091 need -= data->size; 1092 } 1093 if (data == NULL) 1094 { 1095 /* We don't have enough bytes to form an integer. */ 1096 return need; 1097 } 1098 1099 count = u.i; 1100 start = data; 1101 startoff = need; 1102 1103 if (count < 0) 1104 { 1105 /* A negative COUNT is a special case meaning that we 1106 don't need any further information. */ 1107 stop = start; 1108 stopwant = 0; 1109 } 1110 else 1111 { 1112 /* 1113 * We have an integer in COUNT. We have gotten all the 1114 * data from INBUF in all buffers before START, and we 1115 * have gotten STARTOFF bytes from START. See if we have 1116 * enough bytes remaining in INBUF. 1117 */ 1118 need = count - (start->size - startoff); 1119 if (need <= 0) 1120 { 1121 stop = start; 1122 stopwant = count; 1123 } 1124 else 1125 { 1126 for (data = start->next; data != NULL; data = data->next) 1127 { 1128 if (need <= data->size) 1129 break; 1130 need -= data->size; 1131 } 1132 if (data == NULL) 1133 { 1134 /* We don't have enough bytes. */ 1135 return need; 1136 } 1137 stop = data; 1138 stopwant = need; 1139 } 1140 } 1141 1142 /* 1143 * We have enough bytes. Free any buffers in INBUF before 1144 * START, and remove STARTOFF bytes from START, so that we can 1145 * forget about STARTOFF. 1146 */ 1147 start->bufp += startoff; 1148 start->size -= startoff; 1149 1150 if (start->size == 0) 1151 start = start->next; 1152 1153 if (stop->size == stopwant) 1154 { 1155 stop = stop->next; 1156 stopwant = 0; 1157 } 1158 1159 while (inbuf->data != start) 1160 { 1161 data = inbuf->data; 1162 inbuf->data = data->next; 1163 data->next = free_buffer_data; 1164 free_buffer_data = data; 1165 } 1166 1167 /* If COUNT is negative, set *SPECIAL and get out now. */ 1168 if (count < 0) 1169 { 1170 *special = count; 1171 return 0; 1172 } 1173 1174 /* 1175 * We want to copy over the bytes from START through STOP. We 1176 * only want STOPWANT bytes from STOP. 1177 */ 1178 1179 if (start != stop) 1180 { 1181 /* Attach the buffers from START through STOP to OUTBUF. */ 1182 for (data = start; data->next != stop; data = data->next) 1183 ; 1184 inbuf->data = stop; 1185 data->next = NULL; 1186 buf_append_data (outbuf, start, data); 1187 } 1188 1189 if (stopwant > 0) 1190 { 1191 buf_output (outbuf, stop->bufp, stopwant); 1192 stop->bufp += stopwant; 1193 stop->size -= stopwant; 1194 } 1195 } 1196 1197 /*NOTREACHED*/ 1198} 1199 1200/* Shut down a buffer. This returns 0 on success, or an errno code. */ 1201 1202int 1203buf_shutdown (buf) 1204 struct buffer *buf; 1205{ 1206 if (buf->shutdown) 1207 return (*buf->shutdown) (buf); 1208 return 0; 1209} 1210 1211/* The simplest type of buffer is one built on top of a stdio FILE. 1212 For simplicity, and because it is all that is required, we do not 1213 implement setting this type of buffer into nonblocking mode. The 1214 closure field is just a FILE *. */ 1215 1216static int stdio_buffer_input PROTO((void *, char *, int, int, int *)); 1217static int stdio_buffer_output PROTO((void *, const char *, int, int *)); 1218static int stdio_buffer_flush PROTO((void *)); 1219static int stdio_buffer_shutdown PROTO((struct buffer *buf)); 1220 1221/* Initialize a buffer built on a stdio FILE. */ 1222 1223struct stdio_buffer_closure 1224{ 1225 FILE *fp; 1226 int child_pid; 1227}; 1228 1229struct buffer * 1230stdio_buffer_initialize (fp, child_pid, input, memory) 1231 FILE *fp; 1232 int child_pid; 1233 int input; 1234 void (*memory) PROTO((struct buffer *)); 1235{ 1236 struct stdio_buffer_closure *bc = malloc (sizeof (*bc)); 1237 1238 bc->fp = fp; 1239 bc->child_pid = child_pid; 1240 1241 return buf_initialize (input ? stdio_buffer_input : NULL, 1242 input ? NULL : stdio_buffer_output, 1243 input ? NULL : stdio_buffer_flush, 1244 (int (*) PROTO((void *, int))) NULL, 1245 stdio_buffer_shutdown, 1246 memory, 1247 (void *) bc); 1248} 1249 1250/* The buffer input function for a buffer built on a stdio FILE. */ 1251 1252static int 1253stdio_buffer_input (closure, data, need, size, got) 1254 void *closure; 1255 char *data; 1256 int need; 1257 int size; 1258 int *got; 1259{ 1260 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure; 1261 int nbytes; 1262 1263 /* Since stdio does its own buffering, we don't worry about 1264 getting more bytes than we need. */ 1265 1266 if (need == 0 || need == 1) 1267 { 1268 int ch; 1269 1270 ch = getc (bc->fp); 1271 1272 if (ch == EOF) 1273 { 1274 if (feof (bc->fp)) 1275 return -1; 1276 else if (errno == 0) 1277 return EIO; 1278 else 1279 return errno; 1280 } 1281 1282 *data = ch; 1283 *got = 1; 1284 return 0; 1285 } 1286 1287 nbytes = fread (data, 1, need, bc->fp); 1288 1289 if (nbytes == 0) 1290 { 1291 *got = 0; 1292 if (feof (bc->fp)) 1293 return -1; 1294 else if (errno == 0) 1295 return EIO; 1296 else 1297 return errno; 1298 } 1299 1300 *got = nbytes; 1301 1302 return 0; 1303} 1304 1305/* The buffer output function for a buffer built on a stdio FILE. */ 1306 1307static int 1308stdio_buffer_output (closure, data, have, wrote) 1309 void *closure; 1310 const char *data; 1311 int have; 1312 int *wrote; 1313{ 1314 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure; 1315 1316 *wrote = 0; 1317 1318 while (have > 0) 1319 { 1320 int nbytes; 1321 1322 nbytes = fwrite (data, 1, have, bc->fp); 1323 1324 if (nbytes != have) 1325 { 1326 if (errno == 0) 1327 return EIO; 1328 else 1329 return errno; 1330 } 1331 1332 *wrote += nbytes; 1333 have -= nbytes; 1334 data += nbytes; 1335 } 1336 1337 return 0; 1338} 1339 1340/* The buffer flush function for a buffer built on a stdio FILE. */ 1341 1342static int 1343stdio_buffer_flush (closure) 1344 void *closure; 1345{ 1346 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure; 1347 1348 if (fflush (bc->fp) != 0) 1349 { 1350 if (errno == 0) 1351 return EIO; 1352 else 1353 return errno; 1354 } 1355 1356 return 0; 1357} 1358 1359 1360 1361static int 1362stdio_buffer_shutdown (buf) 1363 struct buffer *buf; 1364{ 1365 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) buf->closure; 1366 struct stat s; 1367 int closefp = 1; 1368 1369 /* Must be a pipe or a socket. What could go wrong? */ 1370 assert (fstat ( fileno (bc->fp), &s ) != -1); 1371 1372 /* Flush the buffer if we can */ 1373 if (buf->flush) 1374 { 1375 buf_flush (buf, 1); 1376 buf->flush = NULL; 1377 } 1378 1379 if (buf->input) 1380 { 1381 if (! buf_empty_p (buf) 1382 || getc (bc->fp) != EOF) 1383 { 1384# ifdef SERVER_SUPPORT 1385 if (server_active) 1386 /* FIXME: This should probably be sysloged since it doesn't 1387 * have anywhere else to go at this point. 1388 */ 1389 error (0, 0, "dying gasps from client unexpected"); 1390 else 1391#endif 1392 error (0, 0, "dying gasps from %s unexpected", current_parsed_root->hostname); 1393 } 1394 else if (ferror (bc->fp)) 1395 { 1396# ifdef SERVER_SUPPORT 1397 if (server_active) 1398 /* FIXME: This should probably be sysloged since it doesn't 1399 * have anywhere else to go at this point. 1400 */ 1401 error (0, errno, "reading from client"); 1402 else 1403#endif 1404 error (0, errno, "reading from %s", current_parsed_root->hostname); 1405 } 1406 1407# ifdef SHUTDOWN_SERVER 1408 if (current_parsed_root->method != server_method) 1409# endif 1410# ifndef NO_SOCKET_TO_FD 1411 { 1412 /* shutdown() sockets */ 1413 if (S_ISSOCK(s.st_mode)) 1414 shutdown ( fileno (bc->fp), 0); 1415 } 1416# endif /* NO_SOCKET_TO_FD */ 1417# ifdef START_RSH_WITH_POPEN_RW 1418 /* Can't be set with SHUTDOWN_SERVER defined */ 1419 else if (pclose (bc->fp) == EOF) 1420 { 1421 error (1, errno, "closing connection to %s", 1422 current_parsed_root->hostname); 1423 closefp = 0; 1424 } 1425# endif /* START_RSH_WITH_POPEN_RW */ 1426 1427 buf->input = NULL; 1428 } 1429 else if (buf->output) 1430 { 1431# ifdef SHUTDOWN_SERVER 1432 /* FIXME: Should have a SHUTDOWN_SERVER_INPUT & 1433 * SHUTDOWN_SERVER_OUTPUT 1434 */ 1435 if (current_parsed_root->method == server_method) 1436 SHUTDOWN_SERVER ( fileno (bc->fp) ); 1437 else 1438# endif 1439# ifndef NO_SOCKET_TO_FD 1440 /* shutdown() sockets */ 1441 if (S_ISSOCK(s.st_mode)) 1442 shutdown ( fileno (bc->fp), 1); 1443# else 1444 { 1445 /* I'm not sure I like this empty block, but the alternative 1446 * is a another nested NO_SOCKET_TO_FD switch above. 1447 */ 1448 } 1449# endif /* NO_SOCKET_TO_FD */ 1450 1451 buf->output = NULL; 1452 } 1453 1454 if (closefp && fclose (bc->fp) == EOF) 1455 error (1, errno, 1456 "closing down connection to %s", 1457 current_parsed_root->hostname); 1458 1459 /* If we were talking to a process, make sure it exited */ 1460 if (bc->child_pid 1461 && waitpid (bc->child_pid, (int *) 0, 0) == -1) 1462 error (1, errno, "waiting for process %d", bc->child_pid); 1463 1464 return 0; 1465} 1466 1467 1468 1469/* Certain types of communication input and output data in packets, 1470 where each packet is translated in some fashion. The packetizing 1471 buffer type supports that, given a buffer which handles lower level 1472 I/O and a routine to translate the data in a packet. 1473 1474 This code uses two bytes for the size of a packet, so packets are 1475 restricted to 65536 bytes in total. 1476 1477 The translation functions should just translate; they may not 1478 significantly increase or decrease the amount of data. The actual 1479 size of the initial data is part of the translated data. The 1480 output translation routine may add up to PACKET_SLOP additional 1481 bytes, and the input translation routine should shrink the data 1482 correspondingly. */ 1483 1484#define PACKET_SLOP (100) 1485 1486/* This structure is the closure field of a packetizing buffer. */ 1487 1488struct packetizing_buffer 1489{ 1490 /* The underlying buffer. */ 1491 struct buffer *buf; 1492 /* The input translation function. Exactly one of inpfn and outfn 1493 will be NULL. The input translation function should 1494 untranslate the data in INPUT, storing the result in OUTPUT. 1495 SIZE is the amount of data in INPUT, and is also the size of 1496 OUTPUT. This should return 0 on success, or an errno code. */ 1497 int (*inpfn) PROTO((void *fnclosure, const char *input, char *output, 1498 int size)); 1499 /* The output translation function. This should translate the 1500 data in INPUT, storing the result in OUTPUT. The first two 1501 bytes in INPUT will be the size of the data, and so will SIZE. 1502 This should set *TRANSLATED to the amount of translated data in 1503 OUTPUT. OUTPUT is large enough to hold SIZE + PACKET_SLOP 1504 bytes. This should return 0 on success, or an errno code. */ 1505 int (*outfn) PROTO((void *fnclosure, const char *input, char *output, 1506 int size, int *translated)); 1507 /* A closure for the translation function. */ 1508 void *fnclosure; 1509 /* For an input buffer, we may have to buffer up data here. */ 1510 /* This is non-zero if the buffered data has been translated. 1511 Otherwise, the buffered data has not been translated, and starts 1512 with the two byte packet size. */ 1513 int translated; 1514 /* The amount of buffered data. */ 1515 int holdsize; 1516 /* The buffer allocated to hold the data. */ 1517 char *holdbuf; 1518 /* The size of holdbuf. */ 1519 int holdbufsize; 1520 /* If translated is set, we need another data pointer to track 1521 where we are in holdbuf. If translated is clear, then this 1522 pointer is not used. */ 1523 char *holddata; 1524}; 1525 1526static int packetizing_buffer_input PROTO((void *, char *, int, int, int *)); 1527static int packetizing_buffer_output PROTO((void *, const char *, int, int *)); 1528static int packetizing_buffer_flush PROTO((void *)); 1529static int packetizing_buffer_block PROTO((void *, int)); 1530static int packetizing_buffer_shutdown PROTO((struct buffer *)); 1531 1532/* Create a packetizing buffer. */ 1533 1534struct buffer * 1535packetizing_buffer_initialize (buf, inpfn, outfn, fnclosure, memory) 1536 struct buffer *buf; 1537 int (*inpfn) PROTO ((void *, const char *, char *, int)); 1538 int (*outfn) PROTO ((void *, const char *, char *, int, int *)); 1539 void *fnclosure; 1540 void (*memory) PROTO((struct buffer *)); 1541{ 1542 struct packetizing_buffer *pb; 1543 1544 pb = (struct packetizing_buffer *) xmalloc (sizeof *pb); 1545 memset (pb, 0, sizeof *pb); 1546 1547 pb->buf = buf; 1548 pb->inpfn = inpfn; 1549 pb->outfn = outfn; 1550 pb->fnclosure = fnclosure; 1551 1552 if (inpfn != NULL) 1553 { 1554 /* Add PACKET_SLOP to handle larger translated packets, and 1555 add 2 for the count. This buffer is increased if 1556 necessary. */ 1557 pb->holdbufsize = BUFFER_DATA_SIZE + PACKET_SLOP + 2; 1558 pb->holdbuf = xmalloc (pb->holdbufsize); 1559 } 1560 1561 return buf_initialize (inpfn != NULL ? packetizing_buffer_input : NULL, 1562 inpfn != NULL ? NULL : packetizing_buffer_output, 1563 inpfn != NULL ? NULL : packetizing_buffer_flush, 1564 packetizing_buffer_block, 1565 packetizing_buffer_shutdown, 1566 memory, 1567 pb); 1568} 1569 1570/* Input data from a packetizing buffer. */ 1571 1572static int 1573packetizing_buffer_input (closure, data, need, size, got) 1574 void *closure; 1575 char *data; 1576 int need; 1577 int size; 1578 int *got; 1579{ 1580 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure; 1581 1582 *got = 0; 1583 1584 if (pb->holdsize > 0 && pb->translated) 1585 { 1586 int copy; 1587 1588 copy = pb->holdsize; 1589 1590 if (copy > size) 1591 { 1592 memcpy (data, pb->holddata, size); 1593 pb->holdsize -= size; 1594 pb->holddata += size; 1595 *got = size; 1596 return 0; 1597 } 1598 1599 memcpy (data, pb->holddata, copy); 1600 pb->holdsize = 0; 1601 pb->translated = 0; 1602 1603 data += copy; 1604 need -= copy; 1605 size -= copy; 1606 *got = copy; 1607 } 1608 1609 while (need > 0 || *got == 0) 1610 { 1611 int get, status, nread, count, tcount; 1612 char *bytes; 1613 char stackoutbuf[BUFFER_DATA_SIZE + PACKET_SLOP]; 1614 char *inbuf, *outbuf; 1615 1616 /* If we don't already have the two byte count, get it. */ 1617 if (pb->holdsize < 2) 1618 { 1619 get = 2 - pb->holdsize; 1620 status = buf_read_data (pb->buf, get, &bytes, &nread); 1621 if (status != 0) 1622 { 1623 /* buf_read_data can return -2, but a buffer input 1624 function is only supposed to return -1, 0, or an 1625 error code. */ 1626 if (status == -2) 1627 status = ENOMEM; 1628 return status; 1629 } 1630 1631 if (nread == 0) 1632 { 1633 /* The buffer is in nonblocking mode, and we didn't 1634 manage to read anything. */ 1635 return 0; 1636 } 1637 1638 if (get == 1) 1639 pb->holdbuf[1] = bytes[0]; 1640 else 1641 { 1642 pb->holdbuf[0] = bytes[0]; 1643 if (nread < 2) 1644 { 1645 /* We only got one byte, but we needed two. Stash 1646 the byte we got, and try again. */ 1647 pb->holdsize = 1; 1648 continue; 1649 } 1650 pb->holdbuf[1] = bytes[1]; 1651 } 1652 pb->holdsize = 2; 1653 } 1654 1655 /* Read the packet. */ 1656 1657 count = (((pb->holdbuf[0] & 0xff) << 8) 1658 + (pb->holdbuf[1] & 0xff)); 1659 1660 if (count + 2 > pb->holdbufsize) 1661 { 1662 char *n; 1663 1664 /* We didn't allocate enough space in the initialize 1665 function. */ 1666 1667 n = realloc (pb->holdbuf, count + 2); 1668 if (n == NULL) 1669 { 1670 (*pb->buf->memory_error) (pb->buf); 1671 return ENOMEM; 1672 } 1673 pb->holdbuf = n; 1674 pb->holdbufsize = count + 2; 1675 } 1676 1677 get = count - (pb->holdsize - 2); 1678 1679 status = buf_read_data (pb->buf, get, &bytes, &nread); 1680 if (status != 0) 1681 { 1682 /* buf_read_data can return -2, but a buffer input 1683 function is only supposed to return -1, 0, or an error 1684 code. */ 1685 if (status == -2) 1686 status = ENOMEM; 1687 return status; 1688 } 1689 1690 if (nread == 0) 1691 { 1692 /* We did not get any data. Presumably the buffer is in 1693 nonblocking mode. */ 1694 return 0; 1695 } 1696 1697 if (nread < get) 1698 { 1699 /* We did not get all the data we need to fill the packet. 1700 buf_read_data does not promise to return all the bytes 1701 requested, so we must try again. */ 1702 memcpy (pb->holdbuf + pb->holdsize, bytes, nread); 1703 pb->holdsize += nread; 1704 continue; 1705 } 1706 1707 /* We have a complete untranslated packet of COUNT bytes. */ 1708 1709 if (pb->holdsize == 2) 1710 { 1711 /* We just read the entire packet (the 2 bytes in 1712 PB->HOLDBUF are the size). Save a memcpy by 1713 translating directly from BYTES. */ 1714 inbuf = bytes; 1715 } 1716 else 1717 { 1718 /* We already had a partial packet in PB->HOLDBUF. We 1719 need to copy the new data over to make the input 1720 contiguous. */ 1721 memcpy (pb->holdbuf + pb->holdsize, bytes, nread); 1722 inbuf = pb->holdbuf + 2; 1723 } 1724 1725 if (count <= sizeof stackoutbuf) 1726 outbuf = stackoutbuf; 1727 else 1728 { 1729 outbuf = malloc (count); 1730 if (outbuf == NULL) 1731 { 1732 (*pb->buf->memory_error) (pb->buf); 1733 return ENOMEM; 1734 } 1735 } 1736 1737 status = (*pb->inpfn) (pb->fnclosure, inbuf, outbuf, count); 1738 if (status != 0) 1739 return status; 1740 1741 /* The first two bytes in the translated buffer are the real 1742 length of the translated data. */ 1743 tcount = ((outbuf[0] & 0xff) << 8) + (outbuf[1] & 0xff); 1744 1745 if (tcount > count) 1746 error (1, 0, "Input translation failure"); 1747 1748 if (tcount > size) 1749 { 1750 /* We have more data than the caller has provided space 1751 for. We need to save some of it for the next call. */ 1752 1753 memcpy (data, outbuf + 2, size); 1754 *got += size; 1755 1756 pb->holdsize = tcount - size; 1757 memcpy (pb->holdbuf, outbuf + 2 + size, tcount - size); 1758 pb->holddata = pb->holdbuf; 1759 pb->translated = 1; 1760 1761 if (outbuf != stackoutbuf) 1762 free (outbuf); 1763 1764 return 0; 1765 } 1766 1767 memcpy (data, outbuf + 2, tcount); 1768 1769 if (outbuf != stackoutbuf) 1770 free (outbuf); 1771 1772 pb->holdsize = 0; 1773 1774 data += tcount; 1775 need -= tcount; 1776 size -= tcount; 1777 *got += tcount; 1778 } 1779 1780 return 0; 1781} 1782 1783/* Output data to a packetizing buffer. */ 1784 1785static int 1786packetizing_buffer_output (closure, data, have, wrote) 1787 void *closure; 1788 const char *data; 1789 int have; 1790 int *wrote; 1791{ 1792 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure; 1793 char inbuf[BUFFER_DATA_SIZE + 2]; 1794 char stack_outbuf[BUFFER_DATA_SIZE + PACKET_SLOP + 4]; 1795 struct buffer_data *outdata; 1796 char *outbuf; 1797 int size, status, translated; 1798 1799 if (have > BUFFER_DATA_SIZE) 1800 { 1801 /* It would be easy to malloc a buffer, but I don't think this 1802 case can ever arise. */ 1803 abort (); 1804 } 1805 1806 inbuf[0] = (have >> 8) & 0xff; 1807 inbuf[1] = have & 0xff; 1808 memcpy (inbuf + 2, data, have); 1809 1810 size = have + 2; 1811 1812 /* The output function is permitted to add up to PACKET_SLOP 1813 bytes, and we need 2 bytes for the size of the translated data. 1814 If we can guarantee that the result will fit in a buffer_data, 1815 we translate directly into one to avoid a memcpy in buf_output. */ 1816 if (size + PACKET_SLOP + 2 > BUFFER_DATA_SIZE) 1817 outbuf = stack_outbuf; 1818 else 1819 { 1820 outdata = get_buffer_data (); 1821 if (outdata == NULL) 1822 { 1823 (*pb->buf->memory_error) (pb->buf); 1824 return ENOMEM; 1825 } 1826 1827 outdata->next = NULL; 1828 outdata->bufp = outdata->text; 1829 1830 outbuf = outdata->text; 1831 } 1832 1833 status = (*pb->outfn) (pb->fnclosure, inbuf, outbuf + 2, size, 1834 &translated); 1835 if (status != 0) 1836 return status; 1837 1838 /* The output function is permitted to add up to PACKET_SLOP 1839 bytes. */ 1840 if (translated > size + PACKET_SLOP) 1841 abort (); 1842 1843 outbuf[0] = (translated >> 8) & 0xff; 1844 outbuf[1] = translated & 0xff; 1845 1846 if (outbuf == stack_outbuf) 1847 buf_output (pb->buf, outbuf, translated + 2); 1848 else 1849 { 1850 outdata->size = translated + 2; 1851 buf_append_data (pb->buf, outdata, outdata); 1852 } 1853 1854 *wrote = have; 1855 1856 /* We will only be here because buf_send_output was called on the 1857 packetizing buffer. That means that we should now call 1858 buf_send_output on the underlying buffer. */ 1859 return buf_send_output (pb->buf); 1860} 1861 1862/* Flush data to a packetizing buffer. */ 1863 1864static int 1865packetizing_buffer_flush (closure) 1866 void *closure; 1867{ 1868 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure; 1869 1870 /* Flush the underlying buffer. Note that if the original call to 1871 buf_flush passed 1 for the BLOCK argument, then the buffer will 1872 already have been set into blocking mode, so we should always 1873 pass 0 here. */ 1874 return buf_flush (pb->buf, 0); 1875} 1876 1877/* The block routine for a packetizing buffer. */ 1878 1879static int 1880packetizing_buffer_block (closure, block) 1881 void *closure; 1882 int block; 1883{ 1884 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure; 1885 1886 if (block) 1887 return set_block (pb->buf); 1888 else 1889 return set_nonblock (pb->buf); 1890} 1891 1892/* Shut down a packetizing buffer. */ 1893 1894static int 1895packetizing_buffer_shutdown (buf) 1896 struct buffer *buf; 1897{ 1898 struct packetizing_buffer *pb = (struct packetizing_buffer *) buf->closure; 1899 1900 return buf_shutdown (pb->buf); 1901} 1902 1903#endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */ 1904