Skip to content

Instantly share code, notes, and snippets.

View bnoordhuis's full-sized avatar

Ben Noordhuis bnoordhuis

View GitHub Profile
#include <stdint.h>
#include <limits.h>
uint64_t uint64_add(uint64_t a, uint64_t b, int* overflow) {
uint64_t c;
if (UINT64_MAX - a >= b) {
*overflow = 0;
return a + b;
}
@bnoordhuis
bnoordhuis / trace.py
Created July 6, 2011 19:02
trace function calls with gdb
#!/usr/bin/env python
import re
import sys
import subprocess
import tempfile
nm = 'nm'
gdb = 'gdb'
common = require("../common");
assert = require("assert");
net = require("net");
http = require("http");
var serverResponse = "";
var clientGotEOF = false;
server = http.createServer(function (req, res) {
console.log('server req');
@bnoordhuis
bnoordhuis / uv_ip6_addr.c
Created June 27, 2011 10:03
uv_ip6_addr
/**
* I, the copyright holder of this work, hereby release it into the public domain.
* This applies worldwide. In case this is not legally possible, I grant any entity
* the right to use this work for any purpose, without any conditions, unless such
* conditions are required by law.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
@bnoordhuis
bnoordhuis / benchmark.txt
Created June 26, 2011 00:24
Buffer.writeUInt32() benchmarklet
$ time ./node tmp/test-uint32-slow.js
real 0m8.819s
user 0m8.790s
sys 0m0.030s
$ time ./node tmp/test-uint32-fast.js
real 0m2.210s
user 0m2.190s
@bnoordhuis
bnoordhuis / punycode.py
Created June 20, 2011 16:31
python punycode encoder and decoder
#!/usr/bin/env python
#
# Copyright (C) 2011 by Ben Noordhuis <info@bnoordhuis.nl>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@bnoordhuis
bnoordhuis / punycode.js
Created June 20, 2011 15:49
javascript punycode encoder and decoder
/**
* Copyright (C) 2011 by Ben Noordhuis <info@bnoordhuis.nl>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
@bnoordhuis
bnoordhuis / bad.js
Created June 20, 2011 12:03
V8 data corruption
'Willst du die Blüthe des frühen, die Früchte des späteren Jahres'
.split('')
.map(function(c) { return c.charCodeAt(0); })
.map(String.fromCharCode); // corrupts data
.join('');
// produces 'W\u0000\u0000i\u0001\u0000l\u0002\u0000l\u0003\u0000s\u0004\u0000t\u0005\u0000 \u0006\u0000d\u0007\u0000u\b\u0000 \t\u0000d\n\u0000i\u000b\u0000e\f\u0000 \r\u0000B\u000e\u0000l\u000f\u0000ü\u0010\u0000t\u0011\u0000h\u0012\u0000e\u0013\u0000 \u0014\u0000d\u0015\u0000e\u0016\u0000s\u0017\u0000 \u0018\u0000f\u0019\u0000r\u001a\u0000ü\u001b\u0000h\u001c\u0000e\u001d\u0000n\u001e\u0000,\u001f\u0000 \u0000d!\u0000i"\u0000e#\u0000 $\u0000F%\u0000r&\u0000ü\'\u0000c(\u0000h)\u0000t*\u0000e+\u0000 ,\u0000d-\u0000e.\u0000s/\u0000 0\u0000s1\u0000p2\u0000ä3\u0000t4\u0000e5\u0000r6\u0000e7\u0000n8\u0000 9\u0000J:\u0000a;\u0000h<\u0000r=\u0000e>\u0000s?\u0000'
@bnoordhuis
bnoordhuis / ebadname.js
Created June 18, 2011 22:23
ebadname.js - uncatchable exception
var http = require('http');
var host = '********';
host += host;
host += host;
host += host;
host += host;
host += host;
try {
@bnoordhuis
bnoordhuis / test-utf8.c
Created June 14, 2011 22:49
Simple UTF-8 <-> Unicode encoder
/**
* Copyright (C) 2011 by Ben Noordhuis <info@bnoordhuis.nl>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*