1 /* Code for the buffer data structure. */
9 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
14 # include <sys/socket.h>
17 /* OS/2 doesn't have EIO. FIXME: this whole notion of turning
18 a different error into EIO strikes me as pretty dubious. */
23 /* Linked list of available buffer_data structures. */
24 static struct buffer_data *free_buffer_data;
26 /* Local functions. */
27 static void buf_default_memory_error PROTO ((struct buffer *));
28 static void allocate_buffer_datas PROTO((void));
29 static struct buffer_data *get_buffer_data PROTO((void));
31 /* Initialize a buffer structure. */
34 buf_initialize (input, output, flush, block, shutdown, memory, closure)
35 int (*input) PROTO((void *, char *, int, int, int *));
36 int (*output) PROTO((void *, const char *, int, int *));
37 int (*flush) PROTO((void *));
38 int (*block) PROTO((void *, int));
39 int (*shutdown) PROTO((struct buffer *));
40 void (*memory) PROTO((struct buffer *));
45 buf = (struct buffer *) xmalloc (sizeof (struct buffer));
53 buf->shutdown = shutdown;
54 buf->memory_error = memory ? memory : buf_default_memory_error;
55 buf->closure = closure;
59 /* Free a buffer structure. */
65 if (buf->closure != NULL)
70 if (buf->data != NULL)
72 buf->last->next = free_buffer_data;
73 free_buffer_data = buf->data;
78 /* Initialize a buffer structure which is not to be used for I/O. */
81 buf_nonio_initialize (memory)
82 void (*memory) PROTO((struct buffer *));
84 return (buf_initialize
85 ((int (*) PROTO((void *, char *, int, int, int *))) NULL,
86 (int (*) PROTO((void *, const char *, int, int *))) NULL,
87 (int (*) PROTO((void *))) NULL,
88 (int (*) PROTO((void *, int))) NULL,
89 (int (*) PROTO((struct buffer *))) NULL,
94 /* Default memory error handler. */
97 buf_default_memory_error (buf)
100 error (1, 0, "out of memory");
103 /* Allocate more buffer_data structures. */
106 allocate_buffer_datas ()
108 struct buffer_data *alc;
112 /* Allocate buffer_data structures in blocks of 16. */
113 #define ALLOC_COUNT (16)
115 alc = ((struct buffer_data *)
116 xmalloc (ALLOC_COUNT * sizeof (struct buffer_data)));
117 space = (char *) valloc (ALLOC_COUNT * BUFFER_DATA_SIZE);
118 if (alc == NULL || space == NULL)
120 for (i = 0; i < ALLOC_COUNT; i++, alc++, space += BUFFER_DATA_SIZE)
122 alc->next = free_buffer_data;
123 free_buffer_data = alc;
128 /* Get a new buffer_data structure. */
130 static struct buffer_data *
133 struct buffer_data *ret;
135 if (free_buffer_data == NULL)
137 allocate_buffer_datas ();
138 if (free_buffer_data == NULL)
142 ret = free_buffer_data;
143 free_buffer_data = ret->next;
149 /* See whether a buffer and its file descriptor is empty. */
154 /* Try and read any data on the file descriptor first.
155 * We already know the descriptor is non-blocking.
157 buf_input_data (buf, NULL);
158 return buf_empty_p (buf);
163 /* See whether a buffer is empty. */
168 struct buffer_data *data;
170 for (data = buf->data; data != NULL; data = data->next)
178 #ifdef SERVER_FLOWCONTROL
180 * Count how much data is stored in the buffer..
181 * Note that each buffer is a xmalloc'ed chunk BUFFER_DATA_SIZE.
188 struct buffer_data *data;
191 for (data = buf->data; data != NULL; data = data->next)
192 mem += BUFFER_DATA_SIZE;
196 #endif /* SERVER_FLOWCONTROL */
198 /* Add data DATA of length LEN to BUF. */
201 buf_output (buf, data, len)
206 if (buf->data != NULL
207 && (((buf->last->text + BUFFER_DATA_SIZE)
208 - (buf->last->bufp + buf->last->size))
211 memcpy (buf->last->bufp + buf->last->size, data, len);
212 buf->last->size += len;
218 struct buffer_data *newdata;
220 newdata = get_buffer_data ();
223 (*buf->memory_error) (buf);
227 if (buf->data == NULL)
230 buf->last->next = newdata;
231 newdata->next = NULL;
234 newdata->bufp = newdata->text;
236 if (len <= BUFFER_DATA_SIZE)
239 memcpy (newdata->text, data, len);
243 newdata->size = BUFFER_DATA_SIZE;
244 memcpy (newdata->text, data, BUFFER_DATA_SIZE);
246 data += BUFFER_DATA_SIZE;
247 len -= BUFFER_DATA_SIZE;
253 /* Add a '\0' terminated string to BUF. */
256 buf_output0 (buf, string)
260 buf_output (buf, string, strlen (string));
263 /* Add a single character to BUF. */
266 buf_append_char (buf, ch)
270 if (buf->data != NULL
271 && (buf->last->text + BUFFER_DATA_SIZE
272 != buf->last->bufp + buf->last->size))
274 *(buf->last->bufp + buf->last->size) = ch;
282 buf_output (buf, &b, 1);
287 * Send all the output we've been saving up. Returns 0 for success or
288 * errno code. If the buffer has been set to be nonblocking, this
289 * will just write until the write would block.
293 buf_send_output (buf)
296 if (buf->output == NULL)
299 while (buf->data != NULL)
301 struct buffer_data *data;
309 status = (*buf->output) (buf->closure, data->bufp, data->size,
313 /* Some sort of error. Discard the data, and return. */
315 buf->last->next = free_buffer_data;
316 free_buffer_data = buf->data;
323 if (nbytes != data->size)
325 /* Not all the data was written out. This is only
326 permitted in nonblocking mode. Adjust the buffer,
329 assert (buf->nonblocking);
331 data->size -= nbytes;
332 data->bufp += nbytes;
338 buf->data = data->next;
339 data->next = free_buffer_data;
340 free_buffer_data = data;
349 * Flush any data queued up in the buffer. If BLOCK is nonzero, then
350 * if the buffer is in nonblocking mode, put it into blocking mode for
351 * the duration of the flush. This returns 0 on success, or an error
356 buf_flush (buf, block)
363 if (buf->flush == NULL)
366 nonblocking = buf->nonblocking;
367 if (nonblocking && block)
369 status = set_block (buf);
374 status = buf_send_output (buf);
376 status = (*buf->flush) (buf->closure);
378 if (nonblocking && block)
382 blockstat = set_nonblock (buf);
391 * Set buffer BUF to nonblocking I/O. Returns 0 for success or errno
401 if (buf->nonblocking)
403 if (buf->block == NULL)
405 status = (*buf->block) (buf->closure, 0);
408 buf->nonblocking = 1;
413 * Set buffer BUF to blocking I/O. Returns 0 for success or errno
423 if (! buf->nonblocking)
425 if (buf->block == NULL)
427 status = (*buf->block) (buf->closure, 1);
430 buf->nonblocking = 0;
435 * Send a character count and some output. Returns errno code or 0 for
438 * Sending the count in binary is OK since this is only used on a pipe
439 * within the same system.
443 buf_send_counted (buf)
447 struct buffer_data *data;
450 for (data = buf->data; data != NULL; data = data->next)
453 data = get_buffer_data ();
456 (*buf->memory_error) (buf);
460 data->next = buf->data;
462 if (buf->last == NULL)
465 data->bufp = data->text;
466 data->size = sizeof (int);
468 *((int *) data->text) = size;
470 return buf_send_output (buf);
474 * Send a special count. COUNT should be negative. It will be
475 * handled speciallyi by buf_copy_counted. This function returns 0 or
478 * Sending the count in binary is OK since this is only used on a pipe
479 * within the same system.
483 buf_send_special_count (buf, count)
487 struct buffer_data *data;
489 data = get_buffer_data ();
492 (*buf->memory_error) (buf);
496 data->next = buf->data;
498 if (buf->last == NULL)
501 data->bufp = data->text;
502 data->size = sizeof (int);
504 *((int *) data->text) = count;
506 return buf_send_output (buf);
509 /* Append a list of buffer_data structures to an buffer. */
512 buf_append_data (buf, data, last)
514 struct buffer_data *data;
515 struct buffer_data *last;
519 if (buf->data == NULL)
522 buf->last->next = data;
527 /* Append the data on one buffer to another. This removes the data
528 from the source buffer. */
531 buf_append_buffer (to, from)
535 buf_append_data (to, from->data, from->last);
541 * Copy the contents of file F into buffer_data structures. We can't
542 * copy directly into an buffer, because we want to handle failure and
543 * succeess differently. Returns 0 on success, or -2 if out of
544 * memory, or a status code on error. Since the caller happens to
545 * know the size of the file, it is passed in as SIZE. On success,
546 * this function sets *RETP and *LASTP, which may be passed to
551 buf_read_file (f, size, retp, lastp)
554 struct buffer_data **retp;
555 struct buffer_data **lastp;
564 struct buffer_data *data;
567 data = get_buffer_data ();
577 (*lastp)->next = data;
581 data->bufp = data->text;
584 if (size > BUFFER_DATA_SIZE)
585 get = BUFFER_DATA_SIZE;
590 if (fread (data->text, get, 1, f) != 1)
605 (*lastp)->next = free_buffer_data;
606 free_buffer_data = *retp;
612 * Copy the contents of file F into buffer_data structures. We can't
613 * copy directly into an buffer, because we want to handle failure and
614 * succeess differently. Returns 0 on success, or -2 if out of
615 * memory, or a status code on error. On success, this function sets
616 * *RETP and *LASTP, which may be passed to buf_append_data.
620 buf_read_file_to_eof (f, retp, lastp)
622 struct buffer_data **retp;
623 struct buffer_data **lastp;
632 struct buffer_data *data;
635 data = get_buffer_data ();
645 (*lastp)->next = data;
649 data->bufp = data->text;
652 get = BUFFER_DATA_SIZE;
655 nread = fread (data->text, 1, get, f);
656 if (nread == 0 && !feof (f))
670 (*lastp)->next = free_buffer_data;
671 free_buffer_data = *retp;
676 /* Return the number of bytes in a chain of buffer_data structures. */
679 buf_chain_length (buf)
680 struct buffer_data *buf;
691 /* Return the number of bytes in a buffer. */
697 return buf_chain_length (buf->data);
701 * Read an arbitrary amount of data into an input buffer. The buffer
702 * will be in nonblocking mode, and we just grab what we can. Return
703 * 0 on success, or -1 on end of file, or -2 if out of memory, or an
704 * error code. If COUNTP is not NULL, *COUNTP is set to the number of
709 buf_input_data (buf, countp)
713 if (buf->input == NULL)
724 if (buf->data == NULL
725 || (buf->last->bufp + buf->last->size
726 == buf->last->text + BUFFER_DATA_SIZE))
728 struct buffer_data *data;
730 data = get_buffer_data ();
733 (*buf->memory_error) (buf);
737 if (buf->data == NULL)
740 buf->last->next = data;
744 data->bufp = data->text;
748 get = ((buf->last->text + BUFFER_DATA_SIZE)
749 - (buf->last->bufp + buf->last->size));
751 status = (*buf->input) (buf->closure,
752 buf->last->bufp + buf->last->size,
757 buf->last->size += nbytes;
763 /* If we did not fill the buffer, then presumably we read
764 all the available data. */
773 * Read a line (characters up to a \012) from an input buffer. (We
774 * use \012 rather than \n for the benefit of non Unix clients for
775 * which \n means something else). This returns 0 on success, or -1
776 * on end of file, or -2 if out of memory, or an error code. If it
777 * succeeds, it sets *LINE to an allocated buffer holding the contents
778 * of the line. The trailing \012 is not included in the buffer. If
779 * LENP is not NULL, then *LENP is set to the number of bytes read;
780 * strlen may not work, because there may be embedded null bytes.
784 buf_read_line (buf, line, lenp)
789 if (buf->input == NULL)
796 int len, finallen = 0;
797 struct buffer_data *data;
800 /* See if there is a newline in BUF. */
802 for (data = buf->data; data != NULL; data = data->next)
804 nl = memchr (data->bufp, '\012', data->size);
807 finallen = nl - data->bufp;
814 /* If we found a newline, copy the line into a memory buffer,
815 and remove it from BUF. */
819 struct buffer_data *nldata;
821 p = xmalloc (len + 1);
828 while (data != nldata)
830 struct buffer_data *next;
832 memcpy (p, data->bufp, data->size);
835 data->next = free_buffer_data;
836 free_buffer_data = data;
840 memcpy (p, data->bufp, finallen);
843 data->size -= finallen + 1;
853 /* Read more data until we get a newline. */
856 int size, status, nbytes;
859 if (buf->data == NULL
860 || (buf->last->bufp + buf->last->size
861 == buf->last->text + BUFFER_DATA_SIZE))
863 data = get_buffer_data ();
866 (*buf->memory_error) (buf);
870 if (buf->data == NULL)
873 buf->last->next = data;
877 data->bufp = data->text;
881 mem = buf->last->bufp + buf->last->size;
882 size = (buf->last->text + BUFFER_DATA_SIZE) - mem;
884 /* We need to read at least 1 byte. We can handle up to
885 SIZE bytes. This will only be efficient if the
886 underlying communication stream does its own buffering,
887 or is clever about getting more than 1 byte at a time. */
888 status = (*buf->input) (buf->closure, mem, 1, size, &nbytes);
892 buf->last->size += nbytes;
894 /* Optimize slightly to avoid an unnecessary call to
903 if (memchr (mem, '\012', nbytes) != NULL)
911 * Extract data from the input buffer BUF. This will read up to WANT
912 * bytes from the buffer. It will set *RETDATA to point at the bytes,
913 * and set *GOT to the number of bytes to be found there. Any buffer
914 * call which uses BUF may change the contents of the buffer at *DATA,
915 * so the data should be fully processed before any further calls are
916 * made. This returns 0 on success, or -1 on end of file, or -2 if
917 * out of memory, or an error code.
921 buf_read_data (buf, want, retdata, got)
927 if (buf->input == NULL)
930 while (buf->data != NULL && buf->data->size == 0)
932 struct buffer_data *next;
934 next = buf->data->next;
935 buf->data->next = free_buffer_data;
936 free_buffer_data = buf->data;
942 if (buf->data == NULL)
944 struct buffer_data *data;
945 int get, status, nbytes;
947 data = get_buffer_data ();
950 (*buf->memory_error) (buf);
957 data->bufp = data->text;
960 if (want < BUFFER_DATA_SIZE)
963 get = BUFFER_DATA_SIZE;
964 status = (*buf->input) (buf->closure, data->bufp, get,
965 BUFFER_DATA_SIZE, &nbytes);
972 *retdata = buf->data->bufp;
973 if (want < buf->data->size)
976 buf->data->size -= want;
977 buf->data->bufp += want;
981 *got = buf->data->size;
989 * Copy lines from an input buffer to an output buffer. This copies
990 * all complete lines (characters up to a newline) from INBUF to
991 * OUTBUF. Each line in OUTBUF is preceded by the character COMMAND
996 buf_copy_lines (outbuf, inbuf, command)
997 struct buffer *outbuf;
998 struct buffer *inbuf;
1003 struct buffer_data *data;
1004 struct buffer_data *nldata;
1008 /* See if there is a newline in INBUF. */
1011 for (data = inbuf->data; data != NULL; data = data->next)
1013 nl = memchr (data->bufp, '\n', data->size);
1023 /* There are no more lines in INBUF. */
1027 /* Put in the command. */
1028 buf_append_char (outbuf, command);
1029 buf_append_char (outbuf, ' ');
1031 if (inbuf->data != nldata)
1034 * Simply move over all the buffers up to the one containing
1037 for (data = inbuf->data; data->next != nldata; data = data->next)
1040 buf_append_data (outbuf, inbuf->data, data);
1041 inbuf->data = nldata;
1045 * If the newline is at the very end of the buffer, just move
1046 * the buffer onto OUTBUF. Otherwise we must copy the data.
1048 len = nl + 1 - nldata->bufp;
1049 if (len == nldata->size)
1051 inbuf->data = nldata->next;
1052 if (inbuf->data == NULL)
1055 nldata->next = NULL;
1056 buf_append_data (outbuf, nldata, nldata);
1060 buf_output (outbuf, nldata->bufp, len);
1061 nldata->bufp += len;
1062 nldata->size -= len;
1068 * Copy counted data from one buffer to another. The count is an
1069 * integer, host size, host byte order (it is only used across a
1070 * pipe). If there is enough data, it should be moved over. If there
1071 * is not enough data, it should remain on the original buffer. A
1072 * negative count is a special case. if one is seen, *SPECIAL is set
1073 * to the (negative) count value and no additional data is gathered
1074 * from the buffer; normally *SPECIAL is set to 0. This function
1075 * returns the number of bytes it needs to see in order to actually
1076 * copy something over.
1080 buf_copy_counted (outbuf, inbuf, special)
1081 struct buffer *outbuf;
1082 struct buffer *inbuf;
1089 struct buffer_data *data;
1093 char intbuf[sizeof (int)];
1098 struct buffer_data *start;
1100 struct buffer_data *stop;
1103 /* See if we have enough bytes to figure out the count. */
1104 need = sizeof (int);
1106 for (data = inbuf->data; data != NULL; data = data->next)
1108 if (data->size >= need)
1110 memcpy (intp, data->bufp, need);
1113 memcpy (intp, data->bufp, data->size);
1119 /* We don't have enough bytes to form an integer. */
1129 /* A negative COUNT is a special case meaning that we
1130 don't need any further information. */
1137 * We have an integer in COUNT. We have gotten all the
1138 * data from INBUF in all buffers before START, and we
1139 * have gotten STARTOFF bytes from START. See if we have
1140 * enough bytes remaining in INBUF.
1142 need = count - (start->size - startoff);
1150 for (data = start->next; data != NULL; data = data->next)
1152 if (need <= data->size)
1158 /* We don't have enough bytes. */
1167 * We have enough bytes. Free any buffers in INBUF before
1168 * START, and remove STARTOFF bytes from START, so that we can
1169 * forget about STARTOFF.
1171 start->bufp += startoff;
1172 start->size -= startoff;
1174 if (start->size == 0)
1175 start = start->next;
1177 if (stop->size == stopwant)
1183 while (inbuf->data != start)
1186 inbuf->data = data->next;
1187 data->next = free_buffer_data;
1188 free_buffer_data = data;
1191 /* If COUNT is negative, set *SPECIAL and get out now. */
1199 * We want to copy over the bytes from START through STOP. We
1200 * only want STOPWANT bytes from STOP.
1205 /* Attach the buffers from START through STOP to OUTBUF. */
1206 for (data = start; data->next != stop; data = data->next)
1210 buf_append_data (outbuf, start, data);
1215 buf_output (outbuf, stop->bufp, stopwant);
1216 stop->bufp += stopwant;
1217 stop->size -= stopwant;
1224 /* Shut down a buffer. This returns 0 on success, or an errno code. */
1231 return (*buf->shutdown) (buf);
1237 /* The simplest type of buffer is one built on top of a stdio FILE.
1238 For simplicity, and because it is all that is required, we do not
1239 implement setting this type of buffer into nonblocking mode. The
1240 closure field is just a FILE *. */
1242 static int stdio_buffer_input PROTO((void *, char *, int, int, int *));
1243 static int stdio_buffer_output PROTO((void *, const char *, int, int *));
1244 static int stdio_buffer_flush PROTO((void *));
1245 static int stdio_buffer_shutdown PROTO((struct buffer *buf));
1249 /* Initialize a buffer built on a stdio FILE. */
1250 struct stdio_buffer_closure
1259 stdio_buffer_initialize (fp, child_pid, input, memory)
1263 void (*memory) PROTO((struct buffer *));
1265 struct stdio_buffer_closure *bc = xmalloc (sizeof (*bc));
1268 bc->child_pid = child_pid;
1270 return buf_initialize (input ? stdio_buffer_input : NULL,
1271 input ? NULL : stdio_buffer_output,
1272 input ? NULL : stdio_buffer_flush,
1273 (int (*) PROTO((void *, int))) NULL,
1274 stdio_buffer_shutdown,
1279 /* Return the file associated with a stdio buffer. */
1281 stdio_buffer_get_file (buf)
1284 struct stdio_buffer_closure *bc;
1286 assert(buf->shutdown == stdio_buffer_shutdown);
1288 bc = (struct stdio_buffer_closure *) buf->closure;
1293 /* The buffer input function for a buffer built on a stdio FILE. */
1296 stdio_buffer_input (closure, data, need, size, got)
1303 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure;
1306 /* Since stdio does its own buffering, we don't worry about
1307 getting more bytes than we need. */
1309 if (need == 0 || need == 1)
1319 else if (errno == 0)
1330 nbytes = fread (data, 1, need, bc->fp);
1337 else if (errno == 0)
1348 /* The buffer output function for a buffer built on a stdio FILE. */
1351 stdio_buffer_output (closure, data, have, wrote)
1357 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure;
1365 nbytes = fwrite (data, 1, have, bc->fp);
1385 /* The buffer flush function for a buffer built on a stdio FILE. */
1387 stdio_buffer_flush (closure)
1390 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure;
1392 if (fflush (bc->fp) != 0)
1406 stdio_buffer_shutdown (buf)
1409 struct stdio_buffer_closure *bc = buf->closure;
1413 /* Must be a pipe or a socket. What could go wrong? */
1414 assert (fstat (fileno (bc->fp), &s) != -1);
1416 /* Flush the buffer if we can */
1425 /* There used to be a check here for unread data in the buffer of on
1426 * the pipe, but it was deemed unnecessary and possibly dangerous. In
1427 * some sense it could be second-guessing the caller who requested it
1431 # ifdef SHUTDOWN_SERVER
1432 if (current_parsed_root->method != server_method)
1434 # ifndef NO_SOCKET_TO_FD
1436 /* shutdown() sockets */
1437 if (S_ISSOCK (s.st_mode))
1438 shutdown (fileno (bc->fp), 0);
1440 # endif /* NO_SOCKET_TO_FD */
1441 # ifdef START_RSH_WITH_POPEN_RW
1442 /* Can't be set with SHUTDOWN_SERVER defined */
1443 else if (pclose (bc->fp) == EOF)
1445 error (1, errno, "closing connection to %s",
1446 current_parsed_root->hostname);
1449 # endif /* START_RSH_WITH_POPEN_RW */
1453 else if (buf->output)
1455 # ifdef SHUTDOWN_SERVER
1456 /* FIXME: Should have a SHUTDOWN_SERVER_INPUT &
1457 * SHUTDOWN_SERVER_OUTPUT
1459 if (current_parsed_root->method == server_method)
1460 SHUTDOWN_SERVER (fileno (bc->fp));
1463 # ifndef NO_SOCKET_TO_FD
1464 /* shutdown() sockets */
1465 if (S_ISSOCK (s.st_mode))
1466 shutdown (fileno (bc->fp), 1);
1469 /* I'm not sure I like this empty block, but the alternative
1470 * is a another nested NO_SOCKET_TO_FD switch above.
1473 # endif /* NO_SOCKET_TO_FD */
1478 if (closefp && fclose (bc->fp) == EOF)
1481 # ifdef SERVER_SUPPORT
1483 # endif /* SERVER_SUPPORT */
1488 # ifdef CLIENT_SUPPORT
1491 "closing down connection to %s",
1492 current_parsed_root->hostname);
1493 # endif /* CLIENT_SUPPORT */
1496 /* If we were talking to a process, make sure it exited */
1502 w = waitpid (bc->child_pid, (int *) 0, 0);
1503 while (w == -1 && errno == EINTR);
1505 error (1, errno, "waiting for process %d", bc->child_pid);
1512 /* Certain types of communication input and output data in packets,
1513 where each packet is translated in some fashion. The packetizing
1514 buffer type supports that, given a buffer which handles lower level
1515 I/O and a routine to translate the data in a packet.
1517 This code uses two bytes for the size of a packet, so packets are
1518 restricted to 65536 bytes in total.
1520 The translation functions should just translate; they may not
1521 significantly increase or decrease the amount of data. The actual
1522 size of the initial data is part of the translated data. The
1523 output translation routine may add up to PACKET_SLOP additional
1524 bytes, and the input translation routine should shrink the data
1527 #define PACKET_SLOP (100)
1529 /* This structure is the closure field of a packetizing buffer. */
1531 struct packetizing_buffer
1533 /* The underlying buffer. */
1535 /* The input translation function. Exactly one of inpfn and outfn
1536 will be NULL. The input translation function should
1537 untranslate the data in INPUT, storing the result in OUTPUT.
1538 SIZE is the amount of data in INPUT, and is also the size of
1539 OUTPUT. This should return 0 on success, or an errno code. */
1540 int (*inpfn) PROTO((void *fnclosure, const char *input, char *output,
1542 /* The output translation function. This should translate the
1543 data in INPUT, storing the result in OUTPUT. The first two
1544 bytes in INPUT will be the size of the data, and so will SIZE.
1545 This should set *TRANSLATED to the amount of translated data in
1546 OUTPUT. OUTPUT is large enough to hold SIZE + PACKET_SLOP
1547 bytes. This should return 0 on success, or an errno code. */
1548 int (*outfn) PROTO((void *fnclosure, const char *input, char *output,
1549 int size, int *translated));
1550 /* A closure for the translation function. */
1552 /* For an input buffer, we may have to buffer up data here. */
1553 /* This is non-zero if the buffered data has been translated.
1554 Otherwise, the buffered data has not been translated, and starts
1555 with the two byte packet size. */
1557 /* The amount of buffered data. */
1559 /* The buffer allocated to hold the data. */
1561 /* The size of holdbuf. */
1563 /* If translated is set, we need another data pointer to track
1564 where we are in holdbuf. If translated is clear, then this
1565 pointer is not used. */
1569 static int packetizing_buffer_input PROTO((void *, char *, int, int, int *));
1570 static int packetizing_buffer_output PROTO((void *, const char *, int, int *));
1571 static int packetizing_buffer_flush PROTO((void *));
1572 static int packetizing_buffer_block PROTO((void *, int));
1573 static int packetizing_buffer_shutdown PROTO((struct buffer *));
1575 /* Create a packetizing buffer. */
1578 packetizing_buffer_initialize (buf, inpfn, outfn, fnclosure, memory)
1580 int (*inpfn) PROTO ((void *, const char *, char *, int));
1581 int (*outfn) PROTO ((void *, const char *, char *, int, int *));
1583 void (*memory) PROTO((struct buffer *));
1585 struct packetizing_buffer *pb;
1587 pb = (struct packetizing_buffer *) xmalloc (sizeof *pb);
1588 memset (pb, 0, sizeof *pb);
1593 pb->fnclosure = fnclosure;
1597 /* Add PACKET_SLOP to handle larger translated packets, and
1598 add 2 for the count. This buffer is increased if
1600 pb->holdbufsize = BUFFER_DATA_SIZE + PACKET_SLOP + 2;
1601 pb->holdbuf = xmalloc (pb->holdbufsize);
1604 return buf_initialize (inpfn != NULL ? packetizing_buffer_input : NULL,
1605 inpfn != NULL ? NULL : packetizing_buffer_output,
1606 inpfn != NULL ? NULL : packetizing_buffer_flush,
1607 packetizing_buffer_block,
1608 packetizing_buffer_shutdown,
1613 /* Input data from a packetizing buffer. */
1616 packetizing_buffer_input (closure, data, need, size, got)
1623 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1627 if (pb->holdsize > 0 && pb->translated)
1631 copy = pb->holdsize;
1635 memcpy (data, pb->holddata, size);
1636 pb->holdsize -= size;
1637 pb->holddata += size;
1642 memcpy (data, pb->holddata, copy);
1652 while (need > 0 || *got == 0)
1654 int get, status, nread, count, tcount;
1656 char stackoutbuf[BUFFER_DATA_SIZE + PACKET_SLOP];
1657 char *inbuf, *outbuf;
1659 /* If we don't already have the two byte count, get it. */
1660 if (pb->holdsize < 2)
1662 get = 2 - pb->holdsize;
1663 status = buf_read_data (pb->buf, get, &bytes, &nread);
1666 /* buf_read_data can return -2, but a buffer input
1667 function is only supposed to return -1, 0, or an
1676 /* The buffer is in nonblocking mode, and we didn't
1677 manage to read anything. */
1682 pb->holdbuf[1] = bytes[0];
1685 pb->holdbuf[0] = bytes[0];
1688 /* We only got one byte, but we needed two. Stash
1689 the byte we got, and try again. */
1693 pb->holdbuf[1] = bytes[1];
1698 /* Read the packet. */
1700 count = (((pb->holdbuf[0] & 0xff) << 8)
1701 + (pb->holdbuf[1] & 0xff));
1703 if (count + 2 > pb->holdbufsize)
1707 /* We didn't allocate enough space in the initialize
1710 n = xrealloc (pb->holdbuf, count + 2);
1713 (*pb->buf->memory_error) (pb->buf);
1717 pb->holdbufsize = count + 2;
1720 get = count - (pb->holdsize - 2);
1722 status = buf_read_data (pb->buf, get, &bytes, &nread);
1725 /* buf_read_data can return -2, but a buffer input
1726 function is only supposed to return -1, 0, or an error
1735 /* We did not get any data. Presumably the buffer is in
1736 nonblocking mode. */
1742 /* We did not get all the data we need to fill the packet.
1743 buf_read_data does not promise to return all the bytes
1744 requested, so we must try again. */
1745 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1746 pb->holdsize += nread;
1750 /* We have a complete untranslated packet of COUNT bytes. */
1752 if (pb->holdsize == 2)
1754 /* We just read the entire packet (the 2 bytes in
1755 PB->HOLDBUF are the size). Save a memcpy by
1756 translating directly from BYTES. */
1761 /* We already had a partial packet in PB->HOLDBUF. We
1762 need to copy the new data over to make the input
1764 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1765 inbuf = pb->holdbuf + 2;
1768 if (count <= sizeof stackoutbuf)
1769 outbuf = stackoutbuf;
1772 outbuf = xmalloc (count);
1775 (*pb->buf->memory_error) (pb->buf);
1780 status = (*pb->inpfn) (pb->fnclosure, inbuf, outbuf, count);
1784 /* The first two bytes in the translated buffer are the real
1785 length of the translated data. */
1786 tcount = ((outbuf[0] & 0xff) << 8) + (outbuf[1] & 0xff);
1789 error (1, 0, "Input translation failure");
1793 /* We have more data than the caller has provided space
1794 for. We need to save some of it for the next call. */
1796 memcpy (data, outbuf + 2, size);
1799 pb->holdsize = tcount - size;
1800 memcpy (pb->holdbuf, outbuf + 2 + size, tcount - size);
1801 pb->holddata = pb->holdbuf;
1804 if (outbuf != stackoutbuf)
1810 memcpy (data, outbuf + 2, tcount);
1812 if (outbuf != stackoutbuf)
1826 /* Output data to a packetizing buffer. */
1829 packetizing_buffer_output (closure, data, have, wrote)
1835 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1836 char inbuf[BUFFER_DATA_SIZE + 2];
1837 char stack_outbuf[BUFFER_DATA_SIZE + PACKET_SLOP + 4];
1838 struct buffer_data *outdata;
1840 int size, status, translated;
1842 if (have > BUFFER_DATA_SIZE)
1844 /* It would be easy to xmalloc a buffer, but I don't think this
1845 case can ever arise. */
1849 inbuf[0] = (have >> 8) & 0xff;
1850 inbuf[1] = have & 0xff;
1851 memcpy (inbuf + 2, data, have);
1855 /* The output function is permitted to add up to PACKET_SLOP
1856 bytes, and we need 2 bytes for the size of the translated data.
1857 If we can guarantee that the result will fit in a buffer_data,
1858 we translate directly into one to avoid a memcpy in buf_output. */
1859 if (size + PACKET_SLOP + 2 > BUFFER_DATA_SIZE)
1860 outbuf = stack_outbuf;
1863 outdata = get_buffer_data ();
1864 if (outdata == NULL)
1866 (*pb->buf->memory_error) (pb->buf);
1870 outdata->next = NULL;
1871 outdata->bufp = outdata->text;
1873 outbuf = outdata->text;
1876 status = (*pb->outfn) (pb->fnclosure, inbuf, outbuf + 2, size,
1881 /* The output function is permitted to add up to PACKET_SLOP
1883 if (translated > size + PACKET_SLOP)
1886 outbuf[0] = (translated >> 8) & 0xff;
1887 outbuf[1] = translated & 0xff;
1889 if (outbuf == stack_outbuf)
1890 buf_output (pb->buf, outbuf, translated + 2);
1893 outdata->size = translated + 2;
1894 buf_append_data (pb->buf, outdata, outdata);
1899 /* We will only be here because buf_send_output was called on the
1900 packetizing buffer. That means that we should now call
1901 buf_send_output on the underlying buffer. */
1902 return buf_send_output (pb->buf);
1907 /* Flush data to a packetizing buffer. */
1909 packetizing_buffer_flush (closure)
1912 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1914 /* Flush the underlying buffer. Note that if the original call to
1915 buf_flush passed 1 for the BLOCK argument, then the buffer will
1916 already have been set into blocking mode, so we should always
1918 return buf_flush (pb->buf, 0);
1923 /* The block routine for a packetizing buffer. */
1925 packetizing_buffer_block (closure, block)
1929 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1932 return set_block (pb->buf);
1934 return set_nonblock (pb->buf);
1937 /* Shut down a packetizing buffer. */
1940 packetizing_buffer_shutdown (buf)
1943 struct packetizing_buffer *pb = (struct packetizing_buffer *) buf->closure;
1945 return buf_shutdown (pb->buf);
1948 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */