File size: 65,195 Bytes
3dcad1f |
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 |
/* Copyright 2009-2015,2018-2019,2023
Free Software Foundation, Inc.
This file is part of Guile.
Guile is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Guile is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Guile. If not, see
<https://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <limits.h>
#include <byteswap.h>
#include <intprops.h>
#include <errno.h>
#include <striconveh.h>
#include <uniconv.h>
#include <unistr.h>
#include <string.h>
#include <alloca.h>
#include <assert.h>
#include "scm.h"
#if SCM_ENABLE_MINI_GMP
#include "mini-gmp.h"
#else
#include <gmp.h>
#endif
#include "array-handle.h"
#include "arrays.h"
#include "boolean.h"
#include "dynwind.h"
#include "extensions.h"
#include "generalized-vectors.h"
#include "gsubr.h"
#include "list.h"
#include "numbers.h"
#include "pairs.h"
#include "ports.h"
#include "srfi-4.h"
#include "strings.h"
#include "symbols.h"
#include "uniform.h"
#include "version.h"
#include "bytevectors.h"
/* Utilities. */
/* Convenience macros. These are used by the various templates (macros) that
are parameterized by integer signedness. */
#define INT8_T_signed int8_t
#define INT8_T_unsigned uint8_t
#define INT16_T_signed int16_t
#define INT16_T_unsigned uint16_t
#define INT32_T_signed int32_t
#define INT32_T_unsigned uint32_t
#define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
#define is_unsigned_int8(_x) ((_x) <= 255UL)
#define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
#define is_unsigned_int16(_x) ((_x) <= 65535UL)
#define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
#define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
#define SIGNEDNESS_signed 1
#define SIGNEDNESS_unsigned 0
#define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
#define INT_SWAP(_size) bswap_ ## _size
#define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
#define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
#define INTEGER_ACCESSOR_PROLOGUE(validate, _len, _sign) \
size_t c_len, c_index; \
_sign char *c_bv; \
\
SCM_VALIDATE_##validate (1, bv); \
c_index = scm_to_size_t (index); \
\
c_len = SCM_BYTEVECTOR_LENGTH (bv); \
c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
\
if (SCM_UNLIKELY (c_len < c_index \
|| (c_len - c_index < (_len) / 8))) \
scm_out_of_range (FUNC_NAME, index);
#define INTEGER_GETTER_PROLOGUE(_len, _sign) \
INTEGER_ACCESSOR_PROLOGUE (BYTEVECTOR, _len, _sign)
#define INTEGER_SETTER_PROLOGUE(_len, _sign) \
INTEGER_ACCESSOR_PROLOGUE (MUTABLE_BYTEVECTOR, _len, _sign)
/* Template for fixed-size integer access (only 8, 16 or 32-bit). */
#define INTEGER_REF(_len, _sign) \
SCM result; \
\
INTEGER_GETTER_PROLOGUE (_len, _sign); \
SCM_VALIDATE_SYMBOL (3, endianness); \
\
{ \
INT_TYPE (_len, _sign) c_result; \
\
memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
if (!scm_is_eq (endianness, scm_i_native_endianness)) \
c_result = INT_SWAP (_len) (c_result); \
\
result = SCM_I_MAKINUM (c_result); \
} \
\
return result;
/* Template for fixed-size integer access using the native endianness. */
#define INTEGER_NATIVE_REF(_len, _sign) \
SCM result; \
\
INTEGER_GETTER_PROLOGUE (_len, _sign); \
\
{ \
INT_TYPE (_len, _sign) c_result; \
\
memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
result = SCM_I_MAKINUM (c_result); \
} \
\
return result;
/* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
#define INTEGER_SET(_len, _sign) \
INTEGER_SETTER_PROLOGUE (_len, _sign); \
SCM_VALIDATE_SYMBOL (3, endianness); \
\
{ \
scm_t_signed_bits c_value; \
INT_TYPE (_len, _sign) c_value_short; \
\
if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
scm_wrong_type_arg (FUNC_NAME, 3, value); \
\
c_value = SCM_I_INUM (value); \
if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
scm_out_of_range (FUNC_NAME, value); \
\
c_value_short = (INT_TYPE (_len, _sign)) c_value; \
if (!scm_is_eq (endianness, scm_i_native_endianness)) \
c_value_short = INT_SWAP (_len) (c_value_short); \
\
memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
} \
\
return SCM_UNSPECIFIED;
/* Template for fixed-size integer modification using the native
endianness. */
#define INTEGER_NATIVE_SET(_len, _sign) \
INTEGER_SETTER_PROLOGUE (_len, _sign); \
\
{ \
scm_t_signed_bits c_value; \
INT_TYPE (_len, _sign) c_value_short; \
\
if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
scm_wrong_type_arg (FUNC_NAME, 3, value); \
\
c_value = SCM_I_INUM (value); \
if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
scm_out_of_range (FUNC_NAME, value); \
\
c_value_short = (INT_TYPE (_len, _sign)) c_value; \
\
memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
} \
\
return SCM_UNSPECIFIED;
/* Bytevector type. */
#define SCM_BYTEVECTOR_HEADER_BYTES \
(SCM_BYTEVECTOR_HEADER_SIZE * sizeof (scm_t_bits))
#define SCM_BYTEVECTOR_SET_FLAG(bv, flag) \
SCM_SET_BYTEVECTOR_FLAGS ((bv), SCM_BYTEVECTOR_FLAGS (bv) | flag)
#define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
#define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _contents) \
SCM_SET_CELL_WORD_2 ((_bv), (scm_t_bits) (_contents))
#define SCM_BYTEVECTOR_SET_PARENT(_bv, _parent) \
SCM_SET_CELL_OBJECT_3 ((_bv), (_parent))
#define SCM_VALIDATE_MUTABLE_BYTEVECTOR(pos, v) \
SCM_MAKE_VALIDATE_MSG (pos, v, MUTABLE_BYTEVECTOR_P, "mutable bytevector")
/* The empty bytevector. */
SCM scm_null_bytevector = SCM_UNSPECIFIED;
static inline SCM
make_bytevector (size_t len, scm_t_array_element_type element_type)
{
SCM ret;
size_t c_len;
if (SCM_UNLIKELY (element_type > SCM_ARRAY_ELEMENT_TYPE_LAST
|| scm_i_array_element_type_sizes[element_type] < 8))
/* This would be an internal Guile programming error */
abort ();
/* Make sure that the total allocation size will not overflow size_t,
with ~30 extra bytes to spare to avoid an overflow within the
allocator. */
if (SCM_UNLIKELY (len >= (((size_t) -(SCM_BYTEVECTOR_HEADER_BYTES + 32))
/ (scm_i_array_element_type_sizes[element_type]/8))))
scm_num_overflow ("make-bytevector");
if (SCM_UNLIKELY (len == 0 && element_type == SCM_ARRAY_ELEMENT_TYPE_VU8
&& SCM_BYTEVECTOR_P (scm_null_bytevector)))
ret = scm_null_bytevector;
else
{
signed char *contents;
c_len = len * (scm_i_array_element_type_sizes[element_type] / 8);
contents = scm_gc_malloc_pointerless (SCM_BYTEVECTOR_HEADER_BYTES + c_len,
SCM_GC_BYTEVECTOR);
ret = SCM_PACK_POINTER (contents);
contents += SCM_BYTEVECTOR_HEADER_BYTES;
SCM_SET_BYTEVECTOR_FLAGS (ret,
element_type | SCM_F_BYTEVECTOR_CONTIGUOUS);
SCM_BYTEVECTOR_SET_LENGTH (ret, c_len);
SCM_BYTEVECTOR_SET_CONTENTS (ret, contents);
SCM_BYTEVECTOR_SET_PARENT (ret, SCM_BOOL_F);
}
return ret;
}
/* Return a bytevector of LEN elements of type ELEMENT_TYPE, with element
values taken from CONTENTS. Assume that the storage for CONTENTS will be
automatically reclaimed when it becomes unreachable. */
static inline SCM
make_bytevector_from_buffer (size_t len, void *contents,
scm_t_array_element_type element_type)
{
SCM ret;
if (SCM_UNLIKELY (len == 0))
ret = make_bytevector (len, element_type);
else
{
size_t c_len;
ret = SCM_PACK_POINTER (scm_gc_malloc (SCM_BYTEVECTOR_HEADER_BYTES,
SCM_GC_BYTEVECTOR));
c_len = len * (scm_i_array_element_type_sizes[element_type] / 8);
SCM_SET_BYTEVECTOR_FLAGS (ret, element_type);
SCM_BYTEVECTOR_SET_LENGTH (ret, c_len);
SCM_BYTEVECTOR_SET_CONTENTS (ret, contents);
SCM_BYTEVECTOR_SET_PARENT (ret, SCM_BOOL_F);
}
return ret;
}
/* Return a new bytevector of size LEN octets. */
SCM
scm_c_make_bytevector (size_t len)
{
return make_bytevector (len, SCM_ARRAY_ELEMENT_TYPE_VU8);
}
/* Return a new bytevector of size LEN elements. */
SCM
scm_i_make_typed_bytevector (size_t len, scm_t_array_element_type element_type)
{
return make_bytevector (len, element_type);
}
/* Return a bytevector of size LEN made up of CONTENTS. The area
pointed to by CONTENTS must be protected from GC somehow: either
because it was allocated using `scm_gc_malloc ()', or because it is
part of PARENT. */
SCM
scm_c_take_gc_bytevector (signed char *contents, size_t len, SCM parent)
{
SCM ret;
ret = make_bytevector_from_buffer (len, contents, SCM_ARRAY_ELEMENT_TYPE_VU8);
SCM_BYTEVECTOR_SET_PARENT (ret, parent);
return ret;
}
SCM
scm_c_take_typed_bytevector (signed char *contents, size_t len,
scm_t_array_element_type element_type, SCM parent)
{
SCM ret;
ret = make_bytevector_from_buffer (len, contents, element_type);
SCM_BYTEVECTOR_SET_PARENT (ret, parent);
return ret;
}
SCM_DEFINE (scm_bytevector_slice, "bytevector-slice", 2, 1, 0,
(SCM bv, SCM offset, SCM size),
"Return the slice of @var{bv} starting at @var{offset} and counting\n"
"@var{size} bytes. When @var{size} is omitted, the slice covers all\n"
"of @var{bv} starting from @var{offset}. The returned slice shares\n"
"storage with @var{bv}: changes to the slice are visible in @var{bv}\n"
"and vice-versa.\n"
"\n"
"When @var{bv} is actually a SRFI-4 uniform vector, its element\n"
"type is preserved unless @var{offset} and @var{size} are not aligned\n"
"on its element type size.\n")
#define FUNC_NAME s_scm_bytevector_slice
{
SCM ret;
size_t c_offset, c_size;
scm_t_array_element_type element_type;
SCM_VALIDATE_BYTEVECTOR (1, bv);
c_offset = scm_to_size_t (offset);
if (SCM_UNBNDP (size))
{
if (c_offset < SCM_BYTEVECTOR_LENGTH (bv))
c_size = SCM_BYTEVECTOR_LENGTH (bv) - c_offset;
else
c_size = 0;
}
else
c_size = scm_to_size_t (size);
if (c_offset == 0 && c_size == SCM_BYTEVECTOR_LENGTH (bv))
return bv;
if (INT_ADD_OVERFLOW (c_offset, c_size)
|| (c_offset + c_size > SCM_BYTEVECTOR_LENGTH (bv)))
scm_out_of_range (FUNC_NAME, offset);
/* Preserve the element type of BV, unless we're not slicing on type
boundaries. */
element_type = SCM_BYTEVECTOR_ELEMENT_TYPE (bv);
if ((c_offset % SCM_BYTEVECTOR_TYPE_SIZE (bv) != 0)
|| (c_size % SCM_BYTEVECTOR_TYPE_SIZE (bv) != 0))
element_type = SCM_ARRAY_ELEMENT_TYPE_VU8;
else
c_size /= (scm_i_array_element_type_sizes[element_type] / 8);
ret = make_bytevector_from_buffer (c_size,
SCM_BYTEVECTOR_CONTENTS (bv) + c_offset,
element_type);
if (!SCM_MUTABLE_BYTEVECTOR_P (bv))
{
/* Preserve the immutability property. */
scm_t_bits flags = SCM_BYTEVECTOR_FLAGS (ret);
SCM_SET_BYTEVECTOR_FLAGS (ret, flags | SCM_F_BYTEVECTOR_IMMUTABLE);
}
SCM_BYTEVECTOR_SET_PARENT (ret, bv);
return ret;
}
#undef FUNC_NAME
/* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
size) and return the new bytevector (possibly different from BV). */
SCM
scm_c_shrink_bytevector (SCM bv, size_t c_new_len)
{
SCM new_bv;
size_t c_len;
if (SCM_UNLIKELY (c_new_len % SCM_BYTEVECTOR_TYPE_SIZE (bv)))
/* This would be an internal Guile programming error */
abort ();
c_len = SCM_BYTEVECTOR_LENGTH (bv);
if (SCM_UNLIKELY (c_new_len > c_len))
abort ();
SCM_BYTEVECTOR_SET_LENGTH (bv, c_new_len);
if (SCM_BYTEVECTOR_CONTIGUOUS_P (bv))
{
signed char *c_bv;
c_bv = scm_gc_realloc (SCM2PTR (bv),
c_len + SCM_BYTEVECTOR_HEADER_BYTES,
c_new_len + SCM_BYTEVECTOR_HEADER_BYTES,
SCM_GC_BYTEVECTOR);
new_bv = PTR2SCM (c_bv);
SCM_BYTEVECTOR_SET_CONTENTS (new_bv, c_bv + SCM_BYTEVECTOR_HEADER_BYTES);
}
else
{
signed char *c_bv;
c_bv = scm_gc_realloc (SCM_BYTEVECTOR_CONTENTS (bv),
c_len, c_new_len, SCM_GC_BYTEVECTOR);
SCM_BYTEVECTOR_SET_CONTENTS (bv, c_bv);
new_bv = bv;
}
return new_bv;
}
int
scm_is_bytevector (SCM obj)
{
return SCM_BYTEVECTOR_P (obj);
}
size_t
scm_c_bytevector_length (SCM bv)
#define FUNC_NAME "scm_c_bytevector_length"
{
SCM_VALIDATE_BYTEVECTOR (1, bv);
return SCM_BYTEVECTOR_LENGTH (bv);
}
#undef FUNC_NAME
uint8_t
scm_c_bytevector_ref (SCM bv, size_t index)
#define FUNC_NAME "scm_c_bytevector_ref"
{
size_t c_len;
const uint8_t *c_bv;
SCM_VALIDATE_BYTEVECTOR (1, bv);
c_len = SCM_BYTEVECTOR_LENGTH (bv);
c_bv = (uint8_t *) SCM_BYTEVECTOR_CONTENTS (bv);
if (SCM_UNLIKELY (index >= c_len))
scm_out_of_range (FUNC_NAME, scm_from_size_t (index));
return c_bv[index];
}
#undef FUNC_NAME
void
scm_c_bytevector_set_x (SCM bv, size_t index, uint8_t value)
#define FUNC_NAME "scm_c_bytevector_set_x"
{
size_t c_len;
uint8_t *c_bv;
SCM_VALIDATE_MUTABLE_BYTEVECTOR (1, bv);
c_len = SCM_BYTEVECTOR_LENGTH (bv);
c_bv = (uint8_t *) SCM_BYTEVECTOR_CONTENTS (bv);
if (SCM_UNLIKELY (index >= c_len))
scm_out_of_range (FUNC_NAME, scm_from_size_t (index));
c_bv[index] = value;
}
#undef FUNC_NAME
int
scm_i_print_bytevector (SCM bv, SCM port, scm_print_state *pstate SCM_UNUSED)
{
ssize_t ubnd, inc, i;
scm_t_array_handle h;
scm_array_get_handle (bv, &h);
scm_putc ('#', port);
scm_write (scm_array_handle_element_type (&h), port);
scm_putc ('(', port);
for (i = h.dims[0].lbnd, ubnd = h.dims[0].ubnd, inc = h.dims[0].inc;
i <= ubnd; i += inc)
{
if (i > 0)
scm_putc (' ', port);
scm_write (scm_array_handle_ref (&h, i), port);
}
scm_putc (')', port);
return 1;
}
/* General operations. */
static SCM sym_big;
static SCM sym_little;
SCM scm_endianness_big, scm_endianness_little;
/* Host endianness (a symbol). */
SCM scm_i_native_endianness = SCM_UNSPECIFIED;
/* Byte-swapping. */
#ifndef bswap_24
# define bswap_24(_x) \
((((_x) & 0xff0000) >> 16) | \
(((_x) & 0x00ff00)) | \
(((_x) & 0x0000ff) << 16))
#endif
SCM_DEFINE (scm_native_endianness, "native-endianness", 0, 0, 0,
(void),
"Return a symbol denoting the machine's native endianness.")
#define FUNC_NAME s_scm_native_endianness
{
return scm_i_native_endianness;
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_p, "bytevector?", 1, 0, 0,
(SCM obj),
"Return true if @var{obj} is a bytevector.")
#define FUNC_NAME s_scm_bytevector_p
{
return scm_from_bool (scm_is_bytevector (obj));
}
#undef FUNC_NAME
SCM_DEFINE (scm_make_bytevector, "make-bytevector", 1, 1, 0,
(SCM len, SCM fill),
"Return a newly allocated bytevector of @var{len} bytes, "
"optionally filled with @var{fill}.")
#define FUNC_NAME s_scm_make_bytevector
{
SCM bv;
size_t c_len;
uint8_t c_fill = 0;
SCM_VALIDATE_SIZE_COPY (1, len, c_len);
if (!scm_is_eq (fill, SCM_UNDEFINED))
{
int value;
value = scm_to_int (fill);
if (SCM_UNLIKELY ((value < -128) || (value > 255)))
scm_out_of_range (FUNC_NAME, fill);
c_fill = (uint8_t) value;
}
bv = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
if (!scm_is_eq (fill, SCM_UNDEFINED))
{
size_t i;
uint8_t *contents;
contents = (uint8_t *) SCM_BYTEVECTOR_CONTENTS (bv);
for (i = 0; i < c_len; i++)
contents[i] = c_fill;
}
else
memset (SCM_BYTEVECTOR_CONTENTS (bv), 0, c_len);
return bv;
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_length, "bytevector-length", 1, 0, 0,
(SCM bv),
"Return the length (in bytes) of @var{bv}.")
#define FUNC_NAME s_scm_bytevector_length
{
return scm_from_size_t (scm_c_bytevector_length (bv));
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_eq_p, "bytevector=?", 2, 0, 0,
(SCM bv1, SCM bv2),
"Return is @var{bv1} equals to @var{bv2}---i.e., if they "
"have the same length and contents.")
#define FUNC_NAME s_scm_bytevector_eq_p
{
SCM result = SCM_BOOL_F;
size_t c_len1, c_len2;
SCM_VALIDATE_BYTEVECTOR (1, bv1);
SCM_VALIDATE_BYTEVECTOR (2, bv2);
c_len1 = SCM_BYTEVECTOR_LENGTH (bv1);
c_len2 = SCM_BYTEVECTOR_LENGTH (bv2);
if (c_len1 == c_len2 && (SCM_BYTEVECTOR_ELEMENT_TYPE (bv1)
== SCM_BYTEVECTOR_ELEMENT_TYPE (bv2)))
{
signed char *c_bv1, *c_bv2;
c_bv1 = SCM_BYTEVECTOR_CONTENTS (bv1);
c_bv2 = SCM_BYTEVECTOR_CONTENTS (bv2);
result = scm_from_bool (!memcmp (c_bv1, c_bv2, c_len1));
}
return result;
}
#undef FUNC_NAME
static SCM scm_bytevector_fill_partial_x (SCM bv, SCM fill, SCM start, SCM end);
SCM_DEFINE (scm_bytevector_fill_partial_x, "bytevector-fill!", 2, 2, 0,
(SCM bv, SCM fill, SCM start, SCM end),
"Fill positions [@var{start} ... @var{end}) of bytevector "
"@var{bv} with @var{fill}, a byte. @var{start} defaults to 0 "
"and @var{end} defaults to the length of @var{bv}. "
"The return value is unspecified.")
#define FUNC_NAME s_scm_bytevector_fill_partial_x
{
SCM_VALIDATE_MUTABLE_BYTEVECTOR (1, bv);
int value = scm_to_int (fill);
if (SCM_UNLIKELY ((value < -128) || (value > 255)))
scm_out_of_range (FUNC_NAME, fill);
size_t i = 0;
size_t c_end = SCM_BYTEVECTOR_LENGTH (bv);
uint8_t *c_bv = (uint8_t *) SCM_BYTEVECTOR_CONTENTS (bv);
if (!SCM_UNBNDP (start))
i = scm_to_unsigned_integer (start, 0, c_end);
if (!SCM_UNBNDP (end))
c_end = scm_to_unsigned_integer (end, i, c_end);
memset (c_bv + i, value, c_end-i);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM
scm_bytevector_fill_x (SCM bv, SCM fill)
#define FUNC_NAME s_scm_bytevector_fill_x
{
return scm_bytevector_fill_partial_x (bv, fill, SCM_UNDEFINED, SCM_UNDEFINED);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_copy_x, "bytevector-copy!", 5, 0, 0,
(SCM source, SCM source_start, SCM target, SCM target_start,
SCM len),
"Copy @var{len} bytes from @var{source} into @var{target}, "
"reading from a block starting at @var{source_start} (a positive "
"index within @var{source}) and writing to a block starting at "
"@var{target_start}.\n\n"
"It is permitted for the @var{source} and @var{target} regions to "
"overlap. In that case, copying takes place as if the source is "
"first copied into a temporary bytevector and then into the "
"destination. ")
#define FUNC_NAME s_scm_bytevector_copy_x
{
size_t c_len, c_source_len, c_target_len;
size_t c_source_start, c_target_start;
signed char *c_source, *c_target;
SCM_VALIDATE_BYTEVECTOR (1, source);
SCM_VALIDATE_MUTABLE_BYTEVECTOR (3, target);
c_len = scm_to_size_t (len);
c_source_start = scm_to_size_t (source_start);
c_target_start = scm_to_size_t (target_start);
c_source = SCM_BYTEVECTOR_CONTENTS (source);
c_target = SCM_BYTEVECTOR_CONTENTS (target);
c_source_len = SCM_BYTEVECTOR_LENGTH (source);
c_target_len = SCM_BYTEVECTOR_LENGTH (target);
if (SCM_UNLIKELY (c_source_len < c_source_start
|| (c_source_len - c_source_start < c_len)))
scm_out_of_range (FUNC_NAME, source_start);
if (SCM_UNLIKELY (c_target_len < c_target_start
|| (c_target_len - c_target_start < c_len)))
scm_out_of_range (FUNC_NAME, target_start);
memmove (c_target + c_target_start,
c_source + c_source_start,
c_len);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_copy, "bytevector-copy", 1, 0, 0,
(SCM bv),
"Return a newly allocated copy of @var{bv}.")
#define FUNC_NAME s_scm_bytevector_copy
{
SCM copy;
size_t c_len;
signed char *c_bv, *c_copy;
SCM_VALIDATE_BYTEVECTOR (1, bv);
c_len = SCM_BYTEVECTOR_LENGTH (bv);
c_bv = SCM_BYTEVECTOR_CONTENTS (bv);
copy = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
c_copy = SCM_BYTEVECTOR_CONTENTS (copy);
memcpy (c_copy, c_bv, c_len);
return copy;
}
#undef FUNC_NAME
SCM_DEFINE (scm_uniform_array_to_bytevector, "uniform-array->bytevector",
1, 0, 0, (SCM array),
"Return a newly allocated bytevector whose contents\n"
"will be copied from the uniform array @var{array}.")
#define FUNC_NAME s_scm_uniform_array_to_bytevector
{
SCM contents, ret;
size_t len, sz, byte_len;
scm_t_array_handle h;
const void *elts;
contents = scm_array_contents (array, SCM_BOOL_T);
if (scm_is_false (contents))
scm_wrong_type_arg_msg (FUNC_NAME, 0, array, "uniform contiguous array");
scm_array_get_handle (contents, &h);
assert (h.base == 0);
elts = h.elements;
len = h.dims->inc * (h.dims->ubnd - h.dims->lbnd + 1);
sz = scm_array_handle_uniform_element_bit_size (&h);
if (sz >= 8 && ((sz % 8) == 0))
byte_len = len * (sz / 8);
else if (sz < 8)
/* Elements of sub-byte size (bitvectors) are addressed in 32-bit
units. */
byte_len = ((len * sz + 31) / 32) * 4;
else
/* an internal guile error, really */
SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL);
ret = make_bytevector (byte_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
if (byte_len != 0)
/* Empty arrays may have elements == NULL. We must avoid passing
NULL to memcpy, even if the length is zero, to avoid undefined
behavior. */
memcpy (SCM_BYTEVECTOR_CONTENTS (ret), elts, byte_len);
scm_array_handle_release (&h);
return ret;
}
#undef FUNC_NAME
/* Operations on bytes and octets. */
SCM_DEFINE (scm_bytevector_u8_ref, "bytevector-u8-ref", 2, 0, 0,
(SCM bv, SCM index),
"Return the octet located at @var{index} in @var{bv}.")
#define FUNC_NAME s_scm_bytevector_u8_ref
{
INTEGER_NATIVE_REF (8, unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s8_ref, "bytevector-s8-ref", 2, 0, 0,
(SCM bv, SCM index),
"Return the byte located at @var{index} in @var{bv}.")
#define FUNC_NAME s_scm_bytevector_s8_ref
{
INTEGER_NATIVE_REF (8, signed);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_u8_set_x, "bytevector-u8-set!", 3, 0, 0,
(SCM bv, SCM index, SCM value),
"Return the octet located at @var{index} in @var{bv}.")
#define FUNC_NAME s_scm_bytevector_u8_set_x
{
INTEGER_NATIVE_SET (8, unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s8_set_x, "bytevector-s8-set!", 3, 0, 0,
(SCM bv, SCM index, SCM value),
"Return the octet located at @var{index} in @var{bv}.")
#define FUNC_NAME s_scm_bytevector_s8_set_x
{
INTEGER_NATIVE_SET (8, signed);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_to_u8_list, "bytevector->u8-list", 1, 0, 0,
(SCM bv),
"Return a newly allocated list of octets containing the "
"contents of @var{bv}.")
#define FUNC_NAME s_scm_bytevector_to_u8_list
{
SCM lst, pair;
size_t c_len, i;
uint8_t *c_bv;
SCM_VALIDATE_BYTEVECTOR (1, bv);
c_len = SCM_BYTEVECTOR_LENGTH (bv);
c_bv = (uint8_t *) SCM_BYTEVECTOR_CONTENTS (bv);
lst = scm_make_list (scm_from_size_t (c_len), SCM_UNSPECIFIED);
for (i = 0, pair = lst;
i < c_len;
i++, pair = SCM_CDR (pair))
{
SCM_SETCAR (pair, SCM_I_MAKINUM (c_bv[i]));
}
return lst;
}
#undef FUNC_NAME
SCM_DEFINE (scm_u8_list_to_bytevector, "u8-list->bytevector", 1, 0, 0,
(SCM lst),
"Turn @var{lst}, a list of octets, into a bytevector.")
#define FUNC_NAME s_scm_u8_list_to_bytevector
{
SCM bv, item;
size_t c_len, i;
uint8_t *c_bv;
SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len);
bv = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
c_bv = (uint8_t *) SCM_BYTEVECTOR_CONTENTS (bv);
for (i = 0; i < c_len; lst = SCM_CDR (lst), i++)
{
item = SCM_CAR (lst);
if (SCM_LIKELY (SCM_I_INUMP (item)))
{
scm_t_signed_bits c_item;
c_item = SCM_I_INUM (item);
if (SCM_LIKELY ((c_item >= 0) && (c_item < 256)))
c_bv[i] = (uint8_t) c_item;
else
goto type_error;
}
else
goto type_error;
}
return bv;
type_error:
scm_wrong_type_arg (FUNC_NAME, 1, item);
return SCM_BOOL_F;
}
#undef FUNC_NAME
/* Compute the two's complement of VALUE (a positive integer) on SIZE octets
using (2^(SIZE * 8) - VALUE). */
static inline void
twos_complement (mpz_t value, size_t size)
{
unsigned long bit_count;
/* We expect BIT_COUNT to fit in a unsigned long thanks to the range
checking on SIZE performed earlier. */
bit_count = (unsigned long) size << 3UL;
if (SCM_LIKELY (bit_count < sizeof (unsigned long)))
mpz_ui_sub (value, 1UL << bit_count, value);
else
{
mpz_t max;
mpz_init (max);
mpz_ui_pow_ui (max, 2, bit_count);
mpz_sub (value, max, value);
mpz_clear (max);
}
}
static inline SCM
bytevector_large_ref (const char *c_bv, size_t c_size, int signed_p,
SCM endianness)
{
SCM result;
mpz_t c_mpz;
int c_endianness, negative_p = 0;
if (signed_p)
{
if (scm_is_eq (endianness, sym_big))
negative_p = c_bv[0] & 0x80;
else
negative_p = c_bv[c_size - 1] & 0x80;
}
c_endianness = scm_is_eq (endianness, sym_big) ? 1 : -1;
mpz_init (c_mpz);
mpz_import (c_mpz, 1 /* 1 word */, 1 /* word order doesn't matter */,
c_size /* word is C_SIZE-byte long */,
c_endianness,
0 /* nails */, c_bv);
if (signed_p && negative_p)
{
twos_complement (c_mpz, c_size);
mpz_neg (c_mpz, c_mpz);
}
result = scm_from_mpz (c_mpz);
mpz_clear (c_mpz); /* FIXME: Needed? */
return result;
}
static inline int
bytevector_large_set (char *c_bv, size_t c_size, int signed_p,
SCM value, SCM endianness)
{
mpz_t c_mpz;
int c_endianness, c_sign, err = 0;
c_endianness = scm_is_eq (endianness, sym_big) ? 1 : -1;
mpz_init (c_mpz);
scm_to_mpz (value, c_mpz);
c_sign = mpz_sgn (c_mpz);
if (c_sign < 0)
{
if (SCM_LIKELY (signed_p))
{
mpz_neg (c_mpz, c_mpz);
twos_complement (c_mpz, c_size);
}
else
{
err = -1;
goto finish;
}
}
if (c_sign == 0)
/* Zero. */
memset (c_bv, 0, c_size);
else
{
size_t word_count, value_words;
value_words = ((mpz_sizeinbase (c_mpz, 2) + (8 * c_size) - 1) /
(8 * c_size));
if (SCM_UNLIKELY (value_words > 1))
{
err = -2;
goto finish;
}
mpz_export (c_bv, &word_count, 1 /* word order doesn't matter */,
c_size, c_endianness,
0 /* nails */, c_mpz);
if (SCM_UNLIKELY (word_count != 1))
/* Shouldn't happen since we already checked with VALUE_SIZE. */
abort ();
}
finish:
mpz_clear (c_mpz);
return err;
}
#define GENERIC_INTEGER_ACCESSOR_PROLOGUE(validate, _sign) \
size_t c_len, c_index, c_size; \
char *c_bv; \
\
SCM_VALIDATE_##validate (1, bv); \
c_index = scm_to_size_t (index); \
c_size = scm_to_size_t (size); \
\
c_len = SCM_BYTEVECTOR_LENGTH (bv); \
c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
\
/* C_SIZE must have its 3 higher bits set to zero so that \
multiplying it by 8 yields a number that fits in a \
size_t. */ \
if (SCM_UNLIKELY (c_size == 0 || c_size >= (SIZE_MAX >> 3))) \
scm_out_of_range (FUNC_NAME, size); \
if (SCM_UNLIKELY (c_len < c_index \
|| (c_len - c_index < c_size))) \
scm_out_of_range (FUNC_NAME, index);
#define GENERIC_INTEGER_GETTER_PROLOGUE(_sign) \
GENERIC_INTEGER_ACCESSOR_PROLOGUE (BYTEVECTOR, _sign)
#define GENERIC_INTEGER_SETTER_PROLOGUE(_sign) \
GENERIC_INTEGER_ACCESSOR_PROLOGUE (MUTABLE_BYTEVECTOR, _sign)
/* Template of an integer reference function. */
#define GENERIC_INTEGER_REF(_sign) \
SCM result; \
\
if (c_size < 3) \
{ \
int swap; \
_sign int value; \
\
swap = !scm_is_eq (endianness, scm_i_native_endianness); \
switch (c_size) \
{ \
case 1: \
{ \
_sign char c_value8; \
memcpy (&c_value8, c_bv, 1); \
value = c_value8; \
} \
break; \
case 2: \
{ \
INT_TYPE (16, _sign) c_value16; \
memcpy (&c_value16, c_bv, 2); \
if (swap) \
value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
else \
value = c_value16; \
} \
break; \
default: \
abort (); \
} \
\
result = SCM_I_MAKINUM ((_sign int) value); \
} \
else \
result = bytevector_large_ref ((char *) c_bv, \
c_size, SIGNEDNESS (_sign), \
endianness); \
\
return result;
static inline SCM
bytevector_signed_ref (const char *c_bv, size_t c_size, SCM endianness)
{
GENERIC_INTEGER_REF (signed);
}
static inline SCM
bytevector_unsigned_ref (const char *c_bv, size_t c_size, SCM endianness)
{
GENERIC_INTEGER_REF (unsigned);
}
/* Template of an integer assignment function. */
#define GENERIC_INTEGER_SET(_sign) \
if (c_size < 3) \
{ \
scm_t_signed_bits c_value; \
\
if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
goto range_error; \
\
c_value = SCM_I_INUM (value); \
switch (c_size) \
{ \
case 1: \
if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
{ \
_sign char c_value8; \
c_value8 = (_sign char) c_value; \
memcpy (c_bv, &c_value8, 1); \
} \
else \
goto range_error; \
break; \
\
case 2: \
if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
{ \
int swap; \
INT_TYPE (16, _sign) c_value16; \
\
swap = !scm_is_eq (endianness, scm_i_native_endianness); \
\
if (swap) \
c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
else \
c_value16 = c_value; \
\
memcpy (c_bv, &c_value16, 2); \
} \
else \
goto range_error; \
break; \
\
default: \
abort (); \
} \
} \
else \
{ \
int err; \
\
err = bytevector_large_set (c_bv, c_size, \
SIGNEDNESS (_sign), \
value, endianness); \
if (err) \
goto range_error; \
} \
\
return; \
\
range_error: \
scm_out_of_range (FUNC_NAME, value); \
return;
static inline void
bytevector_signed_set (char *c_bv, size_t c_size,
SCM value, SCM endianness,
const char *func_name)
#define FUNC_NAME func_name
{
GENERIC_INTEGER_SET (signed);
}
#undef FUNC_NAME
static inline void
bytevector_unsigned_set (char *c_bv, size_t c_size,
SCM value, SCM endianness,
const char *func_name)
#define FUNC_NAME func_name
{
GENERIC_INTEGER_SET (unsigned);
}
#undef FUNC_NAME
#undef GENERIC_INTEGER_SET
#undef GENERIC_INTEGER_REF
SCM_DEFINE (scm_bytevector_uint_ref, "bytevector-uint-ref", 4, 0, 0,
(SCM bv, SCM index, SCM endianness, SCM size),
"Return the @var{size}-octet long unsigned integer at index "
"@var{index} in @var{bv}.")
#define FUNC_NAME s_scm_bytevector_uint_ref
{
GENERIC_INTEGER_GETTER_PROLOGUE (unsigned);
return (bytevector_unsigned_ref (&c_bv[c_index], c_size, endianness));
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_sint_ref, "bytevector-sint-ref", 4, 0, 0,
(SCM bv, SCM index, SCM endianness, SCM size),
"Return the @var{size}-octet long unsigned integer at index "
"@var{index} in @var{bv}.")
#define FUNC_NAME s_scm_bytevector_sint_ref
{
GENERIC_INTEGER_GETTER_PROLOGUE (signed);
return (bytevector_signed_ref (&c_bv[c_index], c_size, endianness));
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_uint_set_x, "bytevector-uint-set!", 5, 0, 0,
(SCM bv, SCM index, SCM value, SCM endianness, SCM size),
"Set the @var{size}-octet long unsigned integer at @var{index} "
"to @var{value}.")
#define FUNC_NAME s_scm_bytevector_uint_set_x
{
GENERIC_INTEGER_SETTER_PROLOGUE (unsigned);
bytevector_unsigned_set (&c_bv[c_index], c_size, value, endianness,
FUNC_NAME);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_sint_set_x, "bytevector-sint-set!", 5, 0, 0,
(SCM bv, SCM index, SCM value, SCM endianness, SCM size),
"Set the @var{size}-octet long signed integer at @var{index} "
"to @var{value}.")
#define FUNC_NAME s_scm_bytevector_sint_set_x
{
GENERIC_INTEGER_SETTER_PROLOGUE (signed);
bytevector_signed_set (&c_bv[c_index], c_size, value, endianness,
FUNC_NAME);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
/* Operations on integers of arbitrary size. */
#define INTEGERS_TO_LIST(_sign) \
SCM lst, pair; \
size_t i, c_len, c_size; \
\
SCM_VALIDATE_BYTEVECTOR (1, bv); \
SCM_VALIDATE_SYMBOL (2, endianness); \
c_size = scm_to_unsigned_integer (size, 1, (size_t) -1); \
\
c_len = SCM_BYTEVECTOR_LENGTH (bv); \
if (SCM_UNLIKELY (c_len % c_size != 0)) \
scm_wrong_type_arg_msg \
(FUNC_NAME, 0, size, \
"an exact positive integer that divides the bytevector length"); \
else if (SCM_UNLIKELY (c_len == 0)) \
lst = SCM_EOL; \
else \
{ \
const char *c_bv; \
\
c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
\
lst = scm_make_list (scm_from_size_t (c_len / c_size), \
SCM_UNSPECIFIED); \
for (i = 0, pair = lst; \
i <= c_len - c_size; \
i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
{ \
SCM_SETCAR (pair, \
bytevector_ ## _sign ## _ref (c_bv, c_size, \
endianness)); \
} \
} \
\
return lst;
SCM_DEFINE (scm_bytevector_to_sint_list, "bytevector->sint-list",
3, 0, 0,
(SCM bv, SCM endianness, SCM size),
"Return a list of signed integers of @var{size} octets "
"representing the contents of @var{bv}.")
#define FUNC_NAME s_scm_bytevector_to_sint_list
{
INTEGERS_TO_LIST (signed);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_to_uint_list, "bytevector->uint-list",
3, 0, 0,
(SCM bv, SCM endianness, SCM size),
"Return a list of unsigned integers of @var{size} octets "
"representing the contents of @var{bv}.")
#define FUNC_NAME s_scm_bytevector_to_uint_list
{
INTEGERS_TO_LIST (unsigned);
}
#undef FUNC_NAME
#undef INTEGER_TO_LIST
#define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
SCM bv; \
size_t c_len; \
size_t c_size; \
char *c_bv, *c_bv_ptr; \
\
SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
SCM_VALIDATE_SYMBOL (2, endianness); \
c_size = scm_to_size_t (size); \
\
if (SCM_UNLIKELY (c_size == 0 || c_size >= (SIZE_MAX >> 3))) \
scm_out_of_range (FUNC_NAME, size); \
\
bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
\
for (c_bv_ptr = c_bv; \
!scm_is_null (lst); \
lst = SCM_CDR (lst), c_bv_ptr += c_size) \
{ \
bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
SCM_CAR (lst), endianness, \
FUNC_NAME); \
} \
\
return bv;
SCM_DEFINE (scm_uint_list_to_bytevector, "uint-list->bytevector",
3, 0, 0,
(SCM lst, SCM endianness, SCM size),
"Return a bytevector containing the unsigned integers "
"listed in @var{lst} and encoded on @var{size} octets "
"according to @var{endianness}.")
#define FUNC_NAME s_scm_uint_list_to_bytevector
{
INTEGER_LIST_TO_BYTEVECTOR (unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_sint_list_to_bytevector, "sint-list->bytevector",
3, 0, 0,
(SCM lst, SCM endianness, SCM size),
"Return a bytevector containing the signed integers "
"listed in @var{lst} and encoded on @var{size} octets "
"according to @var{endianness}.")
#define FUNC_NAME s_scm_sint_list_to_bytevector
{
INTEGER_LIST_TO_BYTEVECTOR (signed);
}
#undef FUNC_NAME
#undef INTEGER_LIST_TO_BYTEVECTOR
/* Operations on 16-bit integers. */
SCM_DEFINE (scm_bytevector_u16_ref, "bytevector-u16-ref",
3, 0, 0,
(SCM bv, SCM index, SCM endianness),
"Return the unsigned 16-bit integer from @var{bv} at "
"@var{index}.")
#define FUNC_NAME s_scm_bytevector_u16_ref
{
INTEGER_REF (16, unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s16_ref, "bytevector-s16-ref",
3, 0, 0,
(SCM bv, SCM index, SCM endianness),
"Return the signed 16-bit integer from @var{bv} at "
"@var{index}.")
#define FUNC_NAME s_scm_bytevector_s16_ref
{
INTEGER_REF (16, signed);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_u16_native_ref, "bytevector-u16-native-ref",
2, 0, 0,
(SCM bv, SCM index),
"Return the unsigned 16-bit integer from @var{bv} at "
"@var{index} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_u16_native_ref
{
INTEGER_NATIVE_REF (16, unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s16_native_ref, "bytevector-s16-native-ref",
2, 0, 0,
(SCM bv, SCM index),
"Return the unsigned 16-bit integer from @var{bv} at "
"@var{index} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_s16_native_ref
{
INTEGER_NATIVE_REF (16, signed);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_u16_set_x, "bytevector-u16-set!",
4, 0, 0,
(SCM bv, SCM index, SCM value, SCM endianness),
"Store @var{value} in @var{bv} at @var{index} according to "
"@var{endianness}.")
#define FUNC_NAME s_scm_bytevector_u16_set_x
{
INTEGER_SET (16, unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s16_set_x, "bytevector-s16-set!",
4, 0, 0,
(SCM bv, SCM index, SCM value, SCM endianness),
"Store @var{value} in @var{bv} at @var{index} according to "
"@var{endianness}.")
#define FUNC_NAME s_scm_bytevector_s16_set_x
{
INTEGER_SET (16, signed);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_u16_native_set_x, "bytevector-u16-native-set!",
3, 0, 0,
(SCM bv, SCM index, SCM value),
"Store the unsigned integer @var{value} at index @var{index} "
"of @var{bv} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_u16_native_set_x
{
INTEGER_NATIVE_SET (16, unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s16_native_set_x, "bytevector-s16-native-set!",
3, 0, 0,
(SCM bv, SCM index, SCM value),
"Store the signed integer @var{value} at index @var{index} "
"of @var{bv} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_s16_native_set_x
{
INTEGER_NATIVE_SET (16, signed);
}
#undef FUNC_NAME
/* Operations on 32-bit integers. */
/* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
arbitrary 32-bit integers. Thus we fall back to using the
`large_{ref,set}' variants on 32-bit machines. */
#define LARGE_INTEGER_REF(_len, _sign) \
INTEGER_GETTER_PROLOGUE(_len, _sign); \
SCM_VALIDATE_SYMBOL (3, endianness); \
\
return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
SIGNEDNESS (_sign), endianness));
#define LARGE_INTEGER_SET(_len, _sign) \
int err; \
INTEGER_SETTER_PROLOGUE (_len, _sign); \
SCM_VALIDATE_SYMBOL (4, endianness); \
\
err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
SIGNEDNESS (_sign), value, endianness); \
if (SCM_UNLIKELY (err)) \
scm_out_of_range (FUNC_NAME, value); \
\
return SCM_UNSPECIFIED;
#define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
INTEGER_GETTER_PROLOGUE(_len, _sign); \
return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
SIGNEDNESS (_sign), scm_i_native_endianness));
#define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
int err; \
INTEGER_SETTER_PROLOGUE (_len, _sign); \
\
err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
SIGNEDNESS (_sign), value, \
scm_i_native_endianness); \
if (SCM_UNLIKELY (err)) \
scm_out_of_range (FUNC_NAME, value); \
\
return SCM_UNSPECIFIED;
SCM_DEFINE (scm_bytevector_u32_ref, "bytevector-u32-ref",
3, 0, 0,
(SCM bv, SCM index, SCM endianness),
"Return the unsigned 32-bit integer from @var{bv} at "
"@var{index}.")
#define FUNC_NAME s_scm_bytevector_u32_ref
{
#if SIZEOF_VOID_P > 4
INTEGER_REF (32, unsigned);
#else
LARGE_INTEGER_REF (32, unsigned);
#endif
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s32_ref, "bytevector-s32-ref",
3, 0, 0,
(SCM bv, SCM index, SCM endianness),
"Return the signed 32-bit integer from @var{bv} at "
"@var{index}.")
#define FUNC_NAME s_scm_bytevector_s32_ref
{
#if SIZEOF_VOID_P > 4
INTEGER_REF (32, signed);
#else
LARGE_INTEGER_REF (32, signed);
#endif
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_u32_native_ref, "bytevector-u32-native-ref",
2, 0, 0,
(SCM bv, SCM index),
"Return the unsigned 32-bit integer from @var{bv} at "
"@var{index} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_u32_native_ref
{
#if SIZEOF_VOID_P > 4
INTEGER_NATIVE_REF (32, unsigned);
#else
LARGE_INTEGER_NATIVE_REF (32, unsigned);
#endif
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s32_native_ref, "bytevector-s32-native-ref",
2, 0, 0,
(SCM bv, SCM index),
"Return the unsigned 32-bit integer from @var{bv} at "
"@var{index} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_s32_native_ref
{
#if SIZEOF_VOID_P > 4
INTEGER_NATIVE_REF (32, signed);
#else
LARGE_INTEGER_NATIVE_REF (32, signed);
#endif
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_u32_set_x, "bytevector-u32-set!",
4, 0, 0,
(SCM bv, SCM index, SCM value, SCM endianness),
"Store @var{value} in @var{bv} at @var{index} according to "
"@var{endianness}.")
#define FUNC_NAME s_scm_bytevector_u32_set_x
{
#if SIZEOF_VOID_P > 4
INTEGER_SET (32, unsigned);
#else
LARGE_INTEGER_SET (32, unsigned);
#endif
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s32_set_x, "bytevector-s32-set!",
4, 0, 0,
(SCM bv, SCM index, SCM value, SCM endianness),
"Store @var{value} in @var{bv} at @var{index} according to "
"@var{endianness}.")
#define FUNC_NAME s_scm_bytevector_s32_set_x
{
#if SIZEOF_VOID_P > 4
INTEGER_SET (32, signed);
#else
LARGE_INTEGER_SET (32, signed);
#endif
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_u32_native_set_x, "bytevector-u32-native-set!",
3, 0, 0,
(SCM bv, SCM index, SCM value),
"Store the unsigned integer @var{value} at index @var{index} "
"of @var{bv} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_u32_native_set_x
{
#if SIZEOF_VOID_P > 4
INTEGER_NATIVE_SET (32, unsigned);
#else
LARGE_INTEGER_NATIVE_SET (32, unsigned);
#endif
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s32_native_set_x, "bytevector-s32-native-set!",
3, 0, 0,
(SCM bv, SCM index, SCM value),
"Store the signed integer @var{value} at index @var{index} "
"of @var{bv} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_s32_native_set_x
{
#if SIZEOF_VOID_P > 4
INTEGER_NATIVE_SET (32, signed);
#else
LARGE_INTEGER_NATIVE_SET (32, signed);
#endif
}
#undef FUNC_NAME
/* Operations on 64-bit integers. */
/* For 64-bit integers, we use only the `large_{ref,set}' variant. */
SCM_DEFINE (scm_bytevector_u64_ref, "bytevector-u64-ref",
3, 0, 0,
(SCM bv, SCM index, SCM endianness),
"Return the unsigned 64-bit integer from @var{bv} at "
"@var{index}.")
#define FUNC_NAME s_scm_bytevector_u64_ref
{
LARGE_INTEGER_REF (64, unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s64_ref, "bytevector-s64-ref",
3, 0, 0,
(SCM bv, SCM index, SCM endianness),
"Return the signed 64-bit integer from @var{bv} at "
"@var{index}.")
#define FUNC_NAME s_scm_bytevector_s64_ref
{
LARGE_INTEGER_REF (64, signed);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_u64_native_ref, "bytevector-u64-native-ref",
2, 0, 0,
(SCM bv, SCM index),
"Return the unsigned 64-bit integer from @var{bv} at "
"@var{index} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_u64_native_ref
{
LARGE_INTEGER_NATIVE_REF (64, unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s64_native_ref, "bytevector-s64-native-ref",
2, 0, 0,
(SCM bv, SCM index),
"Return the unsigned 64-bit integer from @var{bv} at "
"@var{index} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_s64_native_ref
{
LARGE_INTEGER_NATIVE_REF (64, signed);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_u64_set_x, "bytevector-u64-set!",
4, 0, 0,
(SCM bv, SCM index, SCM value, SCM endianness),
"Store @var{value} in @var{bv} at @var{index} according to "
"@var{endianness}.")
#define FUNC_NAME s_scm_bytevector_u64_set_x
{
LARGE_INTEGER_SET (64, unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s64_set_x, "bytevector-s64-set!",
4, 0, 0,
(SCM bv, SCM index, SCM value, SCM endianness),
"Store @var{value} in @var{bv} at @var{index} according to "
"@var{endianness}.")
#define FUNC_NAME s_scm_bytevector_s64_set_x
{
LARGE_INTEGER_SET (64, signed);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_u64_native_set_x, "bytevector-u64-native-set!",
3, 0, 0,
(SCM bv, SCM index, SCM value),
"Store the unsigned integer @var{value} at index @var{index} "
"of @var{bv} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_u64_native_set_x
{
LARGE_INTEGER_NATIVE_SET (64, unsigned);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_s64_native_set_x, "bytevector-s64-native-set!",
3, 0, 0,
(SCM bv, SCM index, SCM value),
"Store the signed integer @var{value} at index @var{index} "
"of @var{bv} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_s64_native_set_x
{
LARGE_INTEGER_NATIVE_SET (64, signed);
}
#undef FUNC_NAME
/* Operations on IEEE-754 numbers. */
/* There are two possible word endians, visible in glibc's <ieee754.h>.
However, in R6RS, when the endianness is `little', little endian is
assumed for both the byte order and the word order. This is clear from
Section 2.1 of R6RS-lib (in response to
http://www.r6rs.org/formal-comments/comment-187.txt). */
union scm_ieee754_float
{
float f;
uint32_t i;
};
union scm_ieee754_double
{
double d;
uint64_t i;
};
/* Convert to/from a floating-point number with different endianness. This
method is probably not the most efficient but it should be portable. */
static inline void
float_to_foreign_endianness (union scm_ieee754_float *target,
float source)
{
union scm_ieee754_float input;
input.f = source;
target->i = bswap_32 (input.i);
}
static inline float
float_from_foreign_endianness (const union scm_ieee754_float *source)
{
union scm_ieee754_float result;
result.i = bswap_32 (source->i);
return (result.f);
}
static inline void
double_to_foreign_endianness (union scm_ieee754_double *target,
double source)
{
union scm_ieee754_double input;
input.d = source;
target->i = bswap_64 (input.i);
}
static inline double
double_from_foreign_endianness (const union scm_ieee754_double *source)
{
union scm_ieee754_double result;
result.i = bswap_64 (source->i);
return (result.d);
}
/* Template macros to abstract over doubles and floats.
XXX: Guile can only convert to/from doubles. */
#define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
#define IEEE754_TO_SCM(_c_type) scm_from_double
#define IEEE754_FROM_SCM(_c_type) scm_to_double
#define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
_c_type ## _from_foreign_endianness
#define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
_c_type ## _to_foreign_endianness
/* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
#define VALIDATE_REAL(pos, v) \
do { \
SCM_ASSERT_TYPE (scm_is_real (v), v, pos, FUNC_NAME, "real"); \
} while (0)
/* Templace getters and setters. */
#define IEEE754_GETTER_PROLOGUE(_type) \
INTEGER_GETTER_PROLOGUE (sizeof (_type) << 3UL, signed);
#define IEEE754_SETTER_PROLOGUE(_type) \
INTEGER_SETTER_PROLOGUE (sizeof (_type) << 3UL, signed);
#define IEEE754_REF(_type) \
_type c_result; \
\
IEEE754_GETTER_PROLOGUE (_type); \
SCM_VALIDATE_SYMBOL (3, endianness); \
\
if (scm_is_eq (endianness, scm_i_native_endianness)) \
memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
else \
{ \
IEEE754_UNION (_type) c_raw; \
\
memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
c_result = \
IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
} \
\
return (IEEE754_TO_SCM (_type) (c_result));
#define IEEE754_NATIVE_REF(_type) \
_type c_result; \
\
IEEE754_GETTER_PROLOGUE (_type); \
\
memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
return (IEEE754_TO_SCM (_type) (c_result));
#define IEEE754_SET(_type) \
_type c_value; \
\
IEEE754_SETTER_PROLOGUE (_type); \
VALIDATE_REAL (3, value); \
SCM_VALIDATE_SYMBOL (4, endianness); \
c_value = IEEE754_FROM_SCM (_type) (value); \
\
if (scm_is_eq (endianness, scm_i_native_endianness)) \
memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
else \
{ \
IEEE754_UNION (_type) c_raw; \
\
IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
} \
\
return SCM_UNSPECIFIED;
#define IEEE754_NATIVE_SET(_type) \
_type c_value; \
\
IEEE754_SETTER_PROLOGUE (_type); \
VALIDATE_REAL (3, value); \
c_value = IEEE754_FROM_SCM (_type) (value); \
\
memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
return SCM_UNSPECIFIED;
/* Single precision. */
SCM_DEFINE (scm_bytevector_ieee_single_ref,
"bytevector-ieee-single-ref",
3, 0, 0,
(SCM bv, SCM index, SCM endianness),
"Return the IEEE-754 single from @var{bv} at "
"@var{index}.")
#define FUNC_NAME s_scm_bytevector_ieee_single_ref
{
IEEE754_REF (float);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_ieee_single_native_ref,
"bytevector-ieee-single-native-ref",
2, 0, 0,
(SCM bv, SCM index),
"Return the IEEE-754 single from @var{bv} at "
"@var{index} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
{
IEEE754_NATIVE_REF (float);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_ieee_single_set_x,
"bytevector-ieee-single-set!",
4, 0, 0,
(SCM bv, SCM index, SCM value, SCM endianness),
"Store real @var{value} in @var{bv} at @var{index} according to "
"@var{endianness}.")
#define FUNC_NAME s_scm_bytevector_ieee_single_set_x
{
IEEE754_SET (float);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_ieee_single_native_set_x,
"bytevector-ieee-single-native-set!",
3, 0, 0,
(SCM bv, SCM index, SCM value),
"Store the real @var{value} at index @var{index} "
"of @var{bv} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
{
IEEE754_NATIVE_SET (float);
}
#undef FUNC_NAME
/* Double precision. */
SCM_DEFINE (scm_bytevector_ieee_double_ref,
"bytevector-ieee-double-ref",
3, 0, 0,
(SCM bv, SCM index, SCM endianness),
"Return the IEEE-754 double from @var{bv} at "
"@var{index}.")
#define FUNC_NAME s_scm_bytevector_ieee_double_ref
{
IEEE754_REF (double);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_ieee_double_native_ref,
"bytevector-ieee-double-native-ref",
2, 0, 0,
(SCM bv, SCM index),
"Return the IEEE-754 double from @var{bv} at "
"@var{index} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
{
IEEE754_NATIVE_REF (double);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_ieee_double_set_x,
"bytevector-ieee-double-set!",
4, 0, 0,
(SCM bv, SCM index, SCM value, SCM endianness),
"Store real @var{value} in @var{bv} at @var{index} according to "
"@var{endianness}.")
#define FUNC_NAME s_scm_bytevector_ieee_double_set_x
{
IEEE754_SET (double);
}
#undef FUNC_NAME
SCM_DEFINE (scm_bytevector_ieee_double_native_set_x,
"bytevector-ieee-double-native-set!",
3, 0, 0,
(SCM bv, SCM index, SCM value),
"Store the real @var{value} at index @var{index} "
"of @var{bv} using the native endianness.")
#define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
{
IEEE754_NATIVE_SET (double);
}
#undef FUNC_NAME
#undef IEEE754_UNION
#undef IEEE754_TO_SCM
#undef IEEE754_FROM_SCM
#undef IEEE754_FROM_FOREIGN_ENDIANNESS
#undef IEEE754_TO_FOREIGN_ENDIANNESS
#undef IEEE754_REF
#undef IEEE754_NATIVE_REF
#undef IEEE754_SET
#undef IEEE754_NATIVE_SET
/* Operations on strings. */
/* Produce a function that returns the length of a UTF-encoded string. */
#define UTF_STRLEN_FUNCTION(_utf_width) \
static inline size_t \
utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
{ \
size_t len = 0; \
const uint ## _utf_width ## _t *ptr; \
for (ptr = str; \
*ptr != 0; \
ptr++) \
{ \
len++; \
} \
\
return (len * ((_utf_width) / 8)); \
}
UTF_STRLEN_FUNCTION (8)
/* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
#define UTF_STRLEN(_utf_width, _str) \
utf ## _utf_width ## _strlen (_str)
/* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
encoding name). */
static inline void
utf_encoding_name (char *name, size_t utf_width, SCM endianness)
{
strcpy (name, "UTF-");
strcat (name, ((utf_width == 8)
? "8"
: ((utf_width == 16)
? "16"
: ((utf_width == 32)
? "32"
: "??"))));
strcat (name,
((scm_is_eq (endianness, sym_big))
? "BE"
: ((scm_is_eq (endianness, sym_little))
? "LE"
: "unknown")));
}
/* Maximum length of a UTF encoding name. */
#define MAX_UTF_ENCODING_NAME_LEN 16
/* Produce the body of a `string->utf' function. */
#define STRING_TO_UTF(_utf_width) \
SCM utf; \
int err; \
char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
char *c_utf = NULL; \
size_t c_strlen, c_utf_len = 0; \
\
SCM_VALIDATE_STRING (1, str); \
if (scm_is_eq (endianness, SCM_UNDEFINED)) \
endianness = sym_big; \
else \
SCM_VALIDATE_SYMBOL (2, endianness); \
\
utf_encoding_name (c_utf_name, (_utf_width), endianness); \
\
c_strlen = scm_i_string_length (str); \
if (scm_i_is_narrow_string (str)) \
{ \
err = mem_iconveh (scm_i_string_chars (str), c_strlen, \
"ISO-8859-1", c_utf_name, \
iconveh_question_mark, NULL, \
&c_utf, &c_utf_len); \
if (SCM_UNLIKELY (err)) \
scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
scm_list_1 (str), err); \
} \
else \
{ \
const scm_t_wchar *wbuf = scm_i_string_wide_chars (str); \
c_utf = u32_conv_to_encoding (c_utf_name, \
iconveh_question_mark, \
(uint32_t *) wbuf, \
c_strlen, NULL, NULL, &c_utf_len); \
if (SCM_UNLIKELY (c_utf == NULL)) \
scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
scm_list_1 (str), errno); \
} \
scm_dynwind_begin (0); \
scm_dynwind_free (c_utf); \
utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8); \
memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len); \
scm_dynwind_end (); \
\
return (utf);
SCM_DEFINE (scm_string_to_utf8, "string->utf8",
1, 0, 0,
(SCM str),
"Return a newly allocated bytevector that contains the UTF-8 "
"encoding of @var{str}.")
#define FUNC_NAME s_scm_string_to_utf8
{
SCM utf;
uint8_t *c_utf;
size_t c_utf_len = 0;
SCM_VALIDATE_STRING (1, str);
c_utf = (uint8_t *) scm_to_utf8_stringn (str, &c_utf_len);
utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len);
free (c_utf);
return (utf);
}
#undef FUNC_NAME
SCM_DEFINE (scm_string_to_utf16, "string->utf16",
1, 1, 0,
(SCM str, SCM endianness),
"Return a newly allocated bytevector that contains the UTF-16 "
"encoding of @var{str}.")
#define FUNC_NAME s_scm_string_to_utf16
{
STRING_TO_UTF (16);
}
#undef FUNC_NAME
static void
swap_u32 (scm_t_wchar *vals, size_t len)
{
size_t n;
for (n = 0; n < len; n++)
vals[n] = bswap_32 (vals[n]);
}
SCM_DEFINE (scm_string_to_utf32, "string->utf32",
1, 1, 0,
(SCM str, SCM endianness),
"Return a newly allocated bytevector that contains the UTF-32 "
"encoding of @var{str}.")
#define FUNC_NAME s_scm_string_to_utf32
{
SCM bv;
scm_t_wchar *wchars;
size_t wchar_len, bytes_len;
wchars = scm_to_utf32_stringn (str, &wchar_len);
bytes_len = wchar_len * sizeof (scm_t_wchar);
if (!scm_is_eq (SCM_UNBNDP (endianness) ? scm_endianness_big : endianness,
scm_i_native_endianness))
swap_u32 (wchars, wchar_len);
bv = make_bytevector (bytes_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
memcpy (SCM_BYTEVECTOR_CONTENTS (bv), wchars, bytes_len);
free (wchars);
return bv;
}
#undef FUNC_NAME
/* Produce the body of a function that converts a UTF-encoded bytevector to a
string. */
#define UTF_TO_STRING(_utf_width) \
SCM str = SCM_BOOL_F; \
int err; \
char *c_str = NULL; \
char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
char *c_utf; \
size_t c_strlen = 0, c_utf_len = 0; \
\
SCM_VALIDATE_BYTEVECTOR (1, utf); \
if (scm_is_eq (endianness, SCM_UNDEFINED)) \
endianness = sym_big; \
else \
SCM_VALIDATE_SYMBOL (2, endianness); \
\
c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
utf_encoding_name (c_utf_name, (_utf_width), endianness); \
\
err = mem_iconveh (c_utf, c_utf_len, \
c_utf_name, "UTF-8", \
iconveh_question_mark, NULL, \
&c_str, &c_strlen); \
if (SCM_UNLIKELY (err)) \
scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
scm_list_1 (utf), err); \
else \
{ \
str = scm_from_utf8_stringn (c_str, c_strlen); \
free (c_str); \
} \
return (str);
SCM_DEFINE (scm_utf8_to_string, "utf8->string",
1, 0, 0,
(SCM utf),
"Return a newly allocate string that contains from the UTF-8-"
"encoded contents of bytevector @var{utf}.")
#define FUNC_NAME s_scm_utf8_to_string
{
SCM str;
const char *c_utf;
size_t c_utf_len = 0;
SCM_VALIDATE_BYTEVECTOR (1, utf);
c_utf_len = SCM_BYTEVECTOR_LENGTH (utf);
c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf);
str = scm_from_utf8_stringn (c_utf, c_utf_len);
return (str);
}
#undef FUNC_NAME
SCM_DEFINE (scm_utf16_to_string, "utf16->string",
1, 1, 0,
(SCM utf, SCM endianness),
"Return a newly allocate string that contains from the UTF-16-"
"encoded contents of bytevector @var{utf}.")
#define FUNC_NAME s_scm_utf16_to_string
{
UTF_TO_STRING (16);
}
#undef FUNC_NAME
SCM_DEFINE (scm_utf32_to_string, "utf32->string",
1, 1, 0,
(SCM utf, SCM endianness),
"Return a newly allocate string that contains from the UTF-32-"
"encoded contents of bytevector @var{utf}.")
#define FUNC_NAME s_scm_utf32_to_string
{
UTF_TO_STRING (32);
}
#undef FUNC_NAME
/* Initialization. */
void
scm_bootstrap_bytevectors (void)
{
/* This must be instantiated here because the generalized-vector API may
want to access bytevectors even though `(rnrs bytevectors)' hasn't been
loaded. */
scm_null_bytevector = make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8);
scm_endianness_big = sym_big = scm_from_latin1_symbol ("big");
scm_endianness_little = sym_little = scm_from_latin1_symbol ("little");
#ifdef WORDS_BIGENDIAN
scm_i_native_endianness = sym_big;
#else
scm_i_native_endianness = sym_little;
#endif
scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
"scm_init_bytevectors",
(scm_t_extension_init_func) scm_init_bytevectors,
NULL);
scm_i_register_vector_constructor
(scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_VU8],
scm_make_bytevector);
}
void
scm_init_bytevectors (void)
{
#include "bytevectors.x"
}
|