/contrib/cvs/src/buffer.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1980 lines · 1327 code · 329 blank · 324 comment · 314 complexity · 1a9f15c9205f82d5439603fec3fa5f4d MD5 · raw file

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