Skip to content

Commit

Permalink
Added UTF-8 BOM detection
Browse files Browse the repository at this point in the history
- This prevents things like the following from happening, by warning the
  user if an incompatible file is loaded:
  http://hercules.ws/board/topic/5126-mapname-selft-announcer/?p=33068
- We're erroring out instead of ignoring it, as explained in the source
  comment.
- Special thanks to jaBote.

Signed-off-by: Haru <haru@dotalux.com>
  • Loading branch information
MishimaHaruna committed Apr 2, 2014
1 parent 34658a7 commit 694d32a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/map/npc.c
Expand Up @@ -3951,6 +3951,18 @@ int npc_parsesrcfile(const char* filepath, bool runOnInit) {
}
fclose(fp);

if ((unsigned char)buffer[0] == 0xEF && (unsigned char)buffer[1] == 0xBB && (unsigned char)buffer[2] == 0xBF) {
// UTF-8 BOM. This is most likely an error on the user's part, because:
// - BOM is discouraged in UTF-8, and the only place where you see it is Notepad and such.
// - It's unlikely that the user wants to use UTF-8 data here, since we don't really support it, nor does the client by default.
// - If the user really wants to use UTF-8 (instead of latin1, EUC-KR, SJIS, etc), then they can still do it <without BOM>.
// More info at http://unicode.org/faq/utf_bom.html#bom5 and http://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
ShowError("npc_parsesrcfile: Detected unsupported UTF-8 BOM in file '%s'. Stopping (please consider using another character set.)\n", filepath);
aFree(buffer);
fclose(fp);
return -1;
}

// parse buffer
for( p = script->skip_space(buffer); p && *p ; p = script->skip_space(p) )
{
Expand Down

3 comments on commit 694d32a

@EPuncker
Copy link
Contributor

Choose a reason for hiding this comment

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

oh my! thanks for this!

@EPuncker
Copy link
Contributor

Choose a reason for hiding this comment

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

btw do that covers other files? I got something like this with messages.conf since I changed it when I didn't knew about it, so maybe is good to cover db and conf files too? :D just my 50 cents

@MishimaHaruna
Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, yes, I agree - it currently only works on scripts, but it should be moved elsewhere (common/utils.c) and used in other places as well.

Please sign in to comment.