This commit introduced a lot of changes and seems to have caused a few
issues, including breaking crc32 generation. Revert it for now with the
intention to reimplement some of the improvements.
This reverts commit 9d7600df51.
If we are using C++, we might as well use scope-based memory management
for these things, cleaner and less error prone. Also removed
unnecessary gotos.