gamedig / rust-gamedig

Game Server Query Library.
https://crates.io/crates/gamedig
MIT License
38 stars 11 forks source link

Gamespy 1 using wrong encoding #89

Open Douile opened 1 year ago

Douile commented 1 year ago

Describe the bug When decoding gamespy1 packets we use UTF-8 which doesn't match node-gamedig (latin1):

https://github.com/gamedig/node-gamedig/blob/5db85aae15f3d62e4521cb6fd72c576ab41e28e6/protocols/gamespy1.js#L34-L37

Example error

Utf8Error { valid_up_to: 264, error_len: Some(1) }
[92, 112, 105, 110, 103, 95, 50, 50, 92, 52, 49, 92, 112, 108, 97, 121, 101, 114, 110, 97, 109, 101, 95, 50, 50, 92, 83, 110, 101, 97, 107, 121, 92, 115, 99, 111, 114, 101, 95, 50, 50, 92, 48, 92, 116, 101, 97, 109, 95, 50, 50, 92, 50, 92, 100, 101, 97, 116, 104, 115, 95, 50, 51, 92, 49, 92, 107, 101, 121, 104, 97, 115, 104, 95, 50, 51, 92, 98, 55, 99, 55, 54, 56, 55, 49, 48, 55, 48, 101, 97, 49, 101, 57, 57, 55, 50, 52, 97, 49, 49, 100, 55, 50, 48, 99, 55, 54, 53, 97, 92, 107, 105, 108, 108, 115, 95, 50, 51, 92, 48, 92, 112, 105, 110, 103, 95, 50, 51, 92, 50, 54, 53, 92, 112, 108, 97, 121, 101, 114, 110, 97, 109, 101, 95, 50, 51, 92, 65, 101, 114, 111, 92, 115, 99, 111, 114, 101, 95, 50, 51, 92, 48, 92, 116, 101, 97, 109, 95, 50, 51, 92, 49, 92, 100, 101, 97, 116, 104, 115, 95, 50, 52, 92, 48, 92, 107, 101, 121, 104, 97, 115, 104, 95, 50, 52, 92, 100, 52, 49, 100, 56, 99, 100, 57, 56, 102, 48, 48, 98, 50, 48, 52, 101, 57, 56, 48, 48, 57, 57, 56, 101, 99, 102, 56, 52, 50, 55, 101, 92, 107, 105, 108, 108, 115, 95, 50, 52, 92, 48, 92, 112, 105, 110, 103, 95, 50, 52, 92, 48, 92, 112, 108, 97, 121, 101, 114, 110, 97, 109, 101, 95, 50, 52, 92, 200, 226, 224, 237, 32, 195, 238, 226, 237, 238, 226, 92, 115, 99, 111, 114, 101, 95, 50, 52, 92, 48, 92, 116, 101, 97, 109, 95, 50, 52, 92, 50, 92, 116, 101, 97, 109, 110, 97, 109, 101, 95, 48, 92, 97, 120, 105, 115, 92, 116, 101, 97, 109, 110, 97, 109, 101, 95, 49, 92, 97, 108, 108, 105, 101, 100, 92, 102, 105, 110, 97, 108, 92, 92, 113, 117, 101, 114, 121, 105, 100, 92, 55, 51, 51, 51, 46, 53]
      { fn: "<gamedig::buffer::Utf8Decoder as gamedig::buffer::StringDecoder>::decode_string::{{closure}}", file: "./src/buffer.rs", line: 352 },
      { fn: "core::result::Result<T,E>::map_err", file: "/rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/core/src/result.rs", line: 828 },
      { fn: "<gamedig::buffer::Utf8Decoder as gamedig::buffer::StringDecoder>::decode_string", file: "./src/buffer.rs", line: 347 },
      { fn: "gamedig::buffer::Buffer<B>::read_string", file: "./src/buffer.rs", line: 158 },
      { fn: "gamedig::protocols::gamespy::protocols::one::protocol::get_server_values", file: "./src/protocols/gamespy/protocols/one/protocol.rs", line: 39 },

Steps To Reproduce Happens randomly when parsing gamespy1 games like bf1942

cainthebest commented 1 year ago

Can confirm the provided byte sequence appears to contain segments that are not compliant with UTF-8. Given that UTF-8 only offers limited support for Latin-1 characters, attempting to parse these segments as UTF-8 unsurprisingly triggers a Utf8Error. This inconsistency likely explains the sporadic nature of the issue as it occurs when an unsupported character is encountered during parsing.

Douile commented 1 year ago

Looking at this stackoverflow answer I think the encoding crate is the better choice.