5 //usage:#define dd_trivial_usage
6 //usage: "[if=FILE] [of=FILE] " IF_FEATURE_DD_IBS_OBS("[ibs=N] [obs=N] ") "[bs=N] [count=N] [skip=N]\n"
7 -//usage: " [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync]")
8 +//usage: " [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync|swab]")
9 //usage:#define dd_full_usage "\n\n"
10 //usage: "Copy a file with converting and formatting\n"
11 //usage: "\n if=FILE Read from FILE instead of stdin"
13 //usage: "\n conv=noerror Continue after read errors"
14 //usage: "\n conv=sync Pad blocks with zeros"
15 //usage: "\n conv=fsync Physically write data out before finishing"
16 +//usage: "\n conv=swab Swap every pair of bytes"
19 //usage: "\nNumbers may be suffixed by c (x1), w (x2), b (x512), kD (x1000), k (x1024),"
20 @@ -155,9 +156,10 @@ int dd_main(int argc UNUSED_PARAM, char
22 FLAG_NOERROR = 1 << 2,
25 /* end of conv flags */
26 - FLAG_TWOBUFS = 1 << 4,
27 - FLAG_COUNT = 1 << 5,
28 + FLAG_TWOBUFS = 1 << 5,
29 + FLAG_COUNT = 1 << 6,
31 static const char keywords[] ALIGN1 =
32 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
33 @@ -167,7 +169,7 @@ int dd_main(int argc UNUSED_PARAM, char
35 #if ENABLE_FEATURE_DD_IBS_OBS
36 static const char conv_words[] ALIGN1 =
37 - "notrunc\0""sync\0""noerror\0""fsync\0";
38 + "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
42 @@ -185,11 +187,11 @@ int dd_main(int argc UNUSED_PARAM, char
47 /* Unimplemented conv=XXX: */
48 //nocreat do not create the output file
49 //excl fail if the output file already exists
50 //fdatasync physically write output file data before finishing
51 - //swab swap every pair of input bytes
52 //lcase change upper case to lower case
53 //ucase change lower case to upper case
54 //block pad newline-terminated records with spaces to cbs-size
55 @@ -207,12 +209,14 @@ int dd_main(int argc UNUSED_PARAM, char
59 + ssize_t prev_read_size; /* for detecting swab failure */
62 const char *infile, *outfile;
64 #define flags (Z.flags )
66 +#define prev_read_size (Z.prev_read_size)
67 #define count (Z.count )
68 #define seek (Z.seek )
69 #define skip (Z.skip )
70 @@ -375,6 +379,27 @@ int dd_main(int argc UNUSED_PARAM, char
71 * conv=noerror just ignores input bad blocks */
74 + if (flags & FLAG_SWAB) {
78 + /* Our code allows only last read to be odd-sized */
79 + if (prev_read_size & 1)
80 + bb_error_msg_and_die("can't swab %lu byte buffer",
81 + (unsigned long)prev_read_size);
84 + /* If n is odd, last byte is not swapped:
85 + * echo -n "qwe" | dd conv=swab
91 + *p16 = bswap_16(*p16);