libctf: split serialization and file writeout into its own file