I haven't investigated very closely, but my impression is that you want to use libsquish. I'm not sure if it has C bindings, or if the API is entirely right for what we need, but maybe they accept patches.