fador/mineserver — pull request #183 by fredreichbier (2012-03-29 10:03:56+00:00)
Title: Fixes in the UTF-8 code

Hi there!

I was going to implement the active players / max players statistics for the multiplayer server connect screen and stumbled upon some issues with the UTF-8 code in `src/utf8.h`. Specifically, some of the bitmasks used seemed to be a little off, resulting in a malformed UTF-16 sequence sent to the server.

For example, for a 2-byte codepoint, the first byte [has to be](http://en.wikipedia.org/wiki/UTF-8#Description) `110xxxxx`, so the first three bits have to be `110`. The bitmask to check this has to be `11100000`, and a bitwise-and operation must have the result `11000000` (because only the first two bits are set, the third is unset). Thus, the correct check has to be `(data[0] & 0xE0) == 0xC0`.

Similarly, the code to check the subsequent bytes (which have to be `10xxxxxx`) is `(byte & 0xC0) == 0x80`.

I have done some checks with that code. Most of them succeeded, but then I ran into issues with non-BMP characters that I don't fully understand :D But from a theoretical point of view, this code should be correct. Please review, all these bits and bytes make my brain hurt a little bit.

Cheers!

Friedrich