Browse Source

Decode and encode strings as ISO-8859-1, not UTF-8.

wip
Titouan Rigoudy 9 years ago
parent
commit
0ee0acc2fb
4 changed files with 72 additions and 4 deletions
  1. +58
    -0
      Cargo.lock
  2. +1
    -0
      Cargo.toml
  3. +1
    -0
      src/main.rs
  4. +12
    -4
      src/proto/packet.rs

+ 58
- 0
Cargo.lock View File

@ -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"


+ 1
- 0
Cargo.toml View File

@ -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"


+ 1
- 0
src/main.rs View File

@ -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;


+ 12
- 4
src/proto/packet.rs View File

@ -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<usize> {
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())),
}


Loading…
Cancel
Save