Skip to content

Add remove Utf8 BOM #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: release
Choose a base branch
from

Conversation

paulbauriegel
Copy link

In the OBJ file if the first line starts with a invisible BOM char like: \uFEFFmtllib model.mtl

paul@mac tinyobjloader % head -n 5 ../0FABEEBE-68BE-4F36-A268-CAE2853D08A8/model.obj | hexdump -C
00000000  ef bb bf 6d 74 6c 6c 69  62 20 6d 6f 64 65 6c 2e  |...mtllib model.|
00000010  6d 74 6c 0d 0a 67 20 5f  30 0d 0a 76 20 30 2e 36  |mtl..g _0..v 0.6|
00000020  38 35 36 34 30 20 35 2e  35 36 37 38 33 30 20 2d  |85640 5.567830 -|
00000030  31 2e 33 39 30 32 37 30  0d 0a 76 20 30 2e 36 38  |1.390270..v 0.68|
00000040  35 32 37 30 20 35 2e 35  33 38 30 33 30 20 2d 31  |5270 5.538030 -1|
00000050  2e 33 36 34 34 33 30 0d  0a 76 20 30 2e 36 38 39  |.364430..v 0.689|
00000060  37 37 30 20 35 2e 35 36  33 35 37 30 20 2d 31 2e  |770 5.563570 -1.|
00000070  33 39 30 32 37 30 0d 0a                           |390270..|

The parsing of the textures does not work in that case because of if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) {

In the OBJ file if the first line starts with a invisible BOM char like:\uFEFFmtllib model.mtl the parsing of the textures does not work.
@syoyo syoyo requested a review from Copilot July 11, 2025 23:12
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds handling for UTF-8 BOM characters at the start of OBJ/MTL files to ensure correct parsing of mtllib and other tokens.

  • Introduce removeUtf8Bom helper to strip BOM bytes if present.
  • Invoke BOM removal on each line before token parsing in LoadMtl and LoadObj.
Comments suppressed due to low confidence (1)

tiny_obj_loader.h:2124

  • The new BOM-removal behavior isn't covered by existing tests. Add unit tests for lines starting with \uFEFF to ensure this function works as expected.
    linebuf = removeUtf8Bom(linebuf);

@syoyo
Copy link
Collaborator

syoyo commented Jul 11, 2025

Please add reproducible test .obj/.mtl to models folder https://github.com/tinyobjloader/tinyobjloader/tree/release/models ,
and also write a unit test.

paulbauriegel and others added 7 commits July 13, 2025 17:32
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Add if (line_no == 1)  removeUtf8Bom(linebuf) to obj aswell
@paulbauriegel
Copy link
Author

@syoyo I added a unit test for removeUtf8Bom and a test with UTF-8 BOM encoded models. As Copilot suggested I also added the BOM check to the MTL file not only the obj file. Let me know if there are any further changes you want me to make.

@syoyo
Copy link
Collaborator

syoyo commented Jul 13, 2025

@paulbauriegel Thank you! Will be merged soon after the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy