Fixes so 32-bit builds correctly handle large files.

- Avoid using size_t to represent file sizes and offsets (on some platforms size_t is limited to a 32-bit integer)
- Check for and return an error in the improbable situation where flattening would require an impossible amount of memory