Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
24 changes: 24 additions & 0 deletions 24 models/cube_w_BOM.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
newmtl white
Ka 0 0 0
Kd 1 1 1
Ks 0 0 0

newmtl red
Ka 0 0 0
Kd 1 0 0
Ks 0 0 0

newmtl green
Ka 0 0 0
Kd 0 1 0
Ks 0 0 0

newmtl blue
Ka 0 0 0
Kd 0 0 1
Ks 0 0 0

newmtl light
Ka 20 20 20
Kd 1 1 1
Ks 0 0 0
32 changes: 32 additions & 0 deletions 32 models/cube_w_BOM.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
mtllib cube_w_BOM.mtl

v 0.000000 2.000000 2.000000
v 0.000000 0.000000 2.000000
v 2.000000 0.000000 2.000000
v 2.000000 2.000000 2.000000
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.000000 0.000000
v 2.000000 2.000000 0.000000
# 8 vertices

g front cube
usemtl white
f 1 2 3 4
# two white spaces between 'back' and 'cube'
g back cube
# expects white material
f 8 7 6 5
g right cube
usemtl red
f 4 3 7 8
g top cube
usemtl white
f 5 1 4 8
g left cube
usemtl green
f 5 6 2 1
g bottom cube
usemtl white
f 2 6 7 3
# 6 elements
54 changes: 54 additions & 0 deletions 54 tests/tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1465,9 +1465,61 @@ void test_default_kd_for_multiple_materials_issue391() {
std::cerr << "Unexpected material found!" << std::endl;
TEST_CHECK(false);
}
}
}

void test_removeUtf8Bom() {
// Basic input with BOM
std::string withBOM = "\xEF\xBB\xBFhello world";
TEST_CHECK(tinyobj::removeUtf8Bom(withBOM) == "hello world");

// Input without BOM
std::string noBOM = "hello world";
TEST_CHECK(tinyobj::removeUtf8Bom(noBOM) == "hello world");

// Leaves short string unchanged
std::string shortStr = "\xEF";
TEST_CHECK(tinyobj::removeUtf8Bom(shortStr) == shortStr);

std::string shortStr2 = "\xEF\xBB";
TEST_CHECK(tinyobj::removeUtf8Bom(shortStr2) == shortStr2);

// BOM only returns empty string
std::string justBom = "\xEF\xBB\xBF";
TEST_CHECK(tinyobj::removeUtf8Bom(justBom) == "");

// Empty string
std::string emptyStr = "";
TEST_CHECK(tinyobj::removeUtf8Bom(emptyStr) == "");
}

void test_loadObj_with_BOM() {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;

std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/cube_w_BOM.obj", gMtlBasePath);

if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}

if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
}

TEST_CHECK(true == ret);
TEST_CHECK(6 == shapes.size());
TEST_CHECK(0 == shapes[0].name.compare("front cube"));
TEST_CHECK(0 == shapes[1].name.compare("back cube")); // multiple whitespaces
// are aggregated as
// single white space.
}


// Fuzzer test.
// Just check if it does not crash.
// Disable by default since Windows filesystem can't create filename of afl
Expand Down Expand Up @@ -1579,4 +1631,6 @@ TEST_LIST = {
test_invalid_texture_vertex_index},
{"default_kd_for_multiple_materials_issue391",
test_default_kd_for_multiple_materials_issue391},
{"test_removeUtf8Bom", test_removeUtf8Bom},
{"test_loadObj_with_BOM", test_loadObj_with_BOM},
{NULL, NULL}};
17 changes: 17 additions & 0 deletions 17 tiny_obj_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,17 @@ static inline std::string toString(const T &t) {
return ss.str();
}

static inline std::string removeUtf8Bom(const std::string& input) {
// UTF-8 BOM = 0xEF,0xBB,0xBF
if (input.size() >= 3 &&
static_cast<unsigned char>(input[0]) == 0xEF &&
static_cast<unsigned char>(input[1]) == 0xBB &&
static_cast<unsigned char>(input[2]) == 0xBF) {
return input.substr(3); // Skip BOM
}
return input;
}

struct warning_context {
std::string *warn;
size_t line_number;
Expand Down Expand Up @@ -2110,6 +2121,9 @@ void LoadMtl(std::map<std::string, int> *material_map,
if (linebuf.empty()) {
continue;
}
if (line_no == 1) {
linebuf = removeUtf8Bom(linebuf);
}

// Skip leading space.
const char *token = linebuf.c_str();
Expand Down Expand Up @@ -2637,6 +2651,9 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
if (linebuf.empty()) {
continue;
}
if (line_num == 1) {
linebuf = removeUtf8Bom(linebuf);
}

// Skip leading space.
const char *token = linebuf.c_str();
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.