source: svn/trunk/newcon3bcm2_21bu/dta/tools/sswap32.c @ 5

Last change on this file since 5 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 1.5 KB
Line 
1#include <stdio.h>
2#include <fcntl.h>
3
4#define BLOCK_SIZE 0x1000
5
6unsigned char data[BLOCK_SIZE];
7unsigned char out_data[BLOCK_SIZE];
8
9#define SWAP_INT(x)   ((((unsigned int)x & 0xFF) << 24) |(((unsigned int)x & 0xFF00) << 8) |(((unsigned int)x & 0xFF0000) >> 8) | (((unsigned int)x & 0xFF000000) >> 24))
10
11int main(int argc, char * argv[])
12{
13    FILE * input;
14    FILE * output;
15    size_t read_count;
16    size_t write_count;
17    unsigned long * walker;
18    unsigned long * out_walker;
19    /* uncomment this if compiling on cygwin */
20#if 0
21    if(-1 == setmode(fileno(stdin), O_BINARY)){
22        perror("Cannot set stdin to binary mode");
23        return -1;
24    }
25    if(-1 == setmode(fileno(stdout), O_BINARY)){
26        perror("Cannot set stdout to binary mode");
27        return -1;
28    }
29#endif
30    do{
31        read_count = fread(data, 1, BLOCK_SIZE, stdin);
32        if((0 != (read_count & 3))&&(0 != read_count)){
33            fprintf(stderr, "Input size must be divisible by 4!\n");
34            return -1;
35        }
36        if(read_count != 0){
37            walker = (unsigned long*)data;
38            out_walker = (unsigned long*)out_data;
39            while(walker != (unsigned long*)(&data[BLOCK_SIZE])) {
40                *out_walker = SWAP_INT(*walker);
41                out_walker++; walker++;
42            }
43
44            write_count = fwrite(out_data, 1, read_count, stdout);
45            if(read_count != write_count){
46                perror("Unable to write data");
47                return -1;
48            }
49        }
50    }while(BLOCK_SIZE == read_count);
51
52    return 0;
53}
Note: See TracBrowser for help on using the repository browser.