Make hdr_alloc, hdr_swap_endian, use memcpy in hdr_write

Separated out the ptw_hdr allocation into a separate function so that it can be
called from outside of ptw_file_open, and move the endianness swap of a ptw_hdr
into a function so that the write operation can call it as well.

In ptw_hdr_write, if we're on a bigendian system, swapping the endianness of
the input ptw_hdr would mean that any subsequent access to that header would
have its endianness backwards, so we use memcpy to create a separate ptw_hdr
which we can safely swap for the write operation.