401_nxcomp_bigrequests-and-genericevent-extensions.full+lite.patch 67.1 KB
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
--- a/nxcomp/ClientChannel.cpp
+++ b/nxcomp/ClientChannel.cpp
@@ -447,6 +447,26 @@
         }
       }
 
+      // Get other bits of the header, so will not need to refer to them again
+      unsigned char inputDataByte = inputMessage[1];
+      unsigned int buffer2 = GetUINT(inputMessage + 2, bigEndian_);
+      unsigned int inputDataSize = buffer2 - 1;
+      if (buffer2 == 0)
+      {
+        // BIG-REQUESTS
+        inputMessage += 4;
+        inputLength -= 4;
+        inputDataSize = GetULONG(inputMessage, bigEndian_) - 2;
+      }
+      if (inputLength != (4 * (inputDataSize + 1)))
+      {
+        #ifdef WARNING
+        *logofs << "handleRead: inputLength=" << inputLength
+                << " mismatch inputDataSize=" << inputDataSize
+                << ".\n" << logofs_flush;
+        #endif
+      }
+
       //
       // Go to the message's specific encoding.
       //
@@ -455,6 +475,11 @@
       {
       case X_AllocColor:
         {
+          #ifdef WARNING
+          if (inputLength < 14)
+            *logofs << "handleRead: X_AllocColor inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeCachedValue(GetULONG(inputMessage + 4, bigEndian_), 29,
                              clientCache_ -> colormapCache);
           const unsigned char *nextSrc = inputMessage + 8;
@@ -476,6 +501,11 @@
         break;
       case X_ReparentWindow:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_ReparentWindow inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> windowCache);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 8, bigEndian_),
@@ -486,6 +516,11 @@
         break;
       case X_ChangeProperty:
         {
+          #ifdef WARNING
+          if (inputLength < 24)
+            *logofs << "handleRead: X_ChangeProperty inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           MessageStore *messageStore = clientStore_ ->
                                getRequestStore(X_ChangeProperty);
 
@@ -501,8 +536,36 @@
           encodeBuffer.encodeCachedValue(format, 8,
                              clientCache_ -> changePropertyFormatCache);
           unsigned int dataLength = GetULONG(inputMessage + 20, bigEndian_);
+
+          // Self-preserving sanity check (otherwise we crash and dump core):
+          // some clients do this when not getting their beloved BIG-REQUESTS.
+          unsigned int maxLength = 0;
+          if (format == 8)
+          {
+            maxLength = inputLength - 24;
+          }
+          else if (format == 32)
+          {
+            maxLength = (inputLength - 24) >> 2;
+          }
+          else if (format == 16)
+          {
+            maxLength = (inputLength - 24) >> 1;
+          }
+          if (dataLength > maxLength)
+          {
+            #ifdef WARNING
+            *logofs << "handleRead X_ChangeProperty bogus dataLength=" << dataLength
+                    << " set to " << maxLength
+                    << " when format=" << (int)format
+                    << " inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+            #endif
+            dataLength = maxLength;
+          }
+
           encodeBuffer.encodeValue(dataLength, 32, 6);
-          encodeBuffer.encodeValue(inputMessage[1], 2);
+          encodeBuffer.encodeValue(inputDataByte, 2);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> windowCache);
           encodeBuffer.encodeCachedValue(GetULONG(inputMessage + 8, bigEndian_), 29,
@@ -533,7 +596,7 @@
               nextSrc += 4;
             }
           }
-          else
+          else if (format == 16)
           {
             for (unsigned int i = 0; i < dataLength; i++)
             {
@@ -541,6 +604,13 @@
               nextSrc += 2;
             }
           }
+          else
+          {
+            #ifdef WARNING
+            *logofs << "ChangeProperty bogus format=" << (int)format
+                    << ".\n" << logofs_flush;
+            #endif
+          }
         }
         break;
       case X_SendEvent:
@@ -551,6 +621,11 @@
           // ratio.
           //
 
+          #ifdef WARNING
+          if (inputLength < 44)
+            *logofs << "handleRead: X_SendEvent inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           MessageStore *messageStore = clientStore_ ->
                                getRequestStore(X_SendEvent);
 
@@ -562,7 +637,7 @@
             break;
           }
 
-          encodeBuffer.encodeBoolValue((unsigned int) inputMessage[1]);
+          encodeBuffer.encodeBoolValue((unsigned int) inputDataByte);
           unsigned int window = GetULONG(inputMessage + 4, bigEndian_);
 
           if (window == 0 || window == 1)
@@ -599,7 +674,12 @@
         break;
       case X_ChangeWindowAttributes:
         {
-          encodeBuffer.encodeValue((inputLength - 12) >> 2, 4);
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_ChangeWindowAttributes inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
+          encodeBuffer.encodeValue(inputDataSize - 2, 4);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> windowCache);
           unsigned int bitmask = GetULONG(inputMessage + 8, bigEndian_);
@@ -621,6 +701,11 @@
         break;
       case X_ClearArea:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_ClearArea inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -654,7 +739,7 @@
             break;
           }
 
-          encodeBuffer.encodeBoolValue((unsigned int) inputMessage[1]);
+          encodeBuffer.encodeBoolValue((unsigned int) inputDataByte);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> windowCache);
           const unsigned char *nextSrc = inputMessage + 8;
@@ -668,6 +753,11 @@
         break;
       case X_CloseFont:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_CloseFont inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           unsigned int font = GetULONG(inputMessage + 4, bigEndian_);
           encodeBuffer.encodeValue(font - clientCache_ -> lastFont, 29, 5);
           clientCache_ -> lastFont = font;
@@ -675,6 +765,11 @@
         break;
       case X_ConfigureWindow:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_ConfigureWindow inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           MessageStore *messageStore = clientStore_ ->
                                getRequestStore(X_ConfigureWindow);
 
@@ -708,6 +803,11 @@
         break;
       case X_ConvertSelection:
         {
+          #ifdef WARNING
+          if (inputLength < 24)
+            *logofs << "handleRead: X_ConvertSelection inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeCachedValue(GetULONG(inputMessage + 4, bigEndian_), 29,
                              clientCache_ -> convertSelectionRequestorCache, 9);
           const unsigned char* nextSrc = inputMessage + 8;
@@ -725,6 +825,11 @@
         break;
       case X_CopyArea:
         {
+          #ifdef WARNING
+          if (inputLength < 28)
+            *logofs << "handleRead: X_CopyArea inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -793,6 +898,11 @@
         break;
       case X_CopyGC:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_CopyGC inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int s_g_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -814,6 +924,11 @@
         break;
       case X_CopyPlane:
         {
+          #ifdef WARNING
+          if (inputLength < 32)
+            *logofs << "handleRead: X_CopyPlane inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4,
                              bigEndian_), clientCache_ -> drawableCache);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 8,
@@ -833,6 +948,11 @@
         break;
       case X_CreateGC:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_CreateGC inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int g_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -917,6 +1037,11 @@
         break;
       case X_ChangeGC:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_ChangeGC inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int g_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -998,14 +1123,19 @@
         break;
       case X_CreatePixmap:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_CreatePixmap inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
-          *logofs << "handleRead: X_CreatePixmap depth " << (unsigned) inputMessage[1]
+          *logofs << "handleRead: X_CreatePixmap depth " << (unsigned) inputDataByte
                   << ", pixmap id " << GetULONG(inputMessage + 4, bigEndian_)
                   << ", drawable " << GetULONG(inputMessage + 8, bigEndian_)
                   << ", width " << GetUINT(inputMessage + 12, bigEndian_)
                   << ", height " << GetUINT(inputMessage + 14, bigEndian_)
-                  << ", size " << GetUINT(inputMessage + 2, bigEndian_) << 2
+                  << ", length " << inputLength
                   << ".\n" << logofs_flush;
 
           unsigned int   p_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -1042,6 +1172,11 @@
         break;
       case X_CreateWindow:
         {
+          #ifdef WARNING
+          if (inputLength < 32)
+            *logofs << "handleRead: X_CreateWindow inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int w_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -1054,7 +1189,7 @@
           #endif
 
           unsigned bitmask = GetULONG(inputMessage + 28, bigEndian_);
-          encodeBuffer.encodeCachedValue((unsigned int) inputMessage[1], 8,
+          encodeBuffer.encodeCachedValue((unsigned int) inputDataByte, 8,
                                          clientCache_ -> depthCache);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 8, bigEndian_),
                              clientCache_ -> windowCache);
@@ -1098,6 +1233,11 @@
         break;
       case X_DeleteProperty:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_DeleteProperty inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> windowCache);
           encodeBuffer.encodeValue(GetULONG(inputMessage + 8, bigEndian_), 29, 9);
@@ -1105,6 +1245,11 @@
         break;
       case X_FillPoly:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_FillPoly inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -1138,7 +1283,7 @@
             break;
           }
 
-          unsigned int numPoints = ((inputLength - 16) >> 2);
+          unsigned int numPoints = (inputDataSize - 3);
 
           if (control -> isProtoStep10() == 1)
           {
@@ -1209,7 +1354,12 @@
         break;
       case X_FreeColors:
         {
-          unsigned int numPixels = GetUINT(inputMessage + 2, bigEndian_) - 3;
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_FreeColors inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
+          unsigned int numPixels = inputDataSize - 2;
           encodeBuffer.encodeValue(numPixels, 16, 4);
           encodeBuffer.encodeCachedValue(GetULONG(inputMessage + 4, bigEndian_), 29,
                                          clientCache_ -> colormapCache);
@@ -1225,12 +1375,22 @@
         break;
       case X_FreeCursor:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_FreeCursor inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeCachedValue(GetULONG(inputMessage + 4, bigEndian_),
                                          29, clientCache_ -> cursorCache, 9);
         }
         break;
       case X_FreeGC:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_FreeGC inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int g_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -1284,6 +1444,11 @@
         break;
       case X_FreePixmap:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_FreePixmap inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int p_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -1318,6 +1483,11 @@
         break;
       case X_GetAtomName:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_GetAtomName inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeValue(GetULONG(inputMessage + 4, bigEndian_), 29, 9);
 
           sequenceQueue_.push(clientSequence_, inputOpcode);
@@ -1327,6 +1497,11 @@
         break;
       case X_GetGeometry:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_GetGeometry inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> drawableCache);
 
@@ -1351,6 +1526,11 @@
         break;
       case X_GetKeyboardMapping:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_GetKeyboardMapping inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeValue((unsigned int) inputMessage[4], 8);
           encodeBuffer.encodeValue((unsigned int) inputMessage[5], 8);
 
@@ -1361,6 +1541,11 @@
         break;
       case X_GetProperty:
         {
+          #ifdef WARNING
+          if (inputLength < 24)
+            *logofs << "handleRead: X_GetProperty inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           MessageStore *messageStore = clientStore_ ->
                                getRequestStore(X_GetProperty);
 
@@ -1378,7 +1563,7 @@
             break;
           }
 
-          encodeBuffer.encodeBoolValue((unsigned int) inputMessage[1]);
+          encodeBuffer.encodeBoolValue((unsigned int) inputDataByte);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> windowCache);
           unsigned int property = GetULONG(inputMessage + 8, bigEndian_);
@@ -1394,6 +1579,11 @@
         break;
       case X_GetSelectionOwner:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_GetSelectionOwner inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeCachedValue(GetULONG(inputMessage + 4, bigEndian_), 29,
                              clientCache_ -> getSelectionOwnerSelectionCache, 9);
 
@@ -1404,7 +1594,12 @@
         break;
       case X_GrabButton:
         {
-          encodeBuffer.encodeBoolValue((unsigned int) inputMessage[1]);
+          #ifdef WARNING
+          if (inputLength < 24)
+            *logofs << "handleRead: X_GrabButton inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
+          encodeBuffer.encodeBoolValue((unsigned int) inputDataByte);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> windowCache);
           encodeBuffer.encodeCachedValue(GetUINT(inputMessage + 8, bigEndian_), 16,
@@ -1423,7 +1618,12 @@
         break;
       case X_GrabPointer:
         {
-          encodeBuffer.encodeBoolValue((unsigned int) inputMessage[1]);
+          #ifdef WARNING
+          if (inputLength < 24)
+            *logofs << "handleRead: X_GrabPointer inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
+          encodeBuffer.encodeBoolValue((unsigned int) inputDataByte);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> windowCache);
           encodeBuffer.encodeCachedValue(GetUINT(inputMessage + 8, bigEndian_), 16,
@@ -1448,7 +1648,12 @@
         break;
       case X_GrabKeyboard:
         {
-          encodeBuffer.encodeBoolValue((unsigned int) inputMessage[1]);
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_GrabKeyboard inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
+          encodeBuffer.encodeBoolValue((unsigned int) inputDataByte);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> windowCache);
           unsigned int timestamp = GetULONG(inputMessage + 8, bigEndian_);
@@ -1471,6 +1676,11 @@
         break;
       case X_PolyText8:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_PolyText8 inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -1555,6 +1765,11 @@
         break;
       case X_PolyText16:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_PolyText16 inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -1639,6 +1854,11 @@
         break;
       case X_ImageText8:
         {
+          #ifdef WARNING
+          if (inputLength < 16 + (unsigned int)inputDataByte)
+            *logofs << "handleRead: X_ImageText8 inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -1673,7 +1893,7 @@
             break;
           }
 
-          unsigned int textLength = (unsigned int) inputMessage[1];
+          unsigned int textLength = (unsigned int) inputDataByte;
           encodeBuffer.encodeCachedValue(textLength, 8,
                              clientCache_ -> imageTextLengthCache, 4);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4,
@@ -1706,6 +1926,11 @@
         break;
       case X_ImageText16:
         {
+          #ifdef WARNING
+          if (inputLength < 16 + (unsigned int)inputDataByte)
+            *logofs << "handleRead: X_ImageText16 inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -1740,7 +1965,7 @@
             break;
           }
 
-          unsigned int textLength = (unsigned int) inputMessage[1];
+          unsigned int textLength = (unsigned int) inputDataByte;
           encodeBuffer.encodeCachedValue(textLength, 8,
                              clientCache_ -> imageTextLengthCache, 4);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4,
@@ -1773,6 +1998,11 @@
         break;
       case X_InternAtom:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_InternAtom inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           MessageStore *messageStore = clientStore_ ->
                                getRequestStore(X_InternAtom);
 
@@ -1796,8 +2026,18 @@
           }
 
           unsigned int nameLength = GetUINT(inputMessage + 4, bigEndian_);
+          unsigned int maxLength = inputLength - 8;
+          if (nameLength > maxLength)
+          {
+            #ifdef WARNING
+            *logofs << "handleRead X_InternAtom bogus nameLength=" << nameLength
+                    << " set to " << maxLength
+                    << ".\n" << logofs_flush;
+            #endif
+            nameLength = maxLength;
+          }
           encodeBuffer.encodeValue(nameLength, 16, 6);
-          encodeBuffer.encodeBoolValue((unsigned int) inputMessage[1]);
+          encodeBuffer.encodeBoolValue((unsigned int) inputDataByte);
           const unsigned char *nextSrc = inputMessage + 8;
 
           if (control -> isProtoStep7() == 1)
@@ -1827,7 +2067,22 @@
         break;
       case X_ListFonts:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_ListFonts inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           unsigned int textLength = GetUINT(inputMessage + 6, bigEndian_);
+          unsigned int maxLength = inputLength - 8;
+          if (textLength > maxLength)
+          {
+            #ifdef WARNING
+            *logofs << "handleRead X_ListFonts bogus textLength=" << textLength
+                    << " set to " << maxLength
+                    << ".\n" << logofs_flush;
+            #endif
+            textLength = maxLength;
+          }
           encodeBuffer.encodeValue(textLength, 16, 6);
           encodeBuffer.encodeValue(GetUINT(inputMessage + 4, bigEndian_), 16, 6);
           const unsigned char* nextSrc = inputMessage + 8;
@@ -1853,7 +2108,22 @@
       case X_LookupColor:
       case X_AllocNamedColor:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_AllocNamedColor inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           unsigned int textLength = GetUINT(inputMessage + 8, bigEndian_);
+          unsigned int maxLength = inputLength - 12;
+          if (textLength > maxLength)
+          {
+            #ifdef WARNING
+            *logofs << "handleRead X_AllocNamedColor bogus textLength=" << textLength
+                    << " set to " << maxLength
+                    << ".\n" << logofs_flush;
+            #endif
+            textLength = maxLength;
+          }
           encodeBuffer.encodeValue(textLength, 16, 6);
           encodeBuffer.encodeCachedValue(GetULONG(inputMessage + 4, bigEndian_),
                                          29, clientCache_ -> colormapCache);
@@ -1886,6 +2156,11 @@
       case X_QueryPointer:
       case X_QueryTree:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_MapWindow...X_QueryTree inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           if (inputOpcode == X_DestroyWindow)
@@ -1923,7 +2198,22 @@
         break;
       case X_OpenFont:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_OpenFont inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           unsigned int nameLength = GetUINT(inputMessage + 8, bigEndian_);
+          unsigned int maxLength = inputLength - 12;
+          if (nameLength > maxLength)
+          {
+            #ifdef WARNING
+            *logofs << "handleRead X_InternAtom bogus nameLength=" << nameLength
+                    << " set to " << maxLength
+                    << ".\n" << logofs_flush;
+            #endif
+            nameLength = maxLength;
+          }
           encodeBuffer.encodeValue(nameLength, 16, 7);
           unsigned int font = GetULONG(inputMessage + 4, bigEndian_);
           encodeBuffer.encodeValue(font - clientCache_ -> lastFont, 29, 5);
@@ -1947,6 +2237,11 @@
         break;
       case X_PolyFillRectangle:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_PolyFillRectangle inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -2034,6 +2329,11 @@
         break;
       case X_PolyFillArc:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_PolyFillArc inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -2135,6 +2435,11 @@
         break;
       case X_PolyArc:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_PolyArc inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -2236,6 +2541,11 @@
         break;
       case X_PolyPoint:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_PolyPoint inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -2269,8 +2579,8 @@
             break;
           }
 
-          encodeBuffer.encodeValue(GetUINT(inputMessage + 2, bigEndian_) - 3, 16, 4);
-          encodeBuffer.encodeBoolValue((unsigned int) inputMessage[1]);
+          encodeBuffer.encodeValue(inputDataSize - 2, 32, 4);
+          encodeBuffer.encodeBoolValue((unsigned int) inputDataByte);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> drawableCache);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 8, bigEndian_),
@@ -2303,6 +2613,11 @@
         break;
       case X_PolyLine:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_PolyLine inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -2336,8 +2651,8 @@
             break;
           }
 
-          encodeBuffer.encodeValue(GetUINT(inputMessage + 2, bigEndian_) - 3, 16, 4);
-          encodeBuffer.encodeBoolValue((unsigned int) inputMessage[1]);
+          encodeBuffer.encodeValue(inputDataSize - 2, 32, 4);
+          encodeBuffer.encodeBoolValue((unsigned int) inputDataByte);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4,
                              bigEndian_), clientCache_ -> drawableCache);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 8,
@@ -2370,8 +2685,12 @@
         break;
       case X_PolyRectangle:
         {
-          encodeBuffer.encodeValue((GetUINT(inputMessage + 2,
-                                            bigEndian_) - 3) >> 1, 16, 3);
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_PolyRectangle inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
+          encodeBuffer.encodeValue((inputDataSize - 2) >> 1, 32, 3);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4,
                              bigEndian_), clientCache_ -> drawableCache);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 8,
@@ -2391,6 +2710,11 @@
         break;
       case X_PolySegment:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_PolySegment inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -2424,8 +2748,7 @@
             break;
           }
 
-          encodeBuffer.encodeValue((GetUINT(inputMessage + 2,
-                                            bigEndian_) - 3) >> 1, 16, 4);
+          encodeBuffer.encodeValue((inputDataSize - 2) >> 1, 32, 4);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4,
                              bigEndian_), clientCache_ -> drawableCache);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 8,
@@ -2491,6 +2814,11 @@
         break;
       case X_PutImage:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_PutImage inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -2522,7 +2850,12 @@
         break;
       case X_QueryBestSize:
         {
-          encodeBuffer.encodeValue((unsigned int)inputMessage[1], 2);
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_QueryBestSize inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
+          encodeBuffer.encodeValue((unsigned int)inputDataByte, 2);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4,
                              bigEndian_), clientCache_ -> drawableCache);
           encodeBuffer.encodeValue(GetUINT(inputMessage + 8, bigEndian_), 16, 8);
@@ -2535,10 +2868,15 @@
         break;
       case X_QueryColors:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_QueryColors inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           // Differential encoding.
           encodeBuffer.encodeBoolValue(1);
 
-          unsigned int numColors = ((inputLength - 8) >> 2);
+          unsigned int numColors = (inputDataSize - 1);
           encodeBuffer.encodeValue(numColors, 16, 5);
           encodeBuffer.encodeCachedValue(GetULONG(inputMessage + 4, bigEndian_), 29,
                                          clientCache_ -> colormapCache);
@@ -2567,15 +2905,20 @@
         break;
       case X_QueryExtension:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_QueryExtension inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TEST
 
           char data[256];
 
           int length = GetUINT(inputMessage + 4, bigEndian_);
 
-          if (length > 256)
+          if (length > 255)
           {
-            length = 256;
+            length = 255;
           }
 
           strncpy(data, (char *) inputMessage + 8, length);
@@ -2588,6 +2931,16 @@
           #endif
 
           unsigned int nameLength = GetUINT(inputMessage + 4, bigEndian_);
+          unsigned int maxLength = inputLength - 8;
+          if (nameLength > maxLength)
+          {
+            #ifdef WARNING
+            *logofs << "handleRead X_QueryExtension bogus nameLength=" << nameLength
+                    << " set to " << maxLength
+                    << ".\n" << logofs_flush;
+            #endif
+            nameLength = maxLength;
+          }
           encodeBuffer.encodeValue(nameLength, 16, 6);
           const unsigned char *nextSrc = inputMessage + 8;
 
@@ -2614,6 +2967,11 @@
         break;
       case X_QueryFont:
         {
+          #ifdef WARNING
+          if (inputLength < 8)
+            *logofs << "handleRead: X_QueryFont inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           unsigned int font = GetULONG(inputMessage + 4, bigEndian_);
           encodeBuffer.encodeValue(font - clientCache_ -> lastFont, 29, 5);
           clientCache_ -> lastFont = font;
@@ -2625,6 +2983,11 @@
         break;
       case X_SetClipRectangles:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_SetClipRectangles inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           MessageStore *messageStore = clientStore_ ->
                                getRequestStore(X_SetClipRectangles);
 
@@ -2636,7 +2999,7 @@
             break;
           }
 
-          unsigned int numRectangles = ((inputLength - 12) >> 3);
+          unsigned int numRectangles = ((inputDataSize - 2) >> 1);
 
           if (control -> isProtoStep9() == 1)
           {
@@ -2647,7 +3010,7 @@
             encodeBuffer.encodeValue(numRectangles, 13, 4);
           }
 
-          encodeBuffer.encodeValue((unsigned int) inputMessage[1], 2);
+          encodeBuffer.encodeValue((unsigned int) inputDataByte, 2);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
                              clientCache_ -> gcCache);
           encodeBuffer.encodeCachedValue(GetUINT(inputMessage + 8, bigEndian_), 16,
@@ -2668,7 +3031,22 @@
         break;
       case X_SetDashes:
         {
+          #ifdef WARNING
+          if (inputLength < 12)
+            *logofs << "handleRead: X_SetDashes inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           unsigned int numDashes = GetUINT(inputMessage + 10, bigEndian_);
+          unsigned int maxLength = inputLength - 12;
+          if (numDashes > maxLength)
+          {
+            #ifdef WARNING
+            *logofs << "handleRead X_SetDashes bogus numDashes=" << numDashes
+                    << " set to " << maxLength
+                    << ".\n" << logofs_flush;
+            #endif
+            numDashes = maxLength;
+          }
           encodeBuffer.encodeCachedValue(numDashes, 16,
                              clientCache_ -> setDashesLengthCache, 5);
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4, bigEndian_),
@@ -2683,6 +3061,11 @@
         break;
       case X_SetSelectionOwner:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_SetSelectionOwner inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           encodeBuffer.encodeCachedValue(GetULONG(inputMessage + 4, bigEndian_), 29,
                                     clientCache_ -> setSelectionOwnerCache, 9);
           encodeBuffer.encodeCachedValue(GetULONG(inputMessage + 8, bigEndian_), 29,
@@ -2693,6 +3076,11 @@
         break;
       case X_TranslateCoords:
         {
+          #ifdef WARNING
+          if (inputLength < 16)
+            *logofs << "handleRead: X_TranslateCoords inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -2764,6 +3152,11 @@
         break;
       case X_GetImage:
         {
+          #ifdef WARNING
+          if (inputLength < 20)
+            *logofs << "handleRead: X_GetImage inputLength=" << inputLength
+                    << ".\n" << logofs_flush;
+          #endif
           #ifdef TARGETS
 
           unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -2802,7 +3195,7 @@
           }
 
           // Format.
-          encodeBuffer.encodeValue((unsigned int) inputMessage[1], 2);
+          encodeBuffer.encodeValue((unsigned int) inputDataByte, 2);
           // Drawable.
           encodeBuffer.encodeXidValue(GetULONG(inputMessage + 4,
                              bigEndian_), clientCache_ -> drawableCache);
@@ -2869,6 +3262,11 @@
           }
           else if (inputOpcode == opcodeStore_ -> putPackedImage)
           {
+            #ifdef WARNING
+            if (inputLength < 24)
+              *logofs << "handleRead: putPackedImage inputLength=" << inputLength
+                      << ".\n" << logofs_flush;
+            #endif
             #ifdef TARGETS
 
             unsigned int t_id = GetULONG(inputMessage + 4, bigEndian_);
@@ -3004,7 +3402,7 @@
                     << ".\n" << logofs_flush;
             #endif
 
-            encodeBuffer.encodeCachedValue(*(inputMessage + 1), 8,
+            encodeBuffer.encodeCachedValue(inputDataByte, 8,
                          clientCache_ -> resourceCache);
           }
           else if (inputOpcode == opcodeStore_ -> freeUnpack)
@@ -3015,7 +3413,7 @@
                     << ".\n" << logofs_flush;
             #endif
 
-            encodeBuffer.encodeCachedValue(*(inputMessage + 1), 8,
+            encodeBuffer.encodeCachedValue(inputDataByte, 8,
                          clientCache_ -> resourceCache);
           }
           else if (inputOpcode == opcodeStore_ -> getControlParameters)
@@ -3130,6 +3528,11 @@
             // Enable or disable expose events
             // coming from the real server.
             //
+            #ifdef WARNING
+            if (inputLength < 8)
+              *logofs << "handleRead: setExposeParameters inputLength=" << inputLength
+                      << ".\n" << logofs_flush;
+            #endif
 
             encodeBuffer.encodeBoolValue(*(inputMessage + 4));
             encodeBuffer.encodeBoolValue(*(inputMessage + 5));
@@ -3198,10 +3601,10 @@
       {
         if (hit)
         {
-          statistics -> addRenderCachedRequest(*(inputMessage + 1));
+          statistics -> addRenderCachedRequest(inputDataByte);
         }
 
-        statistics -> addRenderRequestBits(*(inputMessage + 1), inputLength << 3, bits);
+        statistics -> addRenderRequestBits(inputDataByte, inputLength << 3, bits);
       }
 
     }  // End if (firstRequest_)... else ...
@@ -4548,10 +4951,10 @@
         //
 
 /*
-FIXME: Recover the sequence number if the proxy
+Fixed as below? - FIXME: Recover the sequence number if the proxy
        is not connected to an agent.
 */
-        if (serverSequence_ > lastSequence_ ||
+        if (SequenceNumber_x_gt_y(serverSequence_, lastSequence_) ||
                 control -> SessionMode != session_proxy)
         {
           #ifdef DEBUG
@@ -4564,7 +4967,7 @@
           lastSequence_ = serverSequence_;
         }
         #ifdef DEBUG
-        else if (serverSequence_ < lastSequence_)
+        else if (SequenceNumber_x_gt_y(lastSequence_, serverSequence_))
         {
           //
           // Use our last auto-generated sequence.
@@ -5003,6 +5406,12 @@
           break;
         default:
           {
+            // BEWARE: not only inputOpcode == GenericEvent but also
+            // others not handled above, at least:
+            //   GraphicsExpose    13
+            //   MapRequest        20
+            //   ConfigureRequest  23
+            // and any beyond LASTEvent.
             #ifdef TEST
             *logofs << "handleWrite: Using generic event compression "
                     << "for OPCODE#" << (unsigned int) outputOpcode
@@ -5014,11 +5423,51 @@
 
             for (unsigned int i = 0; i < 14; i++)
             {
-              decodeBuffer.decodeCachedValue(value, 16,
-                           *serverCache_ -> genericEventIntCache[i]);
+              //decodeBuffer.decodeCachedValue(value, 16,
+              //             *serverCache_ -> genericEventIntCache[i]);
+              if ( ! (decodeBuffer.decodeCachedValue(value, 16,
+                           *serverCache_ -> genericEventIntCache[i])) )
+              {
+                #ifdef WARNING
+                *logofs << "decodeCachedValue failed for GenEvt:"
+                        << " buffer length=" << length
+                        << " i=" << i
+                        << "\n" << logofs_flush;
+                #endif
+                break;
+              }
 
               PutUINT(value, outputMessage + i * 2 + 4, bigEndian_);
             }
+            // Handle "X Generic Event Extension"
+            // Extra data is not cached...
+            if (outputOpcode == GenericEvent && *(outputMessage+1) != 0 && outputLength == 32)
+            {
+              unsigned int extraOutputLength = (GetULONG(outputMessage + 4, bigEndian_) << 2);
+              if (extraOutputLength > 0 && extraOutputLength < 100*1024*1024)
+              {
+                // Extend buffer for the extra data
+                outputMessage = writeBuffer_.addMessage(extraOutputLength);
+                // Decode data and write into buffer at new position
+                for (unsigned int i = 0; i < (extraOutputLength>>1); i++)
+                {
+                  //decodeBuffer.decodeValue(value, 16);
+                  if ( ! (decodeBuffer.decodeValue(value, 16)) )
+                  {
+                    #ifdef WARNING
+                    *logofs << "decodeValue failed for GenEvt:"
+                            << " extraOutputLength=" << extraOutputLength
+                            << " buffer length=" << length
+                            << " i=" << i
+                            << "\n" << logofs_flush;
+                    #endif
+                    break;
+                  }
+                  PutUINT(value, outputMessage + i * 2, bigEndian_);
+                }
+              }
+            }
+
           }
         } // End of switch (outputOpcode)...
 
@@ -6892,7 +7341,7 @@
   }
   else
   {
-    if (serverSequence_ > lastSequence_)
+    if (SequenceNumber_x_gt_y(serverSequence_, lastSequence_))
     {
       #ifdef DEBUG
       *logofs << "handleNotify: Updating last event's sequence "
@@ -6904,7 +7353,7 @@
       lastSequence_ = serverSequence_;
     }
     #ifdef DEBUG
-    else if (serverSequence_ < lastSequence_)
+    else if (SequenceNumber_x_gt_y(lastSequence_, serverSequence_))
     {
       //
       // Use our last auto-generated sequence.
--- a/nxcomp/ClientReadBuffer.cpp
+++ b/nxcomp/ClientReadBuffer.cpp
@@ -119,15 +119,34 @@
 
     dataLength = (GetUINT(start + 2, bigEndian_) << 2);
 
-    if (dataLength < 4)
+    if (dataLength == 0)        // or equivalently (dataLength < 4)
     {
-      #ifdef TEST
-      *logofs << "ClientReadBuffer: WARNING! Assuming length 4 "
-              << "for suspicious message of length " << dataLength
-              << ".\n" << logofs_flush;
-      #endif
+      // BIG-REQUESTS extension
+      if (size < 8)
+      {
+        remaining_ = 8 - size;
+        return 0;
+      }
 
-      dataLength = 4;
+      dataLength = (GetULONG(start + 4, bigEndian_) << 2);
+
+// See WRITE_BUFFER_OVERFLOW_SIZE elsewhere
+// and also ENCODE_BUFFER_OVERFLOW_SIZE DECODE_BUFFER_OVERFLOW_SIZE.
+      if (dataLength < 8 || dataLength > 100*1024*1024)
+      {
+        #ifdef WARNING
+        *logofs << "BIG-REQUESTS with unacceptable dataLength="
+                << dataLength << ", now set to 8.\n" << logofs_flush;
+        #endif
+        dataLength = 8;
+      }
+      else if (dataLength < 4*64*1024)
+      {
+        #ifdef WARNING
+        *logofs << "BIG-REQUESTS with silly dataLength="
+                << dataLength << ".\n" << logofs_flush;
+        #endif
+      }
     }
   }
 
--- a/nxcomp/DecodeBuffer.cpp
+++ b/nxcomp/DecodeBuffer.cpp
@@ -78,34 +78,45 @@
       {
         if (!endOkay)
         {
-          #ifdef PANIC
-          *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [A] "
-                  << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
-                  << " end_ = " << (end_ - buffer_) << ".\n"
-                  << logofs_flush;
+          #ifdef WARNING
+          *logofs << "DecodeBuffer: Error [A] in decodeValue(), returning 0:"
+                  << " nextSrc_ = " << (nextSrc_ - buffer_)
+                  << " end_ = " << (end_ - buffer_)
+                  << ".\n" << logofs_flush;
           #endif
-
-          //
-          // Label "context" is just used to identify
-          // the routine which detected the problem in
-          // present source file.
-          //
-
-          cerr << "Error" << ": Failure decoding data in context [A].\n";
-
-          HandleAbort();
+          value = 0;
+          return 0;
+          //#ifdef PANIC
+          //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [A] "
+          //        << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
+          //        << " end_ = " << (end_ - buffer_) << ".\n"
+          //        << logofs_flush;
+          //#endif
+          ////
+          //// Label "context" is just used to identify
+          //// the routine which detected the problem in
+          //// present source file.
+          ////
+          //cerr << "Error" << ": Failure decoding data in context [A].\n";
+          //HandleAbort();
         }
 
-        #ifdef PANIC
-        *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [B] "
-                << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
-                << " end_ = " << (end_ - buffer_) << ".\n"
-                << logofs_flush;
+        #ifdef WARNING
+        *logofs << "DecodeBuffer: Error [B] in decodeValue(), returning 0:"
+                << " nextSrc_ = " << (nextSrc_ - buffer_)
+                << " end_ = " << (end_ - buffer_)
+                << ".\n" << logofs_flush;
         #endif
-
-        cerr << "Error" << ": Failure decoding data in context [B].\n";
-
-        HandleAbort();
+        value = 0;
+        return 0;
+        //#ifdef PANIC
+        //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [B] "
+        //        << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
+        //        << " end_ = " << (end_ - buffer_) << ".\n"
+        //        << logofs_flush;
+        //#endif
+        //cerr << "Error" << ": Failure decoding data in context [B].\n";
+        //HandleAbort();
       }
 
       lastBit = (nextSrcChar & srcMask_);
@@ -134,28 +145,40 @@
       {
         if (!endOkay)
         {
-          #ifdef PANIC
-          *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [C] "
-                  << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
-                  << " end_ = " << (end_ - buffer_) << ".\n"
-                  << logofs_flush;
+          #ifdef WARNING
+          *logofs << "DecodeBuffer: Error [C] in decodeValue(), returning 0:"
+                  << " nextSrc_ = " << (nextSrc_ - buffer_)
+                  << " end_ = " << (end_ - buffer_)
+                  << ".\n" << logofs_flush;
           #endif
-
-          cerr << "Error" << ": Failure decoding data in context [C].\n";
-
-          HandleAbort();
+          value = 0;
+          return 0;
+          //#ifdef PANIC
+          //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [C] "
+          //        << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
+          //        << " end_ = " << (end_ - buffer_) << ".\n"
+          //        << logofs_flush;
+          //#endif
+          //cerr << "Error" << ": Failure decoding data in context [C].\n";
+          //HandleAbort();
         }
 
-        #ifdef PANIC
-        *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [D] "
-                << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
-                << " end_ = " << (end_ - buffer_) << ".\n"
-                << logofs_flush;
+        #ifdef WARNING
+        *logofs << "DecodeBuffer: Error [D] in decodeValue(), returning 0:"
+                << " nextSrc_ = " << (nextSrc_ - buffer_)
+                << " end_ = " << (end_ - buffer_)
+                << ".\n" << logofs_flush;
         #endif
-
-        cerr << "Error" << ": Failure decoding data in context [D].\n";
-
-        HandleAbort();
+        value = 0;
+        return 0;
+        //#ifdef PANIC
+        //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [D] "
+        //        << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
+        //        << " end_ = " << (end_ - buffer_) << ".\n"
+        //        << logofs_flush;
+        //#endif
+        //cerr << "Error" << ": Failure decoding data in context [D].\n";
+        //HandleAbort();
       }
 
       unsigned char moreData = (nextSrcChar & srcMask_);
@@ -212,16 +235,24 @@
 
   if (nextSrc_ >= end_)
   {
-    #ifdef PANIC
-    *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [E] "
-            << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
-            << " end_ = " << (end_ - buffer_) << ".\n"
-            << logofs_flush;
+    #ifdef WARNING
+    *logofs << "DecodeBuffer: Error [E] in decodeCachedValue(), returning 0:"
+            << " nextSrc_ = " << (nextSrc_ - buffer_)
+            << " end_ = " << (end_ - buffer_)
+            << ".\n" << logofs_flush;
     #endif
-
-    cerr << "Error" << ": Failure decoding data in context [E].\n";
-
-    HandleAbort();
+    // Failed: return value 0
+    value = 0;
+    // Failed: return 0, though our callers do not check that...
+    return 0;
+    //#ifdef PANIC
+    //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [E] "
+    //        << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
+    //        << " end_ = " << (end_ - buffer_) << ".\n"
+    //        << logofs_flush;
+    //#endif
+    //cerr << "Error" << ": Failure decoding data in context [E].\n";
+    //HandleAbort();
   }
 
   unsigned int index = 0;
@@ -237,30 +268,33 @@
       nextSrc_++;
       if (nextSrc_ >= end_)
       {
-        if (!endOkay)
-        {
-          #ifdef PANIC
-          *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [F] "
-                  << "in decodeCachedValue() nextSrc_ = "
-                  << (nextSrc_ - buffer_) << " end_ = "
-                  << (end_ - buffer_) << ".\n" << logofs_flush;
-          #endif
-
-          cerr << "Error" << ": Failure decoding data in context [F].\n";
-
-          HandleAbort();
-        }
-
-        #ifdef PANIC
-        *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [G] "
-                << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
-                << " end_ = " << (end_ - buffer_) << ".\n"
-                << logofs_flush;
+        #ifdef WARNING
+        *logofs << "DecodeBuffer: Error [G] in decodeCachedValue(), returning 0:"
+                << " nextSrc_ = " << (nextSrc_ - buffer_)
+                << " end_ = " << (end_ - buffer_)
+                << ".\n" << logofs_flush;
         #endif
-
-        cerr << "Error" << ": Failure decoding data in context [G].\n";
-
-        HandleAbort();
+        value = 0;
+        return 0;
+        //if (!endOkay)
+        //{
+        //  #ifdef PANIC
+        //  *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [F] "
+        //          << "in decodeCachedValue() nextSrc_ = "
+        //          << (nextSrc_ - buffer_) << " end_ = "
+        //          << (end_ - buffer_) << ".\n" << logofs_flush;
+        //  #endif
+        //  cerr << "Error" << ": Failure decoding data in context [F].\n";
+        //  HandleAbort();
+        //}
+        //#ifdef PANIC
+        //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [G] "
+        //        << "in decodeValue() nextSrc_ = " << (nextSrc_ - buffer_)
+        //        << " end_ = " << (end_ - buffer_) << ".\n"
+        //        << logofs_flush;
+        //#endif
+        //cerr << "Error" << ": Failure decoding data in context [G].\n";
+        //HandleAbort();
       }
 
       nextSrcChar = *nextSrc_;
@@ -288,15 +322,20 @@
         return 1;
       }
 
-      #ifdef PANIC
-      *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [H] "
-              << "in decodeCacheValue() with no value found.\n"
-              << logofs_flush;
+      #ifdef WARNING
+      *logofs << "DecodeBuffer: Error [H] in decodeCachedValue(), returning 0:"
+              << " no value found"
+              << ".\n" << logofs_flush;
       #endif
-
-      cerr << "Error" << ": Failure decoding data in context [H].\n";
-
-      HandleAbort();
+      value = 0;
+      return 0;
+      //#ifdef PANIC
+      //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [H] "
+      //        << "in decodeCacheValue() with no value found.\n"
+      //        << logofs_flush;
+      //#endif
+      //cerr << "Error" << ": Failure decoding data in context [H].\n";
+      //HandleAbort();
     }
     else
     {
@@ -323,15 +362,20 @@
           return 1;
         }
 
-        #ifdef PANIC
-        *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [H] "
-                << "in decodeCacheValue() with no value found.\n"
-                << logofs_flush;
+        #ifdef WARNING
+        *logofs << "DecodeBuffer: Error [F] in decodeCachedValue(), returning 0:"
+                << " no value found"
+                << " .\n" << logofs_flush;
         #endif
-
-        cerr << "Error" << ": Failure decoding data in context [H].\n";
-
-        HandleAbort();
+        value = 0;
+        return 0;
+        //#ifdef PANIC
+        //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [H] "
+        //        << "in decodeCacheValue() with no value found.\n"
+        //        << logofs_flush;
+        //#endif
+        //cerr << "Error" << ": Failure decoding data in context [H].\n";
+        //HandleAbort();
       }
     }
   }
@@ -344,16 +388,22 @@
 
     if (index > cache.getSize())
     {
-      #ifdef PANIC
-      *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [I] "
-              << "in decodeCachedValue() index = " << index
-              << " cache size = " << cache.getSize() << ".\n"
-              << logofs_flush;
+      #ifdef WARNING
+      *logofs << "DecodeBuffer: Error [I] in decodeCachedValue(), returning 0:"
+              << " index = " << index
+              << " cache size = " << cache.getSize()
+              << ".\n" << logofs_flush;
       #endif
-
-      cerr << "Error" << ": Failure decoding data in context [I].\n";
-
-      HandleAbort();
+      value = 0;
+      return 0;
+      //#ifdef PANIC
+      //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [I] "
+      //        << "in decodeCachedValue() index = " << index
+      //        << " cache size = " << cache.getSize() << ".\n"
+      //        << logofs_flush;
+      //#endif
+      //cerr << "Error" << ": Failure decoding data in context [I].\n";
+      //HandleAbort();
     }
 
     value = cache.get(index);
@@ -401,16 +451,22 @@
       {
         if (!endOkay)
         {
-          #ifdef PANIC
-          *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [K] "
-                  << "in decodeCachedValue() nextSrc_ "
-                  << (nextSrc_ - buffer_) << " end_ " << (end_ - buffer_)
+          #ifdef WARNING
+          *logofs << "DecodeBuffer: Error [K] in decodeCachedValue(), returning 0:"
+                  << " nextSrc_ " << (nextSrc_ - buffer_)
+                  << " end_ " << (end_ - buffer_)
                   << ".\n" << logofs_flush;
           #endif
-
-          cerr << "Error" << ": Failure decoding data in context [K].\n";
-
-          HandleAbort();
+          value = 0;
+          return 0;
+          //#ifdef PANIC
+          //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [K] "
+          //        << "in decodeCachedValue() nextSrc_ "
+          //        << (nextSrc_ - buffer_) << " end_ " << (end_ - buffer_)
+          //        << ".\n" << logofs_flush;
+          //#endif
+          //cerr << "Error" << ": Failure decoding data in context [K].\n";
+          //HandleAbort();
         }
 
         #ifdef TEST
@@ -446,15 +502,20 @@
     }
     else
     {
-      #ifdef PANIC
-      *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [M] "
-              << "in decodeValue() with index = 2.\n"
-              << logofs_flush;
+      #ifdef WARNING
+      *logofs << "DecodeBuffer: Error [M] in decodeCachedValue(), returning 0:"
+              << "with index = 2"
+              << ".\n" << logofs_flush;
       #endif
-
-      cerr << "Error" << ": Failure decoding data in context [M].\n";
-
-      HandleAbort();
+      value = 0;
+      return 0;
+      //#ifdef PANIC
+      //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [M] "
+      //        << "in decodeValue() with index = 2.\n"
+      //        << logofs_flush;
+      //#endif
+      //cerr << "Error" << ": Failure decoding data in context [M].\n";
+      //HandleAbort();
     }
   }
   else
@@ -466,16 +527,22 @@
 
     if (index > cache.getSize())
     {
-      #ifdef PANIC
-      *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [N] "
-              << "in decodeCachedValue() " << "index = " << index
-              << " cache size = " << cache.getSize() << ".\n"
-              << logofs_flush;
+      #ifdef WARNING
+      *logofs << "DecodeBuffer: Error [N] in decodeCachedValue(), returning 0:"
+              << " index = " << index
+              << " cache size = " << cache.getSize()
+              << ".\n" << logofs_flush;
       #endif
-
-      cerr << "Error" << ": Failure decoding data in context [N].\n";
-
-      HandleAbort();
+      value = 0;
+      return 0;
+      //#ifdef PANIC
+      //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [N] "
+      //        << "in decodeCachedValue() " << "index = " << index
+      //        << " cache size = " << cache.getSize() << ".\n"
+      //        << logofs_flush;
+      //#endif
+      //cerr << "Error" << ": Failure decoding data in context [N].\n";
+      //HandleAbort();
     }
 
     value = cache.get(index);
@@ -538,16 +605,22 @@
   }
   else if (end_ - nextSrc_ < (int) numBytes)
   {
-    #ifdef PANIC
-    *logofs << "DecodeBuffer: PANIC! Assertion failed. Error [P] "
-            << "in decodeMemory() " << "with length " << numBytes
+    #ifdef WARNING
+    *logofs << "DecodeBuffer: Error [P] in decodeMemory(), returning NULL:"
+            << " with length " << numBytes
             << " and " << (end_ - nextSrc_)
-            << " bytes remaining.\n" << logofs_flush;
+            << " bytes remaining"
+            << ".\n" << logofs_flush;
     #endif
-
-    cerr << "Error" << ": Failure decoding data in context [P].\n";
-
-    HandleAbort();
+    return NULL;
+    //#ifdef PANIC
+    //*logofs << "DecodeBuffer: PANIC! Assertion failed. Error [P] "
+    //        << "in decodeMemory() " << "with length " << numBytes
+    //        << " and " << (end_ - nextSrc_)
+    //        << " bytes remaining.\n" << logofs_flush;
+    //#endif
+    //cerr << "Error" << ": Failure decoding data in context [P].\n";
+    //HandleAbort();
   }
 
   nextSrc_ += numBytes;
--- a/nxcomp/DecodeBuffer.h
+++ b/nxcomp/DecodeBuffer.h
@@ -30,7 +30,8 @@
 #include "ActionCacheCompat.h"
 #include "PositionCacheCompat.h"
 
-#define DECODE_BUFFER_OVERFLOW_SIZE        4194304
+// See WriteBuffer.h and EncodeBuffer.h
+#define DECODE_BUFFER_OVERFLOW_SIZE        104857600
 
 #define DECODE_BUFFER_POSTFIX_SIZE         1
 
--- a/nxcomp/EncodeBuffer.h
+++ b/nxcomp/EncodeBuffer.h
@@ -33,10 +33,10 @@
 //
 // This should match the maximum size of
 // a single message added to write buffer
-// (see WriteBuffer.h).
+// (see WriteBuffer.h and DecodeBuffer.h).
 //
 
-#define ENCODE_BUFFER_OVERFLOW_SIZE       4194304
+#define ENCODE_BUFFER_OVERFLOW_SIZE       104857600
 
 //
 // Adjust for the control messages and the
--- a/nxcomp/SequenceQueue.h
+++ b/nxcomp/SequenceQueue.h
@@ -18,6 +18,22 @@
 #ifndef SequenceQueue_H
 #define SequenceQueue_H
 
+inline int SequenceNumber_x_gt_y(unsigned int x, unsigned int y)
+{
+  // For two sequence numbers x and y, determine whether (x > y).
+  // Sequence numbers are the trailing 16 bits of a bigger number:
+  // need to handle wraparound, e.g. 0 is 65536, just after 65535.
+  if (x != (x & 0x00ffff)) return 0;
+  if (y != (y & 0x00ffff)) return 0;
+  // Closeness when comparison makes sense: arbitrarily set at 16*1024
+  if ((x > y) && ((x-y) < 16*1024)) return 1;
+  // Wrapped value
+  unsigned int w = x + 64*1024;
+  // We know that w>y but test left for symmetry
+  if ((w > y) && ((w-y) < 16*1024)) return 1;
+  return 0;
+}
+
 //
 // List of outstanding request messages which
 // are waiting for a reply. This class is used
--- a/nxcomp/ServerChannel.cpp
+++ b/nxcomp/ServerChannel.cpp
@@ -104,7 +104,8 @@
 //
 
 #define HIDE_MIT_SHM_EXTENSION
-#define HIDE_BIG_REQUESTS_EXTENSION
+// HIDE_BIG_REQUESTS_EXTENSION : No good to hide, some clients may send crap instead...
+#undef  HIDE_BIG_REQUESTS_EXTENSION
 #define HIDE_XFree86_Bigfont_EXTENSION
 #undef  HIDE_SHAPE_EXTENSION
 #undef  HIDE_XKEYBOARD_EXTENSION
@@ -1412,6 +1413,9 @@
 
         unsigned int inputSequence = GetUINT(inputMessage + 2, bigEndian_);
 
+        // Sometimes we get inputSequence=0 or =256 when inputOpcode=11=X_UnmapSubwindows
+        // Seems weird... but is "normal" and is to be accepted.
+
         //
         // Check if this is an event which we can discard.
         //
@@ -1905,6 +1909,12 @@
           break;
         default:
           {
+            // BEWARE: not only inputOpcode == GenericEvent but also
+            // others not handled above, at least:
+            //   GraphicsExpose    13
+            //   MapRequest        20
+            //   ConfigureRequest  23
+            // and any beyond LASTEvent.
             #ifdef TEST
             *logofs << "handleRead: Using generic event compression "
                     << "for OPCODE#" << (unsigned int) inputOpcode
@@ -1919,6 +1929,16 @@
               encodeBuffer.encodeCachedValue(GetUINT(inputMessage + i * 2 + 4, bigEndian_),
                                  16, *serverCache_ -> genericEventIntCache[i]);
             }
+            // Handle "X Generic Event Extension"
+            // Cannot cache extra data...
+// FIXME: BUG ALERT: is it OK to have the first 32 bytes cached, but not the rest?
+            if (inputOpcode == GenericEvent && inputLength > 32)
+            {
+              for (unsigned int i = 14; i < ((inputLength-4)>>1); i++)
+              {
+                encodeBuffer.encodeValue(GetUINT(inputMessage + i * 2 + 4, bigEndian_), 16);
+              }
+            }
           }
 
         } // switch (inputOpcode)...
@@ -3756,7 +3776,7 @@
           }
 
           unsigned int numPoints;
-          decodeBuffer.decodeValue(numPoints, 16, 4);
+          decodeBuffer.decodeValue(numPoints, 32, 4);
           outputLength = (numPoints << 2) + 12;
           outputMessage = writeBuffer_.addMessage(outputLength);
           unsigned int relativeCoordMode;
@@ -3802,7 +3822,7 @@
           }
 
           unsigned int numPoints;
-          decodeBuffer.decodeValue(numPoints, 16, 4);
+          decodeBuffer.decodeValue(numPoints, 32, 4);
           outputLength = (numPoints << 2) + 12;
           outputMessage = writeBuffer_.addMessage(outputLength);
           unsigned int relativeCoordMode;
@@ -3839,7 +3859,7 @@
       case X_PolyRectangle:
         {
           unsigned int numRectangles;
-          decodeBuffer.decodeValue(numRectangles, 16, 3);
+          decodeBuffer.decodeValue(numRectangles, 32, 3);
           outputLength = (numRectangles << 3) + 12;
           outputMessage = writeBuffer_.addMessage(outputLength);
           decodeBuffer.decodeXidValue(value, clientCache_ -> drawableCache);
@@ -3869,7 +3889,7 @@
           }
 
           unsigned int numSegments;
-          decodeBuffer.decodeValue(numSegments, 16, 4);
+          decodeBuffer.decodeValue(numSegments, 32, 4);
           outputLength = (numSegments << 3) + 12;
           outputMessage = writeBuffer_.addMessage(outputLength);
           decodeBuffer.decodeXidValue(value, clientCache_ -> drawableCache);
@@ -4590,7 +4610,29 @@
 
         *outputMessage = (unsigned char) outputOpcode;
 
-        PutUINT(outputLength >> 2, outputMessage + 2, bigEndian_);
+        if (outputLength < 4*64*1024)
+          PutUINT(outputLength >> 2, outputMessage + 2, bigEndian_);
+        else
+        {
+          // Handle BIG-REQUESTS
+          PutUINT(0, outputMessage + 2, bigEndian_);
+// FIXME: BUG ALERT: following write may not work well,
+// particularly with un-flushed messages.
+if (outputMessage != writeBuffer_.getData())
+{
+*logofs << "PSz BUG handleWrite BIG-REQUESTS:"
+        << " have " << (unsigned int)(outputMessage - writeBuffer_.getData())
+        << " bytes in buffer"
+        << ", write immediate of 4-byte header will not work well"
+        << "\n" << logofs_flush;
+}
+// But, it works well enough in my testing...
+          // Write first four bytes
+          if (transport_ -> write(write_immediate, outputMessage, 4) < 0)
+              return -1;
+          // Replace with new 4-byte length
+          PutULONG(1 + (outputLength >> 2), outputMessage, bigEndian_);
+        }
 
         #if defined(TEST) || defined(OPCODES)
         *logofs << "handleWrite: Handled request OPCODE#"
@@ -5912,7 +5954,7 @@
   unsigned char opcode = *lastMotion_;
   unsigned int size = 32;
 
-  if (GetUINT(buffer + 2, bigEndian_) < serverSequence_)
+  if (SequenceNumber_x_gt_y(serverSequence_, GetUINT(buffer + 2, bigEndian_)))
   {
     PutUINT(serverSequence_, (unsigned char *) buffer + 2, bigEndian_);
   }
--- a/nxcomp/ServerReadBuffer.cpp
+++ b/nxcomp/ServerReadBuffer.cpp
@@ -108,14 +108,21 @@
     {
       dataLength = 32 + (GetULONG(start + 4, bigEndian_) << 2);
     }
+    else if (*start == GenericEvent && *(start+1) != 0)
+    {
+      // X Generic Event Extension
+      dataLength = 32 + (GetULONG(start + 4, bigEndian_) << 2);
+    }
     else
     {
       dataLength = 32;
     }
 
-    if (dataLength < 32)
+// See WRITE_BUFFER_OVERFLOW_SIZE elsewhere
+// and also ENCODE_BUFFER_OVERFLOW_SIZE DECODE_BUFFER_OVERFLOW_SIZE.
+    if (dataLength < 32 || dataLength > 100*1024*1024)
     {
-      #ifdef TEST
+      #ifdef WARNING
       *logofs << "ServerReadBuffer: WARNING! Assuming length 32 "
               << "for suspicious message of length " << dataLength
               << ".\n" << logofs_flush;
--- a/nxcomp/WriteBuffer.h
+++ b/nxcomp/WriteBuffer.h
@@ -32,8 +32,14 @@
 // This is likely to be a reply to a X_ListFonts where
 // user has a large amount of installed fonts.
 //
+// Used also for messages sent, and should accommodate any BIG-REQUESTS.
+// Value was 4MB = 4194304, changed to 100MB = 104857600.
+// See also sanity check limits (set same, to 100*1024*1024) in
+// ClientReadBuffer.cpp ServerReadBuffer.cpp and ClientChannel.cpp, and
+// ENCODE_BUFFER_OVERFLOW_SIZE DECODE_BUFFER_OVERFLOW_SIZE elsewhere.
+//
 
-#define WRITE_BUFFER_OVERFLOW_SIZE        4194304
+#define WRITE_BUFFER_OVERFLOW_SIZE        104857600
 
 class WriteBuffer
 {