C# example to load Doom WAD file

using System;
using System.IO;
namespace waddy
{
class Program
{
static string GetString(byte[] arr)
{
return System.Text.ASCIIEncoding.ASCII.GetString(arr);
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
if (File.Exists("DOOM.WAD"))
{
using (BinaryReader reader = new BinaryReader(File.Open("DOOM.WAD", FileMode.Open)))
{
string name = GetString(reader.ReadBytes(4));
int numberOfLumps = reader.ReadInt32();
int folderPosition = reader.ReadInt32();
reader.BaseStream.Seek(folderPosition, SeekOrigin.Begin);
Console.WriteLine(name);
Console.WriteLine("Contains " + numberOfLumps + " lumps");
int count = 0;
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
reader.BaseStream.Seek(folderPosition + (16 * count), SeekOrigin.Begin);
int filePos = reader.ReadInt32();
int fileSize = reader.ReadInt32();
string lumpName = GetString(reader.ReadBytes(8));
//Console.WriteLine("Next lump pos is " + filePos);
//Console.WriteLine("Next lump size is " + fileSize);
Console.WriteLine("Next lump name is " + lumpName);
//reader.BaseStream.Seek(filePos + 12, SeekOrigin.Begin);
count++;
}
}
}
}
}
}
view raw WadReader.cs hosted with ❤ by GitHub

Example of bouncing ball

var gameScreen = document.getElementById("gameScreen");
var context = gameScreen.getContext("2d");
context.fillStyle = "#FFFFFF";
context.font = "8px Sans-Serif";
context.fillText("Pong!", 10, 10);
var ball = {
x : 50,
y : 50,
width : 5,
height : 5,
x_direction : 1,
y_direction : 1
};
var previousBall = ball;
var boundary = {
x1: 20,
x2: 200,
y1: 20,
y2: 120
};
context.fillStyle = "#FF00FF";
context.strokeStyle = "#FF00FF";
context.strokeRect(boundary.x1, boundary.y1, boundary.x2 boundary.x1, boundary.y2 boundary.y1);
var timer = setInterval(updateGameScreen, 10);
function updateGameScreen() {
context.clearRect(previousBall.x, previousBall.y, previousBall.width, previousBall.height);
previousBall = ball;
if (ball.x_direction === 1) {
ball.x += 2;
if (ball.x_direction === 1 && ball.x > boundary.x2 10) {
ball.x_direction = 1;
}
}
if (ball.x_direction === 1){
ball.x -= 2;
if (ball.x_direction === 1 && ball.x < boundary.x1 + 10) {
ball.x_direction = 1;
}
}
if (ball.y_direction === 1) {
ball.y++;
if (ball.y_direction === 1 && ball.y > boundary.y2 7) {
ball.y_direction = 1;
}
}
if (ball.y_direction === 1){
ball.y;
if (ball.y_direction === 1 && ball.y < boundary.y1 + 7) {
ball.y_direction = 1;
}
}
context.fillRect(ball.x, ball.y, ball.width, ball.height);
}

Learning to use AES encryption

I’ve started working on a simple credentials manager app that runs on the console and I wanted the credentials that are kept on file to be encrypted for obvious reasons. The app is written in C++ and I needed to find and work with an appropriate encryption library.

I’ve tried using two libraries: OpenSSL and Tiny-AES-C. OpenSSL is meant to be a hardened production ready library and it’s trusted by many. Using the libCrypto API that comes with OpenSSL proved to be a challenge to get working with though. I guess with such a powerful library you need to understand all setup and config it provides and I didn’t have the time and energy to dig too deep on that.

Giving up on OpenSSL, I tried my luck with Tiny-AES-C as it’s a far simpler C library that implements just the AES encryption algorithm. AES seems to be the “most” secure algorithm at this time (2020) so seems like a good option to go with. You can find the Tiny-AES-C project on GitHub: https://github.com/kokke/tiny-AES-c. You’d need to compile the library to use it in your project. Reading the Makefile in the project indicates various build options one of which is to build the lib target:

Tiny-AES-C Makefile: target for building the static library

Using Tiny-AES-C took an attempt or two but I’ve managed to create a sample app that handles the following:

  • Padding plain text to block length of 16
  • Encrypting plain text
  • Decrypting plain text
  • Identifying and removing padding from decrypted text

My example Tiny-AES-C usage project can be found here https://github.com/AdhirRamjiawan/Tiny-AES-C-Example

Some considerations to have after getting my example app working:

  • You need to ensure that the buffer size used for encryption/decryption is a multiple of 16 else you’ll get strange buffer overrun issues.
  • With AES CBC algorithm you need to employ some padding mechanism. Padding should be done to ensure block size of 16, so the total length of the text to be encrypted should be a multiple of 16.
  • The Key and IV (Initial Vector) parameters should NOT be kept in your code. I’ve kept it in the example app as a frame of reference going forward. Ideally you should consider using an environment variable or maybe another file containing this info.
  • Ideally the Key should be derived from a passphrase or similar (need to expand on this)
  • Ideally the IV should be generated using a psuedorandom number generator. (need to expand on this)

Quick cloning of kernel mainline

git clone –depth 1 https://github.com/torvalds/linux.git linux_mainline

using github as it’s a mirror of mainline and depending where you’re accessing the internet from, much faster.

the –depth 1 will limit the history to 1 commit back I believe. If you’d need to view more than that then you can probably use

git log — [filename]

https://stackoverflow.com/questions/278192/view-the-change-history-of-a-file-using-git-versioning

Quake 2 tools

Over the past few weeks (perhaps few months) I’ve been trying to create a few Quake 2 tools that can run on the browser from scratch. I’ve started working with a Pak extractor tool that should allow you to load a pak archive, page/search through all items, add/remove items from the archive and also allow you to preview each of the content types in the pak. After some trial and error I’ve managed to extract the Wav audio lumps and play it through the HTML 5 audio api. Now to get a bit more adventurous I’ve started looking into the Wal and Pcx lumps used for textures in Quake 2. Wal is a proprietary format created by Id software for the game and Pcx was a widely used format back when VGA graphics was popular. My attempt to extract Pcx image data and display it on the HTML canvas has been challenging. There is a lot of info online relating to Pcx file format, and in some cases how to attempt to read the data. In future posts I’ll try to document the methods I’ve used to load each of the lump formats.

Starting my journey in Spanish

Growing up I’ve been aware that there are many different languages spoken by people around my town and the rest of the world. I grew up in a household that spoke English predominantly. Any words spoken of another language would generally be colloquial in nature. I’ve even spent twelve years of my life learning isiZulu in school yet I’ve not been able to stick on it.

A few years ago I found employment in the western part of Southern Africa called Stellenbosch in which the majority of the people in the town spoke Afrikaans. I’ve spent two years at Stellenbosch having to navigate my way around struggling to pronounce names of people and places.Luckily for me the people in Stellenbosch have been extremely welcoming and accommodating and spoke to me in English whenever I couldn’t understand the Afrikaans being used.

After having these experiences, I found myself very interested in wanting to learn new languages. If you do simple research into language learning, you will find various articles on how understanding multiple languages increases your ability to think, be more creative and have a greater appreciation for the people of that language and of course the culture they share. Recently I’ve also come to realise that there’s many career opportunities that can arise by understanding more than one language. I now certainly believe that everyone should be bilingual at the very least.

So getting started in picking a new language to learn isn’t really easy. There’s many factors to consider:

  1. Which communities speak/read the language?
  2. How much learning resources are available for the language?
  3. How much effort/time is required in learning the language?
  4. What are the opportunities for you to make use of the language you’d like to learn?

The above four questions were some I needed to answer before choosing a language to learn. I found myself interested by other languages such as Mandarin Chinese, Arabic, German, Afrikaans, isiZulu, Hindi, Kannada, Russian and a few others. I still remember a few words from each of these languages, however at the point of each of these languages I had to deeply immerse myself in the language and culture. I tried to learn each of their alphabet, watch movies, listen to songs and read news in each of these languages. One of the biggest lessons learned  is that you need to try your very best to develop persistence or really just a habit of involving the language in some part of your life everyday.

I’ve finally settled down on learning Spanish as it’s an easier language to get started with and there’s a lots of resources and opportunities available for it. The majority of the world has been influenced by Spanish tradition and culture. From music, cartoons, movies, songs, food and clothing, their influence is evident. Having a career in the technology space has certainly pushed me in wanting to learn Spanish more than any other language.

For the last month or two, I’ve been actively making use of an app called Duolingo. You can find the app here: https://www.duolingo.com. I’ve really enjoying using this app. It has many cool features and most importantly it’s free! I love free things and Duolingo has found a new evangelist in me. The structure, content and repetition the app provides is really well thought off. I cannot say that you’ll be fluent or master a language by using the app, but it will give a fantastic starting point in learning one of the languages it has.

I can only hope that my interest in Spanish grows and that I will eventually become fluent in it. I also wish for you to start learning an additional language, and if you do, I wish you all the very best!

 

TypeScript – Method Overloading


export class HtmlElementFactory {
// method overloading – sort off… pretty weird
static CreateGeneric(elementName: string): HTMLElement;
static CreateGeneric(elementName: string, elementId: string,
innerHTML:string,attributes: Array<HtmlElementAttributes>) : HTMLElement;
static CreateGeneric(elementName: string, elementId?: string,
innerHTML?:string, attributes?: Array<HtmlElementAttributes>) : HTMLElement {
var htmlElement = document.createElement(elementName);
htmlElement.id = (elementId) ? elementId : "";
htmlElement.innerHTML = (innerHTML) ? innerHTML : "";
if (attributes) {
for (let attr of attributes)
htmlElement.setAttribute(attr.key, attr.value);
}
return htmlElement;
}

Shallow cloning git repos

Just making a note of something that might be very useful going forward. I needed to use `​git clone –depth 10 ` to clone a repo but only get the last 10 commits of it.

It helped me cloning the linux repo many years back on a horrible internet connection. This essentially doesn’t clone the entire repo’s history, however, it would clone all files and history relevant to the last 10 commits.