Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
bytes=(0..255).to_a
valid=bytes.product(bytes)
.map{|a,b|(a.chr+b.chr).force_encoding('EUC-JISX0213')}
.select &:valid_encoding?
jis_only = valid.select{ |s| s.encode('UTF-8', undef: :replace) == "\uFFFD" }
# this will obtain only the valid characters that we can't convert to unicode
@berdario
berdario / withFiles.hs
Created January 16, 2015 19:28
I felt the need for an n-ary version of withFile. It works, but for something this simple, readFile + writeFile would've been enough
import System.Environment (getArgs)
import System.IO (withFile, IOMode(..), FilePath, Handle, hGetContents, hPutStr)
import Data.Functor ((<$>))
withFiles' :: [Handle] -> [(IOMode, FilePath)] -> ([Handle] -> IO r) -> IO r
withFiles' handles [] f = f $ reverse handles
withFiles' handles ((mode, fpath):xs) f = withFile fpath mode (\x -> withFiles' (x:handles) xs f)
withFiles = withFiles' []
@berdario
berdario / nixnotes.md
Last active August 29, 2015 14:14
Nix notes

It's been a while since I started to use Nixos, and I promised myself to improve the documentation/wiki, but since I haven't overcome yet the barrier of entry to that, and some of the information that I discovered/asked on irc/etc might otherwise be forgotten, I'll temporarily keep track of it here:

  • Don't rely too much on nix-shell/nix-build: the Phases will kick you out of the shell if an error happens, and you won't be able to load the correct scripts, I reported this here but I'll probably need to add some more details
  • Apparently there's no way to see software that is available for another platform (this would be useful in a porting effort): you have to manually grep nixpkgs yourself
  • There's no way to do a partial match query on nix-env: just nix-env -qaP "*" and grep it
  • The nix manual is seriously paltry when discussing the Nix language itself: some operators aren't even mentioned (e.g. <)
  • If
@berdario
berdario / fulltextindex.hs
Created February 11, 2015 09:11
A simple text index/full text search in Haskell (I wanted a simple, but non-trivial fold example)
import qualified Data.Map.Strict as M
import qualified Data.Set as S
docs = ["the quick brown fox jumps over the lazy dog",
"what does the fox say?",
"what is the meaning of life?"]
buildIndex docs = M.unionsWith S.union [foldr (flip M.insert $ S.singleton doc) M.empty $ words doc | doc <- docs]
search _ [] = S.empty
package object wordwrap{
def wrapSingleLine(column: Int)(words: List[String]): Iterable[String] = words match {
case List() => List();
case (hd :: tl) => {
tl.scanLeft(hd)({_ + " " + _}).span({_.length <= column}) match {
case (List(), _) => hd.grouped(column).toIterable ++ wrapSingleLine(column)(tl)
case (f, s) => {
val f2 = f.map({_.split(" ").last})
f2 ++ wrapSingleLine(column)(s.map{_.drop(f2.last.length)});
}
# I use this both inside configuration.nix with
## environment.systemPackages = (import ((builtins.getEnv "PWD") + "/packages.nix" ));
# and on the command line with
## nix-env -I ~ -f packages.nix -iA
#with (import (builtins.getEnv "HOME" + "/.nix-defexpr/channels_root/nixos/default.nix") {}).pkgs;
with (import "/home/dario/.nix-defexpr/channels_root/nixos/default.nix" {}).pkgs;
[
# software available in the nixos nix-channel
] ++
execve("/home/dario/.nix-profile/bin/java", ["java", "-jar", "/home/dario/Downloads/burpsuite_"...], [/* 74 vars */]) = 0
brk(0) = 0x1c09000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f53fc6f9000
readlink("/proc/self/exe", "/nix/store/nmd034pr2aa1brz015gix"..., 4096) = 80
access("/etc/ld-nix.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/x86_64", 0x7ffece51e4f0) = -1 ENOENT (No such file or directory)
open("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/
This file has been truncated, but you can view the full file.
execve("/home/dario/.nix-profile/bin/java", ["java", "-jar", "/home/dario/Downloads/burpsuite_"...], [/* 75 vars */]) = 0
brk(0) = 0x10b8000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f19e5550000
readlink("/proc/self/exe", "/nix/store/nmd034pr2aa1brz015gix"..., 4096) = 80
access("/etc/ld-nix.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/x86_64", 0x7ffe5c7c7390) = -1 ENOENT (No such file or directory)
open("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/lib/openjdk/bin/../lib/amd64/jli/tls/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/nix/store/nmd034pr2aa1brz015gixp0zni9r2w7f-openjdk-8u40b25/
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Caused by: java.lang.UnsatisfiedLinkError: /usr/lib/jayatana/libjayatanaag.so: libX11.so.6: cannot open shared object file: No such file or directory
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
@berdario
berdario / gist:9e2eecba0d35f9255f29
Last active August 29, 2015 14:18
libpcap1.5.3 regression
dario@curie ~/nixpkgs (pcapbug)>
/nix/store/s29l8m50xv0qxwvchvzf4znqs2z2dzcz-nmap-6.47/bin/nmap --version
Nmap version 6.47 ( http://nmap.org )
Platform: x86_64-unknown-linux-gnu
Compiled with: nmap-liblua-5.2.3 openssl-1.0.1l nmap-libpcre-7.6 libpcap-1.4.0 nmap-libdnet-1.12 ipv6
Compiled without:
Available nsock engines: epoll poll select
dario@curie ~/nixpkgs (pcapbug)>
sudo /nix/store/s29l8m50xv0qxwvchvzf4znqs2z2dzcz-nmap-6.47/bin/nmap -sS -vv 10.129.121.20 -n