You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

422 lines
10 KiB

6 years ago
  1. #![feature(const_generics)]
  2. pub mod crypto;
  3. pub mod packet;
  4. pub mod character;
  5. pub mod util;
  6. use std::io::Read;
  7. #[derive(Debug, PartialEq)]
  8. pub enum PacketParseError {
  9. NotEnoughBytes,
  10. WrongPacketCommand,
  11. WrongPacketForServerType(u16),
  12. WrongPacketSize(u16, usize),
  13. DataStructNotLargeEnough(u64, usize),
  14. InvalidValue,
  15. ReadError,
  16. }
  17. trait PSOPacketData {
  18. //fn size(&self) -> usize;
  19. fn from_bytes<R: Read>(cursor: &mut R) -> Result<Self, PacketParseError> where Self: Sized;
  20. fn as_bytes(&self) -> Vec<u8>;
  21. }
  22. impl PSOPacketData for u8 {
  23. fn from_bytes<R: Read>(cursor: &mut R) -> Result<u8, PacketParseError> {
  24. let mut bytes = [0u8; 1];
  25. let len = cursor.read(&mut bytes).map_err(|_| PacketParseError::ReadError)?;
  26. if len == 1 {
  27. Ok(bytes[0])
  28. } else{
  29. Err(PacketParseError::NotEnoughBytes)
  30. }
  31. }
  32. fn as_bytes(&self) -> Vec<u8> {
  33. vec![*self]
  34. }
  35. }
  36. impl PSOPacketData for u32 {
  37. fn from_bytes<R: Read>(cursor: &mut R) -> Result<u32, PacketParseError> {
  38. let mut bytes = [0u8; 4];
  39. let len = cursor.read(&mut bytes).map_err(|_| PacketParseError::ReadError)?;
  40. if len == 4 {
  41. Ok(u32::from_le_bytes(bytes))
  42. }
  43. else {
  44. Err(PacketParseError::NotEnoughBytes)
  45. }
  46. }
  47. fn as_bytes(&self) -> Vec<u8> {
  48. u32::to_le_bytes(*self).to_vec()
  49. }
  50. }
  51. impl PSOPacketData for u16 {
  52. fn from_bytes<R: Read>(cursor: &mut R) -> Result<u16, PacketParseError> {
  53. let mut bytes = [0u8; 2];
  54. let len = cursor.read(&mut bytes).map_err(|_| PacketParseError::ReadError)?;
  55. if len == 2 {
  56. Ok(u16::from_le_bytes(bytes))
  57. }
  58. else {
  59. Err(PacketParseError::NotEnoughBytes)
  60. }
  61. }
  62. fn as_bytes(&self) -> Vec<u8> {
  63. u16::to_le_bytes(*self).to_vec()
  64. }
  65. }
  66. impl PSOPacketData for String {
  67. fn from_bytes<R: Read>(cursor: &mut R) -> Result<String, PacketParseError> {
  68. let mut s: Vec<u8> = Vec::new();
  69. cursor.read_to_end(&mut s).map_err(|_| PacketParseError::ReadError)?;
  70. let mut utf16 = Vec::new();
  71. for c in s.chunks(2) {
  72. utf16.push(u16::from_le_bytes([c[0], c[1]]));
  73. }
  74. Ok(String::from_utf16_lossy(utf16.as_slice()))
  75. }
  76. fn as_bytes(&self) -> Vec<u8> {
  77. let mut buf = Vec::new();
  78. for c in self.as_str().encode_utf16() {
  79. buf.extend_from_slice(&c.to_le_bytes());
  80. }
  81. buf
  82. }
  83. }
  84. pub trait PSOPacket: std::fmt::Debug {
  85. // const CMD: u16;
  86. fn from_bytes(data: &[u8]) -> Result<Self, PacketParseError> where Self: Sized;
  87. fn as_bytes(&self) -> Vec<u8>;
  88. }
  89. #[cfg(test)]
  90. mod test {
  91. use super::*;
  92. use psopacket::{pso_packet, PSOPacketData};
  93. #[test]
  94. fn test_basic_pso_packet() {
  95. #[pso_packet(0x23)]
  96. struct Test {
  97. a: u32,
  98. b: u16,
  99. c: u16,
  100. }
  101. let test = Test {
  102. a: 123456789,
  103. b: 54321,
  104. c: 9999,
  105. };
  106. let mut bytes = test.as_bytes();
  107. assert!(bytes == vec![16, 0, 35, 0, 0, 0, 0, 0, 21, 205, 91, 7, 49, 212, 15, 39]);
  108. bytes[11] = 17;
  109. let test2 = Test::from_bytes(&bytes).unwrap();
  110. assert!(test2 == Test {
  111. a: 291228949,
  112. b: 54321,
  113. c: 9999,
  114. });
  115. }
  116. #[test]
  117. fn test_array_in_packet() {
  118. #[pso_packet(0x23)]
  119. struct Test {
  120. a: u32,
  121. b: u16,
  122. c: [u16; 3],
  123. }
  124. let test = Test {
  125. a: 123456789,
  126. b: 54321,
  127. c: [1111, 2222, 3333],
  128. };
  129. let mut bytes = test.as_bytes();
  130. assert!(bytes == [20, 0, 35, 0, 0, 0, 0, 0, 21, 205, 91, 7, 49, 212, 87, 4, 174, 8, 5, 13]);
  131. bytes[17] = 17;
  132. let test2 = Test::from_bytes(&bytes).unwrap();
  133. assert!(test2 == Test {
  134. a: 123456789,
  135. b: 54321,
  136. c: [1111, 4526, 3333],
  137. });
  138. }
  139. #[test]
  140. fn test_custom_type_in_packet() {
  141. #[derive(Clone, Debug, PartialEq)]
  142. pub struct MyType {
  143. k: u8,
  144. j: u32,
  145. }
  146. impl PSOPacketData for MyType {
  147. fn from_bytes<R: std::io::Read>(cursor: &mut R) -> Result<MyType, PacketParseError> {
  148. let mut kb = [0u8; 1];
  149. cursor.read(&mut kb).map_err(|_| PacketParseError::ReadError)?;
  150. let mut jb = [0u8; 4];
  151. cursor.read(&mut jb).map_err(|_| PacketParseError::ReadError)?;
  152. Ok(MyType {
  153. k: kb[0],
  154. j: u32::from_le_bytes(jb)
  155. })
  156. }
  157. fn as_bytes(&self) -> Vec<u8> {
  158. let jbytes = u32::to_le_bytes(self.j);
  159. vec![self.k, jbytes[0], jbytes[1], jbytes[2], jbytes[3]]
  160. }
  161. }
  162. #[pso_packet(0x23)]
  163. struct Test {
  164. a: u32,
  165. b: u16,
  166. c: MyType,
  167. d: u8,
  168. }
  169. let test = Test {
  170. a: 123456789,
  171. b: 54321,
  172. c: MyType {
  173. k: 23,
  174. j: 999999,
  175. },
  176. d: 5,
  177. };
  178. let mut bytes = test.as_bytes();
  179. assert!(bytes == [20, 0, 35, 0, 0, 0, 0, 0, 21, 205, 91, 7, 49, 212, 23, 63, 66, 15, 0, 5]);
  180. bytes[17] = 17;
  181. let test2 = Test::from_bytes(&bytes).unwrap();
  182. assert!(test2 == Test {
  183. a: 123456789,
  184. b: 54321,
  185. c: MyType {
  186. k: 23,
  187. j: 1131071,
  188. },
  189. d: 5,
  190. });
  191. }
  192. #[test]
  193. fn test_string_in_packet() {
  194. #[pso_packet(0x23)]
  195. struct Test {
  196. a: u32,
  197. b: u16,
  198. s: String,
  199. }
  200. let test = Test {
  201. a: 123456789,
  202. b: 54321,
  203. s: "asdf あえいおう".to_string(),
  204. };
  205. let mut bytes = test.as_bytes();
  206. assert!(bytes == vec![36, 0, 35, 0, 0, 0, 0, 0, 21, 205, 91, 7, 49, 212, 97, 0, 115, 0, 100, 0,
  207. 102, 0, 32, 0, 66, 48, 72, 48, 68, 48, 74, 48, 70, 48, 0, 0]);
  208. bytes[18] = 99;
  209. let test2 = Test::from_bytes(&bytes).unwrap();
  210. assert!(test2 == Test {
  211. a: 123456789,
  212. b: 54321,
  213. s: "ascf あえいおう\u{0}".to_string(),
  214. });
  215. }
  216. #[test]
  217. fn test_vec_in_packet() {
  218. #[pso_packet(0x23)]
  219. struct Test {
  220. a: u32,
  221. b: u16,
  222. v: Vec<u16>,
  223. }
  224. let test = Test {
  225. a: 123456789,
  226. b: 54321,
  227. v: vec![123,456,789],
  228. };
  229. let mut bytes = test.as_bytes();
  230. assert!(bytes == vec![20, 0, 35, 0, 3, 0, 0, 0, 21, 205, 91, 7, 49, 212, 123, 0, 200, 1, 21, 3]);
  231. bytes[18] = 99;
  232. let test2 = Test::from_bytes(&bytes).unwrap();
  233. assert!(test2 == Test {
  234. a: 123456789,
  235. b: 54321,
  236. v: vec![123,456,867],
  237. });
  238. }
  239. #[test]
  240. fn test_no_flag_packet() {
  241. #[pso_packet(0x23, no_flag)]
  242. struct Test {
  243. a: u32,
  244. b: u16,
  245. c: u16,
  246. }
  247. let test = Test {
  248. a: 123456789,
  249. b: 54321,
  250. c: 9999,
  251. };
  252. let mut bytes = test.as_bytes();
  253. assert!(bytes == vec![12, 0, 35, 0, 21, 205, 91, 7, 49, 212, 15, 39]);
  254. bytes[11] = 17;
  255. let test2 = Test::from_bytes(&bytes).unwrap();
  256. assert!(test2 == Test {
  257. a: 123456789,
  258. b: 54321,
  259. c: 4367,
  260. });
  261. }
  262. #[test]
  263. fn test_command_error() {
  264. #[pso_packet(0x23)]
  265. struct Test {
  266. a: u32,
  267. b: u16,
  268. #[nodebug]
  269. c: [u8; 0x2000],
  270. }
  271. let test = Test {
  272. a: 123456789,
  273. b: 54321,
  274. c: [0; 0x2000],
  275. };
  276. let mut bytes = test.as_bytes();
  277. bytes[2] = 17;
  278. let test2 = Test::from_bytes(&bytes);
  279. assert!(test2 == Err(PacketParseError::WrongPacketCommand));
  280. }
  281. #[test]
  282. fn test_derive_pso_data_packet() {
  283. #[derive(PSOPacketData, Debug, PartialEq)]
  284. struct Test {
  285. a: u8,
  286. b: u32,
  287. c: u16,
  288. }
  289. let test = Test {
  290. a: 12,
  291. b: 34567,
  292. c: 890,
  293. };
  294. let mut bytes = test.as_bytes();
  295. assert!(bytes == vec![12, 7, 135, 0, 0, 122, 3]);
  296. bytes[2] = 17;
  297. let mut cur = std::io::Cursor::new(bytes.clone());
  298. let test2 = Test::from_bytes(&mut cur).unwrap();
  299. assert!(test2 == Test {
  300. a: 12,
  301. b: 4359,
  302. c: 890
  303. });
  304. }
  305. #[test]
  306. fn test_derive_pso_data_packet_array() {
  307. #[derive(PSOPacketData, Debug, PartialEq)]
  308. struct Test {
  309. a: u8,
  310. b: u32,
  311. c: u16,
  312. d: [u8; 5],
  313. }
  314. let test = Test {
  315. a: 12,
  316. b: 34567,
  317. c: 890,
  318. d: [1,2,3,4,5],
  319. };
  320. let mut bytes = test.as_bytes();
  321. assert!(bytes == vec![12, 7, 135, 0, 0, 122, 3, 1, 2, 3, 4, 5]);
  322. bytes[10] = 17;
  323. let mut cur = std::io::Cursor::new(bytes.clone());
  324. let test2 = Test::from_bytes(&mut cur).unwrap();
  325. assert!(test2 == Test {
  326. a: 12,
  327. b: 34567,
  328. c: 890,
  329. d: [1,2,3,17,5]
  330. });
  331. }
  332. #[test]
  333. fn test_derive_pso_data_packet_not_enough_data() {
  334. #[derive(PSOPacketData, Debug, PartialEq)]
  335. struct Test {
  336. a: u8,
  337. b: u32,
  338. c: u16,
  339. }
  340. let test = Test {
  341. a: 12,
  342. b: 34567,
  343. c: 890,
  344. };
  345. let mut bytes = test.as_bytes();
  346. bytes.extend(test.as_bytes());
  347. let mut cur = std::io::Cursor::new(bytes.clone());
  348. let test2 = Test::from_bytes(&mut cur).unwrap();
  349. assert!(test2 == Test {
  350. a: 12,
  351. b: 34567,
  352. c: 890,
  353. });
  354. let bytes2 = (0..14).collect::<Vec<_>>();
  355. let mut cur = std::io::Cursor::new(bytes2);
  356. let test3 = Test::from_bytes(&mut cur);
  357. assert!(test3 == Ok(Test {
  358. a: 0,
  359. b: 67305985,
  360. c: 1541,
  361. }));
  362. let test4 = Test::from_bytes(&mut cur);
  363. assert!(test4 == Ok(Test {
  364. a: 7,
  365. b: 185207048,
  366. c: 3340,
  367. }));
  368. let test5 = Test::from_bytes(&mut cur);
  369. assert!(test5 == Err(PacketParseError::NotEnoughBytes));
  370. }
  371. }