rsleigh-decompile 0.4.2

P-code decompiler — turns rsleigh P-code IR into C-like pseudocode
//! POSIX / C standard library function signatures.

use crate::signatures::*;

pub static LIBC_SIGNATURES: &[FuncSig] = crate::define_signatures! {
    // stdio
    fn printf(format: ConstCharPtr, ...) -> Int;
    fn fprintf(stream: FilePtr, format: ConstCharPtr, ...) -> Int;
    fn sprintf(buf: CharPtr, format: ConstCharPtr, ...) -> Int;
    fn snprintf(buf: CharPtr, size: SizeT, format: ConstCharPtr, ...) -> Int;
    fn puts(s: ConstCharPtr) -> Int;
    fn fputs(s: ConstCharPtr, stream: FilePtr) -> Int;
    fn fgets(s: CharPtr, size: Int, stream: FilePtr) -> CharPtr;
    fn fread(ptr: VoidPtr, size: SizeT, nmemb: SizeT, stream: FilePtr) -> SizeT;
    fn fwrite(ptr: ConstVoidPtr, size: SizeT, nmemb: SizeT, stream: FilePtr) -> SizeT;
    fn fopen(path: ConstCharPtr, mode: ConstCharPtr) -> FilePtr;
    fn fclose(stream: FilePtr) -> Int;
    fn fseek(stream: FilePtr, offset: Long, whence: Int) -> Int;
    fn ftell(stream: FilePtr) -> Long;
    fn feof(stream: FilePtr) -> Int;
    fn ferror(stream: FilePtr) -> Int;
    fn fflush(stream: FilePtr) -> Int;
    fn fputc(c: Int, stream: FilePtr) -> Int;
    fn fgetc(stream: FilePtr) -> Int;
    fn putchar(c: Int) -> Int;
    fn getchar() -> Int;
    fn fprintf_stderr(format: ConstCharPtr, ...) -> Int;
    fn wprintf(format: ConstWCharPtr, ...) -> Int;
    fn fwprintf(stream: FilePtr, format: ConstWCharPtr, ...) -> Int;
    fn fwprintf_stderr(format: ConstWCharPtr, ...) -> Int;
    fn scanf(format: ConstCharPtr, ...) -> Int;
    fn fscanf(stream: FilePtr, format: ConstCharPtr, ...) -> Int;

    // stdlib
    fn malloc(size: SizeT) -> VoidPtr;
    fn calloc(nmemb: SizeT, size: SizeT) -> VoidPtr;
    fn realloc(ptr: VoidPtr, size: SizeT) -> VoidPtr;
    fn free(ptr: VoidPtr);
    fn atoi(s: ConstCharPtr) -> Int;
    fn atol(s: ConstCharPtr) -> Long;
    fn strtol(s: ConstCharPtr, endptr: CharPtr, base: Int) -> Long;
    fn strtoul(s: ConstCharPtr, endptr: CharPtr, base: Int) -> ULong;
    fn exit(status: Int);
    fn abort();
    fn abs(x: Int) -> Int;
    fn qsort(base: VoidPtr, nmemb: SizeT, size: SizeT, compar: VoidPtr);

    // string
    fn strlen(s: ConstCharPtr) -> SizeT;
    fn strcpy(dest: CharPtr, src: ConstCharPtr) -> CharPtr;
    fn strncpy(dest: CharPtr, src: ConstCharPtr, n: SizeT) -> CharPtr;
    fn strcmp(s1: ConstCharPtr, s2: ConstCharPtr) -> Int;
    fn strncmp(s1: ConstCharPtr, s2: ConstCharPtr, n: SizeT) -> Int;
    fn strcat(dest: CharPtr, src: ConstCharPtr) -> CharPtr;
    fn strncat(dest: CharPtr, src: ConstCharPtr, n: SizeT) -> CharPtr;
    fn strchr(s: ConstCharPtr, c: Int) -> CharPtr;
    fn strrchr(s: ConstCharPtr, c: Int) -> CharPtr;
    fn strstr(haystack: ConstCharPtr, needle: ConstCharPtr) -> CharPtr;
    fn memcpy(dest: VoidPtr, src: ConstVoidPtr, n: SizeT) -> VoidPtr;
    fn memset(s: VoidPtr, c: Int, n: SizeT) -> VoidPtr;
    fn memmove(dest: VoidPtr, src: ConstVoidPtr, n: SizeT) -> VoidPtr;
    fn memcmp(s1: ConstVoidPtr, s2: ConstVoidPtr, n: SizeT) -> Int;
    fn strerror(errnum: Int) -> CharPtr;

    // unistd / posix
    fn read(fd: Fd, buf: VoidPtr, count: SizeT) -> Long;
    fn write(fd: Fd, buf: ConstVoidPtr, count: SizeT) -> Long;
    fn open(path: ConstCharPtr, flags: Int, ...) -> Fd;
    fn close(fd: Fd) -> Int;
    fn fork() -> Int;
    fn execve(path: ConstCharPtr, argv: VoidPtr, envp: VoidPtr) -> Int;
    fn getpid() -> Int;
    fn sleep(seconds: UInt) -> UInt;
    fn dup2(oldfd: Fd, newfd: Fd) -> Int;
    fn pipe(pipefd: VoidPtr) -> Int;

    // socket
    fn socket(domain: Int, ty: Int, protocol: Int) -> SockFd;
    fn bind(sockfd: SockFd, addr: ConstVoidPtr, addrlen: UInt) -> Int;
    fn listen(sockfd: SockFd, backlog: Int) -> Int;
    fn accept(sockfd: SockFd, addr: VoidPtr, addrlen: VoidPtr) -> SockFd;
    fn connect(sockfd: SockFd, addr: ConstVoidPtr, addrlen: UInt) -> Int;
    fn send(sockfd: SockFd, buf: ConstVoidPtr, len: SizeT, flags: Int) -> Long;
    fn recv(sockfd: SockFd, buf: VoidPtr, len: SizeT, flags: Int) -> Long;
    fn sendto(sockfd: SockFd, buf: ConstVoidPtr, len: SizeT, flags: Int, dest_addr: ConstVoidPtr, addrlen: UInt) -> Long;
    fn recvfrom(sockfd: SockFd, buf: VoidPtr, len: SizeT, flags: Int, src_addr: VoidPtr, addrlen: VoidPtr) -> Long;
    fn setsockopt(sockfd: SockFd, level: Int, optname: Int, optval: ConstVoidPtr, optlen: UInt) -> Int;
    fn getsockopt(sockfd: SockFd, level: Int, optname: Int, optval: VoidPtr, optlen: VoidPtr) -> Int;
    fn shutdown(sockfd: SockFd, how: Int) -> Int;

    // === Ghidra-imported: additional POSIX/libc (from generic_clib_64.gdt) ===
    fn _exit(status: Int);
    fn access(name: CharPtr, type_: Int) -> Int;
    fn alarm(seconds: UInt) -> UInt;
    fn atexit(func: VoidPtr) -> Int;
    fn atof(nptr: CharPtr) -> Int;
    fn bsearch(key: VoidPtr, base: VoidPtr, nmemb: ULong, size: ULong, compar: VoidPtr) -> VoidPtr;
    fn chmod(file: CharPtr, mode: UInt) -> Int;
    fn chown(file: CharPtr, owner: UInt, group: UInt) -> Int;
    fn closedir(dirp: VoidPtr) -> Int;
    fn difftime(time1: Long, time0: Long) -> Int;
    fn dlclose(handle: VoidPtr) -> Int;
    fn dlerror() -> CharPtr;
    fn dlopen(file: CharPtr, mode: Int) -> VoidPtr;
    fn dlsym(handle: VoidPtr, name: CharPtr) -> VoidPtr;
    fn dup(fd: Int) -> Int;
    fn execl(path: CharPtr, arg: CharPtr, ...) -> Int;
    fn execvp(file: CharPtr, argv: VoidPtr) -> Int;
    fn fcntl(fd: Int, cmd: Int, ...) -> Int;
    fn fstat(fd: Int, buf: VoidPtr) -> Int;
    fn getaddrinfo(name: CharPtr, service: CharPtr, req: VoidPtr, pai: VoidPtr) -> Int;
    fn freeaddrinfo(ai: VoidPtr);
    fn getenv(name: CharPtr) -> CharPtr;
    fn getnameinfo(sa: VoidPtr, salen: UInt, host: CharPtr, hostlen: UInt, serv: CharPtr, servlen: UInt, flags: Int) -> Int;
    fn getppid() -> Int;
    fn gettimeofday(tv: VoidPtr, tz: VoidPtr) -> Int;
    fn gmtime(timer: VoidPtr) -> VoidPtr;
    fn htonl(hostlong: UInt) -> UInt;
    fn htons(hostshort: UInt) -> UInt;
    fn inet_addr(cp: CharPtr) -> UInt;
    fn inet_ntoa(in_: UInt) -> CharPtr;
    fn inet_ntop(af: Int, src: VoidPtr, dst: CharPtr, size: UInt) -> CharPtr;
    fn inet_pton(af: Int, src: CharPtr, dst: VoidPtr) -> Int;
    fn ioctl(fd: Int, request: ULong, ...) -> Int;
    fn isalnum(c: Int) -> Int;
    fn isalpha(c: Int) -> Int;
    fn isdigit(c: Int) -> Int;
    fn ispunct(c: Int) -> Int;
    fn isspace(c: Int) -> Int;
    fn kill(pid: Int, sig: Int) -> Int;
    fn labs(j: Long) -> Long;
    fn link(from: CharPtr, to: CharPtr) -> Int;
    fn localtime(timer: VoidPtr) -> VoidPtr;
    fn longjmp(env: VoidPtr, val: Int);
    fn lseek(fd: Int, offset: Long, whence: Int) -> Long;
    fn lstat(file: CharPtr, buf: VoidPtr) -> Int;
    fn memchr(s: VoidPtr, c: Int, n: ULong) -> VoidPtr;
    fn mkdir(path: CharPtr, mode: UInt) -> Int;
    fn mktime(tp: VoidPtr) -> Long;
    fn mmap(addr: VoidPtr, len: ULong, prot: Int, flags: Int, fd: Int, offset: Long) -> VoidPtr;
    fn mprotect(addr: VoidPtr, len: ULong, prot: Int) -> Int;
    fn munmap(addr: VoidPtr, len: ULong) -> Int;
    fn nanosleep(req: VoidPtr, rem: VoidPtr) -> Int;
    fn ntohs(netshort: UInt) -> UInt;
    fn ntohl(netlong: UInt) -> UInt;
    fn opendir(name: CharPtr) -> VoidPtr;
    fn perror(s: CharPtr);
    fn poll(fds: VoidPtr, nfds: ULong, timeout: Int) -> Int;
    fn pthread_cond_signal(cond: VoidPtr) -> Int;
    fn pthread_cond_wait(cond: VoidPtr, mutex: VoidPtr) -> Int;
    fn pthread_create(newthread: VoidPtr, attr: VoidPtr, start_routine: VoidPtr, arg: VoidPtr) -> Int;
    fn pthread_exit(retval: VoidPtr);
    fn pthread_join(th: ULong, thread_return: VoidPtr) -> Int;
    fn pthread_mutex_destroy(mutex: VoidPtr) -> Int;
    fn pthread_mutex_init(mutex: VoidPtr, mutexattr: VoidPtr) -> Int;
    fn pthread_mutex_lock(mutex: VoidPtr) -> Int;
    fn pthread_mutex_unlock(mutex: VoidPtr) -> Int;
    fn raise(sig: Int) -> Int;
    fn rand() -> Int;
    fn random() -> Long;
    fn readdir(dirp: VoidPtr) -> VoidPtr;
    fn readlink(path: CharPtr, buf: CharPtr, len: ULong) -> Long;
    fn rename(old: CharPtr, new_: CharPtr) -> Int;
    fn rmdir(path: CharPtr) -> Int;
    fn select(nfds: Int, readfds: VoidPtr, writefds: VoidPtr, exceptfds: VoidPtr, timeout: VoidPtr) -> Int;
    fn setenv(name: CharPtr, value: CharPtr, replace: Int) -> Int;
    fn setjmp(env: VoidPtr) -> Int;
    fn sigaction(sig: Int, act: VoidPtr, oact: VoidPtr) -> Int;
    fn signal(sig: Int, handler: VoidPtr) -> VoidPtr;
    fn sigprocmask(how: Int, set: VoidPtr, oset: VoidPtr) -> Int;
    fn srand(seed: UInt);
    fn srandom(seed: UInt);
    fn sscanf(s: CharPtr, format: CharPtr, ...) -> Int;
    fn stat(file: CharPtr, buf: VoidPtr) -> Int;
    fn strdup(s: CharPtr) -> CharPtr;
    fn strftime(s: CharPtr, maxsize: ULong, format: CharPtr, tp: VoidPtr) -> ULong;
    fn strndup(string: CharPtr, n: ULong) -> CharPtr;
    fn strtod(nptr: CharPtr, endptr: VoidPtr) -> Int;
    fn strtok(s: CharPtr, delim: CharPtr) -> CharPtr;
    fn strtok_r(s: CharPtr, delim: CharPtr, save_ptr: VoidPtr) -> CharPtr;
    fn strtoll(nptr: CharPtr, endptr: VoidPtr, base: Int) -> Long;
    fn strtoull(nptr: CharPtr, endptr: VoidPtr, base: Int) -> ULong;
    fn symlink(from: CharPtr, to: CharPtr) -> Int;
    fn system(command: CharPtr) -> Int;
    fn time(timer: VoidPtr) -> Long;
    fn tolower(c: Int) -> Int;
    fn toupper(c: Int) -> Int;
    fn unlink(name: CharPtr) -> Int;
    fn unsetenv(name: CharPtr) -> Int;
    fn usleep(useconds: UInt) -> Int;
    fn wait(stat_loc: VoidPtr) -> Int;
    fn waitpid(pid: Int, stat_loc: VoidPtr, options: Int) -> Int;
};