diff options
Diffstat (limited to 'src/microhttpd/mhd_str.c')
-rw-r--r-- | src/microhttpd/mhd_str.c | 308 |
1 files changed, 308 insertions, 0 deletions
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c index 0c2b2f46..ef870e61 100644 --- a/src/microhttpd/mhd_str.c +++ b/src/microhttpd/mhd_str.c | |||
@@ -1387,6 +1387,314 @@ MHD_bin_to_hex (const void *bin, | |||
1387 | } | 1387 | } |
1388 | 1388 | ||
1389 | 1389 | ||
1390 | size_t | ||
1391 | MHD_str_pct_decode_strict_n_ (const char *pct_encoded, | ||
1392 | size_t pct_encoded_len, | ||
1393 | char *decoded, | ||
1394 | size_t buf_size) | ||
1395 | { | ||
1396 | #ifdef MHD_FAVOR_SMALL_CODE | ||
1397 | bool broken; | ||
1398 | size_t res; | ||
1399 | |||
1400 | res = MHD_str_pct_decode_lenient_n_ (pct_encoded, pct_encoded_len, decoded, | ||
1401 | buf_size, &broken); | ||
1402 | if (broken) | ||
1403 | return 0; | ||
1404 | return res; | ||
1405 | #else /* ! MHD_FAVOR_SMALL_CODE */ | ||
1406 | size_t r; | ||
1407 | size_t w; | ||
1408 | r = 0; | ||
1409 | w = 0; | ||
1410 | |||
1411 | if (buf_size >= pct_encoded_len) | ||
1412 | { | ||
1413 | while (r < pct_encoded_len) | ||
1414 | { | ||
1415 | const char chr = pct_encoded[r]; | ||
1416 | if ('%' == chr) | ||
1417 | { | ||
1418 | if (2 > pct_encoded_len - r) | ||
1419 | return 0; | ||
1420 | else | ||
1421 | { | ||
1422 | const int h = toxdigitvalue (pct_encoded[++r]); | ||
1423 | const int l = toxdigitvalue (pct_encoded[++r]); | ||
1424 | unsigned char out; | ||
1425 | if ((0 > h) || (0 > l)) | ||
1426 | return 0; | ||
1427 | out = (unsigned char) ( (((uint8_t) ((unsigned int) h)) << 4) | ||
1428 | | ((uint8_t) ((unsigned int) l)) ); | ||
1429 | decoded[w] = (char) out; | ||
1430 | } | ||
1431 | } | ||
1432 | else | ||
1433 | decoded[w] = chr; | ||
1434 | ++r; | ||
1435 | ++w; | ||
1436 | } | ||
1437 | return w; | ||
1438 | } | ||
1439 | |||
1440 | while (r < pct_encoded_len) | ||
1441 | { | ||
1442 | const char chr = pct_encoded[r]; | ||
1443 | if (w >= buf_size) | ||
1444 | return 0; | ||
1445 | if ('%' == chr) | ||
1446 | { | ||
1447 | if (2 > pct_encoded_len - r) | ||
1448 | return 0; | ||
1449 | else | ||
1450 | { | ||
1451 | const int h = toxdigitvalue (pct_encoded[++r]); | ||
1452 | const int l = toxdigitvalue (pct_encoded[++r]); | ||
1453 | unsigned char out; | ||
1454 | if ((0 > h) || (0 > l)) | ||
1455 | return 0; | ||
1456 | out = (unsigned char) ( (((uint8_t) ((unsigned int) h)) << 4) | ||
1457 | | ((uint8_t) ((unsigned int) l)) ); | ||
1458 | decoded[w] = (char) out; | ||
1459 | } | ||
1460 | } | ||
1461 | else | ||
1462 | decoded[w] = chr; | ||
1463 | ++r; | ||
1464 | ++w; | ||
1465 | } | ||
1466 | return w; | ||
1467 | #endif /* ! MHD_FAVOR_SMALL_CODE */ | ||
1468 | } | ||
1469 | |||
1470 | |||
1471 | size_t | ||
1472 | MHD_str_pct_decode_lenient_n_ (const char *pct_encoded, | ||
1473 | size_t pct_encoded_len, | ||
1474 | char *decoded, | ||
1475 | size_t buf_size, | ||
1476 | bool *broken_encoding) | ||
1477 | { | ||
1478 | size_t r; | ||
1479 | size_t w; | ||
1480 | r = 0; | ||
1481 | w = 0; | ||
1482 | if (NULL != broken_encoding) | ||
1483 | *broken_encoding = false; | ||
1484 | #ifndef MHD_FAVOR_SMALL_CODE | ||
1485 | if (buf_size >= pct_encoded_len) | ||
1486 | { | ||
1487 | while (r < pct_encoded_len) | ||
1488 | { | ||
1489 | const char chr = pct_encoded[r]; | ||
1490 | if ('%' == chr) | ||
1491 | { | ||
1492 | if (2 > pct_encoded_len - r) | ||
1493 | { | ||
1494 | if (NULL != broken_encoding) | ||
1495 | *broken_encoding = true; | ||
1496 | decoded[w] = chr; /* Copy "as is" */ | ||
1497 | } | ||
1498 | else | ||
1499 | { | ||
1500 | const int h = toxdigitvalue (pct_encoded[++r]); | ||
1501 | const int l = toxdigitvalue (pct_encoded[++r]); | ||
1502 | unsigned char out; | ||
1503 | if ((0 > h) || (0 > l)) | ||
1504 | { | ||
1505 | r -= 2; | ||
1506 | if (NULL != broken_encoding) | ||
1507 | *broken_encoding = true; | ||
1508 | decoded[w] = chr; /* Copy "as is" */ | ||
1509 | } | ||
1510 | else | ||
1511 | { | ||
1512 | out = (unsigned char) ( (((uint8_t) ((unsigned int) h)) << 4) | ||
1513 | | ((uint8_t) ((unsigned int) l)) ); | ||
1514 | decoded[w] = (char) out; | ||
1515 | } | ||
1516 | } | ||
1517 | } | ||
1518 | else | ||
1519 | decoded[w] = chr; | ||
1520 | ++r; | ||
1521 | ++w; | ||
1522 | } | ||
1523 | return w; | ||
1524 | } | ||
1525 | #endif /* ! MHD_FAVOR_SMALL_CODE */ | ||
1526 | while (r < pct_encoded_len) | ||
1527 | { | ||
1528 | const char chr = pct_encoded[r]; | ||
1529 | if (w >= buf_size) | ||
1530 | return 0; | ||
1531 | if ('%' == chr) | ||
1532 | { | ||
1533 | if (2 > pct_encoded_len - r) | ||
1534 | { | ||
1535 | if (NULL != broken_encoding) | ||
1536 | *broken_encoding = true; | ||
1537 | decoded[w] = chr; /* Copy "as is" */ | ||
1538 | } | ||
1539 | else | ||
1540 | { | ||
1541 | const int h = toxdigitvalue (pct_encoded[++r]); | ||
1542 | const int l = toxdigitvalue (pct_encoded[++r]); | ||
1543 | if ((0 > h) || (0 > l)) | ||
1544 | { | ||
1545 | r -= 2; | ||
1546 | if (NULL != broken_encoding) | ||
1547 | *broken_encoding = true; | ||
1548 | decoded[w] = chr; /* Copy "as is" */ | ||
1549 | } | ||
1550 | else | ||
1551 | { | ||
1552 | unsigned char out; | ||
1553 | out = (unsigned char) ( (((uint8_t) ((unsigned int) h)) << 4) | ||
1554 | | ((uint8_t) ((unsigned int) l)) ); | ||
1555 | decoded[w] = (char) out; | ||
1556 | } | ||
1557 | } | ||
1558 | } | ||
1559 | else | ||
1560 | decoded[w] = chr; | ||
1561 | ++r; | ||
1562 | ++w; | ||
1563 | } | ||
1564 | return w; | ||
1565 | } | ||
1566 | |||
1567 | |||
1568 | size_t | ||
1569 | MHD_str_pct_decode_in_place_strict_ (char *str) | ||
1570 | { | ||
1571 | #ifdef MHD_FAVOR_SMALL_CODE | ||
1572 | size_t res; | ||
1573 | bool broken; | ||
1574 | |||
1575 | res = MHD_str_pct_decode_in_place_lenient_ (str, &broken); | ||
1576 | if (broken) | ||
1577 | { | ||
1578 | res = 0; | ||
1579 | str[0] = 0; | ||
1580 | } | ||
1581 | return res; | ||
1582 | #else /* ! MHD_FAVOR_SMALL_CODE */ | ||
1583 | size_t r; | ||
1584 | size_t w; | ||
1585 | r = 0; | ||
1586 | w = 0; | ||
1587 | |||
1588 | while (0 != str[r]) | ||
1589 | { | ||
1590 | const char chr = str[r++]; | ||
1591 | if ('%' == chr) | ||
1592 | { | ||
1593 | const char d1 = str[r++]; | ||
1594 | if (0 == d1) | ||
1595 | return 0; | ||
1596 | else | ||
1597 | { | ||
1598 | const char d2 = str[r++]; | ||
1599 | if (0 == d2) | ||
1600 | return 0; | ||
1601 | else | ||
1602 | { | ||
1603 | const int h = toxdigitvalue (d1); | ||
1604 | const int l = toxdigitvalue (d2); | ||
1605 | unsigned char out; | ||
1606 | if ((0 > h) || (0 > l)) | ||
1607 | return 0; | ||
1608 | out = (unsigned char) ( (((uint8_t) ((unsigned int) h)) << 4) | ||
1609 | | ((uint8_t) ((unsigned int) l)) ); | ||
1610 | str[w++] = (char) out; | ||
1611 | } | ||
1612 | } | ||
1613 | } | ||
1614 | else | ||
1615 | str[w++] = chr; | ||
1616 | } | ||
1617 | str[w] = 0; | ||
1618 | return w; | ||
1619 | #endif /* ! MHD_FAVOR_SMALL_CODE */ | ||
1620 | } | ||
1621 | |||
1622 | |||
1623 | size_t | ||
1624 | MHD_str_pct_decode_in_place_lenient_ (char *str, | ||
1625 | bool *broken_encoding) | ||
1626 | { | ||
1627 | #ifdef MHD_FAVOR_SMALL_CODE | ||
1628 | size_t len; | ||
1629 | size_t res; | ||
1630 | |||
1631 | len = strlen (str); | ||
1632 | res = MHD_str_pct_decode_lenient_n_ (str, len, str, len, broken_encoding); | ||
1633 | str[res] = 0; | ||
1634 | |||
1635 | return res; | ||
1636 | #else /* ! MHD_FAVOR_SMALL_CODE */ | ||
1637 | size_t r; | ||
1638 | size_t w; | ||
1639 | if (NULL != broken_encoding) | ||
1640 | *broken_encoding = false; | ||
1641 | r = 0; | ||
1642 | w = 0; | ||
1643 | while (0 != str[r]) | ||
1644 | { | ||
1645 | const char chr = str[r++]; | ||
1646 | if ('%' == chr) | ||
1647 | { | ||
1648 | const char d1 = str[r++]; | ||
1649 | if (0 == d1) | ||
1650 | { | ||
1651 | if (NULL != broken_encoding) | ||
1652 | *broken_encoding = true; | ||
1653 | str[w++] = chr; /* Copy "as is" */ | ||
1654 | str[w] = 0; | ||
1655 | return w; | ||
1656 | } | ||
1657 | else | ||
1658 | { | ||
1659 | const char d2 = str[r++]; | ||
1660 | if (0 == d2) | ||
1661 | { | ||
1662 | if (NULL != broken_encoding) | ||
1663 | *broken_encoding = true; | ||
1664 | str[w++] = chr; /* Copy "as is" */ | ||
1665 | str[w++] = d1; /* Copy "as is" */ | ||
1666 | str[w] = 0; | ||
1667 | return w; | ||
1668 | } | ||
1669 | else | ||
1670 | { | ||
1671 | const int h = toxdigitvalue (d1); | ||
1672 | const int l = toxdigitvalue (d2); | ||
1673 | unsigned char out; | ||
1674 | if ((0 > h) || (0 > l)) | ||
1675 | { | ||
1676 | if (NULL != broken_encoding) | ||
1677 | *broken_encoding = true; | ||
1678 | str[w++] = chr; /* Copy "as is" */ | ||
1679 | str[w++] = d1; | ||
1680 | str[w++] = d2; | ||
1681 | continue; | ||
1682 | } | ||
1683 | out = (unsigned char) ( (((uint8_t) ((unsigned int) h)) << 4) | ||
1684 | | ((uint8_t) ((unsigned int) l)) ); | ||
1685 | str[w++] = (char) out; | ||
1686 | continue; | ||
1687 | } | ||
1688 | } | ||
1689 | } | ||
1690 | str[w++] = chr; | ||
1691 | } | ||
1692 | str[w] = 0; | ||
1693 | return w; | ||
1694 | #endif /* ! MHD_FAVOR_SMALL_CODE */ | ||
1695 | } | ||
1696 | |||
1697 | |||
1390 | #ifdef DAUTH_SUPPORT | 1698 | #ifdef DAUTH_SUPPORT |
1391 | bool | 1699 | bool |
1392 | MHD_str_equal_quoted_bin_n (const char *quoted, | 1700 | MHD_str_equal_quoted_bin_n (const char *quoted, |