Create C bindings for python automatically with the help of libclang

Overview

Python C Import

Dynamic library + header + ctypes = Module like object!

Create C bindings for python automatically with the help of libclang.

Examples

from c_import import loader
import ctypes

libc = loader.load("libc.so.6", ["stdio.h", "stdint.h", "math.h", "stdlib.h", "string.h", "time.h"])

# Work with file streams
filestream = libc.fopen(b"/tmp/x", b"rw+")
text = ctypes.create_string_buffer(b"Hello world")
libc.fwrite(text, len(text), 1, fstream)
libc.fflush(fstream)
libc.fclose(fstream)

libc.fileno(libc.stdin)
# 1

libc.fputc(ord('A'), libc.stdout)
libc.fflush(libc.stdout)
# A
libc.fprintf(libc.stderr, b"%d\n", 100) # 100

# Use math functions
libc.abs(-1)
# 1

# Create instances of structs
cal_date = libc.tm()
dir(cal_date)
# All fields are available
# [...,
#  'tm_gmtoff',
#  'tm_hour',
#  'tm_isdst',
#  'tm_mday',
#  'tm_min',
#  'tm_mon',
#  'tm_sec',
#  'tm_wday',
#  'tm_yday',
#  'tm_year',
#  'tm_zone']
cal_date.tm_sec = 1
cal_date.tm_year = 1999

# View the fields of "private" structs
# Great when experimenting inside an interactive shell.
dir(libc.stdout.contents)
# (Fields differ between libc implementations)
# [...,
#  '_IO_backup_base',
#  '_IO_buf_base',
#  '_IO_buf_end',
#  '_IO_read_base',
#  '_IO_read_end',
#  '_IO_read_ptr',
#  '_IO_save_base',
#  '_IO_save_end',
#  '_IO_write_base',
#  '_IO_write_end',
#  '_IO_write_ptr',
#  '__pad5',
#  '_chain',
#  '_codecvt',
#  '_cur_column',
#  '_fields_',
#  '_fileno',
#  '_flags',
#  '_flags2',
#  '_freeres_buf',
#  '_freeres_list',
#  '_lock',
#  '_markers',
#  '_mode',
#  '_objects',
#  '_offset',
#  '_old_offset',
#  '_shortbuf',
#  '_unused2',
#  '_vtable_offset',
#  '_wide_data']
libc.stdout.contents._fileno # 1

# Functions that return a struct work too
x = libc.div(49, 8)
x.quot # 6
x.rem  # 1
x.__class__.__name__ # 'div_t'

# Abort the process
libc.abort()

How does that work

The loader calls the c pre-processor to resolve any “include”s and “define”s.

The resulting header is processed by libclang

The header parser module converts the parsed header into ctype types.

The loader wraps symbols with their python ctype.

Features

Working

Basic C types

Functions

Pointers

Arrays

Structs

Unions

Enums

Typedefs

Globals

Bitfields

Forward declarations

Anonymous structs

Anonymous unions

Anonymous enums

Typedefs to anonymous types.

Nested anonymous types.

“Packed” attribute

Variadic arguments

Not implemented yet

Non cdecl functions (Important for Win32API)

Pre-processor constant integer expressions

Their declarations are not preserved after the pre-processing.

Vector types

I thought it was python, why am I seeing lisp?

Hy is sort of a python lisp hybrid. Its written like lisp, but its translated during runtime into python. It has interoperability with python, the fact that its written in hy is abstracted to the end user of the library.

https://docs.hylang.org/en/stable/whyhy.html

LICENSE

This library is licensed under GNU Lesser General Public License version 3 or later. See COPYING and COPYING.LESSER for further details.

iOS Snapchat parser for chats and cached files

ParseSnapchat iOS Snapchat parser for chats and cached files Tested on Windows and Linux install required libraries: pip install -r requirements.txt c

11 Dec 05, 2022
A module for account creation with python

A module for account creation with python

Fayas Noushad 3 Dec 01, 2021
Package that allows for validate and sanitize of string values.

py.validator A library of string validators and sanitizers Insipired by validator.js Strings only This library validates and sanitizes strings only. P

Sanel Hadzini 22 Nov 08, 2022
Give you a better view of your Docker registry disk usage.

registry-du Give you a better view of your Docker registry disk usage. This small tool will analysis your Docker registry(vanilla or Harbor both work)

Nova Kwok 16 Jan 07, 2023
Create C bindings for python automatically with the help of libclang

Python C Import Dynamic library + header + ctypes = Module like object! Create C bindings for python automatically with the help of libclang. Examples

1 Jul 25, 2022
WindowsDebloat - Windows Debloat with python

Windows Debloat 🗑️ Quickly and easily configure Windows 10. Disclaimer I am NOT

1 Mar 26, 2022
Python program to do with percentages and chances, random generation.

Chances and Percentages Python program to do with percentages and chances, random generation. What is this? This small program will generate a list wi

n0 3 Jul 15, 2021
Python module and its web equivalent, to hide text within text by manipulating bits

cacherdutexte.github.io This project contains : Python modules (binary and decimal system 6) with a dedicated tkinter program to use it. A web version

2 Sep 04, 2022
A thing to simplify listening for PG notifications with asyncpg

asyncpg-listen This library simplifies usage of listen/notify with asyncpg: Handles loss of a connection Simplifies notifications processing from mult

ANNA 18 Dec 23, 2022
a simple function that randomly generates and applies console text colors

ChangeConsoleTextColour a simple function that randomly generates and applies console text colors This repository corresponds to my Python Functions f

Mariya 6 Sep 20, 2022
This tool lets you perform some quick tasks for CTFs and Pentesting.

This tool lets you convert strings and numbers between number bases (2, 8, 10 and 16) as well as ASCII text. You can use the IP address analyzer to find out details on IPv4 and perform abbreviation a

Ayomide Ayodele-Soyebo 1 Jul 16, 2022
A BlackJack simulator in Python to simulate thousands or millions of hands using different strategies.

BlackJack Simulator (in Python) A BlackJack simulator to play any number of hands using different strategies The Rules To keep the code relatively sim

Hamid 4 Jun 24, 2022
Install, run, and update apps without root and only in your home directory

Qube Apps Install, run, and update apps in the private storage of a Qube Building instrutions

Micah Lee 26 Dec 27, 2022
A (very dirty) experiment to remove layers from a Docker image.

Surgically remove layers from a Docker image (with a chainsaw)

Jérôme Petazzoni 9 Jun 08, 2022
A utility tool to create .env files

A utility tool to create .env files dump-env takes an .env.template file and some optional environmental variables to create a new .env file from thes

wemake.services 89 Dec 08, 2022
osqueryIR is an artifact collection tool for Linux systems.

osqueryIR osqueryIR is an artifact collection tool for Linux systems. It provides the following capabilities: Execute osquery SQL queries Collect file

AbdulRhman Alfaifi 7 Nov 02, 2022
Aurin - A quick AUR installer for Arch Linux. Install packages from AUR website in a click.

Aurin - A quick AUR installer for Arch Linux. Install packages from AUR website in a click.

Suleman 51 Nov 04, 2022
Here, I find the Fibonacci Series using python

Fibonacci-Series-using-python Here, I find the Fibonacci Series using python Requirements No Special Requirements Contribution I have strong belief on

Sachin Vinayak Dabhade 4 Sep 24, 2021
SmarTool - Smart Util Tool for Python

A set of tools that keep Python sweeter.

Liu Tao 9 Sep 30, 2022
Finds price floor for every single attribute in a given collection

Solana Solanart Scanner Enjoy the Free Code Steps to run Download VS Code

Dalton Nisbett 19 Oct 20, 2022