The home of Barnaby Smith
Header

In the last post I discussed why hex editors are useful for working with binary files. I also talked about XVI32 my hex editor of choice. During this series I will be using XVI32 for examples, so if you are not on a Windows machine or if you want to use a different hex editor then you will need to adapt my examples.

So if you haven’t done so already, download XVI32. XVI32 doesn’t need installing, you can just unzip it and run it from there but you may want to copy it to a more memorable location and set up any relevant shortcuts.

In this post I’ll be taking more of a practical approach, I’ll start by talking about file signatures and then we’ll open up a few common file formats and take a look inside.

File Signatures

An extension does not make a type.

Or to put it more clearly, just because a file has the extension .png doesn’t mean there’s actually a png image inside. This is a very important lesson, since it is incredibly common for games just to give a common file format a different extension. When I was browsing the Call of Duty 4 files I realised instantly when I opened a file format up in a hex editor that it was just a zip with a different extension, meaning I could unzip it and see the files inside.

So how did I realise it was a zip at a glance? What kind of technomagery is this? Many files have a signature at the very start saying what format they’re in. It doesn’t matter what the file is called or what extension it has, if it has a signature then you have a way of identifying the file type. These are often called magic numbers because a piece of text can be represented as a sufficiently long number. In fact, any and all data can be considered just a very, very long number, but I may be straying off the point.

There are a number of very common file signatures you’ll see, including:

Signature Hex Type
MZ Executeable code (.exe)
PK 50 4B 03 04 Compressed Zip (.zip)
Rar! Compressed Rar
BM Bitmap Image (.bmp)
FF D8 FF E0 ** ** 4A 4649 46 00 JPEG Image (.jpg, .jpeg)
%PDF 25 50 44 46 PDF Document (.pdf)
‰PNG PNG Image (.png)
OggS Ogg Vorbis Media e.g. audio, video (.ogg)

For a more extensive list of file signatures check this page out.

Some Examples

Before continuing there are three examples you need to download, in the case of the images you will need to right click the link and hit “save as” or the equivalent option on your browser.

Example 1
Example 2
Example 3

Now for each of the examples, I want you to open it up in XVI32 and have a look inside.

Example 1

Once we open the first example in XVI32 we see that the file header starts with the per mil symbol followed by PNG in the text view. This file clearly is a PNG image.

Example 2

With example two, we see that the file signature is PK (named after the format’s author Phil Katz, but I often translate PK as Packed). This file is a zip as we can see in the table of signatures above.

Example 3

The final example has the file signature BM and is therefore a bitmap image. Ignore the F, that byte is actually part of a variable in the bitmap format that says how big the file is.

Example 3 In Depth

Let’s take a deeper look at the third example now that you’ve got it open. We know that the file is a bitmap image, so let’s take a look at it in Windows Photo Viewer. It’s a small image so you’ll have to zoom in.

If we open up the file in Paint, view it in Explorer or open its properties we’ll see the file is a 2 by 2 pixel image. I’ve created this small image to demonstrate the format more simply.

File Headers

In addition to the file signature, most files have a file header which includes some basic information about the file. In the case of an image this may include its dimensions and colour depth/quality. In the case of audio this may be the duration, number of channels and bit rate.

In the above image I’ve highlighted the file’s header. In the case of a Windows Bitmap the file header is 54 bytes long. To highlight a section in XVI32, select the first byte then select Edit -> Block <n> chars and type 54 in decimal mode.

You can see a couple of 02′s in the header, so a reasonable assumption would be one represents width and one represents height. We can also see an 0×18 which as a decimal is 24, so another reasonable assumption is that this is the colour depth specifying 24 bit colour. For now don’t worry about colour depth, I’ll talk about colour in a dedicated article later in this series.

After the header we have 16 bytes. Now we know there are four pixels (2×2) pixels in the image, so it’d be sensible to assume that those four pixels are represented in these 16 bytes.

Opening the file in Paint, we can use the dropper tool to pick the colour of each pixel. By going to edit colour, we can then see the colour in its red, green and blue components. You can do this manually, or use the figures I’ve shown below. I’ve also converted the values to hex for you.

Top Left

Decimal Hex
Red 34 22
Green 177 B1
Blue 76 4C

Top Right

Decimal Hex
Red 255 FF
Green 242 F2
Blue 0 00

Bottom Left

Decimal Hex
Red 255 FF
Green 127 7F
Blue 39 27

Bottom Right

Decimal Hex
Red 237 ED
Green 28 1C
Blue 36 24

Now using the hex values worked out for each colour, we can spot them in the file. We can spot each three, in reverse order displayed as Blue, Green then Red. The reason for this different ordering is something I’ll talk about in a later article. We can also see that the bottom left pixel is first, followed by the bottom right pixel, then followed by two 00 bytes. Immediately after is the top left pixel, followed by the top right pixel and two more null (00) bytes.

Ignore the two sets of two null bytes, these are due to a nuance of a the bitmap format which means that it must pad the number of bytes representing a row of pixels to a multiple of 4 (so in this case we have 6 bytes representing a row, so it adds on 2 blank bytes to reach a total of 8 bytes and therefore a multiple of 4).

Editing Data

So now that we know where the colour data is in the file, let’s try changing it.

Let’s pick the top right pixel, which is yellow. Let’s change it to blue. Right now its represented as 00 F2 FF, so since this is in Blue Green Red order rather than Red Green Blue, changing the value to FF 00 00 will be a strong blue. To edit the values select the first byte in the “00 F2 FF” sequence and make sure that it says Overwrite in the status bar. If it says Insert then tap the insert key once. The insert key toggles between Overwrite and Insert modes. Now simply type FF 00 00 on your keyboard and hit save.

Opening the file up in Windows Photo Viewer and zooming in, we now see that the top right pixel is blue.

Congratulations, you have made your first successful and practical edit in a hex editor!

In this article, I’ve talked briefly about how to identify common file types regardless of their file extension. I’ve also shown some basic hex editing in a practical example – editing a bitmap image. In the next example I’m going to go into data types which combine multiple bytes to represent larger numbers.

Before I start this article, I need to define a couple of terms:

  1. Byte – This is a data type which can store 256 discrete variations. Typically its said to have a minimum value of 0 and a maximum value of 255 (thus 256 variations including the 0). All files can be considered to just be a series of bytes.
  2. Hex or Hexadecimal – Unlike a decimal or base 10 number which only allows 10 different variations per digit and must include a another digit to include numbers which exceed that range (e.g. 8, 9, 10, 11 / 98, 99, 100, 101) a hexadecimal number stores 16 variations. After 9, the first six letters of the alphabet are used (e.g. 8, 9, A, B, C, D, E, F, 10 , 11 / FE, FF, 100, 101) .

All files are divided into two categories – plain text and binary.

Plain text files as the name suggests just contains text. They cannot contain images, sound, video or any form of text styling unless they mark it up. Examples of these files include .txt, .ini, .csv, .html, .php. These files can be opened in your system’s default plain text editor such as Notepad on Windows and will load and display fine.

Plain text files load and display fine in plain text editors

Binary files however can store a much wider range of data. Your camera photos, mp3s and videos are all binary files. Rather than limit the data storage to just text, binary files can make use of a larger range of encoding which means that we can’t view these files properly in a plain text editor. This is shown in the image below, in which I’ve tried to load a bitmap image into Notepad.

By contrast, loading a binary file in a plain text editor is not a good idea

So to view and edit binary files, we clearly need a different tool. If we know the file format then we could load the file into the relevant editor, loading images into Photoshop or Paint for example. But this is no good to us if we don’t know the file format, if there isn’t a relevant editor yet or if we want to examine the internal data structure.

Hex Editors can display and edit binary data in a very helpful and effective way. They are not limited to text characters and can be used to display and edit the full range of variations in each byte. Unlike text editors which display binary data badly and don’t support changing the value of non text data, hex editors are not hindered by these problems.

XVI32 is a freeware hex editor for Windows

My personal favourite hex editor is XVI32 which can be downloaded for free on Windows. It’s quite a lightweight and functional hex editor and while there are many hex editors out there offering a greater range of features, I like the simplicity and straightforwardness of XVI32.

In the above screen shot we can see three columns. The left and thin column is the line number displayed in hex. The number shown represents the index or offset of the first entry on that line, for example B means 11 as a decimal and if you count across the boxes in either of the other two columns you’ll see that they are also 11 boxes across. These line groupings do not exist in the actual file, this is merely just how its displayed in the editor, a bit like word wrapping text.

The middle column displays the hexadecimal view of the file, while the right column shows a ASCII or text view of the file. Each box in the hex and text views represents a byte in the file.

So why is hex useful? Why not represent the values of each byte as a decimal?

Hexadecimal numbers have the useful property that with two digits they can represent 256 discrete variations, just like a byte. So rather than use 3 digits to represent the value of each byte, we can use two digits to their full range. The minimum hex value for a byte is 0 (or 00) and the maximum hex value for a byte is FF.

To convert between decimal and hexadecimal you can use the built in calculator on Windows (or use a site like this). Depending on your version of Windows you may need to change to either scientific or programmer mode before the hex and decimal options are available. To convert a number, type it into one mode then select the other mode. For example:


In the next post, I’ll explore how to use a hex editor and look at some common data types. Before reading that post however, it would be useful to try opening a few different file types in a hex editor just to get a feel for it. It would also be very helpful to try converting a few different numbers between hex and dec.

Something I’ve been looking into recently are games that are highly addictive but extremely minimalist, a classic example of this is the now infamous (and scourge of office efficiency) Helicopter Game. It’s a procedurally generated one button game, where the player must click to lift the helicopter and let go to let the helicopter descend, avoiding both the canyon floor, ceiling and regular obstacle blocks.

I was particularly interested in the consistency and similarity of different people playing the game. I asked 11 people to record their first 10 results. The samples were aged between 19 and 25 and the majority were male. Complex conclusions should not be drawn from these results nor should they be over-analysed, however the results are indicative of the variety in all player’s scores but the similar range that they tend to share.

On the image below each colour represents a different person, the last result for each person is at the top and the first is at the bottom. The bottom axis is time measured in seconds (time = score/28).

Click to Embiggen

New Portfolio Videos Added

April 13th, 2011 | Posted by mvi in Uncategorized - (0 Comments)

Final year (and with it university) is coming to a close and last week was the final week of teaching. Only my dissertation hand-in/presentation and the AI exam remain. Last week contained the final presentation for team project (where 10 of us developed a prototype of Dark Oz – a puzzle platformer) and also the presentation for my second assignment for the games development module (principally a graphics module). Videos from each module have now been added to my portfolio page so please check out my city simulator here and Dark Oz here.

Looking ahead, I need to finish my dissertation prototype and report which is due for the start of May. My dissertation is on fluid (or fluid-like) painting of mesh textures as an art authoring tool. Rather than attempting to replace current texture painting techniques the idea is to augment and expand upon these techniques to offer another tool to artists. Previous to my dissertation I have worked on projection based approaches (projecting a ray from the mouse position in the viewport and carrying out ray-triangle intersection, finding the point of intersection in the triangle, translating that to the UV coordinate and painting texels within range). It’s been quite challenging working with volumetric/particle models to achieve the same results – especially efficiently!

HyperDesktop – It’s awesome

November 26th, 2010 | Posted by mvi in Uncategorized - (Comments Off)

As a programmer, taking screenshots of what I’m working on frequently is something I need to do. Hitting Print Screen, booting up a graphics program (usually Photoshop, due to Paint’s sucky export compression) pasting the image in, cropping it down to the area I want to show, saving it for web, booting up a new tab, going to imageshack, hitting browse, finding the image I’d saved, uploading it, copying the link (got bored of reading yet?!) is far too slow a process. I started to use a program called QuickShot a while back, which pretty much automated this process for you and uploaded the images to Imageshack. Now for whatever reason Imageshack have pulled support for that program and appear to have stopped it uploading images on their backend. Fortunately however I’ve been pointed to an alternative, entitled HyperDesktop.

HyperDesktop is a remarkably simple tool, that’s not a bad thing in this case, it’s a real strength as it’s lightweight and does exactly the jobs I need it to. Hitting Ctrl-Shift-3 will capture the entire desktop, compress it using a decent format (that you can configure) and then upload it straight to imgur.com. As soon as it’s online, a message box will pop up in the corner for a couple of seconds to notify you and the link is automatically in your clipboard, just waiting to be pasted. I’ve outlined the traditional slow process above, HyperDesktop replaces it with this process: press a shortcut, wait a couple of seconds for a popup, paste the link where you need to. The other great thing about HyperDesktop is that hitting Ctrl-Shift-4 will allow you to highlight a rectangle on screen, that rectangle will then be screenshotted and uploaded. Much easier than having to crop an image down (because Alt-PrtScn isn’t always suitable.)

When I installed HyperDesktop I had to restart the program for the uploading to start working, I don’t know whether that was just bad luck in my case or whether it’s a generalised XP issue. If that doesn’t fix it, then try restarting your whole PC.

Download HyperDesktop for free here

I’m delighted to say that I will be giving a presentation at the next meeting of XBlig-UK, for those of you scratching your head as to what that could be here’s the official description:

The XBlig-UK User Group is your chance to meet & hear from like-minded XNA indie games developers from across the UK. Not only can you pick up tips on to how to write your game, but also how to win fortune & glory (although not necessarily in that order) by publishing it to the world via XBox Live Indie Games on the Xbox 360.

The topic I’ll be covering is Considerations for Tools Development, it’s at 7pm on November 3rd in Coventry. You can find more details here. This will be the first public presentation I’ve given, so I’m slightly nervous but it should be good fun. If you’re a student in Huddersfield and fancy watching it I’ll be doing a practice presentation this week in one of the labs, so get in touch. Good thing I won that wireless mouse as I won’t have to be stood by the computer :-)

15 games, 15 minutes

October 3rd, 2010 | Posted by mvi in Uncategorized - (0 Comments)

So after reading Simon’s post about listing 15 games in 15 minutes, I decided to give it a try. The aim is to list 15 games that will always stick with you in no particular order. I feel perhaps that my list isn’t the greatest, but hey, here it is:

  1. Jezzball
  2. GTA: Vice City
  3. Dune 2000
  4. Civilization II
  5. Super Mario Kart
  6. Call of Duty 4
  7. Goldeneye 64
  8. Super Monkey Ball
  9. Halo 3
  10. Unreal Tournament 2004
  11. Pokemon Yellow
  12. GTA San Andreas: Multiplayer
  13. Syndicate
  14. Initial D
  15. Pro Evolution Soccer 2008

Jezzball was a game I played on Windows 3.11, it was simple, but great fun. If I had a touch screen phone, I’d probably buy the game for it.

GTA: Vice City was my first real introduction to GTA, I got home one day to find my brother had bought it and was playing it on the front room’s TV. I soon realised how much fun it was on the PS2 and after he bought me a copy for PC as a gift from Ibiza (yeah, I know) I got involved in the modding community and learned quite a lot about how modern games are made in the process.

Dune 2000 is another game I’ve modded, the modding community unfortunately never had the chance to mature, so my quite recent work on it has been quite appreciated. I don’t find the time to work on modding the game any more, but I’ll always remember it as the finest of the Dune RTS’s.

Civ II was my first introduction to the Civ series, back in those DOS days it was the sort of game that the series has continued – the sort of game where you look at your watch and realised 5 hours have passed. While I realise Civ V is now out, I am making sure to avoid it as I know those hours will just vanish.

Mario Kart on the SNES was great fun. While I probably played the N64 version more, I’ve still got fond memories of the game. I remember being in hospital as a kid and playing it with a bunch of other kids from the same ward. Fun times.

Explicitives fly from Chris‘s mouth just as fast as the bullets on screen, this was (and I hope still is) the hallmark of lunch at Canalside Studios when I worked there. Call of Duty 4 despite there now being two sequels has remained the lunchtime game of choice in the studio since it started, I may have earned a slight reputation for camping in that game.

Goldeneye 64, my first real FPS and of course, I was always Oddjob – the little man who’s much harder to shoot at in the game that brought the FPS genre into the mainstream on console.

Super Monkey Ball was a very fun game I used to play on a sunday for its multiplayer modes.

The summer after first year, I played Halo 3 every single night with the guys from work. It was quite scary how much we played that game, I’ve not yet played Reach, but one of my brothers is coming over this week and we’re going to give Co-Op a blast.

UT 04 was the first PC FPS I really played, I’d had goes on Doom and Quake when I was younger, but I’ve never really been much of a PC gamer, mainly due to lacking a decent PC. I’ve currently got 3 games on Steam, but I’m sure that’ll change over the next few years.

People that know me, know my hate for RPGs. However, Pokemon Yellow was my game of choice back in Primary School, I was gutted when I got to the final bosses in the game and my save corrupted.

GTA makes an appearance again in this list, but albeit in a different form. The modding community in their fiendishly clever way managed to make San Andreas (as had been previously been done to III and VC with varying levels of success) fully online multiplayer. With support for a powerful scripting language (PAWN) and up to 200 players, SA:MP is my favourite multiplayer experience of all time.

Syndicate is in the list, I’m not entirely sure why, but my online handle is derived from that game, so it must be worth a listing, right?

In the Students Union, Intial D was a great arcade racer. Combining both standard forms of racing and drifting, depending on what options you chose, we had a highly competitive time playing.

Pro Evo, working at Shorts Lifts would always see an upset face after lunchtime. As soon as it hit midday, the Xbox 360 was on and Pro Evo was fired up. An hour and three matches later, the day’s victor would claim the PES cup for his desk, while anyone that lost all three matches would be presented with the wooden spoon.

What I’m working on

September 9th, 2010 | Posted by mvi in Uncategorized - (1 Comments)

Besides the science fiction story I’m currently working on for Future Perfect, I’m also working on some code in my free time. I’ve been learning the (fantastic) Qt framework for a rather straightforward turn-based-strategy game I plan to release in a few weeks. In addition, I’ve been pondering on developing my previous idea for a batch file powered 3d engine into a demo. The idea is to create the engine and a LISP interpreter in a high level language, then convert the code to pure batch. 3d models and LISP scripts would then be loaded into the engine for it to display using unicode. There are a lot of challenges on this project, input polling for one, but I feel confident that it’s achievable. If I manage to find the time to do this batch project you’ll be able to track the updates here.