Skip to content

Instantly share code, notes, and snippets.

View Hanaasagi's full-sized avatar
🧠
我脑子有病

Hanaasagi

🧠
我脑子有病
View GitHub Profile

Keybase proof

I hereby claim:

  • I am Hanaasagi on github.
  • I am hanaasagi (https://keybase.io/hanaasagi) on keybase.
  • I have a public key whose fingerprint is BF53 8DF8 E365 57CC 516D 9A8B A524 26A3 B59F F8EC

To claim this, I am signing this object:

fn my_sqrt(x: i32) -> i32 {
let mut n = x as f64;
let half = 0.5 * n;
let mut i = unsafe {
std::mem::transmute::<f64, i64>(n)
};
i = 0x5fe6ec85e7de30da - (i>>1);
n = unsafe{
std::mem::transmute::<i64, f64>(i)
};
@Hanaasagi
Hanaasagi / hijacking-write.rs
Created December 26, 2018 14:36
hijacking write call in Linux
use libc;
use std::ffi::CString;
use std::mem::transmute;
use std::os::raw::c_char;
use std::os::raw::c_int;
use std::os::raw::c_void;
#[no_mangle]
extern "C" fn strcmp(s1: *const c_char, s2: *const c_char) -> c_int {
println!("strcmp");
@Hanaasagi
Hanaasagi / pre-commit
Created September 1, 2018 05:01
git pre commit hook for remove trailing whitespace in xml
files=$(find . -name '*.xml' | xargs egrep '[a-z].* $' |cut -d : -f 1|uniq)
if [ -z "$files" ]; then
exit 0
fi
echo "====== GIT PRE COMMIT HOOK ======"
echo "following file contains trailing whitespace:"
echo
VERSION=$(echo "${GIT_BRANCH:7:7}-${GIT_COMMIT:0:7}" | sed "s/\//-/g")
echo "VERSION: $VERSION"
sed "s/VERSION=[^0-9.]*\([0-9.]*\).*/VERSION=$VERSION/" Makefile.var
#!/usr/bin/env bash
set -eo pipefail
# hello-world latest ef872312fe1b 3 months ago 910 B
# hello-world latest ef872312fe1bbc5e05aae626791a47ee9b032efa8f3bda39cc0be7b56bfe59b9 3 months ago 910 B
# debian latest f6fab3b798be 10 weeks ago 85.1 MB
# debian latest f6fab3b798be3174f45aa1eb731f8182705555f89c9026d8c1ef230cbf8301dd 10 weeks ago 85.1 MB
if ! command -v curl &> /dev/null; then
echo >&2 'error: "curl" not found!'
@Hanaasagi
Hanaasagi / count.py
Created March 6, 2018 13:21
count substring
def count(s, sub):
result = 0
for i in range(len(s) + 1 - len(sub)):
result += (s[i:i + len(sub)] == sub)
return result
import requests
url = 'http://wlkt.nuist.edu.cn/(S(qfgpjnudhjyxvpnidq2zeu45))/'
def get_checkcode():
res = requests.head(url + 'yzm.aspx')
return res.cookies['checkcode']
'dd if=/dev/urandom bs=1 count=%d 2>/dev/null' % size
@Hanaasagi
Hanaasagi / Consistent-Hashing.py
Created October 29, 2017 12:18
一致性哈希 Python 实现
import hashlib
from bintrees.rbtree import RBTree
from types import GeneratorType
def md5(data: str) -> int:
"""md5 util function"""
return int(hashlib.md5(data.encode('utf-8')).hexdigest(), 16)