#include #include #include #include "FileDescriptor.hh" #include "Exception.hh" #include "endian.hh" using namespace std; using namespace pbe; static void usage(void) { cerr << "Usage: endswap [filename]\n" << "If no filename is given, standard input is read.\n" << "The input is read and endian-swapped until end-of-file,\n" << "with the output sent to standard output.\n" << "If the input terminates on a non-word boundary the\n" << "output will be padded to the word boundary with zeros\n" << "and a warning will be output to standard error.\n"; } static void endswap(FileDescriptor& infd) { string s = infd.readall(); int padbytes=0; while (s.length()%4 > 0) { s += '\0'; ++padbytes; } if (padbytes) { cerr << "Warning: " << padbytes << " bytes of padding needed at EOF.\n"; } const uint32_t* p = reinterpret_cast(s.data()); unsigned int words = s.length()/4; uint32_t swapped[words]; for (unsigned int w=0; w(swapped),sizeof(swapped)); } int main(int argc, char* argv[]) { try { try { if (argc>2) { usage(); exit(1); } if (argc==2) { string arg(argv[1]); if (arg=="--help") { usage(); exit(0); } FileDescriptor infd(arg,FileDescriptor::read_only); endswap(infd); exit(0); } else { FileDescriptor infd(0); endswap(infd); exit(0); } } RETHROW_MISC_EXCEPTIONS } catch (Exception& E) { E.report(cerr); exit(1); } }