Skip to content

Instantly share code, notes, and snippets.

@nyg
nyg / MemoryAddress.swift
Last active October 12, 2023 07:42
Get the memory address of both class and structure instances in Swift.
// https://stackoverflow.com/a/45777692/5536516
import Foundation
struct MemoryAddress<T>: CustomStringConvertible {
let intValue: Int
var description: String {
let length = 2 + 2 * MemoryLayout<UnsafeRawPointer>.size
@nyg
nyg / AddJPEGComment.swift
Last active February 21, 2021 05:17
Add a JPEG comment marker to file in pure Swift.
//
// Inspired by wrjpgcom of libjpeg
// https://github.com/libjpeg-turbo/libjpeg-turbo/blob/master/wrjpgcom.c
//
// https://stackoverflow.com/a/46045524/5536516
//
// Note: To add an EXIF UserComment go here:
// https://gist.github.com/nyg/c90f36abbd30f72c8b6681ef23db886b
import Foundation
@nyg
nyg / pdk.c
Created April 21, 2020 09:43
Create a password-derived key using libsodium.
/*
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.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
@nyg
nyg / keybase.md
Created September 9, 2019 19:13
keybase.md

Keybase proof

I hereby claim:

  • I am nyg on github.
  • I am ngaranis (https://keybase.io/ngaranis) on keybase.
  • I have a public key ASB-F2fnOPuIOpWWLKsB-b8ZJ0MNhPoXSvSdblQLuYfBzQo

To claim this, I am signing this object:

@nyg
nyg / auto_extract.sh
Created April 2, 2019 11:21
Script to recursively extract archives
#!/usr/bin/env bash
# Usage: /absolute/path/to/script.sh /absolute/path/to/folder/with/archive
for file in $1/* ; do
if [ -f "$file" ] ; then
echo Found file: $file
info=$(file $file | tr '[:upper:]' '[:lower:]')
@nyg
nyg / kraken-ledger.js
Created May 30, 2014 21:32
Get all entries of your Kraken.com ledger.
/* Querying your ledger through Kraken's API only return 50 results... */
var KrakenClient = require('kraken-api'); // https://github.com/nothingisdead/npm-kraken-api
var kraken = new KrakenClient('api_key', 'api_secret');
var ledger = [];
var offset = 0;
function getLedgerEntries (error, data) {
@nyg
nyg / sequence_iterator.swift
Last active September 20, 2017 11:41
Implementing Sequence & IteratorProtocol, Swift 3
import Foundation
class Cell {
var name: String
init(_ name: String) {
self.name = name
}
}
@nyg
nyg / AsynchronousServerSocketChannelTest.java
Created September 15, 2017 18:57
Basic code example for AsynchronousServerSocketChannel.
AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(ip, port));
while (true) {
Future<AsynchronousSocketChannel> future = listener.accept();
AsynchronousSocketChannel channel = future.get();
ByteBuffer buffer = ByteBuffer.allocate(5000);
Future<Integer> byteCount = channel.read(buffer);
System.out.println("Bytes read: " + byteCount.get());
@nyg
nyg / Serialize.swift
Created August 29, 2017 19:50
Serialize NSObject (or array of) to XML in Swift.
import Foundation
class MyClass: NSObject, NSCoding {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
@nyg
nyg / fk_delete_recursive.sql
Last active August 25, 2017 08:01
Oracle: delete a row and all rows preventing it to be deleted because of "child record found" errors.
DECLARE
-- ORA-02292: integrity constraint (OWNER.FK) violated - child record found
ora_02292 EXCEPTION;
PRAGMA EXCEPTION_INIT(ora_02292, -2292);
l_foreign_key VARCHAR2(400);
PROCEDURE delete_row(pi_table IN VARCHAR2,
pi_column IN VARCHAR2,
pi_value IN VARCHAR2) IS