macOS USB Enumeration in C

Introduction Apple provides documentation for using IOKit, but it’s not very clear how to do a number of things. Descriptions and relationships of functions often isn’t clear. While there are examples, they tend to be lacking. The generic types IOKit uses doesn’t help either because you could have multiple different types of hardware sharing the same device object type. I was working on a project where I needed to enumerate USB devices on macOS....

December 6, 2020 · John

macOS IOHIDManager Permission Issue

Introduction The other day I was working with a USB-HID device and I thought my machine was starting to act funny. I could use the device no problem in a Windows VM but whenever my application tried to use the device, it was unable to open it. At this point I know the device is good and my machine should be fine but the device was unusable. I tried other software to just inspect the device and it was failing too in the same way as my application....

November 8, 2020 · John

C if Statement Evaluation

Introduction One of the bad things that the C language allows is setting variables and calling functions directly in an if statement. This should be avoided due to how the statement can exit early stopping some of the logic from happening. That said, this can also be abused to write more compact code. Not that you should do this because it’s better to have maintainable, rather than clever code. While how if statements evaluate segments to most programmers....

October 20, 2020 · John

Making and Using C plugins

Introduction No app is ever complete and you’ll never get to the point where users stop asking for new features. Sometimes those feature are low priorities so you’ll never get to them or they’re inane and you’ll never touch the idea with a 10 foot pole. This happened to me when I was working on an ebook editor a number of years ago. There were so many features people wanted that I just didn’t have time for or didn’t like....

May 12, 2020 · John

String List in C

Introduction Currently we have a generic list container which uses void pointers to allow anything to be stored. Previously, we created a type safe string hashtable wrapping the generic hashtable. We want to do the same thing for our list and so now we’re going to create a type safe string list wrapping our generic list. Design Unlike the string hashtable we want to extend the our string list to have some string specific functionality....

April 22, 2020 · John

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