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.

You might also like...
An awesome tool to save articles from RSS feed to Pocket automatically.
An awesome tool to save articles from RSS feed to Pocket automatically.

RSS2Pocket An awesome tool to save articles from RSS feed to Pocket automatically. About the Project I used to use IFTTT to save articles from RSS fee

This program organizes automatically files in folders named as file's extension

Auto Sorting System by Sergiy Grimoldi - V.0.0.2 This program organizes automatically files in folders named as file's extension How to use the code T

Grank is a feature-rich script that automatically grinds Dank Memer for you
Grank is a feature-rich script that automatically grinds Dank Memer for you

Grank Inspired by this repository. This is a WIP and there will be more functions added in the future. What is Grank? Grank is a feature-rich script t

Create a Web Component (a Custom Element) from a python file

wyc Create a Web Component (a Custom Element) from a python file (transpile python code to javascript (es2015)). Features Use python to define your cu

🦩 A Python tool to create comment-free Jupyter notebooks.

Pelikan Pelikan lets you convert notebooks to comment-free notebooks. In other words, It removes Python block and inline comments from source cells in

Import the module and create an object of the class LocalVariable.
Import the module and create an object of the class LocalVariable.

LocalVariable Import the module and create an object of the class LocalVariable. Call the save method with the name and the value of a variable as arg

Create powerful passwords easily and with many options with this program
Create powerful passwords easily and with many options with this program

Password_Generator About the Program: You can create powerful passwords with this program with many options easily! Features: You can copy the generat

This project is a set of programs that I use to create a README.md file.
This project is a set of programs that I use to create a README.md file.

This project is a set of programs that I use to create a README.md file.

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

Owner
null
isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type.

isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type. It provides a command line utility, Python library and plugins for various editors to quickly sort all your imports.

Python Code Quality Authority 5.5k Jan 8, 2023
Python script to launch burp scans automatically

SimpleAutoBurp Python script that takes a config.json file as config and uses Burp Suite Pro to scan a list of websites.

Adan Álvarez 26 Jul 18, 2022
Python code to generate and store certificates automatically , using names from a csv file

WOC-certificate-generator Python code to generate and store certificates automatically , using names from a csv file IMPORTANT In order to make the co

Google Developer Student Club - IIIT Kalyani 10 May 26, 2022
Basic loader is a small tool that will help you generating Cloudflare cookies

Basic Loader Cloudflare cookies loader This tool may help some people getting valide cloudflare cookies Installation ?? : pip install -r requirements.

IHateTomLrge 8 Mar 30, 2022
Control-Alt-Delete - Help Tux Escape Beastie's Jail!

Control-Alt-Delete Help Tux escape Beastie's jail by completing the following challenges! Challenges Challenge 00: Drinks: Tux needs to drink less. Ch

NDLUG 8 Oct 31, 2021
Software to help automate collecting crowdsourced annotations using Mechanical Turk.

Video Crowdsourcing Software to help automate collecting crowdsourced annotations using Mechanical Turk. The goal of this project is to enable crowdso

Mike Peven 1 Oct 25, 2021
This repository contains scripts that help you validate QR codes.

Validation tools This repository contains scripts that help you validate QR codes. It's hacky, and a warning for Apple Silicon users: the dependencies

Ryan Barrett 8 Mar 1, 2022
Airspy-Utils is a small software collection to help with firmware related operations on Airspy HF+ devices.

Airspy-Utils Airspy-Utils is a small software collection to help with firmware related operations on Airspy HF+ devices on Linux (and other free syste

Dhiru Kholia 11 Oct 4, 2022
Automatically Generate Rulesets for IIS for Intelligent HTTP/S C2 Redirection

Automatically Generate Rulesets for IIS for Intelligent HTTP/S C2 Redirection This project converts a Cobalt Strike profile to a functional web.config

Jesse 99 Dec 13, 2022
Find version automatically based on git tags and commit messages.

GIT-CONVENTIONAL-VERSION Find version automatically based on git tags and commit messages. The tool is very specific in its function, so it is very fl

null 0 Nov 7, 2021