|
8 | 8 | #include <functional>
|
9 | 9 | #include <string>
|
10 | 10 |
|
| 11 | +#include <boost/algorithm/string.hpp> |
| 12 | + |
11 | 13 | #include "crow/settings.h"
|
12 | 14 |
|
13 | 15 | namespace crow
|
@@ -544,6 +546,128 @@ template <typename F, typename Set>
|
544 | 546 | return base64encode(data, size, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
|
545 | 547 | }
|
546 | 548 |
|
| 549 | + inline static void sanitize_filename(std::string& data, char replacement = '_') |
| 550 | + { |
| 551 | + unsigned char i = 0, length_limit; |
| 552 | + |
| 553 | + length_limit = data.length() < 255 ? data.length() : 255; |
| 554 | + data = data.substr(0, length_limit); |
| 555 | + |
| 556 | + for (; i < length_limit; i++) |
| 557 | + { |
| 558 | + switch ((unsigned char)data[i]) |
| 559 | + { |
| 560 | + // WARNING While I can't see how using '\' or '/' would cause a problem, it still warrants an investigation |
| 561 | + //case '/': |
| 562 | + case '?': |
| 563 | + case '<': |
| 564 | + case '>': |
| 565 | + //case '\\': |
| 566 | + case ':': |
| 567 | + case '*': |
| 568 | + case '|': |
| 569 | + case '\"': |
| 570 | + |
| 571 | + case 0x00: |
| 572 | + case 0x01: |
| 573 | + case 0x02: |
| 574 | + case 0x03: |
| 575 | + case 0x04: |
| 576 | + case 0x05: |
| 577 | + case 0x06: |
| 578 | + case 0x07: |
| 579 | + case 0x08: |
| 580 | + case 0x09: |
| 581 | + case 0x0a: |
| 582 | + case 0x0b: |
| 583 | + case 0x0c: |
| 584 | + case 0x0d: |
| 585 | + case 0x0e: |
| 586 | + case 0x0f: |
| 587 | + case 0x10: |
| 588 | + case 0x11: |
| 589 | + case 0x12: |
| 590 | + case 0x13: |
| 591 | + case 0x14: |
| 592 | + case 0x15: |
| 593 | + case 0x16: |
| 594 | + case 0x17: |
| 595 | + case 0x18: |
| 596 | + case 0x19: |
| 597 | + case 0x1a: |
| 598 | + case 0x1b: |
| 599 | + case 0x1c: |
| 600 | + case 0x1d: |
| 601 | + case 0x1e: |
| 602 | + case 0x1f: |
| 603 | + |
| 604 | + case 0x80: |
| 605 | + case 0x81: |
| 606 | + case 0x82: |
| 607 | + case 0x83: |
| 608 | + case 0x84: |
| 609 | + case 0x85: |
| 610 | + case 0x86: |
| 611 | + case 0x87: |
| 612 | + case 0x88: |
| 613 | + case 0x89: |
| 614 | + case 0x8a: |
| 615 | + case 0x8b: |
| 616 | + case 0x8c: |
| 617 | + case 0x8d: |
| 618 | + case 0x8e: |
| 619 | + case 0x8f: |
| 620 | + case 0x90: |
| 621 | + case 0x91: |
| 622 | + case 0x92: |
| 623 | + case 0x93: |
| 624 | + case 0x94: |
| 625 | + case 0x95: |
| 626 | + case 0x96: |
| 627 | + case 0x97: |
| 628 | + case 0x98: |
| 629 | + case 0x99: |
| 630 | + case 0x9a: |
| 631 | + case 0x9b: |
| 632 | + case 0x9c: |
| 633 | + case 0x9d: |
| 634 | + case 0x9e: |
| 635 | + case 0x9f: |
| 636 | + |
| 637 | + data[i] = replacement; |
| 638 | + break; |
| 639 | + |
| 640 | + default: |
| 641 | + break; |
| 642 | + } |
| 643 | + } |
| 644 | + std::string str_replacement(1, replacement); |
| 645 | + |
| 646 | + boost::ireplace_all(data, "..", str_replacement); |
| 647 | + |
| 648 | + boost::ireplace_all(data, "CON", str_replacement); |
| 649 | + boost::ireplace_all(data, "PRN", str_replacement); |
| 650 | + boost::ireplace_all(data, "AUX", str_replacement); |
| 651 | + boost::ireplace_all(data, "NUL", str_replacement); |
| 652 | + boost::ireplace_all(data, "COM1", str_replacement); |
| 653 | + boost::ireplace_all(data, "COM2", str_replacement); |
| 654 | + boost::ireplace_all(data, "COM3", str_replacement); |
| 655 | + boost::ireplace_all(data, "COM4", str_replacement); |
| 656 | + boost::ireplace_all(data, "COM5", str_replacement); |
| 657 | + boost::ireplace_all(data, "COM6", str_replacement); |
| 658 | + boost::ireplace_all(data, "COM7", str_replacement); |
| 659 | + boost::ireplace_all(data, "COM8", str_replacement); |
| 660 | + boost::ireplace_all(data, "COM9", str_replacement); |
| 661 | + boost::ireplace_all(data, "LPT1", str_replacement); |
| 662 | + boost::ireplace_all(data, "LPT2", str_replacement); |
| 663 | + boost::ireplace_all(data, "LPT3", str_replacement); |
| 664 | + boost::ireplace_all(data, "LPT4", str_replacement); |
| 665 | + boost::ireplace_all(data, "LPT5", str_replacement); |
| 666 | + boost::ireplace_all(data, "LPT6", str_replacement); |
| 667 | + boost::ireplace_all(data, "LPT7", str_replacement); |
| 668 | + boost::ireplace_all(data, "LPT8", str_replacement); |
| 669 | + boost::ireplace_all(data, "LPT9", str_replacement); |
| 670 | + } |
547 | 671 |
|
548 | 672 | } // namespace utility
|
549 | 673 | }
|
0 commit comments