Generic List in C

Introduction Lists (dynamic arrays) are yet another super useful data structure that C just doesn’t have. C arrays are great for when you can get away with a fixed size but not so fun if you need to dynamically expand because you don’t know quite how many elements you’ll need. You could use a series of reallocs and memmoves but that’s going to get old really fast. It’s also error prone and not obvious when growth is needed....

April 9, 2020 · John

String Hashtable in C

Introduction We have this amazing generic hashtable and we can put pretty much anything we want into it, but it has a few flaws. It uses void pointers and has a pretty verbose setup with that callback struct. If you’re using the same types over and over again you’ll have a lot of redundant code. There is also a much more pressing issue of void pointers. They remove type safely. It would be really bad if you passed the wrong type to a hashtable meant for another....

March 28, 2020 · John

Generic Hashtable in C

Introduction So, C doesn’t have a native hashtable object but that’s not a problem because we can use one one someone else wrote. Lack of a robust standard library is probably the biggest impoundments of working with C. It’s a real shame C doesn’t natively support hashtables because they are so versatile. You should keep this saying in mind, “when in doubt hash it out”. It works for programming and for life in general....

March 6, 2020 · John

Formatting Strings for Logging

Introduction I’ve been working on a project, and I needed to add logging to the app. I wanted a log formatting function that can take a prefix and a format string (think a sprintf-type format string but safer). This function needs to be more than just an sprintf knockoff; it also needs to prefix the date and time to the message. Oh, the messages are passed in without a new line, so we need to add that too....

February 19, 2020 · John

Validating Constant Time String Comparison In C

Introduction Awhile back I wrote a constant time string comparison function. I briefly mentioned how the compiler can optimize away some of what makes the function constant time. Specifically, the k++ counter used to balance the increment when the forward scan of s2 stops. The volatile keyword was used in the code to prevent key operations from being removed. What I neglected to go into was showing that volatile works to keep the function constant time....

January 20, 2020 · John

Python Http Server

Introduction Quite often I find that I need to serve some files for viewing in a web browser. Most recently, I needed to do this with an in progress OpenAPI document as rendered by ReDoc. All I needed was something that can serve static files. I really didn’t want to take the time to setup and configure something like Apache or Nginx. These are overkill for static files on a developer machine....

December 10, 2019 · John

Python Self Signed Cert Gen

Introduction Sometimes I need to write a simple network server to emulate an application I’m integrating with. Typically, this is ends up being a throw away Python script that allows me to easily inspect at a request and returns a basic response. It’s handy to verify what I’m sending isn’t malformed. Also, it helps to ensure my response parser is at least somewhat sane. The software I work on requires a TLS secured connection to all remote end points....

November 14, 2019 · John

Python Binary to C Header

Introduction Once again I needed to embed some text files into a C application. The right way to do this is turn the data into a byte array and compile it in. At least it’s the most portable way because some compilers have string length limitations. xxd -i is the easiest way to format the data so it can be compiled in. However, just like the last time, I don’t have access to xxd -i....

October 9, 2019 · John

Making MiniZip Easier to Use

Introduction Zip files are one of the most popular archive formats out there, and there are a lot of things you can do with them. While working with the ePub ebook format I spent a lot of time working with zip files. Thankfully, the standards committee for ePub used zip as the container format instead of designing their own. If you’ve ever done anything with Android you’ve dealt with zip files and probably don’t even realize it....

September 8, 2019 · John

Read Write File C Helpers

Introduction Reading and writing files in C isn’t as difficult as it sounds. A few simple loops are all you really need. That said, it’s nice to have a few helper functions ready to drop into a project. Before we write anything we need to think about the choice between fopen and open. The major differences are fopen is portable, part of the C standard, and unbuffered. While open is technically not portable it’s ubiquitous across *nix systems....

August 18, 2019 · John