Archive

Archive for August, 2009

Small pseudo-assembler interpreter

August 30th, 2009 Wesley 2 comments

I had to develop a pseudo-assembler interpreter for the course Microprocessing.

Since it was just lying around on my hard drive I figured I could just as well put it on-line. It contains a few things that might be interesting to developers:

  • Using a C library in C++/Qt applications and translating C function callbacks into Qt signals
  • Implementation of virtual static and virtual dynamic memory
  • Converting between virtual signed and unsigned values (system independent)
  • Saving data and instructions in same virtual memory (Von Neumann architecture)

More information (and source code) is available here: http://wesley.vidiqatch.org/files/qpasm/

VN:F [1.6.3_896]
Rating: +1 (from 1 vote)
Categories: C, C++, Open Source, Programming, Qt, School

Back in the Dutch Ubuntu LoCoTeam

August 20th, 2009 Wesley 2 comments

Back In 2006 I was active for nearly two years in the Dutch Ubuntu LoCoTeam as site/forum administrator and release party organiser. Due to time constraints (among many other factors) my participation as LoCoTeam member steadily declined. But yesterday I became moderator of the official Dutch Ubuntu forums again. So far the community response (by e-mail) has been wonderful. It feels kind of good to know that a lot of people thought you did a great job back then and that they are happy with your return :)

I hope I can make enough time to actively help out the LoCoTeam – because there’s a lot of other stuff to do as well – and I can only do my best. The reason for my renewed participation is that I want to make a difference instead of complaining about some of the problems I was seeing. Of course some problems are to be expected: Ubuntu-NL has grown considerably. When I think back of my first days as forum administrator (about 2 years ago) I could easily read all posts myself. There were about 600 registered forum users in total, but today there are over 17.000!

Oh well. I hope I can make a difference.

VN:F [1.6.3_896]
Rating: 0 (from 0 votes)
Categories: Linux, Ubuntu

Overriding dynamic library calls (function interposition)

August 18th, 2009 Wesley 7 comments

About function interposition

I was wondering how I could override dynamic library calls in Linux, and I came across this technique known as function interposition. It is a powerful technique that allows you to override dynamic library calls. It might sound dull, but it can be very, very useful. There are some memory trace tools that make use of this technique to work, but perhaps a cooler example is the OpenGL capture system which was created by nullkey: it can capture OpenGL frames by overriding certain OpenGL functions. Another example are cheat tools (wallhacks, aimbots) which also make use of this technique a lot.

Some background

While Googling (did I spell that right?) I came across this recent blog article which explains the background very well. I will quote it here:

First, some background. When a program that uses dynamic libraries is compiled, a list of undefined symbols is included in the binary, along with a list of libraries the program is linked with. There is no correspondence between the symbols and the libraries; the two lists just tell the loader which libraries to load and which symbols need to be resolved. At runtime, each symbol is resolved using the first library that provides it. This means that if we can get a library containing our wrapper functions to load before other libraries, the undefined symbols in the program will be resolved to our wrappers instead of the real functions.

So if we create a custom shared library which overrides some of the functions of the original library, our functions will be called instead of those of the original library.

How to do it

  • Write new functions which override existing functions
  • Compile the written code to a dynamic library that is linked to the dynamic linking interface library
  • Use the LD_PRELOAD environment variable when running an application to preload your custom library before all other dynamic libraries

The article by Jay Conrod has an example which shows you the basic implementation of a simple memory allocation tracer.

I have also cooked up an example myself. Because I’ve been busy learning more about OpenGL, I thought that it shouldn’t be too hard to create a wallhack for one of my favourite games: Soldier of Fortune 2 running in Wine. Just for testing purposes of course! I am no cheater :D It turned out to be relatively simple, although my first tries weren’t so very successful:

  • Epic fail – At least I know my library is used now.
  • Partial success – Disabling depth testing completely was only a partial success.
  • Success – A debugger can tell that players are drawn using glDrawElements(). By knowing the number of elements for each character, we can disable depth testing selectively.
Soldier of Fortune 2 wallhack example

Soldier of Fortune 2 wallhack example

For those of you who are interested in the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
    Simple wallhack example for Soldier of Fortune 2 (Wine) in Linux using function interposition

    This code snippet was written by Wesley Stessens (wesley@ubuntu.com)
    It is released in the Public Domain.

    Compilation: gcc -Wall -ansi -pedantic -shared -ldl -fPIC glhack.c -o glhack.so
    Usage: LD_PRELOAD=glhack.so wine game.exe
*/


#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdint.h>
#include <GL/gl.h>

/* Override the glDrawElements function */
GLAPI void GLAPIENTRY glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices) {
    /* Store the actual function in a static function pointer */
    static void (*glDrawElements_)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices) = NULL;
    if (!glDrawElements_) {
        glDrawElements_ = (void(*)())(intptr_t)dlsym(RTLD_NEXT, "glDrawElements");
        puts("GLHack: glDrawElements call has been overridden");
    }

    /* Disable depth testing if the number of elements to draw is one of the following, which means a player is being drawn */
    /* To avoid abuse of this code by cheaters, I have changed all count constants below to VALUEX */
    if (count == VALUE1 || count == VALUE2 || count == VALUE3 || count == VALUE4)
        glDisable(GL_DEPTH_TEST);
    else
        glEnable(GL_DEPTH_TEST);
    glDrawElements_(mode, count, type, indices);
}

Interesting thought about multiplayer cheats and Wine

If anti-cheat tools would perform a sanity check of the OpenGL or DirectX DLL, they would only find the virtual DLL’s when a game is run in Wine, right? I’m wondering whether this sort of cheats can be made undetectable then. In a way I hope not, because cheaters are very annoying when you’re playing a game, but on the other hand, it would be an amazing technological achievement. Anyway, anti-cheat tools like PunkBuster don’t even work with Wine at the moment, so it might be a non-issue. What are your thoughts?

VN:F [1.6.3_896]
Rating: +1 (from 1 vote)
Categories: C, Linux, OpenGL, Programming

Code snippets

August 12th, 2009 Wesley No comments

I’ve created a new page where I will upload small code snippets that I created and which might be useful for some. Most code snippets are released in the Public Domain. The code snippets might not have the highest quality, so feel free to propose improvements to them if you feel like it.

PS: I have moved the “safe replacement for gets” blogpost to the code snippets page and have moved over all the comments as well.

VN:F [1.6.3_896]
Rating: +3 (from 3 votes)
Categories: Blog, Programming

100 visits by 50 unique visitors

August 8th, 2009 Wesley 9 comments

This blog has been back on line for 5 days now. I am using Google Analytics to obtain statistics about the visitors of this website. Don’t be scared though; no personal information is collected. I thought it would be nice to discuss some of the statistics, as I have now had 100 visits by 50 unique visitors in total.

I should note that at this early age of my blog the results aren’t very trustworthy, but they should give you a rough sketch anyway.

Webbrowsers

Visitors of this blog seem to prefer Firefox (56.44%), although there have been a lot of visits by the Safari (28.71%) webbrowser as well. Chrome and Mozilla are a bit less popular among my visitors, both have a share of 5.94% individually. The most shocking however, is the low amount of visits by the Internet Explorer webbrowser. Less than 2% of visits to this blog have been made using this webbrowser. I can only applaud that. Okay, perhaps the results aren’t that shocking after all, knowing that this blog is primarily aimed at Linux users and/or developers.

Operating systems

44.55% of all visits were made by people using the Linux operating system. This is not a big surprise, as my blog is primarily aimed at Linux users. More surprisingly however: an astonishing 31.68% of all visits were made by people using the Mac OS X operating system. A possible explanation is that a lot of my blogposts are about cross-platform development and that a lot of the posts about Linux are applicable to Mac OS X as well (both operating systems are fully POSIX-compliant). Only 22.77% of my visitors use the Windows operating system.

PS: I have created a Request article page. If you are interested in something about Linux or programming or anything else that I might find interesting to write about, just tell me by posting a comment on that page!

VN:F [1.6.3_896]
Rating: 0 (from 0 votes)
Categories: Blog

NeHe OpenGL lessons in Qt – Chapter 4

August 8th, 2009 Wesley No comments

As promised, the fourth chapter of the NeHe OpenGL lessons ported to make use of the Qt toolkit.

Fourth chapter: fog, fonts revisited, quadrics, particle engine, triangle strips, masking

In the fourth chapter you will learn a few cooler tricks. You will learn how to create good-looking fog effects and how certain objects can easily be constructed using quadrics. But the coolest thing that you will learn is how to create a simple particle engine (during lesson 19). To end off the chapter, you will learn how you can use masking to create partial transparency using bitmap textures.

Some minor modifications were made to improve the visual appearance of some of the lessons.

Videos and source code

This video shows the fog effect (lesson 16)
This video shows what you can achieve using quadrics (lesson 18)
This video shows the very cool particle engine that you will create! (lesson 19)
This video shows which effect masking has (lesson 20)
You can download the Qt 4 source code for this chapter here.

PS: The port of chapter 5 will take a while… I will be a bit busy the coming weeks, and the first lesson of chapter 5 is HUGE, so will require a lot of time to port.
Oh, and apparently I forgot to upload the source code for the third chapter. I have uploaded it now :)

VN:F [1.6.3_896]
Rating: +8 (from 8 votes)
Categories: C++, OpenGL, Programming, Qt

NeHe OpenGL lessons in Qt – Chapter 3

August 4th, 2009 Wesley 3 comments

Here is the third chapter of the NeHe OpenGL lessons ported to make use of the Qt toolkit.

Third chapter: waving texture, display lists and a lot of fonts

The third chapter starts off with a cool looking waving flag effect. After that you will learn about display lists. The last three lessons focus on different ways of displaying fonts. It is worth noting that rendering basic text at a chosen location, or translated into the OpenGL scene is extremely easy in Qt/OpenGL thanks to QGLWidget::renderText(). For other basic text effects (such as rotated, skewed or otherwise transformed text) you could also use QPainter directly on a QGLWidget. We won’t be doing this in our examples, but I just wanted to point out that it is possible.

I should also note that I have used an extra library in lesson 14 to display the 3D text. Qt itself is not able to display 3D text and doing this in a cross-platform way yourself would be rather hard. That’s why I have used the FTGL library for this. FTGL is a very easy to use cross-platform library with the sole purpose of rendering (3D) text in OpenGL.

Videos and source code

This video shows the waving flag effect (lesson 11)
This video shows the effect of using display lists (lesson 12)
This video shows rotated 3D text (lesson 14)
You can download the Qt 4 source code for this chapter here.

PS: You can expect chapter 4 in a few days :)

VN:F [1.6.3_896]
Rating: +7 (from 7 votes)
Categories: C++, OpenGL, Programming, Qt

Resizable photo frames in Qt

August 4th, 2009 Wesley No comments

Today someone asked how to copy one image into another image in Qt. I thought it was a nice idea to write an example about how to do that, plus a few extra things. We will create a resizable photo frame from just one simple image of a photo frame! It works like this:

  • Reimplement QWidget::paintEvent() and construct a QPainter(this) in the reimplementation so we can draw on the widget
  • Load the image of the photo frame and define 4 QRect objects to define the position of the frame borders – these borders are not allowed to scale
  • Draw something which will be contained inside of the frame – for example another image using QPainter::drawPixmap() or QPainter::drawImage()
  • Generate and draw the frame bars (the pieces between the borders)
    • Make use of the QImage::mirrored() function and another QPainter to create a new pixmap which contains the frame bar plus the mirrored frame bar.
    • This will make the bar look great when the frame bar is enlarged by tiling. This method is actually a very popular one in basic photo manipulation.
  • Draw the four borders

The result looks like this:

Photo frame in native size – versus – Enlarged photo frame

The code looks like this:

photowidget.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef PHOTOWIDGET_H
#define PHOTOWIDGET_H

#include <QtGui/QWidget>

class PhotoWidget : public QWidget {
    Q_OBJECT

public:
    PhotoWidget(QWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent *event);

private:
    QPixmap createBar(Qt::Orientation orientation, const QPixmap &pixmap, const QRect &rect);
};

#endif // PHOTOWIDGET_H

photowidget.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "photowidget.h"
#include <QPainter>
#include <QPaintEvent>

#define SIZE 108

PhotoWidget::PhotoWidget(QWidget *parent) : QWidget(parent) {
    resize(400, 329);
    setWindowTitle("Photo Frame Example");
}

// This function generates a pixmap that can be used as a tiled bar.
// Note: We use a QPainter and the mirrored() function to make sure that the bar looks good when tiled.
//       This technique is often applied in basic photo manipulation.
QPixmap PhotoWidget::createBar(Qt::Orientation orientation, const QPixmap &pixmap, const QRect &rect) {
    QImage barA = pixmap.copy(rect).toImage();
    QImage barB = barA.mirrored(orientation == Qt::Horizontal ? true : false,
                                orientation == Qt::Vertical ? true : false);

    QSize size;
    size.setWidth(orientation == Qt::Horizontal ? barA.width() << 1 : barA.width());
    size.setHeight(orientation == Qt::Vertical ? barA.height() << 1 : barA.height());

    QPixmap bar(size);
    bar.fill(Qt::transparent);
    QPainter merger(&bar);

    merger.drawImage(0, 0, barA);
    if (orientation == Qt::Horizontal)
        merger.drawImage(barA.width(), 0, barB);
    else
        merger.drawImage(0, barA.height(), barB);

    return bar;
}

void PhotoWidget::paintEvent(QPaintEvent *event) {
    QPainter p(this);

    // Our frame as one full image, and an image to put in the frame
    QPixmap frame(":/img/frame.png");
    QPixmap sky(":/img/palmtree.jpg");

    // These four rectangles define the four borders of the frame
    QRect topLeft(0, 0, SIZE, SIZE);
    QRect topRight(frame.width() - SIZE, 0, SIZE, SIZE);
    QRect bottomLeft(0, frame.height() - SIZE, SIZE, SIZE);
    QRect bottomRight(frame.width() - SIZE, frame.height() - SIZE, SIZE, SIZE);

    // Draw the image first
    p.drawPixmap(QRect(40, 40, event->rect().width() - 80, event->rect().height() - 80), sky);

    // Draw the bars
    p.drawTiledPixmap(QRect(QPoint(SIZE, 0), event->rect().topRight() + QPoint(-SIZE, SIZE - 1)),
                      createBar(Qt::Horizontal, frame, QRect(QPoint(SIZE, 0), frame.rect().topRight() + QPoint(-SIZE, SIZE))));
    p.drawTiledPixmap(QRect(event->rect().bottomLeft() + QPoint(SIZE, -SIZE - 1), event->rect().bottomRight() - QPoint(SIZE, 0)),
                      createBar(Qt::Horizontal, frame, QRect(frame.rect().bottomLeft() + QPoint(SIZE, -SIZE), frame.rect().bottomRight() - QPoint(SIZE, 0))));
    p.drawTiledPixmap(QRect(QPoint(0, SIZE), event->rect().bottomLeft() + QPoint(SIZE, -SIZE)),
                      createBar(Qt::Vertical, frame, QRect(QPoint(0, SIZE), frame.rect().bottomLeft() + QPoint(SIZE, -SIZE))));
    p.drawTiledPixmap(QRect(event->rect().topRight() - QPoint(SIZE + 1, -SIZE), event->rect().bottomRight() - QPoint(0, SIZE)),
                      createBar(Qt::Vertical, frame, QRect(frame.rect().topRight() - QPoint(SIZE, -SIZE), frame.rect().bottomRight() - QPoint(0, SIZE))));

    // Draw the borders
    p.drawPixmap(QPoint(0, 0), frame, topLeft);
    p.drawPixmap(event->rect().topRight() - QPoint(SIZE, 0), frame, topRight);
    p.drawPixmap(event->rect().bottomLeft() - QPoint(0, SIZE), frame, bottomLeft);
    p.drawPixmap(event->rect().bottomRight() - QPoint(SIZE, SIZE), frame, bottomRight);
}
VN:F [1.6.3_896]
Rating: +2 (from 2 votes)
Categories: C++, Programming, Qt