summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>2015-02-12 18:02:26 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2015-02-12 21:54:15 -0500
commit5d909c8d54b114eddb7c50506f03bf7309a9192e (patch)
tree276ad2aa80b54b674b67e74552fbab443a235b8b
parent6f6f3fcb87a53c720941f4bd039ec2c0bce66625 (diff)
hexdump: do a few calculations ahead
Instead of doing calculations in each case of different groupsize let's do them beforehand. While there, change the switch to an if-else-if construction. Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--lib/hexdump.c34
1 files changed, 10 insertions, 24 deletions
diff --git a/lib/hexdump.c b/lib/hexdump.c
index a61cb6b1f011..4af53f73c7cc 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -103,6 +103,7 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
103 bool ascii) 103 bool ascii)
104{ 104{
105 const u8 *ptr = buf; 105 const u8 *ptr = buf;
106 int ngroups;
106 u8 ch; 107 u8 ch;
107 int j, lx = 0; 108 int j, lx = 0;
108 int ascii_column; 109 int ascii_column;
@@ -114,45 +115,33 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
114 goto nil; 115 goto nil;
115 if (len > rowsize) /* limit to one line at a time */ 116 if (len > rowsize) /* limit to one line at a time */
116 len = rowsize; 117 len = rowsize;
118 if (!is_power_of_2(groupsize) || groupsize > 8)
119 groupsize = 1;
117 if ((len % groupsize) != 0) /* no mixed size output */ 120 if ((len % groupsize) != 0) /* no mixed size output */
118 groupsize = 1; 121 groupsize = 1;
119 122
120 switch (groupsize) { 123 ngroups = len / groupsize;
121 case 8: { 124 ascii_column = rowsize * 2 + rowsize / groupsize + 1;
125 if (groupsize == 8) {
122 const u64 *ptr8 = buf; 126 const u64 *ptr8 = buf;
123 int ngroups = len / groupsize;
124 127
125 for (j = 0; j < ngroups; j++) 128 for (j = 0; j < ngroups; j++)
126 lx += scnprintf(linebuf + lx, linebuflen - lx, 129 lx += scnprintf(linebuf + lx, linebuflen - lx,
127 "%s%16.16llx", j ? " " : "", 130 "%s%16.16llx", j ? " " : "",
128 (unsigned long long)*(ptr8 + j)); 131 (unsigned long long)*(ptr8 + j));
129 ascii_column = rowsize * 2 + rowsize / 8 + 2; 132 } else if (groupsize == 4) {
130 break;
131 }
132
133 case 4: {
134 const u32 *ptr4 = buf; 133 const u32 *ptr4 = buf;
135 int ngroups = len / groupsize;
136 134
137 for (j = 0; j < ngroups; j++) 135 for (j = 0; j < ngroups; j++)
138 lx += scnprintf(linebuf + lx, linebuflen - lx, 136 lx += scnprintf(linebuf + lx, linebuflen - lx,
139 "%s%8.8x", j ? " " : "", *(ptr4 + j)); 137 "%s%8.8x", j ? " " : "", *(ptr4 + j));
140 ascii_column = rowsize * 2 + rowsize / 4 + 2; 138 } else if (groupsize == 2) {
141 break;
142 }
143
144 case 2: {
145 const u16 *ptr2 = buf; 139 const u16 *ptr2 = buf;
146 int ngroups = len / groupsize;
147 140
148 for (j = 0; j < ngroups; j++) 141 for (j = 0; j < ngroups; j++)
149 lx += scnprintf(linebuf + lx, linebuflen - lx, 142 lx += scnprintf(linebuf + lx, linebuflen - lx,
150 "%s%4.4x", j ? " " : "", *(ptr2 + j)); 143 "%s%4.4x", j ? " " : "", *(ptr2 + j));
151 ascii_column = rowsize * 2 + rowsize / 2 + 2; 144 } else {
152 break;
153 }
154
155 default:
156 for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) { 145 for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) {
157 ch = ptr[j]; 146 ch = ptr[j];
158 linebuf[lx++] = hex_asc_hi(ch); 147 linebuf[lx++] = hex_asc_hi(ch);
@@ -161,14 +150,11 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
161 } 150 }
162 if (j) 151 if (j)
163 lx--; 152 lx--;
164
165 ascii_column = 3 * rowsize + 2;
166 break;
167 } 153 }
168 if (!ascii) 154 if (!ascii)
169 goto nil; 155 goto nil;
170 156
171 while (lx < (linebuflen - 1) && lx < (ascii_column - 1)) 157 while (lx < (linebuflen - 1) && lx < ascii_column)
172 linebuf[lx++] = ' '; 158 linebuf[lx++] = ' ';
173 for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) { 159 for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) {
174 ch = ptr[j]; 160 ch = ptr[j];