Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/kf/TextDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ namespace kf
{
using namespace std;

///////////////////////////////////////////////////////////////////////////////////////////////////
// TextDetector class provides a utility to determine whether a given buffer contains textual data.
// It filters out control characters (except for \t, \n, \r) to verify that the content represents valid text.
// Supported encodings include ANSI, UTF-8, UTF-16 (LE/BE), and UTF-32 (LE/BE).
class TextDetector
{
public:
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ wdk_add_driver(kf-test WINVER NTDDI_WIN10 STL
HexTest.cpp
MapTest.cpp
Vector.cpp
TextDetectorTest.cpp
ScopeExitTest.cpp
SingletonTest.cpp
DoubleLinkedListTest.cpp
Expand Down
221 changes: 221 additions & 0 deletions test/TextDetectorTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
#include "pch.h"
#include <kf/TextDetector.h>

SCENARIO("TextDetector::isText")
{
GIVEN("Valid UTF-8 text with BOM")
{
constexpr uint8_t kData[] = { 0xEF, 0xBB, 0xBF, 'T', 'e', 's', 't' };

THEN("Text is detected")
{
REQUIRE(kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
};

GIVEN("UTF-8 text with BOM and invalid char")
{
constexpr uint8_t kData[] = { 0xEF, 0xBB, 0xBF, 0x01, 'a', 'b' };

THEN("Not a text is detected")
{
REQUIRE(!kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
};

GIVEN("Valid UTF-16LE text with BOM")
{
constexpr uint8_t kData[] = {
0xFF, 0xFE,
'T', 0x00,
'e', 0x00,
's', 0x00,
't', 0x00
};

THEN("Text is detected")
{
REQUIRE(kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
}

GIVEN("UTF-16LE text with BOM and invalid char")
{
constexpr uint8_t kData[] = {
0xFF, 0xFE,
0x01, 0x00, // invalid char
'a', 0x00
};

THEN("Not a text is detected")
{
REQUIRE(!kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
};

GIVEN("UTF-16BE text with BOM")
{
constexpr uint8_t kData[] = {
0xFE, 0xFF,
0x00, 'T',
0x00, 'e',
0x00, 's',
0x00, 't'
};

THEN("Text is detected")
{
REQUIRE(kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
};

GIVEN("UTF-16BE text with BOM and invalid char")
{
constexpr uint8_t kData[] = {
0xFE, 0xFF,
0x00, 0x01, // invalid char
0x00, 'a'
};

THEN("Not a text is detected")
{
REQUIRE(!kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
};

GIVEN("Valid UTF-32LE text with BOM")
{
constexpr uint8_t kData[] = {
0xFF, 0xFE, 0x00, 0x00,
'T', 0x00, 0x00, 0x00,
'e', 0x00, 0x00, 0x00,
's', 0x00, 0x00, 0x00,
't', 0x00, 0x00, 0x00
};

THEN("Text is detected")
{
REQUIRE(kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
}

GIVEN("UTF-32LE text with BOM and invalid char")
{
constexpr uint8_t kData[] = {
0xFF, 0xFE, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, // invalid char
'a', 0x00, 0x00, 0x00
};

THEN("Not a text is detected")
{
REQUIRE(!kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
}

GIVEN("Valid UTF-32BE text with BOM")
{
constexpr uint8_t kData[] = {
0x00, 0x00, 0xFE, 0xFF,
0x00, 0x00, 0x00, 'T',
0x00, 0x00, 0x00, 'e',
0x00, 0x00, 0x00, 's',
0x00, 0x00, 0x00, 't'
};

THEN("Text is detected")
{
REQUIRE(kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
}

GIVEN("UTF-32BE text with BOM and invalid char")
{
constexpr uint8_t kData[] = {
0x00, 0x00, 0xFE, 0xFF,
0x00, 0x00, 0x00, 0x01, // invalid char
0x00, 0x00, 0x00, 'a'
};

THEN("Not a text is detected")
{
REQUIRE(!kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
}

GIVEN("Valid UTF-16BE text without BOM")
{
constexpr uint8_t kData[] = {
0x00, 'T',
0x00, 'e',
0x00, 's',
0x00, 't'
};

THEN("Text is detected")
{
REQUIRE(kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
};

GIVEN("UTF-16BE text without BOM and invalid char")
{
constexpr uint8_t kData[] = {
0x00, 0x01, // invalid char
0x00, 'a'
};

THEN("Not a text is detected")
{
REQUIRE(!kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
};

GIVEN("Valid UTF-16LE text without BOM")
{
constexpr uint8_t kData[] = {
'T', 0x00,
'e', 0x00,
's', 0x00,
't', 0x00
};

THEN("Text is detected")
{
REQUIRE(kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
}

GIVEN("UTF-16LE text without BOM and invalid char")
{
constexpr uint8_t kData[] = {
0x01, 0x00, // invalid char
'a', 0x00
};

THEN("Not a text is detected")
{
REQUIRE(!kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
};

GIVEN("Valid UTF-8 text without BOM")
{
constexpr uint8_t kData[] = { 'T', 'e', 's', 't' };

THEN("Text is detected")
{
REQUIRE(kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
};

GIVEN("UTF-8 text without BOM and invalid char")
{
constexpr uint8_t kData[] = { 0x01, 'a', 'b' };

THEN("Not a text is detected")
{
REQUIRE(!kf::TextDetector::isText(std::as_bytes(std::span{ kData })));
}
};
}