Commit 559b140a36613bb5b63f258b2ad833dad8cd11d9

Authored by Michal Nazarewicz
Committed by Linus Torvalds
1 parent e3f76e3386

lib: vsprintf: useless strlen() removed

The strict_strtoul() and strict_strtoull() functions used strlen() to
check argument's length in a situation where it wasn't strictly necessary

Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
Cc: "Yi Yang" <yi.y.yang@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 1 changed file with 4 additions and 10 deletions Inline Diff

1 /* 1 /*
2 * linux/lib/vsprintf.c 2 * linux/lib/vsprintf.c
3 * 3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds 4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */ 5 */
6 6
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ 7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8 /* 8 /*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-) 9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */ 10 */
11 11
12 /* 12 /*
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> 13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions 14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> 15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf 16 * - scnprintf and vscnprintf
17 */ 17 */
18 18
19 #include <stdarg.h> 19 #include <stdarg.h>
20 #include <linux/module.h> 20 #include <linux/module.h>
21 #include <linux/types.h> 21 #include <linux/types.h>
22 #include <linux/string.h> 22 #include <linux/string.h>
23 #include <linux/ctype.h> 23 #include <linux/ctype.h>
24 #include <linux/kernel.h> 24 #include <linux/kernel.h>
25 #include <linux/kallsyms.h> 25 #include <linux/kallsyms.h>
26 #include <linux/uaccess.h> 26 #include <linux/uaccess.h>
27 #include <linux/ioport.h> 27 #include <linux/ioport.h>
28 #include <net/addrconf.h> 28 #include <net/addrconf.h>
29 29
30 #include <asm/page.h> /* for PAGE_SIZE */ 30 #include <asm/page.h> /* for PAGE_SIZE */
31 #include <asm/div64.h> 31 #include <asm/div64.h>
32 #include <asm/sections.h> /* for dereference_function_descriptor() */ 32 #include <asm/sections.h> /* for dereference_function_descriptor() */
33 33
34 /* Works only for digits and letters, but small and fast */ 34 /* Works only for digits and letters, but small and fast */
35 #define TOLOWER(x) ((x) | 0x20) 35 #define TOLOWER(x) ((x) | 0x20)
36 36
37 static unsigned int simple_guess_base(const char *cp) 37 static unsigned int simple_guess_base(const char *cp)
38 { 38 {
39 if (cp[0] == '0') { 39 if (cp[0] == '0') {
40 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) 40 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
41 return 16; 41 return 16;
42 else 42 else
43 return 8; 43 return 8;
44 } else { 44 } else {
45 return 10; 45 return 10;
46 } 46 }
47 } 47 }
48 48
49 /** 49 /**
50 * simple_strtoull - convert a string to an unsigned long long 50 * simple_strtoull - convert a string to an unsigned long long
51 * @cp: The start of the string 51 * @cp: The start of the string
52 * @endp: A pointer to the end of the parsed string will be placed here 52 * @endp: A pointer to the end of the parsed string will be placed here
53 * @base: The number base to use 53 * @base: The number base to use
54 */ 54 */
55 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 55 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
56 { 56 {
57 unsigned long long result = 0; 57 unsigned long long result = 0;
58 58
59 if (!base) 59 if (!base)
60 base = simple_guess_base(cp); 60 base = simple_guess_base(cp);
61 61
62 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') 62 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
63 cp += 2; 63 cp += 2;
64 64
65 while (isxdigit(*cp)) { 65 while (isxdigit(*cp)) {
66 unsigned int value; 66 unsigned int value;
67 67
68 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; 68 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
69 if (value >= base) 69 if (value >= base)
70 break; 70 break;
71 result = result * base + value; 71 result = result * base + value;
72 cp++; 72 cp++;
73 } 73 }
74 if (endp) 74 if (endp)
75 *endp = (char *)cp; 75 *endp = (char *)cp;
76 76
77 return result; 77 return result;
78 } 78 }
79 EXPORT_SYMBOL(simple_strtoull); 79 EXPORT_SYMBOL(simple_strtoull);
80 80
81 /** 81 /**
82 * simple_strtoul - convert a string to an unsigned long 82 * simple_strtoul - convert a string to an unsigned long
83 * @cp: The start of the string 83 * @cp: The start of the string
84 * @endp: A pointer to the end of the parsed string will be placed here 84 * @endp: A pointer to the end of the parsed string will be placed here
85 * @base: The number base to use 85 * @base: The number base to use
86 */ 86 */
87 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 87 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
88 { 88 {
89 return simple_strtoull(cp, endp, base); 89 return simple_strtoull(cp, endp, base);
90 } 90 }
91 EXPORT_SYMBOL(simple_strtoul); 91 EXPORT_SYMBOL(simple_strtoul);
92 92
93 /** 93 /**
94 * simple_strtol - convert a string to a signed long 94 * simple_strtol - convert a string to a signed long
95 * @cp: The start of the string 95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here 96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use 97 * @base: The number base to use
98 */ 98 */
99 long simple_strtol(const char *cp, char **endp, unsigned int base) 99 long simple_strtol(const char *cp, char **endp, unsigned int base)
100 { 100 {
101 if (*cp == '-') 101 if (*cp == '-')
102 return -simple_strtoul(cp + 1, endp, base); 102 return -simple_strtoul(cp + 1, endp, base);
103 103
104 return simple_strtoul(cp, endp, base); 104 return simple_strtoul(cp, endp, base);
105 } 105 }
106 EXPORT_SYMBOL(simple_strtol); 106 EXPORT_SYMBOL(simple_strtol);
107 107
108 /** 108 /**
109 * simple_strtoll - convert a string to a signed long long 109 * simple_strtoll - convert a string to a signed long long
110 * @cp: The start of the string 110 * @cp: The start of the string
111 * @endp: A pointer to the end of the parsed string will be placed here 111 * @endp: A pointer to the end of the parsed string will be placed here
112 * @base: The number base to use 112 * @base: The number base to use
113 */ 113 */
114 long long simple_strtoll(const char *cp, char **endp, unsigned int base) 114 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
115 { 115 {
116 if (*cp == '-') 116 if (*cp == '-')
117 return -simple_strtoull(cp + 1, endp, base); 117 return -simple_strtoull(cp + 1, endp, base);
118 118
119 return simple_strtoull(cp, endp, base); 119 return simple_strtoull(cp, endp, base);
120 } 120 }
121 EXPORT_SYMBOL(simple_strtoll); 121 EXPORT_SYMBOL(simple_strtoll);
122 122
123 /** 123 /**
124 * strict_strtoul - convert a string to an unsigned long strictly 124 * strict_strtoul - convert a string to an unsigned long strictly
125 * @cp: The string to be converted 125 * @cp: The string to be converted
126 * @base: The number base to use 126 * @base: The number base to use
127 * @res: The converted result value 127 * @res: The converted result value
128 * 128 *
129 * strict_strtoul converts a string to an unsigned long only if the 129 * strict_strtoul converts a string to an unsigned long only if the
130 * string is really an unsigned long string, any string containing 130 * string is really an unsigned long string, any string containing
131 * any invalid char at the tail will be rejected and -EINVAL is returned, 131 * any invalid char at the tail will be rejected and -EINVAL is returned,
132 * only a newline char at the tail is acceptible because people generally 132 * only a newline char at the tail is acceptible because people generally
133 * change a module parameter in the following way: 133 * change a module parameter in the following way:
134 * 134 *
135 * echo 1024 > /sys/module/e1000/parameters/copybreak 135 * echo 1024 > /sys/module/e1000/parameters/copybreak
136 * 136 *
137 * echo will append a newline to the tail. 137 * echo will append a newline to the tail.
138 * 138 *
139 * It returns 0 if conversion is successful and *res is set to the converted 139 * It returns 0 if conversion is successful and *res is set to the converted
140 * value, otherwise it returns -EINVAL and *res is set to 0. 140 * value, otherwise it returns -EINVAL and *res is set to 0.
141 * 141 *
142 * simple_strtoul just ignores the successive invalid characters and 142 * simple_strtoul just ignores the successive invalid characters and
143 * return the converted value of prefix part of the string. 143 * return the converted value of prefix part of the string.
144 */ 144 */
145 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) 145 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
146 { 146 {
147 char *tail; 147 char *tail;
148 unsigned long val; 148 unsigned long val;
149 size_t len;
150 149
151 *res = 0; 150 *res = 0;
152 len = strlen(cp); 151 if (!*cp)
153 if (len == 0)
154 return -EINVAL; 152 return -EINVAL;
155 153
156 val = simple_strtoul(cp, &tail, base); 154 val = simple_strtoul(cp, &tail, base);
157 if (tail == cp) 155 if (tail == cp)
158 return -EINVAL; 156 return -EINVAL;
159 157
160 if ((*tail == '\0') || 158 if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) {
161 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
162 *res = val; 159 *res = val;
163 return 0; 160 return 0;
164 } 161 }
165 162
166 return -EINVAL; 163 return -EINVAL;
167 } 164 }
168 EXPORT_SYMBOL(strict_strtoul); 165 EXPORT_SYMBOL(strict_strtoul);
169 166
170 /** 167 /**
171 * strict_strtol - convert a string to a long strictly 168 * strict_strtol - convert a string to a long strictly
172 * @cp: The string to be converted 169 * @cp: The string to be converted
173 * @base: The number base to use 170 * @base: The number base to use
174 * @res: The converted result value 171 * @res: The converted result value
175 * 172 *
176 * strict_strtol is similiar to strict_strtoul, but it allows the first 173 * strict_strtol is similiar to strict_strtoul, but it allows the first
177 * character of a string is '-'. 174 * character of a string is '-'.
178 * 175 *
179 * It returns 0 if conversion is successful and *res is set to the converted 176 * It returns 0 if conversion is successful and *res is set to the converted
180 * value, otherwise it returns -EINVAL and *res is set to 0. 177 * value, otherwise it returns -EINVAL and *res is set to 0.
181 */ 178 */
182 int strict_strtol(const char *cp, unsigned int base, long *res) 179 int strict_strtol(const char *cp, unsigned int base, long *res)
183 { 180 {
184 int ret; 181 int ret;
185 if (*cp == '-') { 182 if (*cp == '-') {
186 ret = strict_strtoul(cp + 1, base, (unsigned long *)res); 183 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
187 if (!ret) 184 if (!ret)
188 *res = -(*res); 185 *res = -(*res);
189 } else { 186 } else {
190 ret = strict_strtoul(cp, base, (unsigned long *)res); 187 ret = strict_strtoul(cp, base, (unsigned long *)res);
191 } 188 }
192 189
193 return ret; 190 return ret;
194 } 191 }
195 EXPORT_SYMBOL(strict_strtol); 192 EXPORT_SYMBOL(strict_strtol);
196 193
197 /** 194 /**
198 * strict_strtoull - convert a string to an unsigned long long strictly 195 * strict_strtoull - convert a string to an unsigned long long strictly
199 * @cp: The string to be converted 196 * @cp: The string to be converted
200 * @base: The number base to use 197 * @base: The number base to use
201 * @res: The converted result value 198 * @res: The converted result value
202 * 199 *
203 * strict_strtoull converts a string to an unsigned long long only if the 200 * strict_strtoull converts a string to an unsigned long long only if the
204 * string is really an unsigned long long string, any string containing 201 * string is really an unsigned long long string, any string containing
205 * any invalid char at the tail will be rejected and -EINVAL is returned, 202 * any invalid char at the tail will be rejected and -EINVAL is returned,
206 * only a newline char at the tail is acceptible because people generally 203 * only a newline char at the tail is acceptible because people generally
207 * change a module parameter in the following way: 204 * change a module parameter in the following way:
208 * 205 *
209 * echo 1024 > /sys/module/e1000/parameters/copybreak 206 * echo 1024 > /sys/module/e1000/parameters/copybreak
210 * 207 *
211 * echo will append a newline to the tail of the string. 208 * echo will append a newline to the tail of the string.
212 * 209 *
213 * It returns 0 if conversion is successful and *res is set to the converted 210 * It returns 0 if conversion is successful and *res is set to the converted
214 * value, otherwise it returns -EINVAL and *res is set to 0. 211 * value, otherwise it returns -EINVAL and *res is set to 0.
215 * 212 *
216 * simple_strtoull just ignores the successive invalid characters and 213 * simple_strtoull just ignores the successive invalid characters and
217 * return the converted value of prefix part of the string. 214 * return the converted value of prefix part of the string.
218 */ 215 */
219 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res) 216 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
220 { 217 {
221 char *tail; 218 char *tail;
222 unsigned long long val; 219 unsigned long long val;
223 size_t len;
224 220
225 *res = 0; 221 *res = 0;
226 len = strlen(cp); 222 if (!*cp)
227 if (len == 0)
228 return -EINVAL; 223 return -EINVAL;
229 224
230 val = simple_strtoull(cp, &tail, base); 225 val = simple_strtoull(cp, &tail, base);
231 if (tail == cp) 226 if (tail == cp)
232 return -EINVAL; 227 return -EINVAL;
233 if ((*tail == '\0') || 228 if ((tail[0] == '\0') || (tail[0] == '\n' && tail[1] == '\0')) {
234 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
235 *res = val; 229 *res = val;
236 return 0; 230 return 0;
237 } 231 }
238 232
239 return -EINVAL; 233 return -EINVAL;
240 } 234 }
241 EXPORT_SYMBOL(strict_strtoull); 235 EXPORT_SYMBOL(strict_strtoull);
242 236
243 /** 237 /**
244 * strict_strtoll - convert a string to a long long strictly 238 * strict_strtoll - convert a string to a long long strictly
245 * @cp: The string to be converted 239 * @cp: The string to be converted
246 * @base: The number base to use 240 * @base: The number base to use
247 * @res: The converted result value 241 * @res: The converted result value
248 * 242 *
249 * strict_strtoll is similiar to strict_strtoull, but it allows the first 243 * strict_strtoll is similiar to strict_strtoull, but it allows the first
250 * character of a string is '-'. 244 * character of a string is '-'.
251 * 245 *
252 * It returns 0 if conversion is successful and *res is set to the converted 246 * It returns 0 if conversion is successful and *res is set to the converted
253 * value, otherwise it returns -EINVAL and *res is set to 0. 247 * value, otherwise it returns -EINVAL and *res is set to 0.
254 */ 248 */
255 int strict_strtoll(const char *cp, unsigned int base, long long *res) 249 int strict_strtoll(const char *cp, unsigned int base, long long *res)
256 { 250 {
257 int ret; 251 int ret;
258 if (*cp == '-') { 252 if (*cp == '-') {
259 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res); 253 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
260 if (!ret) 254 if (!ret)
261 *res = -(*res); 255 *res = -(*res);
262 } else { 256 } else {
263 ret = strict_strtoull(cp, base, (unsigned long long *)res); 257 ret = strict_strtoull(cp, base, (unsigned long long *)res);
264 } 258 }
265 259
266 return ret; 260 return ret;
267 } 261 }
268 EXPORT_SYMBOL(strict_strtoll); 262 EXPORT_SYMBOL(strict_strtoll);
269 263
270 static noinline_for_stack 264 static noinline_for_stack
271 int skip_atoi(const char **s) 265 int skip_atoi(const char **s)
272 { 266 {
273 int i = 0; 267 int i = 0;
274 268
275 while (isdigit(**s)) 269 while (isdigit(**s))
276 i = i*10 + *((*s)++) - '0'; 270 i = i*10 + *((*s)++) - '0';
277 271
278 return i; 272 return i;
279 } 273 }
280 274
281 /* Decimal conversion is by far the most typical, and is used 275 /* Decimal conversion is by far the most typical, and is used
282 * for /proc and /sys data. This directly impacts e.g. top performance 276 * for /proc and /sys data. This directly impacts e.g. top performance
283 * with many processes running. We optimize it for speed 277 * with many processes running. We optimize it for speed
284 * using code from 278 * using code from
285 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html 279 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
286 * (with permission from the author, Douglas W. Jones). */ 280 * (with permission from the author, Douglas W. Jones). */
287 281
288 /* Formats correctly any integer in [0,99999]. 282 /* Formats correctly any integer in [0,99999].
289 * Outputs from one to five digits depending on input. 283 * Outputs from one to five digits depending on input.
290 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ 284 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
291 static noinline_for_stack 285 static noinline_for_stack
292 char *put_dec_trunc(char *buf, unsigned q) 286 char *put_dec_trunc(char *buf, unsigned q)
293 { 287 {
294 unsigned d3, d2, d1, d0; 288 unsigned d3, d2, d1, d0;
295 d1 = (q>>4) & 0xf; 289 d1 = (q>>4) & 0xf;
296 d2 = (q>>8) & 0xf; 290 d2 = (q>>8) & 0xf;
297 d3 = (q>>12); 291 d3 = (q>>12);
298 292
299 d0 = 6*(d3 + d2 + d1) + (q & 0xf); 293 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
300 q = (d0 * 0xcd) >> 11; 294 q = (d0 * 0xcd) >> 11;
301 d0 = d0 - 10*q; 295 d0 = d0 - 10*q;
302 *buf++ = d0 + '0'; /* least significant digit */ 296 *buf++ = d0 + '0'; /* least significant digit */
303 d1 = q + 9*d3 + 5*d2 + d1; 297 d1 = q + 9*d3 + 5*d2 + d1;
304 if (d1 != 0) { 298 if (d1 != 0) {
305 q = (d1 * 0xcd) >> 11; 299 q = (d1 * 0xcd) >> 11;
306 d1 = d1 - 10*q; 300 d1 = d1 - 10*q;
307 *buf++ = d1 + '0'; /* next digit */ 301 *buf++ = d1 + '0'; /* next digit */
308 302
309 d2 = q + 2*d2; 303 d2 = q + 2*d2;
310 if ((d2 != 0) || (d3 != 0)) { 304 if ((d2 != 0) || (d3 != 0)) {
311 q = (d2 * 0xd) >> 7; 305 q = (d2 * 0xd) >> 7;
312 d2 = d2 - 10*q; 306 d2 = d2 - 10*q;
313 *buf++ = d2 + '0'; /* next digit */ 307 *buf++ = d2 + '0'; /* next digit */
314 308
315 d3 = q + 4*d3; 309 d3 = q + 4*d3;
316 if (d3 != 0) { 310 if (d3 != 0) {
317 q = (d3 * 0xcd) >> 11; 311 q = (d3 * 0xcd) >> 11;
318 d3 = d3 - 10*q; 312 d3 = d3 - 10*q;
319 *buf++ = d3 + '0'; /* next digit */ 313 *buf++ = d3 + '0'; /* next digit */
320 if (q != 0) 314 if (q != 0)
321 *buf++ = q + '0'; /* most sign. digit */ 315 *buf++ = q + '0'; /* most sign. digit */
322 } 316 }
323 } 317 }
324 } 318 }
325 319
326 return buf; 320 return buf;
327 } 321 }
328 /* Same with if's removed. Always emits five digits */ 322 /* Same with if's removed. Always emits five digits */
329 static noinline_for_stack 323 static noinline_for_stack
330 char *put_dec_full(char *buf, unsigned q) 324 char *put_dec_full(char *buf, unsigned q)
331 { 325 {
332 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ 326 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
333 /* but anyway, gcc produces better code with full-sized ints */ 327 /* but anyway, gcc produces better code with full-sized ints */
334 unsigned d3, d2, d1, d0; 328 unsigned d3, d2, d1, d0;
335 d1 = (q>>4) & 0xf; 329 d1 = (q>>4) & 0xf;
336 d2 = (q>>8) & 0xf; 330 d2 = (q>>8) & 0xf;
337 d3 = (q>>12); 331 d3 = (q>>12);
338 332
339 /* 333 /*
340 * Possible ways to approx. divide by 10 334 * Possible ways to approx. divide by 10
341 * gcc -O2 replaces multiply with shifts and adds 335 * gcc -O2 replaces multiply with shifts and adds
342 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) 336 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
343 * (x * 0x67) >> 10: 1100111 337 * (x * 0x67) >> 10: 1100111
344 * (x * 0x34) >> 9: 110100 - same 338 * (x * 0x34) >> 9: 110100 - same
345 * (x * 0x1a) >> 8: 11010 - same 339 * (x * 0x1a) >> 8: 11010 - same
346 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) 340 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
347 */ 341 */
348 d0 = 6*(d3 + d2 + d1) + (q & 0xf); 342 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
349 q = (d0 * 0xcd) >> 11; 343 q = (d0 * 0xcd) >> 11;
350 d0 = d0 - 10*q; 344 d0 = d0 - 10*q;
351 *buf++ = d0 + '0'; 345 *buf++ = d0 + '0';
352 d1 = q + 9*d3 + 5*d2 + d1; 346 d1 = q + 9*d3 + 5*d2 + d1;
353 q = (d1 * 0xcd) >> 11; 347 q = (d1 * 0xcd) >> 11;
354 d1 = d1 - 10*q; 348 d1 = d1 - 10*q;
355 *buf++ = d1 + '0'; 349 *buf++ = d1 + '0';
356 350
357 d2 = q + 2*d2; 351 d2 = q + 2*d2;
358 q = (d2 * 0xd) >> 7; 352 q = (d2 * 0xd) >> 7;
359 d2 = d2 - 10*q; 353 d2 = d2 - 10*q;
360 *buf++ = d2 + '0'; 354 *buf++ = d2 + '0';
361 355
362 d3 = q + 4*d3; 356 d3 = q + 4*d3;
363 q = (d3 * 0xcd) >> 11; /* - shorter code */ 357 q = (d3 * 0xcd) >> 11; /* - shorter code */
364 /* q = (d3 * 0x67) >> 10; - would also work */ 358 /* q = (d3 * 0x67) >> 10; - would also work */
365 d3 = d3 - 10*q; 359 d3 = d3 - 10*q;
366 *buf++ = d3 + '0'; 360 *buf++ = d3 + '0';
367 *buf++ = q + '0'; 361 *buf++ = q + '0';
368 362
369 return buf; 363 return buf;
370 } 364 }
371 /* No inlining helps gcc to use registers better */ 365 /* No inlining helps gcc to use registers better */
372 static noinline_for_stack 366 static noinline_for_stack
373 char *put_dec(char *buf, unsigned long long num) 367 char *put_dec(char *buf, unsigned long long num)
374 { 368 {
375 while (1) { 369 while (1) {
376 unsigned rem; 370 unsigned rem;
377 if (num < 100000) 371 if (num < 100000)
378 return put_dec_trunc(buf, num); 372 return put_dec_trunc(buf, num);
379 rem = do_div(num, 100000); 373 rem = do_div(num, 100000);
380 buf = put_dec_full(buf, rem); 374 buf = put_dec_full(buf, rem);
381 } 375 }
382 } 376 }
383 377
384 #define ZEROPAD 1 /* pad with zero */ 378 #define ZEROPAD 1 /* pad with zero */
385 #define SIGN 2 /* unsigned/signed long */ 379 #define SIGN 2 /* unsigned/signed long */
386 #define PLUS 4 /* show plus */ 380 #define PLUS 4 /* show plus */
387 #define SPACE 8 /* space if plus */ 381 #define SPACE 8 /* space if plus */
388 #define LEFT 16 /* left justified */ 382 #define LEFT 16 /* left justified */
389 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 383 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
390 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 384 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
391 385
392 enum format_type { 386 enum format_type {
393 FORMAT_TYPE_NONE, /* Just a string part */ 387 FORMAT_TYPE_NONE, /* Just a string part */
394 FORMAT_TYPE_WIDTH, 388 FORMAT_TYPE_WIDTH,
395 FORMAT_TYPE_PRECISION, 389 FORMAT_TYPE_PRECISION,
396 FORMAT_TYPE_CHAR, 390 FORMAT_TYPE_CHAR,
397 FORMAT_TYPE_STR, 391 FORMAT_TYPE_STR,
398 FORMAT_TYPE_PTR, 392 FORMAT_TYPE_PTR,
399 FORMAT_TYPE_PERCENT_CHAR, 393 FORMAT_TYPE_PERCENT_CHAR,
400 FORMAT_TYPE_INVALID, 394 FORMAT_TYPE_INVALID,
401 FORMAT_TYPE_LONG_LONG, 395 FORMAT_TYPE_LONG_LONG,
402 FORMAT_TYPE_ULONG, 396 FORMAT_TYPE_ULONG,
403 FORMAT_TYPE_LONG, 397 FORMAT_TYPE_LONG,
404 FORMAT_TYPE_UBYTE, 398 FORMAT_TYPE_UBYTE,
405 FORMAT_TYPE_BYTE, 399 FORMAT_TYPE_BYTE,
406 FORMAT_TYPE_USHORT, 400 FORMAT_TYPE_USHORT,
407 FORMAT_TYPE_SHORT, 401 FORMAT_TYPE_SHORT,
408 FORMAT_TYPE_UINT, 402 FORMAT_TYPE_UINT,
409 FORMAT_TYPE_INT, 403 FORMAT_TYPE_INT,
410 FORMAT_TYPE_NRCHARS, 404 FORMAT_TYPE_NRCHARS,
411 FORMAT_TYPE_SIZE_T, 405 FORMAT_TYPE_SIZE_T,
412 FORMAT_TYPE_PTRDIFF 406 FORMAT_TYPE_PTRDIFF
413 }; 407 };
414 408
415 struct printf_spec { 409 struct printf_spec {
416 u8 type; /* format_type enum */ 410 u8 type; /* format_type enum */
417 u8 flags; /* flags to number() */ 411 u8 flags; /* flags to number() */
418 u8 base; /* number base, 8, 10 or 16 only */ 412 u8 base; /* number base, 8, 10 or 16 only */
419 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ 413 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
420 s16 field_width; /* width of output field */ 414 s16 field_width; /* width of output field */
421 s16 precision; /* # of digits/chars */ 415 s16 precision; /* # of digits/chars */
422 }; 416 };
423 417
424 static noinline_for_stack 418 static noinline_for_stack
425 char *number(char *buf, char *end, unsigned long long num, 419 char *number(char *buf, char *end, unsigned long long num,
426 struct printf_spec spec) 420 struct printf_spec spec)
427 { 421 {
428 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ 422 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
429 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ 423 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
430 424
431 char tmp[66]; 425 char tmp[66];
432 char sign; 426 char sign;
433 char locase; 427 char locase;
434 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 428 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
435 int i; 429 int i;
436 430
437 /* locase = 0 or 0x20. ORing digits or letters with 'locase' 431 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
438 * produces same digits or (maybe lowercased) letters */ 432 * produces same digits or (maybe lowercased) letters */
439 locase = (spec.flags & SMALL); 433 locase = (spec.flags & SMALL);
440 if (spec.flags & LEFT) 434 if (spec.flags & LEFT)
441 spec.flags &= ~ZEROPAD; 435 spec.flags &= ~ZEROPAD;
442 sign = 0; 436 sign = 0;
443 if (spec.flags & SIGN) { 437 if (spec.flags & SIGN) {
444 if ((signed long long)num < 0) { 438 if ((signed long long)num < 0) {
445 sign = '-'; 439 sign = '-';
446 num = -(signed long long)num; 440 num = -(signed long long)num;
447 spec.field_width--; 441 spec.field_width--;
448 } else if (spec.flags & PLUS) { 442 } else if (spec.flags & PLUS) {
449 sign = '+'; 443 sign = '+';
450 spec.field_width--; 444 spec.field_width--;
451 } else if (spec.flags & SPACE) { 445 } else if (spec.flags & SPACE) {
452 sign = ' '; 446 sign = ' ';
453 spec.field_width--; 447 spec.field_width--;
454 } 448 }
455 } 449 }
456 if (need_pfx) { 450 if (need_pfx) {
457 spec.field_width--; 451 spec.field_width--;
458 if (spec.base == 16) 452 if (spec.base == 16)
459 spec.field_width--; 453 spec.field_width--;
460 } 454 }
461 455
462 /* generate full string in tmp[], in reverse order */ 456 /* generate full string in tmp[], in reverse order */
463 i = 0; 457 i = 0;
464 if (num == 0) 458 if (num == 0)
465 tmp[i++] = '0'; 459 tmp[i++] = '0';
466 /* Generic code, for any base: 460 /* Generic code, for any base:
467 else do { 461 else do {
468 tmp[i++] = (digits[do_div(num,base)] | locase); 462 tmp[i++] = (digits[do_div(num,base)] | locase);
469 } while (num != 0); 463 } while (num != 0);
470 */ 464 */
471 else if (spec.base != 10) { /* 8 or 16 */ 465 else if (spec.base != 10) { /* 8 or 16 */
472 int mask = spec.base - 1; 466 int mask = spec.base - 1;
473 int shift = 3; 467 int shift = 3;
474 468
475 if (spec.base == 16) 469 if (spec.base == 16)
476 shift = 4; 470 shift = 4;
477 do { 471 do {
478 tmp[i++] = (digits[((unsigned char)num) & mask] | locase); 472 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
479 num >>= shift; 473 num >>= shift;
480 } while (num); 474 } while (num);
481 } else { /* base 10 */ 475 } else { /* base 10 */
482 i = put_dec(tmp, num) - tmp; 476 i = put_dec(tmp, num) - tmp;
483 } 477 }
484 478
485 /* printing 100 using %2d gives "100", not "00" */ 479 /* printing 100 using %2d gives "100", not "00" */
486 if (i > spec.precision) 480 if (i > spec.precision)
487 spec.precision = i; 481 spec.precision = i;
488 /* leading space padding */ 482 /* leading space padding */
489 spec.field_width -= spec.precision; 483 spec.field_width -= spec.precision;
490 if (!(spec.flags & (ZEROPAD+LEFT))) { 484 if (!(spec.flags & (ZEROPAD+LEFT))) {
491 while (--spec.field_width >= 0) { 485 while (--spec.field_width >= 0) {
492 if (buf < end) 486 if (buf < end)
493 *buf = ' '; 487 *buf = ' ';
494 ++buf; 488 ++buf;
495 } 489 }
496 } 490 }
497 /* sign */ 491 /* sign */
498 if (sign) { 492 if (sign) {
499 if (buf < end) 493 if (buf < end)
500 *buf = sign; 494 *buf = sign;
501 ++buf; 495 ++buf;
502 } 496 }
503 /* "0x" / "0" prefix */ 497 /* "0x" / "0" prefix */
504 if (need_pfx) { 498 if (need_pfx) {
505 if (buf < end) 499 if (buf < end)
506 *buf = '0'; 500 *buf = '0';
507 ++buf; 501 ++buf;
508 if (spec.base == 16) { 502 if (spec.base == 16) {
509 if (buf < end) 503 if (buf < end)
510 *buf = ('X' | locase); 504 *buf = ('X' | locase);
511 ++buf; 505 ++buf;
512 } 506 }
513 } 507 }
514 /* zero or space padding */ 508 /* zero or space padding */
515 if (!(spec.flags & LEFT)) { 509 if (!(spec.flags & LEFT)) {
516 char c = (spec.flags & ZEROPAD) ? '0' : ' '; 510 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
517 while (--spec.field_width >= 0) { 511 while (--spec.field_width >= 0) {
518 if (buf < end) 512 if (buf < end)
519 *buf = c; 513 *buf = c;
520 ++buf; 514 ++buf;
521 } 515 }
522 } 516 }
523 /* hmm even more zero padding? */ 517 /* hmm even more zero padding? */
524 while (i <= --spec.precision) { 518 while (i <= --spec.precision) {
525 if (buf < end) 519 if (buf < end)
526 *buf = '0'; 520 *buf = '0';
527 ++buf; 521 ++buf;
528 } 522 }
529 /* actual digits of result */ 523 /* actual digits of result */
530 while (--i >= 0) { 524 while (--i >= 0) {
531 if (buf < end) 525 if (buf < end)
532 *buf = tmp[i]; 526 *buf = tmp[i];
533 ++buf; 527 ++buf;
534 } 528 }
535 /* trailing space padding */ 529 /* trailing space padding */
536 while (--spec.field_width >= 0) { 530 while (--spec.field_width >= 0) {
537 if (buf < end) 531 if (buf < end)
538 *buf = ' '; 532 *buf = ' ';
539 ++buf; 533 ++buf;
540 } 534 }
541 535
542 return buf; 536 return buf;
543 } 537 }
544 538
545 static noinline_for_stack 539 static noinline_for_stack
546 char *string(char *buf, char *end, const char *s, struct printf_spec spec) 540 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
547 { 541 {
548 int len, i; 542 int len, i;
549 543
550 if ((unsigned long)s < PAGE_SIZE) 544 if ((unsigned long)s < PAGE_SIZE)
551 s = "(null)"; 545 s = "(null)";
552 546
553 len = strnlen(s, spec.precision); 547 len = strnlen(s, spec.precision);
554 548
555 if (!(spec.flags & LEFT)) { 549 if (!(spec.flags & LEFT)) {
556 while (len < spec.field_width--) { 550 while (len < spec.field_width--) {
557 if (buf < end) 551 if (buf < end)
558 *buf = ' '; 552 *buf = ' ';
559 ++buf; 553 ++buf;
560 } 554 }
561 } 555 }
562 for (i = 0; i < len; ++i) { 556 for (i = 0; i < len; ++i) {
563 if (buf < end) 557 if (buf < end)
564 *buf = *s; 558 *buf = *s;
565 ++buf; ++s; 559 ++buf; ++s;
566 } 560 }
567 while (len < spec.field_width--) { 561 while (len < spec.field_width--) {
568 if (buf < end) 562 if (buf < end)
569 *buf = ' '; 563 *buf = ' ';
570 ++buf; 564 ++buf;
571 } 565 }
572 566
573 return buf; 567 return buf;
574 } 568 }
575 569
576 static noinline_for_stack 570 static noinline_for_stack
577 char *symbol_string(char *buf, char *end, void *ptr, 571 char *symbol_string(char *buf, char *end, void *ptr,
578 struct printf_spec spec, char ext) 572 struct printf_spec spec, char ext)
579 { 573 {
580 unsigned long value = (unsigned long) ptr; 574 unsigned long value = (unsigned long) ptr;
581 #ifdef CONFIG_KALLSYMS 575 #ifdef CONFIG_KALLSYMS
582 char sym[KSYM_SYMBOL_LEN]; 576 char sym[KSYM_SYMBOL_LEN];
583 if (ext != 'f' && ext != 's') 577 if (ext != 'f' && ext != 's')
584 sprint_symbol(sym, value); 578 sprint_symbol(sym, value);
585 else 579 else
586 kallsyms_lookup(value, NULL, NULL, NULL, sym); 580 kallsyms_lookup(value, NULL, NULL, NULL, sym);
587 581
588 return string(buf, end, sym, spec); 582 return string(buf, end, sym, spec);
589 #else 583 #else
590 spec.field_width = 2 * sizeof(void *); 584 spec.field_width = 2 * sizeof(void *);
591 spec.flags |= SPECIAL | SMALL | ZEROPAD; 585 spec.flags |= SPECIAL | SMALL | ZEROPAD;
592 spec.base = 16; 586 spec.base = 16;
593 587
594 return number(buf, end, value, spec); 588 return number(buf, end, value, spec);
595 #endif 589 #endif
596 } 590 }
597 591
598 static noinline_for_stack 592 static noinline_for_stack
599 char *resource_string(char *buf, char *end, struct resource *res, 593 char *resource_string(char *buf, char *end, struct resource *res,
600 struct printf_spec spec, const char *fmt) 594 struct printf_spec spec, const char *fmt)
601 { 595 {
602 #ifndef IO_RSRC_PRINTK_SIZE 596 #ifndef IO_RSRC_PRINTK_SIZE
603 #define IO_RSRC_PRINTK_SIZE 6 597 #define IO_RSRC_PRINTK_SIZE 6
604 #endif 598 #endif
605 599
606 #ifndef MEM_RSRC_PRINTK_SIZE 600 #ifndef MEM_RSRC_PRINTK_SIZE
607 #define MEM_RSRC_PRINTK_SIZE 10 601 #define MEM_RSRC_PRINTK_SIZE 10
608 #endif 602 #endif
609 static const struct printf_spec io_spec = { 603 static const struct printf_spec io_spec = {
610 .base = 16, 604 .base = 16,
611 .field_width = IO_RSRC_PRINTK_SIZE, 605 .field_width = IO_RSRC_PRINTK_SIZE,
612 .precision = -1, 606 .precision = -1,
613 .flags = SPECIAL | SMALL | ZEROPAD, 607 .flags = SPECIAL | SMALL | ZEROPAD,
614 }; 608 };
615 static const struct printf_spec mem_spec = { 609 static const struct printf_spec mem_spec = {
616 .base = 16, 610 .base = 16,
617 .field_width = MEM_RSRC_PRINTK_SIZE, 611 .field_width = MEM_RSRC_PRINTK_SIZE,
618 .precision = -1, 612 .precision = -1,
619 .flags = SPECIAL | SMALL | ZEROPAD, 613 .flags = SPECIAL | SMALL | ZEROPAD,
620 }; 614 };
621 static const struct printf_spec bus_spec = { 615 static const struct printf_spec bus_spec = {
622 .base = 16, 616 .base = 16,
623 .field_width = 2, 617 .field_width = 2,
624 .precision = -1, 618 .precision = -1,
625 .flags = SMALL | ZEROPAD, 619 .flags = SMALL | ZEROPAD,
626 }; 620 };
627 static const struct printf_spec dec_spec = { 621 static const struct printf_spec dec_spec = {
628 .base = 10, 622 .base = 10,
629 .precision = -1, 623 .precision = -1,
630 .flags = 0, 624 .flags = 0,
631 }; 625 };
632 static const struct printf_spec str_spec = { 626 static const struct printf_spec str_spec = {
633 .field_width = -1, 627 .field_width = -1,
634 .precision = 10, 628 .precision = 10,
635 .flags = LEFT, 629 .flags = LEFT,
636 }; 630 };
637 static const struct printf_spec flag_spec = { 631 static const struct printf_spec flag_spec = {
638 .base = 16, 632 .base = 16,
639 .precision = -1, 633 .precision = -1,
640 .flags = SPECIAL | SMALL, 634 .flags = SPECIAL | SMALL,
641 }; 635 };
642 636
643 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 637 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
644 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 638 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
645 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 639 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
646 #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 640 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
647 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 641 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
648 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 642 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
649 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 643 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
650 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 644 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
651 645
652 char *p = sym, *pend = sym + sizeof(sym); 646 char *p = sym, *pend = sym + sizeof(sym);
653 int decode = (fmt[0] == 'R') ? 1 : 0; 647 int decode = (fmt[0] == 'R') ? 1 : 0;
654 const struct printf_spec *specp; 648 const struct printf_spec *specp;
655 649
656 *p++ = '['; 650 *p++ = '[';
657 if (res->flags & IORESOURCE_IO) { 651 if (res->flags & IORESOURCE_IO) {
658 p = string(p, pend, "io ", str_spec); 652 p = string(p, pend, "io ", str_spec);
659 specp = &io_spec; 653 specp = &io_spec;
660 } else if (res->flags & IORESOURCE_MEM) { 654 } else if (res->flags & IORESOURCE_MEM) {
661 p = string(p, pend, "mem ", str_spec); 655 p = string(p, pend, "mem ", str_spec);
662 specp = &mem_spec; 656 specp = &mem_spec;
663 } else if (res->flags & IORESOURCE_IRQ) { 657 } else if (res->flags & IORESOURCE_IRQ) {
664 p = string(p, pend, "irq ", str_spec); 658 p = string(p, pend, "irq ", str_spec);
665 specp = &dec_spec; 659 specp = &dec_spec;
666 } else if (res->flags & IORESOURCE_DMA) { 660 } else if (res->flags & IORESOURCE_DMA) {
667 p = string(p, pend, "dma ", str_spec); 661 p = string(p, pend, "dma ", str_spec);
668 specp = &dec_spec; 662 specp = &dec_spec;
669 } else if (res->flags & IORESOURCE_BUS) { 663 } else if (res->flags & IORESOURCE_BUS) {
670 p = string(p, pend, "bus ", str_spec); 664 p = string(p, pend, "bus ", str_spec);
671 specp = &bus_spec; 665 specp = &bus_spec;
672 } else { 666 } else {
673 p = string(p, pend, "??? ", str_spec); 667 p = string(p, pend, "??? ", str_spec);
674 specp = &mem_spec; 668 specp = &mem_spec;
675 decode = 0; 669 decode = 0;
676 } 670 }
677 p = number(p, pend, res->start, *specp); 671 p = number(p, pend, res->start, *specp);
678 if (res->start != res->end) { 672 if (res->start != res->end) {
679 *p++ = '-'; 673 *p++ = '-';
680 p = number(p, pend, res->end, *specp); 674 p = number(p, pend, res->end, *specp);
681 } 675 }
682 if (decode) { 676 if (decode) {
683 if (res->flags & IORESOURCE_MEM_64) 677 if (res->flags & IORESOURCE_MEM_64)
684 p = string(p, pend, " 64bit", str_spec); 678 p = string(p, pend, " 64bit", str_spec);
685 if (res->flags & IORESOURCE_PREFETCH) 679 if (res->flags & IORESOURCE_PREFETCH)
686 p = string(p, pend, " pref", str_spec); 680 p = string(p, pend, " pref", str_spec);
687 if (res->flags & IORESOURCE_WINDOW) 681 if (res->flags & IORESOURCE_WINDOW)
688 p = string(p, pend, " window", str_spec); 682 p = string(p, pend, " window", str_spec);
689 if (res->flags & IORESOURCE_DISABLED) 683 if (res->flags & IORESOURCE_DISABLED)
690 p = string(p, pend, " disabled", str_spec); 684 p = string(p, pend, " disabled", str_spec);
691 } else { 685 } else {
692 p = string(p, pend, " flags ", str_spec); 686 p = string(p, pend, " flags ", str_spec);
693 p = number(p, pend, res->flags, flag_spec); 687 p = number(p, pend, res->flags, flag_spec);
694 } 688 }
695 *p++ = ']'; 689 *p++ = ']';
696 *p = '\0'; 690 *p = '\0';
697 691
698 return string(buf, end, sym, spec); 692 return string(buf, end, sym, spec);
699 } 693 }
700 694
701 static noinline_for_stack 695 static noinline_for_stack
702 char *mac_address_string(char *buf, char *end, u8 *addr, 696 char *mac_address_string(char *buf, char *end, u8 *addr,
703 struct printf_spec spec, const char *fmt) 697 struct printf_spec spec, const char *fmt)
704 { 698 {
705 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 699 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
706 char *p = mac_addr; 700 char *p = mac_addr;
707 int i; 701 int i;
708 char separator; 702 char separator;
709 703
710 if (fmt[1] == 'F') { /* FDDI canonical format */ 704 if (fmt[1] == 'F') { /* FDDI canonical format */
711 separator = '-'; 705 separator = '-';
712 } else { 706 } else {
713 separator = ':'; 707 separator = ':';
714 } 708 }
715 709
716 for (i = 0; i < 6; i++) { 710 for (i = 0; i < 6; i++) {
717 p = pack_hex_byte(p, addr[i]); 711 p = pack_hex_byte(p, addr[i]);
718 if (fmt[0] == 'M' && i != 5) 712 if (fmt[0] == 'M' && i != 5)
719 *p++ = separator; 713 *p++ = separator;
720 } 714 }
721 *p = '\0'; 715 *p = '\0';
722 716
723 return string(buf, end, mac_addr, spec); 717 return string(buf, end, mac_addr, spec);
724 } 718 }
725 719
726 static noinline_for_stack 720 static noinline_for_stack
727 char *ip4_string(char *p, const u8 *addr, const char *fmt) 721 char *ip4_string(char *p, const u8 *addr, const char *fmt)
728 { 722 {
729 int i; 723 int i;
730 bool leading_zeros = (fmt[0] == 'i'); 724 bool leading_zeros = (fmt[0] == 'i');
731 int index; 725 int index;
732 int step; 726 int step;
733 727
734 switch (fmt[2]) { 728 switch (fmt[2]) {
735 case 'h': 729 case 'h':
736 #ifdef __BIG_ENDIAN 730 #ifdef __BIG_ENDIAN
737 index = 0; 731 index = 0;
738 step = 1; 732 step = 1;
739 #else 733 #else
740 index = 3; 734 index = 3;
741 step = -1; 735 step = -1;
742 #endif 736 #endif
743 break; 737 break;
744 case 'l': 738 case 'l':
745 index = 3; 739 index = 3;
746 step = -1; 740 step = -1;
747 break; 741 break;
748 case 'n': 742 case 'n':
749 case 'b': 743 case 'b':
750 default: 744 default:
751 index = 0; 745 index = 0;
752 step = 1; 746 step = 1;
753 break; 747 break;
754 } 748 }
755 for (i = 0; i < 4; i++) { 749 for (i = 0; i < 4; i++) {
756 char temp[3]; /* hold each IP quad in reverse order */ 750 char temp[3]; /* hold each IP quad in reverse order */
757 int digits = put_dec_trunc(temp, addr[index]) - temp; 751 int digits = put_dec_trunc(temp, addr[index]) - temp;
758 if (leading_zeros) { 752 if (leading_zeros) {
759 if (digits < 3) 753 if (digits < 3)
760 *p++ = '0'; 754 *p++ = '0';
761 if (digits < 2) 755 if (digits < 2)
762 *p++ = '0'; 756 *p++ = '0';
763 } 757 }
764 /* reverse the digits in the quad */ 758 /* reverse the digits in the quad */
765 while (digits--) 759 while (digits--)
766 *p++ = temp[digits]; 760 *p++ = temp[digits];
767 if (i < 3) 761 if (i < 3)
768 *p++ = '.'; 762 *p++ = '.';
769 index += step; 763 index += step;
770 } 764 }
771 *p = '\0'; 765 *p = '\0';
772 766
773 return p; 767 return p;
774 } 768 }
775 769
776 static noinline_for_stack 770 static noinline_for_stack
777 char *ip6_compressed_string(char *p, const char *addr) 771 char *ip6_compressed_string(char *p, const char *addr)
778 { 772 {
779 int i, j, range; 773 int i, j, range;
780 unsigned char zerolength[8]; 774 unsigned char zerolength[8];
781 int longest = 1; 775 int longest = 1;
782 int colonpos = -1; 776 int colonpos = -1;
783 u16 word; 777 u16 word;
784 u8 hi, lo; 778 u8 hi, lo;
785 bool needcolon = false; 779 bool needcolon = false;
786 bool useIPv4; 780 bool useIPv4;
787 struct in6_addr in6; 781 struct in6_addr in6;
788 782
789 memcpy(&in6, addr, sizeof(struct in6_addr)); 783 memcpy(&in6, addr, sizeof(struct in6_addr));
790 784
791 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 785 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
792 786
793 memset(zerolength, 0, sizeof(zerolength)); 787 memset(zerolength, 0, sizeof(zerolength));
794 788
795 if (useIPv4) 789 if (useIPv4)
796 range = 6; 790 range = 6;
797 else 791 else
798 range = 8; 792 range = 8;
799 793
800 /* find position of longest 0 run */ 794 /* find position of longest 0 run */
801 for (i = 0; i < range; i++) { 795 for (i = 0; i < range; i++) {
802 for (j = i; j < range; j++) { 796 for (j = i; j < range; j++) {
803 if (in6.s6_addr16[j] != 0) 797 if (in6.s6_addr16[j] != 0)
804 break; 798 break;
805 zerolength[i]++; 799 zerolength[i]++;
806 } 800 }
807 } 801 }
808 for (i = 0; i < range; i++) { 802 for (i = 0; i < range; i++) {
809 if (zerolength[i] > longest) { 803 if (zerolength[i] > longest) {
810 longest = zerolength[i]; 804 longest = zerolength[i];
811 colonpos = i; 805 colonpos = i;
812 } 806 }
813 } 807 }
814 808
815 /* emit address */ 809 /* emit address */
816 for (i = 0; i < range; i++) { 810 for (i = 0; i < range; i++) {
817 if (i == colonpos) { 811 if (i == colonpos) {
818 if (needcolon || i == 0) 812 if (needcolon || i == 0)
819 *p++ = ':'; 813 *p++ = ':';
820 *p++ = ':'; 814 *p++ = ':';
821 needcolon = false; 815 needcolon = false;
822 i += longest - 1; 816 i += longest - 1;
823 continue; 817 continue;
824 } 818 }
825 if (needcolon) { 819 if (needcolon) {
826 *p++ = ':'; 820 *p++ = ':';
827 needcolon = false; 821 needcolon = false;
828 } 822 }
829 /* hex u16 without leading 0s */ 823 /* hex u16 without leading 0s */
830 word = ntohs(in6.s6_addr16[i]); 824 word = ntohs(in6.s6_addr16[i]);
831 hi = word >> 8; 825 hi = word >> 8;
832 lo = word & 0xff; 826 lo = word & 0xff;
833 if (hi) { 827 if (hi) {
834 if (hi > 0x0f) 828 if (hi > 0x0f)
835 p = pack_hex_byte(p, hi); 829 p = pack_hex_byte(p, hi);
836 else 830 else
837 *p++ = hex_asc_lo(hi); 831 *p++ = hex_asc_lo(hi);
838 p = pack_hex_byte(p, lo); 832 p = pack_hex_byte(p, lo);
839 } 833 }
840 else if (lo > 0x0f) 834 else if (lo > 0x0f)
841 p = pack_hex_byte(p, lo); 835 p = pack_hex_byte(p, lo);
842 else 836 else
843 *p++ = hex_asc_lo(lo); 837 *p++ = hex_asc_lo(lo);
844 needcolon = true; 838 needcolon = true;
845 } 839 }
846 840
847 if (useIPv4) { 841 if (useIPv4) {
848 if (needcolon) 842 if (needcolon)
849 *p++ = ':'; 843 *p++ = ':';
850 p = ip4_string(p, &in6.s6_addr[12], "I4"); 844 p = ip4_string(p, &in6.s6_addr[12], "I4");
851 } 845 }
852 *p = '\0'; 846 *p = '\0';
853 847
854 return p; 848 return p;
855 } 849 }
856 850
857 static noinline_for_stack 851 static noinline_for_stack
858 char *ip6_string(char *p, const char *addr, const char *fmt) 852 char *ip6_string(char *p, const char *addr, const char *fmt)
859 { 853 {
860 int i; 854 int i;
861 855
862 for (i = 0; i < 8; i++) { 856 for (i = 0; i < 8; i++) {
863 p = pack_hex_byte(p, *addr++); 857 p = pack_hex_byte(p, *addr++);
864 p = pack_hex_byte(p, *addr++); 858 p = pack_hex_byte(p, *addr++);
865 if (fmt[0] == 'I' && i != 7) 859 if (fmt[0] == 'I' && i != 7)
866 *p++ = ':'; 860 *p++ = ':';
867 } 861 }
868 *p = '\0'; 862 *p = '\0';
869 863
870 return p; 864 return p;
871 } 865 }
872 866
873 static noinline_for_stack 867 static noinline_for_stack
874 char *ip6_addr_string(char *buf, char *end, const u8 *addr, 868 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
875 struct printf_spec spec, const char *fmt) 869 struct printf_spec spec, const char *fmt)
876 { 870 {
877 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 871 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
878 872
879 if (fmt[0] == 'I' && fmt[2] == 'c') 873 if (fmt[0] == 'I' && fmt[2] == 'c')
880 ip6_compressed_string(ip6_addr, addr); 874 ip6_compressed_string(ip6_addr, addr);
881 else 875 else
882 ip6_string(ip6_addr, addr, fmt); 876 ip6_string(ip6_addr, addr, fmt);
883 877
884 return string(buf, end, ip6_addr, spec); 878 return string(buf, end, ip6_addr, spec);
885 } 879 }
886 880
887 static noinline_for_stack 881 static noinline_for_stack
888 char *ip4_addr_string(char *buf, char *end, const u8 *addr, 882 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
889 struct printf_spec spec, const char *fmt) 883 struct printf_spec spec, const char *fmt)
890 { 884 {
891 char ip4_addr[sizeof("255.255.255.255")]; 885 char ip4_addr[sizeof("255.255.255.255")];
892 886
893 ip4_string(ip4_addr, addr, fmt); 887 ip4_string(ip4_addr, addr, fmt);
894 888
895 return string(buf, end, ip4_addr, spec); 889 return string(buf, end, ip4_addr, spec);
896 } 890 }
897 891
898 static noinline_for_stack 892 static noinline_for_stack
899 char *uuid_string(char *buf, char *end, const u8 *addr, 893 char *uuid_string(char *buf, char *end, const u8 *addr,
900 struct printf_spec spec, const char *fmt) 894 struct printf_spec spec, const char *fmt)
901 { 895 {
902 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; 896 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
903 char *p = uuid; 897 char *p = uuid;
904 int i; 898 int i;
905 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 899 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
906 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; 900 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
907 const u8 *index = be; 901 const u8 *index = be;
908 bool uc = false; 902 bool uc = false;
909 903
910 switch (*(++fmt)) { 904 switch (*(++fmt)) {
911 case 'L': 905 case 'L':
912 uc = true; /* fall-through */ 906 uc = true; /* fall-through */
913 case 'l': 907 case 'l':
914 index = le; 908 index = le;
915 break; 909 break;
916 case 'B': 910 case 'B':
917 uc = true; 911 uc = true;
918 break; 912 break;
919 } 913 }
920 914
921 for (i = 0; i < 16; i++) { 915 for (i = 0; i < 16; i++) {
922 p = pack_hex_byte(p, addr[index[i]]); 916 p = pack_hex_byte(p, addr[index[i]]);
923 switch (i) { 917 switch (i) {
924 case 3: 918 case 3:
925 case 5: 919 case 5:
926 case 7: 920 case 7:
927 case 9: 921 case 9:
928 *p++ = '-'; 922 *p++ = '-';
929 break; 923 break;
930 } 924 }
931 } 925 }
932 926
933 *p = 0; 927 *p = 0;
934 928
935 if (uc) { 929 if (uc) {
936 p = uuid; 930 p = uuid;
937 do { 931 do {
938 *p = toupper(*p); 932 *p = toupper(*p);
939 } while (*(++p)); 933 } while (*(++p));
940 } 934 }
941 935
942 return string(buf, end, uuid, spec); 936 return string(buf, end, uuid, spec);
943 } 937 }
944 938
945 /* 939 /*
946 * Show a '%p' thing. A kernel extension is that the '%p' is followed 940 * Show a '%p' thing. A kernel extension is that the '%p' is followed
947 * by an extra set of alphanumeric characters that are extended format 941 * by an extra set of alphanumeric characters that are extended format
948 * specifiers. 942 * specifiers.
949 * 943 *
950 * Right now we handle: 944 * Right now we handle:
951 * 945 *
952 * - 'F' For symbolic function descriptor pointers with offset 946 * - 'F' For symbolic function descriptor pointers with offset
953 * - 'f' For simple symbolic function names without offset 947 * - 'f' For simple symbolic function names without offset
954 * - 'S' For symbolic direct pointers with offset 948 * - 'S' For symbolic direct pointers with offset
955 * - 's' For symbolic direct pointers without offset 949 * - 's' For symbolic direct pointers without offset
956 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 950 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
957 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 951 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
958 * - 'M' For a 6-byte MAC address, it prints the address in the 952 * - 'M' For a 6-byte MAC address, it prints the address in the
959 * usual colon-separated hex notation 953 * usual colon-separated hex notation
960 * - 'm' For a 6-byte MAC address, it prints the hex address without colons 954 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
961 * - 'MF' For a 6-byte MAC FDDI address, it prints the address 955 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
962 * with a dash-separated hex notation 956 * with a dash-separated hex notation
963 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 957 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
964 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 958 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
965 * IPv6 uses colon separated network-order 16 bit hex with leading 0's 959 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
966 * - 'i' [46] for 'raw' IPv4/IPv6 addresses 960 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
967 * IPv6 omits the colons (01020304...0f) 961 * IPv6 omits the colons (01020304...0f)
968 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 962 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
969 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order 963 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
970 * - 'I6c' for IPv6 addresses printed as specified by 964 * - 'I6c' for IPv6 addresses printed as specified by
971 * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 965 * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
972 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 966 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
973 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 967 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
974 * Options for %pU are: 968 * Options for %pU are:
975 * b big endian lower case hex (default) 969 * b big endian lower case hex (default)
976 * B big endian UPPER case hex 970 * B big endian UPPER case hex
977 * l little endian lower case hex 971 * l little endian lower case hex
978 * L little endian UPPER case hex 972 * L little endian UPPER case hex
979 * big endian output byte order is: 973 * big endian output byte order is:
980 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 974 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
981 * little endian output byte order is: 975 * little endian output byte order is:
982 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 976 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
983 * - 'V' For a struct va_format which contains a format string * and va_list *, 977 * - 'V' For a struct va_format which contains a format string * and va_list *,
984 * call vsnprintf(->format, *->va_list). 978 * call vsnprintf(->format, *->va_list).
985 * Implements a "recursive vsnprintf". 979 * Implements a "recursive vsnprintf".
986 * Do not use this feature without some mechanism to verify the 980 * Do not use this feature without some mechanism to verify the
987 * correctness of the format string and va_list arguments. 981 * correctness of the format string and va_list arguments.
988 * 982 *
989 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 983 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
990 * function pointers are really function descriptors, which contain a 984 * function pointers are really function descriptors, which contain a
991 * pointer to the real address. 985 * pointer to the real address.
992 */ 986 */
993 static noinline_for_stack 987 static noinline_for_stack
994 char *pointer(const char *fmt, char *buf, char *end, void *ptr, 988 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
995 struct printf_spec spec) 989 struct printf_spec spec)
996 { 990 {
997 if (!ptr) 991 if (!ptr)
998 return string(buf, end, "(null)", spec); 992 return string(buf, end, "(null)", spec);
999 993
1000 switch (*fmt) { 994 switch (*fmt) {
1001 case 'F': 995 case 'F':
1002 case 'f': 996 case 'f':
1003 ptr = dereference_function_descriptor(ptr); 997 ptr = dereference_function_descriptor(ptr);
1004 /* Fallthrough */ 998 /* Fallthrough */
1005 case 'S': 999 case 'S':
1006 case 's': 1000 case 's':
1007 return symbol_string(buf, end, ptr, spec, *fmt); 1001 return symbol_string(buf, end, ptr, spec, *fmt);
1008 case 'R': 1002 case 'R':
1009 case 'r': 1003 case 'r':
1010 return resource_string(buf, end, ptr, spec, fmt); 1004 return resource_string(buf, end, ptr, spec, fmt);
1011 case 'M': /* Colon separated: 00:01:02:03:04:05 */ 1005 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1012 case 'm': /* Contiguous: 000102030405 */ 1006 case 'm': /* Contiguous: 000102030405 */
1013 /* [mM]F (FDDI, bit reversed) */ 1007 /* [mM]F (FDDI, bit reversed) */
1014 return mac_address_string(buf, end, ptr, spec, fmt); 1008 return mac_address_string(buf, end, ptr, spec, fmt);
1015 case 'I': /* Formatted IP supported 1009 case 'I': /* Formatted IP supported
1016 * 4: 1.2.3.4 1010 * 4: 1.2.3.4
1017 * 6: 0001:0203:...:0708 1011 * 6: 0001:0203:...:0708
1018 * 6c: 1::708 or 1::1.2.3.4 1012 * 6c: 1::708 or 1::1.2.3.4
1019 */ 1013 */
1020 case 'i': /* Contiguous: 1014 case 'i': /* Contiguous:
1021 * 4: 001.002.003.004 1015 * 4: 001.002.003.004
1022 * 6: 000102...0f 1016 * 6: 000102...0f
1023 */ 1017 */
1024 switch (fmt[1]) { 1018 switch (fmt[1]) {
1025 case '6': 1019 case '6':
1026 return ip6_addr_string(buf, end, ptr, spec, fmt); 1020 return ip6_addr_string(buf, end, ptr, spec, fmt);
1027 case '4': 1021 case '4':
1028 return ip4_addr_string(buf, end, ptr, spec, fmt); 1022 return ip4_addr_string(buf, end, ptr, spec, fmt);
1029 } 1023 }
1030 break; 1024 break;
1031 case 'U': 1025 case 'U':
1032 return uuid_string(buf, end, ptr, spec, fmt); 1026 return uuid_string(buf, end, ptr, spec, fmt);
1033 case 'V': 1027 case 'V':
1034 return buf + vsnprintf(buf, end - buf, 1028 return buf + vsnprintf(buf, end - buf,
1035 ((struct va_format *)ptr)->fmt, 1029 ((struct va_format *)ptr)->fmt,
1036 *(((struct va_format *)ptr)->va)); 1030 *(((struct va_format *)ptr)->va));
1037 } 1031 }
1038 spec.flags |= SMALL; 1032 spec.flags |= SMALL;
1039 if (spec.field_width == -1) { 1033 if (spec.field_width == -1) {
1040 spec.field_width = 2*sizeof(void *); 1034 spec.field_width = 2*sizeof(void *);
1041 spec.flags |= ZEROPAD; 1035 spec.flags |= ZEROPAD;
1042 } 1036 }
1043 spec.base = 16; 1037 spec.base = 16;
1044 1038
1045 return number(buf, end, (unsigned long) ptr, spec); 1039 return number(buf, end, (unsigned long) ptr, spec);
1046 } 1040 }
1047 1041
1048 /* 1042 /*
1049 * Helper function to decode printf style format. 1043 * Helper function to decode printf style format.
1050 * Each call decode a token from the format and return the 1044 * Each call decode a token from the format and return the
1051 * number of characters read (or likely the delta where it wants 1045 * number of characters read (or likely the delta where it wants
1052 * to go on the next call). 1046 * to go on the next call).
1053 * The decoded token is returned through the parameters 1047 * The decoded token is returned through the parameters
1054 * 1048 *
1055 * 'h', 'l', or 'L' for integer fields 1049 * 'h', 'l', or 'L' for integer fields
1056 * 'z' support added 23/7/1999 S.H. 1050 * 'z' support added 23/7/1999 S.H.
1057 * 'z' changed to 'Z' --davidm 1/25/99 1051 * 'z' changed to 'Z' --davidm 1/25/99
1058 * 't' added for ptrdiff_t 1052 * 't' added for ptrdiff_t
1059 * 1053 *
1060 * @fmt: the format string 1054 * @fmt: the format string
1061 * @type of the token returned 1055 * @type of the token returned
1062 * @flags: various flags such as +, -, # tokens.. 1056 * @flags: various flags such as +, -, # tokens..
1063 * @field_width: overwritten width 1057 * @field_width: overwritten width
1064 * @base: base of the number (octal, hex, ...) 1058 * @base: base of the number (octal, hex, ...)
1065 * @precision: precision of a number 1059 * @precision: precision of a number
1066 * @qualifier: qualifier of a number (long, size_t, ...) 1060 * @qualifier: qualifier of a number (long, size_t, ...)
1067 */ 1061 */
1068 static noinline_for_stack 1062 static noinline_for_stack
1069 int format_decode(const char *fmt, struct printf_spec *spec) 1063 int format_decode(const char *fmt, struct printf_spec *spec)
1070 { 1064 {
1071 const char *start = fmt; 1065 const char *start = fmt;
1072 1066
1073 /* we finished early by reading the field width */ 1067 /* we finished early by reading the field width */
1074 if (spec->type == FORMAT_TYPE_WIDTH) { 1068 if (spec->type == FORMAT_TYPE_WIDTH) {
1075 if (spec->field_width < 0) { 1069 if (spec->field_width < 0) {
1076 spec->field_width = -spec->field_width; 1070 spec->field_width = -spec->field_width;
1077 spec->flags |= LEFT; 1071 spec->flags |= LEFT;
1078 } 1072 }
1079 spec->type = FORMAT_TYPE_NONE; 1073 spec->type = FORMAT_TYPE_NONE;
1080 goto precision; 1074 goto precision;
1081 } 1075 }
1082 1076
1083 /* we finished early by reading the precision */ 1077 /* we finished early by reading the precision */
1084 if (spec->type == FORMAT_TYPE_PRECISION) { 1078 if (spec->type == FORMAT_TYPE_PRECISION) {
1085 if (spec->precision < 0) 1079 if (spec->precision < 0)
1086 spec->precision = 0; 1080 spec->precision = 0;
1087 1081
1088 spec->type = FORMAT_TYPE_NONE; 1082 spec->type = FORMAT_TYPE_NONE;
1089 goto qualifier; 1083 goto qualifier;
1090 } 1084 }
1091 1085
1092 /* By default */ 1086 /* By default */
1093 spec->type = FORMAT_TYPE_NONE; 1087 spec->type = FORMAT_TYPE_NONE;
1094 1088
1095 for (; *fmt ; ++fmt) { 1089 for (; *fmt ; ++fmt) {
1096 if (*fmt == '%') 1090 if (*fmt == '%')
1097 break; 1091 break;
1098 } 1092 }
1099 1093
1100 /* Return the current non-format string */ 1094 /* Return the current non-format string */
1101 if (fmt != start || !*fmt) 1095 if (fmt != start || !*fmt)
1102 return fmt - start; 1096 return fmt - start;
1103 1097
1104 /* Process flags */ 1098 /* Process flags */
1105 spec->flags = 0; 1099 spec->flags = 0;
1106 1100
1107 while (1) { /* this also skips first '%' */ 1101 while (1) { /* this also skips first '%' */
1108 bool found = true; 1102 bool found = true;
1109 1103
1110 ++fmt; 1104 ++fmt;
1111 1105
1112 switch (*fmt) { 1106 switch (*fmt) {
1113 case '-': spec->flags |= LEFT; break; 1107 case '-': spec->flags |= LEFT; break;
1114 case '+': spec->flags |= PLUS; break; 1108 case '+': spec->flags |= PLUS; break;
1115 case ' ': spec->flags |= SPACE; break; 1109 case ' ': spec->flags |= SPACE; break;
1116 case '#': spec->flags |= SPECIAL; break; 1110 case '#': spec->flags |= SPECIAL; break;
1117 case '0': spec->flags |= ZEROPAD; break; 1111 case '0': spec->flags |= ZEROPAD; break;
1118 default: found = false; 1112 default: found = false;
1119 } 1113 }
1120 1114
1121 if (!found) 1115 if (!found)
1122 break; 1116 break;
1123 } 1117 }
1124 1118
1125 /* get field width */ 1119 /* get field width */
1126 spec->field_width = -1; 1120 spec->field_width = -1;
1127 1121
1128 if (isdigit(*fmt)) 1122 if (isdigit(*fmt))
1129 spec->field_width = skip_atoi(&fmt); 1123 spec->field_width = skip_atoi(&fmt);
1130 else if (*fmt == '*') { 1124 else if (*fmt == '*') {
1131 /* it's the next argument */ 1125 /* it's the next argument */
1132 spec->type = FORMAT_TYPE_WIDTH; 1126 spec->type = FORMAT_TYPE_WIDTH;
1133 return ++fmt - start; 1127 return ++fmt - start;
1134 } 1128 }
1135 1129
1136 precision: 1130 precision:
1137 /* get the precision */ 1131 /* get the precision */
1138 spec->precision = -1; 1132 spec->precision = -1;
1139 if (*fmt == '.') { 1133 if (*fmt == '.') {
1140 ++fmt; 1134 ++fmt;
1141 if (isdigit(*fmt)) { 1135 if (isdigit(*fmt)) {
1142 spec->precision = skip_atoi(&fmt); 1136 spec->precision = skip_atoi(&fmt);
1143 if (spec->precision < 0) 1137 if (spec->precision < 0)
1144 spec->precision = 0; 1138 spec->precision = 0;
1145 } else if (*fmt == '*') { 1139 } else if (*fmt == '*') {
1146 /* it's the next argument */ 1140 /* it's the next argument */
1147 spec->type = FORMAT_TYPE_PRECISION; 1141 spec->type = FORMAT_TYPE_PRECISION;
1148 return ++fmt - start; 1142 return ++fmt - start;
1149 } 1143 }
1150 } 1144 }
1151 1145
1152 qualifier: 1146 qualifier:
1153 /* get the conversion qualifier */ 1147 /* get the conversion qualifier */
1154 spec->qualifier = -1; 1148 spec->qualifier = -1;
1155 if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || 1149 if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
1156 TOLOWER(*fmt) == 'z' || *fmt == 't') { 1150 TOLOWER(*fmt) == 'z' || *fmt == 't') {
1157 spec->qualifier = *fmt++; 1151 spec->qualifier = *fmt++;
1158 if (unlikely(spec->qualifier == *fmt)) { 1152 if (unlikely(spec->qualifier == *fmt)) {
1159 if (spec->qualifier == 'l') { 1153 if (spec->qualifier == 'l') {
1160 spec->qualifier = 'L'; 1154 spec->qualifier = 'L';
1161 ++fmt; 1155 ++fmt;
1162 } else if (spec->qualifier == 'h') { 1156 } else if (spec->qualifier == 'h') {
1163 spec->qualifier = 'H'; 1157 spec->qualifier = 'H';
1164 ++fmt; 1158 ++fmt;
1165 } 1159 }
1166 } 1160 }
1167 } 1161 }
1168 1162
1169 /* default base */ 1163 /* default base */
1170 spec->base = 10; 1164 spec->base = 10;
1171 switch (*fmt) { 1165 switch (*fmt) {
1172 case 'c': 1166 case 'c':
1173 spec->type = FORMAT_TYPE_CHAR; 1167 spec->type = FORMAT_TYPE_CHAR;
1174 return ++fmt - start; 1168 return ++fmt - start;
1175 1169
1176 case 's': 1170 case 's':
1177 spec->type = FORMAT_TYPE_STR; 1171 spec->type = FORMAT_TYPE_STR;
1178 return ++fmt - start; 1172 return ++fmt - start;
1179 1173
1180 case 'p': 1174 case 'p':
1181 spec->type = FORMAT_TYPE_PTR; 1175 spec->type = FORMAT_TYPE_PTR;
1182 return fmt - start; 1176 return fmt - start;
1183 /* skip alnum */ 1177 /* skip alnum */
1184 1178
1185 case 'n': 1179 case 'n':
1186 spec->type = FORMAT_TYPE_NRCHARS; 1180 spec->type = FORMAT_TYPE_NRCHARS;
1187 return ++fmt - start; 1181 return ++fmt - start;
1188 1182
1189 case '%': 1183 case '%':
1190 spec->type = FORMAT_TYPE_PERCENT_CHAR; 1184 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1191 return ++fmt - start; 1185 return ++fmt - start;
1192 1186
1193 /* integer number formats - set up the flags and "break" */ 1187 /* integer number formats - set up the flags and "break" */
1194 case 'o': 1188 case 'o':
1195 spec->base = 8; 1189 spec->base = 8;
1196 break; 1190 break;
1197 1191
1198 case 'x': 1192 case 'x':
1199 spec->flags |= SMALL; 1193 spec->flags |= SMALL;
1200 1194
1201 case 'X': 1195 case 'X':
1202 spec->base = 16; 1196 spec->base = 16;
1203 break; 1197 break;
1204 1198
1205 case 'd': 1199 case 'd':
1206 case 'i': 1200 case 'i':
1207 spec->flags |= SIGN; 1201 spec->flags |= SIGN;
1208 case 'u': 1202 case 'u':
1209 break; 1203 break;
1210 1204
1211 default: 1205 default:
1212 spec->type = FORMAT_TYPE_INVALID; 1206 spec->type = FORMAT_TYPE_INVALID;
1213 return fmt - start; 1207 return fmt - start;
1214 } 1208 }
1215 1209
1216 if (spec->qualifier == 'L') 1210 if (spec->qualifier == 'L')
1217 spec->type = FORMAT_TYPE_LONG_LONG; 1211 spec->type = FORMAT_TYPE_LONG_LONG;
1218 else if (spec->qualifier == 'l') { 1212 else if (spec->qualifier == 'l') {
1219 if (spec->flags & SIGN) 1213 if (spec->flags & SIGN)
1220 spec->type = FORMAT_TYPE_LONG; 1214 spec->type = FORMAT_TYPE_LONG;
1221 else 1215 else
1222 spec->type = FORMAT_TYPE_ULONG; 1216 spec->type = FORMAT_TYPE_ULONG;
1223 } else if (TOLOWER(spec->qualifier) == 'z') { 1217 } else if (TOLOWER(spec->qualifier) == 'z') {
1224 spec->type = FORMAT_TYPE_SIZE_T; 1218 spec->type = FORMAT_TYPE_SIZE_T;
1225 } else if (spec->qualifier == 't') { 1219 } else if (spec->qualifier == 't') {
1226 spec->type = FORMAT_TYPE_PTRDIFF; 1220 spec->type = FORMAT_TYPE_PTRDIFF;
1227 } else if (spec->qualifier == 'H') { 1221 } else if (spec->qualifier == 'H') {
1228 if (spec->flags & SIGN) 1222 if (spec->flags & SIGN)
1229 spec->type = FORMAT_TYPE_BYTE; 1223 spec->type = FORMAT_TYPE_BYTE;
1230 else 1224 else
1231 spec->type = FORMAT_TYPE_UBYTE; 1225 spec->type = FORMAT_TYPE_UBYTE;
1232 } else if (spec->qualifier == 'h') { 1226 } else if (spec->qualifier == 'h') {
1233 if (spec->flags & SIGN) 1227 if (spec->flags & SIGN)
1234 spec->type = FORMAT_TYPE_SHORT; 1228 spec->type = FORMAT_TYPE_SHORT;
1235 else 1229 else
1236 spec->type = FORMAT_TYPE_USHORT; 1230 spec->type = FORMAT_TYPE_USHORT;
1237 } else { 1231 } else {
1238 if (spec->flags & SIGN) 1232 if (spec->flags & SIGN)
1239 spec->type = FORMAT_TYPE_INT; 1233 spec->type = FORMAT_TYPE_INT;
1240 else 1234 else
1241 spec->type = FORMAT_TYPE_UINT; 1235 spec->type = FORMAT_TYPE_UINT;
1242 } 1236 }
1243 1237
1244 return ++fmt - start; 1238 return ++fmt - start;
1245 } 1239 }
1246 1240
1247 /** 1241 /**
1248 * vsnprintf - Format a string and place it in a buffer 1242 * vsnprintf - Format a string and place it in a buffer
1249 * @buf: The buffer to place the result into 1243 * @buf: The buffer to place the result into
1250 * @size: The size of the buffer, including the trailing null space 1244 * @size: The size of the buffer, including the trailing null space
1251 * @fmt: The format string to use 1245 * @fmt: The format string to use
1252 * @args: Arguments for the format string 1246 * @args: Arguments for the format string
1253 * 1247 *
1254 * This function follows C99 vsnprintf, but has some extensions: 1248 * This function follows C99 vsnprintf, but has some extensions:
1255 * %pS output the name of a text symbol with offset 1249 * %pS output the name of a text symbol with offset
1256 * %ps output the name of a text symbol without offset 1250 * %ps output the name of a text symbol without offset
1257 * %pF output the name of a function pointer with its offset 1251 * %pF output the name of a function pointer with its offset
1258 * %pf output the name of a function pointer without its offset 1252 * %pf output the name of a function pointer without its offset
1259 * %pR output the address range in a struct resource with decoded flags 1253 * %pR output the address range in a struct resource with decoded flags
1260 * %pr output the address range in a struct resource with raw flags 1254 * %pr output the address range in a struct resource with raw flags
1261 * %pM output a 6-byte MAC address with colons 1255 * %pM output a 6-byte MAC address with colons
1262 * %pm output a 6-byte MAC address without colons 1256 * %pm output a 6-byte MAC address without colons
1263 * %pI4 print an IPv4 address without leading zeros 1257 * %pI4 print an IPv4 address without leading zeros
1264 * %pi4 print an IPv4 address with leading zeros 1258 * %pi4 print an IPv4 address with leading zeros
1265 * %pI6 print an IPv6 address with colons 1259 * %pI6 print an IPv6 address with colons
1266 * %pi6 print an IPv6 address without colons 1260 * %pi6 print an IPv6 address without colons
1267 * %pI6c print an IPv6 address as specified by 1261 * %pI6c print an IPv6 address as specified by
1268 * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 1262 * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
1269 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper 1263 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1270 * case. 1264 * case.
1271 * %n is ignored 1265 * %n is ignored
1272 * 1266 *
1273 * The return value is the number of characters which would 1267 * The return value is the number of characters which would
1274 * be generated for the given input, excluding the trailing 1268 * be generated for the given input, excluding the trailing
1275 * '\0', as per ISO C99. If you want to have the exact 1269 * '\0', as per ISO C99. If you want to have the exact
1276 * number of characters written into @buf as return value 1270 * number of characters written into @buf as return value
1277 * (not including the trailing '\0'), use vscnprintf(). If the 1271 * (not including the trailing '\0'), use vscnprintf(). If the
1278 * return is greater than or equal to @size, the resulting 1272 * return is greater than or equal to @size, the resulting
1279 * string is truncated. 1273 * string is truncated.
1280 * 1274 *
1281 * Call this function if you are already dealing with a va_list. 1275 * Call this function if you are already dealing with a va_list.
1282 * You probably want snprintf() instead. 1276 * You probably want snprintf() instead.
1283 */ 1277 */
1284 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 1278 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1285 { 1279 {
1286 unsigned long long num; 1280 unsigned long long num;
1287 char *str, *end; 1281 char *str, *end;
1288 struct printf_spec spec = {0}; 1282 struct printf_spec spec = {0};
1289 1283
1290 /* Reject out-of-range values early. Large positive sizes are 1284 /* Reject out-of-range values early. Large positive sizes are
1291 used for unknown buffer sizes. */ 1285 used for unknown buffer sizes. */
1292 if (WARN_ON_ONCE((int) size < 0)) 1286 if (WARN_ON_ONCE((int) size < 0))
1293 return 0; 1287 return 0;
1294 1288
1295 str = buf; 1289 str = buf;
1296 end = buf + size; 1290 end = buf + size;
1297 1291
1298 /* Make sure end is always >= buf */ 1292 /* Make sure end is always >= buf */
1299 if (end < buf) { 1293 if (end < buf) {
1300 end = ((void *)-1); 1294 end = ((void *)-1);
1301 size = end - buf; 1295 size = end - buf;
1302 } 1296 }
1303 1297
1304 while (*fmt) { 1298 while (*fmt) {
1305 const char *old_fmt = fmt; 1299 const char *old_fmt = fmt;
1306 int read = format_decode(fmt, &spec); 1300 int read = format_decode(fmt, &spec);
1307 1301
1308 fmt += read; 1302 fmt += read;
1309 1303
1310 switch (spec.type) { 1304 switch (spec.type) {
1311 case FORMAT_TYPE_NONE: { 1305 case FORMAT_TYPE_NONE: {
1312 int copy = read; 1306 int copy = read;
1313 if (str < end) { 1307 if (str < end) {
1314 if (copy > end - str) 1308 if (copy > end - str)
1315 copy = end - str; 1309 copy = end - str;
1316 memcpy(str, old_fmt, copy); 1310 memcpy(str, old_fmt, copy);
1317 } 1311 }
1318 str += read; 1312 str += read;
1319 break; 1313 break;
1320 } 1314 }
1321 1315
1322 case FORMAT_TYPE_WIDTH: 1316 case FORMAT_TYPE_WIDTH:
1323 spec.field_width = va_arg(args, int); 1317 spec.field_width = va_arg(args, int);
1324 break; 1318 break;
1325 1319
1326 case FORMAT_TYPE_PRECISION: 1320 case FORMAT_TYPE_PRECISION:
1327 spec.precision = va_arg(args, int); 1321 spec.precision = va_arg(args, int);
1328 break; 1322 break;
1329 1323
1330 case FORMAT_TYPE_CHAR: { 1324 case FORMAT_TYPE_CHAR: {
1331 char c; 1325 char c;
1332 1326
1333 if (!(spec.flags & LEFT)) { 1327 if (!(spec.flags & LEFT)) {
1334 while (--spec.field_width > 0) { 1328 while (--spec.field_width > 0) {
1335 if (str < end) 1329 if (str < end)
1336 *str = ' '; 1330 *str = ' ';
1337 ++str; 1331 ++str;
1338 1332
1339 } 1333 }
1340 } 1334 }
1341 c = (unsigned char) va_arg(args, int); 1335 c = (unsigned char) va_arg(args, int);
1342 if (str < end) 1336 if (str < end)
1343 *str = c; 1337 *str = c;
1344 ++str; 1338 ++str;
1345 while (--spec.field_width > 0) { 1339 while (--spec.field_width > 0) {
1346 if (str < end) 1340 if (str < end)
1347 *str = ' '; 1341 *str = ' ';
1348 ++str; 1342 ++str;
1349 } 1343 }
1350 break; 1344 break;
1351 } 1345 }
1352 1346
1353 case FORMAT_TYPE_STR: 1347 case FORMAT_TYPE_STR:
1354 str = string(str, end, va_arg(args, char *), spec); 1348 str = string(str, end, va_arg(args, char *), spec);
1355 break; 1349 break;
1356 1350
1357 case FORMAT_TYPE_PTR: 1351 case FORMAT_TYPE_PTR:
1358 str = pointer(fmt+1, str, end, va_arg(args, void *), 1352 str = pointer(fmt+1, str, end, va_arg(args, void *),
1359 spec); 1353 spec);
1360 while (isalnum(*fmt)) 1354 while (isalnum(*fmt))
1361 fmt++; 1355 fmt++;
1362 break; 1356 break;
1363 1357
1364 case FORMAT_TYPE_PERCENT_CHAR: 1358 case FORMAT_TYPE_PERCENT_CHAR:
1365 if (str < end) 1359 if (str < end)
1366 *str = '%'; 1360 *str = '%';
1367 ++str; 1361 ++str;
1368 break; 1362 break;
1369 1363
1370 case FORMAT_TYPE_INVALID: 1364 case FORMAT_TYPE_INVALID:
1371 if (str < end) 1365 if (str < end)
1372 *str = '%'; 1366 *str = '%';
1373 ++str; 1367 ++str;
1374 break; 1368 break;
1375 1369
1376 case FORMAT_TYPE_NRCHARS: { 1370 case FORMAT_TYPE_NRCHARS: {
1377 u8 qualifier = spec.qualifier; 1371 u8 qualifier = spec.qualifier;
1378 1372
1379 if (qualifier == 'l') { 1373 if (qualifier == 'l') {
1380 long *ip = va_arg(args, long *); 1374 long *ip = va_arg(args, long *);
1381 *ip = (str - buf); 1375 *ip = (str - buf);
1382 } else if (TOLOWER(qualifier) == 'z') { 1376 } else if (TOLOWER(qualifier) == 'z') {
1383 size_t *ip = va_arg(args, size_t *); 1377 size_t *ip = va_arg(args, size_t *);
1384 *ip = (str - buf); 1378 *ip = (str - buf);
1385 } else { 1379 } else {
1386 int *ip = va_arg(args, int *); 1380 int *ip = va_arg(args, int *);
1387 *ip = (str - buf); 1381 *ip = (str - buf);
1388 } 1382 }
1389 break; 1383 break;
1390 } 1384 }
1391 1385
1392 default: 1386 default:
1393 switch (spec.type) { 1387 switch (spec.type) {
1394 case FORMAT_TYPE_LONG_LONG: 1388 case FORMAT_TYPE_LONG_LONG:
1395 num = va_arg(args, long long); 1389 num = va_arg(args, long long);
1396 break; 1390 break;
1397 case FORMAT_TYPE_ULONG: 1391 case FORMAT_TYPE_ULONG:
1398 num = va_arg(args, unsigned long); 1392 num = va_arg(args, unsigned long);
1399 break; 1393 break;
1400 case FORMAT_TYPE_LONG: 1394 case FORMAT_TYPE_LONG:
1401 num = va_arg(args, long); 1395 num = va_arg(args, long);
1402 break; 1396 break;
1403 case FORMAT_TYPE_SIZE_T: 1397 case FORMAT_TYPE_SIZE_T:
1404 num = va_arg(args, size_t); 1398 num = va_arg(args, size_t);
1405 break; 1399 break;
1406 case FORMAT_TYPE_PTRDIFF: 1400 case FORMAT_TYPE_PTRDIFF:
1407 num = va_arg(args, ptrdiff_t); 1401 num = va_arg(args, ptrdiff_t);
1408 break; 1402 break;
1409 case FORMAT_TYPE_UBYTE: 1403 case FORMAT_TYPE_UBYTE:
1410 num = (unsigned char) va_arg(args, int); 1404 num = (unsigned char) va_arg(args, int);
1411 break; 1405 break;
1412 case FORMAT_TYPE_BYTE: 1406 case FORMAT_TYPE_BYTE:
1413 num = (signed char) va_arg(args, int); 1407 num = (signed char) va_arg(args, int);
1414 break; 1408 break;
1415 case FORMAT_TYPE_USHORT: 1409 case FORMAT_TYPE_USHORT:
1416 num = (unsigned short) va_arg(args, int); 1410 num = (unsigned short) va_arg(args, int);
1417 break; 1411 break;
1418 case FORMAT_TYPE_SHORT: 1412 case FORMAT_TYPE_SHORT:
1419 num = (short) va_arg(args, int); 1413 num = (short) va_arg(args, int);
1420 break; 1414 break;
1421 case FORMAT_TYPE_INT: 1415 case FORMAT_TYPE_INT:
1422 num = (int) va_arg(args, int); 1416 num = (int) va_arg(args, int);
1423 break; 1417 break;
1424 default: 1418 default:
1425 num = va_arg(args, unsigned int); 1419 num = va_arg(args, unsigned int);
1426 } 1420 }
1427 1421
1428 str = number(str, end, num, spec); 1422 str = number(str, end, num, spec);
1429 } 1423 }
1430 } 1424 }
1431 1425
1432 if (size > 0) { 1426 if (size > 0) {
1433 if (str < end) 1427 if (str < end)
1434 *str = '\0'; 1428 *str = '\0';
1435 else 1429 else
1436 end[-1] = '\0'; 1430 end[-1] = '\0';
1437 } 1431 }
1438 1432
1439 /* the trailing null byte doesn't count towards the total */ 1433 /* the trailing null byte doesn't count towards the total */
1440 return str-buf; 1434 return str-buf;
1441 1435
1442 } 1436 }
1443 EXPORT_SYMBOL(vsnprintf); 1437 EXPORT_SYMBOL(vsnprintf);
1444 1438
1445 /** 1439 /**
1446 * vscnprintf - Format a string and place it in a buffer 1440 * vscnprintf - Format a string and place it in a buffer
1447 * @buf: The buffer to place the result into 1441 * @buf: The buffer to place the result into
1448 * @size: The size of the buffer, including the trailing null space 1442 * @size: The size of the buffer, including the trailing null space
1449 * @fmt: The format string to use 1443 * @fmt: The format string to use
1450 * @args: Arguments for the format string 1444 * @args: Arguments for the format string
1451 * 1445 *
1452 * The return value is the number of characters which have been written into 1446 * The return value is the number of characters which have been written into
1453 * the @buf not including the trailing '\0'. If @size is <= 0 the function 1447 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1454 * returns 0. 1448 * returns 0.
1455 * 1449 *
1456 * Call this function if you are already dealing with a va_list. 1450 * Call this function if you are already dealing with a va_list.
1457 * You probably want scnprintf() instead. 1451 * You probably want scnprintf() instead.
1458 * 1452 *
1459 * See the vsnprintf() documentation for format string extensions over C99. 1453 * See the vsnprintf() documentation for format string extensions over C99.
1460 */ 1454 */
1461 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 1455 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1462 { 1456 {
1463 int i; 1457 int i;
1464 1458
1465 i = vsnprintf(buf, size, fmt, args); 1459 i = vsnprintf(buf, size, fmt, args);
1466 1460
1467 return (i >= size) ? (size - 1) : i; 1461 return (i >= size) ? (size - 1) : i;
1468 } 1462 }
1469 EXPORT_SYMBOL(vscnprintf); 1463 EXPORT_SYMBOL(vscnprintf);
1470 1464
1471 /** 1465 /**
1472 * snprintf - Format a string and place it in a buffer 1466 * snprintf - Format a string and place it in a buffer
1473 * @buf: The buffer to place the result into 1467 * @buf: The buffer to place the result into
1474 * @size: The size of the buffer, including the trailing null space 1468 * @size: The size of the buffer, including the trailing null space
1475 * @fmt: The format string to use 1469 * @fmt: The format string to use
1476 * @...: Arguments for the format string 1470 * @...: Arguments for the format string
1477 * 1471 *
1478 * The return value is the number of characters which would be 1472 * The return value is the number of characters which would be
1479 * generated for the given input, excluding the trailing null, 1473 * generated for the given input, excluding the trailing null,
1480 * as per ISO C99. If the return is greater than or equal to 1474 * as per ISO C99. If the return is greater than or equal to
1481 * @size, the resulting string is truncated. 1475 * @size, the resulting string is truncated.
1482 * 1476 *
1483 * See the vsnprintf() documentation for format string extensions over C99. 1477 * See the vsnprintf() documentation for format string extensions over C99.
1484 */ 1478 */
1485 int snprintf(char *buf, size_t size, const char *fmt, ...) 1479 int snprintf(char *buf, size_t size, const char *fmt, ...)
1486 { 1480 {
1487 va_list args; 1481 va_list args;
1488 int i; 1482 int i;
1489 1483
1490 va_start(args, fmt); 1484 va_start(args, fmt);
1491 i = vsnprintf(buf, size, fmt, args); 1485 i = vsnprintf(buf, size, fmt, args);
1492 va_end(args); 1486 va_end(args);
1493 1487
1494 return i; 1488 return i;
1495 } 1489 }
1496 EXPORT_SYMBOL(snprintf); 1490 EXPORT_SYMBOL(snprintf);
1497 1491
1498 /** 1492 /**
1499 * scnprintf - Format a string and place it in a buffer 1493 * scnprintf - Format a string and place it in a buffer
1500 * @buf: The buffer to place the result into 1494 * @buf: The buffer to place the result into
1501 * @size: The size of the buffer, including the trailing null space 1495 * @size: The size of the buffer, including the trailing null space
1502 * @fmt: The format string to use 1496 * @fmt: The format string to use
1503 * @...: Arguments for the format string 1497 * @...: Arguments for the format string
1504 * 1498 *
1505 * The return value is the number of characters written into @buf not including 1499 * The return value is the number of characters written into @buf not including
1506 * the trailing '\0'. If @size is <= 0 the function returns 0. 1500 * the trailing '\0'. If @size is <= 0 the function returns 0.
1507 */ 1501 */
1508 1502
1509 int scnprintf(char *buf, size_t size, const char *fmt, ...) 1503 int scnprintf(char *buf, size_t size, const char *fmt, ...)
1510 { 1504 {
1511 va_list args; 1505 va_list args;
1512 int i; 1506 int i;
1513 1507
1514 va_start(args, fmt); 1508 va_start(args, fmt);
1515 i = vsnprintf(buf, size, fmt, args); 1509 i = vsnprintf(buf, size, fmt, args);
1516 va_end(args); 1510 va_end(args);
1517 1511
1518 return (i >= size) ? (size - 1) : i; 1512 return (i >= size) ? (size - 1) : i;
1519 } 1513 }
1520 EXPORT_SYMBOL(scnprintf); 1514 EXPORT_SYMBOL(scnprintf);
1521 1515
1522 /** 1516 /**
1523 * vsprintf - Format a string and place it in a buffer 1517 * vsprintf - Format a string and place it in a buffer
1524 * @buf: The buffer to place the result into 1518 * @buf: The buffer to place the result into
1525 * @fmt: The format string to use 1519 * @fmt: The format string to use
1526 * @args: Arguments for the format string 1520 * @args: Arguments for the format string
1527 * 1521 *
1528 * The function returns the number of characters written 1522 * The function returns the number of characters written
1529 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 1523 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1530 * buffer overflows. 1524 * buffer overflows.
1531 * 1525 *
1532 * Call this function if you are already dealing with a va_list. 1526 * Call this function if you are already dealing with a va_list.
1533 * You probably want sprintf() instead. 1527 * You probably want sprintf() instead.
1534 * 1528 *
1535 * See the vsnprintf() documentation for format string extensions over C99. 1529 * See the vsnprintf() documentation for format string extensions over C99.
1536 */ 1530 */
1537 int vsprintf(char *buf, const char *fmt, va_list args) 1531 int vsprintf(char *buf, const char *fmt, va_list args)
1538 { 1532 {
1539 return vsnprintf(buf, INT_MAX, fmt, args); 1533 return vsnprintf(buf, INT_MAX, fmt, args);
1540 } 1534 }
1541 EXPORT_SYMBOL(vsprintf); 1535 EXPORT_SYMBOL(vsprintf);
1542 1536
1543 /** 1537 /**
1544 * sprintf - Format a string and place it in a buffer 1538 * sprintf - Format a string and place it in a buffer
1545 * @buf: The buffer to place the result into 1539 * @buf: The buffer to place the result into
1546 * @fmt: The format string to use 1540 * @fmt: The format string to use
1547 * @...: Arguments for the format string 1541 * @...: Arguments for the format string
1548 * 1542 *
1549 * The function returns the number of characters written 1543 * The function returns the number of characters written
1550 * into @buf. Use snprintf() or scnprintf() in order to avoid 1544 * into @buf. Use snprintf() or scnprintf() in order to avoid
1551 * buffer overflows. 1545 * buffer overflows.
1552 * 1546 *
1553 * See the vsnprintf() documentation for format string extensions over C99. 1547 * See the vsnprintf() documentation for format string extensions over C99.
1554 */ 1548 */
1555 int sprintf(char *buf, const char *fmt, ...) 1549 int sprintf(char *buf, const char *fmt, ...)
1556 { 1550 {
1557 va_list args; 1551 va_list args;
1558 int i; 1552 int i;
1559 1553
1560 va_start(args, fmt); 1554 va_start(args, fmt);
1561 i = vsnprintf(buf, INT_MAX, fmt, args); 1555 i = vsnprintf(buf, INT_MAX, fmt, args);
1562 va_end(args); 1556 va_end(args);
1563 1557
1564 return i; 1558 return i;
1565 } 1559 }
1566 EXPORT_SYMBOL(sprintf); 1560 EXPORT_SYMBOL(sprintf);
1567 1561
1568 #ifdef CONFIG_BINARY_PRINTF 1562 #ifdef CONFIG_BINARY_PRINTF
1569 /* 1563 /*
1570 * bprintf service: 1564 * bprintf service:
1571 * vbin_printf() - VA arguments to binary data 1565 * vbin_printf() - VA arguments to binary data
1572 * bstr_printf() - Binary data to text string 1566 * bstr_printf() - Binary data to text string
1573 */ 1567 */
1574 1568
1575 /** 1569 /**
1576 * vbin_printf - Parse a format string and place args' binary value in a buffer 1570 * vbin_printf - Parse a format string and place args' binary value in a buffer
1577 * @bin_buf: The buffer to place args' binary value 1571 * @bin_buf: The buffer to place args' binary value
1578 * @size: The size of the buffer(by words(32bits), not characters) 1572 * @size: The size of the buffer(by words(32bits), not characters)
1579 * @fmt: The format string to use 1573 * @fmt: The format string to use
1580 * @args: Arguments for the format string 1574 * @args: Arguments for the format string
1581 * 1575 *
1582 * The format follows C99 vsnprintf, except %n is ignored, and its argument 1576 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1583 * is skiped. 1577 * is skiped.
1584 * 1578 *
1585 * The return value is the number of words(32bits) which would be generated for 1579 * The return value is the number of words(32bits) which would be generated for
1586 * the given input. 1580 * the given input.
1587 * 1581 *
1588 * NOTE: 1582 * NOTE:
1589 * If the return value is greater than @size, the resulting bin_buf is NOT 1583 * If the return value is greater than @size, the resulting bin_buf is NOT
1590 * valid for bstr_printf(). 1584 * valid for bstr_printf().
1591 */ 1585 */
1592 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 1586 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1593 { 1587 {
1594 struct printf_spec spec = {0}; 1588 struct printf_spec spec = {0};
1595 char *str, *end; 1589 char *str, *end;
1596 1590
1597 str = (char *)bin_buf; 1591 str = (char *)bin_buf;
1598 end = (char *)(bin_buf + size); 1592 end = (char *)(bin_buf + size);
1599 1593
1600 #define save_arg(type) \ 1594 #define save_arg(type) \
1601 do { \ 1595 do { \
1602 if (sizeof(type) == 8) { \ 1596 if (sizeof(type) == 8) { \
1603 unsigned long long value; \ 1597 unsigned long long value; \
1604 str = PTR_ALIGN(str, sizeof(u32)); \ 1598 str = PTR_ALIGN(str, sizeof(u32)); \
1605 value = va_arg(args, unsigned long long); \ 1599 value = va_arg(args, unsigned long long); \
1606 if (str + sizeof(type) <= end) { \ 1600 if (str + sizeof(type) <= end) { \
1607 *(u32 *)str = *(u32 *)&value; \ 1601 *(u32 *)str = *(u32 *)&value; \
1608 *(u32 *)(str + 4) = *((u32 *)&value + 1); \ 1602 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1609 } \ 1603 } \
1610 } else { \ 1604 } else { \
1611 unsigned long value; \ 1605 unsigned long value; \
1612 str = PTR_ALIGN(str, sizeof(type)); \ 1606 str = PTR_ALIGN(str, sizeof(type)); \
1613 value = va_arg(args, int); \ 1607 value = va_arg(args, int); \
1614 if (str + sizeof(type) <= end) \ 1608 if (str + sizeof(type) <= end) \
1615 *(typeof(type) *)str = (type)value; \ 1609 *(typeof(type) *)str = (type)value; \
1616 } \ 1610 } \
1617 str += sizeof(type); \ 1611 str += sizeof(type); \
1618 } while (0) 1612 } while (0)
1619 1613
1620 while (*fmt) { 1614 while (*fmt) {
1621 int read = format_decode(fmt, &spec); 1615 int read = format_decode(fmt, &spec);
1622 1616
1623 fmt += read; 1617 fmt += read;
1624 1618
1625 switch (spec.type) { 1619 switch (spec.type) {
1626 case FORMAT_TYPE_NONE: 1620 case FORMAT_TYPE_NONE:
1627 case FORMAT_TYPE_INVALID: 1621 case FORMAT_TYPE_INVALID:
1628 case FORMAT_TYPE_PERCENT_CHAR: 1622 case FORMAT_TYPE_PERCENT_CHAR:
1629 break; 1623 break;
1630 1624
1631 case FORMAT_TYPE_WIDTH: 1625 case FORMAT_TYPE_WIDTH:
1632 case FORMAT_TYPE_PRECISION: 1626 case FORMAT_TYPE_PRECISION:
1633 save_arg(int); 1627 save_arg(int);
1634 break; 1628 break;
1635 1629
1636 case FORMAT_TYPE_CHAR: 1630 case FORMAT_TYPE_CHAR:
1637 save_arg(char); 1631 save_arg(char);
1638 break; 1632 break;
1639 1633
1640 case FORMAT_TYPE_STR: { 1634 case FORMAT_TYPE_STR: {
1641 const char *save_str = va_arg(args, char *); 1635 const char *save_str = va_arg(args, char *);
1642 size_t len; 1636 size_t len;
1643 1637
1644 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 1638 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1645 || (unsigned long)save_str < PAGE_SIZE) 1639 || (unsigned long)save_str < PAGE_SIZE)
1646 save_str = "(null)"; 1640 save_str = "(null)";
1647 len = strlen(save_str) + 1; 1641 len = strlen(save_str) + 1;
1648 if (str + len < end) 1642 if (str + len < end)
1649 memcpy(str, save_str, len); 1643 memcpy(str, save_str, len);
1650 str += len; 1644 str += len;
1651 break; 1645 break;
1652 } 1646 }
1653 1647
1654 case FORMAT_TYPE_PTR: 1648 case FORMAT_TYPE_PTR:
1655 save_arg(void *); 1649 save_arg(void *);
1656 /* skip all alphanumeric pointer suffixes */ 1650 /* skip all alphanumeric pointer suffixes */
1657 while (isalnum(*fmt)) 1651 while (isalnum(*fmt))
1658 fmt++; 1652 fmt++;
1659 break; 1653 break;
1660 1654
1661 case FORMAT_TYPE_NRCHARS: { 1655 case FORMAT_TYPE_NRCHARS: {
1662 /* skip %n 's argument */ 1656 /* skip %n 's argument */
1663 u8 qualifier = spec.qualifier; 1657 u8 qualifier = spec.qualifier;
1664 void *skip_arg; 1658 void *skip_arg;
1665 if (qualifier == 'l') 1659 if (qualifier == 'l')
1666 skip_arg = va_arg(args, long *); 1660 skip_arg = va_arg(args, long *);
1667 else if (TOLOWER(qualifier) == 'z') 1661 else if (TOLOWER(qualifier) == 'z')
1668 skip_arg = va_arg(args, size_t *); 1662 skip_arg = va_arg(args, size_t *);
1669 else 1663 else
1670 skip_arg = va_arg(args, int *); 1664 skip_arg = va_arg(args, int *);
1671 break; 1665 break;
1672 } 1666 }
1673 1667
1674 default: 1668 default:
1675 switch (spec.type) { 1669 switch (spec.type) {
1676 1670
1677 case FORMAT_TYPE_LONG_LONG: 1671 case FORMAT_TYPE_LONG_LONG:
1678 save_arg(long long); 1672 save_arg(long long);
1679 break; 1673 break;
1680 case FORMAT_TYPE_ULONG: 1674 case FORMAT_TYPE_ULONG:
1681 case FORMAT_TYPE_LONG: 1675 case FORMAT_TYPE_LONG:
1682 save_arg(unsigned long); 1676 save_arg(unsigned long);
1683 break; 1677 break;
1684 case FORMAT_TYPE_SIZE_T: 1678 case FORMAT_TYPE_SIZE_T:
1685 save_arg(size_t); 1679 save_arg(size_t);
1686 break; 1680 break;
1687 case FORMAT_TYPE_PTRDIFF: 1681 case FORMAT_TYPE_PTRDIFF:
1688 save_arg(ptrdiff_t); 1682 save_arg(ptrdiff_t);
1689 break; 1683 break;
1690 case FORMAT_TYPE_UBYTE: 1684 case FORMAT_TYPE_UBYTE:
1691 case FORMAT_TYPE_BYTE: 1685 case FORMAT_TYPE_BYTE:
1692 save_arg(char); 1686 save_arg(char);
1693 break; 1687 break;
1694 case FORMAT_TYPE_USHORT: 1688 case FORMAT_TYPE_USHORT:
1695 case FORMAT_TYPE_SHORT: 1689 case FORMAT_TYPE_SHORT:
1696 save_arg(short); 1690 save_arg(short);
1697 break; 1691 break;
1698 default: 1692 default:
1699 save_arg(int); 1693 save_arg(int);
1700 } 1694 }
1701 } 1695 }
1702 } 1696 }
1703 1697
1704 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 1698 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1705 #undef save_arg 1699 #undef save_arg
1706 } 1700 }
1707 EXPORT_SYMBOL_GPL(vbin_printf); 1701 EXPORT_SYMBOL_GPL(vbin_printf);
1708 1702
1709 /** 1703 /**
1710 * bstr_printf - Format a string from binary arguments and place it in a buffer 1704 * bstr_printf - Format a string from binary arguments and place it in a buffer
1711 * @buf: The buffer to place the result into 1705 * @buf: The buffer to place the result into
1712 * @size: The size of the buffer, including the trailing null space 1706 * @size: The size of the buffer, including the trailing null space
1713 * @fmt: The format string to use 1707 * @fmt: The format string to use
1714 * @bin_buf: Binary arguments for the format string 1708 * @bin_buf: Binary arguments for the format string
1715 * 1709 *
1716 * This function like C99 vsnprintf, but the difference is that vsnprintf gets 1710 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1717 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 1711 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1718 * a binary buffer that generated by vbin_printf. 1712 * a binary buffer that generated by vbin_printf.
1719 * 1713 *
1720 * The format follows C99 vsnprintf, but has some extensions: 1714 * The format follows C99 vsnprintf, but has some extensions:
1721 * see vsnprintf comment for details. 1715 * see vsnprintf comment for details.
1722 * 1716 *
1723 * The return value is the number of characters which would 1717 * The return value is the number of characters which would
1724 * be generated for the given input, excluding the trailing 1718 * be generated for the given input, excluding the trailing
1725 * '\0', as per ISO C99. If you want to have the exact 1719 * '\0', as per ISO C99. If you want to have the exact
1726 * number of characters written into @buf as return value 1720 * number of characters written into @buf as return value
1727 * (not including the trailing '\0'), use vscnprintf(). If the 1721 * (not including the trailing '\0'), use vscnprintf(). If the
1728 * return is greater than or equal to @size, the resulting 1722 * return is greater than or equal to @size, the resulting
1729 * string is truncated. 1723 * string is truncated.
1730 */ 1724 */
1731 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 1725 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1732 { 1726 {
1733 struct printf_spec spec = {0}; 1727 struct printf_spec spec = {0};
1734 char *str, *end; 1728 char *str, *end;
1735 const char *args = (const char *)bin_buf; 1729 const char *args = (const char *)bin_buf;
1736 1730
1737 if (WARN_ON_ONCE((int) size < 0)) 1731 if (WARN_ON_ONCE((int) size < 0))
1738 return 0; 1732 return 0;
1739 1733
1740 str = buf; 1734 str = buf;
1741 end = buf + size; 1735 end = buf + size;
1742 1736
1743 #define get_arg(type) \ 1737 #define get_arg(type) \
1744 ({ \ 1738 ({ \
1745 typeof(type) value; \ 1739 typeof(type) value; \
1746 if (sizeof(type) == 8) { \ 1740 if (sizeof(type) == 8) { \
1747 args = PTR_ALIGN(args, sizeof(u32)); \ 1741 args = PTR_ALIGN(args, sizeof(u32)); \
1748 *(u32 *)&value = *(u32 *)args; \ 1742 *(u32 *)&value = *(u32 *)args; \
1749 *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 1743 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1750 } else { \ 1744 } else { \
1751 args = PTR_ALIGN(args, sizeof(type)); \ 1745 args = PTR_ALIGN(args, sizeof(type)); \
1752 value = *(typeof(type) *)args; \ 1746 value = *(typeof(type) *)args; \
1753 } \ 1747 } \
1754 args += sizeof(type); \ 1748 args += sizeof(type); \
1755 value; \ 1749 value; \
1756 }) 1750 })
1757 1751
1758 /* Make sure end is always >= buf */ 1752 /* Make sure end is always >= buf */
1759 if (end < buf) { 1753 if (end < buf) {
1760 end = ((void *)-1); 1754 end = ((void *)-1);
1761 size = end - buf; 1755 size = end - buf;
1762 } 1756 }
1763 1757
1764 while (*fmt) { 1758 while (*fmt) {
1765 const char *old_fmt = fmt; 1759 const char *old_fmt = fmt;
1766 int read = format_decode(fmt, &spec); 1760 int read = format_decode(fmt, &spec);
1767 1761
1768 fmt += read; 1762 fmt += read;
1769 1763
1770 switch (spec.type) { 1764 switch (spec.type) {
1771 case FORMAT_TYPE_NONE: { 1765 case FORMAT_TYPE_NONE: {
1772 int copy = read; 1766 int copy = read;
1773 if (str < end) { 1767 if (str < end) {
1774 if (copy > end - str) 1768 if (copy > end - str)
1775 copy = end - str; 1769 copy = end - str;
1776 memcpy(str, old_fmt, copy); 1770 memcpy(str, old_fmt, copy);
1777 } 1771 }
1778 str += read; 1772 str += read;
1779 break; 1773 break;
1780 } 1774 }
1781 1775
1782 case FORMAT_TYPE_WIDTH: 1776 case FORMAT_TYPE_WIDTH:
1783 spec.field_width = get_arg(int); 1777 spec.field_width = get_arg(int);
1784 break; 1778 break;
1785 1779
1786 case FORMAT_TYPE_PRECISION: 1780 case FORMAT_TYPE_PRECISION:
1787 spec.precision = get_arg(int); 1781 spec.precision = get_arg(int);
1788 break; 1782 break;
1789 1783
1790 case FORMAT_TYPE_CHAR: { 1784 case FORMAT_TYPE_CHAR: {
1791 char c; 1785 char c;
1792 1786
1793 if (!(spec.flags & LEFT)) { 1787 if (!(spec.flags & LEFT)) {
1794 while (--spec.field_width > 0) { 1788 while (--spec.field_width > 0) {
1795 if (str < end) 1789 if (str < end)
1796 *str = ' '; 1790 *str = ' ';
1797 ++str; 1791 ++str;
1798 } 1792 }
1799 } 1793 }
1800 c = (unsigned char) get_arg(char); 1794 c = (unsigned char) get_arg(char);
1801 if (str < end) 1795 if (str < end)
1802 *str = c; 1796 *str = c;
1803 ++str; 1797 ++str;
1804 while (--spec.field_width > 0) { 1798 while (--spec.field_width > 0) {
1805 if (str < end) 1799 if (str < end)
1806 *str = ' '; 1800 *str = ' ';
1807 ++str; 1801 ++str;
1808 } 1802 }
1809 break; 1803 break;
1810 } 1804 }
1811 1805
1812 case FORMAT_TYPE_STR: { 1806 case FORMAT_TYPE_STR: {
1813 const char *str_arg = args; 1807 const char *str_arg = args;
1814 args += strlen(str_arg) + 1; 1808 args += strlen(str_arg) + 1;
1815 str = string(str, end, (char *)str_arg, spec); 1809 str = string(str, end, (char *)str_arg, spec);
1816 break; 1810 break;
1817 } 1811 }
1818 1812
1819 case FORMAT_TYPE_PTR: 1813 case FORMAT_TYPE_PTR:
1820 str = pointer(fmt+1, str, end, get_arg(void *), spec); 1814 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1821 while (isalnum(*fmt)) 1815 while (isalnum(*fmt))
1822 fmt++; 1816 fmt++;
1823 break; 1817 break;
1824 1818
1825 case FORMAT_TYPE_PERCENT_CHAR: 1819 case FORMAT_TYPE_PERCENT_CHAR:
1826 case FORMAT_TYPE_INVALID: 1820 case FORMAT_TYPE_INVALID:
1827 if (str < end) 1821 if (str < end)
1828 *str = '%'; 1822 *str = '%';
1829 ++str; 1823 ++str;
1830 break; 1824 break;
1831 1825
1832 case FORMAT_TYPE_NRCHARS: 1826 case FORMAT_TYPE_NRCHARS:
1833 /* skip */ 1827 /* skip */
1834 break; 1828 break;
1835 1829
1836 default: { 1830 default: {
1837 unsigned long long num; 1831 unsigned long long num;
1838 1832
1839 switch (spec.type) { 1833 switch (spec.type) {
1840 1834
1841 case FORMAT_TYPE_LONG_LONG: 1835 case FORMAT_TYPE_LONG_LONG:
1842 num = get_arg(long long); 1836 num = get_arg(long long);
1843 break; 1837 break;
1844 case FORMAT_TYPE_ULONG: 1838 case FORMAT_TYPE_ULONG:
1845 case FORMAT_TYPE_LONG: 1839 case FORMAT_TYPE_LONG:
1846 num = get_arg(unsigned long); 1840 num = get_arg(unsigned long);
1847 break; 1841 break;
1848 case FORMAT_TYPE_SIZE_T: 1842 case FORMAT_TYPE_SIZE_T:
1849 num = get_arg(size_t); 1843 num = get_arg(size_t);
1850 break; 1844 break;
1851 case FORMAT_TYPE_PTRDIFF: 1845 case FORMAT_TYPE_PTRDIFF:
1852 num = get_arg(ptrdiff_t); 1846 num = get_arg(ptrdiff_t);
1853 break; 1847 break;
1854 case FORMAT_TYPE_UBYTE: 1848 case FORMAT_TYPE_UBYTE:
1855 num = get_arg(unsigned char); 1849 num = get_arg(unsigned char);
1856 break; 1850 break;
1857 case FORMAT_TYPE_BYTE: 1851 case FORMAT_TYPE_BYTE:
1858 num = get_arg(signed char); 1852 num = get_arg(signed char);
1859 break; 1853 break;
1860 case FORMAT_TYPE_USHORT: 1854 case FORMAT_TYPE_USHORT:
1861 num = get_arg(unsigned short); 1855 num = get_arg(unsigned short);
1862 break; 1856 break;
1863 case FORMAT_TYPE_SHORT: 1857 case FORMAT_TYPE_SHORT:
1864 num = get_arg(short); 1858 num = get_arg(short);
1865 break; 1859 break;
1866 case FORMAT_TYPE_UINT: 1860 case FORMAT_TYPE_UINT:
1867 num = get_arg(unsigned int); 1861 num = get_arg(unsigned int);
1868 break; 1862 break;
1869 default: 1863 default:
1870 num = get_arg(int); 1864 num = get_arg(int);
1871 } 1865 }
1872 1866
1873 str = number(str, end, num, spec); 1867 str = number(str, end, num, spec);
1874 } /* default: */ 1868 } /* default: */
1875 } /* switch(spec.type) */ 1869 } /* switch(spec.type) */
1876 } /* while(*fmt) */ 1870 } /* while(*fmt) */
1877 1871
1878 if (size > 0) { 1872 if (size > 0) {
1879 if (str < end) 1873 if (str < end)
1880 *str = '\0'; 1874 *str = '\0';
1881 else 1875 else
1882 end[-1] = '\0'; 1876 end[-1] = '\0';
1883 } 1877 }
1884 1878
1885 #undef get_arg 1879 #undef get_arg
1886 1880
1887 /* the trailing null byte doesn't count towards the total */ 1881 /* the trailing null byte doesn't count towards the total */
1888 return str - buf; 1882 return str - buf;
1889 } 1883 }
1890 EXPORT_SYMBOL_GPL(bstr_printf); 1884 EXPORT_SYMBOL_GPL(bstr_printf);
1891 1885
1892 /** 1886 /**
1893 * bprintf - Parse a format string and place args' binary value in a buffer 1887 * bprintf - Parse a format string and place args' binary value in a buffer
1894 * @bin_buf: The buffer to place args' binary value 1888 * @bin_buf: The buffer to place args' binary value
1895 * @size: The size of the buffer(by words(32bits), not characters) 1889 * @size: The size of the buffer(by words(32bits), not characters)
1896 * @fmt: The format string to use 1890 * @fmt: The format string to use
1897 * @...: Arguments for the format string 1891 * @...: Arguments for the format string
1898 * 1892 *
1899 * The function returns the number of words(u32) written 1893 * The function returns the number of words(u32) written
1900 * into @bin_buf. 1894 * into @bin_buf.
1901 */ 1895 */
1902 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 1896 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1903 { 1897 {
1904 va_list args; 1898 va_list args;
1905 int ret; 1899 int ret;
1906 1900
1907 va_start(args, fmt); 1901 va_start(args, fmt);
1908 ret = vbin_printf(bin_buf, size, fmt, args); 1902 ret = vbin_printf(bin_buf, size, fmt, args);
1909 va_end(args); 1903 va_end(args);
1910 1904
1911 return ret; 1905 return ret;
1912 } 1906 }
1913 EXPORT_SYMBOL_GPL(bprintf); 1907 EXPORT_SYMBOL_GPL(bprintf);
1914 1908
1915 #endif /* CONFIG_BINARY_PRINTF */ 1909 #endif /* CONFIG_BINARY_PRINTF */
1916 1910
1917 /** 1911 /**
1918 * vsscanf - Unformat a buffer into a list of arguments 1912 * vsscanf - Unformat a buffer into a list of arguments
1919 * @buf: input buffer 1913 * @buf: input buffer
1920 * @fmt: format of buffer 1914 * @fmt: format of buffer
1921 * @args: arguments 1915 * @args: arguments
1922 */ 1916 */
1923 int vsscanf(const char *buf, const char *fmt, va_list args) 1917 int vsscanf(const char *buf, const char *fmt, va_list args)
1924 { 1918 {
1925 const char *str = buf; 1919 const char *str = buf;
1926 char *next; 1920 char *next;
1927 char digit; 1921 char digit;
1928 int num = 0; 1922 int num = 0;
1929 u8 qualifier; 1923 u8 qualifier;
1930 u8 base; 1924 u8 base;
1931 s16 field_width; 1925 s16 field_width;
1932 bool is_sign; 1926 bool is_sign;
1933 1927
1934 while (*fmt && *str) { 1928 while (*fmt && *str) {
1935 /* skip any white space in format */ 1929 /* skip any white space in format */
1936 /* white space in format matchs any amount of 1930 /* white space in format matchs any amount of
1937 * white space, including none, in the input. 1931 * white space, including none, in the input.
1938 */ 1932 */
1939 if (isspace(*fmt)) { 1933 if (isspace(*fmt)) {
1940 fmt = skip_spaces(++fmt); 1934 fmt = skip_spaces(++fmt);
1941 str = skip_spaces(str); 1935 str = skip_spaces(str);
1942 } 1936 }
1943 1937
1944 /* anything that is not a conversion must match exactly */ 1938 /* anything that is not a conversion must match exactly */
1945 if (*fmt != '%' && *fmt) { 1939 if (*fmt != '%' && *fmt) {
1946 if (*fmt++ != *str++) 1940 if (*fmt++ != *str++)
1947 break; 1941 break;
1948 continue; 1942 continue;
1949 } 1943 }
1950 1944
1951 if (!*fmt) 1945 if (!*fmt)
1952 break; 1946 break;
1953 ++fmt; 1947 ++fmt;
1954 1948
1955 /* skip this conversion. 1949 /* skip this conversion.
1956 * advance both strings to next white space 1950 * advance both strings to next white space
1957 */ 1951 */
1958 if (*fmt == '*') { 1952 if (*fmt == '*') {
1959 while (!isspace(*fmt) && *fmt != '%' && *fmt) 1953 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1960 fmt++; 1954 fmt++;
1961 while (!isspace(*str) && *str) 1955 while (!isspace(*str) && *str)
1962 str++; 1956 str++;
1963 continue; 1957 continue;
1964 } 1958 }
1965 1959
1966 /* get field width */ 1960 /* get field width */
1967 field_width = -1; 1961 field_width = -1;
1968 if (isdigit(*fmt)) 1962 if (isdigit(*fmt))
1969 field_width = skip_atoi(&fmt); 1963 field_width = skip_atoi(&fmt);
1970 1964
1971 /* get conversion qualifier */ 1965 /* get conversion qualifier */
1972 qualifier = -1; 1966 qualifier = -1;
1973 if (*fmt == 'h' || TOLOWER(*fmt) == 'l' || 1967 if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
1974 TOLOWER(*fmt) == 'z') { 1968 TOLOWER(*fmt) == 'z') {
1975 qualifier = *fmt++; 1969 qualifier = *fmt++;
1976 if (unlikely(qualifier == *fmt)) { 1970 if (unlikely(qualifier == *fmt)) {
1977 if (qualifier == 'h') { 1971 if (qualifier == 'h') {
1978 qualifier = 'H'; 1972 qualifier = 'H';
1979 fmt++; 1973 fmt++;
1980 } else if (qualifier == 'l') { 1974 } else if (qualifier == 'l') {
1981 qualifier = 'L'; 1975 qualifier = 'L';
1982 fmt++; 1976 fmt++;
1983 } 1977 }
1984 } 1978 }
1985 } 1979 }
1986 1980
1987 if (!*fmt || !*str) 1981 if (!*fmt || !*str)
1988 break; 1982 break;
1989 1983
1990 base = 10; 1984 base = 10;
1991 is_sign = 0; 1985 is_sign = 0;
1992 1986
1993 switch (*fmt++) { 1987 switch (*fmt++) {
1994 case 'c': 1988 case 'c':
1995 { 1989 {
1996 char *s = (char *)va_arg(args, char*); 1990 char *s = (char *)va_arg(args, char*);
1997 if (field_width == -1) 1991 if (field_width == -1)
1998 field_width = 1; 1992 field_width = 1;
1999 do { 1993 do {
2000 *s++ = *str++; 1994 *s++ = *str++;
2001 } while (--field_width > 0 && *str); 1995 } while (--field_width > 0 && *str);
2002 num++; 1996 num++;
2003 } 1997 }
2004 continue; 1998 continue;
2005 case 's': 1999 case 's':
2006 { 2000 {
2007 char *s = (char *)va_arg(args, char *); 2001 char *s = (char *)va_arg(args, char *);
2008 if (field_width == -1) 2002 if (field_width == -1)
2009 field_width = SHRT_MAX; 2003 field_width = SHRT_MAX;
2010 /* first, skip leading white space in buffer */ 2004 /* first, skip leading white space in buffer */
2011 str = skip_spaces(str); 2005 str = skip_spaces(str);
2012 2006
2013 /* now copy until next white space */ 2007 /* now copy until next white space */
2014 while (*str && !isspace(*str) && field_width--) 2008 while (*str && !isspace(*str) && field_width--)
2015 *s++ = *str++; 2009 *s++ = *str++;
2016 *s = '\0'; 2010 *s = '\0';
2017 num++; 2011 num++;
2018 } 2012 }
2019 continue; 2013 continue;
2020 case 'n': 2014 case 'n':
2021 /* return number of characters read so far */ 2015 /* return number of characters read so far */
2022 { 2016 {
2023 int *i = (int *)va_arg(args, int*); 2017 int *i = (int *)va_arg(args, int*);
2024 *i = str - buf; 2018 *i = str - buf;
2025 } 2019 }
2026 continue; 2020 continue;
2027 case 'o': 2021 case 'o':
2028 base = 8; 2022 base = 8;
2029 break; 2023 break;
2030 case 'x': 2024 case 'x':
2031 case 'X': 2025 case 'X':
2032 base = 16; 2026 base = 16;
2033 break; 2027 break;
2034 case 'i': 2028 case 'i':
2035 base = 0; 2029 base = 0;
2036 case 'd': 2030 case 'd':
2037 is_sign = 1; 2031 is_sign = 1;
2038 case 'u': 2032 case 'u':
2039 break; 2033 break;
2040 case '%': 2034 case '%':
2041 /* looking for '%' in str */ 2035 /* looking for '%' in str */
2042 if (*str++ != '%') 2036 if (*str++ != '%')
2043 return num; 2037 return num;
2044 continue; 2038 continue;
2045 default: 2039 default:
2046 /* invalid format; stop here */ 2040 /* invalid format; stop here */
2047 return num; 2041 return num;
2048 } 2042 }
2049 2043
2050 /* have some sort of integer conversion. 2044 /* have some sort of integer conversion.
2051 * first, skip white space in buffer. 2045 * first, skip white space in buffer.
2052 */ 2046 */
2053 str = skip_spaces(str); 2047 str = skip_spaces(str);
2054 2048
2055 digit = *str; 2049 digit = *str;
2056 if (is_sign && digit == '-') 2050 if (is_sign && digit == '-')
2057 digit = *(str + 1); 2051 digit = *(str + 1);
2058 2052
2059 if (!digit 2053 if (!digit
2060 || (base == 16 && !isxdigit(digit)) 2054 || (base == 16 && !isxdigit(digit))
2061 || (base == 10 && !isdigit(digit)) 2055 || (base == 10 && !isdigit(digit))
2062 || (base == 8 && (!isdigit(digit) || digit > '7')) 2056 || (base == 8 && (!isdigit(digit) || digit > '7'))
2063 || (base == 0 && !isdigit(digit))) 2057 || (base == 0 && !isdigit(digit)))
2064 break; 2058 break;
2065 2059
2066 switch (qualifier) { 2060 switch (qualifier) {
2067 case 'H': /* that's 'hh' in format */ 2061 case 'H': /* that's 'hh' in format */
2068 if (is_sign) { 2062 if (is_sign) {
2069 signed char *s = (signed char *)va_arg(args, signed char *); 2063 signed char *s = (signed char *)va_arg(args, signed char *);
2070 *s = (signed char)simple_strtol(str, &next, base); 2064 *s = (signed char)simple_strtol(str, &next, base);
2071 } else { 2065 } else {
2072 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *); 2066 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2073 *s = (unsigned char)simple_strtoul(str, &next, base); 2067 *s = (unsigned char)simple_strtoul(str, &next, base);
2074 } 2068 }
2075 break; 2069 break;
2076 case 'h': 2070 case 'h':
2077 if (is_sign) { 2071 if (is_sign) {
2078 short *s = (short *)va_arg(args, short *); 2072 short *s = (short *)va_arg(args, short *);
2079 *s = (short)simple_strtol(str, &next, base); 2073 *s = (short)simple_strtol(str, &next, base);
2080 } else { 2074 } else {
2081 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *); 2075 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2082 *s = (unsigned short)simple_strtoul(str, &next, base); 2076 *s = (unsigned short)simple_strtoul(str, &next, base);
2083 } 2077 }
2084 break; 2078 break;
2085 case 'l': 2079 case 'l':
2086 if (is_sign) { 2080 if (is_sign) {
2087 long *l = (long *)va_arg(args, long *); 2081 long *l = (long *)va_arg(args, long *);
2088 *l = simple_strtol(str, &next, base); 2082 *l = simple_strtol(str, &next, base);
2089 } else { 2083 } else {
2090 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *); 2084 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2091 *l = simple_strtoul(str, &next, base); 2085 *l = simple_strtoul(str, &next, base);
2092 } 2086 }
2093 break; 2087 break;
2094 case 'L': 2088 case 'L':
2095 if (is_sign) { 2089 if (is_sign) {
2096 long long *l = (long long *)va_arg(args, long long *); 2090 long long *l = (long long *)va_arg(args, long long *);
2097 *l = simple_strtoll(str, &next, base); 2091 *l = simple_strtoll(str, &next, base);
2098 } else { 2092 } else {
2099 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *); 2093 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2100 *l = simple_strtoull(str, &next, base); 2094 *l = simple_strtoull(str, &next, base);
2101 } 2095 }
2102 break; 2096 break;
2103 case 'Z': 2097 case 'Z':
2104 case 'z': 2098 case 'z':
2105 { 2099 {
2106 size_t *s = (size_t *)va_arg(args, size_t *); 2100 size_t *s = (size_t *)va_arg(args, size_t *);
2107 *s = (size_t)simple_strtoul(str, &next, base); 2101 *s = (size_t)simple_strtoul(str, &next, base);
2108 } 2102 }
2109 break; 2103 break;
2110 default: 2104 default:
2111 if (is_sign) { 2105 if (is_sign) {
2112 int *i = (int *)va_arg(args, int *); 2106 int *i = (int *)va_arg(args, int *);
2113 *i = (int)simple_strtol(str, &next, base); 2107 *i = (int)simple_strtol(str, &next, base);
2114 } else { 2108 } else {
2115 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*); 2109 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2116 *i = (unsigned int)simple_strtoul(str, &next, base); 2110 *i = (unsigned int)simple_strtoul(str, &next, base);
2117 } 2111 }
2118 break; 2112 break;
2119 } 2113 }
2120 num++; 2114 num++;
2121 2115
2122 if (!next) 2116 if (!next)
2123 break; 2117 break;
2124 str = next; 2118 str = next;
2125 } 2119 }
2126 2120
2127 /* 2121 /*
2128 * Now we've come all the way through so either the input string or the 2122 * Now we've come all the way through so either the input string or the
2129 * format ended. In the former case, there can be a %n at the current 2123 * format ended. In the former case, there can be a %n at the current
2130 * position in the format that needs to be filled. 2124 * position in the format that needs to be filled.
2131 */ 2125 */
2132 if (*fmt == '%' && *(fmt + 1) == 'n') { 2126 if (*fmt == '%' && *(fmt + 1) == 'n') {
2133 int *p = (int *)va_arg(args, int *); 2127 int *p = (int *)va_arg(args, int *);
2134 *p = str - buf; 2128 *p = str - buf;
2135 } 2129 }
2136 2130
2137 return num; 2131 return num;
2138 } 2132 }
2139 EXPORT_SYMBOL(vsscanf); 2133 EXPORT_SYMBOL(vsscanf);
2140 2134
2141 /** 2135 /**
2142 * sscanf - Unformat a buffer into a list of arguments 2136 * sscanf - Unformat a buffer into a list of arguments
2143 * @buf: input buffer 2137 * @buf: input buffer
2144 * @fmt: formatting of buffer 2138 * @fmt: formatting of buffer
2145 * @...: resulting arguments 2139 * @...: resulting arguments
2146 */ 2140 */
2147 int sscanf(const char *buf, const char *fmt, ...) 2141 int sscanf(const char *buf, const char *fmt, ...)
2148 { 2142 {
2149 va_list args; 2143 va_list args;
2150 int i; 2144 int i;
2151 2145
2152 va_start(args, fmt); 2146 va_start(args, fmt);
2153 i = vsscanf(buf, fmt, args); 2147 i = vsscanf(buf, fmt, args);
2154 va_end(args); 2148 va_end(args);
2155 2149
2156 return i; 2150 return i;
2157 } 2151 }
2158 EXPORT_SYMBOL(sscanf); 2152 EXPORT_SYMBOL(sscanf);
2159 2153