Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View lionello's full-sized avatar
🇭🇰

Lio李歐 lionello

🇭🇰
View GitHub Profile
@lionello
lionello / parseder.d
Created May 5, 2017 12:16
Parse a DER or PEM (base64) encoded ASN.1 file (certificates, keys, ...)
import std.file : read;
import std.stdio;
import std.base64;
import std.string;
import std.conv;
import std.algorithm : map;
enum DerTag {
EndOfContent = 0x0,
Bool = 0x1,
@lionello
lionello / countingkeywords.d
Created May 5, 2017 12:18
Count/detect popular words in social media messages
#!/usr/bin/env dmd -run
import std.range;
struct Words(R,int maxWordLen = 4)
{
int wordLen;
R inner;
dchar[maxWordLen] buffer;
this(R inner) {
this.inner = inner.save;//refRange(&inner);
foreach (ref d; buffer) {
@lionello
lionello / robo.ino
Created May 5, 2017 12:25
A simple wall-following robot in Arduino (from Xinchejian roborace)
#define MOTOR_L_F 6
#define MOTOR_L_B 5
#define MOTOR_R_B 9
#define MOTOR_R_F 10
#define SENSOR_L 11
#define SENSOR_F 12
void left(int speed)
{
@lionello
lionello / mrz.d
Last active September 7, 2017 04:02
MRZ (Machine Readable Zone) checksum calculator
// Copyright Lionello Lunesu, placed in the public domain.
import std.algorithm;
import std.range;
ubyte charCode(dchar c) pure {
switch (c) {
case '<': return 0;
case 'A': .. case 'Z': return cast(ubyte)(c - 'A' + 10);
case '0': .. case '9': return cast(ubyte)(c - '0');
default: assert(0);
@lionello
lionello / imageset2mipmap.js
Last active August 22, 2017 08:04
Node.JS script to losslessly convert an iOS .imageset to an Android mipmap
#!/usr/bin/env node
const FS = require('fs')
const Path = require('path')
const FolderMap = {
"1x": "mipmap-mdpi",
"2x": "mipmap-xhdpi",
"3x": "mipmap-xxhdpi",
}
@lionello
lionello / optimize_eagle.d
Created September 17, 2017 01:26
Optimize paths in Eagle PCB files for faster CAM processing of GERBER files
import std.xml;
import std.file;
import std.stdio;
import std.conv;
import std.math;
real EPSILON = 1e-2;
real distance_to_line(real fx, real fy, real tx, real ty, real px, real py) {
@lionello
lionello / androiduml.d
Last active October 20, 2017 06:27
Create PlantUML/graphviz dot activity diagram for an Android project
#!/usr/bin/env dmd -run
import std.xml;
import std.file;
import std.stdio;
Element getElementByTagName(Element parent, string name) {
foreach (child; parent.elements) {
if (child.tag.name == name) {
return child;
@lionello
lionello / ModExp.sol
Last active March 17, 2022 13:07
Solidity wrapper for Ethereum Byzantium's BigInt `modexp` built-in contract 0x5
pragma solidity ^0.4.17;
contract ModExp {
// Wrapper for built-in bigint_modexp (contract 0x5) as described here https://github.com/ethereum/EIPs/pull/198
function modexp(bytes memory _base, bytes memory _exp, bytes memory _mod) public view returns(bytes memory ret) {
uint256 bl = _base.length;
uint256 el = _exp.length;
uint256 ml = _mod.length;
@lionello
lionello / base64url
Last active August 6, 2022 17:37
base64url: wrapper script for base64
#!/usr/bin/env bash
# Source: https://gist.github.com/lionello/6bb597fc600932c6c737aab3b402147c
ignore=0
decode=0
dashdash=0
args=()
files=()
while [ $# -gt 0 ]; do
@lionello
lionello / repro181.sol
Last active March 14, 2018 18:56
Repro for Geth 1.8.x `eth.call` regression
#!/usr/bin/env node
const Child_process = require('child_process')
const Web3 = require('web3')
const Assert = require('assert')
const SOL = `
contract BS {
function isValidSignature(
address signer,
bytes32 hash,