Skip to content

Instantly share code, notes, and snippets.

@bdss58
bdss58 / kmp.py
Created March 1, 2020 13:30
KMP pattern search algorithm
from lps import gen_lps # reference lps.py
def kmp_search(pat, txt):
N = len(txt)
M = len(pat)
lps = gen_lps(pat)
print('here', lps)
i, j = 0, 0
@bdss58
bdss58 / lps.py
Last active March 1, 2020 13:29
lsp array for kmp search
def gen_lps(pat):
M = len(pat) # length of pat
current_len_of_lps = 0 # length of the previous longest prefix suffix
lps = [0]*M
if M == 1:
return lps
for i in range(1, M):
if pat[i] == pat[current_len_of_lps]:
current_len_of_lps += 1
lps[i] = current_len_of_lps
@bdss58
bdss58 / gbk2utf8.go
Created January 15, 2019 08:47
UTF82GBK
func GBKToUTF8(data []byte) []byte {
UTF8Reader := transform.NewReader(bytes.NewReader(data), simplifiedchinese.GBK.NewDecoder())
b, e := ioutil.ReadAll(UTF8Reader)
if e != nil {
log.Error("read GBKReader failed, error: %s, GBKReader: %v", e.Error(), UTF8Reader)
return nil
}
return b
}
@bdss58
bdss58 / ip2int.go
Created January 15, 2019 07:50
ip 字符串转整型 (golang)
func ip2int(ip string) uint32 {
IP := net.ParseIP(ip)
if len(IP) >= 16 {
return binary.BigEndian.Uint32(IP[12:16])
} else {
return 0
}
}
@bdss58
bdss58 / memory_layout.md
Created September 2, 2018 08:56 — forked from CMCDragonkai/memory_layout.md
Linux: Understanding the Memory Layout of Linux Executables

Understanding the Memory Layout of Linux Executables

Required tools for playing around with memory:

  • hexdump
  • objdump
  • readelf
  • xxd
  • gcore
@bdss58
bdss58 / printbits.c
Created August 25, 2018 04:18
print each byte of integer in binary format
#include <stdio.h>
void pbits(char);
int main() {
int n = 1025;
char* p = (char*)&n;
for (int i = 0; i < sizeof(n); i++) {
printf("%d byte: ", i);
@bdss58
bdss58 / tcpdump.sh
Last active September 19, 2018 10:18
tcpdump output to a file and view the out file
# tcpdump the localhost port to a pcap file
tcpdump -i lo 'port 3000' -w result.pcap
# view the pcap file using tcpdump
tcpdump -qns 0 -A -r result.pcap
# tcpdump the to dest host package to a pcap file
tcpdump -i eth1 -vv -n dst host 9.78.206.6 -w result.pcap
@bdss58
bdss58 / rsa_demo.sh
Created July 5, 2018 08:20
use rsa private key and public key to encrypt or decrypt
#!/bin/sh
# Create message to be encrypted
echo "Creating message file"
echo "---------------------"
echo "My secret message" > message.txt
echo "done\n"
# Create asymmetric keypair
echo "Creating asymmetric key pair"
echo "----------------------------"
@bdss58
bdss58 / LRUCache.go
Created July 4, 2018 14:00
LRUCache
package main
import (
"container/list"
"fmt"
"time"
"math/rand"
)
func init() {
rand.Seed(time.Now().UnixNano())
@bdss58
bdss58 / load_json_config.go
Created July 4, 2018 13:58
load json config file and read keys
var config = make(map[string]interface{})
func loadConfig(path string) (err error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Fatal("config file path is not exist")
}
file, err := os.Open(path)
if err != nil {
log.Fatal("can not open: ", path)