Update vis/unvis modules from NetBSD
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 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656
diff --git a/COPYING b/COPYING
index c38a56b..4377959 100644
--- a/COPYING
+++ b/COPYING
@@ -122,7 +122,6 @@ Files:
src/strmode.c
src/strnstr.c
src/unvis.c
- src/vis.c
Copyright:
Copyright © 1980, 1982, 1986, 1989-1994
The Regents of the University of California. All rights reserved.
@@ -316,6 +315,16 @@ License: BSD-2-clause-NetBSD
POSSIBILITY OF SUCH DAMAGE.
Files:
+ src/vis.c
+Copyright:
+ Copyright © 1989, 1993
+ The Regents of the University of California. All rights reserved.
+ .
+ Copyright © 1999, 2005 The NetBSD Foundation, Inc.
+ All rights reserved.
+License: BSD-3-clause-Regents and BSD-2-clause-NetBSD
+
+Files:
include/bsd/sys/endian.h
man/byteorder.3bsd
man/closefrom.3bsd
diff --git a/include/bsd/vis.h b/include/bsd/vis.h
index ab5430c..d8b6635 100644
--- a/include/bsd/vis.h
+++ b/include/bsd/vis.h
@@ -1,3 +1,5 @@
+/* $NetBSD: vis.h,v 1.25 2017/04/23 01:57:36 christos Exp $ */
+
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -27,7 +29,6 @@
* SUCH DAMAGE.
*
* @(#)vis.h 8.1 (Berkeley) 6/2/93
- * $FreeBSD: src/include/vis.h,v 1.11 2003/10/30 10:40:49 phk Exp $
*/
#ifndef LIBBSD_VIS_H
@@ -38,25 +39,34 @@
/*
* to select alternate encoding format
*/
-#define VIS_OCTAL 0x01 /* use octal \ddd format */
-#define VIS_CSTYLE 0x02 /* use \[nrft0..] where appropriate */
+#define VIS_OCTAL 0x0001 /* use octal \ddd format */
+#define VIS_CSTYLE 0x0002 /* use \[nrft0..] where appropriate */
/*
* to alter set of characters encoded (default is to encode all
* non-graphic except space, tab, and newline).
*/
-#define VIS_SP 0x04 /* also encode space */
-#define VIS_TAB 0x08 /* also encode tab */
-#define VIS_NL 0x10 /* also encode newline */
+#define VIS_SP 0x0004 /* also encode space */
+#define VIS_TAB 0x0008 /* also encode tab */
+#define VIS_NL 0x0010 /* also encode newline */
#define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL)
-#define VIS_SAFE 0x20 /* only encode "unsafe" characters */
+#define VIS_SAFE 0x0020 /* only encode "unsafe" characters */
+#define VIS_DQ 0x8000 /* also encode double quotes */
/*
* other
*/
-#define VIS_NOSLASH 0x40 /* inhibit printing '\' */
-#define VIS_HTTPSTYLE 0x80 /* http-style escape % HEX HEX */
-#define VIS_GLOB 0x100 /* encode glob(3) magics */
+#define VIS_NOSLASH 0x0040 /* inhibit printing '\' */
+#define VIS_HTTP1808 0x0080 /* http-style escape % hex hex */
+#define VIS_HTTPSTYLE 0x0080 /* http-style escape % hex hex */
+#define VIS_MIMESTYLE 0x0100 /* mime-style escape = HEX HEX */
+#define VIS_HTTP1866 0x0200 /* http-style &#num; or &string; */
+#define VIS_NOESCAPE 0x0400 /* don't decode `\' */
+#define _VIS_END 0x0800 /* for unvis */
+#define VIS_GLOB 0x1000 /* encode glob(3) magic characters */
+#define VIS_SHELL 0x2000 /* encode shell special characters [not glob] */
+#define VIS_META (VIS_WHITE | VIS_GLOB | VIS_SHELL)
+#define VIS_NOLOCALE 0x4000 /* encode using the C locale */
/*
* unvis return codes
@@ -70,7 +80,7 @@
/*
* unvis flags
*/
-#define UNVIS_END 1 /* no more characters */
+#define UNVIS_END _VIS_END /* no more characters */
#ifdef LIBBSD_OVERLAY
#include <sys/cdefs.h>
@@ -80,12 +90,33 @@
__BEGIN_DECLS
char *vis(char *, int, int, int);
+char *nvis(char *, size_t, int, int, int);
+
+char *svis(char *, int, int, int, const char *);
+char *snvis(char *, size_t, int, int, int, const char *);
+
int strvis(char *, const char *, int);
+int stravis(char **, const char *, int);
+int strnvis(char *, size_t, const char *, int);
+
+int strsvis(char *, const char *, int, const char *);
+int strsnvis(char *, size_t, const char *, int, const char *);
+
int strvisx(char *, const char *, size_t, int);
-int strnvis(char *, const char *, size_t, int);
+int strnvisx(char *, size_t, const char *, size_t, int);
+int strenvisx(char *, size_t, const char *, size_t, int, int *);
+
+int strsvisx(char *, const char *, size_t, int, const char *);
+int strsnvisx(char *, size_t, const char *, size_t, int, const char *);
+int strsenvisx(char *, size_t, const char *, size_t , int, const char *,
+ int *);
+
int strunvis(char *, const char *);
+int strnunvis(char *, size_t, const char *);
+
int strunvisx(char *, const char *, int);
-ssize_t strnunvis(char *, const char *, size_t);
+int strnunvisx(char *, size_t, const char *, int);
+
int unvis(char *, int, int *, int);
__END_DECLS
diff --git a/man/unvis.3bsd b/man/unvis.3bsd
index 525eae8..693fbda 100644
--- a/man/unvis.3bsd
+++ b/man/unvis.3bsd
@@ -1,4 +1,4 @@
-.\" $OpenBSD: unvis.3,v 1.15 2005/07/22 03:16:58 jaredy Exp $
+.\" $NetBSD: unvis.3,v 1.29 2017/10/24 19:14:55 abhinav Exp $
.\"
.\" Copyright (c) 1989, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -27,13 +27,17 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd $Mdocdate: May 31 2007 $
+.\" @(#)unvis.3 8.2 (Berkeley) 12/11/93
+.\"
+.Dd March 12, 2011
.Dt UNVIS 3bsd
.Os
.Sh NAME
.Nm unvis ,
.Nm strunvis ,
-.Nm strnunvis
+.Nm strnunvis ,
+.Nm strunvisx ,
+.Nm strnunvisx
.Nd decode a visual representation of characters
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
@@ -44,88 +48,91 @@
.Xr libbsd 7
for include usage.)
.Ft int
-.Fn unvis "char *cp" "char c" "int *astate" "int flag"
+.Fn unvis "char *cp" "int c" "int *astate" "int flag"
+.Ft int
+.Fn strunvis "char *dst" "const char *src"
+.Ft int
+.Fn strnunvis "char *dst" "size_t dlen" "const char *src"
.Ft int
-.Fn strunvis "char *dst" "char *src"
-.Ft ssize_t
-.Fn strnunvis "char *dst" "char *src" "size_t size"
+.Fn strunvisx "char *dst" "const char *src" "int flag"
+.Ft int
+.Fn strnunvisx "char *dst" "size_t dlen" "const char *src" "int flag"
.Sh DESCRIPTION
The
.Fn unvis ,
.Fn strunvis
and
-.Fn strnunvis
-functions are used to decode a visual representation of characters,
-as produced by the
+.Fn strunvisx
+functions
+are used to decode a visual representation of characters, as produced
+by the
.Xr vis 3bsd
-function, back into the original form.
+function, back into
+the original form.
+.Pp
+The
.Fn unvis
-is called with successive characters in
-.Fa c
-until a valid
-sequence is recognized, at which time the decoded character is
-available at the character pointed to by
-.Fa cp .
+function is called with successive characters in
+.Ar c
+until a valid sequence is recognized, at which time the decoded
+character is available at the character pointed to by
+.Ar cp .
.Pp
+The
.Fn strunvis
-decodes the characters pointed to by
-.Fa src
+function decodes the characters pointed to by
+.Ar src
into the buffer pointed to by
-.Fa dst .
-.Pp
-.Fn strnunvis
-decodes the characters pointed to by
-.Fa src
-into the buffer pointed to by
-.Fa dst ,
-writing a maximum of
-.Fa size
-bytes.
+.Ar dst .
The
.Fn strunvis
function simply copies
-.Fa src
+.Ar src
to
-.Fa dst ,
+.Ar dst ,
decoding any escape sequences along the way,
and returns the number of characters placed into
-.Fa dst ,
+.Ar dst ,
or \-1 if an
invalid escape sequence was detected.
The size of
-.Fa dst
-should be
-equal to the size of
-.Fa src
+.Ar dst
+should be equal to the size of
+.Ar src
(that is, no expansion takes place during decoding).
+.Pp
+The
+.Fn strunvisx
+function does the same as the
.Fn strunvis
-terminates the destination string with a trailing NUL byte;
-.Fn strnunvis
-does so if
-.Fa size
-is larger than 0.
+function,
+but it allows you to add a flag that specifies the style the string
+.Ar src
+is encoded with.
+Currently, the supported flags are:
+.Dv VIS_HTTPSTYLE
+and
+.Dv VIS_MIMESTYLE .
.Pp
The
.Fn unvis
-function implements a state machine that can be used to decode an arbitrary
-stream of bytes.
+function implements a state machine that can be used to decode an
+arbitrary stream of bytes.
All state associated with the bytes being decoded is stored outside the
.Fn unvis
function (that is, a pointer to the state is passed in), so
calls decoding different streams can be freely intermixed.
-To start decoding a stream of bytes, first initialize an integer
-to zero.
+To start decoding a stream of bytes, first initialize an integer to zero.
Call
.Fn unvis
with each successive byte, along with a pointer
to this integer, and a pointer to a destination character.
-.Sh RETURN VALUES
The
.Fn unvis
function has several return codes that must be handled properly.
They are:
.Bl -tag -width UNVIS_VALIDPUSH
-.It Li \&0 (zero)
+.It Li \&0 No (zero)
Another character is necessary; nothing has been recognized yet.
.It Dv UNVIS_VALID
A valid character has been recognized and is available at the location
@@ -140,30 +147,41 @@ however, the character currently passed in should be passed in again.
A valid sequence was detected, but no character was produced.
This return code is necessary to indicate a logical break between characters.
.It Dv UNVIS_SYNBAD
-An invalid escape sequence was detected, or the decoder is in an
-unknown state.
+An invalid escape sequence was detected, or the decoder is in an unknown state.
The decoder is placed into the starting state.
.El
.Pp
When all bytes in the stream have been processed, call
.Fn unvis
-one more time with
-.Fa flag
-set to
+one more time with flag set to
.Dv UNVIS_END
to extract any remaining character (the character passed in is ignored).
.Pp
The
-.Fn strunvis
-function returns the number of bytes written (not counting
-the trailing NUL byte) or \-1 if an error occurred.
+.Fa flag
+argument is also used to specify the encoding style of the source.
+If set to
+.Dv VIS_HTTPSTYLE
+or
+.Dv VIS_HTTP1808 ,
+.Fn unvis
+will decode URI strings as specified in RFC 1808.
+If set to
+.Dv VIS_HTTP1866 ,
+.Fn unvis
+will decode entity references and numeric character references
+as specified in RFC 1866.
+If set to
+.Dv VIS_MIMESTYLE ,
+.Fn unvis
+will decode MIME Quoted-Printable strings as specified in RFC 2045.
+If set to
+.Dv VIS_NOESCAPE ,
+.Fn unvis
+will not decode
+.Ql \e
+quoted characters.
.Pp
-The
-.Fn strnunvis
-function returns the number of bytes (not counting the trailing NUL byte)
-that would be needed to fully convert the input string, or \-1 if an
-error occurred.
-.Sh EXAMPLES
The following code fragment illustrates a proper use of
.Fn unvis .
.Bd -literal -offset indent
@@ -177,25 +195,72 @@ again:
case UNVIS_NOCHAR:
break;
case UNVIS_VALID:
- (void) putchar(out);
+ (void)putchar(out);
break;
case UNVIS_VALIDPUSH:
- (void) putchar(out);
+ (void)putchar(out);
goto again;
case UNVIS_SYNBAD:
- (void)fprintf(stderr, "bad sequence!\en");
- exit(1);
+ errx(EXIT_FAILURE, "Bad character sequence!");
}
}
-if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID)
- (void) putchar(out);
+if (unvis(&out, '\e0', &state, UNVIS_END) == UNVIS_VALID)
+ (void)putchar(out);
.Ed
+.Sh ERRORS
+The functions
+.Fn strunvis ,
+.Fn strnunvis ,
+.Fn strunvisx ,
+and
+.Fn strnunvisx
+will return \-1 on error and set
+.Va errno
+to:
+.Bl -tag -width Er
+.It Bq Er EINVAL
+An invalid escape sequence was detected, or the decoder is in an unknown state.
+.El
+.Pp
+In addition the functions
+.Fn strnunvis
+and
+.Fn strnunvisx
+will can also set
+.Va errno
+on error to:
+.Bl -tag -width Er
+.It Bq Er ENOSPC
+Not enough space to perform the conversion.
+.El
.Sh SEE ALSO
.Xr unvis 1 ,
.Xr vis 1 ,
.Xr vis 3bsd
+.Rs
+.%A R. Fielding
+.%T Relative Uniform Resource Locators
+.%O RFC1808
+.Re
.Sh HISTORY
The
.Fn unvis
-function first appeared in
+function
+first appeared in
.Bx 4.4 .
+The
+.Fn strnunvis
+and
+.Fn strnunvisx
+functions appeared in
+.Nx 6.0 .
+.Sh BUGS
+The names
+.Dv VIS_HTTP1808
+and
+.Dv VIS_HTTP1866
+are wrong.
+Percent-encoding was defined in RFC 1738, the original RFC for URL.
+RFC 1866 defines HTML 2.0, an application of SGML, from which it
+inherits concepts of numeric character references and entity
+references.
diff --git a/man/vis.3bsd b/man/vis.3bsd
index 2215abe..25280a8 100644
--- a/man/vis.3bsd
+++ b/man/vis.3bsd
@@ -1,4 +1,4 @@
-.\" $OpenBSD: vis.3,v 1.23 2005/08/28 19:51:27 millert Exp $
+.\" $NetBSD: vis.3,v 1.49 2017/08/05 20:22:29 wiz Exp $
.\"
.\" Copyright (c) 1989, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -27,53 +27,87 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd $Mdocdate: May 31 2007 $
+.\" @(#)vis.3 8.1 (Berkeley) 6/9/93
+.\"
+.Dd April 22, 2017
.Dt VIS 3bsd
.Os
.Sh NAME
.Nm vis ,
+.Nm nvis ,
.Nm strvis ,
+.Nm stravis ,
.Nm strnvis ,
-.Nm strvisx
+.Nm strvisx ,
+.Nm strnvisx ,
+.Nm strenvisx ,
+.Nm svis ,
+.Nm snvis ,
+.Nm strsvis ,
+.Nm strsnvis ,
+.Nm strsvisx ,
+.Nm strsnvisx ,
+.Nm strsenvisx
.Nd visually encode characters
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In stdlib.h
.In vis.h
(See
.Xr libbsd 7
for include usage.)
.Ft char *
.Fn vis "char *dst" "int c" "int flag" "int nextc"
+.Ft char *
+.Fn nvis "char *dst" "size_t dlen" "int c" "int flag" "int nextc"
.Ft int
.Fn strvis "char *dst" "const char *src" "int flag"
.Ft int
-.Fn strnvis "char *dst" "const char *src" "size_t size" "int flag"
+.Fn stravis "char **dst" "const char *src" "int flag"
+.Ft int
+.Fn strnvis "char *dst" "size_t dlen" "const char *src" "int flag"
.Ft int
.Fn strvisx "char *dst" "const char *src" "size_t len" "int flag"
+.Ft int
+.Fn strnvisx "char *dst" "size_t dlen" "const char *src" "size_t len" "int flag"
+.Ft int
+.Fn strenvisx "char *dst" "size_t dlen" "const char *src" "size_t len" "int flag" "int *cerr_ptr"
+.Ft char *
+.Fn svis "char *dst" "int c" "int flag" "int nextc" "const char *extra"
+.Ft char *
+.Fn snvis "char *dst" "size_t dlen" "int c" "int flag" "int nextc" "const char *extra"
+.Ft int
+.Fn strsvis "char *dst" "const char *src" "int flag" "const char *extra"
+.Ft int
+.Fn strsnvis "char *dst" "size_t dlen" "const char *src" "int flag" "const char *extra"
+.Ft int
+.Fn strsvisx "char *dst" "const char *src" "size_t len" "int flag" "const char *extra"
+.Ft int
+.Fn strsnvisx "char *dst" "size_t dlen" "const char *src" "size_t len" "int flag" "const char *extra"
+.Ft int
+.Fn strsenvisx "char *dst" "size_t dlen" "const char *src" "size_t len" "int flag" "const char *extra" "int *cerr_ptr"
.Sh DESCRIPTION
The
.Fn vis
-function copies into
+function
+copies into
.Fa dst
a string which represents the character
.Fa c .
If
.Fa c
needs no encoding, it is copied in unaltered.
-The string is NUL terminated and a pointer to the end of the string is
+The string is null terminated, and a pointer to the end of the string is
returned.
The maximum length of any encoding is four
-characters (not including the trailing NUL);
+bytes (not including the trailing
+.Dv NUL ) ;
thus, when
encoding a set of characters into a buffer, the size of the buffer should
-be four times the number of characters encoded, plus one for the trailing
-NUL.
-The
-.Fa flag
-parameter is used for altering the default range of
+be four times the number of bytes encoded, plus one for the trailing
+.Dv NUL .
+The flag parameter is used for altering the default range of
characters considered for encoding and for altering the visual
representation.
The additional character,
@@ -84,9 +118,11 @@ encoding format (explained below).
.Pp
The
.Fn strvis ,
-.Fn strnvis
+.Fn stravis ,
+.Fn strnvis ,
+.Fn strvisx ,
and
-.Fn strvisx
+.Fn strnvisx
functions copy into
.Fa dst
a visual representation of
@@ -94,89 +130,153 @@ the string
.Fa src .
The
.Fn strvis
-function encodes characters from
-.Fa src
-up to the first NUL.
-The
+and
.Fn strnvis
-function encodes characters from
+functions encode characters from
.Fa src
-up to the first NUL or the end of
-.Fa dst ,
-as indicated by
-.Fa size .
+up to the
+first
+.Dv NUL .
The
.Fn strvisx
-function encodes exactly
+and
+.Fn strnvisx
+functions encode exactly
.Fa len
characters from
.Fa src
(this
-is useful for encoding a block of data that may contain NULs).
-All three forms NUL terminate
-.Fa dst ,
-except for
-.Fn strnvis
-when
-.Fa size
-is zero, in which case
-.Fa dst
-is not touched.
-For
-.Fn strvis
-and
-.Fn strvisx ,
-the size of
+is useful for encoding a block of data that may contain
+.Dv NUL Ns 's ) .
+Both forms
+.Dv NUL
+terminate
+.Fa dst .
+The size of
.Fa dst
must be four times the number
-of characters encoded from
+of bytes encoded from
.Fa src
-(plus one for the NUL).
-.Fn strvis
-and
-.Fn strvisx
-return the number of characters in
+(plus one for the
+.Dv NUL ) .
+Both
+forms return the number of characters in
.Fa dst
-(not including the trailing NUL).
-.Fn strnvis
-returns the length that
+(not including the trailing
+.Dv NUL ) .
+The
+.Fn stravis
+function allocates space dynamically to hold the string.
+The
+.Dq Nm n
+versions of the functions also take an additional argument
+.Fa dlen
+that indicates the length of the
.Fa dst
-would become if it were of unlimited size (similar to
-.Xr snprintf 3
-or
-.Xr strlcpy 3bsd ) .
-This can be used to detect truncation but it also means that
-the return value of
+buffer.
+If
+.Fa dlen
+is not large enough to fit the converted string then the
.Fn strnvis
-must not be used without checking it against
-.Fa size .
+and
+.Fn strnvisx
+functions return \-1 and set
+.Va errno
+to
+.Dv ENOSPC .
+The
+.Fn strenvisx
+function takes an additional argument,
+.Fa cerr_ptr ,
+that is used to pass in and out a multibyte conversion error flag.
+This is useful when processing single characters at a time when
+it is possible that the locale may be set to something other
+than the locale of the characters in the input data.
+.Pp
+The functions
+.Fn svis ,
+.Fn snvis ,
+.Fn strsvis ,
+.Fn strsnvis ,
+.Fn strsvisx ,
+.Fn strsnvisx ,
+and
+.Fn strsenvisx
+correspond to
+.Fn vis ,
+.Fn nvis ,
+.Fn strvis ,
+.Fn strnvis ,
+.Fn strvisx ,
+.Fn strnvisx ,
+and
+.Fn strenvisx
+but have an additional argument
+.Fa extra ,
+pointing to a
+.Dv NUL
+terminated list of characters.
+These characters will be copied encoded or backslash-escaped into
+.Fa dst .
+These functions are useful e.g. to remove the special meaning
+of certain characters to shells.
.Pp
The encoding is a unique, invertible representation composed entirely of
graphic characters; it can be decoded back into the original form using
the
-.Xr unvis 3bsd
-or
+.Xr unvis 3bsd ,
.Xr strunvis 3bsd
+or
+.Xr strnunvis 3bsd
functions.
.Pp
There are two parameters that can be controlled: the range of
-characters that are encoded, and the type
-of representation used.
-By default, all non-graphic characters
-except space, tab, and newline are encoded
-(see
+characters that are encoded (applies only to
+.Fn vis ,
+.Fn nvis ,
+.Fn strvis ,
+.Fn strnvis ,
+.Fn strvisx ,
+and
+.Fn strnvisx ) ,
+and the type of representation used.
+By default, all non-graphic characters,
+except space, tab, and newline are encoded (see
.Xr isgraph 3 ) .
The following flags
alter this:
.Bl -tag -width VIS_WHITEX
+.It Dv VIS_DQ
+Also encode double quotes
.It Dv VIS_GLOB
-Also encode magic characters recognized by
-.Xr glob 3
-.Pf ( Ql * ,
+Also encode the magic characters
+.Ql ( * ,
.Ql \&? ,
-.Ql \&[ )
+.Ql \&[ ,
and
-.Ql # .
+.Ql # )
+recognized by
+.Xr glob 3 .
+.It Dv VIS_SHELL
+Also encode the meta characters used by shells (in addition to the glob
+characters):
+.Ql ( ' ,
+.Ql ` ,
+.Ql \&" ,
+.Ql \&; ,
+.Ql & ,
+.Ql < ,
+.Ql > ,
+.Ql \&( ,
+.Ql \&) ,
+.Ql \&| ,
+.Ql \&] ,
+.Ql \e ,
+.Ql $ ,
+.Ql \&! ,
+.Ql \&^ ,
+and
+.Ql ~ ) .
.It Dv VIS_SP
Also encode space.
.It Dv VIS_TAB
@@ -185,34 +285,56 @@ Also encode tab.
Also encode newline.
.It Dv VIS_WHITE
Synonym for
-.Dv VIS_SP
-\&|
-.Dv VIS_TAB
-\&|
-.Dv VIS_NL .
+.Dv VIS_SP | VIS_TAB | VIS_NL .
+.It Dv VIS_META
+Synonym for
+.Dv VIS_WHITE | VIS_GLOB | VIS_SHELL .
.It Dv VIS_SAFE
Only encode
.Dq unsafe
characters.
-These are control characters which may cause common terminals to perform
+Unsafe means control characters which may cause common terminals to perform
unexpected functions.
-Currently this form allows space,
-tab, newline, backspace, bell, and return -- in addition
-to all graphic characters -- unencoded.
+Currently this form allows space, tab, newline, backspace, bell, and
+return \(em in addition to all graphic characters \(em unencoded.
.El
.Pp
-There are three forms of encoding.
-All forms use the backslash
+(The above flags have no effect for
+.Fn svis ,
+.Fn snvis ,
+.Fn strsvis ,
+.Fn strsnvis ,
+.Fn strsvisx ,
+and
+.Fn strsnvisx .
+When using these functions, place all graphic characters to be
+encoded in an array pointed to by
+.Fa extra .
+In general, the backslash character should be included in this array, see the
+warning on the use of the
+.Dv VIS_NOSLASH
+flag below).
+.Pp
+There are six forms of encoding.
+All forms use the backslash character
.Ql \e
-character to introduce a special
-sequence; two backslashes are used to represent a real backslash.
+to introduce a special
+sequence; two backslashes are used to represent a real backslash,
+except
+.Dv VIS_HTTPSTYLE
+that uses
+.Ql % ,
+or
+.Dv VIS_MIMESTYLE
+that uses
+.Ql = .
These are the visual formats:
.Bl -tag -width VIS_CSTYLE
.It (default)
Use an
.Ql M
to represent meta characters (characters with the 8th
-bit set), and use a caret
+bit set), and use caret
.Ql ^
to represent control characters (see
.Xr iscntrl 3 ) .
@@ -256,27 +378,27 @@ space.
.It Dv \e240
Represents Meta-space.
.El
-.Pp
.It Dv VIS_CSTYLE
Use C-style backslash sequences to represent standard non-printable
characters.
The following sequences are used to represent the indicated characters:
.Bd -unfilled -offset indent
-.Li \ea Tn - BEL No (007)
-.Li \eb Tn - BS No (010)
-.Li \ef Tn - NP No (014)
-.Li \en Tn - NL No (012)
-.Li \er Tn - CR No (015)
-.Li \es Tn - SP No (040)
-.Li \et Tn - HT No (011)
-.Li \ev Tn - VT No (013)
-.Li \e0 Tn - NUL No (000)
+.Li \ea Tn \(em BEL No (007)
+.Li \eb Tn \(em BS No (010)
+.Li \ef Tn \(em NP No (014)
+.Li \en Tn \(em NL No (012)
+.Li \er Tn \(em CR No (015)
+.Li \es Tn \(em SP No (040)
+.Li \et Tn \(em HT No (011)
+.Li \ev Tn \(em VT No (013)
+.Li \e0 Tn \(em NUL No (000)
.Ed
.Pp
When using this format, the
.Fa nextc
-parameter is looked at to determine
-if a NUL character can be encoded as
+parameter is looked at to determine if a
+.Dv NUL
+character can be encoded as
.Ql \e0
instead of
.Ql \e000 .
@@ -284,13 +406,36 @@ If
.Fa nextc
is an octal digit, the latter representation is used to
avoid ambiguity.
+.Pp
+Non-printable characters without C-style
+backslash sequences use the default representation.
.It Dv VIS_OCTAL
Use a three digit octal sequence.
The form is
.Ql \eddd
where
-.Ar d
+.Em d
represents an octal digit.
+.It Dv VIS_CSTYLE \&| Dv VIS_OCTAL
+Same as
+.Dv VIS_CSTYLE
+except that non-printable characters without C-style
+backslash sequences use a three digit octal sequence.
+.It Dv VIS_HTTPSTYLE
+Use URI encoding as described in RFC 1738.
+The form is
+.Ql %xx
+where
+.Em x
+represents a lower case hexadecimal digit.
+.It Dv VIS_MIMESTYLE
+Use MIME Quoted-Printable encoding as described in RFC 2045, only don't
+break lines and don't handle CRLF.
+The form is
+.Ql =XX
+where
+.Em X
+represents an upper case hexadecimal digit.
.El
.Pp
There is one additional flag,
@@ -304,21 +449,112 @@ meta characters as
.Ql M-C ) .
With this flag set, the encoding is
ambiguous and non-invertible.
+.Sh MULTIBYTE CHARACTER SUPPORT
+These functions support multibyte character input.
+The encoding conversion is influenced by the setting of the
+.Ev LC_CTYPE
+environment variable which defines the set of characters
+that can be copied without encoding.
+.Pp
+If
+.Dv VIS_NOLOCALE
+is set, processing is done assuming the C locale and overriding
+any other environment settings.
+.Pp
+When 8-bit data is present in the input,
+.Ev LC_CTYPE
+must be set to the correct locale or to the C locale.
+If the locales of the data and the conversion are mismatched,
+multibyte character recognition may fail and encoding will be performed
+byte-by-byte instead.
+.Pp
+As noted above,
+.Fa dst
+must be four times the number of bytes processed from
+.Fa src .
+But note that each multibyte character can be up to
+.Dv MB_LEN_MAX
+bytes
+.\" (see
+.\" .Xr multibyte 3 )
+so in terms of multibyte characters,
+.Fa dst
+must be four times
+.Dv MB_LEN_MAX
+times the number of characters processed from
+.Fa src .
+.Sh ENVIRONMENT
+.Bl -tag -width ".Ev LC_CTYPE"
+.It Ev LC_CTYPE
+Specify the locale of the input data.
+Set to C if the input data locale is unknown.
+.El
+.Sh ERRORS
+The functions
+.Fn nvis
+and
+.Fn snvis
+will return
+.Dv NULL
+and the functions
+.Fn strnvis ,
+.Fn strnvisx ,
+.Fn strsnvis ,
+and
+.Fn strsnvisx ,
+will return \-1 when the
+.Fa dlen
+destination buffer size is not enough to perform the conversion while
+setting
+.Va errno
+to:
+.Bl -tag -width ".Bq Er ENOSPC"
+.It Bq Er ENOSPC
+The destination buffer size is not large enough to perform the conversion.
+.El
.Sh SEE ALSO
.Xr unvis 1 ,
.Xr vis 1 ,
-.Xr snprintf 3 ,
-.Xr strlcpy 3bsd ,
+.Xr glob 3 ,
+.\" .Xr multibyte 3 ,
.Xr unvis 3bsd
+.Rs
+.%A T. Berners-Lee
+.%T Uniform Resource Locators (URL)
+.%O "RFC 1738"
+.Re
+.Rs
+.%T "Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies"
+.%O "RFC 2045"
+.Re
.Sh HISTORY
The
.Fn vis ,
-.Fn strvis
+.Fn strvis ,
and
.Fn strvisx
functions first appeared in
.Bx 4.4 .
The
-.Fn strnvis
-function first appeared in
-.Ox 2.9 .
+.Fn svis ,
+.Fn strsvis ,
+and
+.Fn strsvisx
+functions appeared in
+.Nx 1.5 .
+The buffer size limited versions of the functions
+.Po Fn nvis ,
+.Fn strnvis ,
+.Fn strnvisx ,
+.Fn snvis ,
+.Fn strsnvis ,
+and
+.Fn strsnvisx Pc
+appeared in
+.Nx 6.0
+and
+.Fx 9.2 .
+Multibyte character support was added in
+.Nx 7.0
+and
+.Fx 9.2 .
diff --git a/src/libbsd.map b/src/libbsd.map
index 4a41586..a91d522 100644
--- a/src/libbsd.map
+++ b/src/libbsd.map
@@ -144,4 +144,16 @@ LIBBSD_0.9 {
flopenat;
pidfile_fileno;
+
+ nvis;
+ snvis;
+ stravis;
+ strenvisx;
+ strnunvisx;
+ strsenvisx;
+ strsnvis;
+ strsnvisx;
+ strsvis;
+ strsvisx;
+ svis;
} LIBBSD_0.8;
diff --git a/src/unvis.c b/src/unvis.c
index c45a5f8..e16bd50 100644
--- a/src/unvis.c
+++ b/src/unvis.c
@@ -1,3 +1,5 @@
+/* $NetBSD: unvis.c,v 1.44 2014/09/26 15:43:36 roy Exp $ */
+
/*-
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
@@ -27,10 +29,22 @@
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#include <sys/types.h>
+
+#include <assert.h>
#include <ctype.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <errno.h>
#include <vis.h>
+#ifdef __weak_alias
+__weak_alias(strnunvisx,_strnunvisx)
+#endif
+
+#define _DIAGASSERT(x)
+
/*
* decode driven by state machine
*/
@@ -41,15 +55,128 @@
#define S_CTRL 4 /* control char started (^) */
#define S_OCTAL2 5 /* octal digit 2 */
#define S_OCTAL3 6 /* octal digit 3 */
-#define S_HEX2 7 /* hex digit 2 */
+#define S_HEX 7 /* mandatory hex digit */
+#define S_HEX1 8 /* http hex digit */
+#define S_HEX2 9 /* http hex digit 2 */
+#define S_MIME1 10 /* mime hex digit 1 */
+#define S_MIME2 11 /* mime hex digit 2 */
+#define S_EATCRNL 12 /* mime eating CRNL */
+#define S_AMP 13 /* seen & */
+#define S_NUMBER 14 /* collecting number */
+#define S_STRING 15 /* collecting string */
-#define S_HTTP 0x080 /* %HEXHEX escape */
+#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
+#define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
+#define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10))
-#define isoctal(c) \
- (((unsigned char)(c)) >= '0' && ((unsigned char)(c)) <= '7')
-#define ishex(c) \
- ((((unsigned char)(c)) >= '0' && ((unsigned char)(c)) <= '9') || \
- (((unsigned char)(c)) >= 'a' && ((unsigned char)(c)) <= 'f'))
+/*
+ * RFC 1866
+ */
+static const struct nv {
+ char name[7];
+ uint8_t value;
+} nv[] = {
+ { "AElig", 198 }, /* capital AE diphthong (ligature) */
+ { "Aacute", 193 }, /* capital A, acute accent */
+ { "Acirc", 194 }, /* capital A, circumflex accent */
+ { "Agrave", 192 }, /* capital A, grave accent */
+ { "Aring", 197 }, /* capital A, ring */
+ { "Atilde", 195 }, /* capital A, tilde */
+ { "Auml", 196 }, /* capital A, dieresis or umlaut mark */
+ { "Ccedil", 199 }, /* capital C, cedilla */
+ { "ETH", 208 }, /* capital Eth, Icelandic */
+ { "Eacute", 201 }, /* capital E, acute accent */
+ { "Ecirc", 202 }, /* capital E, circumflex accent */
+ { "Egrave", 200 }, /* capital E, grave accent */
+ { "Euml", 203 }, /* capital E, dieresis or umlaut mark */
+ { "Iacute", 205 }, /* capital I, acute accent */
+ { "Icirc", 206 }, /* capital I, circumflex accent */
+ { "Igrave", 204 }, /* capital I, grave accent */
+ { "Iuml", 207 }, /* capital I, dieresis or umlaut mark */
+ { "Ntilde", 209 }, /* capital N, tilde */
+ { "Oacute", 211 }, /* capital O, acute accent */
+ { "Ocirc", 212 }, /* capital O, circumflex accent */
+ { "Ograve", 210 }, /* capital O, grave accent */
+ { "Oslash", 216 }, /* capital O, slash */
+ { "Otilde", 213 }, /* capital O, tilde */
+ { "Ouml", 214 }, /* capital O, dieresis or umlaut mark */
+ { "THORN", 222 }, /* capital THORN, Icelandic */
+ { "Uacute", 218 }, /* capital U, acute accent */
+ { "Ucirc", 219 }, /* capital U, circumflex accent */
+ { "Ugrave", 217 }, /* capital U, grave accent */
+ { "Uuml", 220 }, /* capital U, dieresis or umlaut mark */
+ { "Yacute", 221 }, /* capital Y, acute accent */
+ { "aacute", 225 }, /* small a, acute accent */
+ { "acirc", 226 }, /* small a, circumflex accent */
+ { "acute", 180 }, /* acute accent */
+ { "aelig", 230 }, /* small ae diphthong (ligature) */
+ { "agrave", 224 }, /* small a, grave accent */
+ { "amp", 38 }, /* ampersand */
+ { "aring", 229 }, /* small a, ring */
+ { "atilde", 227 }, /* small a, tilde */
+ { "auml", 228 }, /* small a, dieresis or umlaut mark */
+ { "brvbar", 166 }, /* broken (vertical) bar */
+ { "ccedil", 231 }, /* small c, cedilla */
+ { "cedil", 184 }, /* cedilla */
+ { "cent", 162 }, /* cent sign */
+ { "copy", 169 }, /* copyright sign */
+ { "curren", 164 }, /* general currency sign */
+ { "deg", 176 }, /* degree sign */
+ { "divide", 247 }, /* divide sign */
+ { "eacute", 233 }, /* small e, acute accent */
+ { "ecirc", 234 }, /* small e, circumflex accent */
+ { "egrave", 232 }, /* small e, grave accent */
+ { "eth", 240 }, /* small eth, Icelandic */
+ { "euml", 235 }, /* small e, dieresis or umlaut mark */
+ { "frac12", 189 }, /* fraction one-half */
+ { "frac14", 188 }, /* fraction one-quarter */
+ { "frac34", 190 }, /* fraction three-quarters */
+ { "gt", 62 }, /* greater than */
+ { "iacute", 237 }, /* small i, acute accent */
+ { "icirc", 238 }, /* small i, circumflex accent */
+ { "iexcl", 161 }, /* inverted exclamation mark */
+ { "igrave", 236 }, /* small i, grave accent */
+ { "iquest", 191 }, /* inverted question mark */
+ { "iuml", 239 }, /* small i, dieresis or umlaut mark */
+ { "laquo", 171 }, /* angle quotation mark, left */
+ { "lt", 60 }, /* less than */
+ { "macr", 175 }, /* macron */
+ { "micro", 181 }, /* micro sign */
+ { "middot", 183 }, /* middle dot */
+ { "nbsp", 160 }, /* no-break space */
+ { "not", 172 }, /* not sign */
+ { "ntilde", 241 }, /* small n, tilde */
+ { "oacute", 243 }, /* small o, acute accent */
+ { "ocirc", 244 }, /* small o, circumflex accent */
+ { "ograve", 242 }, /* small o, grave accent */
+ { "ordf", 170 }, /* ordinal indicator, feminine */
+ { "ordm", 186 }, /* ordinal indicator, masculine */
+ { "oslash", 248 }, /* small o, slash */
+ { "otilde", 245 }, /* small o, tilde */
+ { "ouml", 246 }, /* small o, dieresis or umlaut mark */
+ { "para", 182 }, /* pilcrow (paragraph sign) */
+ { "plusmn", 177 }, /* plus-or-minus sign */
+ { "pound", 163 }, /* pound sterling sign */
+ { "quot", 34 }, /* double quote */
+ { "raquo", 187 }, /* angle quotation mark, right */
+ { "reg", 174 }, /* registered sign */
+ { "sect", 167 }, /* section sign */
+ { "shy", 173 }, /* soft hyphen */
+ { "sup1", 185 }, /* superscript one */
+ { "sup2", 178 }, /* superscript two */
+ { "sup3", 179 }, /* superscript three */
+ { "szlig", 223 }, /* small sharp s, German (sz ligature) */
+ { "thorn", 254 }, /* small thorn, Icelandic */
+ { "times", 215 }, /* multiply sign */
+ { "uacute", 250 }, /* small u, acute accent */
+ { "ucirc", 251 }, /* small u, circumflex accent */
+ { "ugrave", 249 }, /* small u, grave accent */
+ { "uml", 168 }, /* umlaut (dieresis) */
+ { "uuml", 252 }, /* small u, dieresis or umlaut mark */
+ { "yacute", 253 }, /* small y, acute accent */
+ { "yen", 165 }, /* yen sign */
+ { "yuml", 255 }, /* small y, dieresis or umlaut mark */
+};
/*
* unvis - decode characters previously encoded by vis
@@ -57,276 +184,367 @@
int
unvis(char *cp, int c, int *astate, int flag)
{
+ unsigned char uc = (unsigned char)c;
+ unsigned char st, ia, is, lc;
+
+/*
+ * Bottom 8 bits of astate hold the state machine state.
+ * Top 8 bits hold the current character in the http 1866 nv string decoding
+ */
+#define GS(a) ((a) & 0xff)
+#define SS(a, b) (((uint32_t)(a) << 24) | (b))
+#define GI(a) ((uint32_t)(a) >> 24)
+
+ _DIAGASSERT(cp != NULL);
+ _DIAGASSERT(astate != NULL);
+ st = GS(*astate);
if (flag & UNVIS_END) {
- if (*astate == S_OCTAL2 || *astate == S_OCTAL3) {
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ switch (st) {
+ case S_OCTAL2:
+ case S_OCTAL3:
+ case S_HEX2:
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
+ case S_GROUND:
+ return UNVIS_NOCHAR;
+ default:
+ return UNVIS_SYNBAD;
}
- return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
}
- switch (*astate & ~S_HTTP) {
+ switch (st) {
case S_GROUND:
*cp = 0;
- if (c == '\\') {
- *astate = S_START;
- return (0);
+ if ((flag & VIS_NOESCAPE) == 0 && c == '\\') {
+ *astate = SS(0, S_START);
+ return UNVIS_NOCHAR;
}
- if (flag & VIS_HTTPSTYLE && c == '%') {
- *astate = S_START | S_HTTP;
- return (0);
+ if ((flag & VIS_HTTP1808) && c == '%') {
+ *astate = SS(0, S_HEX1);
+ return UNVIS_NOCHAR;
+ }
+ if ((flag & VIS_HTTP1866) && c == '&') {
+ *astate = SS(0, S_AMP);
+ return UNVIS_NOCHAR;
+ }
+ if ((flag & VIS_MIMESTYLE) && c == '=') {
+ *astate = SS(0, S_MIME1);
+ return UNVIS_NOCHAR;
}
*cp = c;
- return (UNVIS_VALID);
+ return UNVIS_VALID;
case S_START:
- if (*astate & S_HTTP) {
- if (ishex(tolower(c))) {
- *cp = isdigit(c) ? (c - '0') : (tolower(c) - 'a');
- *astate = S_HEX2;
- return (0);
- }
- }
switch(c) {
case '\\':
*cp = c;
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
*cp = (c - '0');
- *astate = S_OCTAL2;
- return (0);
+ *astate = SS(0, S_OCTAL2);
+ return UNVIS_NOCHAR;
case 'M':
- *cp = 0200;
- *astate = S_META;
- return (0);
+ *cp = (char)0200;
+ *astate = SS(0, S_META);
+ return UNVIS_NOCHAR;
case '^':
- *astate = S_CTRL;
- return (0);
+ *astate = SS(0, S_CTRL);
+ return UNVIS_NOCHAR;
case 'n':
*cp = '\n';
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
case 'r':
*cp = '\r';
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
case 'b':
*cp = '\b';
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
case 'a':
*cp = '\007';
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
case 'v':
*cp = '\v';
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
case 't':
*cp = '\t';
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
case 'f':
*cp = '\f';
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
case 's':
*cp = ' ';
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
case 'E':
*cp = '\033';
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
+ case 'x':
+ *astate = SS(0, S_HEX);
+ return UNVIS_NOCHAR;
case '\n':
/*
* hidden newline
*/
- *astate = S_GROUND;
- return (UNVIS_NOCHAR);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_NOCHAR;
case '$':
/*
* hidden marker
*/
- *astate = S_GROUND;
- return (UNVIS_NOCHAR);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_NOCHAR;
+ default:
+ if (isgraph(c)) {
+ *cp = c;
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
+ }
}
- *astate = S_GROUND;
- return (UNVIS_SYNBAD);
+ goto bad;
case S_META:
if (c == '-')
- *astate = S_META1;
+ *astate = SS(0, S_META1);
else if (c == '^')
- *astate = S_CTRL;
- else {
- *astate = S_GROUND;
- return (UNVIS_SYNBAD);
- }
- return (0);
+ *astate = SS(0, S_CTRL);
+ else
+ goto bad;
+ return UNVIS_NOCHAR;
case S_META1:
- *astate = S_GROUND;
+ *astate = SS(0, S_GROUND);
*cp |= c;
- return (UNVIS_VALID);
+ return UNVIS_VALID;
case S_CTRL:
if (c == '?')
*cp |= 0177;
else
*cp |= c & 037;
- *astate = S_GROUND;
- return (UNVIS_VALID);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
case S_OCTAL2: /* second possible octal digit */
- if (isoctal(c)) {
+ if (isoctal(uc)) {
/*
* yes - and maybe a third
*/
*cp = (*cp << 3) + (c - '0');
- *astate = S_OCTAL3;
- return (0);
+ *astate = SS(0, S_OCTAL3);
+ return UNVIS_NOCHAR;
}
/*
* no - done with current sequence, push back passed char
*/
- *astate = S_GROUND;
- return (UNVIS_VALIDPUSH);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALIDPUSH;
case S_OCTAL3: /* third possible octal digit */
- *astate = S_GROUND;
- if (isoctal(c)) {
+ *astate = SS(0, S_GROUND);
+ if (isoctal(uc)) {
*cp = (*cp << 3) + (c - '0');
- return (UNVIS_VALID);
+ return UNVIS_VALID;
}
/*
* we were done, push back passed char
*/
- return (UNVIS_VALIDPUSH);
+ return UNVIS_VALIDPUSH;
- case S_HEX2: /* second mandatory hex digit */
- if (ishex(tolower(c))) {
- *cp = (isdigit(c) ? (*cp << 4) + (c - '0') : (*cp << 4) + (tolower(c) - 'a' + 10));
+ case S_HEX:
+ if (!isxdigit(uc))
+ goto bad;
+ /*FALLTHROUGH*/
+ case S_HEX1:
+ if (isxdigit(uc)) {
+ *cp = xtod(uc);
+ *astate = SS(0, S_HEX2);
+ return UNVIS_NOCHAR;
}
+ /*
+ * no - done with current sequence, push back passed char
+ */
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALIDPUSH;
+
+ case S_HEX2:
*astate = S_GROUND;
- return (UNVIS_VALID);
+ if (isxdigit(uc)) {
+ *cp = xtod(uc) | (*cp << 4);
+ return UNVIS_VALID;
+ }
+ return UNVIS_VALIDPUSH;
+
+ case S_MIME1:
+ if (uc == '\n' || uc == '\r') {
+ *astate = SS(0, S_EATCRNL);
+ return UNVIS_NOCHAR;
+ }
+ if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
+ *cp = XTOD(uc);
+ *astate = SS(0, S_MIME2);
+ return UNVIS_NOCHAR;
+ }
+ goto bad;
+
+ case S_MIME2:
+ if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
+ *astate = SS(0, S_GROUND);
+ *cp = XTOD(uc) | (*cp << 4);
+ return UNVIS_VALID;
+ }
+ goto bad;
+
+ case S_EATCRNL:
+ switch (uc) {
+ case '\r':
+ case '\n':
+ return UNVIS_NOCHAR;
+ case '=':
+ *astate = SS(0, S_MIME1);
+ return UNVIS_NOCHAR;
+ default:
+ *cp = uc;
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
+ }
+
+ case S_AMP:
+ *cp = 0;
+ if (uc == '#') {
+ *astate = SS(0, S_NUMBER);
+ return UNVIS_NOCHAR;
+ }
+ *astate = SS(0, S_STRING);
+ /*FALLTHROUGH*/
+
+ case S_STRING:
+ ia = *cp; /* index in the array */
+ is = GI(*astate); /* index in the string */
+ lc = is == 0 ? 0 : nv[ia].name[is - 1]; /* last character */
+
+ if (uc == ';')
+ uc = '\0';
+
+ for (; ia < __arraycount(nv); ia++) {
+ if (is != 0 && nv[ia].name[is - 1] != lc)
+ goto bad;
+ if (nv[ia].name[is] == uc)
+ break;
+ }
+
+ if (ia == __arraycount(nv))
+ goto bad;
+
+ if (uc != 0) {
+ *cp = ia;
+ *astate = SS(is + 1, S_STRING);
+ return UNVIS_NOCHAR;
+ }
+
+ *cp = nv[ia].value;
+ *astate = SS(0, S_GROUND);
+ return UNVIS_VALID;
+
+ case S_NUMBER:
+ if (uc == ';')
+ return UNVIS_VALID;
+ if (!isdigit(uc))
+ goto bad;
+ *cp += (*cp * 10) + uc - '0';
+ return UNVIS_NOCHAR;
default:
+ bad:
/*
* decoder in unknown state - (probably uninitialized)
*/
- *astate = S_GROUND;
- return (UNVIS_SYNBAD);
+ *astate = SS(0, S_GROUND);
+ return UNVIS_SYNBAD;
}
}
/*
- * strunvis - decode src into dst
+ * strnunvisx - decode src into dst
*
* Number of chars decoded into dst is returned, -1 on error.
* Dst is null terminated.
*/
int
-strunvis(char *dst, const char *src)
+strnunvisx(char *dst, size_t dlen, const char *src, int flag)
{
char c;
- char *start = dst;
+ char t = '\0', *start = dst;
int state = 0;
- while ((c = *src++)) {
- again:
- switch (unvis(dst, c, &state, 0)) {
+ _DIAGASSERT(src != NULL);
+ _DIAGASSERT(dst != NULL);
+#define CHECKSPACE() \
+ do { \
+ if (dlen-- == 0) { \
+ errno = ENOSPC; \
+ return -1; \
+ } \
+ } while (/*CONSTCOND*/0)
+
+ while ((c = *src++) != '\0') {
+ again:
+ switch (unvis(&t, c, &state, flag)) {
case UNVIS_VALID:
- dst++;
+ CHECKSPACE();
+ *dst++ = t;
break;
case UNVIS_VALIDPUSH:
- dst++;
+ CHECKSPACE();
+ *dst++ = t;
goto again;
case 0:
case UNVIS_NOCHAR:
break;
+ case UNVIS_SYNBAD:
+ errno = EINVAL;
+ return -1;
default:
- *dst = '\0';
- return (-1);
+ _DIAGASSERT(/*CONSTCOND*/0);
+ errno = EINVAL;
+ return -1;
}
}
- if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
- dst++;
+ if (unvis(&t, c, &state, UNVIS_END) == UNVIS_VALID) {
+ CHECKSPACE();
+ *dst++ = t;
+ }
+ CHECKSPACE();
*dst = '\0';
- return (dst - start);
+ return (int)(dst - start);
}
-ssize_t
-strnunvis(char *dst, const char *src, size_t sz)
+int
+strunvisx(char *dst, const char *src, int flag)
{
- char c, p;
- char *start = dst, *end = dst + sz - 1;
- int state = 0;
-
- if (sz > 0)
- *end = '\0';
- while ((c = *src++)) {
- again:
- switch (unvis(&p, c, &state, 0)) {
- case UNVIS_VALID:
- if (dst < end)
- *dst = p;
- dst++;
- break;
- case UNVIS_VALIDPUSH:
- if (dst < end)
- *dst = p;
- dst++;
- goto again;
- case 0:
- case UNVIS_NOCHAR:
- break;
- default:
- if (dst <= end)
- *dst = '\0';
- return (-1);
- }
- }
- if (unvis(&p, c, &state, UNVIS_END) == UNVIS_VALID) {
- if (dst < end)
- *dst = p;
- dst++;
- }
- if (dst <= end)
- *dst = '\0';
- return (dst - start);
+ return strnunvisx(dst, (size_t)~0, src, flag);
}
int
-strunvisx(char *dst, const char *src, int flag)
+strunvis(char *dst, const char *src)
{
- char c;
- char *start = dst;
- int state = 0;
+ return strnunvisx(dst, (size_t)~0, src, 0);
+}
- while ((c = *src++)) {
- again:
- switch (unvis(dst, c, &state, flag)) {
- case UNVIS_VALID:
- dst++;
- break;
- case UNVIS_VALIDPUSH:
- dst++;
- goto again;
- case 0:
- case UNVIS_NOCHAR:
- break;
- default:
- return (-1);
- }
- }
- if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
- dst++;
- *dst = '\0';
- return (dst - start);
+int
+strnunvis(char *dst, size_t dlen, const char *src)
+{
+ return strnunvisx(dst, dlen, src, 0);
}
diff --git a/src/vis.c b/src/vis.c
index c9a06e8..5599439 100644
--- a/src/vis.c
+++ b/src/vis.c
@@ -1,4 +1,5 @@
-/* $OpenBSD: vis.c,v 1.18 2005/08/29 18:38:41 otto Exp $ */
+/* $NetBSD: vis.c,v 1.74 2017/11/27 16:37:21 christos Exp $ */
+
/*-
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
@@ -28,220 +29,717 @@
* SUCH DAMAGE.
*/
+/*-
+ * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
#include <sys/types.h>
-#include <limits.h>
+#include <sys/param.h>
+
+#include <assert.h>
+#include <vis.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <wchar.h>
+#include <wctype.h>
+
+#ifdef __weak_alias
+__weak_alias(strvisx,_strvisx)
+#endif
+
#include <ctype.h>
-#include <string.h>
+#include <limits.h>
#include <stdio.h>
-#include <vis.h>
+#include <string.h>
-#define isoctal(c) \
- (((unsigned char)(c)) >= '0' && ((unsigned char)(c)) <= '7')
-#define isvisible(c) \
- (((unsigned int)(c) <= UCHAR_MAX && \
- isascii((unsigned char)(c)) && \
- (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \
- (flag & VIS_GLOB) == 0) && \
- isgraph((unsigned char)(c))) || \
- ((flag & VIS_SP) == 0 && (c) == ' ') || \
- ((flag & VIS_TAB) == 0 && (c) == '\t') || \
- ((flag & VIS_NL) == 0 && (c) == '\n') || \
- ((flag & VIS_SAFE) && ((c) == '\b' || \
- (c) == '\007' || (c) == '\r' || \
- isgraph((unsigned char)(c)))))
+#define _DIAGASSERT(x)
/*
- * vis - visually encode characters
+ * The reason for going through the trouble to deal with character encodings
+ * in vis(3), is that we use this to safe encode output of commands. This
+ * safe encoding varies depending on the character set. For example if we
+ * display ps output in French, we don't want to display French characters
+ * as M-foo.
*/
-char *
-vis(char *dst, int c, int flag, int nextc)
-{
- c = (unsigned char)c;
-
- if (flag & VIS_HTTPSTYLE) {
- /* Described in RFC 1808 */
- if (!(isalnum(c) /* alpha-numeric */
- /* safe */
- || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
- /* extra */
- || c == '!' || c == '*' || c == '\'' || c == '('
- || c == ')' || c == ',')) {
- *dst++ = '%';
- snprintf(dst, 4, (c < 16 ? "0%X" : "%X"), c);
- dst += 2;
- goto done;
+
+static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *);
+
+#undef BELL
+#define BELL L'\a'
+
+#if defined(LC_C_LOCALE)
+#define iscgraph(c) isgraph_l(c, LC_C_LOCALE)
+#else
+/* Keep it simple for now, no locale stuff */
+#define iscgraph(c) isgraph(c)
+#ifdef notyet
+#include <locale.h>
+static int
+iscgraph(int c) {
+ int rv;
+ char *ol;
+
+ ol = setlocale(LC_CTYPE, "C");
+ rv = isgraph(c);
+ if (ol)
+ setlocale(LC_CTYPE, ol);
+ return rv;
+}
+#endif
+#endif
+
+#define ISGRAPH(flags, c) \
+ (((flags) & VIS_NOLOCALE) ? iscgraph(c) : iswgraph(c))
+
+#define iswoctal(c) (((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7')
+#define iswwhite(c) (c == L' ' || c == L'\t' || c == L'\n')
+#define iswsafe(c) (c == L'\b' || c == BELL || c == L'\r')
+#define xtoa(c) L"0123456789abcdef"[c]
+#define XTOA(c) L"0123456789ABCDEF"[c]
+
+#define MAXEXTRAS 30
+
+static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~";
+static const wchar_t char_glob[] = L"*?[#";
+
+/*
+ * On NetBSD and glibc MB_LEN_MAX is currently > 8 which does not fit on any
+ * integer integral type and it is probably wrong, since currently the maximum
+ * number of bytes and character needs is 6. Until this is fixed, the
+ * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and
+ * the assertion is commented out.
+ */
+#if 0
+#ifndef CTASSERT
+#define CTASSERT(x) _CTASSERT(x, __LINE__)
+#define _CTASSERT(x, y) __CTASSERT(x, y)
+#define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1]
+#endif
+
+CTASSERT(MB_LEN_MAX <= sizeof(uint64_t));
+#endif
+
+/*
+ * This is do_hvis, for HTTP style (RFC 1808)
+ */
+static wchar_t *
+do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
+{
+ if (iswalnum(c)
+ /* safe */
+ || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
+ /* extra */
+ || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
+ || c == L',')
+ dst = do_svis(dst, c, flags, nextc, extra);
+ else {
+ *dst++ = L'%';
+ *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
+ *dst++ = xtoa((unsigned int)c & 0xf);
+ }
+
+ return dst;
+}
+
+/*
+ * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
+ * NB: No handling of long lines or CRLF.
+ */
+static wchar_t *
+do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
+{
+ if ((c != L'\n') &&
+ /* Space at the end of the line */
+ ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
+ /* Out of range */
+ (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
+ /* Specific char to be escaped */
+ wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
+ *dst++ = L'=';
+ *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
+ *dst++ = XTOA((unsigned int)c & 0xf);
+ } else
+ dst = do_svis(dst, c, flags, nextc, extra);
+ return dst;
+}
+
+/*
+ * Output single byte of multibyte character.
+ */
+static wchar_t *
+do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra)
+{
+ if (flags & VIS_CSTYLE) {
+ switch (c) {
+ case L'\n':
+ *dst++ = L'\\'; *dst++ = L'n';
+ return dst;
+ case L'\r':
+ *dst++ = L'\\'; *dst++ = L'r';
+ return dst;
+ case L'\b':
+ *dst++ = L'\\'; *dst++ = L'b';
+ return dst;
+ case BELL:
+ *dst++ = L'\\'; *dst++ = L'a';
+ return dst;
+ case L'\v':
+ *dst++ = L'\\'; *dst++ = L'v';
+ return dst;
+ case L'\t':
+ *dst++ = L'\\'; *dst++ = L't';
+ return dst;
+ case L'\f':
+ *dst++ = L'\\'; *dst++ = L'f';
+ return dst;
+ case L' ':
+ *dst++ = L'\\'; *dst++ = L's';
+ return dst;
+ case L'\0':
+ *dst++ = L'\\'; *dst++ = L'0';
+ if (iswoctal(nextc)) {
+ *dst++ = L'0';
+ *dst++ = L'0';
+ }
+ return dst;
+ /* We cannot encode these characters in VIS_CSTYLE
+ * because they special meaning */
+ case L'n':
+ case L'r':
+ case L'b':
+ case L'a':
+ case L'v':
+ case L't':
+ case L'f':
+ case L's':
+ case L'0':
+ case L'M':
+ case L'^':
+ case L'$': /* vis(1) -l */
+ break;
+ default:
+ if (ISGRAPH(flags, c) && !iswoctal(c)) {
+ *dst++ = L'\\';
+ *dst++ = c;
+ return dst;
+ }
}
}
+ if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
+ *dst++ = L'\\';
+ *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
+ *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
+ *dst++ = (c & 07) + L'0';
+ } else {
+ if ((flags & VIS_NOSLASH) == 0)
+ *dst++ = L'\\';
+
+ if (c & 0200) {
+ c &= 0177;
+ *dst++ = L'M';
+ }
+
+ if (iswcntrl(c)) {
+ *dst++ = L'^';
+ if (c == 0177)
+ *dst++ = L'?';
+ else
+ *dst++ = c + L'@';
+ } else {
+ *dst++ = L'-';
+ *dst++ = c;
+ }
+ }
+
+ return dst;
+}
- if ((flag & VIS_GLOB) &&
- (c == '*' || c == '?' || c == '[' || c == '#'))
- ;
- else if (isgraph(c) ||
- ((flag & VIS_SP) == 0 && c == ' ') ||
- ((flag & VIS_TAB) == 0 && c == '\t') ||
- ((flag & VIS_NL) == 0 && c == '\n') ||
- ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) {
+/*
+ * This is do_vis, the central code of vis.
+ * dst: Pointer to the destination buffer
+ * c: Character to encode
+ * flags: Flags word
+ * nextc: The character following 'c'
+ * extra: Pointer to the list of extra characters to be
+ * backslash-protected.
+ */
+static wchar_t *
+do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
+{
+ int iswextra, i, shft;
+ uint64_t bmsk, wmsk;
+
+ iswextra = wcschr(extra, c) != NULL;
+ if (!iswextra && (ISGRAPH(flags, c) || iswwhite(c) ||
+ ((flags & VIS_SAFE) && iswsafe(c)))) {
*dst++ = c;
- if (c == '\\' && (flag & VIS_NOSLASH) == 0)
- *dst++ = '\\';
- *dst = '\0';
- return (dst);
+ return dst;
}
- if (flag & VIS_CSTYLE) {
- switch(c) {
- case '\n':
- *dst++ = '\\';
- *dst++ = 'n';
- goto done;
- case '\r':
- *dst++ = '\\';
- *dst++ = 'r';
- goto done;
- case '\b':
- *dst++ = '\\';
- *dst++ = 'b';
- goto done;
- case '\a':
- *dst++ = '\\';
- *dst++ = 'a';
- goto done;
- case '\v':
- *dst++ = '\\';
- *dst++ = 'v';
- goto done;
- case '\t':
- *dst++ = '\\';
- *dst++ = 't';
- goto done;
- case '\f':
- *dst++ = '\\';
- *dst++ = 'f';
- goto done;
- case ' ':
- *dst++ = '\\';
- *dst++ = 's';
- goto done;
- case '\0':
- *dst++ = '\\';
- *dst++ = '0';
- if (isoctal(nextc)) {
- *dst++ = '0';
- *dst++ = '0';
- }
- goto done;
+ /* See comment in istrsenvisx() output loop, below. */
+ wmsk = 0;
+ for (i = sizeof(wmsk) - 1; i >= 0; i--) {
+ shft = i * NBBY;
+ bmsk = (uint64_t)0xffLL << shft;
+ wmsk |= bmsk;
+ if ((c & wmsk) || i == 0)
+ dst = do_mbyte(dst, (wint_t)(
+ (uint64_t)(c & bmsk) >> shft),
+ flags, nextc, iswextra);
+ }
+
+ return dst;
+}
+
+typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
+
+/*
+ * Return the appropriate encoding function depending on the flags given.
+ */
+static visfun_t
+getvisfun(int flags)
+{
+ if (flags & VIS_HTTPSTYLE)
+ return do_hvis;
+ if (flags & VIS_MIMESTYLE)
+ return do_mvis;
+ return do_svis;
+}
+
+/*
+ * Expand list of extra characters to not visually encode.
+ */
+static wchar_t *
+makeextralist(int flags, const char *src)
+{
+ wchar_t *dst, *d;
+ size_t len;
+ const wchar_t *s;
+
+ len = strlen(src);
+ if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
+ return NULL;
+
+ if ((flags & VIS_NOLOCALE) || mbstowcs(dst, src, len) == (size_t)-1) {
+ size_t i;
+ for (i = 0; i < len; i++)
+ dst[i] = (wchar_t)(u_char)src[i];
+ d = dst + len;
+ } else
+ d = dst + wcslen(dst);
+
+ if (flags & VIS_GLOB)
+ for (s = char_glob; *s; *d++ = *s++)
+ continue;
+
+ if (flags & VIS_SHELL)
+ for (s = char_shell; *s; *d++ = *s++)
+ continue;
+
+ if (flags & VIS_SP) *d++ = L' ';
+ if (flags & VIS_TAB) *d++ = L'\t';
+ if (flags & VIS_NL) *d++ = L'\n';
+ if (flags & VIS_DQ) *d++ = L'"';
+ if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
+ *d = L'\0';
+
+ return dst;
+}
+
+/*
+ * istrsenvisx()
+ * The main internal function.
+ * All user-visible functions call this one.
+ */
+static int
+istrsenvisx(char **mbdstp, size_t *dlen, const char *mbsrc, size_t mblength,
+ int flags, const char *mbextra, int *cerr_ptr)
+{
+ wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
+ size_t len, olen;
+ uint64_t bmsk, wmsk;
+ wint_t c;
+ visfun_t f;
+ int clen = 0, cerr, error = -1, i, shft;
+ char *mbdst, *mdst;
+ ssize_t mbslength, maxolen;
+
+ _DIAGASSERT(mbdstp != NULL);
+ _DIAGASSERT(mbsrc != NULL || mblength == 0);
+ _DIAGASSERT(mbextra != NULL);
+
+ mbslength = (ssize_t)mblength;
+ /*
+ * When inputing a single character, must also read in the
+ * next character for nextc, the look-ahead character.
+ */
+ if (mbslength == 1)
+ mbslength++;
+
+ /*
+ * Input (mbsrc) is a char string considered to be multibyte
+ * characters. The input loop will read this string pulling
+ * one character, possibly multiple bytes, from mbsrc and
+ * converting each to wchar_t in src.
+ *
+ * The vis conversion will be done using the wide char
+ * wchar_t string.
+ *
+ * This will then be converted back to a multibyte string to
+ * return to the caller.
+ */
+
+ /* Allocate space for the wide char strings */
+ psrc = pdst = extra = NULL;
+ mdst = NULL;
+ if ((psrc = calloc(mbslength + 1, sizeof(*psrc))) == NULL)
+ return -1;
+ if ((pdst = calloc((16 * mbslength) + 1, sizeof(*pdst))) == NULL)
+ goto out;
+ if (*mbdstp == NULL) {
+ if ((mdst = calloc((16 * mbslength) + 1, sizeof(*mdst))) == NULL)
+ goto out;
+ *mbdstp = mdst;
+ }
+
+ mbdst = *mbdstp;
+ dst = pdst;
+ src = psrc;
+
+ if (flags & VIS_NOLOCALE) {
+ /* Do one byte at a time conversion */
+ cerr = 1;
+ } else {
+ /* Use caller's multibyte conversion error flag. */
+ cerr = cerr_ptr ? *cerr_ptr : 0;
+ }
+
+ /*
+ * Input loop.
+ * Handle up to mblength characters (not bytes). We do not
+ * stop at NULs because we may be processing a block of data
+ * that includes NULs.
+ */
+ while (mbslength > 0) {
+ /* Convert one multibyte character to wchar_t. */
+ if (!cerr)
+ clen = mbtowc(src, mbsrc, MB_LEN_MAX);
+ if (cerr || clen < 0) {
+ /* Conversion error, process as a byte instead. */
+ *src = (wint_t)(u_char)*mbsrc;
+ clen = 1;
+ cerr = 1;
+ }
+ if (clen == 0) {
+ /*
+ * NUL in input gives 0 return value. process
+ * as single NUL byte and keep going.
+ */
+ clen = 1;
}
+ /* Advance buffer character pointer. */
+ src++;
+ /* Advance input pointer by number of bytes read. */
+ mbsrc += clen;
+ /* Decrement input byte count. */
+ mbslength -= clen;
}
- if (((c & 0177) == ' ') || isgraph(c) || (flag & VIS_OCTAL)) {
- *dst++ = '\\';
- *dst++ = ((unsigned char)c >> 6 & 07) + '0';
- *dst++ = ((unsigned char)c >> 3 & 07) + '0';
- *dst++ = ((unsigned char)c & 07) + '0';
- goto done;
+ len = src - psrc;
+ src = psrc;
+
+ /*
+ * In the single character input case, we will have actually
+ * processed two characters, c and nextc. Reset len back to
+ * just a single character.
+ */
+ if (mblength < len)
+ len = mblength;
+
+ /* Convert extra argument to list of characters for this mode. */
+ extra = makeextralist(flags, mbextra);
+ if (!extra) {
+ if (dlen && *dlen == 0) {
+ errno = ENOSPC;
+ goto out;
+ }
+ *mbdst = '\0'; /* can't create extra, return "" */
+ error = 0;
+ goto out;
}
- if ((flag & VIS_NOSLASH) == 0)
- *dst++ = '\\';
- if (c & 0200) {
- c &= 0177;
- *dst++ = 'M';
+
+ /* Look up which processing function to call. */
+ f = getvisfun(flags);
+
+ /*
+ * Main processing loop.
+ * Call do_Xvis processing function one character at a time
+ * with next character available for look-ahead.
+ */
+ for (start = dst; len > 0; len--) {
+ c = *src++;
+ dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
+ if (dst == NULL) {
+ errno = ENOSPC;
+ goto out;
+ }
}
- if (iscntrl(c)) {
- *dst++ = '^';
- if (c == 0177)
- *dst++ = '?';
- else
- *dst++ = c + '@';
- } else {
- *dst++ = '-';
- *dst++ = c;
+
+ /* Terminate the string in the buffer. */
+ *dst = L'\0';
+
+ /*
+ * Output loop.
+ * Convert wchar_t string back to multibyte output string.
+ * If we have hit a multi-byte conversion error on input,
+ * output byte-by-byte here. Else use wctomb().
+ */
+ len = wcslen(start);
+ maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
+ olen = 0;
+ for (dst = start; len > 0; len--) {
+ if (!cerr)
+ clen = wctomb(mbdst, *dst);
+ if (cerr || clen < 0) {
+ /*
+ * Conversion error, process as a byte(s) instead.
+ * Examine each byte and higher-order bytes for
+ * data. E.g.,
+ * 0x000000000000a264 -> a2 64
+ * 0x000000001f00a264 -> 1f 00 a2 64
+ */
+ clen = 0;
+ wmsk = 0;
+ for (i = sizeof(wmsk) - 1; i >= 0; i--) {
+ shft = i * NBBY;
+ bmsk = (uint64_t)0xffLL << shft;
+ wmsk |= bmsk;
+ if ((*dst & wmsk) || i == 0)
+ mbdst[clen++] = (char)(
+ (uint64_t)(*dst & bmsk) >>
+ shft);
+ }
+ cerr = 1;
+ }
+ /* If this character would exceed our output limit, stop. */
+ if (olen + clen > (size_t)maxolen)
+ break;
+ /* Advance output pointer by number of bytes written. */
+ mbdst += clen;
+ /* Advance buffer character pointer. */
+ dst++;
+ /* Incrment output character count. */
+ olen += clen;
+ }
+
+ /* Terminate the output string. */
+ *mbdst = '\0';
+
+ if (flags & VIS_NOLOCALE) {
+ /* Pass conversion error flag out. */
+ if (cerr_ptr)
+ *cerr_ptr = cerr;
}
-done:
- *dst = '\0';
- return (dst);
+
+ free(extra);
+ free(pdst);
+ free(psrc);
+
+ return (int)olen;
+out:
+ free(extra);
+ free(pdst);
+ free(psrc);
+ free(mdst);
+ return error;
}
+static int
+istrsenvisxl(char **mbdstp, size_t *dlen, const char *mbsrc,
+ int flags, const char *mbextra, int *cerr_ptr)
+{
+ return istrsenvisx(mbdstp, dlen, mbsrc,
+ mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
+}
+
+
/*
- * strvis, strnvis, strvisx - visually encode characters from src into dst
+ * The "svis" variants all take an "extra" arg that is a pointer
+ * to a NUL-terminated list of characters to be encoded, too.
+ * These functions are useful e. g. to encode strings in such a
+ * way so that they are not interpreted by a shell.
+ */
+
+char *
+svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
+{
+ char cc[2];
+ int ret;
+
+ cc[0] = c;
+ cc[1] = nextc;
+
+ ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, mbextra, NULL);
+ if (ret < 0)
+ return NULL;
+ return mbdst + ret;
+}
+
+char *
+snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
+{
+ char cc[2];
+ int ret;
+
+ cc[0] = c;
+ cc[1] = nextc;
+
+ ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, mbextra, NULL);
+ if (ret < 0)
+ return NULL;
+ return mbdst + ret;
+}
+
+int
+strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
+{
+ return istrsenvisxl(&mbdst, NULL, mbsrc, flags, mbextra, NULL);
+}
+
+int
+strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
+{
+ return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, mbextra, NULL);
+}
+
+int
+strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
+{
+ return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
+}
+
+int
+strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
+ const char *mbextra)
+{
+ return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
+}
+
+int
+strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
+ const char *mbextra, int *cerr_ptr)
+{
+ return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
+}
+
+/*
+ * vis - visually encode characters
+ */
+char *
+vis(char *mbdst, int c, int flags, int nextc)
+{
+ char cc[2];
+ int ret;
+
+ cc[0] = c;
+ cc[1] = nextc;
+
+ ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, "", NULL);
+ if (ret < 0)
+ return NULL;
+ return mbdst + ret;
+}
+
+char *
+nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
+{
+ char cc[2];
+ int ret;
+
+ cc[0] = c;
+ cc[1] = nextc;
+
+ ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, "", NULL);
+ if (ret < 0)
+ return NULL;
+ return mbdst + ret;
+}
+
+/*
+ * strvis - visually encode characters from src into dst
*
* Dst must be 4 times the size of src to account for possible
- * expansion. The length of dst, not including the trailing NUL,
+ * expansion. The length of dst, not including the trailing NULL,
* is returned.
- *
- * Strnvis will write no more than siz-1 bytes (and will NULL terminate).
- * The number of bytes needed to fully encode the string is returned.
- *
- * Strvisx encodes exactly len bytes from src into dst.
- * This is useful for encoding a block of data.
*/
+
int
-strvis(char *dst, const char *src, int flag)
+strvis(char *mbdst, const char *mbsrc, int flags)
{
- char c;
- char *start;
-
- for (start = dst; (c = *src); )
- dst = vis(dst, c, flag, *++src);
- *dst = '\0';
- return (dst - start);
+ return istrsenvisxl(&mbdst, NULL, mbsrc, flags, "", NULL);
}
int
-strnvis(char *dst, const char *src, size_t siz, int flag)
+strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
{
- char *start, *end;
- char tbuf[5];
- int c, i;
+ return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, "", NULL);
+}
- i = 0;
- for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
- if (isvisible(c)) {
- i = 1;
- *dst++ = c;
- if (c == '\\' && (flag & VIS_NOSLASH) == 0) {
- /* need space for the extra '\\' */
- if (dst < end)
- *dst++ = '\\';
- else {
- dst--;
- i = 2;
- break;
- }
- }
- src++;
- } else {
- i = vis(tbuf, c, flag, *++src) - tbuf;
- if (dst + i <= end) {
- memcpy(dst, tbuf, i);
- dst += i;
- } else {
- src--;
- break;
- }
- }
- }
- if (siz > 0)
- *dst = '\0';
- if (dst + i > end) {
- /* adjust return value for truncation */
- while ((c = *src))
- dst += vis(tbuf, c, flag, *++src) - tbuf;
- }
- return (dst - start);
+int
+stravis(char **mbdstp, const char *mbsrc, int flags)
+{
+ *mbdstp = NULL;
+ return istrsenvisxl(mbdstp, NULL, mbsrc, flags, "", NULL);
}
+/*
+ * strvisx - visually encode characters from src into dst
+ *
+ * Dst must be 4 times the size of src to account for possible
+ * expansion. The length of dst, not including the trailing NULL,
+ * is returned.
+ *
+ * Strvisx encodes exactly len characters from src into dst.
+ * This is useful for encoding a block of data.
+ */
+
int
-strvisx(char *dst, const char *src, size_t len, int flag)
+strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
{
- int c;
- char *start;
+ return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, "", NULL);
+}
- for (start = dst; len > 1; len--) {
- c = *src;
- dst = vis(dst, c, flag, *++src);
- }
- if (len)
- dst = vis(dst, *src, flag, '\0');
- *dst = '\0';
+int
+strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
+{
+ return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", NULL);
+}
- return (dst - start);
+int
+strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
+ int *cerr_ptr)
+{
+ return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);
}