Unix wc clone in Node.js

I have been trying to learn NodeJS a bit lately. I have found a good exercise to learn new language is to re-implement UNIX utilities. I decided to try to re-implement ‘wc’, which counts the number of words, characters, and lines that are received on stdin.

The code isn’t perfect, in that it doesn’t match the exact output of wc, but I am happy with it. The main thing I wanted to do is learn about Javascript closures, which this sample helped me to do. Note that the line_processor variable actually has four callable functions, but the ‘lines’, ‘characters’, and ‘words’ variables are all inaccessible outside of the line_processor closure. This was a tricky concept for me to understand.

// Takes a stream of data and counts the number of words, lines, and characters
// Similar to the the UNIX program 'wc'

var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout

var line_processor = (function(){

var lines = 0;
var characters = 0;
var words = 0;

return {
update : function(line) {
// imperfect counting, but good enough
characters = characters + line.length;
words = words + line.split(" ").length;
get_words: function() {
return words;
get_lines: function() {
return lines;
get_characters: function() {
return characters;

rl.on('line', function(line) {
rl.on('close', function() {
console.log("Words: " + line_processor.get_words());
console.log("Lines: " + line_processor.get_lines());
console.log("Characters: " + line_processor.get_characters());

Posted in Uncategorized | Leave a comment

A quick note on building ChromeOS for MIPS

I have been trying to build ChromeOS for a MIPS board lately and having lots of issues. Recording this for others but mostly so I don’t forget it myself. The error message I kept getting was:

(cr) ((8cac397...)) skerr@gravel ~/trunk/src/scripts $ export BOARD=mipsel-o32-generic
(cr) ((8cac397...)) skerr@gravel ~/trunk/src/scripts $ ./setup_board --board=${BOARD}

[ Lots of errors I removed ]
emerge: there are no ebuilds to satisfy "cross-mipsel-cros-linux-gnu/binutils".

emerge: searching for similar names...
emerge: Maybe you meant any of these: cross-x86_64-cros-linux-gnu/binutils, cross-i686-pc-linux-gnu/binutils, cross-armv7a-cros-linux-gnueabi/binutils?

I got through this by adding the –nousepkg flag to the invocation, like:

(cr) ((8cac397...)) skerr@gravel ~/trunk/src/scripts $ ./setup_board --board=${BOARD} --nousepkg
Posted in Uncategorized | Leave a comment

New Theme!

Apparently the last time WordPress updated itself, something changed. I noticed I was unable to login to the admin page, but the normal blog worked fine.

I tried a bunch of different fixes I found online, but the problem was that the wp-admin/ folder was set to group writeable permissions (0775), rather than owner write only. Changing permissions on the folder to 0755 fixed the problem for me.

I tried resetting my theme as part of the fix. I actually kind of like this theme (the default) so I might keep it for a while.

Posted in Uncategorized | Leave a comment

Converting Montgomery Elliptic Curves to Weierstrass Form

Recently I needed to convert a Montgomery form elliptic curve (Curve25519) to Weierstrass form. This post captures some of that work and code for arbitrary Montgomery form curves as well. I am going to do a lot of math hand waving, but will try to provide references as necessary.

Continue reading

Posted in Uncategorized | 13 Comments

A basic WPF updating DataGrid Example

I was pulling my hair out somewhat, trying to create a DataGrid in WPF, rather than using Winforms. Hopefully, posting my results here can help someone in the future. Leave a comment if they do!

Continue reading

Posted in Uncategorized | Leave a comment

PE & Coff Document Typos Are Quiet Irritating

Just a quick post to remind myself of this in the future (and hopefully help others!)

Anyways, I’m working on writing a PE parser module. Part of this is listing the relative virtual addresses (RVA) of exported symbols. To figure out how to do this, I was reading the PE & Coff Specification (http://msdn.microsoft.com/en-us/windows/hardware/gg463119.aspx) document for reference. However, there is some conflicting information in it. For the rest of this post, all section numbers apply to Revision 8.3 (February 6, 2013) unless otherwise noted.

Specifically, Section 5.3.4 contains this listing:

i = Search_ExportNamePointerTable (ExportName);
ordinal = ExportOrdinalTable [i];
SymbolRVA = ExportAddressTable [ordinal - OrdinalBase];

However, Section 5.3.5 contains this line:

Every exported symbol has an ordinal value, which is just the index into the export address table (plus the Ordinal Base value).

So the the table in 5.3.4 should actually be:

i = Search_ExportNamePointerTable (ExportName);
ordinal = ExportOrdinalTable [i + OrdinalBase];
SymbolRVA = ExportAddressTable [ordinal - OrdinalBase];

This caused me several hours of frustration! Hopefully you don’t run into the same problems. I’m going to try to get Microsoft to fix this error if possible.

Posted in Code, Reverse Engineering | Leave a comment

Matlab Reverb and Image Smoothing Functions

Recently, I enrolled in a class on Digital Signals and Systems. One of our first homework assignments was to implement a reverb filter and image smoother in Matlab. I’m not a Matlab expert, but I thought someone else might like to see what I did. Please suggest improvements!

function [ y ] = reverb( Fs, D, Attenuation, x )
%REVERB (Fs, D, Attenuation, x) 
%   Applies reverb to the given sound file
%   Fs - Sample Rate
%   D - Delay in milliseconds
%   Attenuation - Attenuation factor
%   x - WAV file as a column vector

    % Convert delay to samples
    D = round(Fs * D / 1000);

    % Generate the b array (corresponds to X coeffecients)
    b = [1 zeros(1,D+1)];

    % Generate the a array (corresponds to Y coeffecients)
    a = [1 zeros(1,D-1) Attenuation];

    % Apply the filter
    y = filter(b, a, x);

The image smoothing function is as follows


function [ y ] = imagesmooth( filename, N )
%IMAGESMOOTH Smooths the given image with an NxN matrix
%   filename - The image file to use
%   N - The size of the smoothing matrix
    image = imread(filename);
    h = ones(N) / (N*N);
    newimage = filter2(h,image);
    y = uint8(newimage);


Basically, a reverb and an image smoother are types of difference equations. This is one of the things I’m learning about in my signals class. Essentially, what is happening is every sample y[N] of the output is calculated from an input sample, x[N], along with other past samples from either the input or the output. In the case of the reverb, x[N] is combined with y[N-D] to compute y[N]. For the image smoother, y[N,M] is a function of x[N,M] and all the surrounding samples, x[N-1,M-1], x[N-1,M]], x[N-1,M+1], x[N,M-1], x[N,M], x[N,M+1], x[N+1,M-1], x[N+1,M], and x[N+1,M+1].

Posted in Code | Leave a comment

If you’re only using Github, you’re doing Git wrong.

This Rhino has some things to tell you about Git

This Rhino has some things to tell you about Git


The other day, I read the following article. If you don’t want to read it, you really should. It’s awesome. Essentially, a student managed to get access to all of India’s standardized test scores and who they belonged to. The author posted his code and results on Github (thanks for sharing!). However, the data itself was not mirrored and is no longer available. Grumpy. I wanted to pull the data off Github.

This got me thinking about Git, how to use Github with it, and my own personal Git habits. Git is designed as a distributed version control system. This means that there should be no single ‘master’ copy of your source code anywhere; multiple copies should be available from different nodes across the network, be that the local network or the Internet.


You are working on a code project at home and dutifully using Git for version control (as you should!). You commit your changes locally and push them to Github. You get off the computer, drink beer, socialize, work on your plan for world domination, sleep, and head to work the next day. Your co-worker asks to see what you were working on (of course you told her about how awesome your side project was). You go to Github to pull the code and *gasp!* Github is down! Not only that, but your cat ate the laptop at home with all the code! Suddenly, all your code is gone forever. (Maybe this example is a bit hyperbolic, but stay with me.)

The Problem

When most people use Git, the only remote they use is Github. In the previous scenario, Github was considered to be the 100% safe backup solution. By pushing to only Github, what you’ve done case is create a single point of failure.


When you only use Github as the remote you push to, you are negating a lot of benefits of distributed version control. Your code should be spread out among multiple nodes across the network. When you only use one remote, you are using two nodes: your local machine and the remote. This may work fine most of the time, but what if you forget your laptop and the remote is down? You should have multiple remotes to pull from in cases like this.

Note that when I am saying ‘remotes’, I essentially just mean nodes on the network. I do a lot of development in my free time and use Git to keep track of my personal projects. In a company environment though, you could also push code to your co-workers, team leads, build servers, and a backup. In case of a build server failure, you can then pull changes from any of those machines.


The solution is to simply have more than one remote! This is not hard to set up or configure. When you have multiple remotes, you are never at the mercy of a single remote server. Not only could a single remote go down, the admins could decide to be super evil and just delete your code. Bad news bears.

With multiple remotes, if one remote goes down, no worries, just pull and push to the others instead.

Call to Action

There are plenty of free options for Github hosting. Since Git is distributed, just because you are using Github, doesn’t mean you can’t also use BitBucket! I myself use both. If you have a server at home or pay for a VPS, add that as a remote too! Below I have provided an example of adding 2 new remotes  (one for BitBucket and Github) for a project I’m working on in my free time.

Add a new remote

$ # git remote add alt alt-machine:/path/to/repo
$ git remote add github git@github.com:stkerr/MemoryPIN.git
$ git remote add bitbucket git@bitbucket.org:samoz/memorypin.git

Push to the new remote with

$ # git push <remotename> <branch name>
$ git push github master
$ git push bitbucket master

Push to multiple remotes at once

This part is somewhat confusing, so maybe check the StackOverflow link for more info. Essentially, you are editing your .git/config file to create a new remote that has multiple URLs (that you added previously) in it. Edit your .git/config file and add:

[remote "AllTheThings"]
    url = git@github.com:stkerr/MemoryPIN.git
    url = git@bitbucket.org:samoz/memorypin.git

Then to push to all of those different remotes, do

$ git push AllTheThings master

I used info from here and here for the commands above.


If you’re only using Github or one remote server to push your Git code to, you are doing it wrong. Git is designed to have multiple remote servers, so in case of a failure, you simply use a different remote. I have pointed out some options you can use for new Git remotes, such as Github, Bitbucket, or using a personal server. I also showed you how to add these new remotes to your existing repo and to push to them.

Leave a comment if you have any suggestions! I’m not a Git expert by any means, so I’m sure there are other ways of doing this!

Posted in Uncategorized | Tagged , | Leave a comment

Let’s talk about anti virus detection: Kaspersky DNS cache detection

I’ve been playing around with some basic HTTP programs lately and noticed that Kaspersky flags them as viruses for some reason. I did some investigation and learned why and how to get around those detections.

Continue reading

Posted in Uncategorized | Tagged , , , , | Leave a comment

Configuring the analog to digital converter (ADC) on AVR chips

While trying to figure out how to use the ADC on my AVR microcontrollers (not Arduino, raw AVRs!), I kept finding a variety of tutorials with examples that just didn’t work for me. I eventually got it working, so thought I would share, hoping it would help someone else eventually!

I am using an ATmega32U4 (awesome chip!), but these steps should be appropriate for most AVRs.  Note that my clock is running at 8 MHz. If you change the clock speed, make sure to change the prescalar value.

ADCSRA |= (1 << ADPS2) | (1 << ADPS1); // prescalar = 64
ADMUX |= (1 << REFS0); // reference voltage from internal source
ADMUX |= (1 << ADLAR); // 8 bits, instead of 10
ADCSRA |= (1 << ADATE); // free running
ADCSRA |= (1 << ADEN); // enable ADC
ADCSRA |= (1 << ADSC); // start A2D conversions

Note that this code works for a continuously running, non-interrupt driven ADC program. If you want to use single shot conversions or interrupt driven ADC, you will need to change these lines appropriately. I might add that as another post eventually.

Posted in Uncategorized | Leave a comment