00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "largefiles.h"
00012
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015 #include <sys/mman.h>
00016 #include <fcntl.h>
00017 #include <stdio.h>
00018
00019 #ifndef MAP_FILE
00020 #define MAP_FILE 0
00021 #endif
00022
00023 int main(int argc, char *argv[]) {
00024
00025 int fd;
00026 struct stat statbuf;
00027 off_t n;
00028 int i, j, isbig, itmp, argcount=0;
00029 int status_only=0, make_big_only=0, make_little_only=0;
00030 char b[8];
00031 char *d;
00032
00033 if ( argc < 2 ) {
00034 fprintf(stderr,"This program flips byte-ordering of DCD files.\n");
00035 usage:
00036 fprintf(stderr,"Usage: %s [-s] [-B] [-L] file . . . \n",argv[0]);
00037 fprintf(stderr," The default behavior is to flip the byte ordering. Other options are:\n");
00038 fprintf(stderr," -s report the byte-order status of each <file> without changing it\n");
00039 fprintf(stderr," -B make/keep each <file> big-endian\n");
00040 fprintf(stderr," -L make/keep each <file> little-endian\n");
00041 fprintf(stderr," The options are mutually exclusive; the last one read is used.\n\n");
00042 exit(-1);
00043 }
00044
00045 while (++argcount < argc){
00046
00047 if ((strncmp(argv[argcount],"-S",2) == 0) || (strncmp(argv[argcount],"-s",2) == 0)){
00048 status_only=1; make_big_only=0; make_little_only=0;
00049 }
00050 else if ((strncmp(argv[argcount],"-B",2) == 0) || (strncmp(argv[argcount],"-b",2) == 0)){
00051 make_big_only=1; status_only=0; make_little_only=0;
00052 }
00053 else if ((strncmp(argv[argcount],"-L",2) == 0) || (strncmp(argv[argcount],"-l",2) == 0)){
00054 make_little_only=1; make_big_only=0; status_only=0;
00055 }
00056 else if (strncmp(argv[argcount],"-",1) == 0){
00057 printf("\n Error: %s not a valid option. \n\n",argv[argcount]);
00058 goto usage;
00059 }
00060 else{
00061 if ( ( fd = open(argv[argcount], O_RDWR) ) < 0 ) {
00062 fprintf(stderr,"Can't open %s for updating. (File must be read/writeable.)\n",argv[argcount]);
00063 goto end;
00064 }
00065
00066 if ( fstat(fd,&statbuf) < 0 ) {
00067 fprintf(stderr,"Can't stat %s.\n",argv[1]);
00068 goto end;
00069 }
00070
00071 n = statbuf.st_size;
00072
00073 if ( n <= 104 ) {
00074 fprintf(stderr,"%s is not in DCD format.\n",argv[argcount]);
00075 goto end;
00076 }
00077
00078 if ( ( sizeof(char*) < 8 ) && ( n >> 32 ) ) {
00079 fprintf(stderr,"%s is too large, 64-bit build required\n",argv[argcount]);
00080 goto end;
00081 }
00082
00083 if ( n % 4 ) {
00084 fprintf(stderr,"%s is not in DCD format.\n",argv[argcount]);
00085 goto end;
00086 }
00087 if ( ( d = mmap(0,n,PROT_READ|PROT_WRITE,MAP_FILE|MAP_SHARED,fd,0) )
00088 == (caddr_t) -1 ) {
00089 fprintf(stderr,"Can't mmap %s.\n",argv[argcount]);
00090 goto end;
00091 }
00092
00093 if ( status_only ){
00094 if ( d[0] == 84 ) {
00095 isbig = 0;
00096 fprintf(stderr,"%s is little-endian.\n",argv[argcount]);
00097 }
00098 else if ( d[3] == 84 ) {
00099 isbig = 1;
00100 fprintf(stderr,"%s is big-endian.\n",argv[argcount]);
00101 }
00102 else {
00103 fprintf(stderr,"%s is not in DCD format.\n",argv[argcount]);
00104 }
00105 goto end;
00106
00107 }
00108 else {
00109 if ( d[0] == 84 ) {
00110 if ( make_little_only ){
00111 fprintf(stderr,"%s is already little-endian. (No change made.)\n",argv[argcount]);
00112 goto end;
00113 }
00114 else {
00115 isbig = 0;
00116 fprintf(stderr,"%s was little-endian, will be big-endian.\n",argv[argcount]);
00117 }
00118 }
00119 else if ( d[3] == 84 ) {
00120 if ( make_big_only ){
00121 fprintf(stderr,"%s is already big-endian. (No change made.)\n",argv[argcount]);
00122 goto end;
00123 }
00124 else {
00125 isbig = 1;
00126 fprintf(stderr,"%s was big-endian, will be little-endian.\n",argv[argcount]);
00127 }
00128 }
00129 else {
00130 fprintf(stderr,"%s is not in DCD format.\n",argv[argcount]);
00131 goto end;
00132 }
00133 }
00134
00135 #define FLIPFOUR {for(j=0;j<4;++j)b[j]=d[j];for(j=3;j>=0;--j,++d)*d=b[j];n-=4;}
00136 #define FLIPEIGHT {for(j=0;j<8;++j)b[j]=d[j];for(j=7;j>=0;--j,++d)*d=b[j];n-=8;}
00137 #define SKIPFOUR {d+=4;n-=4;}
00138 #define SKIP(X) {d+=(X);n-=(X);}
00139 #define READINT(X) { X=0; if (isbig) { for(j=0;j<4;++j,X<<8) X+=d[j]; } \
00140 else { for(j=3;j>=0;--j,X<<8) X+=d[j]; } }
00141
00142
00143 FLIPFOUR;
00144 SKIPFOUR;
00145 for ( i = 0; i < 20; ++i ) FLIPFOUR;
00146 FLIPFOUR;
00147 FLIPFOUR;
00148 READINT(itmp); FLIPFOUR;
00149 if ( n <= (80*(off_t)itmp + 4) ) {
00150 fprintf(stderr,"%s is too short for DCD format.\n",argv[argcount]);
00151 goto end;
00152 }
00153 SKIP(80*itmp);
00154 FLIPFOUR;
00155
00156 while ( n ) FLIPFOUR;
00157 ;
00158
00159 }
00160 end:
00161 ;
00162 }
00163 }