Skip to content

Instantly share code, notes, and snippets.

@xk
xk / CrossFunctionVarManipulation.c
Created Feb 9, 2014
Cross-function var manipulation
View CrossFunctionVarManipulation.c
//2014-02-09 jorge@jorgechamorro.com
//Cross-function var manipulation
#include <stdio.h>
#include <string.h>
void uno () {
char* b= (char*) &b;
while (strncmp("Ehh", b, 3)) b++;
@xk
xk / ManipulateStackFrameReturnAddress.c
Last active Aug 29, 2015
Manipulate the stack frame return address
View ManipulateStackFrameReturnAddress.c
//2014-02-09 jorge@jorgechamorro.com
//Manipulate the stack frame return address
#include <stdio.h>
#include <string.h>
void uno () {
char** r= (char**) &r+2;
while (**r != '\x90') (*r)++;
@xk
xk / StackFrameGarbage.c
Created Feb 9, 2014
stack frame garbage
View StackFrameGarbage.c
//2014-02-09 jorge@jorgechamorro.com
//stack frame garbage
#include <stdio.h>
void uno () {
char a[18]= "GeorgeOfTheJungle";
}
void dos () {
@xk
xk / happyNumbers.js
Created Feb 10, 2014
Concise Happy Numbers JavaScript
View happyNumbers.js
//2014-02-10 jorge@jorgechamorro.com concise happy numbers JavaScript
function happy (n) {
var past= [];
while (n= [].reduce.call(n.toString(), function (a,n) { return a+n*n }, 0))
if (n === 1) return 1; else if (past.indexOf(n) >= 0) return 0; else past.push(n);
}
for (var i=0 ; i<=100 ; ++i) happy(i) && console.log(i);
@xk
xk / happyNumbers.c
Created Feb 10, 2014
Happy Numbers in C
View happyNumbers.c
//2014-02-10 jorge@jorgechamorro.com happy numbers in C
#include<stdio.h>
unsigned sumaCuadrados (unsigned n) {
unsigned d, r= 0;
while (n) d= n % 10, r+= d*d, n/= 10;
return r;
}
@xk
xk / happyNumbers.js
Created Feb 10, 2014
Happy Numbers in JavaScript
View happyNumbers.js
//2014-02-10 jorge@jorgechamorro.com happy numbers
String.prototype.reduce= [].reduce;
Array.prototype.has= function has (n) { return this.indexOf(n) >= 0 };
Array.prototype.times= function (f,i) { for (i= this[0] ; i<=this[1] ; i++) f(i) };
function happy (n) {
var past= [];
while (1) {
if ((n= n.toString().reduce(function(n,v){ return n+v*v },0)) === 1) return 1;
@xk
xk / fasterHappyNumbers.js
Created Feb 10, 2014
Faster Happy Numbers JavaScript
View fasterHappyNumbers.js
//2014-02-10 jorge@jorgechamorro.com Happy Numbers JavaScript
function sumaCuadrados (n) {
var d, r= 0;
while (n) n-= (d= n % 10), r+= d*d, n/= 10;
return r;
}
function isHappy (n) {
while (n && n!=1 && n!=4 && n!=16 && n!=37 && n!=58 && n!=89 && n!=145 && n!=42 && n!=20)
@xk
xk / blocks.js
Created Mar 10, 2014
Blocks === multiline
View blocks.js
/*!
multiline
Multiline strings in JavaScript
https://github.com/sindresorhus/multiline
by Sindre Sorhus
MIT License
*/
(function () {
'use strict';
View walker.js
var fs = require("fs");
var util = require("util");
function walkSync(filename, cb) {
try {
var stat = fs.lstatSync(filename);
if(stat.isDirectory()) {
try {
if(cb(null, filename, stat)) {
var files = fs.readdirSync(filename);
@xk
xk / lazyEventLoop.js
Created Dec 14, 2010
lazyEventLoop.js
View lazyEventLoop.js
setTimeout(function () {
t= Date.now()-t;
STOP= 1;
console.log(["ctr: ",ctr, ", t:", t, "ms -> ", (ctr/t).toFixed(2), "KHz"].join(''));
}, 2e3);
var ctr= 0;
var STOP= 0;
var t= Date.now()+ 2;
while (t > Date.now()) ; //get in sync with clock
You can’t perform that action at this time.