1 /* Code for the buffer data structure. */
6 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
11 # include <sys/socket.h>
14 /* OS/2 doesn't have EIO. FIXME: this whole notion of turning
15 a different error into EIO strikes me as pretty dubious. */
20 /* Linked list of available buffer_data structures. */
21 static struct buffer_data *free_buffer_data;
23 /* Local functions. */
24 static void buf_default_memory_error (struct buffer *);
25 static void allocate_buffer_datas (void);
26 static struct buffer_data *get_buffer_data (void);
28 /* Initialize a buffer structure. */
31 buf_initialize (int (*input) (void *, char *, int, int, int *),
32 int (*output) (void *, const char *, int, int *),
33 int (*flush) (void *),
34 int (*block) (void *, int),
35 int (*shutdown) (struct buffer *),
36 void (*memory) (struct buffer *),
41 buf = (struct buffer *) xmalloc (sizeof (struct buffer));
49 buf->shutdown = shutdown;
50 buf->memory_error = memory ? memory : buf_default_memory_error;
51 buf->closure = closure;
55 /* Free a buffer structure. */
58 buf_free (struct buffer *buf)
60 if (buf->closure != NULL)
65 if (buf->data != NULL)
67 buf->last->next = free_buffer_data;
68 free_buffer_data = buf->data;
73 /* Initialize a buffer structure which is not to be used for I/O. */
76 buf_nonio_initialize( void (*memory) (struct buffer *) )
78 return (buf_initialize
79 ((int (*) (void *, char *, int, int, int *)) NULL,
80 (int (*) (void *, const char *, int, int *)) NULL,
81 (int (*) (void *)) NULL,
82 (int (*) (void *, int)) NULL,
83 (int (*) (struct buffer *)) NULL,
88 /* Default memory error handler. */
91 buf_default_memory_error (struct buffer *buf)
93 error (1, 0, "out of memory");
96 /* Allocate more buffer_data structures. */
99 allocate_buffer_datas (void)
101 struct buffer_data *alc;
105 /* Allocate buffer_data structures in blocks of 16. */
106 # define ALLOC_COUNT (16)
108 alc = ((struct buffer_data *)
109 xmalloc (ALLOC_COUNT * sizeof (struct buffer_data)));
110 space = (char *) valloc (ALLOC_COUNT * BUFFER_DATA_SIZE);
111 if (alc == NULL || space == NULL)
113 for (i = 0; i < ALLOC_COUNT; i++, alc++, space += BUFFER_DATA_SIZE)
115 alc->next = free_buffer_data;
116 free_buffer_data = alc;
121 /* Get a new buffer_data structure. */
123 static struct buffer_data *
124 get_buffer_data (void)
126 struct buffer_data *ret;
128 if (free_buffer_data == NULL)
130 allocate_buffer_datas ();
131 if (free_buffer_data == NULL)
135 ret = free_buffer_data;
136 free_buffer_data = ret->next;
142 /* See whether a buffer and its file descriptor is empty. */
147 /* Try and read any data on the file descriptor first.
148 * We already know the descriptor is non-blocking.
150 buf_input_data (buf, NULL);
151 return buf_empty_p (buf);
156 /* See whether a buffer is empty. */
158 buf_empty_p (struct buffer *buf)
160 struct buffer_data *data;
162 for (data = buf->data; data != NULL; data = data->next)
170 # ifdef SERVER_FLOWCONTROL
172 * Count how much data is stored in the buffer..
173 * Note that each buffer is a xmalloc'ed chunk BUFFER_DATA_SIZE.
177 buf_count_mem (struct buffer *buf)
179 struct buffer_data *data;
182 for (data = buf->data; data != NULL; data = data->next)
183 mem += BUFFER_DATA_SIZE;
187 # endif /* SERVER_FLOWCONTROL */
189 /* Add data DATA of length LEN to BUF. */
192 buf_output (struct buffer *buf, const char *data, int len)
194 if (buf->data != NULL
195 && (((buf->last->text + BUFFER_DATA_SIZE)
196 - (buf->last->bufp + buf->last->size))
199 memcpy (buf->last->bufp + buf->last->size, data, len);
200 buf->last->size += len;
206 struct buffer_data *newdata;
208 newdata = get_buffer_data ();
211 (*buf->memory_error) (buf);
215 if (buf->data == NULL)
218 buf->last->next = newdata;
219 newdata->next = NULL;
222 newdata->bufp = newdata->text;
224 if (len <= BUFFER_DATA_SIZE)
227 memcpy (newdata->text, data, len);
231 newdata->size = BUFFER_DATA_SIZE;
232 memcpy (newdata->text, data, BUFFER_DATA_SIZE);
234 data += BUFFER_DATA_SIZE;
235 len -= BUFFER_DATA_SIZE;
241 /* Add a '\0' terminated string to BUF. */
244 buf_output0 (struct buffer *buf, const char *string)
246 buf_output (buf, string, strlen (string));
249 /* Add a single character to BUF. */
252 buf_append_char (struct buffer *buf, int ch)
254 if (buf->data != NULL
255 && (buf->last->text + BUFFER_DATA_SIZE
256 != buf->last->bufp + buf->last->size))
258 *(buf->last->bufp + buf->last->size) = ch;
266 buf_output (buf, &b, 1);
271 * Send all the output we've been saving up. Returns 0 for success or
272 * errno code. If the buffer has been set to be nonblocking, this
273 * will just write until the write would block.
277 buf_send_output (struct buffer *buf)
279 if (buf->output == NULL)
282 while (buf->data != NULL)
284 struct buffer_data *data;
292 status = (*buf->output) (buf->closure, data->bufp, data->size,
296 /* Some sort of error. Discard the data, and return. */
298 buf->last->next = free_buffer_data;
299 free_buffer_data = buf->data;
306 if (nbytes != data->size)
308 /* Not all the data was written out. This is only
309 permitted in nonblocking mode. Adjust the buffer,
312 assert (buf->nonblocking);
314 data->size -= nbytes;
315 data->bufp += nbytes;
321 buf->data = data->next;
322 data->next = free_buffer_data;
323 free_buffer_data = data;
332 * Flush any data queued up in the buffer. If BLOCK is nonzero, then
333 * if the buffer is in nonblocking mode, put it into blocking mode for
334 * the duration of the flush. This returns 0 on success, or an error
339 buf_flush (struct buffer *buf, int block)
344 if (buf->flush == NULL)
347 nonblocking = buf->nonblocking;
348 if (nonblocking && block)
350 status = set_block (buf);
355 status = buf_send_output (buf);
357 status = (*buf->flush) (buf->closure);
359 if (nonblocking && block)
363 blockstat = set_nonblock (buf);
372 * Set buffer BUF to nonblocking I/O. Returns 0 for success or errno
377 set_nonblock (struct buffer *buf)
381 if (buf->nonblocking)
383 if (buf->block == NULL)
385 status = (*buf->block) (buf->closure, 0);
388 buf->nonblocking = 1;
393 * Set buffer BUF to blocking I/O. Returns 0 for success or errno
398 set_block (struct buffer *buf)
402 if (! buf->nonblocking)
404 if (buf->block == NULL)
406 status = (*buf->block) (buf->closure, 1);
409 buf->nonblocking = 0;
414 * Send a character count and some output. Returns errno code or 0 for
417 * Sending the count in binary is OK since this is only used on a pipe
418 * within the same system.
422 buf_send_counted (struct buffer *buf)
425 struct buffer_data *data;
428 for (data = buf->data; data != NULL; data = data->next)
431 data = get_buffer_data ();
434 (*buf->memory_error) (buf);
438 data->next = buf->data;
440 if (buf->last == NULL)
443 data->bufp = data->text;
444 data->size = sizeof (int);
446 *((int *) data->text) = size;
448 return buf_send_output (buf);
452 * Send a special count. COUNT should be negative. It will be
453 * handled speciallyi by buf_copy_counted. This function returns 0 or
456 * Sending the count in binary is OK since this is only used on a pipe
457 * within the same system.
461 buf_send_special_count (struct buffer *buf, int count)
463 struct buffer_data *data;
465 data = get_buffer_data ();
468 (*buf->memory_error) (buf);
472 data->next = buf->data;
474 if (buf->last == NULL)
477 data->bufp = data->text;
478 data->size = sizeof (int);
480 *((int *) data->text) = count;
482 return buf_send_output (buf);
485 /* Append a list of buffer_data structures to an buffer. */
488 buf_append_data (struct buffer *buf, struct buffer_data *data,
489 struct buffer_data *last)
493 if (buf->data == NULL)
496 buf->last->next = data;
501 /* Append the data on one buffer to another. This removes the data
502 from the source buffer. */
505 buf_append_buffer (struct buffer *to, struct buffer *from)
507 buf_append_data (to, from->data, from->last);
513 * Copy the contents of file F into buffer_data structures. We can't
514 * copy directly into an buffer, because we want to handle failure and
515 * succeess differently. Returns 0 on success, or -2 if out of
516 * memory, or a status code on error. Since the caller happens to
517 * know the size of the file, it is passed in as SIZE. On success,
518 * this function sets *RETP and *LASTP, which may be passed to
523 buf_read_file (FILE *f, long int size, struct buffer_data **retp,
524 struct buffer_data **lastp)
533 struct buffer_data *data;
536 data = get_buffer_data ();
546 (*lastp)->next = data;
550 data->bufp = data->text;
553 if (size > BUFFER_DATA_SIZE)
554 get = BUFFER_DATA_SIZE;
559 if (fread (data->text, get, 1, f) != 1)
574 (*lastp)->next = free_buffer_data;
575 free_buffer_data = *retp;
581 * Copy the contents of file F into buffer_data structures. We can't
582 * copy directly into an buffer, because we want to handle failure and
583 * succeess differently. Returns 0 on success, or -2 if out of
584 * memory, or a status code on error. On success, this function sets
585 * *RETP and *LASTP, which may be passed to buf_append_data.
589 buf_read_file_to_eof (FILE *f, struct buffer_data **retp,
590 struct buffer_data **lastp)
599 struct buffer_data *data;
602 data = get_buffer_data ();
612 (*lastp)->next = data;
616 data->bufp = data->text;
619 get = BUFFER_DATA_SIZE;
622 nread = fread (data->text, 1, get, f);
623 if (nread == 0 && !feof (f))
637 (*lastp)->next = free_buffer_data;
638 free_buffer_data = *retp;
643 /* Return the number of bytes in a chain of buffer_data structures. */
646 buf_chain_length (struct buffer_data *buf)
657 /* Return the number of bytes in a buffer. */
660 buf_length (struct buffer *buf)
662 return buf_chain_length (buf->data);
666 * Read an arbitrary amount of data into an input buffer. The buffer
667 * will be in nonblocking mode, and we just grab what we can. Return
668 * 0 on success, or -1 on end of file, or -2 if out of memory, or an
669 * error code. If COUNTP is not NULL, *COUNTP is set to the number of
674 buf_input_data (struct buffer *buf, int *countp)
676 if (buf->input == NULL)
687 if (buf->data == NULL
688 || (buf->last->bufp + buf->last->size
689 == buf->last->text + BUFFER_DATA_SIZE))
691 struct buffer_data *data;
693 data = get_buffer_data ();
696 (*buf->memory_error) (buf);
700 if (buf->data == NULL)
703 buf->last->next = data;
707 data->bufp = data->text;
711 get = ((buf->last->text + BUFFER_DATA_SIZE)
712 - (buf->last->bufp + buf->last->size));
714 status = (*buf->input) (buf->closure,
715 buf->last->bufp + buf->last->size,
720 buf->last->size += nbytes;
726 /* If we did not fill the buffer, then presumably we read
727 all the available data. */
736 * Read a line (characters up to a \012) from an input buffer. (We
737 * use \012 rather than \n for the benefit of non Unix clients for
738 * which \n means something else). This returns 0 on success, or -1
739 * on end of file, or -2 if out of memory, or an error code. If it
740 * succeeds, it sets *LINE to an allocated buffer holding the contents
741 * of the line. The trailing \012 is not included in the buffer. If
742 * LENP is not NULL, then *LENP is set to the number of bytes read;
743 * strlen may not work, because there may be embedded null bytes.
747 buf_read_line (struct buffer *buf, char **line, int *lenp)
749 if (buf->input == NULL)
756 int len, finallen = 0;
757 struct buffer_data *data;
760 /* See if there is a newline in BUF. */
762 for (data = buf->data; data != NULL; data = data->next)
764 nl = memchr (data->bufp, '\012', data->size);
767 finallen = nl - data->bufp;
774 /* If we found a newline, copy the line into a memory buffer,
775 and remove it from BUF. */
779 struct buffer_data *nldata;
781 p = xmalloc (len + 1);
788 while (data != nldata)
790 struct buffer_data *next;
792 memcpy (p, data->bufp, data->size);
795 data->next = free_buffer_data;
796 free_buffer_data = data;
800 memcpy (p, data->bufp, finallen);
803 data->size -= finallen + 1;
813 /* Read more data until we get a newline. */
816 int size, status, nbytes;
819 if (buf->data == NULL
820 || (buf->last->bufp + buf->last->size
821 == buf->last->text + BUFFER_DATA_SIZE))
823 data = get_buffer_data ();
826 (*buf->memory_error) (buf);
830 if (buf->data == NULL)
833 buf->last->next = data;
837 data->bufp = data->text;
841 mem = buf->last->bufp + buf->last->size;
842 size = (buf->last->text + BUFFER_DATA_SIZE) - mem;
844 /* We need to read at least 1 byte. We can handle up to
845 SIZE bytes. This will only be efficient if the
846 underlying communication stream does its own buffering,
847 or is clever about getting more than 1 byte at a time. */
848 status = (*buf->input) (buf->closure, mem, 1, size, &nbytes);
852 buf->last->size += nbytes;
854 /* Optimize slightly to avoid an unnecessary call to
863 if (memchr (mem, '\012', nbytes) != NULL)
871 * Extract data from the input buffer BUF. This will read up to WANT
872 * bytes from the buffer. It will set *RETDATA to point at the bytes,
873 * and set *GOT to the number of bytes to be found there. Any buffer
874 * call which uses BUF may change the contents of the buffer at *DATA,
875 * so the data should be fully processed before any further calls are
876 * made. This returns 0 on success, or -1 on end of file, or -2 if
877 * out of memory, or an error code.
881 buf_read_data (struct buffer *buf, int want, char **retdata, int *got)
883 if (buf->input == NULL)
886 while (buf->data != NULL && buf->data->size == 0)
888 struct buffer_data *next;
890 next = buf->data->next;
891 buf->data->next = free_buffer_data;
892 free_buffer_data = buf->data;
898 if (buf->data == NULL)
900 struct buffer_data *data;
901 int get, status, nbytes;
903 data = get_buffer_data ();
906 (*buf->memory_error) (buf);
913 data->bufp = data->text;
916 if (want < BUFFER_DATA_SIZE)
919 get = BUFFER_DATA_SIZE;
920 status = (*buf->input) (buf->closure, data->bufp, get,
921 BUFFER_DATA_SIZE, &nbytes);
928 *retdata = buf->data->bufp;
929 if (want < buf->data->size)
932 buf->data->size -= want;
933 buf->data->bufp += want;
937 *got = buf->data->size;
945 * Copy lines from an input buffer to an output buffer. This copies
946 * all complete lines (characters up to a newline) from INBUF to
947 * OUTBUF. Each line in OUTBUF is preceded by the character COMMAND
952 buf_copy_lines (struct buffer *outbuf, struct buffer *inbuf, int command)
956 struct buffer_data *data;
957 struct buffer_data *nldata;
961 /* See if there is a newline in INBUF. */
964 for (data = inbuf->data; data != NULL; data = data->next)
966 nl = memchr (data->bufp, '\n', data->size);
976 /* There are no more lines in INBUF. */
980 /* Put in the command. */
981 buf_append_char (outbuf, command);
982 buf_append_char (outbuf, ' ');
984 if (inbuf->data != nldata)
987 * Simply move over all the buffers up to the one containing
990 for (data = inbuf->data; data->next != nldata; data = data->next)
993 buf_append_data (outbuf, inbuf->data, data);
994 inbuf->data = nldata;
998 * If the newline is at the very end of the buffer, just move
999 * the buffer onto OUTBUF. Otherwise we must copy the data.
1001 len = nl + 1 - nldata->bufp;
1002 if (len == nldata->size)
1004 inbuf->data = nldata->next;
1005 if (inbuf->data == NULL)
1008 nldata->next = NULL;
1009 buf_append_data (outbuf, nldata, nldata);
1013 buf_output (outbuf, nldata->bufp, len);
1014 nldata->bufp += len;
1015 nldata->size -= len;
1021 * Copy counted data from one buffer to another. The count is an
1022 * integer, host size, host byte order (it is only used across a
1023 * pipe). If there is enough data, it should be moved over. If there
1024 * is not enough data, it should remain on the original buffer. A
1025 * negative count is a special case. if one is seen, *SPECIAL is set
1026 * to the (negative) count value and no additional data is gathered
1027 * from the buffer; normally *SPECIAL is set to 0. This function
1028 * returns the number of bytes it needs to see in order to actually
1029 * copy something over.
1033 buf_copy_counted (struct buffer *outbuf, struct buffer *inbuf, int *special)
1039 struct buffer_data *data;
1043 char intbuf[sizeof (int)];
1048 struct buffer_data *start;
1050 struct buffer_data *stop;
1053 /* See if we have enough bytes to figure out the count. */
1054 need = sizeof (int);
1056 for (data = inbuf->data; data != NULL; data = data->next)
1058 if (data->size >= need)
1060 memcpy (intp, data->bufp, need);
1063 memcpy (intp, data->bufp, data->size);
1069 /* We don't have enough bytes to form an integer. */
1079 /* A negative COUNT is a special case meaning that we
1080 don't need any further information. */
1087 * We have an integer in COUNT. We have gotten all the
1088 * data from INBUF in all buffers before START, and we
1089 * have gotten STARTOFF bytes from START. See if we have
1090 * enough bytes remaining in INBUF.
1092 need = count - (start->size - startoff);
1100 for (data = start->next; data != NULL; data = data->next)
1102 if (need <= data->size)
1108 /* We don't have enough bytes. */
1117 * We have enough bytes. Free any buffers in INBUF before
1118 * START, and remove STARTOFF bytes from START, so that we can
1119 * forget about STARTOFF.
1121 start->bufp += startoff;
1122 start->size -= startoff;
1124 if (start->size == 0)
1125 start = start->next;
1127 if (stop->size == stopwant)
1133 while (inbuf->data != start)
1136 inbuf->data = data->next;
1137 data->next = free_buffer_data;
1138 free_buffer_data = data;
1141 /* If COUNT is negative, set *SPECIAL and get out now. */
1149 * We want to copy over the bytes from START through STOP. We
1150 * only want STOPWANT bytes from STOP.
1155 /* Attach the buffers from START through STOP to OUTBUF. */
1156 for (data = start; data->next != stop; data = data->next)
1160 buf_append_data (outbuf, start, data);
1165 buf_output (outbuf, stop->bufp, stopwant);
1166 stop->bufp += stopwant;
1167 stop->size -= stopwant;
1174 /* Shut down a buffer. This returns 0 on success, or an errno code. */
1177 buf_shutdown (struct buffer *buf)
1180 return (*buf->shutdown) (buf);
1186 /* The simplest type of buffer is one built on top of a stdio FILE.
1187 For simplicity, and because it is all that is required, we do not
1188 implement setting this type of buffer into nonblocking mode. The
1189 closure field is just a FILE *. */
1191 static int stdio_buffer_input (void *, char *, int, int, int *);
1192 static int stdio_buffer_output (void *, const char *, int, int *);
1193 static int stdio_buffer_flush (void *);
1194 static int stdio_buffer_shutdown (struct buffer *buf);
1198 /* Initialize a buffer built on a stdio FILE. */
1199 struct stdio_buffer_closure
1208 stdio_buffer_initialize (FILE *fp, int child_pid, int input,
1209 void (*memory) (struct buffer *))
1211 struct stdio_buffer_closure *bc = xmalloc (sizeof (*bc));
1214 bc->child_pid = child_pid;
1216 return buf_initialize (input ? stdio_buffer_input : NULL,
1217 input ? NULL : stdio_buffer_output,
1218 input ? NULL : stdio_buffer_flush,
1219 NULL, stdio_buffer_shutdown, memory,
1225 /* Return the file associated with a stdio buffer. */
1227 stdio_buffer_get_file (struct buffer *buf)
1229 struct stdio_buffer_closure *bc;
1231 assert (buf->shutdown == stdio_buffer_shutdown);
1240 /* The buffer input function for a buffer built on a stdio FILE. */
1242 stdio_buffer_input (void *closure, char *data, int need, int size, int *got)
1244 struct stdio_buffer_closure *bc = closure;
1247 /* Since stdio does its own buffering, we don't worry about
1248 getting more bytes than we need. */
1250 if (need == 0 || need == 1)
1260 else if (errno == 0)
1271 nbytes = fread (data, 1, need, bc->fp);
1278 else if (errno == 0)
1291 /* The buffer output function for a buffer built on a stdio FILE. */
1293 stdio_buffer_output (void *closure, const char *data, int have, int *wrote)
1295 struct stdio_buffer_closure *bc = closure;
1303 nbytes = fwrite (data, 1, have, bc->fp);
1323 /* The buffer flush function for a buffer built on a stdio FILE. */
1325 stdio_buffer_flush (void *closure)
1327 struct stdio_buffer_closure *bc = (struct stdio_buffer_closure *) closure;
1329 if (fflush (bc->fp) != 0)
1343 stdio_buffer_shutdown (struct buffer *buf)
1345 struct stdio_buffer_closure *bc = buf->closure;
1349 /* Must be a pipe or a socket. What could go wrong? */
1350 assert (fstat (fileno (bc->fp), &s) != -1);
1352 /* Flush the buffer if we can */
1361 /* There used to be a check here for unread data in the buffer of on
1362 * the pipe, but it was deemed unnecessary and possibly dangerous. In
1363 * some sense it could be second-guessing the caller who requested it
1367 # ifdef SHUTDOWN_SERVER
1368 if (current_parsed_root->method != server_method)
1370 # ifndef NO_SOCKET_TO_FD
1372 /* shutdown() sockets */
1373 if (S_ISSOCK (s.st_mode))
1374 shutdown (fileno (bc->fp), 0);
1376 # endif /* NO_SOCKET_TO_FD */
1377 # ifdef START_RSH_WITH_POPEN_RW
1378 /* Can't be set with SHUTDOWN_SERVER defined */
1379 else if (pclose (bc->fp) == EOF)
1381 error (1, errno, "closing connection to %s",
1382 current_parsed_root->hostname);
1385 # endif /* START_RSH_WITH_POPEN_RW */
1389 else if (buf->output)
1391 # ifdef SHUTDOWN_SERVER
1392 /* FIXME: Should have a SHUTDOWN_SERVER_INPUT &
1393 * SHUTDOWN_SERVER_OUTPUT
1395 if (current_parsed_root->method == server_method)
1396 SHUTDOWN_SERVER (fileno (bc->fp));
1399 # ifndef NO_SOCKET_TO_FD
1400 /* shutdown() sockets */
1401 if (S_ISSOCK (s.st_mode))
1402 shutdown (fileno (bc->fp), 1);
1405 /* I'm not sure I like this empty block, but the alternative
1406 * is a another nested NO_SOCKET_TO_FD switch above.
1409 # endif /* NO_SOCKET_TO_FD */
1414 if (closefp && fclose (bc->fp) == EOF)
1417 # ifdef SERVER_SUPPORT
1419 # endif /* SERVER_SUPPORT */
1424 # ifdef CLIENT_SUPPORT
1427 "closing down connection to %s",
1428 current_parsed_root->hostname);
1429 # endif /* CLIENT_SUPPORT */
1432 /* If we were talking to a process, make sure it exited */
1438 w = waitpid (bc->child_pid, (int *) 0, 0);
1439 while (w == -1 && errno == EINTR);
1441 error (1, errno, "waiting for process %d", bc->child_pid);
1448 /* Certain types of communication input and output data in packets,
1449 where each packet is translated in some fashion. The packetizing
1450 buffer type supports that, given a buffer which handles lower level
1451 I/O and a routine to translate the data in a packet.
1453 This code uses two bytes for the size of a packet, so packets are
1454 restricted to 65536 bytes in total.
1456 The translation functions should just translate; they may not
1457 significantly increase or decrease the amount of data. The actual
1458 size of the initial data is part of the translated data. The
1459 output translation routine may add up to PACKET_SLOP additional
1460 bytes, and the input translation routine should shrink the data
1463 # define PACKET_SLOP (100)
1465 /* This structure is the closure field of a packetizing buffer. */
1467 struct packetizing_buffer
1469 /* The underlying buffer. */
1471 /* The input translation function. Exactly one of inpfn and outfn
1472 will be NULL. The input translation function should
1473 untranslate the data in INPUT, storing the result in OUTPUT.
1474 SIZE is the amount of data in INPUT, and is also the size of
1475 OUTPUT. This should return 0 on success, or an errno code. */
1476 int (*inpfn) (void *fnclosure, const char *input, char *output,
1478 /* The output translation function. This should translate the
1479 data in INPUT, storing the result in OUTPUT. The first two
1480 bytes in INPUT will be the size of the data, and so will SIZE.
1481 This should set *TRANSLATED to the amount of translated data in
1482 OUTPUT. OUTPUT is large enough to hold SIZE + PACKET_SLOP
1483 bytes. This should return 0 on success, or an errno code. */
1484 int (*outfn) (void *fnclosure, const char *input, char *output,
1485 int size, int *translated);
1486 /* A closure for the translation function. */
1488 /* For an input buffer, we may have to buffer up data here. */
1489 /* This is non-zero if the buffered data has been translated.
1490 Otherwise, the buffered data has not been translated, and starts
1491 with the two byte packet size. */
1493 /* The amount of buffered data. */
1495 /* The buffer allocated to hold the data. */
1497 /* The size of holdbuf. */
1499 /* If translated is set, we need another data pointer to track
1500 where we are in holdbuf. If translated is clear, then this
1501 pointer is not used. */
1507 static int packetizing_buffer_input (void *, char *, int, int, int *);
1508 static int packetizing_buffer_output (void *, const char *, int, int *);
1509 static int packetizing_buffer_flush (void *);
1510 static int packetizing_buffer_block (void *, int);
1511 static int packetizing_buffer_shutdown (struct buffer *);
1515 /* Create a packetizing buffer. */
1517 packetizing_buffer_initialize (struct buffer *buf,
1518 int (*inpfn) (void *, const char *, char *,
1520 int (*outfn) (void *, const char *, char *,
1523 void (*memory) (struct buffer *))
1525 struct packetizing_buffer *pb;
1527 pb = (struct packetizing_buffer *) xmalloc (sizeof *pb);
1528 memset (pb, 0, sizeof *pb);
1533 pb->fnclosure = fnclosure;
1537 /* Add PACKET_SLOP to handle larger translated packets, and
1538 add 2 for the count. This buffer is increased if
1540 pb->holdbufsize = BUFFER_DATA_SIZE + PACKET_SLOP + 2;
1541 pb->holdbuf = xmalloc (pb->holdbufsize);
1544 return buf_initialize (inpfn != NULL ? packetizing_buffer_input : NULL,
1545 inpfn != NULL ? NULL : packetizing_buffer_output,
1546 inpfn != NULL ? NULL : packetizing_buffer_flush,
1547 packetizing_buffer_block,
1548 packetizing_buffer_shutdown,
1555 /* Input data from a packetizing buffer. */
1557 packetizing_buffer_input (void *closure, char *data, int need, int size,
1560 struct packetizing_buffer *pb = closure;
1564 if (pb->holdsize > 0 && pb->translated)
1568 copy = pb->holdsize;
1572 memcpy (data, pb->holddata, size);
1573 pb->holdsize -= size;
1574 pb->holddata += size;
1579 memcpy (data, pb->holddata, copy);
1589 while (need > 0 || *got == 0)
1591 int get, status, nread, count, tcount;
1593 char stackoutbuf[BUFFER_DATA_SIZE + PACKET_SLOP];
1594 char *inbuf, *outbuf;
1596 /* If we don't already have the two byte count, get it. */
1597 if (pb->holdsize < 2)
1599 get = 2 - pb->holdsize;
1600 status = buf_read_data (pb->buf, get, &bytes, &nread);
1603 /* buf_read_data can return -2, but a buffer input
1604 function is only supposed to return -1, 0, or an
1613 /* The buffer is in nonblocking mode, and we didn't
1614 manage to read anything. */
1619 pb->holdbuf[1] = bytes[0];
1622 pb->holdbuf[0] = bytes[0];
1625 /* We only got one byte, but we needed two. Stash
1626 the byte we got, and try again. */
1630 pb->holdbuf[1] = bytes[1];
1635 /* Read the packet. */
1637 count = (((pb->holdbuf[0] & 0xff) << 8)
1638 + (pb->holdbuf[1] & 0xff));
1640 if (count + 2 > pb->holdbufsize)
1644 /* We didn't allocate enough space in the initialize
1647 n = xrealloc (pb->holdbuf, count + 2);
1650 (*pb->buf->memory_error) (pb->buf);
1654 pb->holdbufsize = count + 2;
1657 get = count - (pb->holdsize - 2);
1659 status = buf_read_data (pb->buf, get, &bytes, &nread);
1662 /* buf_read_data can return -2, but a buffer input
1663 function is only supposed to return -1, 0, or an error
1672 /* We did not get any data. Presumably the buffer is in
1673 nonblocking mode. */
1679 /* We did not get all the data we need to fill the packet.
1680 buf_read_data does not promise to return all the bytes
1681 requested, so we must try again. */
1682 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1683 pb->holdsize += nread;
1687 /* We have a complete untranslated packet of COUNT bytes. */
1689 if (pb->holdsize == 2)
1691 /* We just read the entire packet (the 2 bytes in
1692 PB->HOLDBUF are the size). Save a memcpy by
1693 translating directly from BYTES. */
1698 /* We already had a partial packet in PB->HOLDBUF. We
1699 need to copy the new data over to make the input
1701 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1702 inbuf = pb->holdbuf + 2;
1705 if (count <= sizeof stackoutbuf)
1706 outbuf = stackoutbuf;
1709 outbuf = xmalloc (count);
1712 (*pb->buf->memory_error) (pb->buf);
1717 status = (*pb->inpfn) (pb->fnclosure, inbuf, outbuf, count);
1721 /* The first two bytes in the translated buffer are the real
1722 length of the translated data. */
1723 tcount = ((outbuf[0] & 0xff) << 8) + (outbuf[1] & 0xff);
1726 error (1, 0, "Input translation failure");
1730 /* We have more data than the caller has provided space
1731 for. We need to save some of it for the next call. */
1733 memcpy (data, outbuf + 2, size);
1736 pb->holdsize = tcount - size;
1737 memcpy (pb->holdbuf, outbuf + 2 + size, tcount - size);
1738 pb->holddata = pb->holdbuf;
1741 if (outbuf != stackoutbuf)
1747 memcpy (data, outbuf + 2, tcount);
1749 if (outbuf != stackoutbuf)
1765 /* Output data to a packetizing buffer. */
1767 packetizing_buffer_output (void *closure, const char *data, int have,
1770 struct packetizing_buffer *pb = closure;
1771 char inbuf[BUFFER_DATA_SIZE + 2];
1772 char stack_outbuf[BUFFER_DATA_SIZE + PACKET_SLOP + 4];
1773 struct buffer_data *outdata;
1775 int size, status, translated;
1777 if (have > BUFFER_DATA_SIZE)
1779 /* It would be easy to xmalloc a buffer, but I don't think this
1780 case can ever arise. */
1784 inbuf[0] = (have >> 8) & 0xff;
1785 inbuf[1] = have & 0xff;
1786 memcpy (inbuf + 2, data, have);
1790 /* The output function is permitted to add up to PACKET_SLOP
1791 bytes, and we need 2 bytes for the size of the translated data.
1792 If we can guarantee that the result will fit in a buffer_data,
1793 we translate directly into one to avoid a memcpy in buf_output. */
1794 if (size + PACKET_SLOP + 2 > BUFFER_DATA_SIZE)
1795 outbuf = stack_outbuf;
1798 outdata = get_buffer_data ();
1799 if (outdata == NULL)
1801 (*pb->buf->memory_error) (pb->buf);
1805 outdata->next = NULL;
1806 outdata->bufp = outdata->text;
1808 outbuf = outdata->text;
1811 status = (*pb->outfn) (pb->fnclosure, inbuf, outbuf + 2, size,
1816 /* The output function is permitted to add up to PACKET_SLOP
1818 if (translated > size + PACKET_SLOP)
1821 outbuf[0] = (translated >> 8) & 0xff;
1822 outbuf[1] = translated & 0xff;
1824 if (outbuf == stack_outbuf)
1825 buf_output (pb->buf, outbuf, translated + 2);
1828 outdata->size = translated + 2;
1829 buf_append_data (pb->buf, outdata, outdata);
1834 /* We will only be here because buf_send_output was called on the
1835 packetizing buffer. That means that we should now call
1836 buf_send_output on the underlying buffer. */
1837 return buf_send_output (pb->buf);
1842 /* Flush data to a packetizing buffer. */
1844 packetizing_buffer_flush (void *closure)
1846 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1848 /* Flush the underlying buffer. Note that if the original call to
1849 buf_flush passed 1 for the BLOCK argument, then the buffer will
1850 already have been set into blocking mode, so we should always
1852 return buf_flush (pb->buf, 0);
1857 /* The block routine for a packetizing buffer. */
1859 packetizing_buffer_block (void *closure, int block)
1861 struct packetizing_buffer *pb = (struct packetizing_buffer *) closure;
1864 return set_block (pb->buf);
1866 return set_nonblock (pb->buf);
1871 /* Shut down a packetizing buffer. */
1873 packetizing_buffer_shutdown (struct buffer *buf)
1875 struct packetizing_buffer *pb = buf->closure;
1877 return buf_shutdown (pb->buf);
1880 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */