Skip to content

Instantly share code, notes, and snippets.

View fcicq's full-sized avatar

fcicq fcicq

View GitHub Profile
@fcicq
fcicq / regexp-trie.py
Last active July 14, 2021 23:37
Regexp::Trie in Python, by rex @ iregex.org. similar with Aho-Corasick
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#author: rex
#blog: http://iregex.org
#filename tr.py
#created: 2010-08-01 20:24
#source uri: http://iregex.org/blog/trie-in-python.html
# escape bug fix by fcicq @ 2012.8.19
# for revert instructions please see http://digitaldj.net/2011/07/21/trim-enabler-for-lion/
# \x51 is changed to \x4d in the script
sudo cp /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage.original
# this is for 10.8.1-10.8.2(?)
# sudo perl -pi -e 's|(\x52\x6F\x74\x61\x74\x69\x6F\x6E\x61\x6C\x00{1,20})[^\x00]{9}(\x00{1,20}\x4d)|$1\x00\x00\x00\x00\x00\x00\x00\x00\x00$2|sg' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage
# this is for 10.8.3
sudo perl -pi -e 's|(\x52\x6F\x74\x61\x74\x69\x6F\x6E\x61\x6C\x00{1,20})[^\x00]{9}(\x00{1,20}\x54)|$1\x00\x00\x00\x00\x00\x00\x00\x00\x00$2|sg' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage
sudo touch /System/Library/Extensions/
@fcicq
fcicq / ferm.conf
Created August 26, 2012 18:27
Ferm config file
# IPv6
domain ip6 table filter {
chain INPUT {
proto ipv6-icmp ACCEPT;
}
}
domain (ip ip6) table filter {
# fcicq: temporary disable log...
chain logdrop {
# LOG log-level warning log-prefix "dropped ";
@fcicq
fcicq / fcicq-question.txt
Created September 7, 2012 13:31
A simple question
有一存有整数的数组 A[n] (n <= 2^31 - 1, 但需要的话可以再扩大), 其中 0 <= i, A[i] <= n-1 且 i!=j 时 A[i]!=A[j] (即一个排列). 用你认为*最高效*的方法求 B=A^(-1) (即对范围内满足 A[B[i]]=i 的 B)
@fcicq
fcicq / didntknow.py
Created October 6, 2012 06:47
Things you didn't know about Python https://speakerdeck.com/u/mitsuhiko/p/didntknow
import sys
import traceback
def dump_threads():
for thread_id, frame in sys._current_frames().iteritems():
print 'Thread #%d' % thread_id
print ''.join(traceback.format_stack(frame))
# Usage: print_frame_info(sys._getframe())
def print_frame_info(frame):
print 'module: %s' % frame.f_globals.get('__name__')
@fcicq
fcicq / aes-ctr-test.py
Created October 15, 2012 12:23
AES CTR Test
from Crypto.Cipher import AES
from Crypto.Random import _UserFriendlyRNG
from Crypto.Util import Counter
import base64
AES_NBITS = 256
key = _UserFriendlyRNG.get_random_bytes(AES_NBITS / 8)
iv = _UserFriendlyRNG.get_random_bytes(AES.block_size / 2)
# we use the 8 bytes as iv, 8 bytes as counter, max file size is 2^64 * 16 = 2^68 bytes.
ctr_counter = Counter.new(AES.block_size * 8 / 2, prefix=iv)
@fcicq
fcicq / readability-to-del.py
Created October 24, 2012 05:22
readability to delicious (to import to pocket, http://getpocket.com/import/delicious )
# before using: rename your exported json as read.json
a = json.load(file('read.json'))
print '''<!DOCTYPE NETSCAPE-Bookmark-file-1>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<!-- This is an automatically generated file.
It will be read and overwritten.
Do Not Edit! -->
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
@fcicq
fcicq / poormansprofiler.sh
Created October 30, 2012 17:31
poor man's profiler via http://poormansprofiler.org/ , modify pid if required
#!/bin/bash
nsamples=1
sleeptime=0
pid=$(pidof mysqld)
for x in $(seq 1 $nsamples)
do
gdb -ex "set pagination 0" -ex "thread apply all bt" -batch -p $pid
sleep $sleeptime
done | \
@fcicq
fcicq / a.stp
Created October 30, 2012 17:56
SystemTap (userland) with flamegraph by agentzh
/*
code: https://groups.google.com/d/msg/openresty/u-puKWWONMk/bxsyQdWMkJIJ by agentzh
Require SystemTap 2.0 and linux kernel 3.5+
Example usage:
stap --ldd -d /path/to/nginx/sbin/nginx \
-d /path/to/luajit/lib/libluajit-5.1.so.2.0.0 \
--all-modules -D MAXMAPENTRIES=20240 \
-D MAXACTION=20000 \
-D MAXTRACE=100 \
-D MAXSTRINGLEN=4096 \
@fcicq
fcicq / midreduce.py
Created October 31, 2012 09:56
reduce with midstate preserved
def midreduce_iter(func, d, init=None):
it = iter(d)
if init is None:
i = it.next()
yield i
else:
i = init
while True:
i = func(i, it.next()) # as it will raise StopIteration exception...
yield i