Skip to content

Instantly share code, notes, and snippets.

>>> import pyroute2
>>>
>>> ip = pyroute2.IPRoute()
>>> ip.bind()
>>>
>>> while True:
... for message in ip.get():
... print(message['event'])
...
RTM_DELADDR
import socket
import pyroute2
ip = pyroute2.IPRoute()
addrs = [
'/'.join((
dict(addr.get('attrs', [])).get('IFA_ADDRESS'),
str(addr.get('prefixlen')),
))
#include "ki25.h"
si s16 _sin_s16_4(s16 x) {
s16 x_2 = (x * x) >> 16;
return
( 0x1922 * x
- 0x0a56 * (x_2 * x >> 12)
+ 0x146a * ((x_2 * x_2 >> 16) * x >> 12)
- 0x1330 * (((x_2 * x_2 >> 16) * x_2 >> 16) * x >> 12)
) >> 12;
@acdimalev
acdimalev / cfg.sh
Last active November 4, 2023 21:59
building a miniaturized control flow graph
#!/bin/sh
r2 -qc "e anal.bb.maxsize=2048;s sym.$2;af;agfj" "$1" | python3 control-flow-graph.py
@acdimalev
acdimalev / f.c
Last active September 20, 2018 09:06
function optimizations
void f(f32 *d, u16 n) {
for (u32 i = 0; i < n; i++) {
d[i] = i;
}
}
// KI25 -- Keep It 2hort 5tupid
// TABA -- These Are Bad Acronyms
// - (probably) bad variable names
// - horrible function names
// - reserve for extremely common stuff
#define si static inline
#include <stdint.h>
#include "ki25.h"
// convert an index into a coordinate,
// given a width `w` and height `h`
si u16 xcoord(u32 i, u16 w, u16 h) { return i % w; }
si u16 ycoord(u32 i, u16 w, u16 h) { return i / w; }
// normalize value on a scale from `0` to `s`
si f32 fnorm(u16 x, u16 s) { return (f32)x / s; }
#!/bin/sh
cd "$(xcwd)" && exec i3-sensible-terminal
# use xcwd to spawn terminals in the same cwd
# as a focused window
#
# xcwd: https://github.com/schischi/xcwd
#
# recommended usage:
# bindsym $mod+Shift+Return exec i3-sensible-terminal-xcwd

let w and h be the width and the height of the screen, respectively.

let a be the ratio of the width to the height.

a = w / h

Note that this is equivalent to the aspect ratio of the screen for the case where the screen is wider than it is tall, and the inverse of the aspect ratio otherwise.

Let q be the area of the screen.

@acdimalev
acdimalev / example.c
Last active September 14, 2018 16:59
Banker's Rounding
si u8 _round(u8 x, u8 r) {
u8 a = !(x & 1 << r - 1); // rounding down is nearest
u8 b = !!(x - 1 & 1 << r - 1); // `!a && b`, rounding up is nearest
u8 c = !!(x & 1 << r); // rounding up is even
u8 d = !a && (b || c); // should we round up?
return x - (x & (1 << r) - 1) + ((d ? 1 : 0) << r);;
}