Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 6, 2024 05:03
Show Gist options
  • Save rust-play/92af6fefec0ea8bcc76a5b18be40005e to your computer and use it in GitHub Desktop.
Save rust-play/92af6fefec0ea8bcc76a5b18be40005e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
#![register_tool(c2rust)]
#![feature(register_tool)]
extern "C" {
static mut stdin: *mut _IO_FILE;
static mut stdout: *mut _IO_FILE;
fn fflush(__stream: *mut FILE) -> libc::c_int;
fn fread(
__ptr: *mut libc::c_void,
__size: size_t,
__n: size_t,
__stream: *mut FILE,
) -> size_t;
fn fwrite(
__ptr: *const libc::c_void,
__size: size_t,
__n: size_t,
__s: *mut FILE,
) -> size_t;
fn calloc(_: libc::c_ulong, _: libc::c_ulong) -> *mut libc::c_void;
fn free(__ptr: *mut libc::c_void);
fn strlen(_: *const libc::c_char) -> libc::c_ulong;
}
pub type uint8_t = libc::c_uchar;
pub type uint32_t = libc::c_uint;
pub type size_t = libc::c_ulong;
pub type __off_t = libc::c_long;
pub type __off64_t = libc::c_long;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _IO_FILE {
pub _flags: libc::c_int,
pub _IO_read_ptr: *mut libc::c_char,
pub _IO_read_end: *mut libc::c_char,
pub _IO_read_base: *mut libc::c_char,
pub _IO_write_base: *mut libc::c_char,
pub _IO_write_ptr: *mut libc::c_char,
pub _IO_write_end: *mut libc::c_char,
pub _IO_buf_base: *mut libc::c_char,
pub _IO_buf_end: *mut libc::c_char,
pub _IO_save_base: *mut libc::c_char,
pub _IO_backup_base: *mut libc::c_char,
pub _IO_save_end: *mut libc::c_char,
pub _markers: *mut _IO_marker,
pub _chain: *mut _IO_FILE,
pub _fileno: libc::c_int,
pub _flags2: libc::c_int,
pub _old_offset: __off_t,
pub _cur_column: libc::c_ushort,
pub _vtable_offset: libc::c_schar,
pub _shortbuf: [libc::c_char; 1],
pub _lock: *mut libc::c_void,
pub _offset: __off64_t,
pub __pad1: *mut libc::c_void,
pub __pad2: *mut libc::c_void,
pub __pad3: *mut libc::c_void,
pub __pad4: *mut libc::c_void,
pub __pad5: size_t,
pub _mode: libc::c_int,
pub _unused2: [libc::c_char; 20],
}
pub type _IO_lock_t = ();
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _IO_marker {
pub _next: *mut _IO_marker,
pub _sbuf: *mut _IO_FILE,
pub _pos: libc::c_int,
}
pub type FILE = _IO_FILE;
#[no_mangle]
pub unsafe extern "C" fn getMessage(mut inputLength: *mut size_t) -> *mut uint8_t {
let mut messageLength: uint32_t = 0 as libc::c_int as uint32_t;
let mut result: size_t = fread(
&mut messageLength as *mut uint32_t as *mut libc::c_void,
::std::mem::size_of::<uint32_t>() as libc::c_ulong,
1 as libc::c_int as size_t,
stdin,
);
let mut message: *mut uint8_t = calloc(
messageLength.wrapping_add(1 as libc::c_int as libc::c_uint) as libc::c_ulong,
::std::mem::size_of::<uint8_t>() as libc::c_ulong,
) as *mut uint8_t;
result = fread(
message as *mut libc::c_void,
::std::mem::size_of::<uint8_t>() as libc::c_ulong,
messageLength as size_t,
stdin,
);
*inputLength = messageLength as size_t;
return message;
}
#[no_mangle]
pub unsafe extern "C" fn sendMessage(mut response: *mut uint8_t) {
let responseLength: uint32_t = strlen(response as *const libc::c_char) as uint32_t;
fwrite(
&responseLength as *const uint32_t as *const libc::c_void,
::std::mem::size_of::<uint32_t>() as libc::c_ulong,
1 as libc::c_int as size_t,
stdout,
);
fwrite(
response as *const libc::c_void,
responseLength as size_t,
1 as libc::c_int as size_t,
stdout,
);
fflush(stdout);
}
unsafe fn main_0() -> libc::c_int {
loop {
let mut messageLength: size_t = 0 as libc::c_int as size_t;
let message: *mut uint8_t = getMessage(&mut messageLength);
sendMessage(message);
free(message as *mut libc::c_void);
};
}
pub fn main() {
unsafe { ::std::process::exit(main_0() as i32) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment