Tag: C lang

Recursive Create Directory in C Revisited



Awhile back I wrote a function to recursively create directories in C. It used a string builder to split the parts and rebuild the path. The way mkdir works is by taking a single directory that does not exist and creates it. If there are multiple path... Continue reading

Unsigned Count Down



Introduction Something that comes up surprisingly often is traversing an array backwards. Maybe you’re emptying a queue. How about my personal favorite, reversing the order of elements. Counting in a for loop is so common you just don’t think about it. But counting backwards can lead to issues... Continue reading

Thread Pool in C



Introduction When I was writing Poddown I needed a thread pool and I needed one that is cross platform. Since it’s a lightweight app I didn’t want to include a big third party threading library. So I wrote my own. Why a Thread Pool Creating threads can... Continue reading

Cross Platform Thread Wrapper



Introduction There are many open source applications which use threading and are limited to either *nix or Windows because Windows handles threading a bit differently than *nix. I develop on macOS so pthreads is my go to but using it effectively locks me out of Windows because Windows doesn’t... Continue reading

Looping Through Bytes to Check for Bits



Checking for bits in 1 byte is easy. Checking in 2 bytes is also easy. Checking an odd number of bits in a variable number bytes isn’t so easy. The hard part is dealing with the boundary between bytes where we need to move from one to the next. Lets... Continue reading

Mergesort in C



Introduction Quicksort is most people’s go to sort function and that’s not a bad thing because it’s a really good general purpose sorting algorithm. A good implementation is really fast and, being an in place algorithm, it uses very little memory. The big drawback of Quicksort is that... Continue reading

General Comparison Functions in C



Introduction qsort, heapsort, mergesort, bsearch, and many more search and sort functions all take a compar argument to determine the sorting order of elements in an array. The compar function takes the form int (*compar)(const void *, const void *). The <code... Continue reading

Quicksort in C



Introduction Quicksort is one of the most common sorting algorithms and one of the most efficient. It’s so common that it’s part of C89. That said, it’s still good to know how it works, its strengths, and it’s weaknesses. It takes as a divide and conquer approach to sorting.... Continue reading

Byte Swapping in C



Introduction Some times you need to swap bytes. Sometimes you don’t. But right now we do. Well, we don’t but if we were implementing some sorting functions we’d need a good swap function. Sorting typically doesn’t move pointers around, instead it moves the bytes pointed to by the pointer.... Continue reading

Binary Search and Insert



Introduction When dealing with arrays it’s often necessary to keep their elements in sorted order. The easiest way is to use an insert algorithm that always puts element into the array in sorted order. That said, you don’t know where elements will end up in the array because if... Continue reading

Base64 Encode and Decode in C



Introduction A very popular way to encode binary data is Base64. The basis of this is an encoding table. As you might expect, there are 64 total characters that go into the tale. There are multiple implementations of base64 with slight differences. They are all the same except for... Continue reading

Hex Encode and Decode in C



Introduction A very common task when working with binary data in C is, converting it to and from Hex. It’s unfortunate that C doesn’t provide a standard function that can take care of this for us. However, it’s pretty easy to implement a set of functions to handle it.... Continue reading

Wrapping C++ objects in C



Introduction Using C functions from C++ is very easy but going the other way isn’t. However, it can be done with a little ingenuity. Really, this isn’t as crazy as it sounds. I’ll use a simple adder for the object. It takes an integer as a starting value and... Continue reading

JNI is (Not) Your Friend



Introduction So far we’ve scratched the surface of using JNI when we looked at wrapping a C library and Calling Java from C. Now we’re going to look into some more complex uses. Java Class Since we’re working with JNI, we’ll need a Java class to... Continue reading

Calling Java From C



Introduction Obviously, JNI lets you call Java functions and use Java classes in C. Typical a Java app is the one calling into C. Now Let’s say you don’t have a Java app that kicks off the process but you want your C app to still use some Java... Continue reading

Wrapping a C library in Java



Introduction It can’t be argued that Java is popular and successful. It is consistently the number one language on TIOBE’s popularity list, above C which comes in as number two. This ranking is based on popularity and doesn’t mean Java is more used than C but that doesn’t change... Continue reading

Recursive Create Directory in C



C has a very large gap when it comes to working with files and directories. It is C, so all the building blocks are there. Thankfully, it’s pretty easy to put together something useful. In a project I’ve been working on I needed to create a directory. This is pretty... Continue reading

Poddown a simple podcast downloader



I have been listing to podcasts for a long time and many years ago I wrote a podcast downloader. Back then I had a media center and I wanted my favorite podcasts to automatically download each night. At that time there wasn’t anything that really did that. Well, there... Continue reading

String Splitting in C



For a project I’ve been working on I needed to split a string into it’s component parts. There is strtok which I find useless for pretty much any task. It is not thread-safe, nor is it re-entrant, which makes it impossible to parse two strings (in a loop) at... Continue reading

Constant Time String Comparison in C



Comparing strings in C is typically handled with strncmp. This is fine in most cases but if you need to compare sensitive information, such as a message digest, it’s a really bad choice. strncmp is susceptible to timing attacks because it will stop comparing once the first... Continue reading

Efficient C String Builder



One task that always annoys me when I work with C is building strings. snprintf is all well and good if I know exactly the format I want. It falls down with anything that needs to be build iteratively and dynamically. Other languages that have built in strings will... Continue reading

Interop Objective-C Objects In C Using ARC



Introduction Using C functions from Objective-C is very easy but going the other way isn’t so easy. Especially with ARC which can destroy the object out from under you because C code is outside of ARC. With ARC Objective-C objects are no longer allowed in C structs for this... Continue reading

Client Side Session Cache in OpenSSL



Building on Server Side Session Cache in OpenSSL we need to deal with the Client side. The OpenSSL documentation for SSL_CTX_set_session_cache_mode has an option for client caching. However, it states that, “the application must select the session to be reused by using the SSL_set_session(3) function.” It also states... Continue reading

Server Side Session Cache in OpenSSL



At work (information posed with permission from my employer) we’ve been looking into session caching with OpenSSL. We started this by looking at the server and found that by default OpenSSL will enable and use a session cache when acting as the server. However, there are two major things we... Continue reading

Enable DH and ECDH in OpenSSL (Server)



Recently at work we were looking into Forward Secrecy (FS). We were using Qualys SSL Server Test and noticed that Forward Secrecy was showing as NO. We decided to look into this because we want to use the most robust security we can. What we found was none... Continue reading

Extending an Application with Lua Plugins



Introduction A very common use of the Lua language (which is a very versatile) is using Lua to extend an application via plugins. Many popular games use Lua for this very purpose. Adding a plugin framework to an existing application is trivial with Lua. Also, Lua provides a very... Continue reading

My SixAxis Pairing Tool



I was looking for a way to use a PlayStation Dual Sock 3 SixAxis controller (that is a mouthful of a name) with my Android phone. I found Dancing Pixel Studios Sixaxis Controller app which looked like the only real solution. It requires root which isn’t a problem... Continue reading

Using Lua as a Templating Engine



Introduction There are a lot of templating engines to choose from. The vast majority of which are primarily geared toward the web. Meaning they’re specially designed for outputting HTML/XML documents. Lua provides more flexibility and can easily be used as a general templating engine. Also, Lua is very easy... Continue reading

Wrapping a Lua module in C



Introduction So far I’ve covered wrapping a C library in Lua, writing a Lua module, and calling Lua from C. The next step is using this knowledge to wrap a Lua module in C. Basically, the opposite of wrapping a C library in Lua for use... Continue reading

Calling Lua From C



Introduction One very useful feature of Lua is how easy it is to work with from C. Unlike other scripting languages Lua’s C support goes to and from. Meaning you not only can call C from Lua but you can call Lua from C. Often you’ll see applications written... Continue reading

Wrapping a C library in Lua



Introduction An often overlooked feature of Lua is the C API. The most common use I’ve seen is to allow Lua scripts to use existing C libraries instead of reimplementing existing functionality in pure Lua (which is not always feasible). Fortunately, Lua has very strong integration with C and... Continue reading