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