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
|
/*
* Copyright 2013 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include "b64_ntop.h"
#include "bson.h"
#include "bson-private.h"
#include "bson-string.h"
#ifndef BSON_MAX_RECURSION
# define BSON_MAX_RECURSION 100
#endif
typedef enum {
BSON_VALIDATE_PHASE_START,
BSON_VALIDATE_PHASE_TOP,
BSON_VALIDATE_PHASE_LF_REF_KEY,
BSON_VALIDATE_PHASE_LF_REF_UTF8,
BSON_VALIDATE_PHASE_LF_ID_KEY,
BSON_VALIDATE_PHASE_LF_DB_KEY,
BSON_VALIDATE_PHASE_LF_DB_UTF8,
BSON_VALIDATE_PHASE_NOT_DBREF,
} bson_validate_phase_t;
/*
* Structures.
*/
typedef struct
{
bson_validate_flags_t flags;
ssize_t err_offset;
bson_validate_phase_t phase;
} bson_validate_state_t;
/*
* Globals.
*/
static const uint8_t gZero;
/*
*--------------------------------------------------------------------------
*
* _bson_impl_inline_grow --
*
* Document growth implementation for documents that currently
* contain stack based buffers. The document may be switched to
* a malloc based buffer.
*
* Returns:
* true if successful; otherwise false indicating INT_MAX overflow.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static bool
_bson_impl_inline_grow (bson_impl_inline_t *impl, /* IN */
size_t size) /* IN */
{
bson_impl_alloc_t *alloc = (bson_impl_alloc_t *)impl;
uint8_t *data;
size_t req;
BSON_ASSERT (impl);
BSON_ASSERT (!(impl->flags & BSON_FLAG_RDONLY));
BSON_ASSERT (!(impl->flags & BSON_FLAG_CHILD));
if (((size_t)impl->len + size) <= sizeof impl->data) {
return true;
}
req = bson_next_power_of_two (impl->len + size);
if (req <= INT32_MAX) {
data = bson_malloc (req);
memcpy (data, impl->data, impl->len);
alloc->flags &= ~BSON_FLAG_INLINE;
alloc->parent = NULL;
alloc->depth = 0;
alloc->buf = &alloc->alloc;
alloc->buflen = &alloc->alloclen;
alloc->offset = 0;
alloc->alloc = data;
alloc->alloclen = req;
alloc->realloc = bson_realloc_ctx;
alloc->realloc_func_ctx = NULL;
return true;
}
return false;
}
/*
*--------------------------------------------------------------------------
*
* _bson_impl_alloc_grow --
*
* Document growth implementation for documents containing malloc
* based buffers.
*
* Returns:
* true if successful; otherwise false indicating INT_MAX overflow.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static bool
_bson_impl_alloc_grow (bson_impl_alloc_t *impl, /* IN */
size_t size) /* IN */
{
size_t req;
BSON_ASSERT (impl);
/*
* Determine how many bytes we need for this document in the buffer
* including necessary trailing bytes for parent documents.
*/
req = (impl->offset + impl->len + size + impl->depth);
if (req <= *impl->buflen) {
return true;
}
req = bson_next_power_of_two (req);
if ((req <= INT32_MAX) && impl->realloc) {
*impl->buf = impl->realloc (*impl->buf, req, impl->realloc_func_ctx);
*impl->buflen = req;
return true;
}
return false;
}
/*
*--------------------------------------------------------------------------
*
* _bson_grow --
*
* Grows the bson_t structure to be large enough to contain @size
* bytes.
*
* Returns:
* true if successful, false if the size would overflow.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static bool
_bson_grow (bson_t *bson, /* IN */
uint32_t size) /* IN */
{
BSON_ASSERT (bson);
BSON_ASSERT (!(bson->flags & BSON_FLAG_RDONLY));
if ((bson->flags & BSON_FLAG_INLINE)) {
return _bson_impl_inline_grow ((bson_impl_inline_t *)bson, size);
}
return _bson_impl_alloc_grow ((bson_impl_alloc_t *)bson, size);
}
/*
*--------------------------------------------------------------------------
*
* _bson_data --
*
* A helper function to return the contents of the bson document
* taking into account the polymorphic nature of bson_t.
*
* Returns:
* A buffer which should not be modified or freed.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static BSON_INLINE uint8_t *
_bson_data (const bson_t *bson) /* IN */
{
if ((bson->flags & BSON_FLAG_INLINE)) {
return ((bson_impl_inline_t *)bson)->data;
} else {
bson_impl_alloc_t *impl = (bson_impl_alloc_t *)bson;
return (*impl->buf) + impl->offset;
}
}
/*
*--------------------------------------------------------------------------
*
* _bson_encode_length --
*
* Helper to encode the length of the bson_t in the first 4 bytes
* of the bson document. Little endian format is used as specified
* by bsonspec.
*
* Returns:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static BSON_INLINE void
_bson_encode_length (bson_t *bson) /* IN */
{
#if BSON_BYTE_ORDER == BSON_LITTLE_ENDIAN
memcpy (_bson_data (bson), &bson->len, sizeof (bson->len));
#else
uint32_t length_le = BSON_UINT32_TO_LE (bson->len);
memcpy (_bson_data (bson), &length_le, sizeof (length_le));
#endif
}
/*
*--------------------------------------------------------------------------
*
* _bson_append_va --
*
* Appends the length,buffer pairs to the bson_t. @n_bytes is an
* optimization to perform one array growth rather than many small
* growths.
*
* @bson: A bson_t
* @n_bytes: The number of bytes to append to the document.
* @n_pairs: The number of length,buffer pairs.
* @first_len: Length of first buffer.
* @first_data: First buffer.
* @args: va_list of additional tuples.
*
* Returns:
* true if the bytes were appended successfully.
* false if it bson would overflow INT_MAX.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static BSON_INLINE bool
_bson_append_va (bson_t *bson, /* IN */
uint32_t n_bytes, /* IN */
uint32_t n_pairs, /* IN */
uint32_t first_len, /* IN */
const uint8_t *first_data, /* IN */
va_list args) /* IN */
{
const uint8_t *data;
uint32_t data_len;
uint8_t *buf;
BSON_ASSERT (bson);
BSON_ASSERT (!(bson->flags & BSON_FLAG_IN_CHILD));
BSON_ASSERT (!(bson->flags & BSON_FLAG_RDONLY));
BSON_ASSERT (n_pairs);
BSON_ASSERT (first_len);
BSON_ASSERT (first_data);
if (BSON_UNLIKELY (!_bson_grow (bson, n_bytes))) {
return false;
}
data = first_data;
data_len = first_len;
buf = _bson_data (bson) + bson->len - 1;
do {
n_pairs--;
memcpy (buf, data, data_len);
bson->len += data_len;
buf += data_len;
if (n_pairs) {
data_len = va_arg (args, uint32_t);
data = va_arg (args, const uint8_t *);
}
} while (n_pairs);
_bson_encode_length (bson);
*buf = '\0';
return true;
}
/*
*--------------------------------------------------------------------------
*
* _bson_append --
*
* Variadic function to append length,buffer pairs to a bson_t. If the
* append would cause the bson_t to overflow a 32-bit length, it will
* return false and no append will have occurred.
*
* Parameters:
* @bson: A bson_t.
* @n_pairs: Number of length,buffer pairs.
* @n_bytes: the total number of bytes being appended.
* @first_len: Length of first buffer.
* @first_data: First buffer.
*
* Returns:
* true if successful; otherwise false indicating INT_MAX overflow.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
static bool
_bson_append (bson_t *bson, /* IN */
uint32_t n_pairs, /* IN */
uint32_t n_bytes, /* IN */
uint32_t first_len, /* IN */
const uint8_t *first_data, /* IN */
...)
{
va_list args;
bool ok;
BSON_ASSERT (bson);
BSON_ASSERT (n_pairs);
BSON_ASSERT (first_len);
BSON_ASSERT (first_data);
/*
* Check to see if this append would overflow 32-bit signed integer. I know
* what you're thinking. BSON uses a signed 32-bit length field? Yeah. It
* does.
*/
if (BSON_UNLIKELY (n_bytes > (BSON_MAX_SIZE - bson->len))) {
return false;
}
va_start (args, first_data);
ok = _bson_append_va (bson, n_bytes, n_pairs, first_len, first_data, args);
va_end (args);
return ok;
}
/*
*--------------------------------------------------------------------------
*
* _bson_append_bson_begin --
*
* Begin appending a subdocument or subarray to the document using
* the key provided by @key.
*
* If @key_length is < 0, then strlen() will be called on @key
* to determine the length.
*
* @key_type MUST be either BSON_TYPE_DOCUMENT or BSON_TYPE_ARRAY.
*
* Returns:
* true if successful; otherwise false indiciating INT_MAX overflow.
*
* Side effects:
* @child is initialized if true is returned.
*
*--------------------------------------------------------------------------
*/
static bool
_bson_append_bson_begin (bson_t *bson, /* IN */
const char *key, /* IN */
int key_length, /* IN */
bson_type_t child_type, /* IN */
bson_t *child) /* OUT */
{
const uint8_t type = child_type;
const uint8_t empty[5] = { 5 };
bson_impl_alloc_t *aparent = (bson_impl_alloc_t *)bson;
bson_impl_alloc_t *achild = (bson_impl_alloc_t *)child;
BSON_ASSERT (bson);
BSON_ASSERT (!(bson->flags & BSON_FLAG_RDONLY));
BSON_ASSERT (!(bson->flags & BSON_FLAG_IN_CHILD));
BSON_ASSERT (key);
BSON_ASSERT ((child_type == BSON_TYPE_DOCUMENT) ||
(child_type == BSON_TYPE_ARRAY));
BSON_ASSERT (child);
if (key_length < 0) {
key_length = (int)strlen (key);
}
/*
* If the parent is an inline bson_t, then we need to convert
* it to a heap allocated buffer. This makes extending buffers
* of child bson documents much simpler logic, as they can just
* realloc the *buf pointer.
*/
if ((bson->flags & BSON_FLAG_INLINE)) {
BSON_ASSERT (bson->len <= 120);
if (!_bson_grow (bson, 128 - bson->len)) {
return false;
}
BSON_ASSERT (!(bson->flags & BSON_FLAG_INLINE));
}
/*
* Append the type and key for the field.
*/
if (!_bson_append (bson, 4,
(1 + key_length + 1 + 5),
1, &type,
key_length, key,
1, &gZero,
5, empty)) {
return false;
}
/*
* Mark the document as working on a child document so that no
* further modifications can happen until the caller has called
* bson_append_{document,array}_end().
*/
bson->flags |= BSON_FLAG_IN_CHILD;
/*
* Initialize the child bson_t structure and point it at the parents
* buffers. This allows us to realloc directly from the child without
* walking up to the parent bson_t.
*/
achild->flags = (BSON_FLAG_CHILD | BSON_FLAG_NO_FREE | BSON_FLAG_STATIC);
if ((bson->flags & BSON_FLAG_CHILD)) {
achild->depth = ((bson_impl_alloc_t *)bson)->depth + 1;
} else {
achild->depth = 1;
}
achild->parent = bson;
achild->buf = aparent->buf;
achild->buflen = aparent->buflen;
achild->offset = aparent->offset + aparent->len - 1 - 5;
achild->len = 5;
achild->alloc = NULL;
achild->alloclen = 0;
achild->realloc = aparent->realloc;
achild->realloc_func_ctx = aparent->realloc_func_ctx;
return true;
}
/*
*--------------------------------------------------------------------------
*
* _bson_append_bson_end --
*
* Complete a call to _bson_append_bson_begin.
*
* Returns:
* true if successful; otherwise false indiciating INT_MAX overflow.
*
* Side effects:
* @child is destroyed and no longer valid after calling this
* function.
*
*--------------------------------------------------------------------------
*/
static bool
_bson_append_bson_end (bson_t *bson, /* IN */
bson_t *child) /* IN */
{
BSON_ASSERT (bson);
BSON_ASSERT ((bson->flags & BSON_FLAG_IN_CHILD));
BSON_ASSERT (!(child->flags & BSON_FLAG_IN_CHILD));
/*
* Unmark the IN_CHILD flag.
*/
bson->flags &= ~BSON_FLAG_IN_CHILD;
/*
* Now that we are done building the sub-document, add the size to the
* parent, not including the default 5 byte empty document already added.
*/
bson->len = (bson->len + child->len - 5);
/*
* Ensure we have a \0 byte at the end and proper length encoded at
* the beginning of the document.
*/
_bson_data (bson)[bson->len - 1] = '\0';
_bson_encode_length (bson);
return true;
}
/*
*--------------------------------------------------------------------------
*
* bson_append_array_begin --
*
* Start appending a new array.
*
* Use @child to append to the data area for the given field.
*
* It is a programming error to call any other bson function on
* @bson until bson_append_array_end() has been called. It is
* valid to call bson_append*() functions on @child.
*
* This function is useful to allow building nested documents using
* a single buffer owned by the top-level bson document.
*
* Returns:
* true if successful; otherwise false and @child is invalid.
*
* Side effects:
* @child is initialized if true is returned.
*
*--------------------------------------------------------------------------
*/
bool
bson_append_array_begin (bson_t *bson, /* IN */
const char *key, /* IN */
int key_length, /* IN */
bson_t *child) /* IN */
{
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (child, false);
return _bson_append_bson_begin (bson, key, key_length, BSON_TYPE_ARRAY,
child);
}
/*
*--------------------------------------------------------------------------
*
* bson_append_array_end --
*
* Complete a call to bson_append_array_begin().
*
* It is safe to append other fields to @bson after calling this
* function.
*
* Returns:
* true if successful; otherwise false indiciating INT_MAX overflow.
*
* Side effects:
* @child is invalid after calling this function.
*
*--------------------------------------------------------------------------
*/
bool
bson_append_array_end (bson_t *bson, /* IN */
bson_t *child) /* IN */
{
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (child, false);
return _bson_append_bson_end (bson, child);
}
/*
*--------------------------------------------------------------------------
*
* bson_append_document_begin --
*
* Start appending a new document.
*
* Use @child to append to the data area for the given field.
*
* It is a programming error to call any other bson function on
* @bson until bson_append_document_end() has been called. It is
* valid to call bson_append*() functions on @child.
*
* This function is useful to allow building nested documents using
* a single buffer owned by the top-level bson document.
*
* Returns:
* true if successful; otherwise false and @child is invalid.
*
* Side effects:
* @child is initialized if true is returned.
*
*--------------------------------------------------------------------------
*/
bool
bson_append_document_begin (bson_t *bson, /* IN */
const char *key, /* IN */
int key_length, /* IN */
bson_t *child) /* IN */
{
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (child, false);
return _bson_append_bson_begin (bson, key, key_length, BSON_TYPE_DOCUMENT,
child);
}
/*
*--------------------------------------------------------------------------
*
* bson_append_document_end --
*
* Complete a call to bson_append_document_begin().
*
* It is safe to append new fields to @bson after calling this
* function, if true is returned.
*
* Returns:
* true if successful; otherwise false indicating INT_MAX overflow.
*
* Side effects:
* @child is destroyed and invalid after calling this function.
*
*--------------------------------------------------------------------------
*/
bool
bson_append_document_end (bson_t *bson, /* IN */
bson_t *child) /* IN */
{
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (child, false);
return _bson_append_bson_end (bson, child);
}
/*
*--------------------------------------------------------------------------
*
* bson_append_array --
*
* Append an array to @bson.
*
* Generally, bson_append_array_begin() will result in faster code
* since few buffers need to be malloced.
*
* Returns:
* true if successful; otherwise false indiciating INT_MAX overflow.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
bool
bson_append_array (bson_t *bson, /* IN */
const char *key, /* IN */
int key_length, /* IN */
const bson_t *array) /* IN */
{
static const uint8_t type = BSON_TYPE_ARRAY;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (array, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
/*
* Let's be a bit pedantic and ensure the array has properly formatted key
* names. We will verify this simply by checking the first element for "0"
* if the array is non-empty.
*/
if (array && !bson_empty (array)) {
bson_iter_t iter;
if (bson_iter_init (&iter, array) && bson_iter_next (&iter)) {
if (0 != strcmp ("0", bson_iter_key (&iter))) {
fprintf (stderr,
"%s(): invalid array detected. first element of array "
"parameter is not \"0\".\n",
__FUNCTION__);
}
}
}
return _bson_append (bson, 4,
(1 + key_length + 1 + array->len),
1, &type,
key_length, key,
1, &gZero,
array->len, _bson_data (array));
}
/*
*--------------------------------------------------------------------------
*
* bson_append_binary --
*
* Append binary data to @bson. The field will have the
* BSON_TYPE_BINARY type.
*
* Parameters:
* @subtype: the BSON Binary Subtype. See bsonspec.org for more
* information.
* @binary: a pointer to the raw binary data.
* @length: the size of @binary in bytes.
*
* Returns:
* true if successful; otherwise false.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
bool
bson_append_binary (bson_t *bson, /* IN */
const char *key, /* IN */
int key_length, /* IN */
bson_subtype_t subtype, /* IN */
const uint8_t *binary, /* IN */
uint32_t length) /* IN */
{
static const uint8_t type = BSON_TYPE_BINARY;
uint32_t length_le;
uint32_t deprecated_length_le;
uint8_t subtype8 = 0;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (binary, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
subtype8 = subtype;
if (subtype == BSON_SUBTYPE_BINARY_DEPRECATED) {
length_le = BSON_UINT32_TO_LE (length + 4);
deprecated_length_le = BSON_UINT32_TO_LE (length);
return _bson_append (bson, 7,
(1 + key_length + 1 + 4 + 1 + 4 + length),
1, &type,
key_length, key,
1, &gZero,
4, &length_le,
1, &subtype8,
4, &deprecated_length_le,
length, binary);
} else {
length_le = BSON_UINT32_TO_LE (length);
return _bson_append (bson, 6,
(1 + key_length + 1 + 4 + 1 + length),
1, &type,
key_length, key,
1, &gZero,
4, &length_le,
1, &subtype8,
length, binary);
}
}
/*
*--------------------------------------------------------------------------
*
* bson_append_bool --
*
* Append a new field to @bson with the name @key. The value is
* a boolean indicated by @value.
*
* Returns:
* true if succesful; otherwise false.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
bool
bson_append_bool (bson_t *bson, /* IN */
const char *key, /* IN */
int key_length, /* IN */
bool value) /* IN */
{
static const uint8_t type = BSON_TYPE_BOOL;
uint8_t byte = !!value;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
return _bson_append (bson, 4,
(1 + key_length + 1 + 1),
1, &type,
key_length, key,
1, &gZero,
1, &byte);
}
/*
*--------------------------------------------------------------------------
*
* bson_append_code --
*
* Append a new field to @bson containing javascript code.
*
* @javascript MUST be a zero terminated UTF-8 string. It MUST NOT
* containing embedded \0 characters.
*
* Returns:
* true if successful; otherwise false.
*
* Side effects:
* None.
*
* See also:
* bson_append_code_with_scope().
*
*--------------------------------------------------------------------------
*/
bool
bson_append_code (bson_t *bson, /* IN */
const char *key, /* IN */
int key_length, /* IN */
const char *javascript) /* IN */
{
static const uint8_t type = BSON_TYPE_CODE;
uint32_t length;
uint32_t length_le;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (javascript, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
length = (int)strlen (javascript) + 1;
length_le = BSON_UINT32_TO_LE (length);
return _bson_append (bson, 5,
(1 + key_length + 1 + 4 + length),
1, &type,
key_length, key,
1, &gZero,
4, &length_le,
length, javascript);
}
/*
*--------------------------------------------------------------------------
*
* bson_append_code_with_scope --
*
* Append a new field to @bson containing javascript code with
* supplied scope.
*
* Returns:
* true if successful; otherwise false.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
bool
bson_append_code_with_scope (bson_t *bson, /* IN */
const char *key, /* IN */
int key_length, /* IN */
const char *javascript, /* IN */
const bson_t *scope) /* IN */
{
static const uint8_t type = BSON_TYPE_CODEWSCOPE;
uint32_t codews_length_le;
uint32_t codews_length;
uint32_t js_length_le;
uint32_t js_length;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (javascript, false);
if (bson_empty0 (scope)) {
return bson_append_code (bson, key, key_length, javascript);
}
if (key_length < 0) {
key_length = (int)strlen (key);
}
js_length = (int)strlen (javascript) + 1;
js_length_le = BSON_UINT32_TO_LE (js_length);
codews_length = 4 + 4 + js_length + scope->len;
codews_length_le = BSON_UINT32_TO_LE (codews_length);
return _bson_append (bson, 7,
(1 + key_length + 1 + 4 + 4 + js_length + scope->len),
1, &type,
key_length, key,
1, &gZero,
4, &codews_length_le,
4, &js_length_le,
js_length, javascript,
scope->len, _bson_data (scope));
}
/*
*--------------------------------------------------------------------------
*
* bson_append_dbpointer --
*
* This BSON data type is DEPRECATED.
*
* Append a BSON dbpointer field to @bson.
*
* Returns:
* true if successful; otherwise false.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
bool
bson_append_dbpointer (bson_t *bson, /* IN */
const char *key, /* IN */
int key_length, /* IN */
const char *collection, /* IN */
const bson_oid_t *oid)
{
static const uint8_t type = BSON_TYPE_DBPOINTER;
uint32_t length;
uint32_t length_le;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (collection, false);
bson_return_val_if_fail (oid, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
length = (int)strlen (collection) + 1;
length_le = BSON_UINT32_TO_LE (length);
return _bson_append (bson, 6,
(1 + key_length + 1 + 4 + length + 12),
1, &type,
key_length, key,
1, &gZero,
4, &length_le,
length, collection,
12, oid);
}
/*
*--------------------------------------------------------------------------
*
* bson_append_document --
*
* Append a new field to @bson containing a BSON document.
*
* In general, using bson_append_document_begin() results in faster
* code and less memory fragmentation.
*
* Returns:
* true if successful; otherwise false.
*
* Side effects:
* None.
*
* See also:
* bson_append_document_begin().
*
*--------------------------------------------------------------------------
*/
bool
bson_append_document (bson_t *bson, /* IN */
const char *key, /* IN */
int key_length, /* IN */
const bson_t *value) /* IN */
{
static const uint8_t type = BSON_TYPE_DOCUMENT;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (value, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
return _bson_append (bson, 4,
(1 + key_length + 1 + value->len),
1, &type,
key_length, key,
1, &gZero,
value->len, _bson_data (value));
}
bool
bson_append_double (bson_t *bson,
const char *key,
int key_length,
double value)
{
static const uint8_t type = BSON_TYPE_DOUBLE;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
#if BSON_BYTE_ORDER == BSON_BIG_ENDIAN
value = BSON_DOUBLE_TO_LE (value);
#endif
return _bson_append (bson, 4,
(1 + key_length + 1 + 8),
1, &type,
key_length, key,
1, &gZero,
8, &value);
}
bool
bson_append_int32 (bson_t *bson,
const char *key,
int key_length,
int32_t value)
{
static const uint8_t type = BSON_TYPE_INT32;
uint32_t value_le;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
value_le = BSON_UINT32_TO_LE (value);
return _bson_append (bson, 4,
(1 + key_length + 1 + 4),
1, &type,
key_length, key,
1, &gZero,
4, &value_le);
}
bool
bson_append_int64 (bson_t *bson,
const char *key,
int key_length,
int64_t value)
{
static const uint8_t type = BSON_TYPE_INT64;
uint64_t value_le;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
value_le = BSON_UINT64_TO_LE (value);
return _bson_append (bson, 4,
(1 + key_length + 1 + 8),
1, &type,
key_length, key,
1, &gZero,
8, &value_le);
}
bool
bson_append_iter (bson_t *bson,
const char *key,
int key_length,
const bson_iter_t *iter)
{
bool ret = false;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (iter, false);
if (!key) {
key = bson_iter_key (iter);
key_length = -1;
}
switch (bson_iter_type_unsafe (iter)) {
case BSON_TYPE_EOD:
return false;
case BSON_TYPE_DOUBLE:
ret = bson_append_double (bson, key, key_length, bson_iter_double (iter));
break;
case BSON_TYPE_UTF8:
{
uint32_t len = 0;
const char *str;
str = bson_iter_utf8 (iter, &len);
ret = bson_append_utf8 (bson, key, key_length, str, len);
}
break;
case BSON_TYPE_DOCUMENT:
{
const uint8_t *buf = NULL;
uint32_t len = 0;
bson_t doc;
bson_iter_document (iter, &len, &buf);
if (bson_init_static (&doc, buf, len)) {
ret = bson_append_document (bson, key, key_length, &doc);
bson_destroy (&doc);
}
}
break;
case BSON_TYPE_ARRAY:
{
const uint8_t *buf = NULL;
uint32_t len = 0;
bson_t doc;
bson_iter_array (iter, &len, &buf);
if (bson_init_static (&doc, buf, len)) {
ret = bson_append_array (bson, key, key_length, &doc);
bson_destroy (&doc);
}
}
break;
case BSON_TYPE_BINARY:
{
const uint8_t *binary = NULL;
bson_subtype_t subtype = BSON_SUBTYPE_BINARY;
uint32_t len = 0;
bson_iter_binary (iter, &subtype, &len, &binary);
ret = bson_append_binary (bson, key, key_length,
subtype, binary, len);
}
break;
case BSON_TYPE_UNDEFINED:
ret = bson_append_undefined (bson, key, key_length);
break;
case BSON_TYPE_OID:
ret = bson_append_oid (bson, key, key_length, bson_iter_oid (iter));
break;
case BSON_TYPE_BOOL:
ret = bson_append_bool (bson, key, key_length, bson_iter_bool (iter));
break;
case BSON_TYPE_DATE_TIME:
ret = bson_append_date_time (bson, key, key_length,
bson_iter_date_time (iter));
break;
case BSON_TYPE_NULL:
ret = bson_append_null (bson, key, key_length);
break;
case BSON_TYPE_REGEX:
{
const char *regex;
const char *options;
regex = bson_iter_regex (iter, &options);
ret = bson_append_regex (bson, key, key_length, regex, options);
}
break;
case BSON_TYPE_DBPOINTER:
{
const bson_oid_t *oid;
uint32_t len;
const char *collection;
bson_iter_dbpointer (iter, &len, &collection, &oid);
ret = bson_append_dbpointer (bson, key, key_length, collection, oid);
}
break;
case BSON_TYPE_CODE:
{
uint32_t len;
const char *code;
code = bson_iter_code (iter, &len);
ret = bson_append_code (bson, key, key_length, code);
}
break;
case BSON_TYPE_SYMBOL:
{
uint32_t len;
const char *symbol;
symbol = bson_iter_symbol (iter, &len);
ret = bson_append_symbol (bson, key, key_length, symbol, len);
}
break;
case BSON_TYPE_CODEWSCOPE:
{
const uint8_t *scope = NULL;
uint32_t scope_len = 0;
uint32_t len = 0;
const char *javascript = NULL;
bson_t doc;
javascript = bson_iter_codewscope (iter, &len, &scope_len, &scope);
if (bson_init_static (&doc, scope, scope_len)) {
ret = bson_append_code_with_scope (bson, key, key_length,
javascript, &doc);
bson_destroy (&doc);
}
}
break;
case BSON_TYPE_INT32:
ret = bson_append_int32 (bson, key, key_length, bson_iter_int32 (iter));
break;
case BSON_TYPE_TIMESTAMP:
{
uint32_t ts;
uint32_t inc;
bson_iter_timestamp (iter, &ts, &inc);
ret = bson_append_timestamp (bson, key, key_length, ts, inc);
}
break;
case BSON_TYPE_INT64:
ret = bson_append_int64 (bson, key, key_length, bson_iter_int64 (iter));
break;
case BSON_TYPE_MAXKEY:
ret = bson_append_maxkey (bson, key, key_length);
break;
case BSON_TYPE_MINKEY:
ret = bson_append_minkey (bson, key, key_length);
break;
default:
break;
}
return ret;
}
bool
bson_append_maxkey (bson_t *bson,
const char *key,
int key_length)
{
static const uint8_t type = BSON_TYPE_MAXKEY;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
return _bson_append (bson, 3,
(1 + key_length + 1),
1, &type,
key_length, key,
1, &gZero);
}
bool
bson_append_minkey (bson_t *bson,
const char *key,
int key_length)
{
static const uint8_t type = BSON_TYPE_MINKEY;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
return _bson_append (bson, 3,
(1 + key_length + 1),
1, &type,
key_length, key,
1, &gZero);
}
bool
bson_append_null (bson_t *bson,
const char *key,
int key_length)
{
static const uint8_t type = BSON_TYPE_NULL;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
return _bson_append (bson, 3,
(1 + key_length + 1),
1, &type,
key_length, key,
1, &gZero);
}
bool
bson_append_oid (bson_t *bson,
const char *key,
int key_length,
const bson_oid_t *value)
{
static const uint8_t type = BSON_TYPE_OID;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (value, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
return _bson_append (bson, 4,
(1 + key_length + 1 + 12),
1, &type,
key_length, key,
1, &gZero,
12, value);
}
bool
bson_append_regex (bson_t *bson,
const char *key,
int key_length,
const char *regex,
const char *options)
{
static const uint8_t type = BSON_TYPE_REGEX;
uint32_t regex_len;
uint32_t options_len;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length = (int)strlen (key);
}
if (!regex) {
regex = "";
}
if (!options) {
options = "";
}
regex_len = (int)strlen (regex) + 1;
options_len = (int)strlen (options) + 1;
return _bson_append (bson, 5,
(1 + key_length + 1 + regex_len + options_len),
1, &type,
key_length, key,
1, &gZero,
regex_len, regex,
options_len, options);
}
bool
bson_append_utf8 (bson_t *bson,
const char *key,
int key_length,
const char *value,
int length)
{
static const uint8_t type = BSON_TYPE_UTF8;
uint32_t length_le;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (BSON_UNLIKELY (!value)) {
return bson_append_null (bson, key, key_length);
}
if (BSON_UNLIKELY (key_length < 0)) {
key_length = (int)strlen (key);
}
if (BSON_UNLIKELY (length < 0)) {
length = (int)strlen (value);
}
length_le = BSON_UINT32_TO_LE (length + 1);
return _bson_append (bson, 6,
(1 + key_length + 1 + 4 + length + 1),
1, &type,
key_length, key,
1, &gZero,
4, &length_le,
length, value,
1, &gZero);
}
bool
bson_append_symbol (bson_t *bson,
const char *key,
int key_length,
const char *value,
int length)
{
static const uint8_t type = BSON_TYPE_SYMBOL;
uint32_t length_le;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (!value) {
return bson_append_null (bson, key, key_length);
}
if (key_length < 0) {
key_length = (int)strlen (key);
}
if (length < 0) {
length =(int)strlen (value);
}
length_le = BSON_UINT32_TO_LE (length + 1);
return _bson_append (bson, 6,
(1 + key_length + 1 + 4 + length + 1),
1, &type,
key_length, key,
1, &gZero,
4, &length_le,
length, value,
1, &gZero);
}
bool
bson_append_time_t (bson_t *bson,
const char *key,
int key_length,
time_t value)
{
#ifdef BSON_OS_WIN32
struct timeval tv = { (long)value, 0 };
#else
struct timeval tv = { value, 0 };
#endif
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
return bson_append_timeval (bson, key, key_length, &tv);
}
bool
bson_append_timestamp (bson_t *bson,
const char *key,
int key_length,
uint32_t timestamp,
uint32_t increment)
{
static const uint8_t type = BSON_TYPE_TIMESTAMP;
uint64_t value;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length =(int)strlen (key);
}
value = ((((uint64_t)timestamp) << 32) | ((uint64_t)increment));
value = BSON_UINT64_TO_LE (value);
return _bson_append (bson, 4,
(1 + key_length + 1 + 8),
1, &type,
key_length, key,
1, &gZero,
8, &value);
}
bool
bson_append_now_utc (bson_t *bson,
const char *key,
int key_length)
{
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (key_length >= -1, false);
return bson_append_time_t (bson, key, key_length, time (NULL));
}
bool
bson_append_date_time (bson_t *bson,
const char *key,
int key_length,
int64_t value)
{
static const uint8_t type = BSON_TYPE_DATE_TIME;
uint64_t value_le;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length =(int)strlen (key);
}
value_le = BSON_UINT64_TO_LE (value);
return _bson_append (bson, 4,
(1 + key_length + 1 + 8),
1, &type,
key_length, key,
1, &gZero,
8, &value_le);
}
bool
bson_append_timeval (bson_t *bson,
const char *key,
int key_length,
struct timeval *value)
{
uint64_t unix_msec;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (value, false);
unix_msec = (((uint64_t)value->tv_sec) * 1000UL) +
(value->tv_usec / 1000UL);
return bson_append_date_time (bson, key, key_length, unix_msec);
}
bool
bson_append_undefined (bson_t *bson,
const char *key,
int key_length)
{
static const uint8_t type = BSON_TYPE_UNDEFINED;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (key_length < 0) {
key_length =(int)strlen (key);
}
return _bson_append (bson, 3,
(1 + key_length + 1),
1, &type,
key_length, key,
1, &gZero);
}
bool
bson_append_value (bson_t *bson,
const char *key,
int key_length,
const bson_value_t *value)
{
bson_t local;
bool ret = false;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
bson_return_val_if_fail (value, false);
switch (value->value_type) {
case BSON_TYPE_DOUBLE:
ret = bson_append_double (bson, key, key_length,
value->value.v_double);
break;
case BSON_TYPE_UTF8:
ret = bson_append_utf8 (bson, key, key_length,
value->value.v_utf8.str,
value->value.v_utf8.len);
break;
case BSON_TYPE_DOCUMENT:
if (bson_init_static (&local,
value->value.v_doc.data,
value->value.v_doc.data_len)) {
ret = bson_append_document (bson, key, key_length, &local);
bson_destroy (&local);
}
break;
case BSON_TYPE_ARRAY:
if (bson_init_static (&local,
value->value.v_doc.data,
value->value.v_doc.data_len)) {
ret = bson_append_array (bson, key, key_length, &local);
bson_destroy (&local);
}
break;
case BSON_TYPE_BINARY:
ret = bson_append_binary (bson, key, key_length,
value->value.v_binary.subtype,
value->value.v_binary.data,
value->value.v_binary.data_len);
break;
case BSON_TYPE_UNDEFINED:
ret = bson_append_undefined (bson, key, key_length);
break;
case BSON_TYPE_OID:
ret = bson_append_oid (bson, key, key_length, &value->value.v_oid);
break;
case BSON_TYPE_BOOL:
ret = bson_append_bool (bson, key, key_length, value->value.v_bool);
break;
case BSON_TYPE_DATE_TIME:
ret = bson_append_date_time (bson, key, key_length,
value->value.v_datetime);
break;
case BSON_TYPE_NULL:
ret = bson_append_null (bson, key, key_length);
break;
case BSON_TYPE_REGEX:
ret = bson_append_regex (bson, key, key_length,
value->value.v_regex.regex,
value->value.v_regex.options);
break;
case BSON_TYPE_DBPOINTER:
ret = bson_append_dbpointer (bson, key, key_length,
value->value.v_dbpointer.collection,
&value->value.v_dbpointer.oid);
break;
case BSON_TYPE_CODE:
ret = bson_append_code (bson, key, key_length,
value->value.v_code.code);
break;
case BSON_TYPE_SYMBOL:
ret = bson_append_symbol (bson, key, key_length,
value->value.v_symbol.symbol,
value->value.v_symbol.len);
break;
case BSON_TYPE_CODEWSCOPE:
if (bson_init_static (&local,
value->value.v_codewscope.scope_data,
value->value.v_codewscope.scope_len)) {
ret = bson_append_code_with_scope (bson, key, key_length,
value->value.v_codewscope.code,
&local);
bson_destroy (&local);
}
break;
case BSON_TYPE_INT32:
ret = bson_append_int32 (bson, key, key_length, value->value.v_int32);
break;
case BSON_TYPE_TIMESTAMP:
ret = bson_append_timestamp (bson, key, key_length,
value->value.v_timestamp.timestamp,
value->value.v_timestamp.increment);
break;
case BSON_TYPE_INT64:
ret = bson_append_int64 (bson, key, key_length, value->value.v_int64);
break;
case BSON_TYPE_MAXKEY:
ret = bson_append_maxkey (bson, key, key_length);
break;
case BSON_TYPE_MINKEY:
ret = bson_append_minkey (bson, key, key_length);
break;
case BSON_TYPE_EOD:
default:
break;
}
return ret;
}
void
bson_init (bson_t *bson)
{
bson_impl_inline_t *impl = (bson_impl_inline_t *)bson;
bson_return_if_fail (bson);
impl->flags = BSON_FLAG_INLINE | BSON_FLAG_STATIC;
impl->len = 5;
impl->data[0] = 5;
impl->data[1] = 0;
impl->data[2] = 0;
impl->data[3] = 0;
impl->data[4] = 0;
}
void
bson_reinit (bson_t *bson)
{
uint8_t *data;
bson_return_if_fail (bson);
data = _bson_data (bson);
bson->len = 5;
data [0] = 5;
data [1] = 0;
data [2] = 0;
data [3] = 0;
data [4] = 0;
}
bool
bson_init_static (bson_t *bson,
const uint8_t *data,
size_t length)
{
bson_impl_alloc_t *impl = (bson_impl_alloc_t *)bson;
uint32_t len_le;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (data, false);
if ((length < 5) || (length > INT_MAX)) {
return false;
}
memcpy (&len_le, data, sizeof (len_le));
if ((size_t)BSON_UINT32_FROM_LE (len_le) != length) {
return false;
}
if (data[length - 1]) {
return false;
}
impl->flags = BSON_FLAG_STATIC | BSON_FLAG_RDONLY;
impl->len = (uint32_t)length;
impl->parent = NULL;
impl->depth = 0;
impl->buf = &impl->alloc;
impl->buflen = &impl->alloclen;
impl->offset = 0;
impl->alloc = (uint8_t *)data;
impl->alloclen = length;
impl->realloc = NULL;
impl->realloc_func_ctx = NULL;
return true;
}
bson_t *
bson_new (void)
{
bson_impl_inline_t *impl;
bson_t *bson;
bson = bson_malloc (sizeof *bson);
impl = (bson_impl_inline_t *)bson;
impl->flags = BSON_FLAG_INLINE;
impl->len = 5;
impl->data[0] = 5;
impl->data[1] = 0;
impl->data[2] = 0;
impl->data[3] = 0;
impl->data[4] = 0;
return bson;
}
bson_t *
bson_sized_new (size_t size)
{
bson_impl_alloc_t *impl_a;
bson_impl_inline_t *impl_i;
bson_t *b;
bson_return_val_if_fail (size <= INT32_MAX, NULL);
b = bson_malloc (sizeof *b);
impl_a = (bson_impl_alloc_t *)b;
impl_i = (bson_impl_inline_t *)b;
if (size <= sizeof impl_i->data) {
bson_init (b);
b->flags &= ~BSON_FLAG_STATIC;
} else {
impl_a->flags = BSON_FLAG_NONE;
impl_a->len = 5;
impl_a->parent = NULL;
impl_a->depth = 0;
impl_a->buf = &impl_a->alloc;
impl_a->buflen = &impl_a->alloclen;
impl_a->offset = 0;
impl_a->alloclen = BSON_MAX (5, size);
impl_a->alloc = bson_malloc (impl_a->alloclen);
impl_a->alloc[0] = 5;
impl_a->alloc[1] = 0;
impl_a->alloc[2] = 0;
impl_a->alloc[3] = 0;
impl_a->alloc[4] = 0;
impl_a->realloc = bson_realloc_ctx;
impl_a->realloc_func_ctx = NULL;
}
return b;
}
bson_t *
bson_new_from_data (const uint8_t *data,
size_t length)
{
uint32_t len_le;
bson_t *bson;
bson_return_val_if_fail (data, NULL);
if ((length < 5) || (length > INT_MAX) || data [length - 1]) {
return NULL;
}
memcpy (&len_le, data, sizeof (len_le));
if (length != (size_t)BSON_UINT32_FROM_LE (len_le)) {
return NULL;
}
bson = bson_sized_new (length);
memcpy (_bson_data (bson), data, length);
bson->len = (uint32_t)length;
return bson;
}
bson_t *
bson_new_from_buffer (uint8_t **buf,
size_t *buf_len,
bson_realloc_func realloc_func,
void *realloc_func_ctx)
{
bson_impl_alloc_t *impl;
uint32_t len_le;
uint32_t length;
bson_t *bson;
bson_return_val_if_fail (buf, NULL);
bson_return_val_if_fail (buf_len, NULL);
if (!realloc_func) {
realloc_func = bson_realloc_ctx;
}
bson = bson_malloc0 (sizeof *bson);
impl = (bson_impl_alloc_t *)bson;
if (!*buf) {
length = 5;
len_le = BSON_UINT32_TO_LE (length);
*buf_len = 5;
*buf = realloc_func (*buf, *buf_len, realloc_func_ctx);
memcpy (*buf, &len_le, sizeof (len_le));
(*buf) [4] = '\0';
} else {
if ((*buf_len < 5) || (*buf_len > INT_MAX)) {
bson_free (bson);
return NULL;
}
memcpy (&len_le, *buf, sizeof (len_le));
length = BSON_UINT32_FROM_LE(len_le);
}
if ((*buf)[length - 1]) {
bson_free (bson);
return NULL;
}
impl->flags = BSON_FLAG_NO_FREE;
impl->len = length;
impl->buf = buf;
impl->buflen = buf_len;
impl->realloc = realloc_func;
impl->realloc_func_ctx = realloc_func_ctx;
return bson;
}
bson_t *
bson_copy (const bson_t *bson)
{
const uint8_t *data;
bson_return_val_if_fail (bson, NULL);
data = _bson_data (bson);
return bson_new_from_data (data, bson->len);
}
void
bson_copy_to (const bson_t *src,
bson_t *dst)
{
const uint8_t *data;
bson_impl_alloc_t *adst;
size_t len;
bson_return_if_fail (src);
bson_return_if_fail (dst);
if ((src->flags & BSON_FLAG_INLINE)) {
memcpy (dst, src, sizeof *dst);
dst->flags = (BSON_FLAG_STATIC | BSON_FLAG_INLINE);
return;
}
data = _bson_data (src);
len = bson_next_power_of_two ((size_t)src->len);
adst = (bson_impl_alloc_t *)dst;
adst->flags = BSON_FLAG_STATIC;
adst->len = src->len;
adst->parent = NULL;
adst->depth = 0;
adst->buf = &adst->alloc;
adst->buflen = &adst->alloclen;
adst->offset = 0;
adst->alloc = bson_malloc (len);
adst->alloclen = len;
adst->realloc = bson_realloc_ctx;
adst->realloc_func_ctx = NULL;
memcpy (adst->alloc, data, src->len);
}
static bool
should_ignore (const char *first_exclude,
va_list args,
const char *name)
{
bool ret = false;
const char *exclude = first_exclude;
va_list args_copy;
va_copy (args_copy, args);
do {
if (!strcmp (name, exclude)) {
ret = true;
break;
}
} while ((exclude = va_arg (args_copy, const char *)));
va_end (args_copy);
return ret;
}
static void
_bson_copy_to_excluding_va (const bson_t *src,
bson_t *dst,
const char *first_exclude,
va_list args)
{
bson_iter_t iter;
if (bson_iter_init (&iter, src)) {
while (bson_iter_next (&iter)) {
if (!should_ignore (first_exclude, args, bson_iter_key (&iter))) {
if (!bson_append_iter (dst, NULL, 0, &iter)) {
/*
* This should not be able to happen since we are copying
* from within a valid bson_t.
*/
BSON_ASSERT (false);
return;
}
}
}
}
}
void
bson_copy_to_excluding (const bson_t *src,
bson_t *dst,
const char *first_exclude,
...)
{
va_list args;
bson_return_if_fail (src);
bson_return_if_fail (dst);
bson_return_if_fail (first_exclude);
bson_init (dst);
va_start (args, first_exclude);
_bson_copy_to_excluding_va (src, dst, first_exclude, args);
va_end (args);
}
void
bson_copy_to_excluding_noinit (const bson_t *src,
bson_t *dst,
const char *first_exclude,
...)
{
va_list args;
bson_return_if_fail (src);
bson_return_if_fail (dst);
bson_return_if_fail (first_exclude);
va_start (args, first_exclude);
_bson_copy_to_excluding_va (src, dst, first_exclude, args);
va_end (args);
}
void
bson_destroy (bson_t *bson)
{
BSON_ASSERT (bson);
if (!(bson->flags &
(BSON_FLAG_RDONLY | BSON_FLAG_INLINE | BSON_FLAG_NO_FREE))) {
bson_free (*((bson_impl_alloc_t *)bson)->buf);
}
if (!(bson->flags & BSON_FLAG_STATIC)) {
bson_free (bson);
}
}
uint8_t *
bson_destroy_with_steal (bson_t *bson,
bool steal,
uint32_t *length)
{
uint8_t *ret = NULL;
bson_return_val_if_fail (bson, NULL);
if (length) {
*length = bson->len;
}
if (!steal) {
bson_destroy (bson);
return NULL;
}
if ((bson->flags & (BSON_FLAG_CHILD |
BSON_FLAG_IN_CHILD |
BSON_FLAG_RDONLY))) {
/* Do nothing */
} else if ((bson->flags & BSON_FLAG_INLINE)) {
bson_impl_inline_t *inl;
inl = (bson_impl_inline_t *)bson;
ret = bson_malloc (bson->len);
memcpy (ret, inl->data, bson->len);
} else {
bson_impl_alloc_t *alloc;
alloc = (bson_impl_alloc_t *)bson;
ret = *alloc->buf;
*alloc->buf = NULL;
}
bson_destroy (bson);
return ret;
}
const uint8_t *
bson_get_data (const bson_t *bson)
{
bson_return_val_if_fail (bson, NULL);
return _bson_data (bson);
}
uint32_t
bson_count_keys (const bson_t *bson)
{
uint32_t count = 0;
bson_iter_t iter;
bson_return_val_if_fail (bson, 0);
if (bson_iter_init (&iter, bson)) {
while (bson_iter_next (&iter)) {
count++;
}
}
return count;
}
bool
bson_has_field (const bson_t *bson,
const char *key)
{
bson_iter_t iter;
bson_iter_t child;
bson_return_val_if_fail (bson, false);
bson_return_val_if_fail (key, false);
if (NULL != strchr (key, '.')) {
return (bson_iter_init (&iter, bson) &&
bson_iter_find_descendant (&iter, key, &child));
}
return bson_iter_init_find (&iter, bson, key);
}
int
bson_compare (const bson_t *bson,
const bson_t *other)
{
const uint8_t *data1;
const uint8_t *data2;
size_t len1;
size_t len2;
int64_t ret;
data1 = _bson_data (bson) + 4;
len1 = bson->len - 4;
data2 = _bson_data (other) + 4;
len2 = other->len - 4;
if (len1 == len2) {
return memcmp (data1, data2, len1);
}
ret = memcmp (data1, data2, BSON_MIN (len1, len2));
if (ret == 0) {
ret = len1 - len2;
}
return (ret < 0) ? -1 : (ret > 0);
}
bool
bson_equal (const bson_t *bson,
const bson_t *other)
{
return !bson_compare (bson, other);
}
static bool
_bson_iter_validate_utf8 (const bson_iter_t *iter,
const char *key,
size_t v_utf8_len,
const char *v_utf8,
void *data)
{
bson_validate_state_t *state = data;
bool allow_null;
if ((state->flags & BSON_VALIDATE_UTF8)) {
allow_null = !!(state->flags & BSON_VALIDATE_UTF8_ALLOW_NULL);
if (!bson_utf8_validate (v_utf8, v_utf8_len, allow_null)) {
state->err_offset = iter->off;
return true;
}
}
if ((state->flags & BSON_VALIDATE_DOLLAR_KEYS)) {
if (state->phase == BSON_VALIDATE_PHASE_LF_REF_UTF8) {
state->phase = BSON_VALIDATE_PHASE_LF_ID_KEY;
} else if (state->phase == BSON_VALIDATE_PHASE_LF_DB_UTF8) {
state->phase = BSON_VALIDATE_PHASE_NOT_DBREF;
}
}
return false;
}
static void
_bson_iter_validate_corrupt (const bson_iter_t *iter,
void *data)
{
bson_validate_state_t *state = data;
state->err_offset = iter->err_off;
}
static bool
_bson_iter_validate_before (const bson_iter_t *iter,
const char *key,
void *data)
{
bson_validate_state_t *state = data;
if ((state->flags & BSON_VALIDATE_DOLLAR_KEYS)) {
if (key[0] == '$') {
if (state->phase == BSON_VALIDATE_PHASE_LF_REF_KEY &&
strcmp (key, "$ref") == 0) {
state->phase = BSON_VALIDATE_PHASE_LF_REF_UTF8;
} else if (state->phase == BSON_VALIDATE_PHASE_LF_ID_KEY &&
strcmp (key, "$id") == 0) {
state->phase = BSON_VALIDATE_PHASE_LF_DB_KEY;
} else if (state->phase == BSON_VALIDATE_PHASE_LF_DB_KEY &&
strcmp (key, "$db") == 0) {
state->phase = BSON_VALIDATE_PHASE_LF_DB_UTF8;
} else {
state->err_offset = iter->off;
return true;
}
} else if (state->phase == BSON_VALIDATE_PHASE_LF_ID_KEY ||
state->phase == BSON_VALIDATE_PHASE_LF_REF_UTF8 ||
state->phase == BSON_VALIDATE_PHASE_LF_DB_UTF8) {
state->err_offset = iter->off;
return true;
} else {
state->phase = BSON_VALIDATE_PHASE_NOT_DBREF;
}
}
if ((state->flags & BSON_VALIDATE_DOT_KEYS)) {
if (strstr (key, ".")) {
state->err_offset = iter->off;
return true;
}
}
return false;
}
static bool
_bson_iter_validate_codewscope (const bson_iter_t *iter,
const char *key,
size_t v_code_len,
const char *v_code,
const bson_t *v_scope,
void *data)
{
bson_validate_state_t *state = data;
size_t offset;
if (!bson_validate (v_scope, state->flags, &offset)) {
state->err_offset = iter->off + offset;
return false;
}
return true;
}
static bool
_bson_iter_validate_document (const bson_iter_t *iter,
const char *key,
const bson_t *v_document,
void *data);
static const bson_visitor_t bson_validate_funcs = {
_bson_iter_validate_before,
NULL, /* visit_after */
_bson_iter_validate_corrupt,
NULL, /* visit_double */
_bson_iter_validate_utf8,
_bson_iter_validate_document,
_bson_iter_validate_document, /* visit_array */
NULL, /* visit_binary */
NULL, /* visit_undefined */
NULL, /* visit_oid */
NULL, /* visit_bool */
NULL, /* visit_date_time */
NULL, /* visit_null */
NULL, /* visit_regex */
NULL, /* visit_dbpoint */
NULL, /* visit_code */
NULL, /* visit_symbol */
_bson_iter_validate_codewscope,
};
static bool
_bson_iter_validate_document (const bson_iter_t *iter,
const char *key,
const bson_t *v_document,
void *data)
{
bson_validate_state_t *state = data;
bson_iter_t child;
bson_validate_phase_t phase = state->phase;
if (!bson_iter_init (&child, v_document)) {
state->err_offset = iter->off;
return true;
}
if (state->phase == BSON_VALIDATE_PHASE_START) {
state->phase = BSON_VALIDATE_PHASE_TOP;
} else {
state->phase = BSON_VALIDATE_PHASE_LF_REF_KEY;
}
bson_iter_visit_all (&child, &bson_validate_funcs, state);
if (state->phase == BSON_VALIDATE_PHASE_LF_ID_KEY ||
state->phase == BSON_VALIDATE_PHASE_LF_REF_UTF8 ||
state->phase == BSON_VALIDATE_PHASE_LF_DB_UTF8) {
state->err_offset = iter->off;
return true;
}
state->phase = phase;
return false;
}
bool
bson_validate (const bson_t *bson,
bson_validate_flags_t flags,
size_t *offset)
{
bson_validate_state_t state = { flags, -1, BSON_VALIDATE_PHASE_START };
bson_iter_t iter;
if (!bson_iter_init (&iter, bson)) {
state.err_offset = 0;
goto failure;
}
_bson_iter_validate_document (&iter, NULL, bson, &state);
failure:
if (offset) {
*offset = state.err_offset;
}
return state.err_offset < 0;
}
bool
bson_concat (bson_t *dst,
const bson_t *src)
{
BSON_ASSERT (dst);
BSON_ASSERT (src);
if (!bson_empty (src)) {
return _bson_append (dst, 1, src->len - 5,
src->len - 5, _bson_data (src) + 4);
}
return true;
}
|