|
Last change
on this file since 2 was
2,
checked in by jglee, 11 years ago
|
|
first commit
|
-
Property svn:executable set to
*
|
|
File size:
1023 bytes
|
| Line | |
|---|
| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <string.h> |
|---|
| 4 | |
|---|
| 5 | void byteswap_4(char *b, int size) { |
|---|
| 6 | while (size >= 4) { |
|---|
| 7 | char b0 = b[0]; |
|---|
| 8 | char b1 = b[1]; |
|---|
| 9 | b[0] = b[3]; |
|---|
| 10 | b[1] = b[2]; |
|---|
| 11 | b[2] = b1; |
|---|
| 12 | b[3] = b0; |
|---|
| 13 | size -= 4; |
|---|
| 14 | b += 4; |
|---|
| 15 | } |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | void byteswap_2(char *b, int size) { |
|---|
| 19 | while (size >= 2) { |
|---|
| 20 | char b0 = b[0]; |
|---|
| 21 | b[0] = b[1]; |
|---|
| 22 | b[1] = b0; |
|---|
| 23 | size -= 2; |
|---|
| 24 | b += 2; |
|---|
| 25 | } |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | #define BUFSIZE 4096 /* should be multiple of 4 */ |
|---|
| 29 | |
|---|
| 30 | int main(int argc, char **argv) { |
|---|
| 31 | FILE *in = stdin; |
|---|
| 32 | FILE *out = stdout; |
|---|
| 33 | char buf[BUFSIZE]; |
|---|
| 34 | int mode = 4; |
|---|
| 35 | |
|---|
| 36 | if (argc >= 2) { |
|---|
| 37 | if (!strcmp(argv[1], "--help")) { |
|---|
| 38 | printf( |
|---|
| 39 | "Usage: byteswap [-2] <infile >outfile\n" |
|---|
| 40 | "\n" |
|---|
| 41 | "-2 means 16 bit word byteswap.\n" |
|---|
| 42 | "Otherwise it does 32 bit word byteswap.\n" |
|---|
| 43 | "Stdin and Stdout are used.\n" |
|---|
| 44 | ); |
|---|
| 45 | exit(0); |
|---|
| 46 | } |
|---|
| 47 | else if (!strcmp(argv[1], "-2")) |
|---|
| 48 | mode = 2; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | while (!feof(in)) { |
|---|
| 52 | int n= fread(buf, 1, BUFSIZE, in); |
|---|
| 53 | if (mode == 2) |
|---|
| 54 | byteswap_2(buf, n); |
|---|
| 55 | else |
|---|
| 56 | byteswap_4(buf, n); |
|---|
| 57 | fwrite(buf, 1, n, out); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | return 0; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
Note: See
TracBrowser
for help on using the repository browser.