Skip to content

Instantly share code, notes, and snippets.

from functools import singledispatch
from collections import Iterable
from json import JSONEncoder, dumps
json_converter = singledispatch(JSONEncoder().default)
json_converter.register(Iterable)(list)
class A:
def __init__(self, a, b):
import sys
import os
# This is basically shutil.copyfileobj, but ignoring IO errors when reading.
# After doing a simple experiment with btrfs checksumming, I wondered if it was possible
# to still read a file that gave error with `cp`. The answer is obviously yes, since
# the checksum errors will work at the block-level (thus, why the chunksize is bsize)
# If you actually want to recover corrupted files from btrfs, without having to restore
# every single file in the fs, you might want to use instead:
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GADTs #-}
module Models where
import Database.Persist.Sqlite (runSqlite)
-- without the explicit type signature, it fails
-- with an ambiguous Control.Monad.IO.Class.MonadIO instance
{-# LANGUAGE ScopedTypeVariables #-}
import System.Environment (getArgs, getEnv)
import Control.Exception (catch, SomeException)
import Data.Map.Strict (fromList, lookup, findWithDefault, insert, assocs)
import Prelude hiding (lookup)
partitionPairs' Nothing ys (x:xs) = partitionPairs' (Just x) ys xs
partitionPairs' Nothing ys [] = ys
partitionPairs' (Just x) ys (y:xs) = partitionPairs' Nothing ((x,y):ys) xs
import Data.Bits (clearBit, testBit)
import Data.Foldable (foldl')
data FindHoleError = MaxTooBig deriving (Show)
findholes :: Integer -> Integer -> [Int] -> Either FindHoleError [Int]
findholes min max xs
| max < (fromIntegral (maxBound :: Int)) = Right $ map (+ imin) (findholes' mask $ map (+ (-imin)) xs)
| otherwise = Left MaxTooBig
where
@berdario
berdario / strings.py
Created June 29, 2015 15:58
the "strings" command implemented in Python
from mmap import mmap, PROT_READ
import re
import sys
def strings(fname, n=6):
with open(fname, 'rb') as f, mmap(f.fileno(), 0, prot=PROT_READ) as m:
for match in re.finditer(('([\w/]{%s}[\w/]*)' % n).encode(), m):
yield match.group(0)
if __name__ == '__main__':
from math import ceil
from collections import namedtuple
def calc_bitmasks(bitlengths):
size = sum(bitlengths)
assert size % 8 == 0
assert all(x < 65 for x in bitlengths)
remaining = size
for x in bitlengths:
remaining -= x
@berdario
berdario / transform.hs
Last active August 29, 2015 14:20
Take all the possible letter substitutions in a password, and append to it a list of possible years
import System.Environment (getArgs)
import Data.Map (fromList, findWithDefault)
import qualified Data.Set as S
nub = S.toList . S.fromList
applyIf f = (\x flag -> if flag then f x else x)
flagCombinations n = sequence $ replicate n [True, False]
import sys
import struct
from collections import namedtuple
from mmap import mmap, PROT_READ
Header = namedtuple('Header', 'magic_number version_major version_minor thiszone sigfigs snaplen network')
RecordHeader = namedtuple('RecordHeader', 'timestamp timestamp_us size original_size')
def get_parser(record_type, struct_fmt, bigendian=False):
struct_fmt = ('>' if bigendian else '<') + struct_fmt
>>> raw_assessments = [('sslyze', ('ssl', 'infra')), ('nmap common', ('infra', ))]
>>> from collections import defaultdict
>>> types = defaultdict(set)
>>> for (name, _types) in raw_assessments:
... for t in _types:
... types[t].add(name)
...
>>> types
defaultdict(<class 'set'>, {'ssl': {'sslyze'}, 'infra': {'sslyze', 'nmap common'}})