Skip to content

Instantly share code, notes, and snippets.

View cypres's full-sized avatar

Hans Arnholm cypres

View GitHub Profile
@cypres
cypres / holiday.swift
Created January 16, 2015 23:58
Holiday calculation for iOS
// Playground - noun: a place where people can play
// This is free and unencumbered software released into the public domain.
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
// This playground calculates a set of opening hours keys, such as "every friday",
// "2. day in month friday", "easter sunday" etc, which can be used to figure out
@cypres
cypres / UIImageExt.swift
Created March 16, 2015 14:06
UIImage from UIImagePickerController resize + fix orientation
import UIKit
extension UIImage {
// Based on http://stackoverflow.com/a/1262395/449607
// Optimized for JPEG output
func imageResized(longEdge : CGFloat) -> UIImage {
func radians (degrees : Double) -> CGFloat {
return CGFloat(degrees * M_PI/180)
}
@cypres
cypres / Makefile.diff
Created April 16, 2015 17:51
Modified uWSGI Makefile to support multiple python version in same binary
diff -u /var/portshaker/freebsd/www/uwsgi/Makefile /var/portshaker/ocrepo/www/uwsgi/Makefile
--- /var/portshaker/freebsd/www/uwsgi/Makefile 2015-03-17 12:00:14.000000000 +0100
+++ /var/portshaker/ocrepo/www/uwsgi/Makefile 2015-04-16 19:47:51.000000000 +0200
@@ -12,20 +12,36 @@
LICENSE= GPLv2
LICENSE_FILE= ${WRKSRC}/LICENSE
-USES= python
+USES= python:2.7
USE_RC_SUBR= uwsgi
@cypres
cypres / smslen.cc
Last active August 29, 2015 14:20
Sample code to count number of chars in a SMS
#include <cassert> // cassert
#include <cmath> // ceil
#include <iostream> // cout
#include <cstring> // memset, memcpy
// Given a UTF-8 encoded string, calculate the length of the resulting GSM
// 03.38 converted string.
// It assumes the input is valid UTF-8 and UTF-8 chars that can not be
// represented will be replaced with a single ? char.
// High optimized code, runs best with clang and optimizations (-O) enabled.
@cypres
cypres / cidr_trim.cc
Created May 28, 2015 09:49
Trim a CIDR string to make it compatible with inet_net_pton
#ifdef __FreeBSD__
#include <sys/socket.h>
#endif
#include <arpa/inet.h>
#include <netinet/in.h>
#include <algorithm> // find, find_if_not, reverse_copy, for_each
#include <functional> // bind1st, std::equal_to
#include <string> // string
#include <iostream> // cout
@cypres
cypres / protoSocket.diff
Created June 25, 2015 13:56
Patches for protolib
--- protolib/src/common/protoSocket.cpp.orig 2015-06-25 13:43:52 UTC
+++ protolib/src/common/protoSocket.cpp
@@ -6,6 +6,7 @@
#ifdef MACOSX
#include <arpa/nameser.h>
#endif // MACOSX
+#include <netinet/in.h>
#include <resolv.h>
#endif // HAVE_IPV6
#include <sys/ioctl.h>
@cypres
cypres / norm.shar
Created June 25, 2015 16:50
norm FreeBSD port
# This is a shell archive. Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file". Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
# norm
# norm/distinfo
# norm/Makefile
@cypres
cypres / pf.conf
Created October 14, 2015 12:37
NPF Firewall Config
# Max 3 mio states
set limit states 3000000
# Adaptive state timeouts
set timeout { adaptive.start 400000, adaptive.end 3000000 }
# Own Traffic (front net does not route)
nat on ix0 from 212.98.89.24/30 -> 212.98.117.0/25 round-robin
# NAT NPF traffic with source hashing
# Use a static random hash to keep public IP across firewall changes/reloads
@cypres
cypres / mosh.py
Last active December 13, 2015 20:53
Workaround mosh wrapper
#!/usr/bin/env python3
import subprocess
import argparse
import re
import os
parser = argparse.ArgumentParser(description='Mosh connection tool.')
parser.add_argument('target', metavar='target', type=str, help='the SSH target')
parser.add_argument('--port', type=int, default=22, help='the SSH port')
parser.add_argument('--new', help='Open a new session', action='store_true')
#!/bin/bash
set -eux
sudo apt-get update
packages="postgresql-$PGVERSION postgresql-server-dev-$PGVERSION postgresql-common"
# bug: http://www.postgresql.org/message-id/[email protected]
sudo update-alternatives --remove-all postmaster.1.gz