Replace some unsafe code with a safe stdlib wrapper.

In theory these have minimal extra cost over the manual memcpys.