Giter Club home page Giter Club logo

Comments (7)

bernds avatar bernds commented on August 28, 2024

What is BOM? And how does it fail?

from q5go.

bernds avatar bernds commented on August 28, 2024

Could you attach the SGF file in question?

from q5go.

missdeer avatar missdeer commented on August 28, 2024

BOM explanation: https://en.wikipedia.org/wiki/Byte_order_mark

You may download a SGF file contains BOM at https://www.101weiqi.com/file/sgf/96/962980_1_时越_VS_杨鼎新.sgf

from q5go.

bernds avatar bernds commented on August 28, 2024

Hmm. That really doesn't seem valid according to the SGF spec (which really implies that SGF is a binary format, not a text format), but I guess if it's fairly common, we should try to special-case it.

from q5go.

bernds avatar bernds commented on August 28, 2024

Here's a patch that just ignores such a BOM. Does this work for the files you have? If not, could you attach more test cases?

diff --git a/src/sgfload.cc b/src/sgfload.cc
index a7cd3ae..ac48a8d 100644
--- a/src/sgfload.cc
+++ b/src/sgfload.cc
@@ -93,6 +93,20 @@ sgf *load_sgf (std::istream &in)
 {
     char nextch;
 
+    if (!in.get (nextch))
+	throw premature_eof ();
+    /* Look for (and discard) a UTF-8 BOM.  Bogus, since SGF is a
+       binary file format, but it occurs in the wild.  */
+    if (nextch == (char)0xEF) {
+	if (!in.get (nextch))
+	    throw premature_eof ();
+	if (nextch != (char)0xBB)
+	    throw broken_sgf ();
+	if (!in.get (nextch))
+	    throw premature_eof ();
+	if (nextch != (char)0xBF)
+	    throw broken_sgf ();
+    }
     nextch = skip_whitespace (in);
     if (nextch != '(')
 	throw broken_sgf ();

from q5go.

bernds avatar bernds commented on August 28, 2024

Here's a better version.

diff --git a/src/sgfload.cc b/src/sgfload.cc
index a7cd3ae..cdcacb3 100644
--- a/src/sgfload.cc
+++ b/src/sgfload.cc
@@ -93,7 +93,24 @@ sgf *load_sgf (std::istream &in)
 {
     char nextch;
 
-    nextch = skip_whitespace (in);
+    if (!in.get (nextch))
+       throw premature_eof ();
+    /* Look for (and discard) a UTF-8 BOM.  Bogus, since SGF is a
+       binary file format, but it occurs in the wild.  */
+    if (nextch == (char)0xEF) {
+       if (!in.get (nextch))
+           throw premature_eof ();
+       if (nextch != (char)0xBB)
+           throw broken_sgf ();
+       if (!in.get (nextch))
+           throw premature_eof ();
+       if (nextch != (char)0xBF)
+           throw broken_sgf ();
+       if (!in.get (nextch))
+           throw premature_eof ();
+    }
+    if (isspace (nextch))
+       nextch = skip_whitespace (in);
     if (nextch != '(')
        throw broken_sgf ();
     sgf *s = new sgf (parse_gametree (in));

from q5go.

missdeer avatar missdeer commented on August 28, 2024

so far, I only found UTF-8 encoded SGF files may contains BOM.
the patch works for me.

from q5go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.