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
|
/* Copyright (c) 2007 James Antill -- See LICENSE file for terms. */
#ifndef USTR_MAIN_H
#error " You should have already included ustr-main.h, or just include ustr.h."
#endif
USTR_CONF_i_PROTO
int ustr__dupx_cmp_eq(size_t sz1, size_t rb1, int x1, int emem1,
size_t sz2, size_t rb2, int x2, int emem2)
{
if ((!x1 != !x2) || (!emem1 != !emem2))
return (USTR_FALSE);
if (sz1)
{
if (rb1 < 2)
rb1 = 2;
}
else if (rb1 > 4)
sz1 = 1;
if (sz2)
{
if (rb2 < 2)
rb2 = 2;
}
else if (rb2 > 4)
sz2 = 1;
return ((!sz1 == !sz2) && (rb1 == rb2));
}
USTR_CONF_i_PROTO size_t ustr__sz_get(const struct Ustr *s1)
{
size_t lenn = 0;
USTR_ASSERT(!ustr_ro(s1));
USTR_ASSERT( ustr_sized(s1));
lenn = USTR__LEN_LEN(s1);
return (ustr_xi__embed_val_get(s1->data + 1 + USTR__REF_LEN(s1) + lenn,lenn));
}
USTR_CONF_i_PROTO size_t ustr__nb(size_t num)
{
USTR_ASSERT((num <= 0xFFFFFFFF) || USTR_CONF_HAVE_64bit_SIZE_MAX);
if (num > 0xFFFFFFFF) return (8);
if (num > 0xFFFF) return (4);
if (num > 0xFF) return (2);
else return (1);
}
USTR_CONF_i_PROTO
int ustrp__assert_valid(int p, const struct Ustr *s1)
{
const char *eos_ptr = 0;
size_t eos_len = sizeof(USTR_END_ALOCDx);
size_t rbytes = 0;
size_t lbytes = 0;
size_t sbytes = 0;
size_t sz = 0;
size_t oh = 0;
USTR_ASSERT_RET(s1, USTR_FALSE);
ustr_assert_ret(USTR__ASSERT_MALLOC_CHECK_MEM(p, s1), USTR_FALSE);
if (!s1->data[0])
return (USTR_TRUE);
/* just make sure for compound "bits" tests */
USTR_ASSERT(( ustr_alloc(s1) || ustr_sized(s1)) != ustr_ro(s1));
USTR_ASSERT((!ustr_alloc(s1) && ustr_sized(s1)) == ustr_fixed(s1));
USTR_ASSERT(( ustr_fixed(s1) && ustr_exact(s1)) == ustr_limited(s1));
rbytes = USTR__REF_LEN(s1);
lbytes = USTR__LEN_LEN(s1);
ustr_assert_ret(lbytes, USTR_FALSE);
if (ustr_sized(s1))
{
sbytes = lbytes;
sz = ustr__sz_get(s1);
}
oh = 1 + rbytes + lbytes + sbytes + eos_len;
USTR_ASSERT_RET(!ustr_sized(s1) || (ustr_len(s1) <= sz), USTR_FALSE);
USTR_ASSERT(!sz || (ustr__nb(sz) == lbytes) ||
((ustr__nb(sz) == 1) && (lbytes == 2))); /* 2 is the minimum */
USTR_ASSERT_RET(!sz || (oh <= sz), USTR_FALSE);
USTR_ASSERT_RET(!sz || ((ustr_len(s1) + oh) <= sz), USTR_FALSE);
USTR_ASSERT_RET( ustr_exact(s1) || !ustr_ro(s1), USTR_FALSE);
USTR_ASSERT_RET(!ustr_enomem(s1) || !ustr_ro(s1), USTR_FALSE);
if (!USTR_CONF_USE_EOS_MARK)
{
USTR_ASSERT_RET(!ustr_cstr(s1)[ustr_len(s1)], USTR_FALSE);
return (USTR_TRUE);
}
if (ustr_ro(s1))
eos_ptr = USTR_END_CONSTx;
else if (ustr_fixed(s1))
eos_ptr = USTR_END_FIXEDx;
else
eos_ptr = USTR_END_ALOCDx;
USTR_ASSERT_RET(!memcmp(ustr_cstr(s1) + ustr_len(s1), eos_ptr, eos_len),
USTR_FALSE);
return (USTR_TRUE);
}
/* Due to ustrp_assert_valid() being inline we can't pass 0 for
* ustr_assert_valid() until we've made two ustr_assert_valid() versions.
* Maybe I'll do that for 1.0.3 ... NOTE: We'd need to change the other
* internal ustr_assert_valid() calls. */
USTR_CONF_I_PROTO int ustr_assert_valid(const struct Ustr *s1)
{ return (ustrp__assert_valid(1, s1)); }
/* We can't change the API of this function until 2.0.x time, even if we want
* to. But it's no big deal, as we might not want to. */
USTR_CONF_I_PROTO int ustrp_assert_valid(const struct Ustrp *s1)
{ return (ustrp__assert_valid(1, &s1->s)); }
USTR_CONF_i_PROTO
size_t ustrp__assert_valid_subustr(int p, const struct Ustr *s1,
size_t pos, size_t len)
{
size_t clen = 0;
(void) p;
USTR_ASSERT(ustrp__assert_valid(p, s1));
USTR_ASSERT_RET(pos, 0);
clen = ustr_len(s1);
if (((pos == 1) || !len) && (len == clen))
return (clen);
USTR_ASSERT_RET((clen >= pos), 0);
USTR_ASSERT_RET((clen >= (len + --pos)), 0);
return (clen);
}
/* see comments for ustr_assert_valid() etc. */
USTR_CONF_I_PROTO
size_t ustr_assert_valid_subustr(const struct Ustr *s1, size_t pos, size_t len)
{ return (ustrp__assert_valid_subustr(1, s1, pos, len)); }
USTR_CONF_I_PROTO
int ustrp_assert_valid_subustrp(const struct Ustrp *s1, size_t pos, size_t len)
{ return (ustrp__assert_valid_subustr(1, &s1->s, pos, len)); }
USTR_CONF_I_PROTO char *ustr_wstr(struct Ustr *s1)
{ /* NOTE: Not EI/II so we can call ustr_assert_valid() here. */
unsigned char *data = s1->data;
size_t lenn = 0;
USTR_ASSERT(ustr_assert_valid(s1));
USTR_ASSERT_RET(!ustr_ro(s1), 0);
lenn = USTR__LEN_LEN(s1);
if (ustr_sized(s1))
lenn *= 2;
return ((char *)(data + 1 + USTR__REF_LEN(s1) + lenn));
}
USTR_CONF_I_PROTO int ustr_owner(const struct Ustr *s1)
{
USTR_ASSERT(ustr_assert_valid(s1));
if (ustr_ro(s1)) return (USTR_FALSE);
if (ustr_fixed(s1)) return (USTR_TRUE);
switch (USTR__REF_LEN(s1))
{
#if USTR_CONF_HAVE_64bit_SIZE_MAX
case 8: if (s1->data[8]) return (USTR_FALSE);
if (s1->data[7]) return (USTR_FALSE);
if (s1->data[6]) return (USTR_FALSE);
if (s1->data[5]) return (USTR_FALSE);
#endif
case 4: if (s1->data[4]) return (USTR_FALSE);
if (s1->data[3]) return (USTR_FALSE);
case 2: if (s1->data[2]) return (USTR_FALSE);
case 1: return (s1->data[1] == 1);
case 0:
USTR_ASSERT_NO_SWITCH_DEF("Ref. length bad for ustr__ref_owner()");
}
return (USTR_TRUE); /* Ustr with no ref. count */
}
USTR_CONF_I_PROTO int ustr_setf_enomem_err(struct Ustr *s1)
{
USTR_ASSERT(ustr_assert_valid(s1));
errno = USTR__ENOMEM;
if (!ustr_owner(s1))
return (USTR_FALSE);
s1->data[0] |= USTR__BIT_ENOMEM;
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_setf_enomem_clr(struct Ustr *s1)
{
USTR_ASSERT(ustr_assert_valid(s1));
errno = 0;
if (!ustr_owner(s1))
return (USTR_FALSE);
s1->data[0] &= ~USTR__BIT_ENOMEM;
return (USTR_TRUE);
}
USTR_CONF_i_PROTO void ustr__embed_val_set(unsigned char *data,
size_t len, size_t val)
{
switch (len)
{
#if USTR_CONF_HAVE_64bit_SIZE_MAX
case 8:
data[7] = (val >> 56) & 0xFF;
data[6] = (val >> 48) & 0xFF;
data[5] = (val >> 40) & 0xFF;
data[4] = (val >> 32) & 0xFF;
#endif
case 4:
data[3] = (val >> 24) & 0xFF;
data[2] = (val >> 16) & 0xFF;
case 2:
data[1] = (val >> 8) & 0xFF;
case 1:
data[0] = (val >> 0) & 0xFF;
USTR_ASSERT_NO_SWITCH_DEF("Val. length bad for ustr__embed_val_set()");
}
}
/* no warn here, because most callers already know it's not going to fail */
USTR_CONF_i_PROTO int ustr__ref_set(struct Ustr *s1, size_t ref)
{
size_t len = 0;
USTR_ASSERT(ustr_assert_valid(s1));
USTR_ASSERT(ustr_alloc(s1));
if (!(len = USTR__REF_LEN(s1)))
return (USTR_FALSE);
ustr__embed_val_set(s1->data + 1, len, ref);
USTR_ASSERT(ustr_assert_valid(s1));
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_setf_share(struct Ustr *s1)
{
USTR_ASSERT(ustr_assert_valid(s1));
if (!ustr_alloc(s1))
return (USTR_TRUE);
if (!ustr__ref_set(s1, 0))
return (USTR_FALSE);
USTR_ASSERT(ustr_assert_valid(s1));
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_setf_owner(struct Ustr *s1)
{
USTR_ASSERT(ustr_assert_valid(s1));
if (!ustr_alloc(s1))
return (USTR_FALSE);
ustr__ref_set(s1, 1); /* 0 means it's unsharable and so they are the owner */
USTR_ASSERT(ustr_assert_valid(s1));
return (USTR_TRUE);
}
USTR_CONF_i_PROTO void ustr__len_set(struct Ustr *s1, size_t len)
{ /* can only validate after the right len is in place */
unsigned char *data = s1->data;
USTR_ASSERT(!ustr_ro(s1));
ustr__embed_val_set(data + 1 + USTR__REF_LEN(s1), USTR__LEN_LEN(s1), len);
USTR_ASSERT(ustr_assert_valid(s1));
}
USTR_CONF_i_PROTO void ustr__sz_set(struct Ustr *s1, size_t sz)
{ /* can't validate as this is called before anything is setup */
size_t lenn = 0;
USTR_ASSERT(!ustr_ro(s1));
USTR_ASSERT(ustr_sized(s1));
lenn = USTR__LEN_LEN(s1);
ustr__embed_val_set(s1->data + 1 + USTR__REF_LEN(s1) + lenn, lenn, sz);
}
USTR_CONF_i_PROTO int ustr__ref_add(struct Ustr *s1)
{
size_t ref = 0;
size_t lim = 0;
USTR_ASSERT(ustr_assert_valid(s1));
if (ustr_ro(s1))
return (USTR_TRUE);
if (ustr_fixed(s1))
return (USTR_FALSE);
switch (USTR__REF_LEN(s1))
{
#if USTR_CONF_HAVE_64bit_SIZE_MAX
case 8: if (!lim) lim = 0xFFFFFFFFFFFFFFFFULL;
#endif
case 4: if (!lim) lim = 0xFFFFFFFFUL;
case 2: if (!lim) lim = 0xFFFF;
case 1: if (!lim) lim = 0xFF;
ref = ustr_xi__ref_get(s1);
if (ref == 0)
return (USTR_TRUE);
if (ref == lim)
return (USTR_FALSE);
ustr__ref_set(s1, ref + 1);
return (USTR_TRUE);
case 0: /* Ustr with no reference count */
USTR_ASSERT_NO_SWITCH_DEF("Ref. length bad for ustr__ref_add()");
}
return (USTR_FALSE);
}
USTR_CONF_i_PROTO size_t ustr__ref_del(struct Ustr *s1)
{
USTR_ASSERT(ustr_assert_valid(s1));
if (!ustr_alloc(s1))
return (-1);
switch (USTR__REF_LEN(s1))
{
case 8:
case 4:
case 2:
case 1:
{
size_t ref = ustr_xi__ref_get(s1);
if (ref == 0)
return (-1);
if (ref == 1) /* Special case this so it doesn't "go shared"
* plus this is a common case and doing this is
* marginally faster */
return (0);
ustr__ref_set(s1, ref - 1);
return (ref - 1);
}
case 0: /* Ustr with no reference count */
USTR_ASSERT_NO_SWITCH_DEF("Ref. length bad for ustr__ref_del()");
}
return (0);
}
USTR_CONF_I_PROTO size_t ustr_size_overhead(const struct Ustr *s1)
{
size_t lenn = 0;
USTR_ASSERT(ustr_assert_valid(s1));
if (!s1->data[0])
return (1);
lenn = USTR__LEN_LEN(s1);
if (ustr_sized(s1))
lenn *= 2;
return (1 + USTR__REF_LEN(s1) + lenn + sizeof(USTR_END_ALOCDx));
}
USTR_CONF_I_PROTO size_t ustr_size_alloc(const struct Ustr *s1)
{ /* copy and paste so that ustr_ro() does the right thing */
size_t oh = 0;
USTR_ASSERT(ustr_assert_valid(s1));
if (ustr_sized(s1))
return (ustr__sz_get(s1));
oh = ustr_size_overhead(s1);
USTR_ASSERT((oh + ustr_len(s1)) >= oh);
if (ustr_exact(s1))
return (ustr_len(s1) + oh);
return (ustr__ns(ustr_len(s1) + oh));
}
USTR_CONF_i_PROTO void ustrp__free(struct Ustr_pool *p, struct Ustr *s1)
{
if (!s1) return;
USTR_ASSERT(ustrp__assert_valid(!!p, s1));
if (!ustr__ref_del(s1))
{
if (p)
p->pool_sys_free(p, s1);
else
USTR_CONF_FREE(s1);
}
}
USTR_CONF_I_PROTO void ustr_free(struct Ustr *s1)
{ return (ustrp__free(0, s1)); }
USTR_CONF_I_PROTO void ustrp_free(struct Ustr_pool *p, struct Ustrp *s1)
{ return (ustrp__free(p, &s1->s)); }
/* shortcut -- needs to be here, as lots of things calls this */
USTR_CONF_i_PROTO
void ustrp__sc_free2(struct Ustr_pool *p, struct Ustr **ps1, struct Ustr *s2)
{
USTR_ASSERT(ps1);
USTR_ASSERT(ustrp__assert_valid(!!p, s2)); /* don't pass NULL */
ustrp__free(p, *ps1);
*ps1 = s2;
}
USTR_CONF_i_PROTO size_t ustr__ns(size_t num)
{
size_t min_sz = 4;
if (num > ((USTR__SIZE_MAX / 4) * 3))
return (USTR__SIZE_MAX);
/* *2 is too much, we end up wasting a lot of RAM. So we do *1.5. */
while (min_sz < num)
{
size_t adder = min_sz / 2;
min_sz += adder;
if (min_sz >= num) break;
min_sz += adder;
}
return (min_sz);
}
/* ---------------- init - helpers so others can make Ustr's ---------------- */
USTR_CONF_I_PROTO size_t ustr_init_size(size_t sz, size_t rbytes, int exact,
size_t len)
{
size_t oh = 0;
size_t rsz = sz ? sz : len;
size_t lbytes = 0;
USTR_ASSERT_RET((rbytes == 0) ||
(rbytes == 1) || (rbytes == 2) || (rbytes == 4) ||
(USTR_CONF_HAVE_64bit_SIZE_MAX && (rbytes == 8)), 0);
do
{
size_t sbytes = 0;
lbytes = ustr__nb(rsz);
if (!sz && ((lbytes == 8) || (rbytes == 8)))
sz = 1;
USTR_ASSERT( (lbytes == 1) || (lbytes == 2) || (lbytes == 4) ||
(USTR_CONF_HAVE_64bit_SIZE_MAX && (lbytes == 8)));
if (sz)
{
if (rbytes <= 1)
rbytes = 2;
if (lbytes <= 1)
lbytes = 2;
sbytes = lbytes;
}
oh = 1 + rbytes + lbytes + sbytes + sizeof(USTR_END_ALOCDx);
rsz = oh + len;
if (rsz < len)
{
errno = USTR__EINVAL;
return (0);
}
USTR_ASSERT((lbytes <= ustr__nb(rsz)) ||
((lbytes == 2) && sz && (ustr__nb(rsz) == 1)));
} while (lbytes < ustr__nb(rsz));
if (exact)
return (rsz);
return (ustr__ns(rsz));
}
/* NIL terminate -- with possible end marker */
USTR_CONF_i_PROTO void ustr__terminate(unsigned char *ptr, int alloc,size_t len)
{
if (sizeof(USTR_END_ALOCDx) == 1)
ptr[len] = 0;
else if (alloc)
memcpy(ptr + len, USTR_END_ALOCDx, sizeof(USTR_END_ALOCDx));
else
memcpy(ptr + len, USTR_END_FIXEDx, sizeof(USTR_END_FIXEDx));
}
USTR_CONF_I_PROTO
struct Ustr *ustr_init_alloc(void *data, size_t rsz, size_t sz,
size_t rbytes, int exact, int emem, size_t len)
{
static const unsigned char map_big_pow2[9] = {-1, -1, 0, -1, 1, -1, -1, -1,2};
static const unsigned char map_pow2[5] = {0, 1, 2, -1, 3};
struct Ustr *ret = data;
int nexact = !exact;
int sized = 0;
size_t lbytes = 0;
size_t sbytes = 0;
size_t oh = 0;
const size_t eos_len = sizeof(USTR_END_ALOCDx);
USTR_ASSERT_RET((rbytes == 0) ||
(rbytes == 1) || (rbytes == 2) || (rbytes == 4) ||
(USTR_CONF_HAVE_64bit_SIZE_MAX && (rbytes == 8)), USTR_NULL);
USTR_ASSERT(data);
USTR_ASSERT(exact == !!exact);
USTR_ASSERT(emem == !!emem);
USTR_ASSERT(!sz || (sz == rsz));
USTR_ASSERT_RET(!sz || (sz > len), USTR_NULL);
if (!sz && (rbytes == 8))
sz = rsz; /* whee... */
lbytes = ustr__nb(sz ? sz : len);
if (!sz && (lbytes == 8))
sz = rsz; /* whee... */
USTR_ASSERT(lbytes == ustr__nb(sz ? sz : len));
USTR_ASSERT( (lbytes == 1) || (lbytes == 2) || (lbytes == 4) ||
(USTR_CONF_HAVE_64bit_SIZE_MAX && (lbytes == 8)));
if (sz)
{
if (sz < (1 + 2 + 2 + 1))
goto val_inval;
if (rbytes <= 1)
rbytes = 2;
if (lbytes <= 1)
lbytes = 2;
sbytes = lbytes;
}
oh = 1 + rbytes + lbytes + sbytes + eos_len;
if (rsz < (oh + len))
goto val_inval;
if (sz) sized = USTR__BIT_HAS_SZ;
if (nexact) nexact = USTR__BIT_NEXACT;
if (emem) emem = USTR__BIT_ENOMEM;
ret->data[0] = USTR__BIT_ALLOCD | sized | nexact | emem;
if (sz)
ret->data[0] |= (map_big_pow2[rbytes] << 2) | map_big_pow2[lbytes];
else
ret->data[0] |= (map_pow2[rbytes] << 2) | map_pow2[lbytes];
ustr__terminate(ret->data, USTR_TRUE, (oh - eos_len) + len);
if (sz)
ustr__sz_set(ret, sz);
ustr__len_set(ret, len);
ustr__ref_set(ret, 1);
USTR_ASSERT(ustr_assert_valid(ret));
USTR_ASSERT( ustr_alloc(ret));
USTR_ASSERT(!ustr_fixed(ret));
USTR_ASSERT(!ustr_ro(ret));
USTR_ASSERT( ustr_enomem(ret) == !!emem);
USTR_ASSERT( ustr_exact(ret) == exact);
USTR_ASSERT(!ustr_shared(ret));
USTR_ASSERT( ustr_owner(ret));
return (ret);
val_inval:
errno = USTR__EINVAL;
return (USTR_NULL);
}
USTR_CONF_I_PROTO
struct Ustrp *ustrp_init_alloc(void *data, size_t rsz, size_t sz,
size_t rbytes, int exact, int emem, size_t len)
{ return (USTRP(ustr_init_alloc(data, rsz, sz, rbytes, exact, emem, len))); }
USTR_CONF_I_PROTO
struct Ustr *ustr_init_fixed(void *data, size_t sz, int exact, size_t len)
{
struct Ustr *ret = data;
void *tmp = 0; /* move type between char and unsigned char */
size_t rbytes = 0;
const int emem = USTR_FALSE;
USTR_ASSERT(sz);
if (!ustr_init_alloc(data, sz, sz, rbytes, exact, emem, len))
return (USTR_NULL);
tmp = ustr_wstr(ret); /* done here,
as it might not be valid until we terminate */
ret->data[0] &= ~USTR__BIT_ALLOCD;
ustr__terminate(tmp, USTR_FALSE, len);
if ((rbytes = USTR__REF_LEN(ret))) /* _large_ */
ustr__embed_val_set(ret->data + 1, rbytes, 0);
USTR_ASSERT(ustr_assert_valid(ret));
USTR_ASSERT( ustr_fixed(ret));
USTR_ASSERT(!ustr_alloc(ret));
USTR_ASSERT(!ustr_ro(ret));
USTR_ASSERT( ustr_enomem(ret) == emem);
USTR_ASSERT(!ustr_shared(ret));
USTR_ASSERT( ustr_owner(ret));
return (ret);
}
USTR_CONF_I_PROTO
struct Ustrp *ustrp_init_fixed(void *data, size_t sz, int exact, size_t len)
{ return (USTRP(ustr_init_fixed(data, sz, exact, len))); }
USTR_CONF_I_PROTO size_t ustr_size(const struct Ustr *s1)
{ /* size of available space in the string */
size_t oh = 0;
USTR_ASSERT(ustr_assert_valid(s1));
if (ustr_sized(s1))
return (ustr__sz_get(s1) - ustr_size_overhead(s1));
if (ustr_exact(s1))
return (ustr_len(s1));
oh = ustr_size_overhead(s1);
return (ustr__ns(ustr_len(s1) + oh) - oh);
}
/* ---------------- allocations ---------------- */
/* NOTE: This is one of the two main "allocation" functions --
* this is the only thing that calls MALLOC. */
USTR_CONF_i_PROTO
struct Ustr *ustrp__dupx_undef(struct Ustr_pool *p, size_t sz, size_t rbytes,
int exact, int emem, size_t len)
{
struct Ustr *ret = USTR_NULL;
struct Ustr *USTR__COMPILE_ATTR_MAYBE_UNUSED(chk) = USTR_NULL;
size_t rsz = 0;
USTR_ASSERT((rbytes == 0) || (rbytes == 1) || (rbytes == 2) || (rbytes == 4)||
(USTR_CONF_HAVE_64bit_SIZE_MAX && (rbytes == 8)));
USTR_ASSERT(exact == !!exact);
USTR_ASSERT(emem == !!emem);
if (!len && ustr__dupx_cmp_eq(sz, rbytes, exact, emem, USTR__DUPX_DEF))
return (USTR("")); /* We don't go to all the trouble ustr_del() does.
* Which is probably better overall. */
if (!(rsz = ustr_init_size(sz, rbytes, exact, len)))
return (USTR_NULL);
if (p)
ret = p->pool_sys_malloc(p, rsz);
else
ret = USTR_CONF_MALLOC(rsz);
if (!ret)
{
errno = USTR__ENOMEM;
return (USTR_NULL);
}
chk = ustr_init_alloc(ret, rsz, sz ? rsz : 0, rbytes, exact, emem, len);
USTR_ASSERT(chk);
USTR_ASSERT(ustrp__assert_valid(!!p, ret));
return (ret);
}
USTR_CONF_i_PROTO
int ustrp__rw_realloc(struct Ustr_pool *p, struct Ustr **ps1,
int sized, size_t osz, size_t nsz)
{
struct Ustr *ret = USTR_NULL;
USTR_ASSERT(ustr_alloc(*ps1));
USTR_ASSERT(osz == ustr_size_alloc(*ps1));
USTR_ASSERT(sized == !!sized);
USTR_ASSERT(sized == ustr_sized(*ps1));
ustr_assert(USTR__ASSERT_MALLOC_CHECK_MEM(p, *ps1));
/* printf("1. p=%p, osz=%zu, nsz=%zu\n", p, osz, nsz); */
if (p)
ret = p->pool_sys_realloc(p, *ps1, osz, nsz);
else
ret = USTR_CONF_REALLOC(*ps1, nsz);
if (!ret)
{
ustr_setf_enomem_err(*ps1);
return (USTR_FALSE);
}
if (sized)
ustr__sz_set(ret, nsz);
*ps1 = ret;
return (USTR_TRUE);
}
USTR_CONF_i_PROTO void ustr__memcpy(struct Ustr *s1, size_t off,
const void *ptr, size_t len)
{ /* can't call ustr_wstr() if len == 0, as it might be RO */
if (!len)
return;
memcpy(ustr_wstr(s1) + off, ptr, len);
}
USTR_CONF_i_PROTO void ustr__memset(struct Ustr *s1, size_t off,
int chr, size_t len)
{ /* can't call ustr_wstr() if len == 0, as it might be RO */
if (!len)
return;
memset(ustr_wstr(s1) + off, chr, len);
}
/* ---------------- del ---------------- */
/* Fine grained management of the space allocated to a sized Ustr */
USTR_CONF_i_PROTO
int ustrp__realloc(struct Ustr_pool *p, struct Ustr **ps1, size_t nsz)
{
struct Ustr *s1 = USTR_NULL;
size_t oh = 0;
size_t len = 0;
size_t msz = 0; /* min size */
size_t osz = 0; /* old size */
int ret = USTR_TRUE;
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
s1 = *ps1;
if (!ustr_sized(s1) || !ustr_alloc(s1) || !ustr_owner(s1))
return (USTR_FALSE);
oh = ustr_size_overhead(s1);
len = ustr_len(s1);
msz = oh + len;
if (!nsz)
nsz = len;
nsz += oh; /* if this overflows, we'll get it at the msz test */
osz = ustr__sz_get(s1);
if (nsz == osz) /* if there's nothing to do... */
return (USTR_TRUE);
if (nsz < msz) /* we can't go less than the minimum */
return (USTR_FALSE);
/* Don't let the length num. bytes go up, as we might as well just dupx()
*/
if (ustr__nb(nsz) > USTR__LEN_LEN(s1))
return (USTR_FALSE);
ret = ustrp__rw_realloc(p, ps1, USTR_TRUE, osz, nsz);
USTR_ASSERT(ustrp__assert_valid(!!p, *ps1));
return (ret);
}
USTR_CONF_I_PROTO int ustr_realloc(struct Ustr **ps1, size_t sz)
{ return (ustrp__realloc(0, ps1, sz)); }
USTR_CONF_I_PROTO int ustrp_realloc(struct Ustr_pool *p,
struct Ustrp **ps1, size_t sz)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__realloc(p, &tmp, sz);
*ps1 = USTRP(tmp);
return (ret);
}
/* Can we actually RW to this Ustr, at _this_ moment, _this_ len */
USTR_CONF_i_PROTO
int ustr__rw_mod(struct Ustr *s1, size_t nlen, size_t *sz, size_t *oh,
size_t *osz, size_t *nsz, int *alloc)
{
size_t lbytes = 0;
size_t sbytes = 0;
if (!ustr_owner(s1))
return (USTR_FALSE);
*sz = 0;
if (ustr_sized(s1))
*sz = ustr__sz_get(s1);
*osz = *sz;
lbytes = USTR__LEN_LEN(s1);
if (*sz)
sbytes = lbytes;
USTR_ASSERT(!*sz || (ustr__nb(*sz) == lbytes) ||
((ustr__nb(*sz) == 1) && (lbytes == 2))); /* 2 is the minimum */
if (ustr__nb(nlen) > lbytes)
return (USTR_FALSE); /* in theory we could do better, but it's _hard_ */
*oh = 1 + USTR__REF_LEN(s1) + lbytes + sbytes + sizeof(USTR_END_ALOCDx);
*nsz = *oh + nlen;
if (*nsz < nlen)
return (USTR_FALSE);
if (*nsz > USTR__SIZE_MAX) /* 32bit overflow on 64bit size_t */
return (USTR_FALSE);
*alloc = USTR_FALSE; /* don't need to allocate/deallocate anything */
if (*nsz <= *sz)
return (USTR_TRUE); /* ustr_sized() */
if (!ustr_exact(s1))
*nsz = ustr__ns(*nsz);
*osz = ustr_size_alloc(s1);
if (!*sz && (*nsz == *osz))
return (USTR_TRUE);
*alloc = ustr_alloc(s1); /* _do_ need to deallocate */
if (!*sz && (*nsz <= *osz))
return (USTR_TRUE);
if (!*alloc)
return (USTR_FALSE);
return (USTR_TRUE);
}
/* NOTE: This is the main "deallocation" function (apart from plain free) --
* this is one of only three things that removes data via. REALLOC.
* Others are in ustr-set.h */
USTR_CONF_i_PROTO
int ustrp__del(struct Ustr_pool *p, struct Ustr **ps1, size_t len)
{
struct Ustr *s1 = USTR_NULL;
struct Ustr *ret = USTR_NULL;
size_t sz = 0;
size_t oh = 0;
size_t osz = 0;
size_t nsz = 0;
size_t clen = 0;
size_t nlen = 0;
int alloc = USTR_FALSE;
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
if (!len)
return (USTR_TRUE);
s1 = *ps1;
clen = ustr_len(s1);
/* under certain conditions, we can just return "" as it's much more efficient
* and we don't get anything with not doing it. */
if (!(nlen = clen - len) && /* we are deleting everything */
!(ustr_fixed(*ps1) || /* NOT in "free" space, or */
(ustr_sized(*ps1) && ustr_owner(*ps1))) && /* sized with one ref. */
ustr__dupx_cmp_eq(USTR__DUPX_DEF, USTR__DUPX_FROM(s1))) /* def. config. */
{
ustrp__sc_free2(p, ps1, USTR(""));
return (USTR_TRUE);
}
if (nlen > clen) /* underflow */
return (USTR_FALSE);
if (ustr__rw_mod(s1, nlen, &sz, &oh, &osz, &nsz, &alloc))
{
size_t eos_len = sizeof(USTR_END_ALOCDx);
if (alloc)
{ /* ignore errors? -- can they happen on truncate? */
int emem = ustr_enomem(*ps1);
USTR_ASSERT(nsz < osz);
USTR_ASSERT(!sz);
if (!ustrp__rw_realloc(p, ps1, USTR_FALSE, osz, nsz))
{
if (!p)
{
ustr_assert(USTR_CNTL_MALLOC_CHECK_MEM_SZ(*ps1, osz));
USTR__CNTL_MALLOC_CHECK_FIXUP_REALLOC(*ps1, nsz);
ustr_assert(USTR_CNTL_MALLOC_CHECK_MEM_SZ(*ps1, nsz));
}
if (!emem)
ustr_setf_enomem_clr(*ps1);
}
}
ustr__terminate((*ps1)->data, ustr_alloc(*ps1), (oh - eos_len) + nlen);
ustr__len_set(*ps1, nlen);
USTR_ASSERT(ustrp__assert_valid(!!p, *ps1));
return (USTR_TRUE);
}
USTR_ASSERT(!ustr_limited(s1));
if (!(ret = ustrp__dupx_undef(p, USTR__DUPX_FROM(s1), nlen)))
{
ustr_setf_enomem_err(s1);
return (USTR_FALSE);
}
ustr__memcpy(ret, 0, ustr_cstr(s1), nlen);
ustrp__sc_free2(p, ps1, ret);
USTR_ASSERT(ustrp__assert_valid(!!p, *ps1));
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_del(struct Ustr **ps1, size_t len)
{ return (ustrp__del(0, ps1, len)); }
USTR_CONF_I_PROTO
int ustrp_del(struct Ustr_pool *p, struct Ustrp **ps1, size_t len)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__del(p, &tmp, len);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_i_PROTO
int ustrp__del_subustr(struct Ustr_pool *p,
struct Ustr **ps1, size_t pos, size_t len)
{
struct Ustr *s1 = USTR_NULL;
struct Ustr *ret = USTR_NULL;
size_t sz = 0;
size_t oh = 0;
size_t osz = 0;
size_t nsz = 0;
size_t clen = 0;
size_t nlen = 0;
int alloc = USTR_FALSE;
const char *ocstr = 0;
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
if (!len)
return (USTR_TRUE);
s1 = *ps1;
clen = ustrp__assert_valid_subustr(!!p, s1, pos, len);
if (!clen)
return (USTR_FALSE);
if (--pos == (clen - len)) /* deleting from the end */
return (ustrp__del(p, ps1, len));
nlen = clen - len;
USTR_ASSERT(nlen < clen);
if (ustr__rw_mod(s1, nlen, &sz, &oh, &osz, &nsz, &alloc))
{ /* Move everything to the begining and call ustr_del() */
char *ptr = ustr_wstr(s1);
USTR_ASSERT(nlen - pos);
memmove(ptr + pos, ptr + pos + len, (nlen - pos));
return (ustrp__del(p, ps1, len));
}
USTR_ASSERT(!ustr_limited(s1));
/* Can't do anything sane, give up and build a new string from scratch */
if (!(ret = ustrp__dupx_undef(p, USTR__DUPX_FROM(s1), nlen)))
{
ustr_setf_enomem_err(s1);
return (USTR_FALSE);
}
ocstr = ustr_cstr(s1);
USTR_ASSERT(pos || (nlen - pos)); /* can be both */
ustr__memcpy(ret, 0, ocstr, pos);
ustr__memcpy(ret, pos, ocstr + pos + len, nlen - pos);
ustrp__sc_free2(p, ps1, ret);
USTR_ASSERT(ustrp__assert_valid(!!p, *ps1));
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_del_subustr(struct Ustr **ps1, size_t pos,size_t len)
{ return (ustrp__del_subustr(0, ps1, pos, len)); }
USTR_CONF_I_PROTO
int ustrp_del_subustrp(struct Ustr_pool *p,
struct Ustrp **ps1, size_t pos, size_t len)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__del_subustr(p, &tmp, pos, len);
*ps1 = USTRP(tmp);
return (ret);
}
/* ---------------- dupx/dup ---------------- */
USTR_CONF_I_PROTO struct Ustr *ustr_dupx_undef(size_t sz, size_t rbytes,
int exact, int emem, size_t len)
{ return (ustrp__dupx_undef(0, sz, rbytes, exact, emem, len)); }
USTR_CONF_I_PROTO
struct Ustrp *ustrp_dupx_undef(struct Ustr_pool *p, size_t sz, size_t rbytes,
int exact, int emem, size_t len)
{ return (USTRP(ustrp__dupx_undef(p, sz, rbytes, exact, emem, len))); }
USTR_CONF_I_PROTO struct Ustr *ustr_dup_undef(size_t len)
{ return (ustr_dupx_undef(USTR__DUPX_DEF, len)); }
USTR_CONF_I_PROTO struct Ustrp *ustrp_dup_undef(struct Ustr_pool *p, size_t len)
{ return (ustrp_dupx_undef(p, USTR__DUPX_DEF, len)); }
USTR_CONF_i_PROTO
struct Ustr *ustrp__dupx_empty(struct Ustr_pool *p, size_t sz, size_t rbytes,
int exact, int emem)
{ /* set the error bit, so we always get malloc()'d data, then clear it */
struct Ustr *s1 = ustrp__dupx_undef(p, sz, rbytes, exact, USTR_TRUE, 0);
int USTR__COMPILE_ATTR_MAYBE_UNUSED(eret) = USTR_FALSE;
if (!s1 || emem)
return (s1);
eret = ustr_setf_enomem_clr(s1);
USTR_ASSERT(eret);
return (s1);
}
USTR_CONF_I_PROTO struct Ustr *ustr_dupx_empty(size_t sz, size_t rbytes,
int exact, int emem)
{ return (ustrp__dupx_empty(0, sz, rbytes, exact, emem)); }
USTR_CONF_I_PROTO
struct Ustrp *ustrp_dupx_empty(struct Ustr_pool *p, size_t sz, size_t rbytes,
int exact, int emem)
{ return (USTRP(ustrp__dupx_empty(p, sz, rbytes, exact, emem))); }
USTR_CONF_I_PROTO struct Ustr *ustr_dup_empty(void)
{ return (ustr_dupx_empty(USTR__DUPX_DEF)); }
USTR_CONF_I_PROTO struct Ustrp *ustrp_dup_empty(struct Ustr_pool *p)
{ return (ustrp_dupx_empty(p, USTR__DUPX_DEF)); }
USTR_CONF_i_PROTO
struct Ustr *ustrp__dupx_buf(struct Ustr_pool *p, size_t sz, size_t rbytes,
int exact, int emem, const void *data, size_t len)
{
struct Ustr *s1 = ustrp__dupx_undef(p, sz, rbytes, exact, emem, len);
if (!s1)
return (USTR_NULL);
ustr__memcpy(s1, 0, data, len);
USTR_ASSERT(ustrp__assert_valid(!!p, s1));
return (s1);
}
USTR_CONF_I_PROTO
struct Ustr *ustr_dupx_buf(size_t sz, size_t rb, int exact,
int emem, const void *data, size_t len)
{ return (ustrp__dupx_buf(0, sz, rb, exact, emem, data, len)); }
USTR_CONF_I_PROTO
struct Ustrp *ustrp_dupx_buf(struct Ustr_pool *p, size_t sz, size_t rb,
int exact, int emem, const void *data, size_t len)
{ return (USTRP(ustrp__dupx_buf(p, sz, rb, exact, emem, data, len))); }
USTR_CONF_I_PROTO struct Ustr *ustr_dup_buf(const void *data, size_t len)
{ return (ustr_dupx_buf(USTR__DUPX_DEF, data, len)); }
USTR_CONF_I_PROTO
struct Ustrp *ustrp_dup_buf(struct Ustr_pool *p, const void *data, size_t len)
{ return (USTRP(ustrp_dupx_buf(p, USTR__DUPX_DEF, data, len))); }
USTR_CONF_i_PROTO
struct Ustr *ustrp__dup(struct Ustr_pool *p, const struct Ustr *s1)
{ /* This ignores the const argument, because it doesn't alter the data, or at
* all when ustr_ro(). */
ustr_assert(USTR__ASSERT_MALLOC_CHECK_MEM(p, s1));
if (ustr__ref_add((struct Ustr *)s1))
return ((struct Ustr *)s1);
return (ustrp__dupx_buf(p, USTR__DUPX_FROM(s1), ustr_cstr(s1), ustr_len(s1)));
}
USTR_CONF_I_PROTO struct Ustr *ustr_dup(const struct Ustr *s1)
{ return (ustrp__dup(0, s1)); }
USTR_CONF_I_PROTO
struct Ustrp *ustrp_dup(struct Ustr_pool *p, const struct Ustrp *s1)
{ return (USTRP(ustrp__dup(p, &s1->s))); }
USTR_CONF_i_PROTO
struct Ustr *ustrp__dupx(struct Ustr_pool *p, size_t sz, size_t rbytes,
int exact, int emem, const struct Ustr *s1)
{ /* the exactneustr of the options is more important than the reference */
USTR_ASSERT((rbytes == 0) || (rbytes == 1) || (rbytes == 2) || (rbytes == 4)||
(USTR_CONF_HAVE_64bit_SIZE_MAX && (rbytes == 8)));
USTR_ASSERT(exact == !!exact);
USTR_ASSERT(emem == !!emem);
if (ustr__dupx_cmp_eq(sz, rbytes, exact, emem, USTR__DUPX_FROM(s1)))
return (ustrp__dup(p, s1));
return (ustrp__dupx_buf(p, sz,rbytes,exact,emem, ustr_cstr(s1),ustr_len(s1)));
}
USTR_CONF_I_PROTO struct Ustr *ustr_dupx(size_t sz, size_t rbytes, int exact,
int emem, const struct Ustr *s1)
{ return (ustrp__dupx(0, sz, rbytes, exact, emem, s1)); }
USTR_CONF_I_PROTO
struct Ustrp *ustrp_dupx(struct Ustr_pool *p, size_t sz, size_t rbytes,
int exact, int emem, const struct Ustrp *s1)
{ return (USTRP(ustrp__dupx(p, sz, rbytes, exact, emem, &s1->s))); }
USTR_CONF_i_PROTO
struct Ustr *ustrp__dupx_subustr(struct Ustr_pool *p,
size_t sz, size_t rbytes, int exact, int emem,
const struct Ustr *s2, size_t pos, size_t len)
{
size_t clen = 0;
USTR_ASSERT(ustrp__assert_valid(!!p, s2));
USTR_ASSERT(pos);
if (!len)
return (ustrp__dupx_undef(p, sz, rbytes, exact, emem, len));
clen = ustrp__assert_valid_subustr(!!p, s2, pos, len);
if (!clen)
return (USTR_NULL);
if (len == clen)
return (ustrp__dupx(p, sz, rbytes, exact, emem, s2));
return (ustrp__dupx_buf(p,sz,rbytes,exact,emem, ustr_cstr(s2) + --pos, len));
}
USTR_CONF_I_PROTO
struct Ustr *ustr_dupx_subustr(size_t sz, size_t rbytes, int exact, int emem,
const struct Ustr *s2, size_t pos, size_t len)
{ return (ustrp__dupx_subustr(0, sz, rbytes, exact, emem, s2, pos, len)); }
USTR_CONF_I_PROTO
struct Ustrp *ustrp_dupx_subustrp(struct Ustr_pool *p,
size_t sz, size_t rbytes, int exact, int emem,
const struct Ustrp *s2, size_t pos,size_t len)
{ return (USTRP(ustrp__dupx_subustr(p, sz, rbytes, exact, emem,
&s2->s, pos, len))); }
USTR_CONF_i_PROTO
struct Ustr *ustrp__dup_subustr(struct Ustr_pool *p, const struct Ustr *s2,
size_t pos, size_t len)
{ return (ustrp__dupx_subustr(p, USTR__DUPX_FROM(s2), s2, pos, len)); }
USTR_CONF_I_PROTO struct Ustr *ustr_dup_subustr(const struct Ustr *s2,
size_t pos, size_t len)
{ return (ustrp__dup_subustr(0, s2, pos, len)); }
USTR_CONF_I_PROTO
struct Ustrp *ustrp_dup_subustrp(struct Ustr_pool *p, const struct Ustrp *s2,
size_t pos, size_t len)
{ return (USTRP(ustrp__dup_subustr(p, &s2->s, pos, len))); }
USTR_CONF_i_PROTO
struct Ustr *ustrp__dupx_rep_chr(struct Ustr_pool *p,
size_t sz, size_t rbytes, int exact, int emem,
char chr, size_t len)
{
struct Ustr *s1 = ustrp__dupx_undef(p, sz, rbytes, exact, emem, len);
if (!s1)
return (USTR_NULL);
if (len)
ustr__memset(s1, 0, chr, len);
USTR_ASSERT(ustrp__assert_valid(!!p, s1));
return (s1);
}
USTR_CONF_I_PROTO
struct Ustr *ustr_dupx_rep_chr(size_t sz, size_t rbytes, int exact, int emem,
char chr, size_t len)
{ return (ustrp__dupx_rep_chr(0, sz, rbytes, exact, emem, chr, len)); }
USTR_CONF_I_PROTO
struct Ustrp *ustrp_dupx_rep_chr(struct Ustr_pool *p,
size_t sz, size_t rbytes, int exact, int emem,
char chr, size_t len)
{ return (USTRP(ustrp__dupx_rep_chr(p, sz, rbytes, exact, emem, chr, len))); }
USTR_CONF_I_PROTO struct Ustr *ustr_dup_rep_chr(char chr, size_t len)
{ return (ustr_dupx_rep_chr(USTR__DUPX_DEF, chr, len)); }
USTR_CONF_I_PROTO
struct Ustrp *ustrp_dup_rep_chr(struct Ustr_pool *p, char chr, size_t len)
{ return (ustrp_dupx_rep_chr(p, USTR__DUPX_DEF, chr, len)); }
/* ---------------- add ---------------- */
/* NOTE: This is one of the two main "allocation" functions --
* this is one of three things funcs that gets more data via. REALLOC.
* Others are in ustr-set.h */
USTR_CONF_i_PROTO
int ustrp__add_undef(struct Ustr_pool *p, struct Ustr **ps1, size_t len)
{
struct Ustr *s1 = USTR_NULL;
struct Ustr *ret = USTR_NULL;
size_t clen = 0;
size_t nlen = 0;
size_t sz = 0;
size_t oh = 0;
size_t osz = 0;
size_t nsz = 0;
int alloc = USTR_FALSE;
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
if (!len)
return (USTR_TRUE);
s1 = *ps1;
clen = ustr_len(s1);
if ((nlen = clen + len) < clen) /* overflow */
goto fail_enomem;
if (ustr__rw_mod(s1, nlen, &sz, &oh, &osz, &nsz, &alloc))
{
size_t eos_len = sizeof(USTR_END_ALOCDx);
if (alloc && !ustrp__rw_realloc(p, ps1, !!sz, osz, nsz))
return (USTR_FALSE);
ustr__terminate((*ps1)->data, ustr_alloc(*ps1), (oh - eos_len) + nlen);
ustr__len_set(*ps1, nlen);
USTR_ASSERT(ustrp__assert_valid(!!p, *ps1));
return (USTR_TRUE);
}
if (ustr_limited(s1))
{
ustr_setf_enomem_err(s1);
return (USTR_FALSE);
}
if (!(ret = ustrp__dupx_undef(p, USTR__DUPX_FROM(s1), nlen)))
goto fail_enomem;
ustr__memcpy(ret, 0, ustr_cstr(s1), ustr_len(s1));
ustrp__sc_free2(p, ps1, ret);
USTR_ASSERT(ustrp__assert_valid(!!p, *ps1));
return (USTR_TRUE);
fail_enomem:
ustr_setf_enomem_err(s1);
return (USTR_FALSE);
}
USTR_CONF_I_PROTO int ustr_add_undef(struct Ustr **ps1, size_t len)
{ return (ustrp__add_undef(0, ps1, len)); }
USTR_CONF_I_PROTO
int ustrp_add_undef(struct Ustr_pool *p, struct Ustrp **ps1, size_t len)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__add_undef(p, &tmp, len);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_i_PROTO int ustrp__add_buf(struct Ustr_pool *p, struct Ustr **ps1,
const void *s2, size_t len)
{
if (!ustrp__add_undef(p, ps1, len))
return (USTR_FALSE);
ustr__memcpy(*ps1, ustr_len(*ps1) - len, s2, len);
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_add_buf(struct Ustr **ps1, const void *s2,size_t len)
{ return (ustrp__add_buf(0, ps1, s2, len)); }
USTR_CONF_I_PROTO int ustrp_add_buf(struct Ustr_pool *p, struct Ustrp **ps1,
const void *s2, size_t len)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__add_buf(p, &tmp, s2, len);
*ps1 = USTRP(tmp);
return (ret);
}
/* If we can use _dup(), otherwise just pretend it's a buf+len */
USTR_CONF_i_PROTO
int ustr__treat_as_buf(const struct Ustr *s1, size_t len1, size_t len2)
{
USTR_ASSERT(!len1 || (len1 == ustr_len(s1))); /* set() uses 0 */
USTR_ASSERT((len1 < (len1 + len2)) || !len2); /* no overflow allowed */
if (len1)
return (USTR_TRUE);
if (ustr_limited(s1))
return (USTR_TRUE);
if (ustr_owner(s1) && (ustr_size(s1) >= len2))
return (USTR_TRUE);
return (USTR_FALSE);
}
USTR_CONF_i_PROTO
int ustrp__add(struct Ustr_pool *p, struct Ustr **ps1, const struct Ustr *s2)
{
struct Ustr *ret = USTR_NULL;
size_t len1 = 0;
size_t len2 = 0;
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
USTR_ASSERT(ustrp__assert_valid(!!p, s2));
len1 = ustr_len(*ps1);
len2 = ustr_len(s2);
if (len1 > (len1 + len2))
{
errno = USTR__ENOMEM;
return (USTR_FALSE);
}
if (!len2)
return (USTR_TRUE);
if ((*ps1 == s2) && ustr_owner(s2) && ustr_alloc(s2))
{ /* only one reference, so we can't take _cstr() before we realloc */
if (!ustrp__add_undef(p, ps1, len1))
return (USTR_FALSE);
ustr__memcpy(*ps1, len1, ustr_cstr(*ps1), len1);
USTR_ASSERT(ustrp__assert_valid(!!p, *ps1));
return (USTR_TRUE);
}
if (ustr__treat_as_buf(*ps1, len1, len2))
return (ustrp__add_buf(p, ps1, ustr_cstr(s2), len2));
USTR_ASSERT(!len1);
if (!(ret = ustrp__dupx(p, USTR__DUPX_FROM(*ps1), s2)))
{
ustr_setf_enomem_err(*ps1);
return (USTR_FALSE);
}
ustrp__sc_free2(p, ps1, ret);
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_add(struct Ustr **ps1, const struct Ustr *s2)
{ return (ustrp__add(0, ps1, s2)); }
USTR_CONF_I_PROTO
int ustrp_add(struct Ustr_pool *p, struct Ustrp **ps1, const struct Ustrp *s2)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__add(p, &tmp, &s2->s);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_i_PROTO
int ustrp__add_subustr(struct Ustr_pool *p, struct Ustr **ps1,
const struct Ustr *s2, size_t pos, size_t len)
{
size_t clen = 0;
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
USTR_ASSERT(ustrp__assert_valid(!!p, s2));
USTR_ASSERT(pos);
if (!len)
return (USTR_TRUE);
clen = ustrp__assert_valid_subustr(!!p, s2, pos, len);
if (!clen)
return (USTR_FALSE);
if (len == clen)
return (ustrp__add(p, ps1, s2));
if (*ps1 != s2)
return (ustrp__add_buf(p, ps1, ustr_cstr(s2) + pos - 1, len));
/* maybe only one reference, so we can't take _cstr() before we realloc */
if (!ustrp__add_undef(p, ps1, len))
return (USTR_FALSE);
ustr__memcpy(*ps1, clen, ustr_cstr(*ps1) + pos - 1, len);
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_add_subustr(struct Ustr **ps1, const struct Ustr *s2,
size_t pos, size_t len)
{ return (ustrp__add_subustr(0, ps1, s2, pos, len)); }
USTR_CONF_I_PROTO
int ustrp_add_subustrp(struct Ustr_pool *p, struct Ustrp **ps1,
const struct Ustrp *s2, size_t pos, size_t len)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__add_subustr(p, &tmp, &s2->s, pos, len);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_i_PROTO int ustrp__add_rep_chr(struct Ustr_pool *p, struct Ustr **ps1,
char chr, size_t len)
{
if (!ustrp__add_undef(p, ps1, len))
return (USTR_FALSE);
ustr__memset(*ps1, ustr_len(*ps1) - len, chr, len);
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_add_rep_chr(struct Ustr **ps1, char chr, size_t len)
{ return (ustrp__add_rep_chr(0, ps1, chr, len)); }
USTR_CONF_I_PROTO int ustrp_add_rep_chr(struct Ustr_pool *p, struct Ustrp **ps1,
char chr, size_t len)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__add_rep_chr(p, &tmp, chr, len);
*ps1 = USTRP(tmp);
return (ret);
}
/* ---------------- shortcut ---------------- */
USTR_CONF_I_PROTO void ustr_sc_free2(struct Ustr **ps1, struct Ustr *s2)
{ ustrp__sc_free2(0, ps1, s2); }
USTR_CONF_I_PROTO void ustrp_sc_free2(struct Ustr_pool *p,
struct Ustrp **ps1, struct Ustrp *s2)
{
struct Ustr *tmp = &(*ps1)->s;
ustrp__sc_free2(p, &tmp, &s2->s);
*ps1 = USTRP(tmp);
}
USTR_CONF_i_PROTO void ustrp__sc_free(struct Ustr_pool *p, struct Ustr **ps1)
{
USTR_ASSERT(ps1);
ustrp__free(p, *ps1);
*ps1 = USTR_NULL;
}
USTR_CONF_I_PROTO void ustr_sc_free(struct Ustr **ps1)
{ ustrp__sc_free(0, ps1); }
USTR_CONF_I_PROTO void ustrp_sc_free(struct Ustr_pool *p, struct Ustrp **ps1)
{
struct Ustr *tmp = &(*ps1)->s;
ustrp__sc_free(p, &tmp);
*ps1 = USTRP(tmp);
}
USTR_CONF_i_PROTO void ustrp__sc_del(struct Ustr_pool *p, struct Ustr **ps1)
{
if (!ustrp__del(p, ps1, ustr_len(*ps1)))
/* Very unlikely, but in this case ignore saving the options/data.
* We can be a little less efficient etc., but no error handling is nice.
* Only thing you have to watch for is ustr_enomem() might not "work"
* after a call to here. */
ustrp__sc_free2(p, ps1, USTR(""));
USTR_ASSERT(!ustr_len(*ps1));
}
USTR_CONF_I_PROTO void ustr_sc_del(struct Ustr **ps1)
{ ustrp__sc_del(0, ps1); }
USTR_CONF_I_PROTO void ustrp_sc_del(struct Ustr_pool *p, struct Ustrp **ps1)
{
struct Ustr *tmp = &(*ps1)->s;
ustrp__sc_del(p, &tmp);
*ps1 = USTRP(tmp);
}
USTR_CONF_I_PROTO
void ustr_conf(const struct Ustr *s1, size_t *ret_esz, size_t *ret_ref,
int *ret_exact, size_t *ret_lenn, size_t *ret_refc)
{
size_t esz = 0;
size_t ref = 0;
int exact = 0;
size_t refc = 0;
USTR_ASSERT(ustr_assert_valid(s1));
if (!ustr_alloc(s1))
{
esz = USTR_CONF_HAS_SIZE;
ref = USTR_CONF_REF_BYTES;
exact = USTR_CONF_EXACT_BYTES;
}
else
{
if (ustr_sized(s1))
esz = ustr__sz_get(s1);
else
esz = 0;
ref = USTR__REF_LEN(s1);
exact = ustr_exact(s1);
refc = !!ref;
}
USTR_ASSERT(ustr__dupx_cmp_eq(USTR__DUPX_FROM(s1),
esz, ref, exact, ustr_enomem(s1)));
if (ret_esz) *ret_esz = esz;
if (ret_ref) *ret_ref = ref;
if (ret_exact) *ret_exact = exact;
if (ret_lenn) *ret_lenn = USTR__LEN_LEN(s1);
if (ret_refc) *ret_refc = refc ? ustr_xi__ref_get(s1) : 0;
}
USTR_CONF_i_PROTO
int ustrp__sc_ensure_owner(struct Ustr_pool *p, struct Ustr **ps1)
{
struct Ustr *ret = USTR_NULL;
size_t clen = 0;
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
if (ustr_owner(*ps1))
return (USTR_TRUE);
if (!(clen = ustr_len(*ps1))) /* so ustr_enomem() and ustr_wstr() work */
ret = ustrp__dupx_empty(p, USTR__DUPX_FROM(*ps1));
else
ret = ustrp__dupx_buf(p, USTR__DUPX_FROM(*ps1), ustr_cstr(*ps1), clen);
if (!ret)
return (USTR_FALSE);
ustrp__sc_free2(p, ps1, ret);
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_sc_ensure_owner(struct Ustr **ps1)
{ return (ustrp__sc_ensure_owner(0, ps1)); }
USTR_CONF_I_PROTO
int ustrp_sc_ensure_owner(struct Ustr_pool *p, struct Ustrp **ps1)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__sc_ensure_owner(p, &tmp);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_i_PROTO char *ustrp__sc_wstr(struct Ustr_pool *p, struct Ustr **ps1)
{
if (!ustrp__sc_ensure_owner(p, ps1))
return (USTR_FALSE);
return (ustr_wstr(*ps1));
}
USTR_CONF_I_PROTO char *ustr_sc_wstr(struct Ustr **ps1)
{ return (ustrp__sc_wstr(0, ps1)); }
USTR_CONF_I_PROTO char *ustrp_sc_wstr(struct Ustr_pool *p, struct Ustrp **ps1)
{
struct Ustr *tmp = &(*ps1)->s;
char *ret = ustrp__sc_wstr(p, &tmp);
*ps1 = USTRP(tmp);
return (ret);
}
|