diff --git a/Cargo.lock b/Cargo.lock index 936c7e5..ef2b421 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,7 @@ name = "solstice" version = "0.1.0" dependencies = [ "byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "encoding 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -59,6 +60,63 @@ dependencies = [ "url 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "encoding" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "encoding-index-japanese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)", + "encoding-index-korean 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)", + "encoding-index-simpchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)", + "encoding-index-singlebyte 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)", + "encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "encoding-index-japanese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "encoding-index-korean" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "encoding-index-simpchinese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "encoding-index-singlebyte" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "encoding-index-tradchinese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "encoding_index_tests" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "env_logger" version = "0.3.2" diff --git a/Cargo.toml b/Cargo.toml index a446b26..a481329 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["letitz"] [dependencies] byteorder = "^0.4.2" +encoding = "^0.2" env_logger = "^0.3.2" log = "^0.3.5" mio = "^0.5" diff --git a/src/main.rs b/src/main.rs index 997eb31..ee3b75b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ mod user; extern crate byteorder; extern crate core; extern crate crypto; +extern crate encoding; #[macro_use] extern crate log; extern crate env_logger; extern crate mio; diff --git a/src/proto/packet.rs b/src/proto/packet.rs index 00068b7..db7c52a 100644 --- a/src/proto/packet.rs +++ b/src/proto/packet.rs @@ -3,6 +3,8 @@ use std::iter::repeat; use std::io::{Read, Write}; use byteorder::{ByteOrder, LittleEndian, ReadBytesExt, WriteBytesExt}; +use encoding::{Encoding, DecoderTrap, EncoderTrap}; +use encoding::all::ISO_8859_1; use mio::{ Evented, EventLoop, EventSet, Handler, PollOpt, Token, TryRead, TryWrite }; @@ -42,8 +44,15 @@ impl Packet { // Writing convenience pub fn write_str(&mut self, string: &str) -> io::Result { - try!(self.write_uint(string.len() as u32)); - let n = try!(self.write(string.as_bytes())); + let bytes = match ISO_8859_1.encode(string, EncoderTrap::Strict) { + Ok(bytes) => bytes, + Err(_) => { + let copy = string.to_string(); + return Err(io::Error::new(io::ErrorKind::Other, copy)) + } + }; + try!(self.write_uint(bytes.len() as u32)); + let n = try!(self.write(&bytes)); Ok(n + U32_SIZE) } @@ -68,8 +77,7 @@ impl Packet { let len = try!(self.read_uint()) as usize; let mut buffer = vec![0; len]; try!(self.read(&mut buffer)); - let result = String::from_utf8(buffer); - match result { + match ISO_8859_1.decode(&buffer, DecoderTrap::Strict) { Ok(string) => Ok(string), Err(e) => Err(io::Error::new(io::ErrorKind::Other, e.to_string())), }