Archive for July, 2009

Boring Friday – What are you doing that is fun?

Suffering from a little burnout after delivering a few projects. For those coders out there, what are you working on thats fun / exciting?

I will start:

Favicon_normalFollowing up from my previous post, the most fun thing I did was compete in the Engine Yard Contest.. I created a python client, which communicated the results back to my webserver every 23 minutes via xml. On the webserver itself, I used the watch command to provide stats that auto update: ‘watch -n 30 “cat /tmp/results | sort | uniq | head”‘. The results had their score first, with lowest being the best like golf.

My favorite webpage I have viewed today has been the Arch Linux July Screenshots thread: http://bbs.archlinux.org/viewtopic.php?id=75154

Tags: ,

Programming in C: Converting an Integer to Binary (int to bin)

Engineyard Competition
Recently I saw on some news site a competition to calculate SHA1 collisions. Naturally, I tried it out, but used Python, my preferred language. Unfortunately, my Python implementation was significantly slower than others using C (which was smoot since both C and Python implementations were completely dwarfed by CUDA implementations). I entered the competition after it was already in progress, and managed to get a pretty good score, but that was with an insane amount of computers crunching it.

So that leads me back to the topic of this post. Convinced that I had more hardware than anyone else in the competition, and that even though I started with less than 12 hours to beat everyone, I was pretty sure I should have won (before I saw the CUDA performance). So I started rewriting my hastily written Python program in C. The competition was already over, so this was purely for self improvement.

A Little Rusty
My first programming language was C, and I was an expert in it when I was 13 or so (probably 1992). Since then, my attention was driven towards Python and PHP, as the defacto languages of the web. This made me realize exactly how much I had forgotten about plain old C (my C++ was not horrible, atleast I could still understand C++ code written by decent programmers). Anyhow, part of the competition included taking numbers and comparing how much each of the 160 bits varied (ie 1011 and 1001 differ by 1 bit, 1100 and 1111 differ by 2 bits etc). So I wanted to make a C function that converts any arbitrary integer to a binary string (which really wasn’t the goal of the exercise, as the hashes were in hex, but it made me try and remember how to convert an integer as it had been so long).

Conclusion to a Very Boring Story
So I ended up writing an int2binary function and a program that converts them and prints them out. It will break with very large number, but I included a function to estimate it’s size (always going to the next number divisible by 4 as I like that padding), so linking that to a malloc would be trivial. I don’t suggest this is run anywhere as it hasn’t been really tested or code reviewed, but I like my answer better than most of the answers I found on the net, so I will share it with you, and hope to get a higher google score than the ones I don’t like:

int2bin.c

#include <stdio.h>

void int2binary(unsigned int n, char *buffer, unsigned int buffer_size);
unsigned int buffer_sizer(unsigned int n);

int main(int argc, char **argv)
{
        char buffer[2048];
        unsigned int binaryNumbers[9] = { 1, 4, 6, 8, 10, 55, 123, 2000, 2048 };
        unsigned int i = 0;

        for(i = 0; i < 9; i++)
        {
                int2binary(binaryNumbers[i], buffer, (buffer_sizer(binaryNumbers[i]) + 1));
                printf("%4u: %s\n", binaryNumbers[i], buffer);
        }

        return 0;
}

void int2binary(unsigned int n, char *buffer, unsigned int buffer_size)
{
        unsigned int i = (buffer_size - 1);
        buffer[i] = '\0';

        while(i > 0)
        {
                if(n & 0x01)
                {
                        buffer[--i] = '1';
                } else
                {
                        buffer[--i] = '0';
                }

                n >>= 1;
        }
}

unsigned int buffer_sizer(unsigned int n)
{
        unsigned int x = 1;
        unsigned int i = 0;
        unsigned int lastFound = 0;

        // Takes care of n = 0, and we dont need to loop for 1st case
        if(n < 16)
        {
                return 4;
        }

        while(1)
        {
                i++;
                if(n / x)
                {
                        lastFound = i;
                } else
                {
                        break;
                }

                x <<= 1;
        }

        // Not a multiple of 4, how much do we need to add to it
        if(lastFound % 4)
        {
                lastFound += (4 - (lastFound % 4));
        }

        return lastFound;
}

As you can see I did a pretty poor job commenting it and naming variables, basically though in int2binary we need to start from the right hand side and work our way left, so we start variable i at the end of the string and work our way back. We use & to compare variable n and 0×01 (which means that if the rightmost position has a 1, we add a 1 to the buffer, else add a 0 to the buffer).

For buffer sizer, this just tried to take a guess at how long that string should be. If the integer is less than 16 it will always be 4, so no need to do anything else (I was in recursive mode for a different algorithm so I was cutting things off at beginning mode). Otherwise we loop through, and try to divide variable n by variable x. Each time it is ran, variable x is shifted to the left, which has the effect of squaring the number. This is also super fast on the cpu.

For people not in C development mode, didn’t make a make file, but to compile it:

gcc int2bin.c

Output should be:

[sharms@sparrow ~]$ ./a.out
   1: 0001
   4: 0100
   6: 0110
   8: 1000
  10: 1010
  55: 00110111
 123: 01111011
2000: 011111010000
2048: 100000000000

Annoying people with code: A gentle introduction to C# and Mono Part 3: Creating a GUI (Graphical) Mono / C# Program

In this post we will cover how to make a basic GUI application using Mono along with Monodevelop. If you haven’t already, you will want to read part 1 and part 2 of this series before this article.

Monodevelop
Monodevelop is the tool we are using as our editor. This is Linux’s equivalent of Visual Studio. It can be used to code, debug and design your application, and can link in with revision control systems. This article was written using version 2.0, if your version is not 2.0 you may see slight differences.

Create your project
After starting Monodevelop, go to File -> New -> Solution. You will be presented with a prompt asking which kind of solution we are starting. Click on C# -> Gtk# 2.0 Project as shown below:

new-solution

Read the rest of this entry »

Tags: , , ,

MMap to null

I was reading an lwn article about an exploit: http://lwn.net/Articles/341773/

Being that I am writing posts this week about programming, and about my Fedora run down, thought people might find this interesting.

I wrote a little test code that fails on Ubuntu but works on Fedora 11 (based off lwn post):

#include <stdio.h>
#include <sys/mman.h>

int main(int argc, char **argv)
{
    // Try to write to memory location 0
    void *mem;
    mem = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);

    if(mem != NULL)
    {
        printf("Could not write to memory position 0\n");
    } else
    {
        printf("We can write to memory location 0\n");
    }

    sprintf((char *) mem, " This is a test\n");
    printf("Memory contents: %s\n", (char *)(mem + (sizeof(char))));
    return 0;
}

Fedora 11 results:

./a.out
We can write to memory location 0
Memory contents: This is a test

Ubuntu 9.04 results:

./a.out
Could not write to memory position 0
Segmentation fault

What does this mean?
As far as I can understand it, userspace programs segfault when trying to access data in the NULL (or 0) memory region. The kernel does not have this limitation. The author of the exploit said this is because GCC optimises out the null check. So if there is kernel code which references a pointer to 0, then you can have it run whatever you want. And in atleast 2.6.30, there is kernel code that does that.

Ubuntu does not let the userspace programs write to 0, but in F11 you can. Interesting stuff.

Tags: , ,

Annoying people with code: A gentle introduction to C# and Mono Part 2 – Data Types

In part one of this series we covered making a basic “Hello World” plugin in Mono / C#. Today we are going to dive into different types of data, and how to define them. Show me part 1! Also if you thought part 1 was ugly, I added a new javascript syntax highlighter so I recommend you read it on my blog instead of planet.ubuntu.com.

Variables
A variable is something that stores a value. In different programming languages, these are handled differently. C# is called a static / strongly typed language as you have to tell it what variables are. To explain, I will use python in comparison:

Python Example

x = "This is a string"
y = 5

As you can see, we didn’t have to tell Python anything about what type of variables they are. In C# / Mono, we would do this like this:

C# Example

string x = "This is a string";
int y = 5;

I will tell you, after my years of development, that I program a ton of stuff in PHP / Python today. One of the worst and best parts of these languages is that they are dynamically typed. The fact that C# requires variables to be declared with a type before using makes life easier for testing and reading. There is a stack overflow post on what static typing gives you: Read the stack overflow post.

Variable Naming
Before we continue, you will notice in the top example I used the variable names x and y (to illustrate that the variable names really mean nothing to the compiler / interpreter. If I named them myString you might have been confused). If you do this in real life, I will find you and give you a thorough verbal lashing. I generally use camel case to name variables. Just do not abbreviate them as it really serves no purpose, and makes a lot of pain for people besides yourself maintaining code.

You can read about camel case at wikipedia.

The Source
For this program, I copied our HelloWorld.cs, and just modified it. I also have a modified makefile.

dataTypes.cs

using System;

// We create a class to contain it
class dataTypes
{
    static void Main(string[] args)
    {
        // The type 'bool' is always true or false
        bool isMonoTheOnlyPatentRisk = false;

        // To check if it is true or false in program code,
        // we can use an 'if statement'
        if(isMonoTheOnlyPatentRisk)
        {
            Console.WriteLine("isMonoTheOnlyPatentRisk was set to true");
        } else
        {
            Console.WriteLine("isMonoTheOnlyPatentRisk was set to false");
        }

        // Note how I named the variables -
        // lowercaseFirstThenCapitalizeEachWorld
        // this is called camel case -- I first read about it
        // in a Charles Petzold book, but I am sure it
        // probably didn't originate there.  Either way its
        // how I code, I recommend you name variables
        // as descriptive as possible.

        // Note to only kids:  You will have the initial
        // reaction of saying: Why should I name stuff long?
        // That takes time and is stupid!
        // I used to be you 10 years ago, the problem is if
        // you need to use your code in several years of not
        // using it.  Your time will be completely wasted
        // figuring out what the heck is going on, despite
        // seeming so intuitive / easy to understand when you
        // did it

        // Integers store numbers
        int ourNumber = 5;

        // In most langauges we can evaluate these in
        // 'if statements' like bools above, but if we try in
        // C# this will fail.  In C# a bool can only be 0 for
        // false, 1 for true, or we can use the
        // Convert.ToBoolean function like this:
        if(Convert.ToBoolean(ourNumber))
        {
            Console.WriteLine("ourNumber was not 0!");
        } else
        {
            Console.WriteLine("ourNumber was 0");
        }

        // Lets see if we can compare this number
        if(ourNumber > 4)
        {
            Console.WriteLine("ourNumber was greater than 4");
        } else
        {
            Console.WriteLine("ourNumber was less than or equal to 4");
        }

        // Make a string, and output it
        string helloWorldVariable = "Hello World!";
        Console.WriteLine(helloWorldVariable);
    }
}

Makefile

COMPILER=gmcs

all: helloWorld.exe dataTypes.exe

helloWorld.exe: helloWorld.cs
	$(COMPILER) helloWorld.cs

dataTypes.exe: dataTypes.cs
	$(COMPILER) dataTypes.cs
clean:
	rm -f helloWorld.exe dataTypes.exe

.PHONY: all clean

Output from our terminal

[sharms@sparrow mono]$ make
gmcs helloWorld.cs
gmcs dataTypes.cs
[sharms@sparrow mono]$ mono dataTypes.exe
isMonoTheOnlyPatentRisk was set to false
ourNumber was not 0!
ourNumber was greater than 4
Hello World!

Bonus Tip
Did you notice in the first part the way I named the bool? If you are programming, generally, your bool’s should start with ‘is’. This becomes very important once you start making complex ‘if statements’. Trust me on this one, it’s a habit you want.

Tags: , , ,

Annoying people with code: A gentle introduction to C# and Mono

This is meant to be an introduction to C# and Mono. First, we need to download the mono runtime and compiler:

sudo apt-get install monodevelop mono-devel

For the purpose of this post, we are just going to create a simple program which prints out the text “Hello World”. Go ahead and make a directory for our project so we don’t make our home directory entirely too messy. I just make a directory called ‘mono’ under /home/sharms/mono for the duration of this post.

Go ahead and create a file called ‘helloWorld.cs’, and paste the following:

// Declare which namespace we want to use.
// This allows us to use Console.WriteLine
// instead of System.Console.WriteLine
// Basically like a python 'import' statement, or a
// php 'include' statement
using System;

// We create a class to contain it
class HelloWorld
{
    // Must have a main function as this is what is
    // first called when executing this
    static void Main()
    {
         // C# requires ; after statements
         // Actually output "Hello World"
        Console.WriteLine("Hello World");
    }
}

To compile it, run:

gmcs helloWorld.cs

This will generate a file called ‘helloWorld.exe’. To run it, type:

mono helloWorld.exe
Hello World

And there you have it. I also stumbled on a Makefile tutorial today, so we can make a simple make file (I won’t describe all of this, just look at how it’s used). The tutorial is at: http://www.wlug.org.nz/MakefileHowto

[sharms@sparrow mono]$ cat Makefile
COMPILER=gmcs

all: helloWorld.exe

helloWorld.exe: helloWorld.cs
	$(COMPILER) helloWorld.cs

clean:
	rm -f helloWorld.exe

.PHONY: all clean
[sharms@sparrow mono]$ make
gmcs helloWorld.cs
[sharms@sparrow mono]$ make clean
rm -f helloWorld.exe

Tags: , ,

Fedora 11 vs. Ubuntu 9.04

Put Fedora 11 on my laptop just out of boredom, some notes:

  • Fedora 11 SELinux by default: Cool but confusing
  • Fedora 11 repositories: Better selection than previous releases, still not as many choices as Ubuntu
  • Ubuntu still wins on the default menu organization for new users (just a bit easier to navigate)
  • Fedora bootup vs. Ubuntu bootup is about a wash, they both look good and are fast
  • Default themes: Neither will win a competition on looks, Linux Mint is much better looking than both
  • Yum vs. Apt: Yum was fast, but a lot of 404′s on the repositories (which is more of an issue of Fedora’s mirror infrastructure)
  • PPAs vs. ???: This is where Fedora appears (correct me if I am wrong) to have absolutely no answer to OpenSUSE and Ubuntu. In Ubuntu we can get up to date packages that were not yet officially released using PPAs. OpenSUSE users can download packages from the build service. Fedora really has nothing this fun (I am aware OpenSUSE can build Fedora packages, but the selection is not even close).
  • Support: Fedora is a distro that is on the cutting edge. It will never compete in support, but this is intentional.
  • Community: Ubuntu community is simply the biggest Linux community on the internet. Nobody is even in the same ball park. This also means that Ubuntu has much more “noise” than Fedora (ie people who contribute nothing and are generally factually inaccurate). Experts may like Fedora more because of the lack of this noise.

So if you are looking to try out Fedora, I don’t think you will gain or miss much. Personally I am going to put Ubuntu back on as I really love software from PPAs, and I love using apt just out of habit. But hope that helps someone who wonders what the differences are or what they are missing.

I have added a screenshot which is the default screen with Gnome-Do with docky theme, but this obviously works in Ubuntu also:

Fedora11

Tags: , ,

Connecting to Ubuntu from Windows

Recently I needed to connect to a Ubuntu box from a Windows machine because FGLRX just would not work with 3 monitors in surround view. Fortunately using a few tools made it much easier to interface with my Ubuntu box again.

Assumptions

  • Ubuntu system named ubuntubox with ip 192.168.1.2
  • Windows system named windowsbox with ip 192.168.1.3

Remote Access

For those of us who are comfortable with the command line, ssh is the only way to go. To make sure you have ssh installed, you can run (on your Ubuntu box):

sudo apt-get install ssh

This service should automatically start a boot up, and will allow you to open a terminal to access the command prompt. Now to access our ssh server, we need to download a ssh client for Windows. Look no further than PuTTY, which is free, open source, and easy to use. There are a few programs PuTTY provides, we just need putty.exe and optionally plink.exe (for next post in this series).

These can be downloaded at the PuTTY download page.

putty

Once downloaded, you can put putty.exe pretty much anywhere you can remember it. I just left it on my desktop and dragged the shortcut to my quick-launch bar. Double click putty.exe, and in the dialog box labeled “Host Name” go ahead and enter your Ubuntu box’s IP address.

Click “Open” when you are ready to connect, and it will prompt you for a username and password. You should already have a username and password from your Ubuntu install.

putty2

If you want your prompt to be colorful, you need to type ‘nano .bashrc’ at the prompt, and scroll down to the line that looks like “#force_color_prompt=yes” and remove the “#” character. After logging in it should now have colors. If you want other cool tips for your shell, I suggest you check out the Ubuntu Wiki as I wrote up a specification on making it more usable: Enhanced Bash.

At this point you should have remove access to your Ubuntu system.

Sharing Files

We need a good way to share files between these two systems. A lot of people will suggest Samba / CIFS (which is really a technical way of just saying Windows File Sharing). I think that Samba / CIFS lacks any technical merit, and is harder to configure. I am going to show you two ways of sharing files, both which use SSH.

WinSCP

WinSCP is basically CuteFTP or whatever FTP program you grew up with, but instead it works with SSH. It is incredibly easy to use. Go ahead and download WinSCP from their website. You can download either the portable executable (which means you can run it from anywhere without installing) or the full installer. Either one will work for our purposes.

The download page is at: http://winscp.net/eng/download.php. To connect from Windows to our Ubuntu system, go ahead and install the program (or not if you downloaded the portable version). Once you start it up, it should look like this:

winscp

Go ahead and fill in the IP address for your system. It is going to say it doesn’t know it’s key yet, go ahead and click Yes there. You will now see your home directory on your Ubuntu box and your Windows box. You can now drag files back and forth, and better yet, encrypted.

Something More?

As a programmer, one thing I need is a great editor. I happen to use VIM to code everything I make. VIM through SSH is ok, but one problem I have is I can’t use any of the really cool color schemes out there, as PuTTY is limited to 256 colors. There are some plugins out there which are supposed to convert the color schemes, but none works reliably for me. Luckily, GVIM is available for windows, so I went to their download page: http://www.vim.org/download.php and downloaded / installed it.

This was great, but for Django development I really wanted to edit the files in place, via a shared folder, instead of using WinSCP every 10 seconds (I use test driven development, as in I may change a file 5 times a minute). How do we go about using our SSH server as a shared folder under Windows? We can use the free / open source program Dokan.

Visit their download page, and retrieve the “Dokan library” and “Dokan SSHFS”. Their download page is at http://dokan-dev.net/en/download/. Install the Dokan Library first. After installing that, install Dokan SSHFS. Once installed, Dokan SSHFS will appear under your start menu. Go ahead and run it:

dokan

This should be a familiar pattern by now. Go ahead and set the “Host” file to the ip of your Ubuntu system. You will also need to provide a username and password. In the screenshot above you can see that my SSH server will be mounted as my N: drive now.

With SSHFS up and running, I can now edit files directly on my Ubuntu server. Combining that with GVIM, I can use an editor that provides excellent syntax highlighting:

gvim

In this screen shot I am using the Wombat theme for GVIM, and the Microsoft font Consolas. Consolas is freely available for Windows XP if you download the Office 2007 compatibility pack, or PowerPoint Viewer.

Tags: , ,