diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-06-07 13:29:02 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-06-07 19:32:57 +0300 |
commit | bde583e6474553c8e86d1286b82ac3bbb04c0fee (patch) | |
tree | 002693cf9a902f20c3dc174111fc3b018e658f04 /src/microhttpd/mhd_str.c | |
parent | 4195c246bb47080eb7ce8c55d25c825da96a3a6e (diff) | |
download | libmicrohttpd-bde583e6474553c8e86d1286b82ac3bbb04c0fee.tar.gz libmicrohttpd-bde583e6474553c8e86d1286b82ac3bbb04c0fee.zip |
MHD_str_quote(): optimized for typical scenario
Diffstat (limited to 'src/microhttpd/mhd_str.c')
-rw-r--r-- | src/microhttpd/mhd_str.c | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c index f8e1965f..d3d8c594 100644 --- a/src/microhttpd/mhd_str.c +++ b/src/microhttpd/mhd_str.c | |||
@@ -1489,25 +1489,45 @@ MHD_str_quote (const char *unquoted, | |||
1489 | r = 0; | 1489 | r = 0; |
1490 | w = 0; | 1490 | w = 0; |
1491 | 1491 | ||
1492 | if (unquoted_len > buf_size) | 1492 | #ifndef MHD_FAVOR_SMALL_CODE |
1493 | return 0; /* The output buffer is too small */ | 1493 | if (unquoted_len * 2 <= buf_size) |
1494 | |||
1495 | while (unquoted_len > r) | ||
1496 | { | 1494 | { |
1497 | if (buf_size <= w) | 1495 | /* Fast loop: the output will fit the buffer with any input string content */ |
1498 | return 0; /* The output buffer is too small */ | 1496 | while (unquoted_len > r) |
1499 | else | ||
1500 | { | 1497 | { |
1501 | const char chr = unquoted[r++]; | 1498 | const char chr = unquoted[r++]; |
1502 | if (('\\' == chr) || ('\"' == chr)) | 1499 | if (('\\' == chr) || ('\"' == chr)) |
1503 | { | ||
1504 | result[w++] = '\\'; /* Escape current char */ | 1500 | result[w++] = '\\'; /* Escape current char */ |
1505 | if (buf_size <= w) | ||
1506 | return 0; /* The output buffer is too small */ | ||
1507 | } | ||
1508 | result[w++] = chr; | 1501 | result[w++] = chr; |
1509 | } | 1502 | } |
1510 | } | 1503 | } |
1504 | else | ||
1505 | { | ||
1506 | if (unquoted_len > buf_size) | ||
1507 | return 0; /* Quick fail: the output buffer is too small */ | ||
1508 | #else /* MHD_FAVOR_SMALL_CODE */ | ||
1509 | if (1) | ||
1510 | { | ||
1511 | #endif /* MHD_FAVOR_SMALL_CODE */ | ||
1512 | |||
1513 | while (unquoted_len > r) | ||
1514 | { | ||
1515 | if (buf_size <= w) | ||
1516 | return 0; /* The output buffer is too small */ | ||
1517 | else | ||
1518 | { | ||
1519 | const char chr = unquoted[r++]; | ||
1520 | if (('\\' == chr) || ('\"' == chr)) | ||
1521 | { | ||
1522 | result[w++] = '\\'; /* Escape current char */ | ||
1523 | if (buf_size <= w) | ||
1524 | return 0; /* The output buffer is too small */ | ||
1525 | } | ||
1526 | result[w++] = chr; | ||
1527 | } | ||
1528 | } | ||
1529 | } | ||
1530 | |||
1511 | mhd_assert (w >= r); | 1531 | mhd_assert (w >= r); |
1512 | mhd_assert (w <= r * 2); | 1532 | mhd_assert (w <= r * 2); |
1513 | return w; | 1533 | return w; |