Nicolas Winkler 6 年 前
コミット
ba59e8af5c
3 ファイル変更34 行追加9 行削除
  1. 6 0
      Cargo.toml
  2. 13 1
      src/hex.rs
  3. 15 8
      src/main.rs

+ 6 - 0
Cargo.toml

@@ -5,3 +5,9 @@ authors = ["Nicolas Winkler <nicolas.winkler@gmx.ch>"]
 edition = "2018"
 
 [dependencies]
+
+libc = "*"
+
+[profile.release]
+lto = true
+panic = 'abort'

+ 13 - 1
src/hex.rs

@@ -13,7 +13,7 @@ pub fn hex_pair(x: u8) -> (char, char) {
     (get_hex((x & 0xF0) >> 4), get_hex(x & 0x0F))
 }
 
-pub fn to_line(data: &[u8]) -> String {
+/*pub fn to_line(data: &[u8]) -> String {
     let mut out = String::new();
     for byte in data {
         let pair = hex_pair(*byte);
@@ -21,5 +21,17 @@ pub fn to_line(data: &[u8]) -> String {
         out.push(pair.1);
     }
     out
+}*/
+
+
+pub fn create_line(data: &[u8], line: &mut [u8], length: usize, offset: usize) {
+    let mut i = 0;
+    for byte in 0..length {
+        let pair = hex_pair(*byte);
+        line[i] = pair.0 as u8;
+        line[i + 1] = pair.1 as u8;
+        i += 2;
+    }
 }
 
+

+ 15 - 8
src/main.rs

@@ -1,15 +1,22 @@
-use std::io::{self, Read};
+#![feature(lang_items)]
+#![feature(start)]
+#![feature(no_std)]
+#![no_std]
+
+extern crate libc;
 
 pub mod hex;
 
-fn main() {
-    let stdin = std::io::stdin();
-    let mut input = stdin.lock();
+#[start]
+fn start(argc: isize, argv: *const *const u8) -> isize {
+    let mut buffer: [u8; 64] = [0; 64];
+    let mut out: [u8; 129] = [0; 129];
+    out[128] = 0;
+    loop {
+        let read = unsafe{ libc::read(libc::STDIN_FILENO, buffer.as_mut_ptr() as _, 64) }; //input.read(&mut buffer);
 
-    while true {
-        let mut buffer: [u8; 64] = [0; 64];
-        let read = input.read(&mut buffer);
-        println!("{}", hex::to_line(&buffer));
+        hex::create_line(&buffer, &mut out, 0);
+        unsafe { libc::puts(out.as_ptr() as *const _); }
     }
 }