Fix potential crash from parsing output format for MIME type.

Heads-up from Mayhem project and Daniel Leidert.
Fix minor syntax warning with Clang -- clarify sign inversion.
Fix invalid sscanf format string and a variety of minor bugs.

Bugs found through Coverity Scan.