]> git.sur5r.net Git - u-boot/blob - lib_generic/vsprintf.c
Add LZO decompressor support
[u-boot] / lib_generic / vsprintf.c
1 /*
2  *  linux/lib/vsprintf.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8 /*
9  * Wirzenius wrote this portably, Torvalds fucked it up :-)
10  */
11
12 #include <stdarg.h>
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
16
17 #include <common.h>
18 #if !defined (CONFIG_PANIC_HANG)
19 #include <command.h>
20 /*cmd_boot.c*/
21 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
22 #endif
23
24 #ifdef CONFIG_SYS_64BIT_VSPRINTF
25 # define NUM_TYPE long long
26 #else
27 # define NUM_TYPE long
28 #endif
29 #define noinline __attribute__((noinline))
30
31 #define do_div(n, base) ({ \
32         unsigned int __res; \
33         __res = ((unsigned NUM_TYPE) n) % base; \
34         n = ((unsigned NUM_TYPE) n) / base; \
35         __res; \
36 })
37
38 const char hex_asc[] = "0123456789abcdef";
39 #define hex_asc_lo(x)   hex_asc[((x) & 0x0f)]
40 #define hex_asc_hi(x)   hex_asc[((x) & 0xf0) >> 4]
41
42 static inline char *pack_hex_byte(char *buf, u8 byte)
43 {
44         *buf++ = hex_asc_hi(byte);
45         *buf++ = hex_asc_lo(byte);
46         return buf;
47 }
48
49 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
50 {
51         unsigned long result = 0,value;
52
53         if (*cp == '0') {
54                 cp++;
55                 if ((*cp == 'x') && isxdigit(cp[1])) {
56                         base = 16;
57                         cp++;
58                 }
59                 if (!base) {
60                         base = 8;
61                 }
62         }
63         if (!base) {
64                 base = 10;
65         }
66         while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
67             ? toupper(*cp) : *cp)-'A'+10) < base) {
68                 result = result*base + value;
69                 cp++;
70         }
71         if (endp)
72                 *endp = (char *)cp;
73         return result;
74 }
75
76 long simple_strtol(const char *cp,char **endp,unsigned int base)
77 {
78         if(*cp=='-')
79                 return -simple_strtoul(cp+1,endp,base);
80         return simple_strtoul(cp,endp,base);
81 }
82
83 int ustrtoul(const char *cp, char **endp, unsigned int base)
84 {
85         unsigned long result = simple_strtoul(cp, endp, base);
86         switch (**endp) {
87         case 'G' :
88                 result *= 1024;
89                 /* fall through */
90         case 'M':
91                 result *= 1024;
92                 /* fall through */
93         case 'K':
94         case 'k':
95                 result *= 1024;
96                 if ((*endp)[1] == 'i') {
97                         if ((*endp)[2] == 'B')
98                                 (*endp) += 3;
99                         else
100                                 (*endp) += 2;
101                 }
102         }
103         return result;
104 }
105
106 #ifdef CONFIG_SYS_64BIT_STRTOUL
107 unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
108 {
109         unsigned long long result = 0, value;
110
111         if (*cp == '0') {
112                 cp++;
113                 if ((*cp == 'x') && isxdigit (cp[1])) {
114                         base = 16;
115                         cp++;
116                 }
117                 if (!base) {
118                         base = 8;
119                 }
120         }
121         if (!base) {
122                 base = 10;
123         }
124         while (isxdigit (*cp) && (value = isdigit (*cp)
125                                 ? *cp - '0'
126                                 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
127                 result = result * base + value;
128                 cp++;
129         }
130         if (endp)
131                 *endp = (char *) cp;
132         return result;
133 }
134 #endif /* CONFIG_SYS_64BIT_STRTOUL */
135
136 /* we use this so that we can do without the ctype library */
137 #define is_digit(c)     ((c) >= '0' && (c) <= '9')
138
139 static int skip_atoi(const char **s)
140 {
141         int i=0;
142
143         while (is_digit(**s))
144                 i = i*10 + *((*s)++) - '0';
145         return i;
146 }
147
148 /* Decimal conversion is by far the most typical, and is used
149  * for /proc and /sys data. This directly impacts e.g. top performance
150  * with many processes running. We optimize it for speed
151  * using code from
152  * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
153  * (with permission from the author, Douglas W. Jones). */
154
155 /* Formats correctly any integer in [0,99999].
156  * Outputs from one to five digits depending on input.
157  * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
158 static char* put_dec_trunc(char *buf, unsigned q)
159 {
160         unsigned d3, d2, d1, d0;
161         d1 = (q>>4) & 0xf;
162         d2 = (q>>8) & 0xf;
163         d3 = (q>>12);
164
165         d0 = 6*(d3 + d2 + d1) + (q & 0xf);
166         q = (d0 * 0xcd) >> 11;
167         d0 = d0 - 10*q;
168         *buf++ = d0 + '0'; /* least significant digit */
169         d1 = q + 9*d3 + 5*d2 + d1;
170         if (d1 != 0) {
171                 q = (d1 * 0xcd) >> 11;
172                 d1 = d1 - 10*q;
173                 *buf++ = d1 + '0'; /* next digit */
174
175                 d2 = q + 2*d2;
176                 if ((d2 != 0) || (d3 != 0)) {
177                         q = (d2 * 0xd) >> 7;
178                         d2 = d2 - 10*q;
179                         *buf++ = d2 + '0'; /* next digit */
180
181                         d3 = q + 4*d3;
182                         if (d3 != 0) {
183                                 q = (d3 * 0xcd) >> 11;
184                                 d3 = d3 - 10*q;
185                                 *buf++ = d3 + '0';  /* next digit */
186                                 if (q != 0)
187                                         *buf++ = q + '0';  /* most sign. digit */
188                         }
189                 }
190         }
191         return buf;
192 }
193 /* Same with if's removed. Always emits five digits */
194 static char* put_dec_full(char *buf, unsigned q)
195 {
196         /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
197         /* but anyway, gcc produces better code with full-sized ints */
198         unsigned d3, d2, d1, d0;
199         d1 = (q>>4) & 0xf;
200         d2 = (q>>8) & 0xf;
201         d3 = (q>>12);
202
203         /* Possible ways to approx. divide by 10 */
204         /* gcc -O2 replaces multiply with shifts and adds */
205         // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
206         // (x * 0x67) >> 10:  1100111
207         // (x * 0x34) >> 9:    110100 - same
208         // (x * 0x1a) >> 8:     11010 - same
209         // (x * 0x0d) >> 7:      1101 - same, shortest code (on i386)
210
211         d0 = 6*(d3 + d2 + d1) + (q & 0xf);
212         q = (d0 * 0xcd) >> 11;
213         d0 = d0 - 10*q;
214         *buf++ = d0 + '0';
215         d1 = q + 9*d3 + 5*d2 + d1;
216                 q = (d1 * 0xcd) >> 11;
217                 d1 = d1 - 10*q;
218                 *buf++ = d1 + '0';
219
220                 d2 = q + 2*d2;
221                         q = (d2 * 0xd) >> 7;
222                         d2 = d2 - 10*q;
223                         *buf++ = d2 + '0';
224
225                         d3 = q + 4*d3;
226                                 q = (d3 * 0xcd) >> 11; /* - shorter code */
227                                 /* q = (d3 * 0x67) >> 10; - would also work */
228                                 d3 = d3 - 10*q;
229                                 *buf++ = d3 + '0';
230                                         *buf++ = q + '0';
231         return buf;
232 }
233 /* No inlining helps gcc to use registers better */
234 static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
235 {
236         while (1) {
237                 unsigned rem;
238                 if (num < 100000)
239                         return put_dec_trunc(buf, num);
240                 rem = do_div(num, 100000);
241                 buf = put_dec_full(buf, rem);
242         }
243 }
244
245 #define ZEROPAD 1               /* pad with zero */
246 #define SIGN    2               /* unsigned/signed long */
247 #define PLUS    4               /* show plus */
248 #define SPACE   8               /* space if plus */
249 #define LEFT    16              /* left justified */
250 #define SMALL   32              /* Must be 32 == 0x20 */
251 #define SPECIAL 64              /* 0x */
252
253 static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
254 {
255         /* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
256         static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
257
258         char tmp[66];
259         char sign;
260         char locase;
261         int need_pfx = ((type & SPECIAL) && base != 10);
262         int i;
263
264         /* locase = 0 or 0x20. ORing digits or letters with 'locase'
265          * produces same digits or (maybe lowercased) letters */
266         locase = (type & SMALL);
267         if (type & LEFT)
268                 type &= ~ZEROPAD;
269         sign = 0;
270         if (type & SIGN) {
271                 if ((signed NUM_TYPE) num < 0) {
272                         sign = '-';
273                         num = - (signed NUM_TYPE) num;
274                         size--;
275                 } else if (type & PLUS) {
276                         sign = '+';
277                         size--;
278                 } else if (type & SPACE) {
279                         sign = ' ';
280                         size--;
281                 }
282         }
283         if (need_pfx) {
284                 size--;
285                 if (base == 16)
286                         size--;
287         }
288
289         /* generate full string in tmp[], in reverse order */
290         i = 0;
291         if (num == 0)
292                 tmp[i++] = '0';
293         /* Generic code, for any base:
294         else do {
295                 tmp[i++] = (digits[do_div(num,base)] | locase);
296         } while (num != 0);
297         */
298         else if (base != 10) { /* 8 or 16 */
299                 int mask = base - 1;
300                 int shift = 3;
301                 if (base == 16) shift = 4;
302                 do {
303                         tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
304                         num >>= shift;
305                 } while (num);
306         } else { /* base 10 */
307                 i = put_dec(tmp, num) - tmp;
308         }
309
310         /* printing 100 using %2d gives "100", not "00" */
311         if (i > precision)
312                 precision = i;
313         /* leading space padding */
314         size -= precision;
315         if (!(type & (ZEROPAD+LEFT)))
316                 while(--size >= 0)
317                         *buf++ = ' ';
318         /* sign */
319         if (sign)
320                 *buf++ = sign;
321         /* "0x" / "0" prefix */
322         if (need_pfx) {
323                 *buf++ = '0';
324                 if (base == 16)
325                         *buf++ = ('X' | locase);
326         }
327         /* zero or space padding */
328         if (!(type & LEFT)) {
329                 char c = (type & ZEROPAD) ? '0' : ' ';
330                 while (--size >= 0)
331                         *buf++ = c;
332         }
333         /* hmm even more zero padding? */
334         while (i <= --precision)
335                 *buf++ = '0';
336         /* actual digits of result */
337         while (--i >= 0)
338                 *buf++ = tmp[i];
339         /* trailing space padding */
340         while (--size >= 0)
341                 *buf++ = ' ';
342         return buf;
343 }
344
345 static char *string(char *buf, char *s, int field_width, int precision, int flags)
346 {
347         int len, i;
348
349         if (s == 0)
350                 s = "<NULL>";
351
352         len = strnlen(s, precision);
353
354         if (!(flags & LEFT))
355                 while (len < field_width--)
356                         *buf++ = ' ';
357         for (i = 0; i < len; ++i)
358                 *buf++ = *s++;
359         while (len < field_width--)
360                 *buf++ = ' ';
361         return buf;
362 }
363
364 #ifdef CONFIG_CMD_NET
365 static char *mac_address_string(char *buf, u8 *addr, int field_width,
366                                 int precision, int flags)
367 {
368         char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
369         char *p = mac_addr;
370         int i;
371
372         for (i = 0; i < 6; i++) {
373                 p = pack_hex_byte(p, addr[i]);
374                 if (!(flags & SPECIAL) && i != 5)
375                         *p++ = ':';
376         }
377         *p = '\0';
378
379         return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
380 }
381
382 static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
383                          int precision, int flags)
384 {
385         char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
386         char *p = ip6_addr;
387         int i;
388
389         for (i = 0; i < 8; i++) {
390                 p = pack_hex_byte(p, addr[2 * i]);
391                 p = pack_hex_byte(p, addr[2 * i + 1]);
392                 if (!(flags & SPECIAL) && i != 7)
393                         *p++ = ':';
394         }
395         *p = '\0';
396
397         return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
398 }
399
400 static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
401                          int precision, int flags)
402 {
403         char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
404         char temp[3];   /* hold each IP quad in reverse order */
405         char *p = ip4_addr;
406         int i, digits;
407
408         for (i = 0; i < 4; i++) {
409                 digits = put_dec_trunc(temp, addr[i]) - temp;
410                 /* reverse the digits in the quad */
411                 while (digits--)
412                         *p++ = temp[digits];
413                 if (i != 3)
414                         *p++ = '.';
415         }
416         *p = '\0';
417
418         return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
419 }
420 #endif
421
422 /*
423  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
424  * by an extra set of alphanumeric characters that are extended format
425  * specifiers.
426  *
427  * Right now we handle:
428  *
429  * - 'M' For a 6-byte MAC address, it prints the address in the
430  *       usual colon-separated hex notation
431  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
432  *       decimal for v4 and colon separated network-order 16 bit hex for v6)
433  * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
434  *       currently the same
435  *
436  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
437  * function pointers are really function descriptors, which contain a
438  * pointer to the real address.
439  */
440 static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
441 {
442         if (!ptr)
443                 return string(buf, "(null)", field_width, precision, flags);
444
445 #ifdef CONFIG_CMD_NET
446         switch (*fmt) {
447         case 'm':
448                 flags |= SPECIAL;
449                 /* Fallthrough */
450         case 'M':
451                 return mac_address_string(buf, ptr, field_width, precision, flags);
452         case 'i':
453                 flags |= SPECIAL;
454                 /* Fallthrough */
455         case 'I':
456                 if (fmt[1] == '6')
457                         return ip6_addr_string(buf, ptr, field_width, precision, flags);
458                 if (fmt[1] == '4')
459                         return ip4_addr_string(buf, ptr, field_width, precision, flags);
460                 flags &= ~SPECIAL;
461                 break;
462         }
463 #endif
464         flags |= SMALL;
465         if (field_width == -1) {
466                 field_width = 2*sizeof(void *);
467                 flags |= ZEROPAD;
468         }
469         return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
470 }
471
472 /**
473  * vsprintf - Format a string and place it in a buffer
474  * @buf: The buffer to place the result into
475  * @fmt: The format string to use
476  * @args: Arguments for the format string
477  *
478  * This function follows C99 vsprintf, but has some extensions:
479  * %pS output the name of a text symbol
480  * %pF output the name of a function pointer
481  * %pR output the address range in a struct resource
482  *
483  * The function returns the number of characters written
484  * into @buf.
485  *
486  * Call this function if you are already dealing with a va_list.
487  * You probably want sprintf() instead.
488  */
489 int vsprintf(char *buf, const char *fmt, va_list args)
490 {
491         unsigned NUM_TYPE num;
492         int base;
493         char *str;
494
495         int flags;              /* flags to number() */
496
497         int field_width;        /* width of output field */
498         int precision;          /* min. # of digits for integers; max
499                                    number of chars for from string */
500         int qualifier;          /* 'h', 'l', or 'L' for integer fields */
501                                 /* 'z' support added 23/7/1999 S.H.    */
502                                 /* 'z' changed to 'Z' --davidm 1/25/99 */
503                                 /* 't' added for ptrdiff_t */
504
505         str = buf;
506
507         for (; *fmt ; ++fmt) {
508                 if (*fmt != '%') {
509                         *str++ = *fmt;
510                         continue;
511                 }
512
513                 /* process flags */
514                 flags = 0;
515                 repeat:
516                         ++fmt;          /* this also skips first '%' */
517                         switch (*fmt) {
518                                 case '-': flags |= LEFT; goto repeat;
519                                 case '+': flags |= PLUS; goto repeat;
520                                 case ' ': flags |= SPACE; goto repeat;
521                                 case '#': flags |= SPECIAL; goto repeat;
522                                 case '0': flags |= ZEROPAD; goto repeat;
523                         }
524
525                 /* get field width */
526                 field_width = -1;
527                 if (is_digit(*fmt))
528                         field_width = skip_atoi(&fmt);
529                 else if (*fmt == '*') {
530                         ++fmt;
531                         /* it's the next argument */
532                         field_width = va_arg(args, int);
533                         if (field_width < 0) {
534                                 field_width = -field_width;
535                                 flags |= LEFT;
536                         }
537                 }
538
539                 /* get the precision */
540                 precision = -1;
541                 if (*fmt == '.') {
542                         ++fmt;
543                         if (is_digit(*fmt))
544                                 precision = skip_atoi(&fmt);
545                         else if (*fmt == '*') {
546                                 ++fmt;
547                                 /* it's the next argument */
548                                 precision = va_arg(args, int);
549                         }
550                         if (precision < 0)
551                                 precision = 0;
552                 }
553
554                 /* get the conversion qualifier */
555                 qualifier = -1;
556                 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
557                     *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
558                         qualifier = *fmt;
559                         ++fmt;
560                         if (qualifier == 'l' && *fmt == 'l') {
561                                 qualifier = 'L';
562                                 ++fmt;
563                         }
564                 }
565
566                 /* default base */
567                 base = 10;
568
569                 switch (*fmt) {
570                 case 'c':
571                         if (!(flags & LEFT))
572                                 while (--field_width > 0)
573                                         *str++ = ' ';
574                         *str++ = (unsigned char) va_arg(args, int);
575                         while (--field_width > 0)
576                                 *str++ = ' ';
577                         continue;
578
579                 case 's':
580                         str = string(str, va_arg(args, char *), field_width, precision, flags);
581                         continue;
582
583                 case 'p':
584                         str = pointer(fmt+1, str,
585                                         va_arg(args, void *),
586                                         field_width, precision, flags);
587                         /* Skip all alphanumeric pointer suffixes */
588                         while (isalnum(fmt[1]))
589                                 fmt++;
590                         continue;
591
592                 case 'n':
593                         if (qualifier == 'l') {
594                                 long * ip = va_arg(args, long *);
595                                 *ip = (str - buf);
596                         } else {
597                                 int * ip = va_arg(args, int *);
598                                 *ip = (str - buf);
599                         }
600                         continue;
601
602                 case '%':
603                         *str++ = '%';
604                         continue;
605
606                 /* integer number formats - set up the flags and "break" */
607                 case 'o':
608                         base = 8;
609                         break;
610
611                 case 'x':
612                         flags |= SMALL;
613                 case 'X':
614                         base = 16;
615                         break;
616
617                 case 'd':
618                 case 'i':
619                         flags |= SIGN;
620                 case 'u':
621                         break;
622
623                 default:
624                         *str++ = '%';
625                         if (*fmt)
626                                 *str++ = *fmt;
627                         else
628                                 --fmt;
629                         continue;
630                 }
631 #ifdef CONFIG_SYS_64BIT_VSPRINTF
632                 if (qualifier == 'L')  /* "quad" for 64 bit variables */
633                         num = va_arg(args, unsigned long long);
634                 else
635 #endif
636                 if (qualifier == 'l') {
637                         num = va_arg(args, unsigned long);
638                         if (flags & SIGN)
639                                 num = (signed long) num;
640                 } else if (qualifier == 'Z' || qualifier == 'z') {
641                         num = va_arg(args, size_t);
642                 } else if (qualifier == 't') {
643                         num = va_arg(args, ptrdiff_t);
644                 } else if (qualifier == 'h') {
645                         num = (unsigned short) va_arg(args, int);
646                         if (flags & SIGN)
647                                 num = (signed short) num;
648                 } else {
649                         num = va_arg(args, unsigned int);
650                         if (flags & SIGN)
651                                 num = (signed int) num;
652                 }
653                 str = number(str, num, base, field_width, precision, flags);
654         }
655         *str = '\0';
656         return str-buf;
657 }
658
659 /**
660  * sprintf - Format a string and place it in a buffer
661  * @buf: The buffer to place the result into
662  * @fmt: The format string to use
663  * @...: Arguments for the format string
664  *
665  * The function returns the number of characters written
666  * into @buf.
667  *
668  * See the vsprintf() documentation for format string extensions over C99.
669  */
670 int sprintf(char * buf, const char *fmt, ...)
671 {
672         va_list args;
673         int i;
674
675         va_start(args, fmt);
676         i=vsprintf(buf,fmt,args);
677         va_end(args);
678         return i;
679 }
680
681 void panic(const char *fmt, ...)
682 {
683         va_list args;
684         va_start(args, fmt);
685         vprintf(fmt, args);
686         putc('\n');
687         va_end(args);
688 #if defined (CONFIG_PANIC_HANG)
689         hang();
690 #else
691         udelay (100000);        /* allow messages to go out */
692         do_reset (NULL, 0, 0, NULL);
693 #endif
694 }