From be04cdcaa1dd6726992d13d576be7e29fb8ced24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 29 Jun 2020 23:58:17 +0200 Subject: [PATCH] =?UTF-8?q?pkstring:=20na=C3=AFve=20implementation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- Cargo.toml | 1 + pkstrings/Cargo.toml | 9 +++ pkstrings/src/lib.rs | 137 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 pkstrings/Cargo.toml create mode 100644 pkstrings/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index a39d16c..1d011db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,6 @@ license = "AGPL-3.0-or-later" [workspace] members = [ + "pkstrings", "savestate", ] diff --git a/pkstrings/Cargo.toml b/pkstrings/Cargo.toml new file mode 100644 index 0000000..dcbc9a7 --- /dev/null +++ b/pkstrings/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "pkstrings" +version = "0.1.0" +authors = ["Maxime “pep” Buquet "] +edition = "2018" +license = "AGPL-3.0-or-later" + +[dependencies] +lazy_static = "1.4" diff --git a/pkstrings/src/lib.rs b/pkstrings/src/lib.rs new file mode 100644 index 0000000..d29f0a5 --- /dev/null +++ b/pkstrings/src/lib.rs @@ -0,0 +1,137 @@ +// Copyright (C) 2020 "Maxime “pep” Buquet " +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at your +// option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License +// for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use std::ops::Deref; +use std::convert::TryFrom; +use std::collections::HashMap; +use lazy_static::lazy_static; + +lazy_static! { + static ref STRTOHEX: HashMap = { + let tmp = [ + ('A', 0x80), ('B', 0x81), ('C', 0x82), ('D', 0x83), + ('E', 0x84), ('F', 0x85), ('G', 0x86), ('H', 0x87), + ('I', 0x88), ('J', 0x89), ('K', 0x8a), ('L', 0x8b), + ('M', 0x8c), ('N', 0x8d), ('O', 0x8e), ('P', 0x8f), + ('Q', 0x90), ('R', 0x91), ('S', 0x92), ('T', 0x93), + ('U', 0x94), ('V', 0x95), ('W', 0x96), ('X', 0x97), + ('Y', 0x98), ('Z', 0x99), ('(', 0x9a), (')', 0x9b), + (':', 0x9c), (';', 0x9d), ('[', 0x9e), (']', 0x9f), + ('a', 0xa0), ('b', 0xa1), ('c', 0xa2), ('d', 0xa3), + ('e', 0xa4), ('f', 0xa5), ('g', 0xa6), ('h', 0xa7), + ('i', 0xa8), ('j', 0xa9), ('k', 0xaa), ('l', 0xab), + ('m', 0xac), ('n', 0xad), ('o', 0xae), ('p', 0xaf), + ('q', 0xb0), ('r', 0xb1), ('s', 0xb2), ('t', 0xb3), + ('u', 0xb4), ('v', 0xb5), ('w', 0xb6), ('x', 0xb7), + ('y', 0xb8), ('z', 0xb9), ('\'', 0xe0), ('-', 0xe3), + ('?', 0xe6), ('!', 0xe7), ('.', 0xe8), ('▷', 0xec), + ('▶', 0xed), ('▼', 0xee), ('♂', 0xef), ('×', 0xf1), + ('.', 0xf2), ('/', 0xf3), (',', 0xf4), ('♀', 0xf5), + ('0', 0xf6), ('1', 0xf7), ('2', 0xf8), ('3', 0xf9), + ('4', 0xfa), ('5', 0xfb), ('6', 0xfc), ('7', 0xfd), + ('8', 0xfe), ('9', 0xff), (' ', 0x7f), ('@', 0x50), + ]; + + let mut map = HashMap::new(); + for (k, v) in tmp.iter() { + map.insert(*k, *v); + } + map + }; + + static ref HEXTOSTR: HashMap = { + let tmp = [ + (0x80, 'A'), (0x81, 'B'), (0x82, 'C'), (0x83, 'D'), + (0x84, 'E'), (0x85, 'F'), (0x86, 'G'), (0x87, 'H'), + (0x88, 'I'), (0x89, 'J'), (0x8a, 'K'), (0x8b, 'L'), + (0x8c, 'M'), (0x8d, 'N'), (0x8e, 'O'), (0x8f, 'P'), + (0x90, 'Q'), (0x91, 'R'), (0x92, 'S'), (0x93, 'T'), + (0x94, 'U'), (0x95, 'V'), (0x96, 'W'), (0x97, 'X'), + (0x98, 'Y'), (0x99, 'Z'), (0x9a, '('), (0x9b, ')'), + (0x9c, ':'), (0x9d, ';'), (0x9e, '['), (0x9f, ']'), + (0xa0, 'a'), (0xa1, 'b'), (0xa2, 'c'), (0xa3, 'd'), + (0xa4, 'e'), (0xa5, 'f'), (0xa6, 'g'), (0xa7, 'h'), + (0xa8, 'i'), (0xa9, 'j'), (0xaa, 'k'), (0xab, 'l'), + (0xac, 'm'), (0xad, 'n'), (0xae, 'o'), (0xaf, 'p'), + (0xb0, 'q'), (0xb1, 'r'), (0xb2, 's'), (0xb3, 't'), + (0xb4, 'u'), (0xb5, 'v'), (0xb6, 'w'), (0xb7, 'x'), + (0xb8, 'y'), (0xb9, 'z'), (0xe0, '\''), (0xe3, '-'), + (0xe6, '?'), (0xe7, '!'), (0xe8, '.'), (0xec, '▷'), + (0xed, '▶'), (0xee, '▼'), (0xef, '♂'), (0xf1, '×'), + (0xf2, '.'), (0xf3, '/'), (0xf4, ','), (0xf5, '♀'), + (0xf6, '0'), (0xf7, '1'), (0xf8, '2'), (0xf9, '3'), + (0xfa, '4'), (0xfb, '5'), (0xfc, '6'), (0xfd, '7'), + (0xfe, '8'), (0xff, '9'), (0x7f, ' '), (0x50, '@'), + ]; + + let mut map = HashMap::new(); + for (k, v) in tmp.iter() { + map.insert(*k, *v); + } + map + }; +} + +#[derive(Debug, Eq, PartialEq)] +pub enum Error { + InvalidCharacter, +} + +#[derive(Debug, Eq, PartialEq)] +pub struct PKString(String); + +impl Deref for PKString { + type Target = String; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl TryFrom<&[u8]> for PKString { + type Error = Error; + + fn try_from(data: &[u8]) -> Result { + let mut buf = String::with_capacity(data.len()); + let placeholder = '_'; + + for ord in data { + if let Some(chr) = HEXTOSTR.get(&ord) { + buf.push(*chr); + } else { + buf.push(placeholder); + } + } + + Ok(PKString(buf)) + } +} + +impl Into> for PKString { + fn into(self) -> Vec { + let mut vec = Vec::with_capacity(self.len()); + + for chr in self.0.chars() { + if let Some(ord) = STRTOHEX.get(&chr) { + vec.push(*ord); + } else { + // TODO: Change this. + panic!(); + } + } + + vec + } +}